Bug 636199 - Port |Bug 195702 - attachment size should be visible in compose window| (and bug 634620). r+moa=Mnyromyr
authorJens Hatlak <jh@junetz.de>
Wed, 09 Mar 2011 22:16:37 +0100
changeset 7292 34dc4195f5fd2fb8b77b7fb4ba326996e9c216ff
parent 7291 e1e05d692fc4ff52c03cbbbd75e8dfd4303bb42b
child 7293 06cf8a4770ac5397f91dbb2a3915247b2851984a
push idunknown
push userunknown
push dateunknown
bugs636199, 195702, 634620
Bug 636199 - Port |Bug 195702 - attachment size should be visible in compose window| (and bug 634620). r+moa=Mnyromyr
suite/mailnews/compose/MsgComposeCommands.js
suite/mailnews/msgHdrViewOverlay.js
--- a/suite/mailnews/compose/MsgComposeCommands.js
+++ b/suite/mailnews/compose/MsgComposeCommands.js
@@ -72,16 +72,18 @@ var sNameProperty = null;
 var sDictCount = 0;
 
 /* Create message window object. This is use by mail-offline.js and therefore should not be renamed. We need to avoid doing
    this kind of cross file global stuff in the future and instead pass this object as parameter when needed by function
    in the other js file.
 */
 var msgWindow = Components.classes["@mozilla.org/messenger/msgwindow;1"]
                           .createInstance(Components.interfaces.nsIMsgWindow);
+var gMessenger = Components.classes["@mozilla.org/messenger;1"]
+                           .createInstance(Components.interfaces.nsIMessenger);
 
 /**
  * Global variables, need to be re-initialized every time mostly because we need to release them when the window close
  */
 var gHideMenus;
 var gMsgCompose;
 var gAccountManager;
 var gIOService;
@@ -2579,16 +2581,17 @@ function AttachFiles(attachments)
     var ioService = Components.classes["@mozilla.org/network/io-service;1"]
     ioService = ioService.getService(Components.interfaces.nsIIOService);
     var fileHandler = ioService.getProtocolHandler("file").QueryInterface(Components.interfaces.nsIFileProtocolHandler);
     var currentAttachment = fileHandler.getURLSpecFromFile(currentFile);
 
     if (!DuplicateFileCheck(currentAttachment)) {
       var attachment = Components.classes["@mozilla.org/messengercompose/attachment;1"].createInstance(Components.interfaces.nsIMsgAttachment);
       attachment.url = currentAttachment;
+      attachment.size = currentFile.fileSize;
       AddAttachment(attachment);
       gContentChanged = true;
     }
   }
   return firstAttachedFile;
 }
 
 function AddAttachment(attachment)
@@ -2609,25 +2612,29 @@ function AddAttachment(attachment)
     else {
       // for security reasons, don't allow mail protocol uris to leak out
       // we don't want to reveal the .slt path (for mailbox://), or the username or hostname
       var mailProtocol = /^file:|^mailbox:|^imap:|^s?news:/i;
       if (mailProtocol.test(attachment.name))
         attachment.name = sComposeMsgsBundle.getString("partAttachmentSafeName");
     }
 
-    item.setAttribute("label", attachment.name);    //use for display only
+    var nameAndSize = attachment.name;
+    if (attachment.size != -1)
+      nameAndSize += " (" + gMessenger.formatFileSize(attachment.size) + ")";
+    item.setAttribute("label", nameAndSize);    //use for display only
     item.attachment = attachment;   //full attachment object stored here
     try {
       item.setAttribute("tooltiptext", decodeURI(attachment.url));
     } catch(e) {
       item.setAttribute("tooltiptext", attachment.url);
     }
     item.setAttribute("class", "listitem-iconic");
     item.setAttribute("image", "moz-icon:" + attachment.url);
