|
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/. |
|
6 |
|
7 function AppPicker() {}; |
|
8 |
|
9 var g_dialog = null; |
|
10 |
|
11 AppPicker.prototype = |
|
12 { |
|
13 // Class members |
|
14 _incomingParams:null, |
|
15 |
|
16 /** |
|
17 * Init the dialog and populate the application list |
|
18 */ |
|
19 appPickerLoad: function appPickerLoad() { |
|
20 const nsILocalHandlerApp = Components.interfaces.nsILocalHandlerApp; |
|
21 |
|
22 this._incomingParams = window.arguments[0]; |
|
23 this._incomingParams.handlerApp = null; |
|
24 |
|
25 document.title = this._incomingParams.title; |
|
26 |
|
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) |
|
38 |
|
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 } |
|
49 |
|
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); |
|
58 |
|
59 // Grab a list of nsILocalHandlerApp application helpers to list |
|
60 var fileList = mimeInfo.possibleLocalHandlers; |
|
61 |
|
62 var list = document.getElementById("app-picker-listbox"); |
|
63 |
|
64 var primaryCount = 0; |
|
65 |
|
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 } |
|
71 |
|
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 } |
|
80 |
|
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); |
|
87 |
|
88 primaryCount++; |
|
89 } |
|
90 |
|
91 if ( primaryCount == 0 ) { |
|
92 // display a message saying nothing is configured |
|
93 document.getElementById("app-picker-notfound").removeAttribute("hidden"); |
|
94 } |
|
95 }, |
|
96 |
|
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); |
|
103 |
|
104 if (!ios) return ""; |
|
105 const nsIFileProtocolHandler = |
|
106 Components.interfaces.nsIFileProtocolHandler; |
|
107 |
|
108 var fph = ios.getProtocolHandler("file") |
|
109 .QueryInterface(nsIFileProtocolHandler); |
|
110 if (!fph) return ""; |
|
111 |
|
112 var urlSpec = fph.getURLSpecFromFile(file); |
|
113 return "moz-icon://" + urlSpec + "?size=32"; |
|
114 }, |
|
115 |
|
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 }, |
|
136 |
|
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; |
|
143 |
|
144 if (!selItem) { |
|
145 this._incomingParams.handlerApp = null; |
|
146 return true; |
|
147 } |
|
148 |
|
149 this._incomingParams.handlerApp = selItem.handlerApp; |
|
150 window.close(); |
|
151 |
|
152 return true; |
|
153 }, |
|
154 |
|
155 appPickerOK: function appPickerOK() { |
|
156 if (this._incomingParams.handlerApp) return true; |
|
157 |
|
158 var list = document.getElementById("app-picker-listbox"); |
|
159 var selItem = list.selectedItem; |
|
160 |
|
161 if (!selItem) { |
|
162 this._incomingParams.handlerApp = null; |
|
163 return true; |
|
164 } |
|
165 this._incomingParams.handlerApp = selItem.handlerApp; |
|
166 |
|
167 return true; |
|
168 }, |
|
169 |
|
170 appPickerCancel: function appPickerCancel() { |
|
171 this._incomingParams.handlerApp = null; |
|
172 return true; |
|
173 }, |
|
174 |
|
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); |
|
182 |
|
183 fp.init(window, this._incomingParams.title, nsIFilePicker.modeOpen); |
|
184 fp.appendFilters(nsIFilePicker.filterApps); |
|
185 |
|
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); |
|
200 |
|
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; |
|
206 |
|
207 this._incomingParams.handlerApp = localHandlerApp; |
|
208 window.close(); |
|
209 } |
|
210 return true; |
|
211 } |
|
212 } |
|
213 |
|
214 // Global object |
|
215 var g_dialog = new AppPicker(); |