mainWinInit.js
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
25 //
26 // Mainwin Initialization
27 //
28 // XXX: This functionality was copied from the original 0.1
29 // songbird_hack.js and desperately needs to be rewritten.
30 //
31 
32 
40 
41 Components.utils.import("resource://app/jsmodules/sbSmartMediaListColumnSpecUpdater.jsm");
42 Components.utils.import("resource://app/jsmodules/sbLibraryUtils.jsm");
43 
44 //
45 // Module specific auto-init/deinit support
46 //
47 var mainWinInit = {};
48 mainWinInit.init_once = 0;
49 mainWinInit.deinit_once = 0;
50 mainWinInit.onLoad = function()
51 {
52  if (mainWinInit.init_once++) { dump("WARNING: mainWinInit double init!!\n"); return; }
53  SBInitialize();
54 }
55 mainWinInit.onUnload = function()
56 {
57  if (mainWinInit.deinit_once++) { dump("WARNING: mainWinInit double deinit!!\n"); return; }
58  window.removeEventListener("load", mainWinInit.onLoad, false);
59  window.removeEventListener("unload", mainWinInit.onUnload, false);
60  document.removeEventListener("sb-overlay-load", SBPostOverlayLoad, false);
62 }
63 
69 function checkFirstRun()
70 {
71  var firstRunDoImportLibrary =
72  Application.prefs.getValue("songbird.firstrun.do_import_library",
73  false);
74  if (firstRunDoImportLibrary) {
75  Application.prefs.setValue("songbird.firstrun.skip_load_default_page",
76  true);
77  }
78 }
79 
80 checkFirstRun();
81 
82 window.addEventListener("load", mainWinInit.onLoad, false);
83 window.addEventListener("unload", mainWinInit.onUnload, false);
84 document.addEventListener("sb-overlay-load", SBPostOverlayLoad, false);
85 
86 
87 
93 function SBInitialize()
94 {
95  try
96  {
97  initializeDocumentPlatformAttribute();
98 
99  // Delay setting the min max callback to enable the reflow of the mainwin to
100  // happen. The reflow is caused by restoring the window size and position (or
101  // using the default size from xul/css).
102  // See bug #5185.
103  setTimeout("setMinMaxCallback()", 25);
104 
105  if (window.addEventListener)
106  window.addEventListener("keydown", checkQuitKey, true);
107  }
108  catch(err)
109  {
110  alert("mainWinInit.js - SBInitialize - " + err);
111  }
112 }
113 
119 function SBUninitialize()
120 {
121  window.removeEventListener("keydown", checkQuitKey, true);
122 
123  window.gServicePane = null;
124  window.gBrowser = null;
125 
127 }
128 
130 {
131  // Shrink until the box doesn't match the window, then stop.
132  GetMinWidth: function()
133  {
134  return 700;
135  },
136 
137  GetMinHeight: function()
138  {
139  return 400;
140  },
141 
142  GetMaxWidth: function()
143  {
144  return -1;
145  },
146 
147  GetMaxHeight: function()
148  {
149  return -1;
150  },
151 
152  OnWindowClose: function()
153  {
154  setTimeout(quitApp, 0);
155  },
156 
157  QueryInterface : function(aIID)
158  {
159  if (!aIID.equals(Components.interfaces.sbIWindowMinMaxCallback) &&
160  !aIID.equals(Components.interfaces.nsISupportsWeakReference) &&
161  !aIID.equals(Components.interfaces.nsISupports))
162  {
163  throw Components.results.NS_ERROR_NO_INTERFACE;
164  }
165 
166  return this;
167  }
168 }; // SBWindowMinMax callback class definition
169 
170 function setMinMaxCallback(evt)
171 {
172  var platfrom = getPlatformString();
173 
174  try {
175 
176  if (platfrom == "Windows_NT") {
177  var windowMinMax = Components.classes["@songbirdnest.com/Songbird/WindowMinMax;1"];
178  var service = windowMinMax.getService(Components.interfaces.sbIWindowMinMax);
179 
180  service.setCallback(window, SBWindowMinMaxCB);
181  return;
182  }
183 
184  }
185  catch (err) {
186  // No component
187  dump("Error. songbird_hack.js:setMinMaxCallback() \n " + err + "\n");
188  }
189 
190  return;
191 }
192 
194 {
195  var platform = getPlatformString();
196 
197  try
198  {
199  if (platform == "Windows_NT") {
200  var windowMinMax = Components.classes["@songbirdnest.com/Songbird/WindowMinMax;1"];
201  var service = windowMinMax.getService(Components.interfaces.sbIWindowMinMax);
202  service.resetCallback(window);
203 
204  return;
205  }
206 
207  }
208  catch(err) {
209  dump("Error. songbird_hack.js: SBUnitialize() \n" + err + "\n");
210  }
211 
212  return;
213 }
214 
216 {
217  // Handle first run media import.
218  // Delay this until later, to give the session restore code time to load the
219  // placeholder page if we are not importing anything
220  setTimeout(SBDoFirstRun, 500);
221 
222 }
223 
224 // Find media on first run
225 function SBDoFirstRun() {
226 
227  // Run first-run library import.
228  var job = SBFirstRunImportLibrary();
229 
230  // Run first-run directory scan.
231  job = SBFirstRunScanDirectories() || job;
232 
233  // determine whether to load the media library in the background by preference
234  // so it can be overriden by partners in the distribution.ini
235  var loadMLInBackground =
236  Application.prefs.getValue("songbird.firstrun.load_ml_in_background", false);
237 
238  // Prepare to notify observers when the library is ready
239  let obs = Components.classes["@mozilla.org/observer-service;1"]
240  .getService(Components.interfaces.nsIObserverService);
241 
242  // If we are scanning directories or importing a library,
243  // track the progress and show the library on completion.
244  // This is done to simplify the display, avoid some bugs,
245  // and improve performance.
246  if (job) {
247  function onJobComplete() {
248  // load the main library in the media tab / first tab
249  const nsIWebNavigation = Components.interfaces.nsIWebNavigation;
250  var mediaListView =
251  LibraryUtils.createConstrainedMediaListView(
252  LibraryUtils.mainLibrary, [SBProperties.contentType, "audio"]);
253  gBrowser.loadMediaListViewWithFlags(mediaListView,
254  gBrowser.mediaTab,
255  null,
256  nsIWebNavigation.LOAD_FLAGS_REPLACE_HISTORY,
257  loadMLInBackground);
258 
259  // Set up the smart playlists after import is complete
260  // (improves performance slightly)
262 
263  obs.notifyObservers(null, "songbird-main-library-ready", null);
264  }
265  if (job.status != Ci.sbIJobProgress.STATUS_RUNNING)
266  setTimeout(onJobComplete, 100);
267  else {
268  job.addJobProgressListener(function firstRunLibraryHider(){
269  // only do anything on complete
270  if (job.status == Ci.sbIJobProgress.STATUS_RUNNING) {
271  return;
272  }
273  // unhook the listener
274  job.removeJobProgressListener(arguments.callee);
275  // We have to do this from the "main thread" via the timer
276  setTimeout(onJobComplete, 100);
277  });
278  }
279  } else {
280  // Make sure we have the default smart playlists
282 
283  var isFirstRun =
284  Application.prefs.getValue("songbird.firstrun.is_session", false);
285  var mediaListView =
286  LibraryUtils.createConstrainedMediaListView(
287  LibraryUtils.mainLibrary, [SBProperties.contentType, "audio"]);
288  if (isFirstRun) {
289  const placeholderURL = "chrome://songbird/content/mediapages/firstrun.xul";
290  var currentURI = gBrowser.mediaTab.linkedBrowser.currentURI.spec;
291  if (currentURI == placeholderURL || currentURI == "about:blank") {
292  const nsIWebNavigation = Components.interfaces.nsIWebNavigation;
293  gBrowser.loadMediaListViewWithFlags(mediaListView,
294  gBrowser.mediaTab,
295  null,
296  nsIWebNavigation.LOAD_FLAGS_REPLACE_HISTORY,
297  loadMLInBackground);
298  }
299  }
300 
301  obs.notifyObservers(null, "songbird-main-library-ready", null);
302  }
303 }
304 
306 {
307  // Do nothing if not set to scan directories.
308  var firstRunDoScanDirectory =
309  Application.prefs.getValue("songbird.firstrun.do_scan_directory",
310  false);
311  if (!firstRunDoScanDirectory) {
312  return null;
313  }
314 
315  // Don't do a first-run directory scan again. Flush to disk to be sure.
316  Application.prefs.setValue("songbird.firstrun.do_scan_directory", false);
317  var prefService = Cc["@mozilla.org/preferences-service;1"]
318  .getService(Ci.nsIPrefService);
319  prefService.savePrefFile(null);
320 
321  // Get the first-run scan directory.
322  var firstRunScanDirectoryPath =
323  Application.prefs.getValue("songbird.firstrun.scan_directory_path", "");
324  var firstRunScanDirectory = Cc["@mozilla.org/file/local;1"]
325  .createInstance(Ci.nsILocalFile);
326  try {
327  firstRunScanDirectory.initWithPath(firstRunScanDirectoryPath);
328  } catch (ex) {
329  firstRunScanDirectory = null;
330  }
331 
332  var job = null;
333 
334  // Start scanning. Report error if scan directory does not exist.
335  if (firstRunScanDirectory && firstRunScanDirectory.exists()) {
336  job = SBScanMedia(null, firstRunScanDirectory);
337  } else {
338  Cu.reportError("Scan directory does not exist: \"" +
339  firstRunScanDirectoryPath + "\"\n");
340  }
341  return job;
342 }
343 
345 {
346  // Do nothing if not set to import library.
347  var firstRunDoImportLibrary =
348  Application.prefs.getValue("songbird.firstrun.do_import_library",
349  false);
350  if (!firstRunDoImportLibrary)
351  return null;
352 
353  // Don't do a first-run library import again. Flush to disk to be sure.
354  Application.prefs.setValue("songbird.firstrun.do_import_library", false);
355  var prefService = Cc["@mozilla.org/preferences-service;1"]
356  .getService(Ci.nsIPrefService);
357  prefService.savePrefFile(null);
358 
359  // Import library.
360  return SBLibraryOpen(null, true);
361 }
362 
364  // Do nothing if first-run smart playlists were already created in the main
365  // library.
366  var libraryManager =
367  Components.classes["@songbirdnest.com/Songbird/library/Manager;1"]
368  .getService(Components.interfaces.sbILibraryManager);
369  var library = libraryManager.mainLibrary;
370  if (library.getProperty(SBProperties.createdFirstRunSmartPlaylists) == "1")
371  return;
372 
373  // For backward compatibility, check preferences to see if the first-run smart
374  // playlists were already created.
375  var defaultSmartPlaylists = SBDataGetIntValue("firstrun.smartplaylist");
376  if (defaultSmartPlaylists) {
377  library.setProperty(SBProperties.createdFirstRunSmartPlaylists, "1");
378  return;
379  }
380 
381  library.setProperty(SBProperties.createdFirstRunSmartPlaylists, "1");
383 }
384 
386  var defaultSmartPlaylists = [];
387 
388  var propertyManager =
389  Cc["@songbirdnest.com/Songbird/Properties/PropertyManager;1"]
390  .getService(Ci.sbIPropertyManager);
391  var numberPI =
392  propertyManager.getPropertyInfo(SBProperties.playCount);
393  var ratingPI =
394  propertyManager.getPropertyInfo(SBProperties.rating);
395  var datePI =
396  propertyManager.getPropertyInfo(SBProperties.created);
397  var typePI =
398  propertyManager.getPropertyInfo(SBProperties.contentType);
399 
400  const sbILDSML = Components.interfaces.sbILocalDatabaseSmartMediaList;
401 
402  datePI.QueryInterface(Components.interfaces.sbIDatetimePropertyInfo);
403 
404  // XXXlone> waiting for a patch to land before autoUpdateMode fields
405  // can be enabled
406 
407  defaultSmartPlaylists = [
408  {
409  name: "&smart.defaultlist.highestrated",
410  conditions: [
411  {
412  property : SBProperties.rating,
413  operator : ratingPI.getOperator(ratingPI.OPERATOR_GREATER),
414  leftValue : 3,
415  rightValue : null,
416  displayUnit : null
417  },
418  {
419  property : SBProperties.contentType,
420  operator : typePI.getOperator(typePI.OPERATOR_NOTEQUALS),
421  leftValue : "video",
422  rightValue : null,
423  displayUnit : null
424  }
425  ],
426  matchType : sbILDSML.MATCH_TYPE_ALL,
427  limitType : sbILDSML.LIMIT_TYPE_NONE,
428  limit : 0,
429  selectPropertyID : SBProperties.rating,
430  selectDirection : false,
431  randomSelection : false,
432  autoUpdate : true
433  },
434  {
435  name: "&smart.defaultlist.recentlyadded",
436  conditions: [
437  {
438  property : SBProperties.created,
439  operator : datePI.getOperator(datePI.OPERATOR_INTHELAST),
440  leftValue : 1000*60*60*24*30, // 30 days
441  rightValue : null,
442  displayUnit : "m"
443  },
444  {
445  property : SBProperties.contentType,
446  operator : typePI.getOperator(typePI.OPERATOR_NOTEQUALS),
447  leftValue : "video",
448  rightValue : null,
449  displayUnit : null
450  }
451  ],
452  matchType : sbILDSML.MATCH_TYPE_ALL,
453  limitType : sbILDSML.LIMIT_TYPE_NONE,
454  limit : 0,
455  selectPropertyID : SBProperties.created,
456  selectDirection : false,
457  randomSelection : false,
458  autoUpdate : true
459  },
460  {
461  name: "&smart.defaultlist.recentlyplayed",
462  conditions: [
463  {
464  property : SBProperties.lastPlayTime,
465  operator : datePI.getOperator(datePI.OPERATOR_INTHELAST),
466  leftValue : 1000*60*60*24*7, // 7 days
467  rightValue : null,
468  displayUnit : "w"
469  },
470  {
471  property : SBProperties.contentType,
472  operator : typePI.getOperator(typePI.OPERATOR_NOTEQUALS),
473  leftValue : "video",
474  rightValue : null,
475  displayUnit : null
476  }
477  ],
478  matchType : sbILDSML.MATCH_TYPE_ALL,
479  limitType : sbILDSML.LIMIT_TYPE_NONE,
480  limit : 0,
481  selectPropertyID : SBProperties.lastPlayTime,
482  selectDirection : false,
483  randomSelection : false,
484  autoUpdate : true
485  }
486  ];
487 
488  for each (var item in defaultSmartPlaylists) {
489  addSmartPlaylist(item);
490  }
491 }
492 
493 function addSmartPlaylist(aItem) {
494  var libraryManager =
495  Components.classes["@songbirdnest.com/Songbird/library/Manager;1"]
496  .getService(Components.interfaces.sbILibraryManager);
497 
498  library = libraryManager.mainLibrary;
499  var mediaList = library.createMediaList("smart");
500  for (var prop in aItem) {
501  if (prop == "conditions") {
502  for each (var condition in aItem.conditions) {
503  mediaList.appendCondition(condition.property,
504  condition.operator,
505  condition.leftValue,
506  condition.rightValue,
507  condition.displayUnit);
508  }
509  } else {
510  mediaList[prop] = aItem[prop];
511  }
512  }
513  SmartMediaListColumnSpecUpdater.update(mediaList);
514  mediaList.rebuild();
515 }
const Cu
const Cc
var SBWindowMinMaxCB
Definition: mainWinInit.js:129
function SBDoFirstRun()
Definition: mainWinInit.js:225
id service()
var Application
Definition: sbAboutDRM.js:37
function getPlatformString()
Get the name of the platform we are running on.
function SBFirstRunScanDirectories()
Definition: mainWinInit.js:305
function addSmartPlaylist(aItem)
Definition: mainWinInit.js:493
populateBox limit
Definition: tuner2.js:877
function SBDataGetIntValue(aKey)
Get the value of the data in integer format.
function SBFirstRunImportLibrary()
Definition: mainWinInit.js:344
sbOSDControlService prototype QueryInterface
let window
function SBPostOverlayLoad()
Definition: mainWinInit.js:215
const nsIWebNavigation
Definition: browser.js:71
var libraryManager
aWindow setTimeout(function(){_this.restoreHistory(aWindow, aTabs, aTabData, aIdMap);}, 0)
function SBUninitialize()
Uninitialize the main window.
Definition: mainWinInit.js:119
function SBInitialize()
Initialize the main window.
Definition: mainWinInit.js:93
return null
Definition: FeedWriter.js:1143
function createDefaultSmartPlaylists()
Definition: mainWinInit.js:385
return!aWindow arguments!aWindow arguments[0]
var mainWinInit
Definition: mainWinInit.js:47
const Ci
function resetMinMaxCallback()
Definition: mainWinInit.js:193
Javascript wrappers for common library tasks.
var gServicePane
Definition: mainWinInit.js:39
function SBFirstRunSmartPlaylists()
Definition: mainWinInit.js:363
function setMinMaxCallback(evt)
Definition: mainWinInit.js:170