!import
1 //@line 36 "/home/visbrero/mnt/roisin/rev_control/hg/mozilla/mail/components/migration/content/migration.js"
2
3 const kIMig = Components.interfaces.nsIMailProfileMigrator;
4 const kIPStartup = Components.interfaces.nsIProfileStartup;
5 const kProfileMigratorContractIDPrefix = "@mozilla.org/profile/migrator;1?app=mail&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
init
15 init: function ()
16 {
17 var os = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
18 os.addObserver(this, "Migration:Started", false);
19 os.addObserver(this, "Migration:ItemBeforeMigrate", false);
20 os.addObserver(this, "Migration:ItemAfterMigrate", false);
21 os.addObserver(this, "Migration:Ended", false);
22 os.addObserver(this, "Migration:Progress", false);
23
24 this._wiz = document.documentElement;
25
26 if ("arguments" in window) {
27 this._source = window.arguments[0];
28 this._migrator = window.arguments[1] ? window.arguments[1].QueryInterface(kIMig) : null;
29 this._autoMigrate = window.arguments[2].QueryInterface(kIPStartup);
30
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 this.onImportSourcePageShow();
38
39 // Behavior alert! If we were given a migrator already, then we are going to perform migration
40 // with that migrator, skip the wizard screen where we show all of the migration sources and
41 // jump right into migration.
42 if (this._migrator)
43 {
44 if (this._migrator.sourceHasMultipleProfiles)
45 this._wiz.goTo("selectProfile");
46 else
47 {
48 var sourceProfiles = this._migrator.sourceProfiles;
49 var profileName = sourceProfiles.QueryElementAt(0, Components.interfaces.nsISupportsString);
50 this._selectedProfile = profileName.data;
51 this._wiz.goTo("migrating");
52 }
53 }
54 },
55
uninit
56 uninit: function ()
57 {
58 var os = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
59 os.removeObserver(this, "Migration:Started");
60 os.removeObserver(this, "Migration:ItemBeforeMigrate");
61 os.removeObserver(this, "Migration:ItemAfterMigrate");
62 os.removeObserver(this, "Migration:Ended");
63 os.removeObserver(this, "Migration:Progress");
64 },
65
66 // 1 - Import Source
onImportSourcePageShow
67 onImportSourcePageShow: function ()
68 {
69 this._wiz.canRewind = false;
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") {
76 var contractID = kProfileMigratorContractIDPrefix + suffix;
77 var migrator = Components.classes[contractID].createInstance(kIMig);
78 if (!migrator.sourceExists) {
79 group.childNodes[i].hidden = true;
80 if (this._source == suffix) this._source = null;
81 }
82 }
83 }
84
85 var firstNonDisabled = null;
86 for (var i = 0; i < group.childNodes.length; ++i) {
87 if (!group.childNodes[i].hidden) {
88 firstNonDisabled = group.childNodes[i];
89 break;
90 }
91 }
92 group.selectedItem = this._source == "" ? firstNonDisabled : document.getElementById(this._source);
93 },
94
onImportSourcePageAdvanced
95 onImportSourcePageAdvanced: function ()
96 {
97 var newSource = document.getElementById("importSourceGroup").selectedItem.id;
98
99 if (newSource == "nothing") {
100 document.documentElement.cancel();
101 return;
102 }
103
104 if (!this._migrator || (newSource != this._source)) {
105 // Create the migrator for the selected source.
106 var contractID = kProfileMigratorContractIDPrefix + newSource;
107 this._migrator = Components.classes[contractID].createInstance(kIMig);
108
109 this._itemsFlags = kIMig.ALL;
110 this._selectedProfile = null;
111 }
112
113 this._source = newSource;
114
115 // check for more than one source profile
116 if (this._migrator.sourceHasMultipleProfiles)
117 this._wiz.currentPage.next = "selectProfile";
118 else {
119 this._wiz.currentPage.next = "migrating";
120 var sourceProfiles = this._migrator.sourceProfiles;
121 if (sourceProfiles && sourceProfiles.Count() == 1) {
122 var profileName = sourceProfiles.QueryElementAt(0, Components.interfaces.nsISupportsString);
123 this._selectedProfile = profileName.data;
124 }
125 else
126 this._selectedProfile = "";
127 }
128 },
129
130 // 2 - [Profile Selection]
onSelectProfilePageShow
131 onSelectProfilePageShow: function ()
132 {
133 // Disabling this for now, since we ask about import sources in automigration
134 // too and don't want to disable the back button
135 // if (this._autoMigrate)
136 // document.documentElement.getButton("back").disabled = true;
137
138 var profiles = document.getElementById("profiles");
139 while (profiles.hasChildNodes())
140 profiles.removeChild(profiles.firstChild);
141
142 var sourceProfiles = this._migrator.sourceProfiles;
143 var count = sourceProfiles.Count();
144 for (var i = 0; i < count; ++i) {
145 var item = document.createElement("radio");
146 var str = sourceProfiles.QueryElementAt(i, Components.interfaces.nsISupportsString);
147 item.id = str.data;
148 item.setAttribute("label", str.data);
149 profiles.appendChild(item);
150 }
151
152 profiles.selectedItem = this._selectedProfile ? document.getElementById(this._selectedProfile) : profiles.firstChild;
153 },
154
155 onSelectProfilePageRewound: function ()
156 {
157 var profiles = document.getElementById("profiles");
158 this._selectedProfile = profiles.selectedItem.id;
159 },
160
onSelectProfilePageAdvanced
161 onSelectProfilePageAdvanced: function ()
162 {
163 var profiles = document.getElementById("profiles");
164 this._selectedProfile = profiles.selectedItem.id;
165
166 // If we're automigrating, don't show the item selection page, just grab everything.
167 if (this._autoMigrate)
168 this._wiz.currentPage.next = "migrating";
169 },
170
171 // 3 - ImportItems
onImportItemsPageShow
172 onImportItemsPageShow: function ()
173 {
174 var dataSources = document.getElementById("dataSources");
175 while (dataSources.hasChildNodes())
176 dataSources.removeChild(dataSources.firstChild);
177
178 var bundle = document.getElementById("bundle");
179
180 var items = this._migrator.getMigrateData(this._selectedProfile, this._autoMigrate);
181 for (var i = 0; i < 16; ++i) {
182 var itemID = (items >> i) & 0x1 ? Math.pow(2, i) : 0;
183 if (itemID > 0) {
184 var checkbox = document.createElement("checkbox");
185 checkbox.id = itemID;
186 checkbox.setAttribute("label", bundle.getString(itemID + "_" + this._source));
187 dataSources.appendChild(checkbox);
188 if (!this._itemsFlags || this._itemsFlags & itemID)
189 checkbox.checked = true;
190 }
191 }
192 },
193
onImportItemsPageAdvanced
194 onImportItemsPageAdvanced: function ()
195 {
196 var dataSources = document.getElementById("dataSources");
197 this._itemsFlags = 0;
198 for (var i = 0; i < dataSources.childNodes.length; ++i) {
199 var checkbox = dataSources.childNodes[i];
200 if (checkbox.localName == "checkbox" && checkbox.checked)
201 this._itemsFlags |= parseInt(checkbox.id);
202 }
203 },
204
onImportItemCommand
205 onImportItemCommand: function (aEvent)
206 {
207 var items = document.getElementById("dataSources");
208 var checkboxes = items.getElementsByTagName("checkbox");
209
210 var oneChecked = false;
211 for (var i = 0; i < checkboxes.length; ++i) {
212 if (checkboxes[i].checked) {
213 oneChecked = true;
214 break;
215 }
216 }
217
218 this._wiz.canAdvance = oneChecked;
219 },
220
221 // 4 - Migrating
onMigratingPageShow
222 onMigratingPageShow: function ()
223 {
224 this._wiz.getButton("cancel").disabled = true;
225 this._wiz.canRewind = false;
226 this._wiz.canAdvance = false;
227
228 // When automigrating, show all of the data that can be received from this source.
229 if (this._autoMigrate)
230 this._itemsFlags = this._migrator.getMigrateData(this._selectedProfile, this._autoMigrate);
231
232 this._listItems("migratingItems");
233 setTimeout(this.onMigratingMigrate, 0, this);
234 },
235
onMigratingMigrate
236 onMigratingMigrate: function (aOuter)
237 {
238 aOuter._migrator.migrate(aOuter._itemsFlags, aOuter._autoMigrate, aOuter._selectedProfile);
239 },
240
_listItems
241 _listItems: function (aID)
242 {
243 var items = document.getElementById(aID);
244 while (items.hasChildNodes())
245 items.removeChild(items.firstChild);
246
247 var bundle = document.getElementById("bundle");
248 var itemID;
249 for (var i = 0; i < 16; ++i) {
250 var itemID = (this._itemsFlags >> i) & 0x1 ? Math.pow(2, i) : 0;
251 if (itemID > 0) {
252 var label = document.createElement("label");
253 label.id = itemID + "_migrated";
254 try {
255 label.setAttribute("value", "- " + bundle.getString(itemID + "_" + this._source));
256 items.appendChild(label);
257 }
258 catch (e) {
259 // if the block above throws, we've enumerated all the import data types we
260 // currently support and are now just wasting time, break.
261 break;
262 }
263 }
264 }
265 },
266
observe
267 observe: function (aSubject, aTopic, aData)
268 {
269 switch (aTopic) {
270 case "Migration:Started":
271 dump("*** started\n");
272 break;
273 case "Migration:ItemBeforeMigrate":
274 dump("*** before " + aData + "\n");
275 var label = document.getElementById(aData + "_migrated");
276 if (label)
277 label.setAttribute("style", "font-weight: bold");
278 break;
279 case "Migration:ItemAfterMigrate":
280 dump("*** after " + aData + "\n");
281 var label = document.getElementById(aData + "_migrated");
282 if (label)
283 label.removeAttribute("style");
284 break;
285 case "Migration:Ended":
286 dump("*** done\n");
287 if (this._autoMigrate) {
288 // We're done now.
289 this._wiz.canAdvance = true;
290 this._wiz.advance();
291 setTimeout("close()", 5000);
292 }
293 else {
294 this._wiz.canAdvance = true;
295 var nextButton = this._wiz.getButton("next");
296 nextButton.click();
297 }
298 break;
299 case "Migration:Progress":
300 document.getElementById('progressBar').value = aData;
301 break;
302 }
303 },
304
onDonePageShow
305 onDonePageShow: function ()
306 {
307 this._wiz.getButton("cancel").disabled = true;
308 this._wiz.canRewind = false;
309 this._listItems("doneItems");
310 }
311 };