Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / browser / components / migration / src / nsSeamonkeyProfileMigrator.cpp
blob7b8abbb1357df404151887362510ef82a92191cd
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is The Browser Profile Migrator.
17 * The Initial Developer of the Original Code is Ben Goodger.
18 * Portions created by the Initial Developer are Copyright (C) 2004
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Ben Goodger <ben@bengoodger.com>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsBrowserProfileMigratorUtils.h"
39 #include "nsDirectoryServiceDefs.h"
40 #include "nsIObserverService.h"
41 #include "nsILoginInfo.h"
42 #include "nsILoginManager.h"
43 #include "nsILoginManagerStorage.h"
44 #include "nsIPrefLocalizedString.h"
45 #include "nsIPrefService.h"
46 #include "nsIServiceManager.h"
47 #include "nsISupportsArray.h"
48 #include "nsISupportsPrimitives.h"
49 #include "nsNetCID.h"
50 #include "nsNetUtil.h"
51 #include "nsSeamonkeyProfileMigrator.h"
52 #include "nsVoidArray.h"
53 #include "nsIProfileMigrator.h"
55 ///////////////////////////////////////////////////////////////////////////////
56 // nsSeamonkeyProfileMigrator
58 #define FILE_NAME_BOOKMARKS NS_LITERAL_STRING("bookmarks.html")
59 #define FILE_NAME_COOKIES NS_LITERAL_STRING("cookies.txt")
60 #define FILE_NAME_SITEPERM_OLD NS_LITERAL_STRING("cookperm.txt")
61 #define FILE_NAME_SITEPERM_NEW NS_LITERAL_STRING("hostperm.1")
62 #define FILE_NAME_CERT8DB NS_LITERAL_STRING("cert8.db")
63 #define FILE_NAME_KEY3DB NS_LITERAL_STRING("key3.db")
64 #define FILE_NAME_SECMODDB NS_LITERAL_STRING("secmod.db")
65 #define FILE_NAME_HISTORY NS_LITERAL_STRING("history.dat")
66 #define FILE_NAME_MIMETYPES NS_LITERAL_STRING("mimeTypes.rdf")
67 #define FILE_NAME_DOWNLOADS NS_LITERAL_STRING("downloads.rdf")
68 #define FILE_NAME_PREFS NS_LITERAL_STRING("prefs.js")
69 #define FILE_NAME_USER_PREFS NS_LITERAL_STRING("user.js")
70 #define FILE_NAME_USERCONTENT NS_LITERAL_STRING("userContent.css")
71 #define DIR_NAME_CHROME NS_LITERAL_STRING("chrome")
73 NS_IMPL_ISUPPORTS1(nsSeamonkeyProfileMigrator, nsIBrowserProfileMigrator)
75 nsSeamonkeyProfileMigrator::nsSeamonkeyProfileMigrator()
77 mObserverService = do_GetService("@mozilla.org/observer-service;1");
80 nsSeamonkeyProfileMigrator::~nsSeamonkeyProfileMigrator()
84 ///////////////////////////////////////////////////////////////////////////////
85 // nsIBrowserProfileMigrator
87 NS_IMETHODIMP
88 nsSeamonkeyProfileMigrator::Migrate(PRUint16 aItems, nsIProfileStartup* aStartup, const PRUnichar* aProfile)
90 nsresult rv = NS_OK;
91 PRBool aReplace = aStartup ? PR_TRUE : PR_FALSE;
93 if (!mTargetProfile) {
94 GetProfilePath(aStartup, mTargetProfile);
95 if (!mTargetProfile) return NS_ERROR_FAILURE;
97 if (!mSourceProfile)
98 GetSourceProfile(aProfile);
100 NOTIFY_OBSERVERS(MIGRATION_STARTED, nsnull);
102 COPY_DATA(CopyPreferences, aReplace, nsIBrowserProfileMigrator::SETTINGS);
103 COPY_DATA(CopyCookies, aReplace, nsIBrowserProfileMigrator::COOKIES);
104 COPY_DATA(CopyHistory, aReplace, nsIBrowserProfileMigrator::HISTORY);
105 COPY_DATA(CopyPasswords, aReplace, nsIBrowserProfileMigrator::PASSWORDS);
106 COPY_DATA(CopyOtherData, aReplace, nsIBrowserProfileMigrator::OTHERDATA);
108 // Need to do startup before trying to copy bookmarks, since bookmarks
109 // import requires a profile. Can't do it earlier because services might
110 // end up creating the files we try to copy above.
111 if (aStartup) {
112 rv = aStartup->DoStartup();
113 NS_ENSURE_SUCCESS(rv, rv);
116 COPY_DATA(CopyBookmarks, aReplace, nsIBrowserProfileMigrator::BOOKMARKS);
118 if (aReplace &&
119 (aItems & nsIBrowserProfileMigrator::SETTINGS ||
120 aItems & nsIBrowserProfileMigrator::COOKIES ||
121 aItems & nsIBrowserProfileMigrator::PASSWORDS ||
122 !aItems)) {
123 // Permissions (Images, Cookies, Popups)
124 rv |= CopyFile(FILE_NAME_SITEPERM_NEW, FILE_NAME_SITEPERM_NEW);
125 rv |= CopyFile(FILE_NAME_SITEPERM_OLD, FILE_NAME_SITEPERM_OLD);
128 NOTIFY_OBSERVERS(MIGRATION_ENDED, nsnull);
130 return rv;
133 NS_IMETHODIMP
134 nsSeamonkeyProfileMigrator::GetMigrateData(const PRUnichar* aProfile,
135 PRBool aReplace,
136 PRUint16* aResult)
138 *aResult = 0;
140 if (!mSourceProfile) {
141 GetSourceProfile(aProfile);
142 if (!mSourceProfile)
143 return NS_ERROR_FILE_NOT_FOUND;
146 MigrationData data[] = { { ToNewUnicode(FILE_NAME_PREFS),
147 nsIBrowserProfileMigrator::SETTINGS,
148 PR_TRUE },
149 { ToNewUnicode(FILE_NAME_USER_PREFS),
150 nsIBrowserProfileMigrator::SETTINGS,
151 PR_TRUE },
152 { ToNewUnicode(FILE_NAME_COOKIES),
153 nsIBrowserProfileMigrator::COOKIES,
154 PR_FALSE },
155 { ToNewUnicode(FILE_NAME_HISTORY),
156 nsIBrowserProfileMigrator::HISTORY,
157 PR_TRUE },
158 { ToNewUnicode(FILE_NAME_BOOKMARKS),
159 nsIBrowserProfileMigrator::BOOKMARKS,
160 PR_FALSE },
161 { ToNewUnicode(FILE_NAME_DOWNLOADS),
162 nsIBrowserProfileMigrator::OTHERDATA,
163 PR_TRUE },
164 { ToNewUnicode(FILE_NAME_MIMETYPES),
165 nsIBrowserProfileMigrator::OTHERDATA,
166 PR_TRUE } };
168 // Frees file name strings allocated above.
169 GetMigrateDataFromArray(data, sizeof(data)/sizeof(MigrationData),
170 aReplace, mSourceProfile, aResult);
172 // Now locate passwords
173 nsCString signonsFileName;
174 GetSignonFileName(aReplace, getter_Copies(signonsFileName));
176 if (!signonsFileName.IsEmpty()) {
177 NS_ConvertASCIItoUTF16 fileName(signonsFileName);
178 nsCOMPtr<nsIFile> sourcePasswordsFile;
179 mSourceProfile->Clone(getter_AddRefs(sourcePasswordsFile));
180 sourcePasswordsFile->Append(fileName);
182 PRBool exists;
183 sourcePasswordsFile->Exists(&exists);
184 if (exists)
185 *aResult |= nsIBrowserProfileMigrator::PASSWORDS;
188 return NS_OK;
191 NS_IMETHODIMP
192 nsSeamonkeyProfileMigrator::GetSourceExists(PRBool* aResult)
194 nsCOMPtr<nsISupportsArray> profiles;
195 GetSourceProfiles(getter_AddRefs(profiles));
197 if (profiles) {
198 PRUint32 count;
199 profiles->Count(&count);
200 *aResult = count > 0;
202 else
203 *aResult = PR_FALSE;
205 return NS_OK;
208 NS_IMETHODIMP
209 nsSeamonkeyProfileMigrator::GetSourceHasMultipleProfiles(PRBool* aResult)
211 nsCOMPtr<nsISupportsArray> profiles;
212 GetSourceProfiles(getter_AddRefs(profiles));
214 if (profiles) {
215 PRUint32 count;
216 profiles->Count(&count);
217 *aResult = count > 1;
219 else
220 *aResult = PR_FALSE;
222 return NS_OK;
225 NS_IMETHODIMP
226 nsSeamonkeyProfileMigrator::GetSourceProfiles(nsISupportsArray** aResult)
228 if (!mProfileNames && !mProfileLocations) {
229 mProfileNames = do_CreateInstance(NS_SUPPORTSARRAY_CONTRACTID);
230 mProfileLocations = do_CreateInstance(NS_SUPPORTSARRAY_CONTRACTID);
231 NS_ENSURE_TRUE(mProfileNames && mProfileLocations, NS_ERROR_UNEXPECTED);
233 // Fills mProfileNames and mProfileLocations
234 FillProfileDataFromSeamonkeyRegistry();
237 NS_IF_ADDREF(*aResult = mProfileNames);
238 return NS_OK;
241 NS_IMETHODIMP
242 nsSeamonkeyProfileMigrator::GetSourceHomePageURL(nsACString& aResult)
244 // Load the source pref file
245 nsCOMPtr<nsIPrefService> psvc(do_GetService(NS_PREFSERVICE_CONTRACTID));
246 psvc->ResetPrefs();
248 nsCOMPtr<nsIFile> sourcePrefsFile;
250 mSourceProfile->Clone(getter_AddRefs(sourcePrefsFile));
251 sourcePrefsFile->Append(FILE_NAME_PREFS);
253 psvc->ReadUserPrefs(sourcePrefsFile);
255 nsCOMPtr<nsIPrefBranch> branch(do_QueryInterface(psvc));
257 PRBool hasUserValue;
258 nsCOMPtr<nsIPrefLocalizedString> prefValue;
259 nsresult rv = branch->PrefHasUserValue("browser.startup.homepage", &hasUserValue);
260 if (NS_SUCCEEDED(rv) && hasUserValue) {
261 rv = branch->GetComplexValue("browser.startup.homepage",
262 NS_GET_IID(nsIPrefLocalizedString),
263 getter_AddRefs(prefValue));
264 if (NS_SUCCEEDED(rv) && prefValue) {
265 nsString data;
266 prefValue->ToString(getter_Copies(data));
268 nsCAutoString val;
269 val = ToNewCString(NS_ConvertUTF16toUTF8(data));
271 aResult.Assign(val);
275 psvc->ResetPrefs();
276 psvc->ReadUserPrefs(nsnull);
278 return NS_OK;
281 ///////////////////////////////////////////////////////////////////////////////
282 // nsSeamonkeyProfileMigrator
284 nsresult
285 nsSeamonkeyProfileMigrator::GetSourceProfile(const PRUnichar* aProfile)
287 PRUint32 count;
288 mProfileNames->Count(&count);
289 for (PRUint32 i = 0; i < count; ++i) {
290 nsCOMPtr<nsISupportsString> str;
291 mProfileNames->QueryElementAt(i, NS_GET_IID(nsISupportsString),
292 getter_AddRefs(str));
293 nsString profileName;
294 str->GetData(profileName);
295 if (profileName.Equals(aProfile)) {
296 mProfileLocations->QueryElementAt(i, NS_GET_IID(nsILocalFile),
297 getter_AddRefs(mSourceProfile));
298 break;
302 return NS_OK;
305 nsresult
306 nsSeamonkeyProfileMigrator::FillProfileDataFromSeamonkeyRegistry()
308 // Find the Seamonkey Registry
309 nsCOMPtr<nsIProperties> fileLocator(do_GetService("@mozilla.org/file/directory_service;1"));
310 nsCOMPtr<nsILocalFile> seamonkeyRegistry;
311 #ifdef XP_WIN
312 fileLocator->Get(NS_WIN_APPDATA_DIR, NS_GET_IID(nsILocalFile), getter_AddRefs(seamonkeyRegistry));
314 seamonkeyRegistry->Append(NS_LITERAL_STRING("Mozilla"));
315 seamonkeyRegistry->Append(NS_LITERAL_STRING("registry.dat"));
316 #elif defined(XP_MACOSX)
317 fileLocator->Get(NS_MAC_USER_LIB_DIR, NS_GET_IID(nsILocalFile), getter_AddRefs(seamonkeyRegistry));
319 seamonkeyRegistry->Append(NS_LITERAL_STRING("Mozilla"));
320 seamonkeyRegistry->Append(NS_LITERAL_STRING("Application Registry"));
321 #elif defined(XP_UNIX)
322 fileLocator->Get(NS_UNIX_HOME_DIR, NS_GET_IID(nsILocalFile), getter_AddRefs(seamonkeyRegistry));
324 seamonkeyRegistry->Append(NS_LITERAL_STRING(".mozilla"));
325 seamonkeyRegistry->Append(NS_LITERAL_STRING("appreg"));
326 #elif defined(XP_BEOS)
327 fileLocator->Get(NS_BEOS_SETTINGS_DIR, NS_GET_IID(nsILocalFile), getter_AddRefs(seamonkeyRegistry));
329 seamonkeyRegistry->Append(NS_LITERAL_STRING("Mozilla"));
330 seamonkeyRegistry->Append(NS_LITERAL_STRING("appreg"));
331 #elif defined(XP_OS2)
332 fileLocator->Get(NS_OS2_HOME_DIR, NS_GET_IID(nsILocalFile), getter_AddRefs(seamonkeyRegistry));
334 seamonkeyRegistry->Append(NS_LITERAL_STRING("Mozilla"));
335 seamonkeyRegistry->Append(NS_LITERAL_STRING("registry.dat"));
336 #endif
338 return GetProfileDataFromRegistry(seamonkeyRegistry, mProfileNames, mProfileLocations);
341 #define F(a) nsSeamonkeyProfileMigrator::a
343 #define MAKEPREFTRANSFORM(pref, newpref, getmethod, setmethod) \
344 { pref, newpref, F(Get##getmethod), F(Set##setmethod), PR_FALSE, { -1 } }
346 #define MAKESAMETYPEPREFTRANSFORM(pref, method) \
347 { pref, 0, F(Get##method), F(Set##method), PR_FALSE, { -1 } }
350 static
351 nsSeamonkeyProfileMigrator::PrefTransform gTransforms[] = {
352 MAKESAMETYPEPREFTRANSFORM("signon.SignonFileName", String),
353 MAKESAMETYPEPREFTRANSFORM("browser.history_expire_days", Int),
354 MAKESAMETYPEPREFTRANSFORM("browser.tabs.autoHide", Bool),
355 MAKESAMETYPEPREFTRANSFORM("browser.tabs.loadInBackground", Bool),
356 MAKESAMETYPEPREFTRANSFORM("browser.enable_automatic_image_resizing", Bool),
357 MAKESAMETYPEPREFTRANSFORM("network.cookie.warnAboutCookies", Bool),
358 MAKESAMETYPEPREFTRANSFORM("network.cookie.lifetime.enabled", Bool),
359 MAKESAMETYPEPREFTRANSFORM("network.cookie.lifetime.behavior", Int),
360 MAKESAMETYPEPREFTRANSFORM("dom.disable_open_during_load", Bool),
361 MAKESAMETYPEPREFTRANSFORM("signon.rememberSignons", Bool),
362 MAKESAMETYPEPREFTRANSFORM("security.enable_ssl2", Bool),
363 MAKESAMETYPEPREFTRANSFORM("security.enable_ssl3", Bool),
364 MAKESAMETYPEPREFTRANSFORM("security.enable_tls", Bool),
365 MAKESAMETYPEPREFTRANSFORM("security.warn_entering_secure", Bool),
366 MAKESAMETYPEPREFTRANSFORM("security.warn_entering_weak", Bool),
367 MAKESAMETYPEPREFTRANSFORM("security.warn_leaving_secure", Bool),
368 MAKESAMETYPEPREFTRANSFORM("security.warn_submit_insecure", Bool),
369 MAKESAMETYPEPREFTRANSFORM("security.warn_viewing_mixed", Bool),
370 MAKESAMETYPEPREFTRANSFORM("security.default_personal_cert", String),
371 MAKESAMETYPEPREFTRANSFORM("security.OSCP.enabled", Int),
372 MAKESAMETYPEPREFTRANSFORM("security.OSCP.signingCA", String),
373 MAKESAMETYPEPREFTRANSFORM("security.OSCP.URL", String),
374 MAKESAMETYPEPREFTRANSFORM("security.enable_java", Bool),
375 MAKESAMETYPEPREFTRANSFORM("javascript.enabled", Bool),
376 MAKESAMETYPEPREFTRANSFORM("dom.disable_window_move_resize", Bool),
377 MAKESAMETYPEPREFTRANSFORM("dom.disable_window_flip", Bool),
378 MAKESAMETYPEPREFTRANSFORM("dom.disable_window_open_feature.status", Bool),
379 MAKESAMETYPEPREFTRANSFORM("dom.disable_window_status_change", Bool),
380 MAKESAMETYPEPREFTRANSFORM("dom.disable_image_src_set", Bool),
381 MAKESAMETYPEPREFTRANSFORM("accessibility.typeaheadfind.autostart", Bool),
382 MAKESAMETYPEPREFTRANSFORM("accessibility.typeaheadfind.linksonly", Bool),
383 MAKESAMETYPEPREFTRANSFORM("network.proxy.type", Int),
384 MAKESAMETYPEPREFTRANSFORM("network.proxy.http", String),
385 MAKESAMETYPEPREFTRANSFORM("network.proxy.http_port", Int),
386 MAKESAMETYPEPREFTRANSFORM("network.proxy.ftp", String),
387 MAKESAMETYPEPREFTRANSFORM("network.proxy.ftp_port", Int),
388 MAKESAMETYPEPREFTRANSFORM("network.proxy.ssl", String),
389 MAKESAMETYPEPREFTRANSFORM("network.proxy.ssl_port", Int),
390 MAKESAMETYPEPREFTRANSFORM("network.proxy.socks", String),
391 MAKESAMETYPEPREFTRANSFORM("network.proxy.socks_port", Int),
392 MAKESAMETYPEPREFTRANSFORM("network.proxy.gopher", String),
393 MAKESAMETYPEPREFTRANSFORM("network.proxy.gopher_port", Int),
394 MAKESAMETYPEPREFTRANSFORM("network.proxy.no_proxies_on", String),
395 MAKESAMETYPEPREFTRANSFORM("network.proxy.autoconfig_url", String),
396 MAKESAMETYPEPREFTRANSFORM("browser.display.foreground_color", String),
397 MAKESAMETYPEPREFTRANSFORM("browser.display.background_color", String),
398 MAKESAMETYPEPREFTRANSFORM("browser.anchor_color", String),
399 MAKESAMETYPEPREFTRANSFORM("browser.visited_color", String),
400 MAKESAMETYPEPREFTRANSFORM("browser.underline_anchors", Bool),
401 MAKESAMETYPEPREFTRANSFORM("browser.display.use_system_colors", Bool),
402 MAKESAMETYPEPREFTRANSFORM("browser.display.use_document_colors", Bool),
403 MAKESAMETYPEPREFTRANSFORM("browser.display.use_document_fonts", Bool),
404 MAKESAMETYPEPREFTRANSFORM("intl.charset.default", String),
405 MAKESAMETYPEPREFTRANSFORM("intl.accept_languages", String),
406 MAKESAMETYPEPREFTRANSFORM("intl.accept_charsets", String),
408 MAKEPREFTRANSFORM("network.image.imageBehavior", 0, Int, Image),
409 MAKEPREFTRANSFORM("network.cookie.cookieBehavior", 0, Int, Cookie),
410 MAKEPREFTRANSFORM("browser.downloadmanager.behavior", 0, Int, DownloadManager),
412 MAKEPREFTRANSFORM("wallet.captureForms", "formfill.enabled", Bool, Bool)
415 nsresult
416 nsSeamonkeyProfileMigrator::SetImage(void* aTransform, nsIPrefBranch* aBranch)
418 PrefTransform* xform = (PrefTransform*)aTransform;
419 nsresult rv = NS_OK;
421 if (xform->prefHasValue)
422 rv = aBranch->SetIntPref("network.image.imageBehavior", xform->intValue == 1 ? 0 : xform->intValue);
424 return rv;
427 nsresult
428 nsSeamonkeyProfileMigrator::SetCookie(void* aTransform, nsIPrefBranch* aBranch)
430 PrefTransform* xform = (PrefTransform*)aTransform;
431 nsresult rv = NS_OK;
433 if (xform->prefHasValue)
434 rv = aBranch->SetIntPref("network.cookie.cookieBehavior", xform->intValue == 3 ? 0 : xform->intValue);
436 return rv;
439 nsresult
440 nsSeamonkeyProfileMigrator::SetDownloadManager(void* aTransform, nsIPrefBranch* aBranch)
442 PrefTransform* xform = (PrefTransform*)aTransform;
443 nsresult rv = NS_OK;
445 if (xform->prefHasValue) {
446 // Seamonkey's download manager uses a single pref to control behavior:
447 // 0 - show download manager window
448 // 1 - show individual progress dialogs
449 // 2 - show nothing
451 // Firefox has only a download manager window, but it can behave like a progress dialog, thus:
452 // 0 || 1 -> show downloads window when a download starts
453 // 2 -> don't show anything when a download starts
454 // 1 -> close the downloads window as if it were a progress window when downloads complete.
456 rv |= aBranch->SetBoolPref("browser.download.manager.showWhenStarting", xform->intValue != 2);
457 rv |= aBranch->SetBoolPref("browser.download.manager.closeWhenDone", xform->intValue == 1);
459 return NS_OK;
462 nsresult
463 nsSeamonkeyProfileMigrator::TransformPreferences(const nsAString& aSourcePrefFileName,
464 const nsAString& aTargetPrefFileName)
466 PrefTransform* transform;
467 PrefTransform* end = gTransforms + sizeof(gTransforms)/sizeof(PrefTransform);
469 // Load the source pref file
470 nsCOMPtr<nsIPrefService> psvc(do_GetService(NS_PREFSERVICE_CONTRACTID));
471 psvc->ResetPrefs();
473 nsCOMPtr<nsIFile> sourcePrefsFile;
474 mSourceProfile->Clone(getter_AddRefs(sourcePrefsFile));
475 sourcePrefsFile->Append(aSourcePrefFileName);
476 psvc->ReadUserPrefs(sourcePrefsFile);
478 nsCOMPtr<nsIPrefBranch> branch(do_QueryInterface(psvc));
479 for (transform = gTransforms; transform < end; ++transform)
480 transform->prefGetterFunc(transform, branch);
482 nsVoidArray* fontPrefs = new nsVoidArray();
483 if (!fontPrefs)
484 return NS_ERROR_OUT_OF_MEMORY;
485 ReadFontsBranch(psvc, fontPrefs);
487 // Now that we have all the pref data in memory, load the target pref file,
488 // and write it back out
489 psvc->ResetPrefs();
490 for (transform = gTransforms; transform < end; ++transform)
491 transform->prefSetterFunc(transform, branch);
493 WriteFontsBranch(psvc, fontPrefs);
494 delete fontPrefs;
495 fontPrefs = nsnull;
497 nsCOMPtr<nsIFile> targetPrefsFile;
498 mTargetProfile->Clone(getter_AddRefs(targetPrefsFile));
499 targetPrefsFile->Append(aTargetPrefFileName);
500 psvc->SavePrefFile(targetPrefsFile);
502 psvc->ResetPrefs();
503 psvc->ReadUserPrefs(nsnull);
505 return NS_OK;
508 struct FontPref {
509 char* prefName;
510 PRInt32 type;
511 union {
512 char* stringValue;
513 PRInt32 intValue;
514 PRBool boolValue;
515 PRUnichar* wstringValue;
519 void
520 nsSeamonkeyProfileMigrator::ReadFontsBranch(nsIPrefService* aPrefService,
521 nsVoidArray* aPrefs)
523 // Enumerate the branch
524 nsCOMPtr<nsIPrefBranch> branch;
525 aPrefService->GetBranch("font.", getter_AddRefs(branch));
527 PRUint32 count;
528 char** prefs = nsnull;
529 nsresult rv = branch->GetChildList("", &count, &prefs);
530 if (NS_FAILED(rv)) return;
532 for (PRUint32 i = 0; i < count; ++i) {
533 // Save each pref's value into an array
534 char* currPref = prefs[i];
535 PRInt32 type;
536 branch->GetPrefType(currPref, &type);
537 FontPref* pref = new FontPref;
538 pref->prefName = currPref;
539 pref->type = type;
540 switch (type) {
541 case nsIPrefBranch::PREF_STRING:
542 rv = branch->GetCharPref(currPref, &pref->stringValue);
543 break;
544 case nsIPrefBranch::PREF_BOOL:
545 rv = branch->GetBoolPref(currPref, &pref->boolValue);
546 break;
547 case nsIPrefBranch::PREF_INT:
548 rv = branch->GetIntPref(currPref, &pref->intValue);
549 break;
550 case nsIPrefBranch::PREF_INVALID:
552 nsCOMPtr<nsIPrefLocalizedString> str;
553 rv = branch->GetComplexValue(currPref,
554 NS_GET_IID(nsIPrefLocalizedString),
555 getter_AddRefs(str));
556 if (NS_SUCCEEDED(rv) && str)
557 str->ToString(&pref->wstringValue);
559 break;
562 if (NS_SUCCEEDED(rv))
563 aPrefs->AppendElement((void*)pref);
567 void
568 nsSeamonkeyProfileMigrator::WriteFontsBranch(nsIPrefService* aPrefService,
569 nsVoidArray* aPrefs)
571 nsresult rv;
573 // Enumerate the branch
574 nsCOMPtr<nsIPrefBranch> branch;
575 aPrefService->GetBranch("font.", getter_AddRefs(branch));
577 PRUint32 count = aPrefs->Count();
578 for (PRUint32 i = 0; i < count; ++i) {
579 FontPref* pref = (FontPref*)aPrefs->ElementAt(i);
580 switch (pref->type) {
581 case nsIPrefBranch::PREF_STRING:
582 rv = branch->SetCharPref(pref->prefName, pref->stringValue);
583 NS_Free(pref->stringValue);
584 pref->stringValue = nsnull;
585 break;
586 case nsIPrefBranch::PREF_BOOL:
587 rv = branch->SetBoolPref(pref->prefName, pref->boolValue);
588 break;
589 case nsIPrefBranch::PREF_INT:
590 rv = branch->SetIntPref(pref->prefName, pref->intValue);
591 break;
592 case nsIPrefBranch::PREF_INVALID:
593 nsCOMPtr<nsIPrefLocalizedString> pls(do_CreateInstance("@mozilla.org/pref-localizedstring;1"));
594 pls->SetData(pref->wstringValue);
595 rv = branch->SetComplexValue(pref->prefName,
596 NS_GET_IID(nsIPrefLocalizedString),
597 pls);
598 NS_Free(pref->wstringValue);
599 pref->wstringValue = nsnull;
600 break;
602 NS_Free(pref->prefName);
603 pref->prefName = nsnull;
604 delete pref;
605 pref = nsnull;
607 aPrefs->Clear();
610 nsresult
611 nsSeamonkeyProfileMigrator::CopyPreferences(PRBool aReplace)
613 nsresult rv = NS_OK;
614 if (!aReplace)
615 return rv;
617 rv |= TransformPreferences(FILE_NAME_PREFS, FILE_NAME_PREFS);
618 rv |= CopyFile(FILE_NAME_USER_PREFS, FILE_NAME_USER_PREFS);
620 // Security Stuff
621 rv |= CopyFile(FILE_NAME_CERT8DB, FILE_NAME_CERT8DB);
622 rv |= CopyFile(FILE_NAME_KEY3DB, FILE_NAME_KEY3DB);
623 rv |= CopyFile(FILE_NAME_SECMODDB, FILE_NAME_SECMODDB);
625 // User MIME Type overrides
626 rv |= CopyFile(FILE_NAME_MIMETYPES, FILE_NAME_MIMETYPES);
628 rv |= CopyUserContentSheet();
629 return rv;
632 nsresult
633 nsSeamonkeyProfileMigrator::CopyUserContentSheet()
635 nsCOMPtr<nsIFile> sourceUserContent;
636 mSourceProfile->Clone(getter_AddRefs(sourceUserContent));
637 sourceUserContent->Append(DIR_NAME_CHROME);
638 sourceUserContent->Append(FILE_NAME_USERCONTENT);
640 PRBool exists = PR_FALSE;
641 sourceUserContent->Exists(&exists);
642 if (!exists)
643 return NS_OK;
645 nsCOMPtr<nsIFile> targetUserContent;
646 mTargetProfile->Clone(getter_AddRefs(targetUserContent));
647 targetUserContent->Append(DIR_NAME_CHROME);
648 nsCOMPtr<nsIFile> targetChromeDir;
649 targetUserContent->Clone(getter_AddRefs(targetChromeDir));
650 targetUserContent->Append(FILE_NAME_USERCONTENT);
652 targetUserContent->Exists(&exists);
653 if (exists)
654 targetUserContent->Remove(PR_FALSE);
656 return sourceUserContent->CopyTo(targetChromeDir, FILE_NAME_USERCONTENT);
659 nsresult
660 nsSeamonkeyProfileMigrator::CopyCookies(PRBool aReplace)
662 nsresult rv;
663 if (aReplace)
664 rv = CopyFile(FILE_NAME_COOKIES, FILE_NAME_COOKIES);
665 else {
666 nsCOMPtr<nsIFile> seamonkeyCookiesFile;
667 mSourceProfile->Clone(getter_AddRefs(seamonkeyCookiesFile));
668 seamonkeyCookiesFile->Append(FILE_NAME_COOKIES);
670 rv = ImportNetscapeCookies(seamonkeyCookiesFile);
672 return rv;
675 nsresult
676 nsSeamonkeyProfileMigrator::CopyHistory(PRBool aReplace)
678 return aReplace ? CopyFile(FILE_NAME_HISTORY, FILE_NAME_HISTORY) : NS_OK;
681 nsresult
682 nsSeamonkeyProfileMigrator::CopyPasswords(PRBool aReplace)
684 nsresult rv;
686 nsCString signonsFileName;
687 GetSignonFileName(aReplace, getter_Copies(signonsFileName));
689 if (signonsFileName.IsEmpty())
690 return NS_ERROR_FILE_NOT_FOUND;
692 NS_ConvertASCIItoUTF16 fileName(signonsFileName);
693 if (aReplace)
694 rv = CopyFile(fileName, fileName);
695 else {
696 // Get the password manager, which is the destination for the passwords
697 // being migrated. Also create a new instance of the legacy password
698 // storage component, which we'll use to slurp in the signons from
699 // Seamonkey's signons.txt.
700 nsCOMPtr<nsILoginManager> pwmgr(
701 do_GetService("@mozilla.org/login-manager;1"));
702 nsCOMPtr<nsILoginManagerStorage> importer(
703 do_CreateInstance("@mozilla.org/login-manager/storage/legacy;1"));
705 nsCOMPtr<nsIFile> signonsFile(do_QueryInterface(mSourceProfile));
706 signonsFile->SetLeafName(fileName);
708 importer->InitWithFile(signonsFile, nsnull);
710 PRUint32 count;
711 nsILoginInfo **logins;
713 rv = importer->GetAllLogins(&count, &logins);
714 NS_ENSURE_SUCCESS(rv, rv);
715 for (PRUint32 i = 0; i < count; i++) {
716 pwmgr->AddLogin(logins[i]);
718 NS_FREE_XPCOM_ISUPPORTS_POINTER_ARRAY(count, logins);
720 PRUnichar **hostnames;
721 rv = importer->GetAllDisabledHosts(&count, &hostnames);
722 NS_ENSURE_SUCCESS(rv, rv);
723 for (PRUint32 i = 0; i < count; i++) {
724 pwmgr->SetLoginSavingEnabled(nsDependentString(hostnames[i]),
725 PR_FALSE);
727 NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(count, hostnames);
729 return rv;
732 nsresult
733 nsSeamonkeyProfileMigrator::CopyBookmarks(PRBool aReplace)
735 if (aReplace) {
736 // Initialize the default bookmarks
737 nsresult rv = InitializeBookmarks(mTargetProfile);
738 NS_ENSURE_SUCCESS(rv, rv);
740 // Merge in the bookmarks from the source profile
741 nsCOMPtr<nsIFile> sourceFile;
742 mSourceProfile->Clone(getter_AddRefs(sourceFile));
743 sourceFile->Append(FILE_NAME_BOOKMARKS);
744 rv = ImportBookmarksHTML(sourceFile, PR_TRUE, PR_FALSE, EmptyString().get());
745 NS_ENSURE_SUCCESS(rv, rv);
747 // we need to set this pref so that on startup
748 // we don't blow away what we just imported
749 nsCOMPtr<nsIPrefBranch> pref(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
750 NS_ENSURE_SUCCESS(rv, rv);
752 return pref->SetBoolPref("browser.places.importBookmarksHTML", PR_FALSE);
755 return ImportNetscapeBookmarks(FILE_NAME_BOOKMARKS,
756 NS_LITERAL_STRING("sourceNameSeamonkey").get());
759 nsresult
760 nsSeamonkeyProfileMigrator::CopyOtherData(PRBool aReplace)
762 return aReplace ? CopyFile(FILE_NAME_DOWNLOADS, FILE_NAME_DOWNLOADS) : NS_OK;