sbLibraryImporterManager.js
Go to the documentation of this file.
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4 //
5 // BEGIN SONGBIRD GPL
6 //
7 // This file is part of the Songbird web player.
8 //
9 // Copyright(c) 2005-2008 POTI, Inc.
10 // http://songbirdnest.com
11 //
12 // This file may be licensed under the terms of of the
13 // GNU General Public License Version 2 (the "GPL").
14 //
15 // Software distributed under the License is distributed
16 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
17 // express or implied. See the GPL for the specific language
18 // governing rights and limitations.
19 //
20 // You should have received a copy of the GPL along with this
21 // program. If not, go to http://www.gnu.org/licenses/gpl.html
22 // or write to the Free Software Foundation, Inc.,
23 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 //
25 // END SONGBIRD GPL
26 //
27  */
28 
34 //------------------------------------------------------------------------------
35 //------------------------------------------------------------------------------
36 //
37 // Library imoprter manager services.
38 //
39 //------------------------------------------------------------------------------
40 //------------------------------------------------------------------------------
41 
42 //------------------------------------------------------------------------------
43 //
44 // Library importer manager imported services.
45 //
46 //------------------------------------------------------------------------------
47 
48 // Songbird services.
49 Components.utils.import("resource://app/jsmodules/ArrayConverter.jsm");
50 Components.utils.import("resource://app/jsmodules/SBJobUtils.jsm");
51 Components.utils.import("resource://app/jsmodules/StringUtils.jsm");
52 Components.utils.import("resource://app/jsmodules/WindowUtils.jsm");
53 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
54 
55 
56 //------------------------------------------------------------------------------
57 //
58 // Library importer manager services defs.
59 //
60 //------------------------------------------------------------------------------
61 
62 // Component manager defs.
63 if (typeof(Cc) == "undefined")
64  var Cc = Components.classes;
65 if (typeof(Ci) == "undefined")
66  var Ci = Components.interfaces;
67 if (typeof(Cr) == "undefined")
68  var Cr = Components.results;
69 if (typeof(Cu) == "undefined")
70  var Cu = Components.utils;
71 
72 
73 //------------------------------------------------------------------------------
74 //
75 // Library importer manager services configuration.
76 //
77 //------------------------------------------------------------------------------
78 
79 //
80 // className Name of component class.
81 // cid Component CID.
82 // contractID Component contract ID.
83 // ifList List of external component interfaces.
84 // categoryList List of component categories.
85 //
86 
88  className: "Songbird Library Importer Manager Service",
89  cid: Components.ID("{0ed2a7e0-78ac-4574-8554-b1e422b02642}"),
90  contractID: "@songbirdnest.com/Songbird/LibraryImporterManager;1",
91  ifList: [ Ci.nsIObserver,
92  Ci.sbILibraryImporterManager,
93  Ci.sbILibraryImporterListener ]
94 };
95 
96 sbLibraryImporterManagerCfg.categoryList = [
97  {
98  category: "app-startup",
99  entry: sbLibraryImporterManagerCfg.className,
100  value: "service," + sbLibraryImporterManagerCfg.contractID
101  }
102 ];
103 
104 
105 //------------------------------------------------------------------------------
106 //
107 // Library importer manager services.
108 //
109 //------------------------------------------------------------------------------
110 
116 }
117 
118 // Define the object.
119 sbLibraryImporterManager.prototype = {
120  // Set the constructor.
122 
123  //
124  // Library importer manager services fields.
125  //
126  // classDescription Description of component class.
127  // classID Component class ID.
128  // contractID Component contract ID.
129  // _xpcom_categories List of component categories.
130  //
131  // _cfg Configuration settings.
132  // _isInitialized True if the library importer manager has been
133  // initialized.
134  // _libraryImporterList List of library importers.
135  // _prefsAvailable True if preferences are available.
136  // _observerSvc Observer services object.
137  //
138 
139  classDescription: sbLibraryImporterManagerCfg.className,
140  classID: sbLibraryImporterManagerCfg.cid,
141  contractID: sbLibraryImporterManagerCfg.contractID,
142  _xpcom_categories: sbLibraryImporterManagerCfg.categoryList,
143 
145  _isInitialized: false,
146  _libraryImporterList: null,
147  _prefsAvailable: false,
148  _observerSvc: null,
149 
150 
151  //----------------------------------------------------------------------------
152  //
153  // Library importer manager sbILibraryImporterManager services.
154  //
155  //----------------------------------------------------------------------------
156 
161  defaultLibraryImporter: null,
162 
163 
164  //----------------------------------------------------------------------------
165  //
166  // Library importer manager sbILibraryImporterListener services.
167  //
168  //----------------------------------------------------------------------------
169 
179  onLibraryChanged:
180  function sbLibraryImporterManager_onLibraryChanged(aLibFilePath, aGUID) {
181 
182  // Get the FUEL application services.
183  var Application = Cc["@mozilla.org/fuel/application;1"]
184  .getService(Ci.fuelIApplication);
185 
186  // Get library file path from which to import and previously imported
187  // library file path.
188  //XXXeps should use importer that called onLibraryChanged
189  var importer = this.defaultLibraryImporter;
190  var libraryFilePath = Application.prefs.getValue
191  ("songbird.library_importer.library_file_path",
192  "");
193  var libraryPreviousImportPath = importer.libraryPreviousImportPath;
194 
195  // Initiate import.
196  var job = importer.import(libraryFilePath, "songbird", false);
197 
198  // Get the library file from which to import. Set to null for bad paths.
199  var libraryFile = Cc["@mozilla.org/file/local;1"]
200  .createInstance(Ci.nsILocalFile);
201  try { libraryFile.initWithPath(libraryFilePath); }
202  catch (ex) { libaryFile = null; }
203 
204  // Get the previously imported library file. Set to null for bad paths.
205  var previousLibraryFile = Cc["@mozilla.org/file/local;1"]
206  .createInstance(Ci.nsILocalFile);
207  try { previousLibraryFile.initWithPath(libraryPreviousImportPath); }
208  catch (ex) { previousLibraryFile = null; }
209 
210  // Show progress dialog if importing from a new library file.
211  if (!libraryFile ||
212  !previousLibraryFile ||
213  !libraryFile.equals(previousLibraryFile)) {
214  SBJobUtils.showProgressDialog(job, null, 0, true);
215  }
216  },
217 
218 
224  onImportError: function sbLibraryImporterManager_onImportError() {
225  // Give user the option to try importing again.
226  var doImport = {};
227  WindowUtils.openModalDialog
228  (null,
229  "chrome://songbird/content/xul/importLibrary.xul",
230  "",
231  "chrome,centerscreen",
232  [ "error" ],
233  [ doImport ]);
234  doImport = (doImport.value == "true");
235 
236  // Import the library as user directs.
237  //XXXeps should use importer that called onImportError
238  if (doImport) {
239  var Application = Cc["@mozilla.org/fuel/application;1"]
240  .getService(Ci.fuelIApplication);
241  var libraryFilePath = Application.prefs.getValue
242  ("songbird.library_importer.library_file_path",
243  "");
244  this.defaultLibraryImporter.import(libraryFilePath, "songbird", false);
245  }
246  },
247 
248 
258  onNonExistentMedia:
259  function sbLibraryImporterManager_onNonExistentMedia(aNonExistentMediaCount,
260  aTrackCount) {
261  // Present a non-existent media alert dialog.
262  var prompter = Cc["@songbirdnest.com/Songbird/Prompter;1"]
263  .createInstance(Ci.sbIPrompter);
264  var alertTitle = SBBrandedString
265  ("import_library.nonexistent_media_alert.title");
266  var alertMsg =
267  SBFormattedString("import_library.nonexistent_media_alert.msg",
268  [ aNonExistentMediaCount, aTrackCount ]);
269  var songWin = Cc["@mozilla.org/appshell/window-mediator;1"]
270  .getService(Ci.nsIWindowMediator)
271  .getMostRecentWindow("Songbird:Main");
272 
273  prompter.alert(songWin, alertTitle, alertMsg);
274  },
275 
276 
282  onUnsupportedMedia: function sbLibraryImporterManager_onUnsupportedMedia() {
283  // Get unsupported media alert enabled preference.
284  var Application = Cc["@mozilla.org/fuel/application;1"]
285  .getService(Ci.fuelIApplication);
286  var alertEnabledPref =
287  "songbird.library_importer.unsupported_media_alert.enabled";
288  var alertEnabled = Application.prefs.getValue(alertEnabledPref, false);
289 
290  // Do nothing if alert not enabled.
291  if (!alertEnabled)
292  return;
293 
294  // Present an unsupported media alert dialog.
295  var prompter = Cc["@songbirdnest.com/Songbird/Prompter;1"]
296  .createInstance(Ci.sbIPrompter);
297  var alertTitle = SBBrandedString
298  ("import_library.unsupported_media_alert.title");
299  var alertMsg = SBString("import_library.unsupported_media_alert.msg");
300  var alertCheckMsg =
301  SBString("import_library.unsupported_media_alert.enable_label");
302  var checkState = { value: alertEnabled };
303 
304  var songWin = Cc["@mozilla.org/appshell/window-mediator;1"]
305  .getService(Ci.nsIWindowMediator)
306  .getMostRecentWindow("Songbird:Main");
307 
308  prompter.alertCheck(songWin,
309  alertTitle,
310  alertMsg,
311  alertCheckMsg,
312  checkState);
313  checkState = checkState.value;
314 
315  // Update the unsupported media alert enabled preference.
316  Application.prefs.setValue(alertEnabledPref, checkState);
317  },
318 
319 
334  onDirtyPlaylist:
335  function sbLibraryImporterManager_onDirtyPlaylist(aPlaylistName,
336  aApplyAll) {
337  // Present a dirty playlist dialog.
338  var action = {};
339  var applyAll = {};
340  WindowUtils.openModalDialog
341  (null,
342  "chrome://songbird/content/xul/dirtyPlaylistDialog.xul",
343  "",
344  "chrome,centerscreen",
345  [ aPlaylistName ],
346  [ action, applyAll ]);
347  action = action.value;
348  applyAll = (applyAll.value == "true");
349 
350  // Return results.
351  aApplyAll.value = applyAll;
352 
353  return action;
354  },
355 
356 
357  //----------------------------------------------------------------------------
358  //
359  // Library importer manager nsIObserver services.
360  //
361  //----------------------------------------------------------------------------
362 
371  observe: function sbLibraryImporterManager_observe(aSubject, aTopic, aData) {
372  // Dispatch processing of the event.
373  switch (aTopic) {
374  case "app-startup" :
375  this._handleAppStartup();
376  break;
377 
378  case "profile-after-change" :
379  this._handleProfileAfterChange();
380  break;
381 
382  case "songbird-library-manager-ready" :
383  this._handleLibraryManagerReady();
384  break;
385 
386  case "songbird-library-manager-before-shutdown" :
387  this._handleAppQuit();
388  break;
389 
390  default :
391  break;
392  }
393  },
394 
395 
396  //----------------------------------------------------------------------------
397  //
398  // Library importer manager nsISupports services.
399  //
400  //----------------------------------------------------------------------------
401 
402  QueryInterface: XPCOMUtils.generateQI(sbLibraryImporterManagerCfg.ifList),
403 
404 
405  //----------------------------------------------------------------------------
406  //
407  // Library importer manager event handler services.
408  //
409  //----------------------------------------------------------------------------
410 
415  _handleAppStartup: function sbLibraryImporterManager__handleAppStartup() {
416  // Initialize the services.
417  this._initialize();
418  },
419 
420 
425  _handleProfileAfterChange:
426  function sbLibraryImporterManager__handleProfileAfterChange() {
427  // Preferences are now available.
428  this._prefsAvailable = true;
429 
430  // Initialize the services.
431  this._initialize();
432  },
433 
434 
439  _handleLibraryManagerReady:
440  function sbLibraryImporterManager__handleLibraryManagerReady() {
441  // Initialize the services.
442  this._initialize();
443  },
444 
445 
450  _handleAppQuit: function sbLibraryImporterManager__handleAppQuit() {
451  // Finalize the services.
452  this._finalize();
453  },
454 
455 
456  //----------------------------------------------------------------------------
457  //
458  // Internal library importer manager services.
459  //
460  //----------------------------------------------------------------------------
461 
466  _initialize: function sbLibraryImporterManager__initialize() {
467  // Do nothing if already initialized.
468  if (this._isInitialized)
469  return;
470 
471  // Set up observers.
472  if (!this._observerSvc) {
473  this._observerSvc = Cc["@mozilla.org/observer-service;1"]
474  .getService(Ci.nsIObserverService);
475  this._observerSvc.addObserver(this, "songbird-library-manager-before-shutdown", false);
476  this._observerSvc.addObserver(this, "profile-after-change", false);
477  this._observerSvc.addObserver(this,
478  "songbird-library-manager-ready",
479  false);
480  }
481 
482  // Wait until preferences are available.
483  if (!this._prefsAvailable)
484  return;
485 
486  // Wait until the library manager services are available.
487  try {
488  var libraryManager = Cc["@songbirdnest.com/Songbird/library/Manager;1"]
489  .getService(Ci.sbILibraryManager);
490  if (!libraryManager.mainLibrary)
491  return;
492  } catch (ex) {
493  return;
494  }
495 
496  // Initialize the list of library importers.
497  this._libraryImporterList = [];
498 
499  // Add all library importers.
500  this._addAllLibraryImporters();
501 
502  // If the library file path preference is not set, set a default one.
503  var Application = Cc["@mozilla.org/fuel/application;1"]
504  .getService(Ci.fuelIApplication);
505  var hasLibraryFilePath =
506  Application.prefs.has("songbird.library_importer.library_file_path");
507  if (!hasLibraryFilePath && this.defaultLibraryImporter) {
508  var libraryDefaultFilePath =
509  this.defaultLibraryImporter.libraryDefaultFilePath;
510  if (libraryDefaultFilePath) {
511  Application.prefs.setValue
512  ("songbird.library_importer.library_file_path",
513  libraryDefaultFilePath);
514  }
515  }
516 
517  // Initialization is now complete.
518  this._isInitialized = true;
519 
520  // Initiate auto-import if configured to do so.
521  this._autoImport();
522  },
523 
524 
529  _finalize: function sbLibraryImporterManager__finalize() {
530  // Remove observers.
531  this._observerSvc.removeObserver(this, "songbird-library-manager-before-shutdown");
532  this._observerSvc.removeObserver(this, "profile-after-change");
533  this._observerSvc.removeObserver(this, "songbird-library-manager-ready");
534 
535  // Remove all library importers.
536  this._removeAllLibraryImporters();
537 
538  // Clear object references.
539  this._libraryImporterList = null;
540  this.defaultLibraryImporter = null;
541  },
542 
543 
548  _addAllLibraryImporters:
549  function sbLibraryImporterManager__addAllLibraryImporters() {
550  // Add all of the library importers.
551  var categoryManager = Cc["@mozilla.org/categorymanager;1"]
552  .getService(Ci.nsICategoryManager);
553  var libraryImporterEnum = categoryManager.enumerateCategory
554  ("library-importer");
555  while (libraryImporterEnum.hasMoreElements()) {
556  // Get the next library importer category entry.
557  var entry = libraryImporterEnum.getNext()
558  .QueryInterface(Ci.nsISupportsCString);
559 
560  // Get the library importer contract ID from the category entry value.
561  var contractID = categoryManager.getCategoryEntry("library-importer",
562  entry);
563 
564  // Add the library importer.
565  this._addLibraryImporter(contractID);
566  }
567  },
568 
569 
576  _addLibraryImporter:
577  function sbLibraryImporterManager__addLibraryImporter(aContractID) {
578  // Get the library importer.
579  var libraryImporter = Cc[aContractID].getService(Ci.sbILibraryImporter);
580 
581  // Initialize the library importer.
582  libraryImporter.initialize();
583 
584  // Set the library importer listener.
585  libraryImporter.setListener(this);
586 
587  // Add the library importer.
588  this._libraryImporterList.push(libraryImporter);
589 
590  // Update the default library importer.
591  this._updateDefaultLibraryImporter();
592  },
593 
594 
599  _removeAllLibraryImporters:
600  function sbLibraryImporterManager__removeAllLibraryImporters() {
601  for (var i = this._libraryImporterList.length - 1; i >= 0; i--) {
602  // Remove the next library importer from the library importer list.
603  var libraryImporter = this._libraryImporterList.pop();
604 
605  // Unset the library importer listener.
606  libraryImporter.setListener(null);
607 
608  // Finalize the library importer.
609  libraryImporter.finalize();
610  libraryImporter = null;
611  }
612  },
613 
614 
619  _updateDefaultLibraryImporter:
620  function sbLibraryImporterManager__updateDefaultLibraryImporter() {
621  // Choose the first library importer as the default.
622  if (!this.defaultLibraryImporter) {
623  if (this._libraryImporterList.length > 0)
624  this.defaultLibraryImporter = this._libraryImporterList[0];
625  }
626  },
627 
628 
633  _autoImport: function sbLibraryImporterManager_autoImport() {
634  // Get the auto-import preferences.
635  var Application = Cc["@mozilla.org/fuel/application;1"]
636  .getService(Ci.fuelIApplication);
637  var firstRunDoImportLibrary =
638  Application.prefs.getValue("songbird.firstrun.do_import_library",
639  false);
640  var importTracks = Application.prefs.getValue(
641  "songbird.library_importer.import_tracks", false);
642  var importPlaylists = Application.prefs.getValue(
643  "songbird.library_importer.import_playlists", false);
644 
645  // Do nothing if not auto-importing, or if this is first run.
646  if (!this.defaultLibraryImporter || firstRunDoImportLibrary ||
647  (!importTracks && !importPlaylists))
648  return;
649 
650  // Wait until the main Songbird window is ready before initiating
651  // auto-import. This ensures any import modal dialogs have a parent.
652  var windowWatcher = Cc["@songbirdnest.com/Songbird/window-watcher;1"]
653  .getService(Ci.sbIWindowWatcher);
654  var _this = this;
655  var func = function(aWindow) { _this._autoImportWithWindow(aWindow); };
656  windowWatcher.callWithWindow("Songbird:Main", func, false);
657  },
658 
659  _autoImportWithWindow:
660  function sbLibraryImporterManager_autoImportWithWindow(aWindow) {
661  // Initiate auto-import.
662  var Application = Cc["@mozilla.org/fuel/application;1"]
663  .getService(Ci.fuelIApplication);
664  var libraryFilePath = Application.prefs.getValue
665  ("songbird.library_importer.library_file_path",
666  "");
667  if (libraryFilePath)
668  this.defaultLibraryImporter.import(libraryFilePath, "songbird", true);
669  }
670 }
671 
672 
673 //------------------------------------------------------------------------------
674 //
675 // Library importer manager component services.
676 //
677 //------------------------------------------------------------------------------
678 
679 function NSGetModule(compMgr, fileSpec) {
680  return XPCOMUtils.generateModule([sbLibraryImporterManager]);
681 }
682 
function SBBrandedString(aKey, aDefault, aStringBundle)
const Cu
classDescription entry
Definition: FeedWriter.js:1427
const Cc
var Application
Definition: sbAboutDRM.js:37
function SBFormattedString(aKey, aParams, aDefault, aStringBundle)
sbOSDControlService prototype className
sbDeviceFirmwareAutoCheckForUpdate prototype contractID
sbOSDControlService prototype QueryInterface
sbDeviceFirmwareAutoCheckForUpdate prototype classDescription
function SBString(aKey, aDefault, aStringBundle)
Definition: StringUtils.jsm:93
DataRemote prototype constructor
var _this
function sbLibraryImporterManager()
var libraryManager
return null
Definition: FeedWriter.js:1143
countRef value
Definition: FeedWriter.js:1423
const Cr
const Ci
sbDeviceFirmwareAutoCheckForUpdate prototype classID
sbWindowsAutoPlayServiceCfg _xpcom_categories
_getSelectedPageStyle s i
_updateTextAndScrollDataForFrame aData
sbDeviceFirmwareAutoCheckForUpdate prototype observe
var sbLibraryImporterManagerCfg