Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / browser / components / migration / src / nsMacIEProfileMigrator.cpp
blobc0625695bc36f6d8c7352465aa235956ce8ad86a
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>
23 * Benjamin Smedberg <benjamin@smedbergs.us>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsDirectoryServiceDefs.h"
40 #include "nsBrowserProfileMigratorUtils.h"
41 #include "nsMacIEProfileMigrator.h"
42 #include "nsILocalFile.h"
43 #include "nsIObserverService.h"
44 #include "nsIProfileMigrator.h"
45 #include "nsIServiceManager.h"
46 #include "nsIStringBundle.h"
47 #include "nsISupportsArray.h"
48 #include "nsISupportsPrimitives.h"
49 #include "nsServiceManagerUtils.h"
50 #include "nsIProperties.h"
51 #include <InternetConfig.h>
53 #define MACIE_BOOKMARKS_FILE_NAME NS_LITERAL_STRING("Favorites.html")
54 #define MACIE_PREFERENCES_FOLDER_NAME NS_LITERAL_STRING("Explorer")
55 #define MACIE_DEFAULT_HOMEPAGE_PREF "\p4D534945„WWWHomePage"
56 #define TEMP_BOOKMARKS_FILE_NAME NS_LITERAL_STRING("bookmarks_tmp.html")
58 #define MIGRATION_BUNDLE "chrome://browser/locale/migration/migration.properties"
60 ///////////////////////////////////////////////////////////////////////////////
61 // nsMacIEProfileMigrator
63 NS_IMPL_ISUPPORTS1(nsMacIEProfileMigrator, nsIBrowserProfileMigrator)
65 nsMacIEProfileMigrator::nsMacIEProfileMigrator()
67 mObserverService = do_GetService("@mozilla.org/observer-service;1");
70 nsMacIEProfileMigrator::~nsMacIEProfileMigrator()
74 ///////////////////////////////////////////////////////////////////////////////
75 // nsIBrowserProfileMigrator
77 NS_IMETHODIMP
78 nsMacIEProfileMigrator::Migrate(PRUint16 aItems, nsIProfileStartup* aStartup, const PRUnichar* aProfile)
80 nsresult rv = NS_OK;
82 PRBool replace = aStartup ? PR_TRUE : PR_FALSE;
84 if (!mTargetProfile) {
85 GetProfilePath(aStartup, mTargetProfile);
86 if (!mTargetProfile) return NS_ERROR_FAILURE;
89 if (!mSourceProfile) {
90 nsCOMPtr<nsIProperties> fileLocator =
91 do_GetService("@mozilla.org/file/directory_service;1", &rv);
92 NS_ENSURE_SUCCESS(rv, rv);
94 fileLocator->Get(NS_OSX_USER_PREFERENCES_DIR,
95 NS_GET_IID(nsILocalFile),
96 getter_AddRefs(mSourceProfile));
97 mSourceProfile->Append(MACIE_PREFERENCES_FOLDER_NAME);
100 NOTIFY_OBSERVERS(MIGRATION_STARTED, nsnull);
102 COPY_DATA(CopyBookmarks, replace, nsIBrowserProfileMigrator::BOOKMARKS);
104 NOTIFY_OBSERVERS(MIGRATION_ENDED, nsnull);
106 return rv;
109 NS_IMETHODIMP
110 nsMacIEProfileMigrator::GetMigrateData(const PRUnichar* aProfile,
111 PRBool aReplace,
112 PRUint16* aResult)
114 *aResult = 0;
116 if (!mSourceProfile) {
117 nsresult rv;
118 nsCOMPtr<nsIProperties> fileLocator =
119 do_GetService("@mozilla.org/file/directory_service;1", &rv);
120 NS_ENSURE_SUCCESS(rv, rv);
122 fileLocator->Get(NS_OSX_USER_PREFERENCES_DIR,
123 NS_GET_IID(nsILocalFile),
124 getter_AddRefs(mSourceProfile));
125 mSourceProfile->Append(MACIE_PREFERENCES_FOLDER_NAME);
128 MigrationData data[] = { { ToNewUnicode(MACIE_BOOKMARKS_FILE_NAME),
129 nsIBrowserProfileMigrator::BOOKMARKS,
130 PR_FALSE } };
132 // Frees file name strings allocated above.
133 GetMigrateDataFromArray(data, sizeof(data)/sizeof(MigrationData),
134 aReplace, mSourceProfile, aResult);
136 return NS_OK;
139 NS_IMETHODIMP
140 nsMacIEProfileMigrator::GetSourceExists(PRBool* aResult)
142 // Since the IE bookmarks file can sometimes be created by programs
143 // other than Internet Explorer, thus misleading, we must first
144 // check whether IE is even installed on this Mac. We accomplish this by
145 // checking one of IEs stored preferences in the apple.internetconfig file.
146 PRBool prefExists = PR_FALSE;
147 OSErr err;
148 ICInstance icInstance;
150 err = ::ICStart(&icInstance, 'FRFX');
151 if (err == noErr) {
152 ICAttr attrs;
153 Str255 IEhomePageValue;
154 long size = kICFileSpecHeaderSize;
155 err = ::ICGetPref(icInstance, MACIE_DEFAULT_HOMEPAGE_PREF, &attrs,
156 IEhomePageValue, &size);
157 if (err == noErr)
158 prefExists = PR_TRUE;
160 ::ICStop(icInstance);
163 if (!prefExists) {
164 *aResult = PR_FALSE;
165 return NS_OK;
168 PRUint16 data;
169 GetMigrateData(nsnull, PR_FALSE, &data);
171 *aResult = data != 0;
173 return NS_OK;
176 NS_IMETHODIMP
177 nsMacIEProfileMigrator::GetSourceHasMultipleProfiles(PRBool* aResult)
179 *aResult = PR_FALSE;
180 return NS_OK;
183 NS_IMETHODIMP
184 nsMacIEProfileMigrator::GetSourceProfiles(nsISupportsArray** aResult)
186 *aResult = nsnull;
187 return NS_OK;
190 NS_IMETHODIMP
191 nsMacIEProfileMigrator::GetSourceHomePageURL(nsACString& aResult)
193 aResult.Truncate();
194 return NS_OK;
197 ///////////////////////////////////////////////////////////////////////////////
198 // nsMacIEProfileMigrator
200 nsresult
201 nsMacIEProfileMigrator::CopyBookmarks(PRBool aReplace)
203 nsresult rv;
204 nsCOMPtr<nsIFile> sourceFile;
205 mSourceProfile->Clone(getter_AddRefs(sourceFile));
207 sourceFile->Append(MACIE_BOOKMARKS_FILE_NAME);
208 PRBool exists = PR_FALSE;
209 sourceFile->Exists(&exists);
210 if (!exists)
211 return NS_OK;
213 // it's an import
214 if (!aReplace)
215 return ImportBookmarksHTML(sourceFile,
216 PR_FALSE,
217 PR_FALSE,
218 NS_LITERAL_STRING("sourceNameIE").get());
220 // Initialize the default bookmarks
221 rv = InitializeBookmarks(mTargetProfile);
222 NS_ENSURE_SUCCESS(rv, rv);
224 // If we're blowing away existing content, annotate the Personal Toolbar and
225 // then import the file.
226 nsCOMPtr<nsIFile> tempFile;
227 mTargetProfile->Clone(getter_AddRefs(tempFile));
228 tempFile->Append(TEMP_BOOKMARKS_FILE_NAME);
230 // Look for the localized name of the IE Favorites Bar
231 nsCOMPtr<nsIStringBundleService> bundleService =
232 do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
233 NS_ENSURE_SUCCESS(rv, rv);
235 nsCOMPtr<nsIStringBundle> bundle;
236 rv = bundleService->CreateBundle(MIGRATION_BUNDLE, getter_AddRefs(bundle));
237 NS_ENSURE_SUCCESS(rv, rv);
239 nsString toolbarFolderNameMacIE;
240 bundle->GetStringFromName(NS_LITERAL_STRING("toolbarFolderNameMacIE").get(),
241 getter_Copies(toolbarFolderNameMacIE));
242 nsCAutoString ctoolbarFolderNameMacIE;
243 CopyUTF16toUTF8(toolbarFolderNameMacIE, ctoolbarFolderNameMacIE);
245 // Now read the 4.x bookmarks file, correcting the Personal Toolbar Folder
246 // line and writing to the temporary file.
247 rv = AnnotatePersonalToolbarFolder(sourceFile,
248 tempFile,
249 ctoolbarFolderNameMacIE.get());
250 NS_ENSURE_SUCCESS(rv, rv);
252 // import the temp file
253 rv = ImportBookmarksHTML(tempFile,
254 PR_TRUE,
255 PR_FALSE,
256 EmptyString().get());
257 NS_ENSURE_SUCCESS(rv, rv);
259 // remove the temp file
260 return tempFile->Remove(PR_FALSE);