nsMacIEProfileMigrator.cpp
Go to the documentation of this file.
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
4  *
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/
9  *
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.
14  *
15  * The Original Code is The Browser Profile Migrator.
16  *
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.
20  *
21  * Contributor(s):
22  * Ben Goodger <ben@bengoodger.com>
23  * Benjamin Smedberg <benjamin@smedbergs.us>
24  *
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.
36  *
37  * ***** END LICENSE BLOCK ***** */
38 
39 #include "nsDirectoryServiceDefs.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 <Carbon/Carbon.h>
52 
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")
57 
58 #define MIGRATION_BUNDLE "chrome://browser/locale/migration/migration.properties"
59 
61 // nsMacIEProfileMigrator
62 
64 
66 {
67  mObserverService = do_GetService("@mozilla.org/observer-service;1");
68 }
69 
71 {
72 }
73 
75 // nsIBrowserProfileMigrator
76 
77 NS_IMETHODIMP
78 nsMacIEProfileMigrator::Migrate(PRUint16 aItems, nsIProfileStartup* aStartup, const PRUnichar* aProfile)
79 {
80  nsresult rv = NS_OK;
81 
82  PRBool replace = aStartup ? PR_TRUE : PR_FALSE;
83 
84  if (!mTargetProfile) {
85  GetProfilePath(aStartup, mTargetProfile);
86  if (!mTargetProfile) return NS_ERROR_FAILURE;
87  }
88 
89  if (!mSourceProfile) {
90  nsCOMPtr<nsIProperties> fileLocator =
91  do_GetService("@mozilla.org/file/directory_service;1", &rv);
92  NS_ENSURE_SUCCESS(rv, rv);
93 
94  fileLocator->Get(NS_OSX_USER_PREFERENCES_DIR,
95  NS_GET_IID(nsILocalFile),
96  getter_AddRefs(mSourceProfile));
98  }
99 
101 
103 
105 
106  return rv;
107 }
108 
109 NS_IMETHODIMP
110 nsMacIEProfileMigrator::GetMigrateData(const PRUnichar* aProfile,
111  PRBool aReplace,
112  PRUint16* aResult)
113 {
114  *aResult = 0;
115 
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);
121 
122  fileLocator->Get(NS_OSX_USER_PREFERENCES_DIR,
123  NS_GET_IID(nsILocalFile),
124  getter_AddRefs(mSourceProfile));
126  }
127 
128  MigrationData data[] = { { ToNewUnicode(MACIE_BOOKMARKS_FILE_NAME),
130  PR_FALSE } };
131 
132  // Frees file name strings allocated above.
133  GetMigrateDataFromArray(data, sizeof(data)/sizeof(MigrationData),
134  aReplace, mSourceProfile, aResult);
135 
136  return NS_OK;
137 }
138 
139 NS_IMETHODIMP
140 nsMacIEProfileMigrator::GetSourceExists(PRBool* aResult)
141 {
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;
149 
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;
159 
160  ::ICStop(icInstance);
161  }
162 
163  if (!prefExists) {
164  *aResult = PR_FALSE;
165  return NS_OK;
166  }
167 
168  PRUint16 data;
169  GetMigrateData(nsnull, PR_FALSE, &data);
170 
171  *aResult = data != 0;
172 
173  return NS_OK;
174 }
175 
176 NS_IMETHODIMP
177 nsMacIEProfileMigrator::GetSourceHasMultipleProfiles(PRBool* aResult)
178 {
179  *aResult = PR_FALSE;
180  return NS_OK;
181 }
182 
183 NS_IMETHODIMP
184 nsMacIEProfileMigrator::GetSourceProfiles(nsISupportsArray** aResult)
185 {
186  *aResult = nsnull;
187  return NS_OK;
188 }
189 
190 NS_IMETHODIMP
191 nsMacIEProfileMigrator::GetSourceHomePageURL(nsACString& aResult)
192 {
193  aResult.Truncate();
194  return NS_OK;
195 }
196 
198 // nsMacIEProfileMigrator
199 
200 nsresult
202 {
203  nsresult rv;
204  nsCOMPtr<nsIFile> sourceFile;
205  mSourceProfile->Clone(getter_AddRefs(sourceFile));
206 
207  sourceFile->Append(MACIE_BOOKMARKS_FILE_NAME);
208  PRBool exists = PR_FALSE;
209  sourceFile->Exists(&exists);
210  if (!exists)
211  return NS_OK;
212 
213  // it's an import
214  if (!aReplace)
215  return ImportBookmarksHTML(sourceFile,
216  PR_FALSE,
217  PR_FALSE,
218  NS_LITERAL_STRING("sourceNameIE").get());
219 
220  // Initialize the default bookmarks
222  NS_ENSURE_SUCCESS(rv, rv);
223 
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);
229 
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);
234  nsCOMPtr<nsIStringBundle> bundle;
235  rv = bundleService->CreateBundle(MIGRATION_BUNDLE, getter_AddRefs(bundle));
236  NS_ENSURE_SUCCESS(rv, rv);
237 
238  nsString toolbarFolderNameMacIE;
239  rv = bundle->GetStringFromName(NS_LITERAL_STRING("toolbarFolderNameMacIE").get(),
240  getter_Copies(toolbarFolderNameMacIE));
241  NS_ENSURE_SUCCESS(rv, rv);
242 
243  // Now read the 4.x bookmarks file, correcting the Personal Toolbar Folder
244  // line and writing to the temporary file.
245  rv = AnnotatePersonalToolbarFolder(sourceFile,
246  tempFile,
247  NS_ConvertUTF16toUTF8(toolbarFolderNameMacIE).get());
248  NS_ENSURE_SUCCESS(rv, rv);
249 
250  // import the temp file
251  rv = ImportBookmarksHTML(tempFile,
252  PR_TRUE,
253  PR_FALSE,
254  EmptyString().get());
255  NS_ENSURE_SUCCESS(rv, rv);
256 
257  // remove the temp file
258  return tempFile->Remove(PR_FALSE);
259 }
nsresult InitializeBookmarks(nsIFile *aTargetProfile)
return NS_OK
NS_IMPL_ISUPPORTS1(sbDeviceCapabilitiesUtils, sbIDeviceCapabilitiesUtils) sbDeviceCapabilitiesUtils
nsresult CopyBookmarks(PRBool aReplace)
#define MACIE_PREFERENCES_FOLDER_NAME
nsresult ImportBookmarksHTML(nsIFile *aBookmarksFile, PRBool aImportIntoRoot, PRBool aOverwriteDefaults, const PRUnichar *aImportSourceNameKey)
nsresult AnnotatePersonalToolbarFolder(nsIFile *aSourceBookmarksFile, nsIFile *aTargetBookmarksFile, const char *aToolbarFolderName)
nsCOMPtr< nsIFile > mTargetProfile
#define TEMP_BOOKMARKS_FILE_NAME
#define NOTIFY_OBSERVERS(message, item)
var bundle
#define MIGRATION_STARTED
nsCOMPtr< nsILocalFile > mSourceProfile
#define COPY_DATA(func, replace, itemIndex)
#define MIGRATION_ENDED
#define MACIE_BOOKMARKS_FILE_NAME
#define MACIE_DEFAULT_HOMEPAGE_PREF
#define MIGRATION_BUNDLE
void GetProfilePath(nsIProfileStartup *aStartup, nsCOMPtr< nsIFile > &aProfileDir)
observe data
Definition: FeedWriter.js:1329
void GetMigrateDataFromArray(MigrationData *aDataArray, PRInt32 aDataArrayLength, PRBool aReplace, nsIFile *aSourceProfile, PRUint16 *aResult)