firstRunWizard.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  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2010 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
32 //------------------------------------------------------------------------------
33 //------------------------------------------------------------------------------
34 //
35 // First-run wizard dialog.
36 //
37 //------------------------------------------------------------------------------
38 //------------------------------------------------------------------------------
39 
40 //------------------------------------------------------------------------------
41 //
42 // First-run wizard dialog imported services.
43 //
44 //------------------------------------------------------------------------------
45 
46 // Songbird imports.
47 Components.utils.import("resource://app/jsmodules/DOMUtils.jsm");
48 Components.utils.import("resource://app/jsmodules/SBUtils.jsm");
49 
50 //------------------------------------------------------------------------------
51 //
52 // First-run wizard dialog defs.
53 //
54 //------------------------------------------------------------------------------
55 
56 // Component manager defs.
57 if (typeof(Cc) == "undefined")
58  var Cc = Components.classes;
59 if (typeof(Ci) == "undefined")
60  var Ci = Components.interfaces;
61 if (typeof(Cr) == "undefined")
62  var Cr = Components.results;
63 if (typeof(Cu) == "undefined")
64  var Cu = Components.utils;
65 
66 var SBLanguage = {
67  DEFAULT_LOCALE: "en-US",
68  locale: "",
69  firstRunWizard: null,
70  getCountry: function() {
72  /* try to get the country from the preferences */
74  // Default country is United States
75  var localeVal = SBLanguage.DEFAULT_LOCALE; // Default to english
76  var regCountryVal = "1033";
77 
78  var langLocaleMap = {
79  "1046": "pt-BR",
80  "2052": "zh-CN",
81  "1029":"cs",
82  "1030":"da",
83  "1035":"fi",
84  "1036":"fr",
85  "1031":"de",
86  "1032":"el",
87  "1038":"hu",
88  "1040":"it",
89  "1041":"ja",
90  "1042":"ko",
91  "1025":"ar",
92  "1043":"nl",
93  "1044":"nb-NO",
94  "1045":"pl",
95  "2070":"pt",
96  "1049":"ru",
97  "1051":"sk",
98  "1034":"es-ES",
99  "1053":"sv-SE" ,
100  "1028":"zh-TW",
101  "1054":"th",
102  "1055":"tr",
103  "1033":"en-US"
104  };
105  try {
106  var wrk = Cc["@mozilla.org/windows-registry-key;1"]
107  .createInstance(Ci.nsIWindowsRegKey);
108  if (wrk) {
109  wrk.open(wrk.ROOT_KEY_LOCAL_MACHINE,
110  "SOFTWARE\\Songbird",
111  wrk.ACCESS_READ);
112  if (wrk.hasValue("Installer Country")) {
113  regCountryVal = wrk.readStringValue("Installer Country");
114  }
115  wrk.close();
116  }
117  }
118  catch (e) {
119  // Just continue if there is an error
120  }
121 
122  // Convert the country code in country name
123  if ( !isNaN(regCountryVal)) {
124  localeVal = langLocaleMap["" + regCountryVal];
125  }
126  return localeVal;
127  },
128 
129  exists: function (array, value) {
130  for (i in array) {
131  if (array[i] == value)
132  return true;
133  }
134  return false;
135  },
138  onDownloadComplete: function(bundle) {
139  try {
140  for (var i=0; i < bundle.bundleExtensionCount; i++) {
141  var thisLocale = bundle.getExtensionAttribute(i, "languageTag");
142  if (thisLocale == SBLanguage.locale) {
143  for (var x = 0; x < bundle.bundleExtensionCount; x++) {
144  bundle.setExtensionInstallFlag(x, x == i);
145  }
146  var res = bundle.installFlaggedExtensions(window);
147 
148  if (res == bundle.BUNDLE_INSTALL_SUCCESS) {
149  SBLanguage.firstRunWizard._markFirstRunComplete = false;
150  switchLocale(SBLanguage.locale, false);
151  restartApp();
152  }
153  else {
154  gPrompt.alert( window,
155  SBString( "locales.installfailed.title", "Language Download" ),
156  SBString( "locales.installfailed.msg", "Language installation failed, check your network connectivity!" ) );
157  }
158  break;
159  }
160  }
161  }
162  catch (e)
163  {
164  dump("Exception: " + e.toString() + "\n");
165  }
166  },
167  onError: function(bundle) { dump("DWB: Error downloading bundles\n"); },
168  QueryInterface : function(aIID) {
169  if (!aIID.equals(Components.interfaces.sbIBundleDataListener) &&
170  !aIID.equals(Components.interfaces.nsISupports))
171  {
172  throw Components.results.NS_ERROR_NO_INTERFACE;
173  }
174  return this;
175  }
176  },
177  loadLocaleFromRegistry : function(firstRunWizard) {
178  SBLanguage.firstRunWizard = firstRunWizard;
179  try {
180  SBLanguage.locale = SBLanguage.getCountry();
181  var prefLocaleVal = Application.prefs.getValue("general.useragent.locale" , "");
182  var prefLocaleFirstRun = Application.prefs.getValue("general.useragent.locale.firstrun", true);
183  Application.prefs.setValue("general.useragent.locale.firstrun", false);
184  // We only want to do this on first run, else if the users switches the
185  // language we'll
186  // end up switching it back here
187  if (prefLocaleFirstRun && SBLanguage.locale != prefLocaleVal) {
188  switchLocale(SBLanguage.locale);
189  try {
190  var sbIBundle = new Components.Constructor("@songbirdnest.com/Songbird/Bundle;1", "sbIBundle");
191  menubar_locales_bundle = new sbIBundle();
192  menubar_locales_bundle.bundleId = "locales";
193  menubar_locales_bundle.bundleURL = Application.prefs.getValue("songbird.url.locales", "default");
194  menubar_locales_bundle.addBundleDataListener(this.menubarLocalesBundleCB);
196  } catch ( err ) {
197  SB_LOG("initLocalesBundle", "" + err );
198  }
199  }
200  }
201  catch (e)
202  {
203  dump("Exception:" + e.toString() + "\n");
204  }
205  }
206 }
207 
208 //------------------------------------------------------------------------------
209 //
210 // First-run wizard dialog services.
211 //
212 //------------------------------------------------------------------------------
213 
214 var firstRunWizard = {
215  //
216  // Public first-run wizard fields.
217  //
218  // wizardElem Wizard element.
219  // restartApp Restart application on wizard close.
220  // restartWizard Restart wizard on wizard close.
221  //
222 
223  restartApp: false,
224  restartWizard: false,
225 
226 
227  //
228  // Internal first-run wizard fields.
229  //
230  // _initialized True if these services have been initialized.
231  // _domEventListenerSet Set of DOM event listeners.
232  // _savedSettings True if settings have been saved.
233  // _markFirstRunComplete True if first-run should be marked as complete
234  // when wizard exits.
235  // _connectionErrorHandled True if a connection error has been handled.
236  //
237 
238  _initialized: false,
239  _domEventListenerSet: null,
240  _savedSettings: false,
241  _markFirstRunComplete: false,
242  _connectionErrorHandled: false,
243 
244 
245  //----------------------------------------------------------------------------
246  //
247  // Public first-run wizard field getters/setters.
248  //
249  //----------------------------------------------------------------------------
250 
251  //
252  // wizardElem
253  //
254 
255  _wizardElem: null,
256 
257  get wizardElem() {
258  if (!this._wizardElem)
259  this._wizardElem = document.getElementById("first_run_wizard");
260  return this._wizardElem;
261  },
262 
263 
264  //----------------------------------------------------------------------------
265  //
266  // Event handling services.
267  //
268  //----------------------------------------------------------------------------
269 
274  doLoad: function firstRunWizard_doLoad() {
275  // Initialize the services.
276  this._initialize();
277  },
278 
279 
284  doUnload: function firstRunWizard_doUnload() {
285  // Indicate that the first-run checks have been made.
286  if (this._markFirstRunComplete) {
287  // Set the first-run check preference and flush to disk.
288  Application.prefs.setValue("songbird.firstrun.check.0.3", true);
289  var prefService = Cc["@mozilla.org/preferences-service;1"]
290  .getService(Ci.nsIPrefService);
291  prefService.savePrefFile(null);
292  }
293 
294  // Indicate that the wizard is complete and whether it should be restarted.
295  this._firstRunData.onComplete(this.restartWizard);
296 
297  // Finalize the services.
298  this._finalize();
299 
300  // Restart application as specified. (AFTER we're done finalizing)
301  if (this.restartApp)
302  restartApp();
303  },
304 
305 
310  doFinish: function firstRunWizard_doFinish() {
311  // Record our time startup pref
312  try {
313  var timingService = Cc["@songbirdnest.com/Songbird/TimingService;1"]
314  .getService(Ci.sbITimingService);
315  timingService.startPerfTimer("CSPerfEndEULA");
316  timingService.stopPerfTimer("CSPerfEndEULA");
317  } catch (e) {
318  dump("Timing service exception: " + e);
319  // Ignore errors
320  }
321 
322  // Save wizard settings.
323  this._saveSettings();
324  },
325 
326 
331  doCancel: function firstRunWizard_doCancel() {
332  },
333 
334 
339  doQuit: function firstRunWizard_doQuit() {
340  // Defer invocation of quitApp to outside this even handler so that this
341  // window may be immediately closed by quitApp. If this window doesn't
342  // close, it will prevent the application from quitting.
343  // See "http://bugzilla.songbirdnest.com/show_bug.cgi?id=21890".
344  SBUtils.deferFunction(quitApp);
345  },
346 
347 
352  doPageShow: function firstRunWizard_doPageShow() {
353  // Initialize the services. Page show can occur before load.
354  this._initialize();
355 
356  // Update the UI.
357  this.update();
358  },
359 
365  _getNextProxyImport: function
366  firstRunWizard_getNextProxyImport(aProxyImport, aLastImportId) {
367  var importSources = aProxyImport.importSources.enumerate();
368  while (importSources.hasMoreElements()) {
369  var id = importSources.getNext()
370  .QueryInterface(Ci.nsISupportsString) + '';
371  if (!aLastImportId)
372  return id;
373  if (id == aLastImportId) {
374  if (importSources.hasMoreElements())
375  return importSources.getNext()
376  .QueryInterface(Ci.nsISupportsString) + '';
377  return null;
378  }
379  }
380  return null;
381  },
382 
388  _tryNextProxyImport: function firstRunWizard_tryNextProxyImport() {
389  var proxyImport = Cc["@songbirdnest.com/Songbird/NetworkProxyImport;1"]
390  .getService(Ci.sbINetworkProxyImport);
391  var lastImportId = this._lastProxyImport;
392  var importId;
393  var success = false;
394  while (!success) {
395  importId = this._getNextProxyImport(proxyImport, lastImportId);
396  if (!importId)
397  return false;
398  success = proxyImport.importProxySettings(importId);
399  }
400  this._lastProxyImport = importId;
401  return true;
402  },
403 
408  _resetProxySettings: function firstRunWizard_resetProxySettings() {
409  var proxyData = [
410  ["network.proxy.autoconfig_url", ""],
411  ["network.proxy.type", 0],
412  ["network.proxy.ftp", ""],
413  ["network.proxy.ftp_port", 0],
414  ["network.proxy.gopher", ""],
415  ["network.proxy.gopher_port", 0],
416  ["network.proxy.http", ""],
417  ["network.proxy.http_port", 0],
418  ["network.proxy.ssl", ""],
419  ["network.proxy.ssl_port", 0],
420  ["network.proxy.socks", ""],
421  ["network.proxy.socks_port", 0],
422  ["network.proxy.share_proxy_settings", false],
423  ["network.proxy.no_proxies_on", "127.0.0.1;localhost"]
424  ];
425  for (var i in proxyData) {
426  Application.prefs.setValue(proxyData[i][0], proxyData[i][1]);
427  }
428  },
429 
436  handleConnectionError: function firstRunWizard_handleConnectionError() {
437  // Only handle connection errors once.
438  if (this._connectionErrorHandled)
439  return false;
440 
441  // Defer handling of the connection error until after the current event
442  // completes. This allows the calling wizard page to complete any updates
443  // before the first-run wizard connection page is presented.
444  var _this = this;
445  var func = function() { _this._handleConnectionError(); };
446  SBUtils.deferFunction(func);
447 
448  return true;
449  },
450 
451  _handleConnectionError: function firstRunWizard__handleConnectionError() {
452  // Only handle connection errors once.
453  if (this._connectionErrorHandled)
454  return;
455 
456  if (this._tryNextProxyImport()) {
457  // retry whatever caused the connection failure
458  var event = document.createEvent("Events");
459  event.initEvent("firstRunConnectionReset", true, true);
460  this.wizardElem.dispatchEvent(event);
461  // reload the same page
462  this.wizardElem.goTo(this.wizardElem.currentPage.pageid);
463  return;
464  } else {
465  this._resetProxySettings();
466  }
467 
468  this._connectionErrorHandled = true;
469 
470  // Suppress the wizard page advanced event sent when advancing to the
471  // first-run wizard connection page. The wizard is not really advancing.
472  var func = function(aEvent) { aEvent.stopPropagation(); };
473  this._domEventListenerSet.add(this.wizardElem,
474  "pageadvanced",
475  func,
476  true,
477  true);
478 
479  // Go to the first-run wizard connection page.
480  this.wizardElem.canAdvance = true;
481  this.wizardElem.advance("first_run_connection_page");
482 
483  // A connection error has been handled.
484  this._connectionErrorHandled = true;
485  },
486 
487 
488  //----------------------------------------------------------------------------
489  //
490  // UI services.
491  //
492  //----------------------------------------------------------------------------
493 
498  update: function firstRunWizard_update() {
499  // Get the current wizard page.
500  var currentPage = this.wizardElem.currentPage;
501 
502  // Set to mark first-run complete if showing welcome page.
503  if (currentPage.id == "first_run_welcome_page")
504  this._markFirstRunComplete = true;
505 
506  // If we're perf testing then we want to just after the EULA is done
507  if (window.arguments[0].perfTest && this._markFirstRunComplete) {
508  finishButton.click();
509  }
510  },
511 
517  openURL: function firstRunWizard_openURL(aURL) {
518  var uri = null;
519  if (aURL instanceof Ci.nsIURI) {
520  uri = aURL;
521  }
522  else {
523  uri = Cc["@mozilla.org/network/io-service;1"]
524  .getService(Ci.nsIIOService)
525  .newURI(aURL, null, null);
526  }
527  Cc["@mozilla.org/uriloader/external-protocol-service;1"]
528  .getService(Ci.nsIExternalProtocolService)
529  .loadURI(uri);
530  },
531 
532  //----------------------------------------------------------------------------
533  //
534  // Internal services.
535  //
536  //----------------------------------------------------------------------------
537 
542  _initialize: function firstRunWizard__initialize() {
543  // Do nothing if already initialized.
544  if (this._initialized)
545  return;
546 
547  SBLanguage.loadLocaleFromRegistry(this);
548 
549  // Get the wizard element.
550  this.wizardElem = document.getElementById("first_run_wizard");
551 
552  // Create a DOM event listener set.
553  this._domEventListenerSet = new DOMEventListenerSet();
554 
555  // Grap the param block
556  var dialogPB =
557  window.arguments[0].QueryInterface(Ci.nsIDialogParamBlock);
558 
559  // Unwrap the object coming from the application startup service
560  this._firstRunData =
561  dialogPB.objects
562  .queryElementAt(0, Ci.nsISupportsInterfacePointer)
563  .data
564  .wrappedJSObject;
565 
566  // Services are now initialized.
567  this._initialized = true;
568  var skipToPage = Application.prefs.getValue("songbird.firstrun.goto", null);
569  if (skipToPage) {
570  Application.prefs.get("songbird.firstrun.goto").reset();
571  this.wizardElem.advance(skipToPage);
572  return;
573  }
574  // we want to skip the EULA if we're on Windows, as the Windows' installer
575  // has already presented it
576  if (getPlatformString() == "Windows_NT") {
577  Application.prefs.setValue("songbird.eulacheck", true);
578  }
579  },
580 
581 
586  _finalize: function firstRunWizard__finalize() {
587  // Remove DOM event listeners.
588  if (this._domEventListenerSet)
589  this._domEventListenerSet.removeAll();
590  this._domEventListenerSet = null;
591  },
592 
593 
598  _saveSettings: function firstRunWizard__saveSettings() {
599  // Do nothing if settings already saved.
600  if (this._savedSettings)
601  return;
602 
603  // Get all first-run wizard page elements.
604  var firstRunWizardPageElemList =
605  DOMUtils.getElementsByAttribute(this.wizardElem,
606  "firstrunwizardpage",
607  "true");
608 
609  // Save settings for each first-run wizard page.
610  for (var i = 0; i < firstRunWizardPageElemList.length; i++) {
611  // Invoke the page saveSettings method.
612  var firstRunWizardPageElem = firstRunWizardPageElemList[i];
613  if (typeof(firstRunWizardPageElem.saveSettings) == "function")
614  firstRunWizardPageElem.saveSettings();
615  }
616  // Settings have now been saved.
617  this._savedSettings = true;
618  }
619 };
620 
const Cu
const Cc
function restartApp()
Definition: safeMode.js:40
var Application
Definition: sbAboutDRM.js:37
menuItem id
Definition: FeedWriter.js:971
function getPlatformString()
Get the name of the platform we are running on.
inArray array
var event
function doLoad()
sbOSDControlService prototype QueryInterface
function DOMEventListenerSet()
Definition: DOMUtils.jsm:766
function SB_LOG(scopeStr, msg)
Definition: windowUtils.js:244
function SBString(aKey, aDefault, aStringBundle)
Definition: StringUtils.jsm:93
let window
function doCancel()
var menubar_locales_bundle
Definition: switchLocale.js:41
var menubarLocalesBundleCB
Locales Bundle listener. Handles download completion and errors.
Definition: switchLocale.js:96
var bundle
var SBUtils
Definition: SBUtils.jsm:71
var _this
const MENUBAR_LOCALESBUNDLE_TIMEOUT
Definition: switchLocale.js:36
return null
Definition: FeedWriter.js:1143
var SBLanguage
var uri
Definition: FeedWriter.js:1135
countRef value
Definition: FeedWriter.js:1423
var gPrompt
Definition: windowUtils.js:64
const Cr
const Ci
Songbird Bundle Interface This is the main bundle management interface, used to get the bundle data...
Definition: sbIBundle.idl:50
_getSelectedPageStyle s i
function doQuit()
function openURL(target)
Definition: pageInfo.js:645