|
1 // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components; |
|
7 |
|
8 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
9 Cu.import("resource://gre/modules/Services.jsm"); |
|
10 |
|
11 XPCOMUtils.defineLazyModuleGetter(this, "Downloads", |
|
12 "resource://gre/modules/Downloads.jsm"); |
|
13 |
|
14 XPCOMUtils.defineLazyModuleGetter(this, "Task", |
|
15 "resource://gre/modules/Task.jsm"); |
|
16 |
|
17 // ----------------------------------------------------------------------- |
|
18 // HelperApp Launcher Dialog |
|
19 // |
|
20 // For now on b2g we never prompt and just download to the default |
|
21 // location. |
|
22 // |
|
23 // ----------------------------------------------------------------------- |
|
24 |
|
25 function HelperAppLauncherDialog() { } |
|
26 |
|
27 HelperAppLauncherDialog.prototype = { |
|
28 classID: Components.ID("{710322af-e6ae-4b0c-b2c9-1474a87b077e}"), |
|
29 QueryInterface: XPCOMUtils.generateQI([Ci.nsIHelperAppLauncherDialog]), |
|
30 |
|
31 show: function(aLauncher, aContext, aReason) { |
|
32 aLauncher.MIMEInfo.preferredAction = Ci.nsIMIMEInfo.saveToDisk; |
|
33 aLauncher.saveToDisk(null, false); |
|
34 }, |
|
35 |
|
36 promptForSaveToFile: function(aLauncher, |
|
37 aContext, |
|
38 aDefaultFile, |
|
39 aSuggestedFileExt, |
|
40 aForcePrompt) { |
|
41 throw Cr.NS_ERROR_NOT_AVAILABLE; |
|
42 }, |
|
43 |
|
44 promptForSaveToFileAsync: function(aLauncher, |
|
45 aContext, |
|
46 aDefaultFile, |
|
47 aSuggestedFileExt, |
|
48 aForcePrompt) { |
|
49 // Retrieve the user's default download directory. |
|
50 Task.spawn(function() { |
|
51 let file = null; |
|
52 try { |
|
53 let defaultFolder = yield Downloads.getPreferredDownloadsDirectory(); |
|
54 let dir = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile); |
|
55 dir.initWithPath(defaultFolder); |
|
56 file = this.validateLeafName(dir, aDefaultFile, aSuggestedFileExt); |
|
57 } catch(e) { } |
|
58 aLauncher.saveDestinationAvailable(file); |
|
59 }.bind(this)).then(null, Cu.reportError); |
|
60 }, |
|
61 |
|
62 validateLeafName: function(aLocalFile, aLeafName, aFileExt) { |
|
63 if (!(aLocalFile && this.isUsableDirectory(aLocalFile))) |
|
64 return null; |
|
65 |
|
66 // Remove any leading periods, since we don't want to save hidden files |
|
67 // automatically. |
|
68 aLeafName = aLeafName.replace(/^\.+/, ""); |
|
69 |
|
70 if (aLeafName == "") |
|
71 aLeafName = "unnamed" + (aFileExt ? "." + aFileExt : ""); |
|
72 aLocalFile.append(aLeafName); |
|
73 |
|
74 this.makeFileUnique(aLocalFile); |
|
75 return aLocalFile; |
|
76 }, |
|
77 |
|
78 makeFileUnique: function(aLocalFile) { |
|
79 try { |
|
80 // Note - this code is identical to that in |
|
81 // toolkit/content/contentAreaUtils.js. |
|
82 // If you are updating this code, update that code too! We can't share code |
|
83 // here since this is called in a js component. |
|
84 let collisionCount = 0; |
|
85 while (aLocalFile.exists()) { |
|
86 collisionCount++; |
|
87 if (collisionCount == 1) { |
|
88 // Append "(2)" before the last dot in (or at the end of) the filename |
|
89 // special case .ext.gz etc files so we don't wind up with .tar(2).gz |
|
90 if (aLocalFile.leafName.match(/\.[^\.]{1,3}\.(gz|bz2|Z)$/i)) |
|
91 aLocalFile.leafName = aLocalFile.leafName.replace(/\.[^\.]{1,3}\.(gz|bz2|Z)$/i, "(2)$&"); |
|
92 else |
|
93 aLocalFile.leafName = aLocalFile.leafName.replace(/(\.[^\.]*)?$/, "(2)$&"); |
|
94 } |
|
95 else { |
|
96 // replace the last (n) in the filename with (n+1) |
|
97 aLocalFile.leafName = aLocalFile.leafName.replace(/^(.*\()\d+\)/, "$1" + (collisionCount+1) + ")"); |
|
98 } |
|
99 } |
|
100 aLocalFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0600); |
|
101 } |
|
102 catch (e) { |
|
103 dump("*** exception in makeFileUnique: " + e + "\n"); |
|
104 |
|
105 if (e.result == Cr.NS_ERROR_FILE_ACCESS_DENIED) |
|
106 throw e; |
|
107 |
|
108 if (aLocalFile.leafName == "" || aLocalFile.isDirectory()) { |
|
109 aLocalFile.append("unnamed"); |
|
110 if (aLocalFile.exists()) |
|
111 aLocalFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600); |
|
112 } |
|
113 } |
|
114 }, |
|
115 |
|
116 isUsableDirectory: function(aDirectory) { |
|
117 return aDirectory.exists() && |
|
118 aDirectory.isDirectory() && |
|
119 aDirectory.isWritable(); |
|
120 }, |
|
121 }; |
|
122 |
|
123 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([HelperAppLauncherDialog]); |