toolkit/components/apppicker/content/appPicker.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/apppicker/content/appPicker.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,215 @@
     1.4 +# -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 +#
     1.6 +# This Source Code Form is subject to the terms of the Mozilla Public
     1.7 +# License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 +# file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.9 +
    1.10 +function AppPicker() {};
    1.11 +
    1.12 +var g_dialog = null;
    1.13 +
    1.14 +AppPicker.prototype = 
    1.15 +{
    1.16 +    // Class members
    1.17 +    _incomingParams:null,
    1.18 +
    1.19 +    /** 
    1.20 +    * Init the dialog and populate the application list
    1.21 +    */ 
    1.22 +    appPickerLoad: function appPickerLoad() {
    1.23 +        const nsILocalHandlerApp = Components.interfaces.nsILocalHandlerApp;
    1.24 +
    1.25 +        this._incomingParams = window.arguments[0];
    1.26 +        this._incomingParams.handlerApp = null;
    1.27 +
    1.28 +        document.title = this._incomingParams.title;
    1.29 +
    1.30 +        // Header creation - at the very least, we must have 
    1.31 +        // a mime type:
    1.32 +        //        
    1.33 +        // (icon) Zip File
    1.34 +        // (icon) filename
    1.35 +        //
    1.36 +        // (icon) Web Feed
    1.37 +        // (icon) mime/type
    1.38 +        //
    1.39 +        // (icon) mime/type
    1.40 +        // (icon)
    1.41 +
    1.42 +        var mimeInfo = this._incomingParams.mimeInfo;
    1.43 +        var filename = this._incomingParams.filename;
    1.44 +        if (!filename) {
    1.45 +          filename = mimeInfo.MIMEType;
    1.46 +        }
    1.47 +        var description = this._incomingParams.description;
    1.48 +        if (!description) {
    1.49 +          description = filename;
    1.50 +          filename = "";
    1.51 +        }
    1.52 +        
    1.53 +        // Setup the dialog header information
    1.54 +        document.getElementById("content-description").setAttribute("value", 
    1.55 +          description);
    1.56 +        document.getElementById("suggested-filename").setAttribute("value",
    1.57 +          filename);
    1.58 +        document.getElementById("content-icon").setAttribute("src",
    1.59 +          "moz-icon://" + filename + "?size=32&contentType=" +
    1.60 +          mimeInfo.MIMEType);
    1.61 +
    1.62 +        // Grab a list of nsILocalHandlerApp application helpers to list
    1.63 +        var fileList = mimeInfo.possibleLocalHandlers;
    1.64 +
    1.65 +        var list = document.getElementById("app-picker-listbox");
    1.66 +
    1.67 +        var primaryCount = 0;
    1.68 +        
    1.69 +        if (!fileList || fileList.length == 0) {
    1.70 +          // display a message saying nothing is configured
    1.71 +          document.getElementById("app-picker-notfound").removeAttribute("hidden");
    1.72 +          return;
    1.73 +        }
    1.74 +        
    1.75 +        for (var idx = 0; idx < fileList.length; idx++) {
    1.76 +          var file = fileList.queryElementAt(idx, nsILocalHandlerApp);
    1.77 +          try {
    1.78 +              if (!file.executable || !file.executable.isFile())
    1.79 +                continue;
    1.80 +          } catch (err) {
    1.81 +            continue;
    1.82 +          }
    1.83 +
    1.84 +          var item = document.createElement("listitem");
    1.85 +          item.className = "listitem-iconic";
    1.86 +          item.handlerApp = file;
    1.87 +          item.setAttribute("label", this.getFileDisplayName(file.executable));
    1.88 +          item.setAttribute("image", this.getFileIconURL(file.executable));
    1.89 +          list.appendChild(item);
    1.90 +
    1.91 +          primaryCount++;
    1.92 +        }
    1.93 +
    1.94 +        if ( primaryCount == 0 ) {
    1.95 +          // display a message saying nothing is configured
    1.96 +          document.getElementById("app-picker-notfound").removeAttribute("hidden");
    1.97 +        }
    1.98 +    },
    1.99 +
   1.100 +    /** 
   1.101 +    * Retrieve the moz-icon for the app
   1.102 +    */ 
   1.103 +    getFileIconURL: function getFileIconURL(file) {
   1.104 +      var ios = Components.classes["@mozilla.org/network/io-service;1"].
   1.105 +                getService(Components.interfaces.nsIIOService);
   1.106 +
   1.107 +      if (!ios) return "";
   1.108 +      const nsIFileProtocolHandler =
   1.109 +        Components.interfaces.nsIFileProtocolHandler;
   1.110 +
   1.111 +      var fph = ios.getProtocolHandler("file")
   1.112 +                .QueryInterface(nsIFileProtocolHandler);
   1.113 +      if (!fph) return "";
   1.114 +
   1.115 +      var urlSpec = fph.getURLSpecFromFile(file);
   1.116 +      return "moz-icon://" + urlSpec + "?size=32";
   1.117 +    },
   1.118 +
   1.119 +    /** 
   1.120 +    * Retrieve the pretty description from the file
   1.121 +    */ 
   1.122 +    getFileDisplayName: function getFileDisplayName(file) {
   1.123 +#ifdef XP_WIN
   1.124 +      if (file instanceof Components.interfaces.nsILocalFileWin) {
   1.125 +        try {
   1.126 +          return file.getVersionInfoField("FileDescription");
   1.127 +        } catch (e) {}
   1.128 +      }
   1.129 +#endif
   1.130 +#ifdef XP_MACOSX
   1.131 +      if (file instanceof Components.interfaces.nsILocalFileMac) {
   1.132 +        try {
   1.133 +          return file.bundleDisplayName;
   1.134 +        } catch (e) {}
   1.135 +      }
   1.136 +#endif
   1.137 +      return file.leafName;
   1.138 +    },
   1.139 +
   1.140 +    /**
   1.141 +    * Double click accepts an app
   1.142 +    */
   1.143 +    appDoubleClick: function appDoubleClick() {
   1.144 +      var list = document.getElementById("app-picker-listbox");
   1.145 +      var selItem = list.selectedItem;
   1.146 +
   1.147 +      if (!selItem) {
   1.148 +          this._incomingParams.handlerApp = null;
   1.149 +          return true;
   1.150 +      }
   1.151 +
   1.152 +      this._incomingParams.handlerApp = selItem.handlerApp;
   1.153 +      window.close();
   1.154 +
   1.155 +      return true;
   1.156 +    },
   1.157 +
   1.158 +    appPickerOK: function appPickerOK() {
   1.159 +      if (this._incomingParams.handlerApp) return true;
   1.160 +
   1.161 +      var list = document.getElementById("app-picker-listbox");
   1.162 +      var selItem = list.selectedItem;
   1.163 +
   1.164 +      if (!selItem) {
   1.165 +        this._incomingParams.handlerApp = null;
   1.166 +        return true;
   1.167 +      }
   1.168 +      this._incomingParams.handlerApp = selItem.handlerApp;
   1.169 +
   1.170 +      return true;
   1.171 +    },
   1.172 +
   1.173 +    appPickerCancel: function appPickerCancel() {
   1.174 +      this._incomingParams.handlerApp = null;
   1.175 +      return true;
   1.176 +    },
   1.177 +
   1.178 +    /**
   1.179 +    * User browse for an app.
   1.180 +    */
   1.181 +    appPickerBrowse: function appPickerBrowse() {
   1.182 +      var nsIFilePicker = Components.interfaces.nsIFilePicker;
   1.183 +      var fp = Components.classes["@mozilla.org/filepicker;1"].
   1.184 +               createInstance(nsIFilePicker);
   1.185 +
   1.186 +      fp.init(window, this._incomingParams.title, nsIFilePicker.modeOpen);
   1.187 +      fp.appendFilters(nsIFilePicker.filterApps);
   1.188 +      
   1.189 +      var fileLoc = Components.classes["@mozilla.org/file/directory_service;1"]
   1.190 +                            .getService(Components.interfaces.nsIProperties);
   1.191 +      var startLocation;
   1.192 +#ifdef XP_WIN
   1.193 +    startLocation = "ProgF"; // Program Files
   1.194 +#else
   1.195 +#ifdef XP_MACOSX
   1.196 +    startLocation = "LocApp"; // Local Applications
   1.197 +#else
   1.198 +    startLocation = "Home";
   1.199 +#endif
   1.200 +#endif
   1.201 +      fp.displayDirectory = 
   1.202 +        fileLoc.get(startLocation, Components.interfaces.nsILocalFile);
   1.203 +      
   1.204 +      if (fp.show() == nsIFilePicker.returnOK && fp.file) {
   1.205 +          var localHandlerApp = 
   1.206 +            Components.classes["@mozilla.org/uriloader/local-handler-app;1"].
   1.207 +            createInstance(Components.interfaces.nsILocalHandlerApp);
   1.208 +          localHandlerApp.executable = fp.file;
   1.209 +
   1.210 +          this._incomingParams.handlerApp = localHandlerApp;
   1.211 +          window.close();
   1.212 +      }
   1.213 +      return true;
   1.214 +    }
   1.215 +}
   1.216 +
   1.217 +// Global object
   1.218 +var g_dialog = new AppPicker();

mercurial