Bug 1427317 - Allow addition of search engines from XML file and removal in general pane in preferences. r=Paenglab a=jorgk DONTBUILD
authorJorg K <jorgk@jorgk.com>
Thu, 07 Feb 2019 00:20:03 +0100
changeset 34314 b5802d8134e4
parent 34313 22bc0ca01eef
child 34315 9abb72db409c
push id389
push userclokep@gmail.com
push dateMon, 18 Mar 2019 19:01:53 +0000
reviewersPaenglab, jorgk
bugs1427317, 1510936
Bug 1427317 - Allow addition of search engines from XML file and removal in general pane in preferences. r=Paenglab a=jorgk DONTBUILD * * * Bug 1427317 - Follow-up: Rename currentEngine to defaultEngine (see bug 1510936). r+a=me DONTBUILD
mail/components/preferences/general.inc.xul
mail/components/preferences/general.js
--- a/mail/components/preferences/general.inc.xul
+++ b/mail/components/preferences/general.inc.xul
@@ -39,20 +39,28 @@
                   oncommand="gGeneralPane.restoreDefaultStartPage();">
             <observes element="mailnewsStartPageUrl" attribute="disabled"/>
           </button>
         </hbox>
     </groupbox>
 
     <groupbox>
       <label><html:h2 >&defaultSearchEngine.label;</html:h2></label>
-      <hbox>
+      <hbox align="center">
         <menulist id="defaultWebSearch">
           <menupopup id="defaultWebSearchPopup"/>
         </menulist>
+        <button id="addSearchEngine"
+                label="+"
+                style="min-width: 2.5em;"
+                oncommand="gGeneralPane.addSearchEngine();"/>
+        <button id="removeSearchEngine"
+                label="-"
+                style="min-width: 2.5em;"
+                oncommand="gGeneralPane.removeSearchEngine();"/>
       </hbox>
     </groupbox>
 
     <groupbox>
       <label><html:h2>&newMessagesArrive.label;</html:h2></label>
 #ifdef XP_MACOSX
       <hbox align="center">
         <description flex="1">&changeDockIconOptions.label;</description>
--- a/mail/components/preferences/general.js
+++ b/mail/components/preferences/general.js
@@ -171,30 +171,101 @@ var gGeneralPane = {
     let customizeAlertButton = document.getElementById("customizeMailAlert");
     if (customizeAlertButton) {
       customizeAlertButton.disabled =
         !document.getElementById("newMailNotificationAlert").checked;
     }
   },
 
   updateWebSearch() {
+    let self = this;
     Services.search.init({
       onInitComplete() {
         let engineList = document.getElementById("defaultWebSearch");
         for (let engine of Services.search.getVisibleEngines()) {
           let item = engineList.appendItem(engine.name);
           item.engine = engine;
           item.className = "menuitem-iconic";
           item.setAttribute(
             "image", engine.iconURI ? engine.iconURI.spec :
                      "resource://gre-resources/broken-image.png"
           );
           if (engine == Services.search.defaultEngine)
             engineList.selectedItem = item;
         }
+        self.defaultEngines = Services.search.getDefaultEngines();
+        self.updateRemoveButton();
 
         engineList.addEventListener("command", function() {
           Services.search.defaultEngine = engineList.selectedItem.engine;
+          self.updateRemoveButton();
         });
       },
     });
   },
+
+  // Caches the default engines so we only retrieve them once.
+  defaultEngines: null,
+
+  updateRemoveButton() {
+    let engineList = document.getElementById("defaultWebSearch");
+    let removeButton = document.getElementById("removeSearchEngine");
+    if (this.defaultEngines.includes(Services.search.defaultEngine)) {
+      // Don't allow deletion of a default engine (saves us having a 'restore' button).
+      removeButton.disabled = true;
+    } else {
+      // Don't allow removal of last engine. This shouldn't happen since there should
+      // always be default engines.
+      removeButton.disabled = engineList.itemCount <= 1;
+    }
+  },
+
+  addSearchEngine() {
+    let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
+    fp.init(window, "", Ci.nsIFilePicker.modeOpen);
+
+    // Filter on XML files only
+    fp.appendFilter("XML", "*.xml");
+
+    fp.open(rv => {
+      if (rv != Ci.nsIFilePicker.returnOK || !fp.file) {
+        return;
+      }
+      let engineAdd = "";
+      engineAdd = fp.fileURL.spec;
+      let self = this;
+      Services.search.addEngine(engineAdd, null, false, {
+        onSuccess(engine) {
+          // Add new engine to the list.
+          let engineList = document.getElementById("defaultWebSearch");
+
+          let item = engineList.appendItem(engine.name);
+          item.engine = engine;
+          item.className = "menuitem-iconic";
+          item.setAttribute(
+            "image", engine.iconURI ? engine.iconURI.spec :
+                     "resource://gre-resources/broken-image.png"
+          );
+
+          self.updateRemoveButton();
+        },
+        onError(errCode) { /* no-op so far */
+        },
+      });
+    });
+  },
+
+  removeSearchEngine() {
+    // Deletes the current engine. Firefox does a better job since it
+    // shows all the engines in the list. But better than nothing.
+    let engineList = document.getElementById("defaultWebSearch");
+    for (let i = 0; i < engineList.itemCount; i++) {
+      let item = engineList.getItemAtIndex(i);
+      if (item.engine == Services.search.defaultEngine) {
+        item.remove();
+        engineList.selectedIndex = 0;
+        Services.search.removeEngine(item.engine);
+        this.updateRemoveButton();
+        break;
+      }
+    }
+  },
 };