Bug 1675272 - Add entries for copying key ids and fingerprints in the openpgp key manager context menu. r=aleca,mkmelin
authorLasana Murray <lasana@thunderbird.net>
Sat, 14 Nov 2020 00:08:43 +0200
changeset 31019 4b19adc64fc28ff6fe9aff18cb6f43b6ef3e7032
parent 31018 31475c37d0bbd27ecdf3b6fe6fb41a8851786059
child 31020 c640a0a534bf55349693458634f86cc6e9222a2a
push id18169
push usermkmelin@iki.fi
push dateFri, 13 Nov 2020 22:11:58 +0000
treeherdercomm-central@4b19adc64fc2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaleca, mkmelin
bugs1675272
Bug 1675272 - Add entries for copying key ids and fingerprints in the openpgp key manager context menu. r=aleca,mkmelin
mail/extensions/openpgp/content/ui/enigmailKeyManager.js
mail/extensions/openpgp/content/ui/enigmailKeyManager.xhtml
mail/locales/en-US/messenger/openpgp/openpgp.ftl
--- a/mail/extensions/openpgp/content/ui/enigmailKeyManager.js
+++ b/mail/extensions/openpgp/content/ui/enigmailKeyManager.js
@@ -239,16 +239,24 @@ function enigmailKeyMenu() {
     for (let key of keyList) {
       if (!gKeyList[key].secretAvailable) {
         haveSecretForAll = false;
         break;
       }
     }
   }
 
