Back to index

lightning-sunbird  0.9+nobinonly
nsNetscapeProfileMigratorBase.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is The Browser Profile Migrator.
00016  *
00017  * The Initial Developer of the Original Code is Ben Goodger.
00018  * Portions created by the Initial Developer are Copyright (C) 2004
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *  Ben Goodger <ben@bengoodger.com>
00023  *  Scott MacGregor <mscott@mozilla.org>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsAppDirectoryServiceDefs.h"
00040 #include "nsCRT.h"
00041 #include "nsIFile.h"
00042 #include "nsIInputStream.h"
00043 #include "nsILineInputStream.h"
00044 #include "nsInt64.h"
00045 #include "nsIPrefBranch.h"
00046 #include "nsIPrefLocalizedString.h"
00047 #include "nsIPrefService.h"
00048 #include "nsIRDFService.h"
00049 #include "nsIRegistry.h"
00050 #include "nsIServiceManager.h"
00051 #include "nsISupportsArray.h"
00052 #include "nsISupportsPrimitives.h"
00053 #include "nsIURL.h"
00054 #include "nsNetscapeProfileMigratorBase.h"
00055 #include "nsNetUtil.h"
00056 #include "nsReadableUtils.h"
00057 #include "nsXPIDLString.h"
00058 #include "prtime.h"
00059 #include "prprf.h"
00060 #include "nsVoidArray.h"
00061 
00062 static NS_DEFINE_CID(kStringBundleServiceCID, NS_STRINGBUNDLESERVICE_CID);
00063 
00064 #define MIGRATION_BUNDLE "chrome://messenger/locale/migration/migration.properties"
00065 
00066 #define FILE_NAME_PREFS_5X NS_LITERAL_STRING("prefs.js")
00067 
00069 // nsNetscapeProfileMigratorBase
00070 nsNetscapeProfileMigratorBase::nsNetscapeProfileMigratorBase()
00071 {
00072   nsCOMPtr<nsIStringBundleService> bundleService(do_GetService(kStringBundleServiceCID));
00073   bundleService->CreateBundle(MIGRATION_BUNDLE, getter_AddRefs(mBundle));
00074 
00075   // create the array we'll be using to keep track of the asynchronous file copy routines
00076   mFileCopyTransactions = new nsVoidArray();
00077   mFileCopyTransactionIndex = 0;
00078 }
00079 
00080 nsresult
00081 nsNetscapeProfileMigratorBase::GetProfileDataFromRegistry(nsILocalFile* aRegistryFile,
00082                                                           nsISupportsArray* aProfileNames,
00083                                                           nsISupportsArray* aProfileLocations)
00084 {
00085   nsresult rv = NS_OK;
00086 
00087   // Open It
00088   nsCOMPtr<nsIRegistry> reg(do_CreateInstance("@mozilla.org/registry;1"));
00089   reg->Open(aRegistryFile);
00090 
00091   nsRegistryKey profilesTree;
00092   rv = reg->GetKey(nsIRegistry::Common, NS_LITERAL_STRING("Profiles").get(), &profilesTree);
00093   if (NS_FAILED(rv)) return rv;
00094 
00095   nsCOMPtr<nsIEnumerator> keys;
00096   reg->EnumerateSubtrees(profilesTree, getter_AddRefs(keys));
00097 
00098   keys->First();
00099   while (keys->IsDone() != NS_OK) {
00100     nsCOMPtr<nsISupports> key;
00101     keys->CurrentItem(getter_AddRefs(key));
00102 
00103     nsCOMPtr<nsIRegistryNode> node(do_QueryInterface(key));
00104 
00105     nsRegistryKey profile;
00106     node->GetKey(&profile);
00107 
00108     // "migrated" is "yes" for all valid Seamonkey profiles. It is only "no"
00109     // for 4.x profiles. 
00110     nsXPIDLString isMigrated;
00111     reg->GetString(profile, NS_LITERAL_STRING("migrated").get(), getter_Copies(isMigrated));
00112 
00113     if (isMigrated.Equals(NS_LITERAL_STRING("no"))) {
00114       keys->Next();
00115       continue;
00116     }
00117 
00118     // Get the profile name and add it to the names array
00119     nsXPIDLString profileName;
00120     node->GetName(getter_Copies(profileName));
00121 
00122     // Get the profile location and add it to the locations array
00123     nsXPIDLString directory;
00124     reg->GetString(profile, NS_LITERAL_STRING("directory").get(), getter_Copies(directory));
00125 
00126     nsCOMPtr<nsILocalFile> dir;
00127 #ifdef XP_MACOSX
00128     rv = NS_NewNativeLocalFile(EmptyCString(), PR_TRUE, getter_AddRefs(dir));
00129     if (NS_FAILED(rv)) return rv;
00130     dir->SetPersistentDescriptor(NS_LossyConvertUCS2toASCII(directory));
00131 #else
00132     rv = NS_NewLocalFile(directory, PR_TRUE, getter_AddRefs(dir));
00133     if (NS_FAILED(rv)) return rv;
00134 #endif
00135 
00136     PRBool exists;
00137     dir->Exists(&exists);
00138 
00139     if (exists) {
00140       nsCOMPtr<nsISupportsString> profileNameString(do_CreateInstance("@mozilla.org/supports-string;1"));
00141       profileNameString->SetData(profileName);
00142       aProfileNames->AppendElement(profileNameString);
00143 
00144       aProfileLocations->AppendElement(dir);
00145     }
00146 
00147     keys->Next();
00148   }
00149   return rv;
00150 }
00151 
00152 #define GETPREF(xform, method, value) \
00153   nsresult rv = aBranch->method(xform->sourcePrefName, value); \
00154   if (NS_SUCCEEDED(rv)) \
00155     xform->prefHasValue = PR_TRUE; \
00156   return rv;
00157 
00158 #define SETPREF(xform, method, value) \
00159   if (xform->prefHasValue) { \
00160     return aBranch->method(xform->targetPrefName ? xform->targetPrefName : xform->sourcePrefName, value); \
00161   } \
00162   return NS_OK;
00163 
00164 nsresult 
00165 nsNetscapeProfileMigratorBase::GetString(void* aTransform, nsIPrefBranch* aBranch)
00166 {
00167   PrefTransform* xform = (PrefTransform*)aTransform;
00168   GETPREF(xform, GetCharPref, &xform->stringValue);
00169 }
00170 
00171 nsresult 
00172 nsNetscapeProfileMigratorBase::SetString(void* aTransform, nsIPrefBranch* aBranch)
00173 {
00174   PrefTransform* xform = (PrefTransform*)aTransform;
00175   SETPREF(xform, SetCharPref, xform->stringValue);
00176 }
00177 
00178 nsresult
00179 nsNetscapeProfileMigratorBase::GetWString(void* aTransform, nsIPrefBranch* aBranch)
00180 {
00181   PrefTransform* xform = (PrefTransform*)aTransform;
00182   nsCOMPtr<nsIPrefLocalizedString> prefValue;
00183   nsresult rv = aBranch->GetComplexValue(xform->sourcePrefName, 
00184                                          NS_GET_IID(nsIPrefLocalizedString),
00185                                          getter_AddRefs(prefValue));
00186 
00187   if (NS_SUCCEEDED(rv) && prefValue) {
00188     nsXPIDLString data;
00189     prefValue->ToString(getter_Copies(data));
00190 
00191     xform->stringValue = ToNewCString(NS_ConvertUCS2toUTF8(data));
00192     xform->prefHasValue = PR_TRUE;
00193   }
00194   return rv;
00195 }
00196 
00197 nsresult 
00198 nsNetscapeProfileMigratorBase::SetWStringFromASCII(void* aTransform, nsIPrefBranch* aBranch)
00199 {
00200   PrefTransform* xform = (PrefTransform*)aTransform;
00201   if (xform->prefHasValue) {
00202     nsCOMPtr<nsIPrefLocalizedString> pls(do_CreateInstance("@mozilla.org/pref-localizedstring;1"));
00203     nsAutoString data; data.AssignWithConversion(xform->stringValue);
00204     pls->SetData(data.get());
00205     return aBranch->SetComplexValue(xform->targetPrefName ? xform->targetPrefName : xform->sourcePrefName, NS_GET_IID(nsIPrefLocalizedString), pls);
00206   }
00207   return NS_OK;
00208 }
00209 
00210 nsresult
00211 nsNetscapeProfileMigratorBase::SetWString(void* aTransform, nsIPrefBranch* aBranch)
00212 {
00213   PrefTransform* xform = (PrefTransform*)aTransform;
00214   if (xform->prefHasValue) {
00215     nsCOMPtr<nsIPrefLocalizedString> pls(do_CreateInstance("@mozilla.org/pref-localizedstring;1"));
00216     nsAutoString data = NS_ConvertUTF8toUCS2(xform->stringValue);
00217     pls->SetData(data.get());
00218     return aBranch->SetComplexValue(xform->targetPrefName ? xform->targetPrefName : xform->sourcePrefName, NS_GET_IID(nsIPrefLocalizedString), pls);
00219   }
00220   return NS_OK;
00221 }
00222 
00223 
00224 nsresult 
00225 nsNetscapeProfileMigratorBase::GetBool(void* aTransform, nsIPrefBranch* aBranch)
00226 {
00227   PrefTransform* xform = (PrefTransform*)aTransform;
00228   GETPREF(xform, GetBoolPref, &xform->boolValue);
00229 }
00230 
00231 nsresult 
00232 nsNetscapeProfileMigratorBase::SetBool(void* aTransform, nsIPrefBranch* aBranch)
00233 {
00234   PrefTransform* xform = (PrefTransform*)aTransform;
00235   SETPREF(xform, SetBoolPref, xform->boolValue);
00236 }
00237 
00238 nsresult 
00239 nsNetscapeProfileMigratorBase::GetInt(void* aTransform, nsIPrefBranch* aBranch)
00240 {
00241   PrefTransform* xform = (PrefTransform*)aTransform;
00242   GETPREF(xform, GetIntPref, &xform->intValue);
00243 }
00244 
00245 nsresult 
00246 nsNetscapeProfileMigratorBase::SetInt(void* aTransform, nsIPrefBranch* aBranch)
00247 {
00248   PrefTransform* xform = (PrefTransform*)aTransform;
00249   SETPREF(xform, SetIntPref, xform->intValue);
00250 }
00251 
00252 nsresult
00253 nsNetscapeProfileMigratorBase::CopyFile(const nsAString& aSourceFileName, const nsAString& aTargetFileName)
00254 {
00255   nsCOMPtr<nsIFile> sourceFile;
00256   mSourceProfile->Clone(getter_AddRefs(sourceFile));
00257 
00258   sourceFile->Append(aSourceFileName);
00259   PRBool exists = PR_FALSE;
00260   sourceFile->Exists(&exists);
00261   if (!exists)
00262     return NS_OK;
00263 
00264   nsCOMPtr<nsIFile> targetFile;
00265   mTargetProfile->Clone(getter_AddRefs(targetFile));
00266   
00267   targetFile->Append(aTargetFileName);
00268   targetFile->Exists(&exists);
00269   if (exists)
00270     targetFile->Remove(PR_FALSE);
00271 
00272   return sourceFile->CopyTo(mTargetProfile, aTargetFileName);
00273 }
00274 
00275 nsresult
00276 nsNetscapeProfileMigratorBase::GetSignonFileName(PRBool aReplace, char** aFileName)
00277 {
00278   nsresult rv;
00279   if (aReplace) {
00280     // Find out what the signons file was called, this is stored in a pref
00281     // in Seamonkey.
00282     nsCOMPtr<nsIPrefService> psvc(do_GetService(NS_PREFSERVICE_CONTRACTID));
00283     psvc->ResetPrefs();
00284 
00285     nsCOMPtr<nsIFile> sourcePrefsName;
00286     mSourceProfile->Clone(getter_AddRefs(sourcePrefsName));
00287     sourcePrefsName->Append(FILE_NAME_PREFS_5X);
00288     psvc->ReadUserPrefs(sourcePrefsName);
00289 
00290     nsCOMPtr<nsIPrefBranch> branch(do_QueryInterface(psvc));
00291     rv = branch->GetCharPref("signon.SignonFileName", aFileName);
00292   }
00293   else 
00294     rv = LocateSignonsFile(aFileName);
00295   return rv;
00296 }
00297 
00298 nsresult
00299 nsNetscapeProfileMigratorBase::LocateSignonsFile(char** aResult)
00300 {
00301   nsCOMPtr<nsISimpleEnumerator> entries;
00302   nsresult rv = mSourceProfile->GetDirectoryEntries(getter_AddRefs(entries));
00303   if (NS_FAILED(rv)) return rv;
00304 
00305   nsCAutoString fileName;
00306   do {
00307     PRBool hasMore = PR_FALSE;
00308     rv = entries->HasMoreElements(&hasMore);
00309     if (NS_FAILED(rv) || !hasMore) break;
00310 
00311     nsCOMPtr<nsISupports> supp;
00312     rv = entries->GetNext(getter_AddRefs(supp));
00313     if (NS_FAILED(rv)) break;
00314 
00315     nsCOMPtr<nsIFile> currFile(do_QueryInterface(supp));
00316 
00317     nsCOMPtr<nsIURI> uri;
00318     rv = NS_NewFileURI(getter_AddRefs(uri), currFile);
00319     if (NS_FAILED(rv)) break;
00320     nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
00321 
00322     nsCAutoString extn;
00323     url->GetFileExtension(extn);
00324 
00325     if (extn.EqualsIgnoreCase("s")) {
00326       url->GetFileName(fileName);
00327       break;
00328     }
00329   }
00330   while (1);
00331 
00332   *aResult = ToNewCString(fileName);
00333 
00334   return NS_OK;
00335 }
00336 
00337 // helper function, copies the contents of srcDir into destDir.
00338 // destDir will be created if it doesn't exist.
00339 
00340 nsresult nsNetscapeProfileMigratorBase::RecursiveCopy(nsIFile* srcDir, nsIFile* destDir)
00341 {
00342   nsresult rv;
00343   PRBool isDir;
00344   
00345   rv = srcDir->IsDirectory(&isDir);
00346   if (NS_FAILED(rv)) return rv;
00347   if (!isDir) return NS_ERROR_INVALID_ARG;
00348   
00349   PRBool exists;
00350   rv = destDir->Exists(&exists);
00351   if (NS_SUCCEEDED(rv) && !exists)
00352     rv = destDir->Create(nsIFile::DIRECTORY_TYPE, 0775);
00353   if (NS_FAILED(rv)) return rv;
00354   
00355   PRBool hasMore = PR_FALSE;
00356   nsCOMPtr<nsISimpleEnumerator> dirIterator;
00357   rv = srcDir->GetDirectoryEntries(getter_AddRefs(dirIterator));
00358   if (NS_FAILED(rv)) return rv;
00359   
00360   rv = dirIterator->HasMoreElements(&hasMore);
00361   if (NS_FAILED(rv)) return rv;
00362   
00363   nsCOMPtr<nsIFile> dirEntry;
00364   
00365   while (hasMore)
00366   {
00367     rv = dirIterator->GetNext((nsISupports**)getter_AddRefs(dirEntry));
00368     if (NS_SUCCEEDED(rv))
00369     {
00370       rv = dirEntry->IsDirectory(&isDir);
00371       if (NS_SUCCEEDED(rv))
00372       {
00373         if (isDir)
00374         {
00375           nsCOMPtr<nsIFile> destClone;
00376           rv = destDir->Clone(getter_AddRefs(destClone));
00377           if (NS_SUCCEEDED(rv))
00378           {
00379             nsCOMPtr<nsILocalFile> newChild(do_QueryInterface(destClone));
00380             nsAutoString leafName;
00381             dirEntry->GetLeafName(leafName);
00382             newChild->AppendRelativePath(leafName);
00383             rv = newChild->Exists(&exists);
00384             if (NS_SUCCEEDED(rv) && !exists)
00385               rv = newChild->Create(nsIFile::DIRECTORY_TYPE, 0775);
00386             rv = RecursiveCopy(dirEntry, newChild);
00387           }
00388         }
00389         else
00390         {
00391           // we aren't going to do any actual file copying here. Instead, add this to our
00392           // file transaction list so we can copy files asynchronously...
00393           fileTransactionEntry* fileEntry = new fileTransactionEntry;
00394           fileEntry->srcFile = dirEntry;
00395           fileEntry->destFile = destDir;
00396 
00397           mFileCopyTransactions->AppendElement((void*) fileEntry);
00398         }
00399       }      
00400     }
00401     rv = dirIterator->HasMoreElements(&hasMore);
00402     if (NS_FAILED(rv)) return rv;
00403   }
00404   
00405   return rv;
00406 }
00407