Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / browser / components / migration / src / nsDogbertProfileMigrator.cpp
blob9269e55cddc2b566012c43ff5fa40a2d2ed6bcb8
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 "nsAppDirectoryServiceDefs.h"
39 #include "nsBrowserProfileMigratorUtils.h"
40 #include "nsDogbertProfileMigrator.h"
41 #include "nsICookieManager2.h"
42 #include "nsIFile.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"
53 #include "nsNetCID.h"
54 #include "nsNetUtil.h"
55 #include "prprf.h"
56 #include "prenv.h"
57 #include "NSReg.h"
58 #include "nsDirectoryServiceDefs.h"
59 #include "nsDirectoryServiceUtils.h"
60 #include <stdlib.h>
61 #include <limits.h>
63 #ifndef MAXPATHLEN
64 #ifdef PATH_MAX
65 #define MAXPATHLEN PATH_MAX
66 #elif defined(_MAX_PATH)
67 #define MAXPATHLEN _MAX_PATH
68 #elif defined(CCHMAXPATH)
69 #define MAXPATHLEN CCHMAXPATH
70 #else
71 #define MAXPATHLEN 1024
72 #endif
73 #endif
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"
90 #ifdef XP_WIN
91 #define OLDREG_DIR NS_WIN_WINDOWS_DIR
92 #else
93 #define OLDREG_DIR NS_OS2_DIR
94 #endif
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"
112 #else
113 #error No netscape4.x profile-migrator on this platform.
114 #endif /* XP_UNIX */
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")
120 ///////////////////////////////////////////////////////////////////////////////
121 // nsDogbertProfileMigrator
123 NS_IMPL_ISUPPORTS1(nsDogbertProfileMigrator, nsIBrowserProfileMigrator)
125 nsDogbertProfileMigrator::nsDogbertProfileMigrator()
127 mObserverService = do_GetService("@mozilla.org/observer-service;1");
130 nsDogbertProfileMigrator::~nsDogbertProfileMigrator()
134 ///////////////////////////////////////////////////////////////////////////////
135 // nsIBrowserProfileMigrator
137 NS_IMETHODIMP
138 nsDogbertProfileMigrator::Migrate(PRUint16 aItems, nsIProfileStartup* aStartup,
139 const PRUnichar* aProfile)
141 nsresult rv = NS_OK;
142 PRBool aReplace = aStartup ? PR_TRUE : PR_FALSE;
144 if (!mTargetProfile) {
145 GetProfilePath(aStartup, mTargetProfile);
146 if (!mTargetProfile) return NS_ERROR_FAILURE;
149 if (!mSourceProfile) {
150 GetSourceProfile(aProfile);
151 if (!mSourceProfile)
152 return NS_ERROR_FAILURE;
155 NOTIFY_OBSERVERS(MIGRATION_STARTED, nsnull);
157 COPY_DATA(CopyPreferences, aReplace, nsIBrowserProfileMigrator::SETTINGS);
158 COPY_DATA(CopyCookies, aReplace, nsIBrowserProfileMigrator::COOKIES);
159 COPY_DATA(CopyBookmarks, aReplace, nsIBrowserProfileMigrator::BOOKMARKS);
161 NOTIFY_OBSERVERS(MIGRATION_ENDED, nsnull);
163 return rv;
166 // on win/mac/os2, NS4x uses a registry to determine profile locations
167 #if defined(XP_WIN) || defined(XP_MACOSX) || defined(XP_OS2)
168 void
169 nsDogbertProfileMigrator::GetSourceProfile(const PRUnichar* aProfile)
171 nsresult rv;
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));
179 nsCAutoString path;
180 rv = regFile->GetNativePath(path);
181 if (NS_FAILED(rv)) return;
183 if (NR_StartupRegistry())
184 return;
186 HREG reg = nsnull;
187 RKEY profile = nsnull;
189 if (NR_RegOpen(path.get(), &reg))
190 goto cleanup;
193 // on macos, registry entries are UTF8 encoded
194 NS_ConvertUTF16toUTF8 profileName(aProfile);
196 if (NR_RegGetKey(reg, ROOTKEY_USERS, profileName.get(), &profile))
197 goto cleanup;
200 char profilePath[MAXPATHLEN];
201 if (NR_RegGetEntryString(reg, profile, "ProfileLocation", profilePath, MAXPATHLEN))
202 goto cleanup;
204 mSourceProfile = do_CreateInstance("@mozilla.org/file/local;1");
205 if (!mSourceProfile) goto cleanup;
208 // the string is UTF8 encoded, which forces us to do some strange string-do
209 rv = mSourceProfile->InitWithPath(NS_ConvertUTF8toUTF16(profilePath));
212 if (NS_FAILED(rv))
213 mSourceProfile = nsnull;
215 cleanup:
216 if (reg)
217 NR_RegClose(reg);
218 NR_ShutdownRegistry();
220 #else
222 #endif
224 NS_IMETHODIMP
225 nsDogbertProfileMigrator::GetMigrateData(const PRUnichar* aProfile,
226 PRBool aReplace,
227 PRUint16* aResult)
229 *aResult = 0;
230 if (!mSourceProfile) {
231 GetSourceProfile(aProfile);
232 if (!mSourceProfile)
233 return NS_ERROR_FILE_NOT_FOUND;
236 MigrationData data[] = { { ToNewUnicode(PREF_FILE_NAME_IN_4x),
237 nsIBrowserProfileMigrator::SETTINGS,
238 PR_TRUE },
239 { ToNewUnicode(COOKIES_FILE_NAME_IN_4x),
240 nsIBrowserProfileMigrator::COOKIES,
241 PR_FALSE },
242 { ToNewUnicode(BOOKMARKS_FILE_NAME_IN_4x),
243 nsIBrowserProfileMigrator::BOOKMARKS,
244 PR_FALSE } };
246 // Frees file name strings allocated above.
247 GetMigrateDataFromArray(data, sizeof(data)/sizeof(MigrationData),
248 aReplace, mSourceProfile, aResult);
250 return NS_OK;
253 NS_IMETHODIMP
254 nsDogbertProfileMigrator::GetSourceExists(PRBool* aResult)
256 nsCOMPtr<nsISupportsArray> profiles;
257 GetSourceProfiles(getter_AddRefs(profiles));
259 if (profiles) {
260 PRUint32 count;
261 profiles->Count(&count);
262 *aResult = count > 0;
264 else
265 *aResult = PR_FALSE;
267 return NS_OK;
270 NS_IMETHODIMP
271 nsDogbertProfileMigrator::GetSourceHasMultipleProfiles(PRBool* aResult)
273 nsCOMPtr<nsISupportsArray> profiles;
274 GetSourceProfiles(getter_AddRefs(profiles));
276 if (profiles) {
277 PRUint32 count;
278 profiles->Count(&count);
279 *aResult = count > 1;
281 else
282 *aResult = PR_FALSE;
284 return NS_OK;
287 #if defined(XP_WIN) || defined(XP_OS2) || defined(XP_MACOSX)
288 NS_IMETHODIMP
289 nsDogbertProfileMigrator::GetSourceProfiles(nsISupportsArray** aResult)
291 if (!mProfiles) {
292 nsresult rv;
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));
302 nsCAutoString path;
303 rv = regFile->GetNativePath(path);
304 NS_ENSURE_SUCCESS(rv, rv);
306 if (NR_StartupRegistry())
307 return NS_ERROR_FAILURE;
309 HREG reg = nsnull;
310 REGENUM enumstate = 0;
312 if (NR_RegOpen(path.get(), &reg)) {
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"));
322 if (nameString) {
323 nameString->SetData(NS_ConvertUTF8toUTF16(profileName));
324 mProfiles->AppendElement(nameString);
329 NS_IF_ADDREF(*aResult = mProfiles);
330 return NS_OK;
332 #else // XP_UNIX
334 NS_IMETHODIMP
335 nsDogbertProfileMigrator::GetSourceProfiles(nsISupportsArray** aResult)
337 nsresult rv;
338 const char* profileDir = PR_GetEnv(PROFILE_HOME_ENVIRONMENT_VARIABLE);
340 if (!profileDir) {
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"));
358 PRBool exists;
359 rv = prefFile->Exists(&exists);
360 if (NS_FAILED(rv) || !exists) {
361 return NS_ERROR_FAILURE;
364 mSourceProfile = profileFile;
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);
376 return NS_OK;
379 void
380 nsDogbertProfileMigrator::GetSourceProfile(const PRUnichar* aProfile)
382 // if GetSourceProfiles didn't do its magic, we're screwed
384 #endif // GetSourceProfiles
386 NS_IMETHODIMP
387 nsDogbertProfileMigrator::GetSourceHomePageURL(nsACString& aResult)
389 aResult.Truncate();
390 return NS_OK;
392 ///////////////////////////////////////////////////////////////////////////////
393 // nsDogbertProfileMigrator
394 #define F(a) nsDogbertProfileMigrator::a
396 static
397 nsDogbertProfileMigrator::PrefTransform gTransforms[] = {
398 // Simple Copy Prefs
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 { "security.enable_java", 0, F(GetBool), F(SetBool), PR_FALSE, -1 },
403 { "network.cookie.cookieBehavior", 0, F(GetInt), F(SetInt), PR_FALSE, -1 },
404 { "network.cookie.warnAboutCookies",0, F(GetBool), F(SetBool), PR_FALSE, -1 },
405 { "javascript.enabled", 0, F(GetBool), F(SetBool), PR_FALSE, -1 },
406 { "network.proxy.type", 0, F(GetInt), F(SetInt), PR_FALSE, -1 },
407 { "network.proxy.no_proxies_on", 0, F(GetString), F(SetString), PR_FALSE, -1 },
408 { "network.proxy.autoconfig_url", 0, F(GetString), F(SetString), PR_FALSE, -1 },
409 { "network.proxy.ftp", 0, F(GetString), F(SetString), PR_FALSE, -1 },
410 { "network.proxy.ftp_port", 0, F(GetInt), F(SetInt), PR_FALSE, -1 },
411 { "network.proxy.gopher", 0, F(GetString), F(SetString), PR_FALSE, -1 },
412 { "network.proxy.gopher_port", 0, F(GetInt), F(SetInt), PR_FALSE, -1 },
413 { "network.proxy.http", 0, F(GetString), F(SetString), PR_FALSE, -1 },
414 { "network.proxy.http_port", 0, F(GetInt), F(SetInt), PR_FALSE, -1 },
415 { "network.proxy.ssl", 0, F(GetString), F(SetString), PR_FALSE, -1 },
416 { "network.proxy.ssl_port", 0, F(GetInt), F(SetInt), PR_FALSE, -1 },
418 // Prefs with Different Names
419 { "network.hosts.socks_server", "network.proxy.socks", F(GetString), F(SetString), PR_FALSE, -1 },
420 { "network.hosts.socks_serverport", "network.proxy.socks_port", F(GetInt), F(SetInt), PR_FALSE, -1 },
421 { "browser.background_color", "browser.display.background_color", F(GetString), F(SetString), PR_FALSE, -1 },
422 { "browser.foreground_color", "browser.display.foreground_color", F(GetString), F(SetString), PR_FALSE, -1 },
423 { "browser.wfe.use_windows_colors", "browser.display.use_system_colors", F(GetBool), F(SetBool), PR_FALSE, -1 },
424 { "browser.use_document_colors", "browser.display.use_document_colors",F(GetBool), F(SetBool), PR_FALSE, -1 },
425 { "browser.use_document.fonts", "browser.display.use_document_fonts", F(GetInt), F(SetInt), PR_FALSE, -1 },
426 { "browser.link_expiration", "browser.history_expire_days", F(GetInt), F(SetInt), PR_FALSE, -1 },
427 { "browser.startup.page", "browser.startup.homepage", F(GetHomepage), F(SetWStringFromASCII), PR_FALSE, -1 },
428 { "general.always_load_images", "permissions.default.image", F(GetImagePref),F(SetInt), PR_FALSE, -1 },
431 nsresult
432 nsDogbertProfileMigrator::TransformPreferences(const nsAString& aSourcePrefFileName,
433 const nsAString& aTargetPrefFileName)
435 PrefTransform* transform;
436 PrefTransform* end = gTransforms + sizeof(gTransforms)/sizeof(PrefTransform);
438 // Load the source pref file
439 nsCOMPtr<nsIPrefService> psvc(do_GetService(NS_PREFSERVICE_CONTRACTID));
440 psvc->ResetPrefs();
442 nsCOMPtr<nsIFile> sourcePrefsFile;
443 mSourceProfile->Clone(getter_AddRefs(sourcePrefsFile));
444 sourcePrefsFile->Append(aSourcePrefFileName);
445 psvc->ReadUserPrefs(sourcePrefsFile);
447 nsCOMPtr<nsIPrefBranch> branch(do_QueryInterface(psvc));
448 for (transform = gTransforms; transform < end; ++transform)
449 transform->prefGetterFunc(transform, branch);
451 // Now that we have all the pref data in memory, load the target pref file,
452 // and write it back out
453 psvc->ResetPrefs();
454 for (transform = gTransforms; transform < end; ++transform)
455 transform->prefSetterFunc(transform, branch);
457 nsCOMPtr<nsIFile> targetPrefsFile;
458 mTargetProfile->Clone(getter_AddRefs(targetPrefsFile));
459 targetPrefsFile->Append(aTargetPrefFileName);
460 psvc->SavePrefFile(targetPrefsFile);
462 return NS_OK;
465 nsresult
466 nsDogbertProfileMigrator::CopyPreferences(PRBool aReplace)
468 nsresult rv = NS_OK;
470 if (!aReplace)
471 return rv;
473 // 1) Copy Preferences
474 TransformPreferences(PREF_FILE_NAME_IN_4x, PREF_FILE_NAME_IN_5x);
476 // 2) Copy Certficates
477 rv |= CopyFile(PSM_CERT7_DB, PSM_CERT7_DB);
478 rv |= CopyFile(PSM_KEY3_DB, PSM_KEY3_DB);
479 rv |= CopyFile(PSM_SECMODULE_DB, PSM_SECMODULE_DB);
481 return rv;
484 nsresult
485 nsDogbertProfileMigrator::GetHomepage(void* aTransform, nsIPrefBranch* aBranch)
487 PrefTransform* xform = (PrefTransform*)aTransform;
488 PRInt32 val;
489 nsresult rv = aBranch->GetIntPref(xform->sourcePrefName, &val);
490 if (NS_SUCCEEDED(rv) && val == 0) {
491 xform->stringValue = "about:blank";
492 xform->prefHasValue = PR_TRUE;
494 return rv;
497 nsresult
498 nsDogbertProfileMigrator::GetImagePref(void* aTransform, nsIPrefBranch* aBranch)
500 PrefTransform* xform = (PrefTransform*)aTransform;
501 PRBool loadImages;
502 nsresult rv = aBranch->GetBoolPref(xform->sourcePrefName, &loadImages);
503 if (NS_SUCCEEDED(rv)) {
504 xform->intValue = loadImages ? 1 : 2;
505 xform->prefHasValue = PR_TRUE;
507 return rv;
510 nsresult
511 nsDogbertProfileMigrator::CopyCookies(PRBool aReplace)
513 nsresult rv;
514 if (aReplace) {
515 #ifdef NEED_TO_FIX_4X_COOKIES
516 rv = CopyFile(COOKIES_FILE_NAME_IN_4x, COOKIES_FILE_NAME_IN_5x);
517 if (NS_FAILED(rv)) return rv;
519 rv = FixDogbertCookies();
520 #else
521 rv = CopyFile(COOKIES_FILE_NAME_IN_4x, COOKIES_FILE_NAME_IN_5x);
522 #endif
524 else {
525 nsCOMPtr<nsICookieManager2> cookieManager(do_GetService(NS_COOKIEMANAGER_CONTRACTID));
526 if (!cookieManager)
527 return NS_ERROR_OUT_OF_MEMORY;
529 nsCOMPtr<nsIFile> dogbertCookiesFile;
530 mSourceProfile->Clone(getter_AddRefs(dogbertCookiesFile));
531 dogbertCookiesFile->Append(COOKIES_FILE_NAME_IN_4x);
533 rv = ImportNetscapeCookies(dogbertCookiesFile);
535 return rv;
538 #if NEED_TO_FIX_4X_COOKIES
539 nsresult
540 nsDogbertProfileMigrator::FixDogbertCookies()
542 nsCOMPtr<nsIFile> dogbertCookiesFile;
543 mSourceProfile->Clone(getter_AddRefs(dogbertCookiesFile));
544 dogbertCookiesFile->Append(COOKIES_FILE_NAME_IN_4x);
546 nsCOMPtr<nsIInputStream> fileInputStream;
547 NS_NewLocalFileInputStream(getter_AddRefs(fileInputStream), dogbertCookiesFile);
548 if (!fileInputStream) return NS_ERROR_OUT_OF_MEMORY;
550 nsCOMPtr<nsIFile> firebirdCookiesFile;
551 mTargetProfile->Clone(getter_AddRefs(firebirdCookiesFile));
552 firebirdCookiesFile->Append(COOKIES_FILE_NAME_IN_5x);
554 nsCOMPtr<nsIOutputStream> fileOutputStream;
555 NS_NewLocalFileOutputStream(getter_AddRefs(fileOutputStream), firebirdCookiesFile);
556 if (!fileOutputStream) return NS_ERROR_OUT_OF_MEMORY;
558 nsCOMPtr<nsILineInputStream> lineInputStream(do_QueryInterface(fileInputStream));
559 nsCAutoString buffer, outBuffer;
560 PRBool moreData = PR_FALSE;
561 PRUint32 written = 0;
562 do {
563 nsresult rv = lineInputStream->ReadLine(buffer, &moreData);
564 if (NS_FAILED(rv)) return rv;
566 if (!moreData)
567 break;
569 // skip line if it is a comment or null line
570 if (buffer.IsEmpty() || buffer.CharAt(0) == '#' ||
571 buffer.CharAt(0) == '\r' || buffer.CharAt(0) == '\n') {
572 fileOutputStream->Write(buffer.get(), buffer.Length(), &written);
573 continue;
576 // locate expire field, skip line if it does not contain all its fields
577 int hostIndex, isDomainIndex, pathIndex, xxxIndex, expiresIndex, nameIndex, cookieIndex;
578 hostIndex = 0;
579 if ((isDomainIndex = buffer.FindChar('\t', hostIndex)+1) == 0 ||
580 (pathIndex = buffer.FindChar('\t', isDomainIndex)+1) == 0 ||
581 (xxxIndex = buffer.FindChar('\t', pathIndex)+1) == 0 ||
582 (expiresIndex = buffer.FindChar('\t', xxxIndex)+1) == 0 ||
583 (nameIndex = buffer.FindChar('\t', expiresIndex)+1) == 0 ||
584 (cookieIndex = buffer.FindChar('\t', nameIndex)+1) == 0 )
585 continue;
587 // separate the expires field from the rest of the cookie line
588 const nsDependentCSubstring prefix =
589 Substring(buffer, hostIndex, expiresIndex-hostIndex-1);
590 const nsDependentCSubstring expiresString =
591 Substring(buffer, expiresIndex, nameIndex-expiresIndex-1);
592 const nsDependentCSubstring suffix =
593 Substring(buffer, nameIndex, buffer.Length()-nameIndex);
595 // correct the expires field
596 char* expiresCString = ToNewCString(expiresString);
597 unsigned long expires = strtoul(expiresCString, nsnull, 10);
598 NS_Free(expiresCString);
600 // if the cookie is supposed to expire at the end of the session
601 // expires == 0. don't adjust those cookies.
602 if (expires)
603 expires -= SECONDS_BETWEEN_1900_AND_1970;
604 char dateString[36];
605 PR_snprintf(dateString, sizeof(dateString), "%lu", expires);
607 // generate the output buffer and write it to file
608 outBuffer = prefix;
609 outBuffer.Append('\t');
610 outBuffer.Append(dateString);
611 outBuffer.Append('\t');
612 outBuffer.Append(suffix);
614 fileOutputStream->Write(outBuffer.get(), outBuffer.Length(), &written);
616 while (1);
618 return NS_OK;
621 #endif // NEED_TO_FIX_4X_COOKIES
624 nsresult
625 nsDogbertProfileMigrator::CopyBookmarks(PRBool aReplace)
627 // If we're blowing away existing content, just copy the file, don't do fancy importing.
628 if (aReplace) {
629 nsresult rv = InitializeBookmarks(mTargetProfile);
630 NS_ENSURE_SUCCESS(rv, rv);
631 return MigrateDogbertBookmarks();
634 return ImportNetscapeBookmarks(BOOKMARKS_FILE_NAME_IN_4x,
635 NS_LITERAL_STRING("sourceNameDogbert").get());
638 nsresult
639 nsDogbertProfileMigrator::MigrateDogbertBookmarks()
641 nsresult rv;
643 // Find out what the personal toolbar folder was called, this is stored in a pref
644 // in 4.x
645 nsCOMPtr<nsIPrefService> psvc(do_GetService(NS_PREFSERVICE_CONTRACTID));
646 psvc->ResetPrefs();
648 nsCOMPtr<nsIFile> dogbertPrefsFile;
649 mSourceProfile->Clone(getter_AddRefs(dogbertPrefsFile));
650 dogbertPrefsFile->Append(PREF_FILE_NAME_IN_4x);
651 psvc->ReadUserPrefs(dogbertPrefsFile);
653 nsCString toolbarName;
654 nsCOMPtr<nsIPrefBranch> branch(do_QueryInterface(psvc));
655 rv = branch->GetCharPref("custtoolbar.personal_toolbar_folder", getter_Copies(toolbarName));
656 // If the pref wasn't set in the user's 4.x preferences, there's no way we can "Fix" the
657 // file when importing it to set the personal toolbar folder correctly, so don't bother
658 // with the more involved file correction procedure and just copy the file over.
659 if (NS_FAILED(rv))
660 return CopyFile(BOOKMARKS_FILE_NAME_IN_4x, BOOKMARKS_FILE_NAME_IN_5x);
662 // Now read the 4.x bookmarks file, correcting the Personal Toolbar Folder line
663 // and writing to the new location.
664 nsCOMPtr<nsIFile> sourceBookmarksFile;
665 mSourceProfile->Clone(getter_AddRefs(sourceBookmarksFile));
666 sourceBookmarksFile->Append(BOOKMARKS_FILE_NAME_IN_4x);
668 nsCOMPtr<nsIFile> targetBookmarksFile;
669 mTargetProfile->Clone(getter_AddRefs(targetBookmarksFile));
670 targetBookmarksFile->Append(BOOKMARKS_FILE_NAME_IN_5x);
672 return AnnotatePersonalToolbarFolder(sourceBookmarksFile,
673 targetBookmarksFile, toolbarName.get());