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.

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

mercurial