38 #include "nsAppDirectoryServiceDefs.h"
41 #include "nsICookieManager2.h"
43 #include "nsIInputStream.h"
44 #include "nsILineInputStream.h"
45 #include "nsIObserverService.h"
46 #include "nsIOutputStream.h"
47 #include "nsIPrefBranch.h"
48 #include "nsIPrefLocalizedString.h"
49 #include "nsIPrefService.h"
50 #include "nsIServiceManager.h"
51 #include "nsISupportsArray.h"
52 #include "nsISupportsPrimitives.h"
54 #include "nsNetUtil.h"
58 #include "nsDirectoryServiceDefs.h"
59 #include "nsDirectoryServiceUtils.h"
65 #define MAXPATHLEN PATH_MAX
66 #elif defined(_MAX_PATH)
67 #define MAXPATHLEN _MAX_PATH
68 #elif defined(CCHMAXPATH)
69 #define MAXPATHLEN CCHMAXPATH
71 #define MAXPATHLEN 1024
75 #define PREF_FILE_HEADER_STRING "# Mozilla User Preferences "
77 #if defined(XP_MACOSX)
78 #define OLDREG_NAME "Netscape Registry"
79 #define OLDREG_DIR NS_MAC_PREFS_DIR
80 #define PREF_FILE_NAME_IN_4x NS_LITERAL_STRING("Netscape Preferences")
81 #define COOKIES_FILE_NAME_IN_4x NS_LITERAL_STRING("MagicCookie")
82 #define BOOKMARKS_FILE_NAME_IN_4x NS_LITERAL_STRING("Bookmarks.html")
83 #define SECURITY_PATH "Security"
84 #define PSM_CERT7_DB NS_LITERAL_STRING("Certificates7")
85 #define PSM_KEY3_DB NS_LITERAL_STRING("Key Database3")
86 #define PSM_SECMODULE_DB NS_LITERAL_STRING("Security Modules")
88 #elif defined(XP_WIN) || defined(XP_OS2)
89 #define OLDREG_NAME "nsreg.dat"
91 #define OLDREG_DIR NS_WIN_WINDOWS_DIR
93 #define OLDREG_DIR NS_OS2_DIR
95 #define PREF_FILE_NAME_IN_4x NS_LITERAL_STRING("prefs.js")
96 #define COOKIES_FILE_NAME_IN_4x NS_LITERAL_STRING("cookies.txt")
97 #define BOOKMARKS_FILE_NAME_IN_4x NS_LITERAL_STRING("bookmark.htm")
98 #define PSM_CERT7_DB NS_LITERAL_STRING("cert7.db")
99 #define PSM_KEY3_DB NS_LITERAL_STRING("key3.db")
100 #define PSM_SECMODULE_DB NS_LITERAL_STRING("secmod.db")
102 #elif defined(XP_UNIX)
103 #define PREF_FILE_NAME_IN_4x NS_LITERAL_STRING("preferences.js")
104 #define COOKIES_FILE_NAME_IN_4x NS_LITERAL_STRING("cookies")
105 #define BOOKMARKS_FILE_NAME_IN_4x NS_LITERAL_STRING("bookmarks.html")
106 #define PSM_CERT7_DB NS_LITERAL_STRING("cert7.db")
107 #define PSM_KEY3_DB NS_LITERAL_STRING("key3.db")
108 #define PSM_SECMODULE_DB NS_LITERAL_STRING("secmodule.db")
109 #define HOME_ENVIRONMENT_VARIABLE "HOME"
110 #define PROFILE_HOME_ENVIRONMENT_VARIABLE "PROFILE_HOME"
111 #define DEFAULT_UNIX_PROFILE_NAME "default"
113 #error No netscape4.x profile-migrator on this platform.
116 #define COOKIES_FILE_NAME_IN_5x NS_LITERAL_STRING("cookies.txt")
117 #define BOOKMARKS_FILE_NAME_IN_5x NS_LITERAL_STRING("bookmarks.html")
118 #define PREF_FILE_NAME_IN_5x NS_LITERAL_STRING("prefs.js")
127 mObserverService = do_GetService(
"@mozilla.org/observer-service;1");
138 nsDogbertProfileMigrator::Migrate(PRUint16 aItems, nsIProfileStartup* aStartup,
139 const PRUnichar* aProfile)
142 PRBool aReplace = aStartup ? PR_TRUE : PR_FALSE;
152 return NS_ERROR_FAILURE;
167 #if defined(XP_WIN) || defined(XP_MACOSX) || defined(XP_OS2)
173 nsCOMPtr<nsIFile> regFile;
174 rv = NS_GetSpecialDirectory(OLDREG_DIR, getter_AddRefs(regFile));
175 if (NS_FAILED(rv))
return;
177 regFile->AppendNative(NS_LITERAL_CSTRING(OLDREG_NAME));
180 rv = regFile->GetNativePath(path);
181 if (NS_FAILED(rv))
return;
183 if (NR_StartupRegistry())
187 RKEY profile = nsnull;
189 if (NR_RegOpen(path.get(), ®))
194 NS_ConvertUTF16toUTF8 profileName(aProfile);
196 if (NR_RegGetKey(reg, ROOTKEY_USERS, profileName.get(), &profile))
201 if (NR_RegGetEntryString(reg, profile,
"ProfileLocation", profilePath,
MAXPATHLEN))
209 rv =
mSourceProfile->InitWithPath(NS_ConvertUTF8toUTF16(profilePath));
218 NR_ShutdownRegistry();
225 nsDogbertProfileMigrator::GetMigrateData(
const PRUnichar* aProfile,
233 return NS_ERROR_FILE_NOT_FOUND;
239 { ToNewUnicode(COOKIES_FILE_NAME_IN_4x),
242 { ToNewUnicode(BOOKMARKS_FILE_NAME_IN_4x),
254 nsDogbertProfileMigrator::GetSourceExists(PRBool* aResult)
256 nsCOMPtr<nsISupportsArray> profiles;
257 GetSourceProfiles(getter_AddRefs(profiles));
261 profiles->Count(&count);
262 *aResult = count > 0;
271 nsDogbertProfileMigrator::GetSourceHasMultipleProfiles(PRBool* aResult)
273 nsCOMPtr<nsISupportsArray> profiles;
274 GetSourceProfiles(getter_AddRefs(profiles));
278 profiles->Count(&count);
279 *aResult = count > 1;
287 #if defined(XP_WIN) || defined(XP_OS2) || defined(XP_MACOSX)
289 nsDogbertProfileMigrator::GetSourceProfiles(nsISupportsArray** aResult)
294 mProfiles = do_CreateInstance(NS_SUPPORTSARRAY_CONTRACTID, &rv);
295 NS_ENSURE_SUCCESS(rv, rv);
297 nsCOMPtr<nsIFile> regFile;
298 rv = NS_GetSpecialDirectory(OLDREG_DIR, getter_AddRefs(regFile));
299 NS_ENSURE_SUCCESS(rv, rv);
300 regFile->AppendNative(NS_LITERAL_CSTRING(OLDREG_NAME));
303 rv = regFile->GetNativePath(path);
304 NS_ENSURE_SUCCESS(rv, rv);
306 if (NR_StartupRegistry())
307 return NS_ERROR_FAILURE;
310 REGENUM enumstate = 0;
312 if (NR_RegOpen(path.get(), ®)) {
313 NR_ShutdownRegistry();
314 return NS_ERROR_FAILURE;
317 char profileName[MAXREGNAMELEN];
318 while (!NR_RegEnumSubkeys(reg, ROOTKEY_USERS, &enumstate,
319 profileName, MAXREGNAMELEN, REGENUM_CHILDREN)) {
320 nsCOMPtr<nsISupportsString> nameString
321 (do_CreateInstance(
"@mozilla.org/supports-string;1"));
323 nameString->SetData(NS_ConvertUTF8toUTF16(profileName));
324 mProfiles->AppendElement(nameString);
329 NS_IF_ADDREF(*aResult = mProfiles);
335 nsDogbertProfileMigrator::GetSourceProfiles(nsISupportsArray** aResult)
338 const char*
profileDir = PR_GetEnv(PROFILE_HOME_ENVIRONMENT_VARIABLE);
341 profileDir = PR_GetEnv(HOME_ENVIRONMENT_VARIABLE);
343 if (!profileDir)
return NS_ERROR_FAILURE;
345 nsCAutoString profilePath(profileDir);
346 profilePath +=
"/.netscape";
348 nsCOMPtr<nsILocalFile> profileFile;
349 rv = NS_NewNativeLocalFile(profilePath, PR_TRUE, getter_AddRefs(profileFile));
350 NS_ENSURE_SUCCESS(rv, rv);
352 nsCOMPtr<nsIFile> prefFile;
353 rv = profileFile->Clone(getter_AddRefs(prefFile));
354 NS_ENSURE_SUCCESS(rv, rv);
356 prefFile->AppendNative(NS_LITERAL_CSTRING(
"preferences.js"));
359 rv = prefFile->Exists(&exists);
360 if (NS_FAILED(rv) || !exists) {
361 return NS_ERROR_FAILURE;
366 mProfiles = do_CreateInstance(NS_SUPPORTSARRAY_CONTRACTID, &rv);
367 NS_ENSURE_SUCCESS(rv, rv);
369 nsCOMPtr<nsISupportsString> nameString
370 (do_CreateInstance(
"@mozilla.org/supports-string;1"));
371 if (!nameString)
return NS_ERROR_FAILURE;
373 nameString->SetData(NS_LITERAL_STRING(
"Netscape 4.x"));
374 mProfiles->AppendElement(nameString);
375 NS_ADDREF(*aResult = mProfiles);
384 #endif // GetSourceProfiles
387 nsDogbertProfileMigrator::GetSourceHomePageURL(nsACString& aResult)
394 #define F(a) nsDogbertProfileMigrator::a
399 {
"browser.anchor_color", 0,
F(GetString),
F(SetString), PR_FALSE, { -1 } },
400 {
"browser.visited_color", 0,
F(GetString),
F(SetString), PR_FALSE, { -1 } },
401 {
"browser.startup.homepage", 0,
F(GetString),
F(SetString), PR_FALSE, { -1 } },
402 {
"network.cookie.cookieBehavior", 0,
F(GetInt),
F(SetInt), PR_FALSE, { -1 } },
403 {
"network.cookie.warnAboutCookies",0,
F(GetBool),
F(SetBool), PR_FALSE, { -1 } },
404 {
"javascript.enabled", 0,
F(GetBool),
F(SetBool), PR_FALSE, { -1 } },
405 {
"network.proxy.type", 0,
F(GetInt),
F(SetInt), PR_FALSE, { -1 } },
406 {
"network.proxy.no_proxies_on", 0,
F(GetString),
F(SetString), PR_FALSE, { -1 } },
407 {
"network.proxy.autoconfig_url", 0,
F(GetString),
F(SetString), PR_FALSE, { -1 } },
408 {
"network.proxy.ftp", 0,
F(GetString),
F(SetString), PR_FALSE, { -1 } },
409 {
"network.proxy.ftp_port", 0,
F(GetInt),
F(SetInt), PR_FALSE, { -1 } },
410 {
"network.proxy.gopher", 0,
F(GetString),
F(SetString), PR_FALSE, { -1 } },
411 {
"network.proxy.gopher_port", 0,
F(GetInt),
F(SetInt), PR_FALSE, { -1 } },
412 {
"network.proxy.http", 0,
F(GetString),
F(SetString), PR_FALSE, { -1 } },
413 {
"network.proxy.http_port", 0,
F(GetInt),
F(SetInt), PR_FALSE, { -1 } },
414 {
"network.proxy.ssl", 0,
F(GetString),
F(SetString), PR_FALSE, { -1 } },
415 {
"network.proxy.ssl_port", 0,
F(GetInt),
F(SetInt), PR_FALSE, { -1 } },
418 {
"network.hosts.socks_server",
"network.proxy.socks",
F(GetString),
F(SetString), PR_FALSE, { -1 } },
419 {
"network.hosts.socks_serverport",
"network.proxy.socks_port",
F(GetInt),
F(SetInt), PR_FALSE, { -1 } },
420 {
"browser.background_color",
"browser.display.background_color",
F(GetString),
F(SetString), PR_FALSE, { -1 } },
421 {
"browser.foreground_color",
"browser.display.foreground_color",
F(GetString),
F(SetString), PR_FALSE, { -1 } },
422 {
"browser.wfe.use_windows_colors",
"browser.display.use_system_colors",
F(GetBool),
F(SetBool), PR_FALSE, { -1 } },
423 {
"browser.use_document_colors",
"browser.display.use_document_colors",
F(GetBool),
F(SetBool), PR_FALSE, { -1 } },
424 {
"browser.use_document.fonts",
"browser.display.use_document_fonts",
F(GetInt),
F(SetInt), PR_FALSE, { -1 } },
425 {
"browser.link_expiration",
"browser.history_expire_days",
F(GetInt),
F(SetInt), PR_FALSE, { -1 } },
426 {
"browser.startup.page",
"browser.startup.homepage",
F(GetHomepage),
F(SetWStringFromASCII), PR_FALSE, { -1 } },
427 {
"general.always_load_images",
"permissions.default.image",
F(GetImagePref),
F(SetInt), PR_FALSE, { -1 } },
432 const nsAString& aTargetPrefFileName)
441 nsCOMPtr<nsIFile> sourcePrefsFile;
443 sourcePrefsFile->Append(aSourcePrefFileName);
444 psvc->ReadUserPrefs(sourcePrefsFile);
446 nsCOMPtr<nsIPrefBranch> branch(do_QueryInterface(psvc));
447 for (transform = gTransforms; transform < end; ++transform)
453 for (transform = gTransforms; transform < end; ++transform)
456 nsCOMPtr<nsIFile> targetPrefsFile;
458 targetPrefsFile->Append(aTargetPrefFileName);
459 psvc->SavePrefFile(targetPrefsFile);
476 rv |=
CopyFile(PSM_CERT7_DB, PSM_CERT7_DB);
477 rv |=
CopyFile(PSM_KEY3_DB, PSM_KEY3_DB);
478 rv |=
CopyFile(PSM_SECMODULE_DB, PSM_SECMODULE_DB);
489 if (NS_SUCCEEDED(rv) && val == 0) {
501 nsresult rv = aBranch->GetBoolPref(xform->
sourcePrefName, &loadImages);
502 if (NS_SUCCEEDED(rv)) {
503 xform->
intValue = loadImages ? 1 : 2;
514 #ifdef NEED_TO_FIX_4X_COOKIES
516 if (NS_FAILED(rv))
return rv;
518 rv = FixDogbertCookies();
524 nsCOMPtr<nsICookieManager2>
cookieManager(do_GetService(NS_COOKIEMANAGER_CONTRACTID));
526 return NS_ERROR_OUT_OF_MEMORY;
528 nsCOMPtr<nsIFile> dogbertCookiesFile;
530 dogbertCookiesFile->Append(COOKIES_FILE_NAME_IN_4x);
537 #if NEED_TO_FIX_4X_COOKIES
539 nsDogbertProfileMigrator::FixDogbertCookies()
541 nsCOMPtr<nsIFile> dogbertCookiesFile;
543 dogbertCookiesFile->Append(COOKIES_FILE_NAME_IN_4x);
545 nsCOMPtr<nsIInputStream> fileInputStream;
546 NS_NewLocalFileInputStream(getter_AddRefs(fileInputStream), dogbertCookiesFile);
547 if (!fileInputStream)
return NS_ERROR_OUT_OF_MEMORY;
549 nsCOMPtr<nsIFile> firebirdCookiesFile;
553 nsCOMPtr<nsIOutputStream> fileOutputStream;
554 NS_NewLocalFileOutputStream(getter_AddRefs(fileOutputStream), firebirdCookiesFile);
555 if (!fileOutputStream)
return NS_ERROR_OUT_OF_MEMORY;
557 nsCOMPtr<nsILineInputStream> lineInputStream(do_QueryInterface(fileInputStream));
558 nsCAutoString buffer, outBuffer;
559 PRBool moreData = PR_FALSE;
560 PRUint32 written = 0;
562 nsresult rv = lineInputStream->ReadLine(buffer, &moreData);
563 if (NS_FAILED(rv))
return rv;
569 if (buffer.IsEmpty() || buffer.CharAt(0) ==
'#' ||
570 buffer.CharAt(0) ==
'\r' || buffer.CharAt(0) ==
'\n') {
571 fileOutputStream->Write(buffer.get(), buffer.Length(), &written);
576 int hostIndex, isDomainIndex, pathIndex, xxxIndex, expiresIndex, nameIndex, cookieIndex;
578 if ((isDomainIndex = buffer.FindChar(
'\t', hostIndex)+1) == 0 ||
579 (pathIndex = buffer.FindChar(
'\t', isDomainIndex)+1) == 0 ||
580 (xxxIndex = buffer.FindChar(
'\t', pathIndex)+1) == 0 ||
581 (expiresIndex = buffer.FindChar(
'\t', xxxIndex)+1) == 0 ||
582 (nameIndex = buffer.FindChar(
'\t', expiresIndex)+1) == 0 ||
583 (cookieIndex = buffer.FindChar(
'\t', nameIndex)+1) == 0 )
587 const nsDependentCSubstring prefix =
588 Substring(buffer, hostIndex, expiresIndex-hostIndex-1);
589 const nsDependentCSubstring expiresString =
590 Substring(buffer, expiresIndex, nameIndex-expiresIndex-1);
591 const nsDependentCSubstring suffix =
592 Substring(buffer, nameIndex, buffer.Length()-nameIndex);
595 char* expiresCString = ToNewCString(expiresString);
596 unsigned long expires = strtoul(expiresCString, nsnull, 10);
597 NS_Free(expiresCString);
602 expires -= SECONDS_BETWEEN_1900_AND_1970;
604 PR_snprintf(dateString,
sizeof(dateString),
"%lu", expires);
608 outBuffer.Append(
'\t');
609 outBuffer.Append(dateString);
610 outBuffer.Append(
'\t');
611 outBuffer.Append(suffix);
613 fileOutputStream->Write(outBuffer.get(), outBuffer.Length(), &written);
620 #endif // NEED_TO_FIX_4X_COOKIES
629 NS_ENSURE_SUCCESS(rv, rv);
634 NS_LITERAL_STRING(
"sourceNameDogbert").
get());
647 nsCOMPtr<nsIFile> dogbertPrefsFile;
649 dogbertPrefsFile->Append(PREF_FILE_NAME_IN_4x);
650 psvc->ReadUserPrefs(dogbertPrefsFile);
652 nsCString toolbarName;
653 nsCOMPtr<nsIPrefBranch> branch(do_QueryInterface(psvc));
654 rv = branch->GetCharPref(
"custtoolbar.personal_toolbar_folder", getter_Copies(toolbarName));
663 nsCOMPtr<nsIFile> sourceBookmarksFile;
665 sourceBookmarksFile->Append(BOOKMARKS_FILE_NAME_IN_4x);
667 nsCOMPtr<nsIFile> targetBookmarksFile;
672 targetBookmarksFile, toolbarName.get());
nsresult CopyFile(const nsAString &aSourceFileName, const nsAString &aTargetFileName)
nsresult ImportNetscapeBookmarks(const nsAString &aBookmarksFileName, const PRUnichar *aImportSourceNameKey)
nsresult InitializeBookmarks(nsIFile *aTargetProfile)
nsresult CopyBookmarks(PRBool aReplace)
NS_IMPL_ISUPPORTS1(sbDeviceCapabilitiesUtils, sbIDeviceCapabilitiesUtils) sbDeviceCapabilitiesUtils
static nsresult GetImagePref(void *aTransform, nsIPrefBranch *aBranch)
const NS_PREFSERVICE_CONTRACTID
nsresult AnnotatePersonalToolbarFolder(nsIFile *aSourceBookmarksFile, nsIFile *aTargetBookmarksFile, const char *aToolbarFolderName)
const unsigned short SETTINGS
void GetSourceProfile(const PRUnichar *aProfile)
#define NOTIFY_OBSERVERS(message, item)
nsresult CopyPreferences(PRBool aReplace)
nsCOMPtr< nsIFile > mTargetProfile
nsresult MigrateDogbertBookmarks()
#define MIGRATION_STARTED
#define COPY_DATA(func, replace, itemIndex)
this _dialogInput val(dateText)
nsresult CopyCookies(PRBool aReplace)
#define PREF_FILE_NAME_IN_5x
const unsigned short COOKIES
nsresult TransformPreferences(const nsAString &aSourcePrefFileName, const nsAString &aTargetPrefFileName)
const unsigned short BOOKMARKS
static nsresult GetHomepage(void *aTransform, nsIPrefBranch *aBranch)
#define COOKIES_FILE_NAME_IN_5x
virtual ~nsDogbertProfileMigrator()
void GetProfilePath(nsIProfileStartup *aStartup, nsCOMPtr< nsIFile > &aProfileDir)
#define BOOKMARKS_FILE_NAME_IN_5x
nsresult ImportNetscapeCookies(nsIFile *aCookiesFile)
nsCOMPtr< nsILocalFile > mSourceProfile
static nsDogbertProfileMigrator::PrefTransform gTransforms[]
void GetMigrateDataFromArray(MigrationData *aDataArray, PRInt32 aDataArrayLength, PRBool aReplace, nsIFile *aSourceProfile, PRUint16 *aResult)