40 #include "nsINavBookmarksService.h"
42 #include "nsToolkitCompsCID.h"
43 #include "nsIPlacesImportExportService.h"
45 #include "nsIInputStream.h"
46 #include "nsILineInputStream.h"
47 #include "nsIProperties.h"
48 #include "nsIProfileMigrator.h"
51 #include "nsNetUtil.h"
52 #include "nsISupportsPrimitives.h"
54 #include "nsAppDirectoryServiceDefs.h"
55 #include "nsIRDFService.h"
56 #include "nsIStringBundle.h"
57 #include "nsISupportsArray.h"
58 #include "nsXPCOMCID.h"
60 #define MIGRATION_BUNDLE "chrome://browser/locale/migration/migration.properties"
62 #define BOOKMARKS_FILE_NAME NS_LITERAL_STRING("bookmarks.html")
67 nsCOMPtr<nsISupportsString> supportsString =
68 do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID);
70 supportsString->SetData(aValue);
84 if (NS_SUCCEEDED(NS_NewURI(getter_AddRefs(uri), aHostPort))
85 && NS_SUCCEEDED(uri->GetHost(host))
87 && NS_SUCCEEDED(uri->GetPort(&portValue))) {
89 aPrefs->SetIntPref(aPortPref, portValue);
92 nsAutoString hostPort(aHostPort);
93 PRInt32 portDelimOffset = hostPort.RFindChar(
':');
94 if (portDelimOffset > 0) {
95 SetUnicharPref(aPref, Substring(hostPort, 0, portDelimOffset), aPrefs);
96 nsAutoString port(Substring(hostPort, portDelimOffset + 1));
98 portValue = port.ToInteger(&stringErr);
99 if (NS_SUCCEEDED(stringErr))
100 aPrefs->SetIntPref(aPortPref, portValue);
113 nsAutoString
override(aServers);
117 const nsAString& host = Substring(
override, left,
118 (
right < 0 ?
override.Length() :
right) - left);
119 if (host.EqualsLiteral(
"<local>"))
120 override.Replace(left, 7, NS_LITERAL_STRING(
"localhost,127.0.0.1"));
124 override.Replace(
right, 1, NS_LITERAL_STRING(
","));
130 PRBool aReplace, nsIFile* aSourceProfile,
133 nsCOMPtr<nsIFile> sourceFile;
137 for (cursor = aDataArray; cursor < end && cursor->
fileName; ++cursor) {
142 aSourceProfile->Clone(getter_AddRefs(sourceFile));
143 sourceFile->Append(nsDependentString(cursor->
fileName));
144 sourceFile->Exists(&exists);
157 aStartup->GetDirectory(getter_AddRefs(aProfileDir));
160 nsCOMPtr<nsIProperties>
dirSvc
161 (do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID));
164 (
void**) getter_AddRefs(aProfileDir));
171 nsIFile* aTargetBookmarksFile,
172 const char* aToolbarFolderName)
174 nsCOMPtr<nsIInputStream> fileInputStream;
175 nsresult rv = NS_NewLocalFileInputStream(getter_AddRefs(fileInputStream),
176 aSourceBookmarksFile);
177 NS_ENSURE_SUCCESS(rv, rv);
179 nsCOMPtr<nsIOutputStream> outputStream;
180 rv = NS_NewLocalFileOutputStream(getter_AddRefs(outputStream),
181 aTargetBookmarksFile);
182 NS_ENSURE_SUCCESS(rv, rv);
184 nsCOMPtr<nsILineInputStream> lineInputStream =
185 do_QueryInterface(fileInputStream, &rv);
186 NS_ENSURE_SUCCESS(rv, rv);
188 nsCAutoString sourceBuffer;
189 nsCAutoString targetBuffer;
190 PRBool moreData = PR_FALSE;
191 PRUint32 bytesWritten = 0;
193 lineInputStream->ReadLine(sourceBuffer, &moreData);
197 PRInt32 nameOffset = sourceBuffer.Find(aToolbarFolderName);
198 if (nameOffset >= 0) {
201 NS_NAMED_LITERAL_CSTRING(folderPrefix,
"<DT><H3 ");
202 PRInt32 folderPrefixOffset = sourceBuffer.Find(folderPrefix);
203 if (folderPrefixOffset >= 0)
204 sourceBuffer.Insert(NS_LITERAL_CSTRING(
"PERSONAL_TOOLBAR_FOLDER=\"true\" "),
205 folderPrefixOffset + folderPrefix.Length());
208 targetBuffer.Assign(sourceBuffer);
209 targetBuffer.Append(
"\r\n");
210 outputStream->Write(targetBuffer.get(), targetBuffer.Length(),
215 outputStream->Close();
222 PRBool aImportIntoRoot,
223 PRBool aOverwriteDefaults,
224 const PRUnichar* aImportSourceNameKey)
228 nsCOMPtr<nsILocalFile> localFile(do_QueryInterface(aBookmarksFile));
229 NS_ENSURE_TRUE(localFile, NS_ERROR_FAILURE);
231 NS_ENSURE_SUCCESS(rv, rv);
234 if (aImportIntoRoot) {
235 rv = importer->ImportHTMLFromFile(localFile, aOverwriteDefaults);
236 NS_ENSURE_SUCCESS(rv, rv);
241 nsCOMPtr<nsIStringBundleService> bundleService =
242 do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
243 NS_ENSURE_SUCCESS(rv, rv);
244 nsCOMPtr<nsIStringBundle>
bundle;
246 NS_ENSURE_SUCCESS(rv, rv);
249 rv = bundle->GetStringFromName(aImportSourceNameKey,
250 getter_Copies(sourceName));
251 NS_ENSURE_SUCCESS(rv, rv);
253 const PRUnichar* sourceNameStrings[] = { sourceName.get() };
254 nsString importedBookmarksTitle;
255 rv = bundle->FormatStringFromName(NS_LITERAL_STRING(
"importedBookmarksFolder").
get(),
256 sourceNameStrings, 1,
257 getter_Copies(importedBookmarksTitle));
258 NS_ENSURE_SUCCESS(rv, rv);
261 nsCOMPtr<nsINavBookmarksService> bms =
262 do_GetService(NS_NAVBOOKMARKSSERVICE_CONTRACTID, &rv);
263 NS_ENSURE_SUCCESS(rv, rv);
267 rv = bms->GetBookmarksMenuFolder(&root);
268 NS_ENSURE_SUCCESS(rv, rv);
271 rv = bms->CreateFolder(root, NS_ConvertUTF16toUTF8(importedBookmarksTitle),
272 nsINavBookmarksService::DEFAULT_INDEX, &folder);
273 NS_ENSURE_SUCCESS(rv, rv);
276 return importer->ImportHTMLFromFileToFolder(localFile, folder, PR_FALSE);
282 nsCOMPtr<nsIFile> bookmarksFile;
283 aTargetProfile->Clone(getter_AddRefs(bookmarksFile));
287 NS_ENSURE_SUCCESS(rv, rv);
nsresult InitializeBookmarks(nsIFile *aTargetProfile)
void ParseOverrideServers(const nsAString &aServers, nsIPrefBranch *aBranch)
nsresult ImportBookmarksHTML(nsIFile *aBookmarksFile, PRBool aImportIntoRoot, PRBool aOverwriteDefaults, const PRUnichar *aImportSourceNameKey)
nsresult AnnotatePersonalToolbarFolder(nsIFile *aSourceBookmarksFile, nsIFile *aTargetBookmarksFile, const char *aToolbarFolderName)
#define BOOKMARKS_FILE_NAME
void SetUnicharPref(const char *aPref, const nsAString &aValue, nsIPrefBranch *aPrefs)
void GetProfilePath(nsIProfileStartup *aStartup, nsCOMPtr< nsIFile > &aProfileDir)
void SetProxyPref(const nsAString &aHostPort, const char *aPref, const char *aPortPref, nsIPrefBranch *aPrefs)
#define NS_PLACESIMPORTEXPORTSERVICE_CONTRACTID
void GetMigrateDataFromArray(MigrationData *aDataArray, PRInt32 aDataArrayLength, PRBool aReplace, nsIFile *aSourceProfile, PRUint16 *aResult)