+  // Make the selected key count available to translations.
+  for (let el of document.querySelectorAll(".enigmail-bulk-key-operation")) {
+    el.setAttribute(
+      "data-l10n-args",
+      JSON.stringify({ count: keyList.length })
+    );
+  }
+
   if (haveSecretForAll) {
     document.getElementById("bcBackupSecret").removeAttribute("disabled");
   } else {
     document.getElementById("bcBackupSecret").setAttribute("disabled", "true");
   }
 
   if (keyList.length == 1 && gKeyList[keyList[0]].secretAvailable) {
     document.getElementById("bcRevoke").removeAttribute("disabled");
@@ -277,16 +285,19 @@ function enigmailKeyMenu() {
     document.getElementById("bcOneKey").setAttribute("disabled", "true");
     document.getElementById("bcDeleteKey").setAttribute("disabled", "true");
   }
 
   // Disable the "Generate key" menu item if no mail account is available.
   document
     .getElementById("genKey")
     .setAttribute("disabled", MailServices.accounts.defaultAccount == null);
+
+  // Disable the context menu if no keys are selected.
+  return keyList.length > 0;
 }
 
 function onListClick(event) {
   if (event.detail > 2) {
     return;
   }
 
   if (event.type === "click") {
@@ -331,21 +342,16 @@ function enigmailKeyDetails(keyId = null
     refreshKeys();
   }
 }
 
 function enigmailDeleteKey() {
   var keyList = getSelectedKeys();
   var deleteSecret = false;
 
-  var enigmailSvc = GetEnigmailSvc();
-  if (!enigmailSvc) {
-    return;
-  }
-
   if (keyList.length == 1) {
     // one key selected
     var userId = gKeyList[keyList[0]].userId;
     if (gKeyList[keyList[0]].secretAvailable) {
       if (
         !EnigConfirm(
           l10n.formatValueSync("delete-secret-key", {
             userId,
@@ -398,29 +404,17 @@ function enigmailDeleteKey() {
     let fpr = gKeyList[keyList[j]].fpr;
     cApi.sync(cApi.deleteKey(fpr, deleteSecret));
     cApi.sync(PgpSqliteDb2.deleteAcceptance(fpr));
   }
   clearKeyCache();
 }
 
 function enigCreateKeyMsg() {
-  var enigmailSvc = GetEnigmailSvc();
-  if (!enigmailSvc) {
-    return;
-  }
-
   var keyList = getSelectedKeyIds();
-  if (keyList.length === 0) {
-    document.l10n.formatValue("no-key-selected").then(value => {
-      EnigAlert(value);
-    });
-    return;
-  }
-
   var tmpDir = EnigGetTempDir();
   var tmpFile;
   try {
     tmpFile = Cc[ENIG_LOCAL_FILE_CONTRACTID].createInstance(
       EnigGetLocalFileApi()
     );
     tmpFile.initWithPath(tmpDir);
     if (!(tmpFile.isDirectory() && tmpFile.isWritable())) {
@@ -565,25 +559,18 @@ function enigCreateRevokeCert() {
 
 function dlgOpenCallback(dlgUri, args) {
   window.openDialog(dlgUri, "", "dialog,modal,centerscreen,resizable", args);
 }
 
 async function enigmailExportKeys(which) {
   let exportSecretKey = which == "secret";
   var keyList = getSelectedKeys();
-  if (keyList.length === 0) {
-    EnigmailDialog.info(
-      window,
-      await document.l10n.formatValue("no-key-selected")
-    );
-    return;
-  }
+  var defaultFileName;
 
-  var defaultFileName;
   if (keyList.length == 1) {
     let extension = exportSecretKey ? "secret.asc" : "public.asc";
     defaultFileName = gKeyList[keyList[0]].userId.replace(/[<>]/g, "");
     defaultFileName =
       defaultFileName +
       "-" +
       `(0x${gKeyList[keyList[0]].keyId})` +
       "-" +
@@ -653,21 +640,16 @@ function enigmailChangePwd() {
 function enigGetClipboard() {
   return EnigmailClipboard.getClipboardContent(
     window,
     Ci.nsIClipboard.kGlobalClipboard
   );
 }
 
 function enigmailImportFromClipbrd() {
-  var enigmailSvc = GetEnigmailSvc();
-  if (!enigmailSvc) {
-    return;
-  }
-
   if (
     !EnigConfirm(
       l10n.formatValueSync("import-from-clip"),
       l10n.formatValueSync("key-man-button-import")
     )
   ) {
     return;
   }
@@ -724,22 +706,35 @@ function enigmailImportFromClipbrd() {
     }
   } else {
     document.l10n.formatValue("preview-failed").then(value => {
       EnigmailDialog.alert(window, value);
     });
   }
 }
 
+/**
+ * Places the fingerprint of each selected key onto the keyboard.
+ */
+function copyOpenPGPFingerPrints() {
+  let fprs = getSelectedKeys()
+    .map(idx => gKeyList[idx].fpr)
+    .join("\n");
+  EnigmailClipboard.setClipboardContent(fprs);
+}
+
+/**
+ * Places the key id of each key selected onto the clipboard.
+ */
+function copyOpenPGPKeyIds() {
+  let ids = getSelectedKeyIds();
+  EnigmailClipboard.setClipboardContent(ids.map(id => `0x${id}`).join("\n"));
+}
+
 function enigmailCopyToClipbrd() {
-  var enigmailSvc = GetEnigmailSvc();
-  if (!enigmailSvc) {
-    return;
-  }
-
   var keyList = getSelectedKeyIds();
   if (keyList.length === 0) {
     document.l10n.formatValue("no-key-selected").then(value => {
       EnigmailDialog.info(window, value);
     });
     return;
   }
   var exitCodeObj = {};
@@ -1155,21 +1150,16 @@ function determineHiddenKeys(keyObj, sho
 
   return show;
 }
 
 //
 // ----- keyserver related functionality ----
 //
 function accessKeyServer(accessType, callbackFunc) {
-  var enigmailSvc = GetEnigmailSvc();
-  if (!enigmailSvc) {
-    return;
-  }
-
   const ioService = Services.io;
   if (ioService && ioService.offline) {
     document.l10n.formatValue("need-online").then(value => {
       EnigmailDialog.alert(window, value);
     });
     return;
   }
 
@@ -1469,17 +1459,17 @@ var gKeyListView = {
     }
 
     switch (r.rowType) {
       case "key":
         switch (col.id) {
           case "enigUserNameCol":
             return keyObj.userId;
           case "keyCol":
-            return keyObj.keyId;
+            return `0x${keyObj.keyId}`;
           case "expCol":
             return keyObj.effectiveExpiry;
           case "fprCol":
             return keyObj.fprFormatted;
         }
         break;
       case "uid":
         switch (col.id) {
--- a/mail/extensions/openpgp/content/ui/enigmailKeyManager.xhtml
+++ b/mail/extensions/openpgp/content/ui/enigmailKeyManager.xhtml
@@ -105,18 +105,32 @@
         <menupopup onpopupshowing="enigmailKeyMenu();">
           <menuitem id="importFromClipbrd"
                     data-l10n-id="openpgp-key-man-import-from-clipbrd"
                     observes="bcClipbrd"
                     oncommand="enigmailImportFromClipbrd();"/>
           <menuitem id="importFromUrl"
                     data-l10n-id="openpgp-key-man-import-from-url"
                     oncommand="enigmailImportKeysFromUrl();"/>
+          <menuitem id="copyFprs"
+                    data-l10n-id="openpgp-key-man-copy-fprs"
+                    data-l10n-args='{"count": 0}'
+                    class="enigmail-bulk-key-operation"
+                    observes="bcNoKey"
+                    oncommand="copyOpenPGPFingerPrints()"/>
+          <menuitem id="copyKeyIds"
+                    data-l10n-id="openpgp-key-man-copy-key-ids"
+                    data-l10n-args='{"count": 0}'
+                    class="enigmail-bulk-key-operation"
+                    observes="bcNoKey"
+                    oncommand="copyOpenPGPKeyIds()"/>
           <menuitem id="copyToClipbrd"
-                    data-l10n-id="openpgp-key-man-copy-to-clipbrd"
+                    data-l10n-id="openpgp-key-man-copy-to-clipboard"
+                    data-l10n-args='{"count": 0}'
+                    class="enigmail-bulk-key-operation"
                     observes="bcNoKey"
                     oncommand="enigmailCopyToClipbrd();"/>
           <menuseparator/>
 
           <menuitem id="revokeKey"
                     data-l10n-id="openpgp-key-man-revoke-key"
                     observes="bcRevoke"
                     oncommand="enigmailRevokeKey()"/>
@@ -191,20 +205,37 @@
           -->
         </menupopup>
       </menu>
     </menubar>
   </toolbar>
 
   <popupset>
     <menupopup id="ctxmenu"
-               onpopupshowing="enigmailKeyMenu();">
-      <menuitem data-l10n-id="openpgp-key-man-ctx-copy-to-clipbrd-label"
-                id="ctxCopyToClipbrd"
-                oncommand="enigmailCopyToClipbrd()"/>
+               onpopupshowing="return enigmailKeyMenu();">
+      <menu id="ctxmenu-copy"
+            data-l10n-id="openpgp-key-man-ctx-copy">
+        <menupopup id="ctxmenu-copy-popup">
+          <menuitem id="ctxCopyFprs"
+                    data-l10n-id="openpgp-key-man-ctx-copy-fprs"
+                    data-l10n-args='{"count": 0}'
+                    class="enigmail-bulk-key-operation"
+                    oncommand="copyOpenPGPFingerPrints()"/>
+          <menuitem id="ctxCopyKeyIds"
+                    data-l10n-id="openpgp-key-man-ctx-copy-key-ids"
+                    data-l10n-args='{"count": 0}'
+                    class="enigmail-bulk-key-operation"
+                    oncommand="copyOpenPGPKeyIds()"/>
+          <menuitem id="ctxCopyPublicKeys"
+                    data-l10n-id="openpgp-key-man-ctx-copy-public-keys"
+                    data-l10n-args='{"count": 0}'
+                    class="enigmail-bulk-key-operation"
+                    oncommand="enigmailCopyToClipbrd()"/>
+        </menupopup>
+      </menu>
       <menuitem data-l10n-id="openpgp-key-man-ctx-expor-to-file-label"
                 id="ctxExport"
                 oncommand="enigmailExportKeys('public')"/>
       <menuitem data-l10n-id="openpgp-key-man-send-keys"
                 id="ctxSendKey"
                 oncommand="enigCreateKeyMsg()"/>
 
       <menuseparator/>
--- a/mail/locales/en-US/messenger/openpgp/openpgp.ftl
+++ b/mail/locales/en-US/messenger/openpgp/openpgp.ftl
@@ -112,23 +112,64 @@ openpgp-key-export-key =
 openpgp-key-backup-key =
     .label = Backup Secret Key To File
     .accesskey = B
 
 openpgp-key-send-key =
     .label = Send Public Key Via Email
     .accesskey = S
 
-openpgp-key-man-copy-to-clipbrd =
-    .label = Copy Public Key(s) To Clipboard
-    .accesskey = C
+openpgp-key-man-copy-key-ids =
+    .label = { $count ->
+               [one] Copy Key ID To Clipboard
+               *[other] Copy Key IDs To Clipboard
+             }
+    .accesskey = K
+
+openpgp-key-man-copy-fprs =
+    .label = { $count ->
+               [one] Copy Fingerprint To Clipboard
+               *[other] Copy Fingerprints To Clipboard
+             }
+    .accesskey = F
+
+openpgp-key-man-copy-to-clipboard =
+    .label = { $count ->
+               [one] Copy Public Key To Clipboard
+               *[other] Copy Public Keys To Clipboard
+             }
+    .accesskey = P
+
 openpgp-key-man-ctx-expor-to-file-label =
     .label = Export Keys To File
-openpgp-key-man-ctx-copy-to-clipbrd-label =
-    .label = Copy Public Keys To Clipboard
+
+openpgp-key-man-ctx-copy =
+    .label = Copy
+    .accesskey = C
+
+openpgp-key-man-ctx-copy-fprs =
+    .label = { $count ->
+               [one] Fingerprint
+               *[other] Fingerprints
+             }
+    .accesskey = F
+
+openpgp-key-man-ctx-copy-key-ids =
+    .label = { $count ->
+               [one] Key ID
+               *[other] Key IDs
+             }
+    .accesskey = K
+
+openpgp-key-man-ctx-copy-public-keys =
+    .label = { $count ->
+               [one] Public Key
+               *[other] Public Keys
+             }
+    .accesskey = P
 
 openpgp-key-man-close =
     .label = Close
 openpgp-key-man-reload =
     .label = Reload Key Cache
     .accesskey = R
 openpgp-key-man-change-expiry =
     .label = Change Expiration Date