+    item.setAttribute("crop", "center");
     bucket.appendChild(item);
   }
 }
 
 function SelectAllAttachments()
 {
   var bucketList = document.getElementById("attachmentBucket");
   if (bucketList)
@@ -2730,17 +2737,20 @@ function RenameSelectedAttachment()
                      attachmentName,
                      null,
                      {value: 0}))
   {
     var modifiedAttachmentName = attachmentName.value;
     if (modifiedAttachmentName == "")
       return; // name was not filled, bail out
 
-    item.label = modifiedAttachmentName;
+    var nameAndSize = modifiedAttachmentName;
+    if (item.attachment.size != -1)
+      nameAndSize += " (" + gMessenger.formatFileSize(item.attachment.size) + ")";
+    item.label = nameAndSize;
     item.attachment.name = modifiedAttachmentName;
     gContentChanged = true;
   }
 }
 
 function FocusOnFirstAttachment()
 {
   var bucketList = document.getElementById("attachmentBucket");
@@ -2761,18 +2771,17 @@ function OpenSelectedAttachment()
   if (bucket.selectedItems.length == 1)
   {
     var attachmentUrl = bucket.getSelectedItem(0).attachment.url;
 
     var messagePrefix = /^mailbox-message:|^imap-message:|^news-message:/i;
     if (messagePrefix.test(attachmentUrl))
     {
       // we must be dealing with a forwarded attachment, treat this specially
-      var messenger = Components.classes["@mozilla.org/messenger;1"].createInstance(Components.interfaces.nsIMessenger);
-      var msgHdr = messenger.msgHdrFromURI(attachmentUrl);
+      var msgHdr = gMessenger.msgHdrFromURI(attachmentUrl);
       if (msgHdr)
       {
         var folderUri = msgHdr.folder.folderURL;
         window.openDialog("chrome://messenger/content/messageWindow.xul", "_blank", "all,chrome,dialog=no,status,toolbar",
                           attachmentUrl, folderUri, null);
       }
     }
     else
@@ -3064,73 +3073,82 @@ var attachmentBucketObserver = {
       var dataList = aData.dataList;
       var dataListLength = dataList.length;
       var errorTitle;
       var attachment;
       var errorMsg;
 
       for (let i = 0; i < dataListLength; i++)
       {
-        var item = dataList[i].first;
-        var prettyName;
-        var rawData = item.data;
+        let item = dataList[i].first;
+        let prettyName;
+        let size = NaN;
+        let rawData = item.data;
 
         if (item.flavour.contentType == "text/x-moz-url" ||
             item.flavour.contentType == "text/x-moz-message" ||
             item.flavour.contentType == "application/x-moz-file")
         {
           if (item.flavour.contentType == "application/x-moz-file")
           {
-            var ioService = Components.classes["@mozilla.org/network/io-service;1"]
+            let ioService = Components.classes["@mozilla.org/network/io-service;1"]
                             .getService(Components.interfaces.nsIIOService);
-            var fileHandler = ioService.getProtocolHandler("file")
+            let fileHandler = ioService.getProtocolHandler("file")
                               .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
+            size = rawData.fileSize;
             rawData = fileHandler.getURLSpecFromFile(rawData);
           }
+          else if (item.flavour.contentType == "text/x-moz-message")
+          {
+            size = gMessenger.messageServiceFromURI(rawData)
+                             .messageURIToMsgHdr(rawData).messageSize;
+          }
           else
           {
-            var separator = rawData.indexOf("\n");
-            if (separator != -1)
-            {
-              prettyName = rawData.substr(separator+1);
-              rawData = rawData.substr(0,separator);
-            }
+            let pieces = rawData.split("\n");
+            rawData = pieces[0];
+            if (pieces.length > 1)
+              prettyName = pieces[1];
+            if (pieces.length > 2)
+              size = Number(pieces[2]);
           }
 
           if (DuplicateFileCheck(rawData))
           {
             dump("Error, attaching the same item twice\n");
           }
           else
           {
-            var isValid = true;
+            let isValid = true;
             if (item.flavour.contentType == "text/x-moz-url") {
               // if this is a url (or selected text)
               // see if it's a valid url by checking
               // if we can extract a scheme
               // using the ioservice
               //
               // also skip mailto:, since it doesn't make sense
               // to attach and send mailto urls
               try {
-                var scheme = gIOService.extractScheme(rawData);
+                let scheme = gIOService.extractScheme(rawData);
                 // don't attach mailto: urls
                 if (scheme == "mailto")
                   isValid = false;
               }
               catch (ex) {
                 isValid = false;
               }
             }
 
             if (isValid) {
               attachment = Components.classes["@mozilla.org/messengercompose/attachment;1"]
                            .createInstance(Components.interfaces.nsIMsgAttachment);
               attachment.url = rawData;
               attachment.name = prettyName;
+              if (!isNaN(size))
+                attachment.size = size;
               AddAttachment(attachment);
             }
           }
         }
       }
     },
 
   onDragOver: function (aEvent, aFlavour, aDragSession)
--- a/suite/mailnews/msgHdrViewOverlay.js
+++ b/suite/mailnews/msgHdrViewOverlay.js
@@ -1684,18 +1684,18 @@ var attachmentAreaDNDObserver = {
       if (attachment.contentType == "text/x-moz-deleted")
         return;
 
       var data = new TransferData();
       if (attachment.url && attachment.displayName)
       {
         var info = attachment.url + "&type=" + attachment.contentType +
                    "&filename=" + encodeURIComponent(attachment.displayName);
-        data.addDataForFlavour("text/x-moz-url",
-                               info + "\n" + attachment.displayName);
+        data.addDataForFlavour("text/x-moz-url", info + "\n" +
+                               attachment.displayName + "\n" + attachment.size);
         data.addDataForFlavour("text/x-moz-url-data", attachment.url);
         data.addDataForFlavour("text/x-moz-url-desc", attachment.displayName);
         data.addDataForFlavour("application/x-moz-file-promise-url",
                                attachment.url);
         data.addDataForFlavour("application/x-moz-file-promise",
                                new nsFlavorDataProvider(), 0,
                                Components.interfaces.nsISupports);
       }