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