27 Components.utils.import(
"resource://gre/modules/XPCOMUtils.jsm");
28 Components.utils.import(
"resource://app/jsmodules/sbProperties.jsm");
29 Components.utils.import(
"resource://app/jsmodules/ArrayConverter.jsm");
30 Components.utils.import(
"resource://app/jsmodules/FolderUtils.jsm");
32 const Ci = Components.interfaces;
33 const Cc = Components.classes;
34 const Cr = Components.results;
35 const Cu = Components.utils;
36 const CE = Components.Exception;
39 video:
"songbird.download.video.folder"};
41 video:
"songbird.download.video.alwaysPrompt"};
43 Ci.sbIMediaFileManager.MANAGE_COPY |
44 Ci.sbIMediaFileManager.MANAGE_RENAME;
58 Components.classes[
"@mozilla.org/system-info;1"]
59 .getService(Components.interfaces.nsIPropertyBag2);
60 return sysInfo.getProperty(
"name");
63 dump(
"System-info not available, trying the user agent string.\n");
64 var user_agent = navigator.userAgent;
65 if (user_agent.indexOf(
"Windows") != -1)
67 else if (user_agent.indexOf(
"Mac OS X") != -1)
69 else if (user_agent.indexOf(
"Linux") != -1)
71 else if (user_agent.indexOf(
"SunOS") != -1)
82 return folder && folder.isDirectory();
93 var
file =
Cc[
"@mozilla.org/file/local;1"].
98 var actualPath =
path;
100 actualPath = actualPath.toLowerCase();
104 file.initWithPath(actualPath);
122 var actualPath =
path;
124 actualPath = actualPath.toLowerCase();
127 return "file://" + actualPath;
132 this._mainLibrary =
Cc[
"@songbirdnest.com/Songbird/library/Manager;1"]
133 .getService(
Ci.sbILibraryManager).mainLibrary;
139 classID: Components.ID(
"{576b6833-15d8-483a-84d6-2fbd329c82e1}"),
140 contractID:
"@songbirdnest.com/Songbird/DownloadDeviceHelper;1",
145 function sbDownloadDeviceHelper_downloadItem(aMediaItem)
147 var downloadFolder = this._getDownloadFolder_nothrow(aMediaItem.contentType);
148 if (!downloadFolder) {
152 let uriArray =
Cc[
"@songbirdnest.com/moz/xpcom/threadsafe-array;1"]
153 .createInstance(
Ci.nsIMutableArray);
154 let propertyArrayArray =
Cc[
"@songbirdnest.com/moz/xpcom/threadsafe-array;1"]
155 .createInstance(
Ci.nsIMutableArray);
158 if (aMediaItem.library.equals(
this._mainLibrary)) {
162 this._addItemToArrays(aMediaItem, uriArray, propertyArrayArray);
163 let items = this._mainLibrary.batchCreateMediaItems(uriArray,
166 item = items.queryElementAt(0,
Ci.sbIMediaItem);
172 this._setDownloadDestinationIfNotSet([item],
173 {
'media-folder': downloadFolder});
174 this._checkAndRemoveExistingItem(item);
175 this.getDownloadMediaList().add(item);
179 function sbDownloadDeviceHelper_downloadSome(aMediaItems)
181 let uriArray =
Cc[
"@songbirdnest.com/moz/xpcom/threadsafe-array;1"]
182 .createInstance(
Ci.nsIMutableArray);
183 let propertyArrayArray =
Cc[
"@songbirdnest.com/moz/xpcom/threadsafe-array;1"]
184 .createInstance(
Ci.nsIMutableArray);
188 while (aMediaItems.hasMoreElements()) {
189 let item = aMediaItems.getNext();
190 if (item.library.equals(
this._mainLibrary)) {
194 this._checkAndRemoveExistingItem(item);
195 this._addItemToArrays(item, uriArray, propertyArrayArray);
200 foundTypes[item.contentType] =
true;
211 for (let contentType in foundTypes) {
212 let folder = this.getDownloadFolder(contentType);
214 mfmFolders[
"media-folder:" + contentType] = folder;
218 catch (e
if e.result ==
Cr.NS_ERROR_ABORT) {
222 if (uriArray.length > 0) {
223 let addedItems = this._mainLibrary.batchCreateMediaItems(uriArray,
226 for (let
i = 0;
i < addedItems.length;
i++) {
227 items.push(addedItems.queryElementAt(
i,
Ci.sbIMediaItem));
231 this._setDownloadDestinationIfNotSet(items, mfmFolders);
232 this.getDownloadMediaList().addSome(ArrayConverter.enumerator(items));
236 function sbDownloadDeviceHelper_downloadAll(aMediaList)
238 let uriArray =
Cc[
"@songbirdnest.com/moz/xpcom/threadsafe-array;1"]
239 .createInstance(
Ci.nsIMutableArray);
240 let propertyArrayArray =
Cc[
"@songbirdnest.com/moz/xpcom/threadsafe-array;1"]
241 .createInstance(
Ci.nsIMutableArray);
245 var isForeign = aMediaList.library.equals(this._mainLibrary);
246 for (let
i = 0;
i < aMediaList.length;
i++) {
247 var item = aMediaList.getItemByIndex(
i);
249 this._checkAndRemoveExistingItem(item);
250 this._addItemToArrays(item, uriArray, propertyArrayArray);
258 foundTypes[item.contentType] =
true;
269 for (let contentType in foundTypes) {
270 let folder = this.getDownloadFolder(contentType);
272 mfmFolders[
"media-folder:" + contentType] = folder;
276 catch (e
if e.result ==
Cr.NS_ERROR_ABORT) {
280 if (uriArray.length > 0) {
281 let addedItems = this._mainLibrary.batchCreateMediaItems(uriArray,
284 for (let
i = 0;
i < addedItems.length;
i++) {
285 items.push(addedItems.queryElementAt(
i,
Ci.sbIMediaItem));
289 this._setDownloadDestinationIfNotSet(items, mfmFolders);
290 this.getDownloadMediaList().addSome(ArrayConverter.enumerator(items));
294 function sbDownloadDeviceHelper_getDownloadMediaList()
296 if (!this._downloadDevice) {
297 var devMgr =
Cc[
"@songbirdnest.com/Songbird/DeviceManager;1"]
298 .getService(
Ci.sbIDeviceManager);
300 var downloadCat =
"Songbird Download Device";
301 if (devMgr.hasDeviceForCategory(downloadCat)) {
302 this._downloadDevice = devMgr.getDeviceByCategory(downloadCat)
303 .QueryInterface(
Ci.sbIDownloadDevice);
307 return this._downloadDevice ? this._downloadDevice.downloadMediaList :
null;
311 function sbDownloadDeviceHelper_getDefaultDownloadFolder(aContentType)
313 var mediaDir = FolderUtils.getDownloadFolder(aContentType);
316 Cu.reportError(
"Desktop directory is not a directory!");
317 throw Cr.NS_ERROR_FILE_NOT_DIRECTORY;
324 function sbDownloadDeviceHelper_getDownloadFolder(aContentType)
337 downloadFolder =
makeFile(downloadPath);
341 downloadFolder = this.getDefaultDownloadFolder(aContentType);
342 if (downloadFolder) {
349 return downloadFolder;
352 const sbs =
Cc[
"@mozilla.org/intl/stringbundle;1"].
355 sbs.createBundle(
"chrome://songbird/locale/songbird.properties");
356 const title =
strings.GetStringFromName(
357 "prefs.main.downloads." + aContentType +
".chooseTitle");
359 const wm =
Cc[
"@mozilla.org/appshell/window-mediator;1"].
361 const mainWin =
wm.getMostRecentWindow(
"Songbird:Main");
364 const folderPicker =
Cc[
"@mozilla.org/filepicker;1"].
366 folderPicker.init(mainWin, title,
Ci.nsIFilePicker.modeGetFolder);
367 folderPicker.displayDirectory = downloadFolder;
369 if (folderPicker.show() !=
Ci.nsIFilePicker.returnOK) {
372 throw new CE(
"User canceled the download dialog.",
Cr.NS_ERROR_ABORT);
375 downloadFolder = folderPicker.file;
378 return downloadFolder;
382 function sbDownloadDeviceHelper__checkAndRemoveExistingItem(aMediaItem)
384 var downloadMediaList = this.getDownloadMediaList();
385 if (downloadMediaList) {
386 var itemOriginUrl = aMediaItem.getProperty(SBProperties.originURL);
388 var listEnumerationListener = {
389 onEnumerationBegin:
function() {
391 onEnumeratedItem:
function(list, item) {
392 downloadMediaList.remove(item);
394 onEnumerationEnd:
function() {
398 if ( (itemOriginUrl !=
"") &&
399 (itemOriginUrl !=
null) ) {
400 downloadMediaList.enumerateItemsByProperty(SBProperties.originURL,
402 listEnumerationListener);
408 function sbDownloadDeviceHelper__getDownloadFolder_nothrow(aContentType)
412 return this.getDownloadFolder(aContentType);
414 catch (e
if e.result ==
Cr.NS_ERROR_ABORT) {
420 function sbDownloadDeviceHelper__addItemToArrays(aMediaItem,
424 aURIArray.appendElement(aMediaItem.contentSrc,
false);
426 var dest = SBProperties.createArray();
427 var source = aMediaItem.getProperties();
428 for (let
i = 0;
i < source.length;
i++) {
429 var prop = source.getPropertyAt(
i);
433 if (prop.id != SBProperties.contentSrc &&
434 prop.id != SBProperties.enableAutoDownload)
436 dest.appendElement(prop,
false);
440 var
target = aMediaItem.library.guid +
"," + aMediaItem.guid;
441 dest.appendProperty(SBProperties.downloadStatusTarget, target);
443 aPropertyArrayArray.appendElement(dest,
false);
447 function sbDownloadDeviceHelper__setDownloadDestinationIfNotSet(
449 aMediaFileManagerOptions)
452 var bag =
Cc[
"@mozilla.org/hash-property-bag;1"]
453 .createInstance(
Ci.nsIWritablePropertyBag);
454 for (prop in aMediaFileManagerOptions) {
455 bag.setProperty(prop, aMediaFileManagerOptions[prop]);
461 var mediaFileMgr =
Cc[
"@songbirdnest.com/Songbird/media-manager/file;1"]
462 .createInstance(
Ci.sbIMediaFileManager);
463 mediaFileMgr.init(bag);
465 for each (var item
in aItems) {
467 var curDestination = item.getProperty(SBProperties.destination);
468 if (!curDestination) {
469 let
destFile = mediaFileMgr.getManagedPath(
472 let
path = destFile.path;
479 if (!item.getProperty(SBProperties.trackName)) {
481 path = path.replace(/[^/\\]*$/,
"");
488 actualDestination = actualDestination.toLowerCase();
491 item.setProperty(SBProperties.destination, actualDestination);
499 throw new CE(
"Download destination could not be set on this media item:\n"
500 +
" " + item +
"\nIs it actually a remote media item?",
function getPlatformString()
Get the name of the platform we are running on.
sbDeviceFirmwareAutoCheckForUpdate prototype contractID
sbOSDControlService prototype QueryInterface
sbDeviceFirmwareAutoCheckForUpdate prototype classDescription
getService(Ci.sbIFaceplateManager)
const _MFM_GET_MANAGED_PATH_OPTIONS
function sbDownloadDeviceHelper()
function folderIsValid(folder)
sbDeviceFirmwareAutoCheckForUpdate prototype classID
const PREF_DOWNLOAD_FOLDER
_getSelectedPageStyle s i
function makeFileURL(path)
const PREF_DOWNLOAD_ALWAYSPROMPT