home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2005 October / maximum-cd-2005-10.iso / Software / Apps / FirefoxSetup1.0.6.exe / browser.xpi / bin / chrome / browser.jar / content / browser / migration / migration.js next >
Encoding:
Text File  |  2004-11-05  |  11.3 KB  |  330 lines

  1. const kIMig = Components.interfaces.nsIBrowserProfileMigrator;
  2. const kIPStartup = Components.interfaces.nsIProfileStartup;
  3. const kProfileMigratorContractIDPrefix = "@mozilla.org/profile/migrator;1?app=browser&type=";
  4.  
  5. var MigrationWizard = {
  6.   _source: "",                  // Source Profile Migrator ContractID suffix
  7.   _itemsFlags: kIMig.ALL,       // Selected Import Data Sources (16-bit bitfield)
  8.   _selectedProfile: null,       // Selected Profile name to import from
  9.   _wiz: null,
  10.   _migrator: null,
  11.   _autoMigrate: null,
  12.   _bookmarks: false,
  13.  
  14.   init: function ()
  15.   {
  16.     var os = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  17.     os.addObserver(this, "Migration:Started", false);
  18.     os.addObserver(this, "Migration:ItemBeforeMigrate", false);
  19.     os.addObserver(this, "Migration:ItemAfterMigrate", false);
  20.     os.addObserver(this, "Migration:Ended", false);
  21.     
  22.     this._wiz = document.documentElement;
  23.  
  24.     if ("arguments" in window && window.arguments.length > 1) {
  25.       this._source = window.arguments[0];
  26.       this._migrator = window.arguments[1].QueryInterface(kIMig);
  27.       this._autoMigrate = window.arguments[2].QueryInterface(kIPStartup);
  28.       
  29.       if (this._autoMigrate) {
  30.         // Show the "nothing" option in the automigrate case to provide an
  31.         // easily identifiable way to avoid migration and create a new profile.
  32.         var nothing = document.getElementById("nothing");
  33.         nothing.hidden = false;
  34.       }
  35.     }
  36.     
  37.     this.onImportSourcePageShow();
  38.   },
  39.   
  40.   uninit: function ()
  41.   {
  42.     var os = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  43.     os.removeObserver(this, "Migration:Started");
  44.     os.removeObserver(this, "Migration:ItemBeforeMigrate");
  45.     os.removeObserver(this, "Migration:ItemAfterMigrate");
  46.     os.removeObserver(this, "Migration:Ended");
  47.   },
  48.   
  49.   // 1 - Import Source
  50.   onImportSourcePageShow: function ()
  51.   {
  52.     //XXXquark This function is called before init, so check for bookmarks here
  53.     if("arguments" in window && window.arguments[0] == "bookmarks")
  54.     {
  55.       this._bookmarks = true;
  56.     }
  57.  
  58.     if(this._bookmarks) {
  59.       var fromfile = document.getElementById("fromfile");
  60.       fromfile.hidden = false;
  61.  
  62.       var importBookmarks = document.getElementById("importBookmarks");
  63.       importBookmarks.hidden = false;
  64.  
  65.       var importAll = document.getElementById("importAll");
  66.       importAll.hidden = true;
  67.     }
  68.  
  69.     document.documentElement.getButton("back").disabled = true;
  70.     
  71.     // Figure out what source apps are are available to import from:
  72.     var group = document.getElementById("importSourceGroup");
  73.     for (var i = 0; i < group.childNodes.length; ++i) {
  74.       var suffix = group.childNodes[i].id;
  75.       if (suffix != "nothing" && suffix != "fromfile") {
  76.         var contractID = kProfileMigratorContractIDPrefix + suffix;
  77.         try {
  78.           var migrator = Components.classes[contractID].createInstance(kIMig);
  79.         }
  80.         catch (e) {
  81.           dump("*** invalid contractID =" + contractID + "\n");
  82.           return;
  83.         }
  84.         if (!migrator.sourceExists || (suffix == "phoenix" && !this._autoMigrate))
  85.           group.childNodes[i].hidden = true;
  86.       }
  87.     }
  88.     
  89.     var firstSelectable = null;
  90.     for (var i = 0; i < group.childNodes.length; ++i) {
  91.       if (!group.childNodes[i].disabled && !group.childNodes[i].hidden) {
  92.         firstSelectable = group.childNodes[i];
  93.         break;
  94.       }
  95.     }
  96.  
  97.     if (this._source) {
  98.       // Somehow the Profile Migrator got confused, and gave us a migrate source
  99.       // that doesn't actually exist. This could be because of a bogus registry
  100.       // state. Set the _source property to null so the first visible item in
  101.       // the list is selected instead. 
  102.       var source = document.getElementById(this._source);
  103.       if (source.hidden)
  104.         this._source = null;
  105.     }
  106.     group.selectedItem = !this._source ? firstSelectable : document.getElementById(this._source);
  107.   },
  108.   
  109.   onImportSourcePageAdvanced: function ()
  110.   {
  111.     var newSource = document.getElementById("importSourceGroup").selectedItem.id;
  112.     
  113.     if (newSource == "nothing" || newSource == "fromfile") {
  114.       if(newSource == "fromfile")
  115.         window.opener.fromFile = true;
  116.       document.documentElement.cancel();
  117.       return;
  118.     }
  119.     
  120.     if (!this._migrator || (newSource != this._source)) {
  121.       // Create the migrator for the selected source.
  122.       var contractID = kProfileMigratorContractIDPrefix + newSource;
  123.       this._migrator = Components.classes[contractID].createInstance(kIMig);
  124.  
  125.       this._itemsFlags = kIMig.ALL;
  126.       this._selectedProfile = null;
  127.     }
  128.     this._source = newSource;
  129.       
  130.     // check for more than one source profile
  131.     if (this._migrator.sourceHasMultipleProfiles)
  132.       this._wiz.currentPage.next = "selectProfile";
  133.     else {
  134.       this._wiz.currentPage.next = (this._autoMigrate || this._bookmarks) ? "migrating" : "importItems";
  135.       var sourceProfiles = this._migrator.sourceProfiles;
  136.       if (sourceProfiles && sourceProfiles.Count() == 1) {
  137.         var profileName = sourceProfiles.QueryElementAt(0, Components.interfaces.nsISupportsString);
  138.         this._selectedProfile = profileName.data;
  139.       }
  140.       else
  141.         this._selectedProfile = "";
  142.     }
  143.   },
  144.   
  145.   // 2 - [Profile Selection]
  146.   onSelectProfilePageShow: function ()
  147.   {
  148.     // Disabling this for now, since we ask about import sources in automigration
  149.     // too and don't want to disable the back button
  150.     // if (this._autoMigrate)
  151.     //   document.documentElement.getButton("back").disabled = true;
  152.       
  153.     var profiles = document.getElementById("profiles");
  154.     while (profiles.hasChildNodes()) 
  155.       profiles.removeChild(profiles.firstChild);
  156.     
  157.     var sourceProfiles = this._migrator.sourceProfiles;
  158.     var count = sourceProfiles.Count();
  159.     for (var i = 0; i < count; ++i) {
  160.       var item = document.createElement("radio");
  161.       var str = sourceProfiles.QueryElementAt(i, Components.interfaces.nsISupportsString);
  162.       item.id = str.data;
  163.       item.setAttribute("label", str.data);
  164.       profiles.appendChild(item);
  165.     }
  166.     
  167.     profiles.selectedItem = this._selectedProfile ? document.getElementById(this._selectedProfile) : profiles.firstChild;
  168.   },
  169.   
  170.   onSelectProfilePageRewound: function ()
  171.   {
  172.     var profiles = document.getElementById("profiles");
  173.     this._selectedProfile = profiles.selectedItem.id;
  174.   },
  175.   
  176.   onSelectProfilePageAdvanced: function ()
  177.   {
  178.     var profiles = document.getElementById("profiles");
  179.     this._selectedProfile = profiles.selectedItem.id;
  180.     
  181.     // If we're automigrating or just doing bookmarks don't show the item selection page
  182.     if (this._autoMigrate || this._bookmarks)
  183.       this._wiz.currentPage.next = "migrating";
  184.   },
  185.   
  186.   // 3 - ImportItems
  187.   onImportItemsPageShow: function ()
  188.   {
  189.     var dataSources = document.getElementById("dataSources");
  190.     while (dataSources.hasChildNodes())
  191.       dataSources.removeChild(dataSources.firstChild);
  192.     
  193.     var bundle = document.getElementById("bundle");
  194.     
  195.     var items = this._migrator.getMigrateData(this._selectedProfile, this._autoMigrate);
  196.     for (var i = 0; i < 16; ++i) {
  197.       var itemID = (items >> i) & 0x1 ? Math.pow(2, i) : 0;
  198.       if (itemID > 0) {
  199.         var checkbox = document.createElement("checkbox");
  200.         checkbox.id = itemID;
  201.         checkbox.setAttribute("label", bundle.getString(itemID + "_" + this._source));
  202.         dataSources.appendChild(checkbox);
  203.         if (!this._itemsFlags || this._itemsFlags & itemID)
  204.           checkbox.checked = true;
  205.       }
  206.     }
  207.   },
  208.  
  209.   onImportItemsPageAdvanced: function ()
  210.   {
  211.     var dataSources = document.getElementById("dataSources");
  212.     this._itemsFlags = 0;
  213.     for (var i = 0; i < dataSources.childNodes.length; ++i) {
  214.       var checkbox = dataSources.childNodes[i];
  215.       if (checkbox.localName == "checkbox" && checkbox.checked)
  216.         this._itemsFlags |= parseInt(checkbox.id);
  217.     }
  218.   },
  219.   
  220.   onImportItemCommand: function (aEvent)
  221.   {
  222.     var items = document.getElementById("dataSources");
  223.     var checkboxes = items.getElementsByTagName("checkbox");
  224.  
  225.     var oneChecked = false;
  226.     for (var i = 0; i < checkboxes.length; ++i) {
  227.       if (checkboxes[i].checked) {
  228.         oneChecked = true;
  229.         break;
  230.       }
  231.     }
  232.  
  233.     this._wiz.getButton("next").disabled = !oneChecked;
  234.   },
  235.   
  236.   // 4 - Migrating
  237.   onMigratingPageShow: function ()
  238.   {
  239.     this._wiz.getButton("cancel").disabled = true;
  240.     this._wiz.getButton("back").disabled = true;
  241.     this._wiz.getButton("next").disabled = true;
  242.     
  243.     // When automigrating, show all of the data that can be received from this source.
  244.     if (this._autoMigrate)
  245.       this._itemsFlags = this._migrator.getMigrateData(this._selectedProfile, this._autoMigrate);
  246.  
  247.     // When importing bookmarks, show only bookmarks
  248.     if (this._bookmarks)
  249.       this._itemsFlags = 32;
  250.  
  251.     this._listItems("migratingItems");
  252.     setTimeout(this.onMigratingMigrate, 0, this);
  253.   },
  254.   
  255.   onMigratingMigrate: function (aOuter)
  256.   {
  257.     aOuter._migrator.migrate(aOuter._itemsFlags, aOuter._autoMigrate, aOuter._selectedProfile);
  258.   },
  259.   
  260.   _listItems: function (aID)
  261.   {
  262.     var items = document.getElementById(aID);
  263.     while (items.hasChildNodes())
  264.       items.removeChild(items.firstChild);
  265.     
  266.     var bundle = document.getElementById("bundle");
  267.     var itemID;
  268.     for (var i = 0; i < 16; ++i) {
  269.       var itemID = (this._itemsFlags >> i) & 0x1 ? Math.pow(2, i) : 0;
  270.       if (itemID > 0) {
  271.         var label = document.createElement("label");
  272.         label.id = itemID + "_migrated";
  273.         try {
  274.           label.setAttribute("value", bundle.getString(itemID + "_" + this._source));
  275.           items.appendChild(label);
  276.         }
  277.         catch (e) {
  278.           // if the block above throws, we've enumerated all the import data types we
  279.           // currently support and are now just wasting time, break. 
  280.           break;
  281.         }
  282.       }
  283.     }
  284.   },
  285.   
  286.   observe: function (aSubject, aTopic, aData)
  287.   {
  288.     switch (aTopic) {
  289.     case "Migration:Started":
  290.       dump("*** started\n");
  291.       break;
  292.     case "Migration:ItemBeforeMigrate":
  293.       dump("*** before " + aData + "\n");
  294.       var label = document.getElementById(aData + "_migrated");
  295.       if (label)
  296.         label.setAttribute("style", "font-weight: bold");
  297.       break;
  298.     case "Migration:ItemAfterMigrate":
  299.       dump("*** after " + aData + "\n");
  300.       var label = document.getElementById(aData + "_migrated");
  301.       if (label)
  302.         label.removeAttribute("style");
  303.       break;
  304.     case "Migration:Ended":
  305.       dump("*** done\n");
  306.       if (this._autoMigrate) {
  307.         // We're done now.
  308.         this._wiz.advance();
  309.         setTimeout(close, 5000);
  310.       }
  311.       else {
  312.         var nextButton = this._wiz.getButton("next");
  313.         nextButton.disabled = false;
  314.         nextButton.click();
  315.       }
  316.       break;
  317.     }
  318.   },
  319.   
  320.   onDonePageShow: function ()
  321.   {
  322.     this._wiz.getButton("cancel").disabled = true;
  323.     this._wiz.getButton("back").disabled = true;
  324.     this._wiz.getButton("finish").disabled = false;
  325.     this._listItems("doneItems");
  326.   }
  327. };
  328.  
  329.  
  330.