home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / firefox-3.0.14 / chrome / browser.jar / content / browser / migration / migration.js next >
Encoding:
Text File  |  2008-03-16  |  15.5 KB  |  468 lines

  1. //@line 36 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/migration/content/migration.js"
  2.  
  3. const kIMig = Components.interfaces.nsIBrowserProfileMigrator;
  4. const kIPStartup = Components.interfaces.nsIProfileStartup;
  5. const kProfileMigratorContractIDPrefix = "@mozilla.org/profile/migrator;1?app=browser&type=";
  6.  
  7. var MigrationWizard = {
  8.   _source: "",                  // Source Profile Migrator ContractID suffix
  9.   _itemsFlags: kIMig.ALL,       // Selected Import Data Sources (16-bit bitfield)
  10.   _selectedProfile: null,       // Selected Profile name to import from
  11.   _wiz: null,
  12.   _migrator: null,
  13.   _autoMigrate: null,
  14.   _bookmarks: false,
  15.  
  16.   init: function ()
  17.   {
  18.     var os = Components.classes["@mozilla.org/observer-service;1"]
  19.                        .getService(Components.interfaces.nsIObserverService);
  20.     os.addObserver(this, "Migration:Started", false);
  21.     os.addObserver(this, "Migration:ItemBeforeMigrate", false);
  22.     os.addObserver(this, "Migration:ItemAfterMigrate", false);
  23.     os.addObserver(this, "Migration:Ended", false);
  24.  
  25.     this._wiz = document.documentElement;
  26.  
  27.     if ("arguments" in window && window.arguments.length > 1) {
  28.       this._source = window.arguments[0];
  29.       this._migrator = window.arguments[1].QueryInterface(kIMig);
  30.       this._autoMigrate = window.arguments[2].QueryInterface(kIPStartup);
  31.  
  32.       if (this._autoMigrate) {
  33.         // Show the "nothing" option in the automigrate case to provide an
  34.         // easily identifiable way to avoid migration and create a new profile.
  35.         var nothing = document.getElementById("nothing");
  36.         nothing.hidden = false;
  37.       }
  38.     }
  39.  
  40.     this.onImportSourcePageShow();
  41.   },
  42.  
  43.   uninit: function ()
  44.   {
  45.     var os = Components.classes["@mozilla.org/observer-service;1"]
  46.                        .getService(Components.interfaces.nsIObserverService);
  47.     os.removeObserver(this, "Migration:Started");
  48.     os.removeObserver(this, "Migration:ItemBeforeMigrate");
  49.     os.removeObserver(this, "Migration:ItemAfterMigrate");
  50.     os.removeObserver(this, "Migration:Ended");
  51.   },
  52.  
  53.   // 1 - Import Source
  54.   onImportSourcePageShow: function ()
  55.   {
  56.     // Reference to the "From File" radio button 
  57.     var fromfile = null;
  58.  
  59.     //XXXquark This function is called before init, so check for bookmarks here
  60.     if ("arguments" in window && window.arguments[0] == "bookmarks") {
  61.       this._bookmarks = true;
  62.  
  63.       fromfile = document.getElementById("fromfile");
  64.       fromfile.hidden = false;
  65.  
  66.       var importBookmarks = document.getElementById("importBookmarks");
  67.       importBookmarks.hidden = false;
  68.  
  69.       var importAll = document.getElementById("importAll");
  70.       importAll.hidden = true;
  71.     }
  72.  
  73.     this._wiz.canRewind = false;
  74.  
  75.     // The migrator to select. If the "fromfile" migrator is available, use it
  76.     // as the default in case we have no other migrators.
  77.     var selectedMigrator = fromfile;
  78.  
  79.     // Figure out what source apps are are available to import from:
  80.     var group = document.getElementById("importSourceGroup");
  81.     for (var i = 0; i < group.childNodes.length; ++i) {
  82.       var suffix = group.childNodes[i].id;
  83.       if (suffix != "nothing" && suffix != "fromfile") {
  84.         var contractID = kProfileMigratorContractIDPrefix + suffix;
  85.         try {
  86.           var migrator = Components.classes[contractID].createInstance(kIMig);
  87.         }
  88.         catch (e) {
  89.           dump("*** invalid contractID =" + contractID + "\n");
  90.           return;
  91.         }
  92.  
  93.         if (migrator.sourceExists &&
  94.             !(suffix == "phoenix" && !this._autoMigrate)) {
  95.           // Save this as the first selectable item, if we don't already have
  96.           // one, or if it is the migrator that was passed to us.
  97.           if (!selectedMigrator || this._source == suffix)
  98.             selectedMigrator = group.childNodes[i];
  99.         } else {
  100.           // Hide this option
  101.           group.childNodes[i].hidden = true;
  102.         }
  103.       }
  104.     }
  105.  
  106.     if (selectedMigrator)
  107.       group.selectedItem = selectedMigrator;
  108.     else {
  109.       // We didn't find a migrator, notify the user
  110.       document.getElementById("noSources").hidden = false;
  111.  
  112.       this._wiz.canAdvance = false;
  113.  
  114.       document.getElementById("importBookmarks").hidden = true;
  115.       document.getElementById("importAll").hidden = true;
  116.     }
  117.   },
  118.   
  119.   onImportSourcePageAdvanced: function ()
  120.   {
  121.     var newSource = document.getElementById("importSourceGroup").selectedItem.id;
  122.     
  123.     if (newSource == "nothing" || newSource == "fromfile") {
  124.       if(newSource == "fromfile")
  125.         window.opener.fromFile = true;
  126.       document.documentElement.cancel();
  127.       return false;
  128.     }
  129.     
  130.     if (!this._migrator || (newSource != this._source)) {
  131.       // Create the migrator for the selected source.
  132.       var contractID = kProfileMigratorContractIDPrefix + newSource;
  133.       this._migrator = Components.classes[contractID].createInstance(kIMig);
  134.  
  135.       this._itemsFlags = kIMig.ALL;
  136.       this._selectedProfile = null;
  137.     }
  138.     this._source = newSource;
  139.       
  140.     // check for more than one source profile
  141.     if (this._migrator.sourceHasMultipleProfiles)
  142.       this._wiz.currentPage.next = "selectProfile";
  143.     else {
  144.       if (this._autoMigrate)
  145.         this._wiz.currentPage.next = "homePageImport";
  146.       else if (this._bookmarks)
  147.         this._wiz.currentPage.next = "migrating"
  148.       else
  149.         this._wiz.currentPage.next = "importItems";
  150.  
  151.       var sourceProfiles = this._migrator.sourceProfiles;
  152.       if (sourceProfiles && sourceProfiles.Count() == 1) {
  153.         var profileName = sourceProfiles.QueryElementAt(0, Components.interfaces.nsISupportsString);
  154.         this._selectedProfile = profileName.data;
  155.       }
  156.       else
  157.         this._selectedProfile = "";
  158.     }
  159.   },
  160.   
  161.   // 2 - [Profile Selection]
  162.   onSelectProfilePageShow: function ()
  163.   {
  164.     // Disabling this for now, since we ask about import sources in automigration
  165.     // too and don't want to disable the back button
  166.     // if (this._autoMigrate)
  167.     //   document.documentElement.getButton("back").disabled = true;
  168.       
  169.     var profiles = document.getElementById("profiles");
  170.     while (profiles.hasChildNodes()) 
  171.       profiles.removeChild(profiles.firstChild);
  172.     
  173.     // Note that this block is still reached even if the user chose 'From File'
  174.     // and we canceled the dialog.  When that happens, _migrator will be null.
  175.     if (this._migrator) {
  176.       var sourceProfiles = this._migrator.sourceProfiles;
  177.       var count = sourceProfiles.Count();
  178.       for (var i = 0; i < count; ++i) {
  179.         var item = document.createElement("radio");
  180.         var str = sourceProfiles.QueryElementAt(i, Components.interfaces.nsISupportsString);
  181.         item.id = str.data;
  182.         item.setAttribute("label", str.data);
  183.         profiles.appendChild(item);
  184.       }
  185.     }
  186.     
  187.     profiles.selectedItem = this._selectedProfile ? document.getElementById(this._selectedProfile) : profiles.firstChild;
  188.   },
  189.   
  190.   onSelectProfilePageRewound: function ()
  191.   {
  192.     var profiles = document.getElementById("profiles");
  193.     this._selectedProfile = profiles.selectedItem.id;
  194.   },
  195.   
  196.   onSelectProfilePageAdvanced: function ()
  197.   {
  198.     var profiles = document.getElementById("profiles");
  199.     this._selectedProfile = profiles.selectedItem.id;
  200.     
  201.     // If we're automigrating or just doing bookmarks don't show the item selection page
  202.     if (this._autoMigrate)
  203.       this._wiz.currentPage.next = "homePageImport";
  204.     else if (this._bookmarks)
  205.       this._wiz.currentPage.next = "migrating"
  206.   },
  207.   
  208.   // 3 - ImportItems
  209.   onImportItemsPageShow: function ()
  210.   {
  211.     var dataSources = document.getElementById("dataSources");
  212.     while (dataSources.hasChildNodes())
  213.       dataSources.removeChild(dataSources.firstChild);
  214.     
  215.     var bundle = document.getElementById("bundle");
  216.     
  217.     var items = this._migrator.getMigrateData(this._selectedProfile, this._autoMigrate);
  218.     for (var i = 0; i < 16; ++i) {
  219.       var itemID = (items >> i) & 0x1 ? Math.pow(2, i) : 0;
  220.       if (itemID > 0) {
  221.         var checkbox = document.createElement("checkbox");
  222.         checkbox.id = itemID;
  223.         checkbox.setAttribute("label", bundle.getString(itemID + "_" + this._source));
  224.         dataSources.appendChild(checkbox);
  225.         if (!this._itemsFlags || this._itemsFlags & itemID)
  226.           checkbox.checked = true;
  227.       }
  228.     }
  229.   },
  230.  
  231.   onImportItemsPageRewound: function ()
  232.   {
  233.     this._wiz.canAdvance = true;
  234.     this.onImportItemsPageAdvanced();
  235.   },
  236.  
  237.   onImportItemsPageAdvanced: function ()
  238.   {
  239.     var dataSources = document.getElementById("dataSources");
  240.     this._itemsFlags = 0;
  241.     for (var i = 0; i < dataSources.childNodes.length; ++i) {
  242.       var checkbox = dataSources.childNodes[i];
  243.       if (checkbox.localName == "checkbox" && checkbox.checked)
  244.         this._itemsFlags |= parseInt(checkbox.id);
  245.     }
  246.   },
  247.   
  248.   onImportItemCommand: function (aEvent)
  249.   {
  250.     var items = document.getElementById("dataSources");
  251.     var checkboxes = items.getElementsByTagName("checkbox");
  252.  
  253.     var oneChecked = false;
  254.     for (var i = 0; i < checkboxes.length; ++i) {
  255.       if (checkboxes[i].checked) {
  256.         oneChecked = true;
  257.         break;
  258.       }
  259.     }
  260.  
  261.     this._wiz.canAdvance = oneChecked;
  262.   },
  263.  
  264.   // 4 - Home Page Selection
  265.   onHomePageMigrationPageShow: function ()
  266.   {
  267.     // only want this on the first run
  268.     if (!this._autoMigrate) {
  269.       this._wiz.advance();
  270.       return;
  271.     }
  272.  
  273.     var bundle = document.getElementById("brandBundle");
  274.     // These strings don't exist when not using official branding. If that's
  275.     // the case, just skip this page.
  276.     try {
  277.       var pageTitle = bundle.getString("homePageMigrationPageTitle");
  278.       var pageDesc = bundle.getString("homePageMigrationDescription");
  279.       var mainStr = bundle.getString("homePageSingleStartMain");
  280.     }
  281.     catch (e) {
  282.       this._wiz.advance();
  283.       return;
  284.     }
  285.  
  286.     document.getElementById("homePageImport").setAttribute("label", pageTitle);
  287.     document.getElementById("homePageImportDesc").setAttribute("value", pageDesc);
  288.  
  289.     this._wiz._adjustWizardHeader();
  290.  
  291.     var singleStart = document.getElementById("homePageSingleStart");
  292.     singleStart.setAttribute("label", mainStr);
  293.     singleStart.setAttribute("value", "DEFAULT");
  294.  
  295.     var source = null;
  296.     switch (this._source) {
  297.       case "ie":
  298.       case "macie":
  299.         source = "sourceNameIE";
  300.         break;
  301.       case "opera":
  302.         source = "sourceNameOpera";
  303.         break;
  304.       case "dogbert":
  305.         source = "sourceNameDogbert";
  306.         break;
  307.       case "safari":
  308.         source = "sourceNameSafari";
  309.         break;
  310.       case "seamonkey":
  311.         source = "sourceNameSeamonkey";
  312.         break;
  313.     }
  314.  
  315.     // semi-wallpaper for crash when multiple profiles exist, since we haven't initialized mSourceProfile in places
  316.     this._migrator.getMigrateData(this._selectedProfile, this._autoMigrate);
  317.  
  318.     var oldHomePageURL = this._migrator.sourceHomePageURL;
  319.  
  320.     if (oldHomePageURL && source) {
  321.       var bundle2 = document.getElementById("bundle");
  322.       var appName = bundle2.getString(source);
  323.       var oldHomePageLabel = bundle.getFormattedString("homePageImport",
  324.                                                        [appName]);
  325.       var oldHomePage = document.getElementById("oldHomePage");
  326.       oldHomePage.setAttribute("label", oldHomePageLabel);
  327.       oldHomePage.setAttribute("value", oldHomePageURL);
  328.       oldHomePage.removeAttribute("hidden");
  329.     }
  330.     else {
  331.       // if we don't have at least two options, just advance
  332.       this._wiz.advance();
  333.     }
  334.   },
  335.  
  336.   onHomePageMigrationPageAdvanced: function ()
  337.   {
  338.     // we might not have a selectedItem if we're in fallback mode
  339.     try {
  340.       var radioGroup = document.getElementById("homePageRadiogroup");
  341.  
  342.       this._newHomePage = radioGroup.selectedItem.value;
  343.     } catch(ex) {}
  344.   },
  345.  
  346.   // 5 - Migrating
  347.   onMigratingPageShow: function ()
  348.   {
  349.     this._wiz.getButton("cancel").disabled = true;
  350.     this._wiz.canRewind = false;
  351.     this._wiz.canAdvance = false;
  352.     
  353.     // When automigrating, show all of the data that can be received from this source.
  354.     if (this._autoMigrate)
  355.       this._itemsFlags = this._migrator.getMigrateData(this._selectedProfile, this._autoMigrate);
  356.  
  357.     // When importing bookmarks, show only bookmarks
  358.     if (this._bookmarks)
  359.       this._itemsFlags = 32;
  360.  
  361.     this._listItems("migratingItems");
  362.     setTimeout(this.onMigratingMigrate, 0, this);
  363.   },
  364.  
  365.   onMigratingMigrate: function (aOuter)
  366.   {
  367.     aOuter._migrator.migrate(aOuter._itemsFlags, aOuter._autoMigrate, aOuter._selectedProfile);
  368.   },
  369.   
  370.   _listItems: function (aID)
  371.   {
  372.     var items = document.getElementById(aID);
  373.     while (items.hasChildNodes())
  374.       items.removeChild(items.firstChild);
  375.     
  376.     var bundle = document.getElementById("bundle");
  377.     var brandBundle = document.getElementById("brandBundle");
  378.     var itemID;
  379.     for (var i = 0; i < 16; ++i) {
  380.       var itemID = (this._itemsFlags >> i) & 0x1 ? Math.pow(2, i) : 0;
  381.       if (itemID > 0) {
  382.         var label = document.createElement("label");
  383.         label.id = itemID + "_migrated";
  384.         try {
  385.           label.setAttribute("value", bundle.getString(itemID + "_" + this._source));
  386.           items.appendChild(label);
  387.         }
  388.         catch (e) {
  389.           // if the block above throws, we've enumerated all the import data types we
  390.           // currently support and are now just wasting time, break. 
  391.           break;
  392.         }
  393.       }
  394.     }
  395.   },
  396.   
  397.   observe: function (aSubject, aTopic, aData)
  398.   {
  399.     switch (aTopic) {
  400.     case "Migration:Started":
  401.       break;
  402.     case "Migration:ItemBeforeMigrate":
  403.       var label = document.getElementById(aData + "_migrated");
  404.       if (label)
  405.         label.setAttribute("style", "font-weight: bold");
  406.       break;
  407.     case "Migration:ItemAfterMigrate":
  408.       var label = document.getElementById(aData + "_migrated");
  409.       if (label)
  410.         label.removeAttribute("style");
  411.       break;
  412.     case "Migration:Ended":
  413.       if (this._autoMigrate) {
  414.         if (this._newHomePage) {
  415.           try {
  416.             // set homepage properly
  417.             var prefSvc = Components.classes["@mozilla.org/preferences-service;1"]
  418.                                     .getService(Components.interfaces.nsIPrefService);
  419.             var prefBranch = prefSvc.getBranch(null);
  420.  
  421.             if (this._newHomePage == "DEFAULT") {
  422.               try {
  423.                 prefBranch.clearUserPref("browser.startup.homepage");
  424.               }
  425.               catch (e) { }
  426.             }
  427.             else {
  428.               var str = Components.classes["@mozilla.org/supports-string;1"]
  429.                                 .createInstance(Components.interfaces.nsISupportsString);
  430.               str.data = this._newHomePage;
  431.               prefBranch.setComplexValue("browser.startup.homepage",
  432.                                          Components.interfaces.nsISupportsString,
  433.                                          str);
  434.             }
  435.  
  436.             var dirSvc = Components.classes["@mozilla.org/file/directory_service;1"]
  437.                                    .getService(Components.interfaces.nsIProperties);
  438.             var prefFile = dirSvc.get("ProfDS", Components.interfaces.nsIFile);
  439.             prefFile.append("prefs.js");
  440.             prefSvc.savePrefFile(prefFile);
  441.           } catch(ex) { 
  442.             dump(ex); 
  443.           }
  444.         }
  445.  
  446.         // We're done now.
  447.         this._wiz.canAdvance = true;
  448.         this._wiz.advance();
  449.  
  450.         setTimeout(close, 5000);
  451.       }
  452.       else {
  453.         this._wiz.canAdvance = true;
  454.         var nextButton = this._wiz.getButton("next");
  455.         nextButton.click();
  456.       }
  457.       break;
  458.     }
  459.   },
  460.  
  461.   onDonePageShow: function ()
  462.   {
  463.     this._wiz.getButton("cancel").disabled = true;
  464.     this._wiz.canRewind = false;
  465.     this._listItems("doneItems");
  466.   }
  467. };
  468.