michael@0: # -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: # michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: function AppPicker() {}; michael@0: michael@0: var g_dialog = null; michael@0: michael@0: AppPicker.prototype = michael@0: { michael@0: // Class members michael@0: _incomingParams:null, michael@0: michael@0: /** michael@0: * Init the dialog and populate the application list michael@0: */ michael@0: appPickerLoad: function appPickerLoad() { michael@0: const nsILocalHandlerApp = Components.interfaces.nsILocalHandlerApp; michael@0: michael@0: this._incomingParams = window.arguments[0]; michael@0: this._incomingParams.handlerApp = null; michael@0: michael@0: document.title = this._incomingParams.title; michael@0: michael@0: // Header creation - at the very least, we must have michael@0: // a mime type: michael@0: // michael@0: // (icon) Zip File michael@0: // (icon) filename michael@0: // michael@0: // (icon) Web Feed michael@0: // (icon) mime/type michael@0: // michael@0: // (icon) mime/type michael@0: // (icon) michael@0: michael@0: var mimeInfo = this._incomingParams.mimeInfo; michael@0: var filename = this._incomingParams.filename; michael@0: if (!filename) { michael@0: filename = mimeInfo.MIMEType; michael@0: } michael@0: var description = this._incomingParams.description; michael@0: if (!description) { michael@0: description = filename; michael@0: filename = ""; michael@0: } michael@0: michael@0: // Setup the dialog header information michael@0: document.getElementById("content-description").setAttribute("value", michael@0: description); michael@0: document.getElementById("suggested-filename").setAttribute("value", michael@0: filename); michael@0: document.getElementById("content-icon").setAttribute("src", michael@0: "moz-icon://" + filename + "?size=32&contentType=" + michael@0: mimeInfo.MIMEType); michael@0: michael@0: // Grab a list of nsILocalHandlerApp application helpers to list michael@0: var fileList = mimeInfo.possibleLocalHandlers; michael@0: michael@0: var list = document.getElementById("app-picker-listbox"); michael@0: michael@0: var primaryCount = 0; michael@0: michael@0: if (!fileList || fileList.length == 0) { michael@0: // display a message saying nothing is configured michael@0: document.getElementById("app-picker-notfound").removeAttribute("hidden"); michael@0: return; michael@0: } michael@0: michael@0: for (var idx = 0; idx < fileList.length; idx++) { michael@0: var file = fileList.queryElementAt(idx, nsILocalHandlerApp); michael@0: try { michael@0: if (!file.executable || !file.executable.isFile()) michael@0: continue; michael@0: } catch (err) { michael@0: continue; michael@0: } michael@0: michael@0: var item = document.createElement("listitem"); michael@0: item.className = "listitem-iconic"; michael@0: item.handlerApp = file; michael@0: item.setAttribute("label", this.getFileDisplayName(file.executable)); michael@0: item.setAttribute("image", this.getFileIconURL(file.executable)); michael@0: list.appendChild(item); michael@0: michael@0: primaryCount++; michael@0: } michael@0: michael@0: if ( primaryCount == 0 ) { michael@0: // display a message saying nothing is configured michael@0: document.getElementById("app-picker-notfound").removeAttribute("hidden"); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Retrieve the moz-icon for the app michael@0: */ michael@0: getFileIconURL: function getFileIconURL(file) { michael@0: var ios = Components.classes["@mozilla.org/network/io-service;1"]. michael@0: getService(Components.interfaces.nsIIOService); michael@0: michael@0: if (!ios) return ""; michael@0: const nsIFileProtocolHandler = michael@0: Components.interfaces.nsIFileProtocolHandler; michael@0: michael@0: var fph = ios.getProtocolHandler("file") michael@0: .QueryInterface(nsIFileProtocolHandler); michael@0: if (!fph) return ""; michael@0: michael@0: var urlSpec = fph.getURLSpecFromFile(file); michael@0: return "moz-icon://" + urlSpec + "?size=32"; michael@0: }, michael@0: michael@0: /** michael@0: * Retrieve the pretty description from the file michael@0: */ michael@0: getFileDisplayName: function getFileDisplayName(file) { michael@0: #ifdef XP_WIN michael@0: if (file instanceof Components.interfaces.nsILocalFileWin) { michael@0: try { michael@0: return file.getVersionInfoField("FileDescription"); michael@0: } catch (e) {} michael@0: } michael@0: #endif michael@0: #ifdef XP_MACOSX michael@0: if (file instanceof Components.interfaces.nsILocalFileMac) { michael@0: try { michael@0: return file.bundleDisplayName; michael@0: } catch (e) {} michael@0: } michael@0: #endif michael@0: return file.leafName; michael@0: }, michael@0: michael@0: /** michael@0: * Double click accepts an app michael@0: */ michael@0: appDoubleClick: function appDoubleClick() { michael@0: var list = document.getElementById("app-picker-listbox"); michael@0: var selItem = list.selectedItem; michael@0: michael@0: if (!selItem) { michael@0: this._incomingParams.handlerApp = null; michael@0: return true; michael@0: } michael@0: michael@0: this._incomingParams.handlerApp = selItem.handlerApp; michael@0: window.close(); michael@0: michael@0: return true; michael@0: }, michael@0: michael@0: appPickerOK: function appPickerOK() { michael@0: if (this._incomingParams.handlerApp) return true; michael@0: michael@0: var list = document.getElementById("app-picker-listbox"); michael@0: var selItem = list.selectedItem; michael@0: michael@0: if (!selItem) { michael@0: this._incomingParams.handlerApp = null; michael@0: return true; michael@0: } michael@0: this._incomingParams.handlerApp = selItem.handlerApp; michael@0: michael@0: return true; michael@0: }, michael@0: michael@0: appPickerCancel: function appPickerCancel() { michael@0: this._incomingParams.handlerApp = null; michael@0: return true; michael@0: }, michael@0: michael@0: /** michael@0: * User browse for an app. michael@0: */ michael@0: appPickerBrowse: function appPickerBrowse() { michael@0: var nsIFilePicker = Components.interfaces.nsIFilePicker; michael@0: var fp = Components.classes["@mozilla.org/filepicker;1"]. michael@0: createInstance(nsIFilePicker); michael@0: michael@0: fp.init(window, this._incomingParams.title, nsIFilePicker.modeOpen); michael@0: fp.appendFilters(nsIFilePicker.filterApps); michael@0: michael@0: var fileLoc = Components.classes["@mozilla.org/file/directory_service;1"] michael@0: .getService(Components.interfaces.nsIProperties); michael@0: var startLocation; michael@0: #ifdef XP_WIN michael@0: startLocation = "ProgF"; // Program Files michael@0: #else michael@0: #ifdef XP_MACOSX michael@0: startLocation = "LocApp"; // Local Applications michael@0: #else michael@0: startLocation = "Home"; michael@0: #endif michael@0: #endif michael@0: fp.displayDirectory = michael@0: fileLoc.get(startLocation, Components.interfaces.nsILocalFile); michael@0: michael@0: if (fp.show() == nsIFilePicker.returnOK && fp.file) { michael@0: var localHandlerApp = michael@0: Components.classes["@mozilla.org/uriloader/local-handler-app;1"]. michael@0: createInstance(Components.interfaces.nsILocalHandlerApp); michael@0: localHandlerApp.executable = fp.file; michael@0: michael@0: this._incomingParams.handlerApp = localHandlerApp; michael@0: window.close(); michael@0: } michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: // Global object michael@0: var g_dialog = new AppPicker();