Bug 1759372 - Show POP3 activity message in status bar. r=mkmelin
authorPing Chen <remotenonsense@gmail.com>
Wed, 16 Mar 2022 12:31:30 +0200
changeset 35192 ad1eb74d45ccf7cc8bad9316bfc410b9d2fffcb4
parent 35191 dbc775a37ad66abf3655c914edf472595b987f36
child 35193 d721be96a806be4e7492cc04ae850ac4b1ec45f5
push id19741
push usermkmelin@iki.fi
push dateWed, 16 Mar 2022 10:38:42 +0000
treeherdercomm-central@2f1f333cfcf0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmkmelin
bugs1759372
Bug 1759372 - Show POP3 activity message in status bar. r=mkmelin Differential Revision: https://phabricator.services.mozilla.com/D141077
mailnews/local/src/Pop3Client.jsm
--- a/mailnews/local/src/Pop3Client.jsm
+++ b/mailnews/local/src/Pop3Client.jsm
@@ -413,16 +413,18 @@ class Pop3Client {
   }
 
   /**
    * Send `CAPA` request to the server.
    */
   _actionCapa = () => {
     this._nextAction = this._actionCapaResponse;
     this._capabilities = [];
+    this._newMessageDownloaded = 0;
+    this._newMessageTotal = 0;
     this._send("CAPA");
   };
 
   /**
    * Handle `CAPA` response.
    * @param {Pop3Response} res - CAPA response received from the server.
    */
   _actionCapaResponse = res => {
@@ -480,16 +482,17 @@ class Pop3Client {
       !this._supportedAuthMethods.length &&
       this._server.authMethod == Ci.nsMsgAuthMethod.passwordCleartext
     ) {
       this._possibleAuthMethods.unshift("USERPASS");
       this._nextAuthMethod = "USERPASS";
     }
 
     if (this._nextAuthMethod) {
+      this._updateStatus("hostContact");
       this._actionAuth();
       return;
     }
 
     // Preferred auth methods don't match any supported auth methods. Give user
     // some hints to change the config.
     if (
       this._server.authMethod == Ci.nsMsgAuthMethod.passwordCleartext &&
@@ -923,16 +926,17 @@ class Pop3Client {
               messageNumber,
               status: uidlState.status,
             });
           } else {
             // Do nothing to this message.
             this._newUidlMap.set(uidl, uidlState);
           }
         } else {
+          this._newMessageTotal++;
           // Fetch the full message or only headers depending on server settings
           // and message size.
           let status =
             this._capabilities.includes("TOP") &&
             (this._server.headersOnly ||
               this._messageSizeMap.get(messageNumber) > this._maxMessageSize)
               ? UIDL_TOO_BIG
               : UIDL_FETCH_BODY;
@@ -1036,16 +1040,20 @@ class Pop3Client {
 
   /**
    * Send `TOP` request to the server.
    */
   _actionTop = () => {
     this._nextAction = this._actionTopResponse;
     let lineNumber = this._server.headersOnly ? 0 : 20;
     this._send(`TOP ${this._currentMessage.messageNumber} ${lineNumber}`);
+    this._updateStatus("receivingMessages", [
+      ++this._newMessageDownloaded,
+      this._newMessageTotal,
+    ]);
   };
 
   /**
    * Handle `TOP` response.
    * @param {Pop3Response} res - TOP response received from the server.
    */
   _actionTopResponse = res => {
     if (res.status) {
@@ -1094,16 +1102,20 @@ class Pop3Client {
   };
 
   /**
    * Send `RETR` request to the server.
    */
   _actionRetr = () => {
     this._nextAction = this._actionRetrResponse;
     this._send(`RETR ${this._currentMessage.messageNumber}`);
+    this._updateStatus("receivingMessages", [
+      ++this._newMessageDownloaded,
+      this._newMessageTotal,
+    ]);
   };
 
   /**
    * Handle `RETR` response.
    * @param {Pop3Response} res - RETR response received from the server.
    */
   _actionRetrResponse = res => {
     if (res.status) {
@@ -1208,25 +1220,62 @@ class Pop3Client {
     let errorTitle = bundle.formatStringFromName("pop3ErrorDialogTitle", [
       this._server.prettyName,
     ]);
     this._msgWindow.promptDialog.alert(errorTitle, errorMsg);
   }
 
   _actionDone = (status = Cr.NS_OK) => {
     this._authenticating = false;
-    if (status != Cr.NS_OK) {
+    if (status == Cr.NS_OK) {
+      if (this._newMessageTotal) {
+        this._updateStatus("receivedMsgs", [
+          this._newMessageTotal,
+          this._newMessageTotal,
+        ]);
+      } else {
+        this._updateStatus("noNewMessages");
+      }
+    } else {
       this._sink.abortMailDelivery(this);
       if (this._currentMessage) {
         // Put _currentMessage back to the queue to prevent loss of popstate.
         this._messagesToHandle.unshift(this._currentMessage);
       }
     }
     this._writeUidlState();
     this.urlListener?.OnStopRunningUrl(this.runningUri, status);
     this.quit();
   };
 
+  /**
+   * Show a status message in the status bar.
+   * @param {string} statusName - A string name in localMsgs.properties.
+   * @param {string[]} [params] - Params to format the string.
+   */
+  _updateStatus(statusName, params) {
+    if (!this._msgWindow?.statusFeedback) {
+      return;
+    }
+    if (!this._localBundle) {
+      this._localBundle = Services.strings.createBundle(
+        "chrome://messenger/locale/localMsgs.properties"
+      );
+      this._messengerBundle = Services.strings.createBundle(
+        "chrome://messenger/locale/messenger.properties"
+      );
+    }
+    let status = params
+      ? this._localBundle.formatStringFromName(statusName, params)
+      : this._localBundle.GetStringFromName(statusName);
+    this._msgWindow.statusFeedback.showStatusString(
+      this._messengerBundle.formatStringFromName("statusMessage", [
+        this._server.prettyName,
+        status,
+      ])
+    );
+  }
+
   /** @see nsIPop3Protocol */
   checkMessage(uidl) {
     return this._uidlMap.has(uidl);
   }
 }