toolkit/components/apppicker/content/appPicker.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 # -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     2 #
     3 # This Source Code Form is subject to the terms of the Mozilla Public
     4 # License, v. 2.0. If a copy of the MPL was not distributed with this
     5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
     7 function AppPicker() {};
     9 var g_dialog = null;
    11 AppPicker.prototype = 
    12 {
    13     // Class members
    14     _incomingParams:null,
    16     /** 
    17     * Init the dialog and populate the application list
    18     */ 
    19     appPickerLoad: function appPickerLoad() {
    20         const nsILocalHandlerApp = Components.interfaces.nsILocalHandlerApp;
    22         this._incomingParams = window.arguments[0];
    23         this._incomingParams.handlerApp = null;
    25         document.title = this._incomingParams.title;
    27         // Header creation - at the very least, we must have 
    28         // a mime type:
    29         //        
    30         // (icon) Zip File
    31         // (icon) filename
    32         //
    33         // (icon) Web Feed
    34         // (icon) mime/type
    35         //
    36         // (icon) mime/type
    37         // (icon)
    39         var mimeInfo = this._incomingParams.mimeInfo;
    40         var filename = this._incomingParams.filename;
    41         if (!filename) {
    42           filename = mimeInfo.MIMEType;
    43         }
    44         var description = this._incomingParams.description;
    45         if (!description) {
    46           description = filename;
    47           filename = "";
    48         }
    50         // Setup the dialog header information
    51         document.getElementById("content-description").setAttribute("value", 
    52           description);
    53         document.getElementById("suggested-filename").setAttribute("value",
    54           filename);
    55         document.getElementById("content-icon").setAttribute("src",
    56           "moz-icon://" + filename + "?size=32&contentType=" +
    57           mimeInfo.MIMEType);
    59         // Grab a list of nsILocalHandlerApp application helpers to list
    60         var fileList = mimeInfo.possibleLocalHandlers;
    62         var list = document.getElementById("app-picker-listbox");
    64         var primaryCount = 0;
    66         if (!fileList || fileList.length == 0) {
    67           // display a message saying nothing is configured
    68           document.getElementById("app-picker-notfound").removeAttribute("hidden");
    69           return;
    70         }
    72         for (var idx = 0; idx < fileList.length; idx++) {
    73           var file = fileList.queryElementAt(idx, nsILocalHandlerApp);
    74           try {
    75               if (!file.executable || !file.executable.isFile())
    76                 continue;
    77           } catch (err) {
    78             continue;
    79           }
    81           var item = document.createElement("listitem");
    82           item.className = "listitem-iconic";
    83           item.handlerApp = file;
    84           item.setAttribute("label", this.getFileDisplayName(file.executable));
    85           item.setAttribute("image", this.getFileIconURL(file.executable));
    86           list.appendChild(item);
    88           primaryCount++;
    89         }
    91         if ( primaryCount == 0 ) {
    92           // display a message saying nothing is configured
    93           document.getElementById("app-picker-notfound").removeAttribute("hidden");
    94         }
    95     },
    97     /** 
    98     * Retrieve the moz-icon for the app
    99     */ 
   100     getFileIconURL: function getFileIconURL(file) {
   101       var ios = Components.classes["@mozilla.org/network/io-service;1"].
   102                 getService(Components.interfaces.nsIIOService);
   104       if (!ios) return "";
   105       const nsIFileProtocolHandler =
   106         Components.interfaces.nsIFileProtocolHandler;
   108       var fph = ios.getProtocolHandler("file")
   109                 .QueryInterface(nsIFileProtocolHandler);
   110       if (!fph) return "";
   112       var urlSpec = fph.getURLSpecFromFile(file);
   113       return "moz-icon://" + urlSpec + "?size=32";
   114     },
   116     /** 
   117     * Retrieve the pretty description from the file
   118     */ 
   119     getFileDisplayName: function getFileDisplayName(file) {
   120 #ifdef XP_WIN
   121       if (file instanceof Components.interfaces.nsILocalFileWin) {
   122         try {
   123           return file.getVersionInfoField("FileDescription");
   124         } catch (e) {}
   125       }
   126 #endif
   127 #ifdef XP_MACOSX
   128       if (file instanceof Components.interfaces.nsILocalFileMac) {
   129         try {
   130           return file.bundleDisplayName;
   131         } catch (e) {}
   132       }
   133 #endif
   134       return file.leafName;
   135     },
   137     /**
   138     * Double click accepts an app
   139     */
   140     appDoubleClick: function appDoubleClick() {
   141       var list = document.getElementById("app-picker-listbox");
   142       var selItem = list.selectedItem;
   144       if (!selItem) {
   145           this._incomingParams.handlerApp = null;
   146           return true;
   147       }
   149       this._incomingParams.handlerApp = selItem.handlerApp;
   150       window.close();
   152       return true;
   153     },
   155     appPickerOK: function appPickerOK() {
   156       if (this._incomingParams.handlerApp) return true;
   158       var list = document.getElementById("app-picker-listbox");
   159       var selItem = list.selectedItem;
   161       if (!selItem) {
   162         this._incomingParams.handlerApp = null;
   163         return true;
   164       }
   165       this._incomingParams.handlerApp = selItem.handlerApp;
   167       return true;
   168     },
   170     appPickerCancel: function appPickerCancel() {
   171       this._incomingParams.handlerApp = null;
   172       return true;
   173     },
   175     /**
   176     * User browse for an app.
   177     */
   178     appPickerBrowse: function appPickerBrowse() {
   179       var nsIFilePicker = Components.interfaces.nsIFilePicker;
   180       var fp = Components.classes["@mozilla.org/filepicker;1"].
   181                createInstance(nsIFilePicker);
   183       fp.init(window, this._incomingParams.title, nsIFilePicker.modeOpen);
   184       fp.appendFilters(nsIFilePicker.filterApps);
   186       var fileLoc = Components.classes["@mozilla.org/file/directory_service;1"]
   187                             .getService(Components.interfaces.nsIProperties);
   188       var startLocation;
   189 #ifdef XP_WIN
   190     startLocation = "ProgF"; // Program Files
   191 #else
   192 #ifdef XP_MACOSX
   193     startLocation = "LocApp"; // Local Applications
   194 #else
   195     startLocation = "Home";
   196 #endif
   197 #endif
   198       fp.displayDirectory = 
   199         fileLoc.get(startLocation, Components.interfaces.nsILocalFile);
   201       if (fp.show() == nsIFilePicker.returnOK && fp.file) {
   202           var localHandlerApp = 
   203             Components.classes["@mozilla.org/uriloader/local-handler-app;1"].
   204             createInstance(Components.interfaces.nsILocalHandlerApp);
   205           localHandlerApp.executable = fp.file;
   207           this._incomingParams.handlerApp = localHandlerApp;
   208           window.close();
   209       }
   210       return true;
   211     }
   212 }
   214 // Global object
   215 var g_dialog = new AppPicker();

mercurial