Back to index

lightning-sunbird  0.9+nobinonly
nsProfileMigrator.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  *  Benjamin Smedberg <bsmedberg@covad.net>
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 "nsILocalFile.h"
00040 #include "nsIDOMWindowInternal.h"
00041 #include "nsIProfileMigrator.h"
00042 #include "nsIPrefService.h"
00043 #include "nsIServiceManager.h"
00044 #include "nsIToolkitProfile.h"
00045 #include "nsIToolkitProfileService.h"
00046 #include "nsIWindowWatcher.h"
00047 #include "nsISupportsPrimitives.h"
00048 #include "nsISupportsArray.h"
00049 
00050 #include "nsDirectoryServiceDefs.h"
00051 #include "nsProfileMigrator.h"
00052 #include "nsMailMigrationCID.h"
00053 
00054 #include "nsCRT.h"
00055 #include "NSReg.h"
00056 #include "nsReadableUtils.h"
00057 #include "nsString.h"
00058 #ifdef XP_WIN
00059 #include <windows.h>
00060 #endif
00061 
00062 #ifndef MAXPATHLEN
00063 #ifdef _MAX_PATH
00064 #define MAXPATHLEN _MAX_PATH
00065 #elif defined(CCHMAXPATH)
00066 #define MAXPATHLEN CCHMAXPATH
00067 #else
00068 #define MAXPATHLEN 1024
00069 #endif
00070 #endif
00071 
00072 NS_IMPL_ISUPPORTS1(nsProfileMigrator, nsIProfileMigrator)
00073 
00074 #define MIGRATION_WIZARD_FE_URL "chrome://messenger/content/migration/migration.xul"
00075 #define MIGRATION_WIZARD_FE_FEATURES "chrome,dialog,modal,centerscreen"
00076 
00077 NS_IMETHODIMP
00078 nsProfileMigrator::Migrate(nsIProfileStartup* aStartup)
00079 {
00080   nsCAutoString key;
00081   nsCOMPtr<nsIMailProfileMigrator> mailMigrator;
00082   nsresult rv = GetDefaultMailMigratorKey(key, mailMigrator);
00083   NS_ENSURE_SUCCESS(rv, rv); // abort migration if we failed to get a mailMigrator (if we were supposed to)
00084 
00085   nsCOMPtr<nsISupportsCString> cstr (do_CreateInstance("@mozilla.org/supports-cstring;1"));
00086   NS_ENSURE_TRUE(cstr, NS_ERROR_OUT_OF_MEMORY);
00087   cstr->SetData(key);
00088 
00089   // By opening the Migration FE with a supplied mailMigrator, it will automatically
00090   // migrate from it. 
00091   nsCOMPtr<nsIWindowWatcher> ww (do_GetService(NS_WINDOWWATCHER_CONTRACTID));
00092   nsCOMPtr<nsISupportsArray> params;
00093   NS_NewISupportsArray(getter_AddRefs(params));
00094   if (!ww || !params) return NS_ERROR_FAILURE;
00095 
00096   params->AppendElement(cstr);
00097   params->AppendElement(mailMigrator);
00098   params->AppendElement(aStartup);
00099 
00100   nsCOMPtr<nsIDOMWindow> migrateWizard;
00101   return ww->OpenWindow(nsnull, 
00102                         MIGRATION_WIZARD_FE_URL,
00103                         "_blank",
00104                         MIGRATION_WIZARD_FE_FEATURES,
00105                         params,
00106                         getter_AddRefs(migrateWizard));
00107 }
00108 
00109 #ifdef XP_WIN
00110 typedef struct {
00111   WORD wLanguage;
00112   WORD wCodePage;
00113 } LANGANDCODEPAGE;
00114 
00115 #define INTERNAL_NAME_THUNDERBIRD     "Thunderbird"
00116 #define INTERNAL_NAME_SEAMONKEY       "Mozilla"
00117 #define INTERNAL_NAME_DOGBERT         "Netscape Messenger"
00118 #endif
00119 
00120 nsresult
00121 nsProfileMigrator::GetDefaultMailMigratorKey(nsACString& aKey, nsCOMPtr<nsIMailProfileMigrator>& mailMigrator)
00122 {
00123   // look up the value of profile.force.migration in case we are supposed to force migration using a particular
00124   // migrator....
00125   nsresult rv = NS_OK;
00126   nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
00127   NS_ENSURE_SUCCESS(rv, rv);
00128 
00129   nsXPIDLCString forceMigrationType;
00130   prefs->GetCharPref("profile.force.migration", getter_Copies(forceMigrationType));
00131 
00132   // if we are being forced to migrate to a particular migration type, then create an instance of that migrator
00133   // and return it.
00134   if (forceMigrationType.get())
00135   {
00136     PRBool exists = PR_FALSE;
00137     nsCAutoString migratorID (NS_MAILPROFILEMIGRATOR_CONTRACTID_PREFIX);
00138     migratorID.Append(forceMigrationType);
00139     mailMigrator = do_CreateInstance(migratorID.get());
00140   
00141     if (mailMigrator)
00142     {
00143       mailMigrator->GetSourceExists(&exists);
00144       if (exists) 
00145         aKey = forceMigrationType;
00146       else
00147         rv = NS_ERROR_FAILURE; // trying to force migration on a source which does not have any profiles
00148     }
00149   }
00150  
00151   return rv;
00152 }
00153 
00154 NS_IMETHODIMP
00155 nsProfileMigrator::Import()
00156 {
00157   if (ImportRegistryProfiles(NS_LITERAL_CSTRING("Thunderbird")))
00158     return NS_OK;
00159 
00160   return NS_ERROR_FAILURE;
00161 }
00162 
00163 PRBool
00164 nsProfileMigrator::ImportRegistryProfiles(const nsACString& aAppName)
00165 {
00166   nsresult rv;
00167 
00168   nsCOMPtr<nsIToolkitProfileService> profileSvc
00169     (do_GetService(NS_PROFILESERVICE_CONTRACTID));
00170   NS_ENSURE_TRUE(profileSvc, NS_ERROR_FAILURE);
00171 
00172   nsCOMPtr<nsIProperties> dirService
00173     (do_GetService("@mozilla.org/file/directory_service;1"));
00174   NS_ENSURE_TRUE(dirService, NS_ERROR_FAILURE);
00175 
00176   nsCOMPtr<nsILocalFile> regFile;
00177 #ifdef XP_WIN
00178   rv = dirService->Get(NS_WIN_APPDATA_DIR, NS_GET_IID(nsILocalFile),
00179                        getter_AddRefs(regFile));
00180   NS_ENSURE_SUCCESS(rv, PR_FALSE);
00181   regFile->AppendNative(aAppName);
00182   regFile->AppendNative(NS_LITERAL_CSTRING("registry.dat"));
00183 #elif defined(XP_MACOSX)
00184   rv = dirService->Get(NS_MAC_USER_LIB_DIR, NS_GET_IID(nsILocalFile),
00185                        getter_AddRefs(regFile));
00186   NS_ENSURE_SUCCESS(rv, PR_FALSE);
00187   regFile->AppendNative(aAppName);
00188   regFile->AppendNative(NS_LITERAL_CSTRING("Application Registry"));
00189 #elif defined(XP_OS2)
00190   rv = dirService->Get(NS_OS2_HOME_DIR, NS_GET_IID(nsILocalFile),
00191                        getter_AddRefs(regFile));
00192   NS_ENSURE_SUCCESS(rv, PR_FALSE);
00193   regFile->AppendNative(aAppName);
00194   regFile->AppendNative(NS_LITERAL_CSTRING("registry.dat"));
00195 #elif defined(XP_BEOS)
00196   rv = dirService->Get(NS_BEOS_SETTINGS_DIR, NS_GET_IID(nsILocalFile),
00197                        getter_AddRefs(regFile));
00198   NS_ENSURE_SUCCESS(rv, PR_FALSE);
00199   regFile->AppendNative(aAppName);
00200   regFile->AppendNative(NS_LITERAL_CSTRING("appreg"));
00201 #else
00202   rv = dirService->Get(NS_UNIX_HOME_DIR, NS_GET_IID(nsILocalFile),
00203                        getter_AddRefs(regFile));
00204   NS_ENSURE_SUCCESS(rv, PR_FALSE);
00205   nsCAutoString dotAppName;
00206   ToLowerCase(aAppName, dotAppName);
00207   dotAppName.Insert('.', 0);
00208   
00209   regFile->AppendNative(dotAppName);
00210   regFile->AppendNative(NS_LITERAL_CSTRING("appreg"));
00211 #endif
00212 
00213   nsCAutoString path;
00214   rv = regFile->GetNativePath(path);
00215   NS_ENSURE_SUCCESS(rv, PR_FALSE);
00216 
00217   if (NR_StartupRegistry())
00218     return PR_FALSE;
00219 
00220   PRBool migrated = PR_FALSE;
00221   HREG reg = nsnull;
00222   RKEY profiles = 0;
00223   REGENUM enumstate = 0;
00224   char profileName[MAXREGNAMELEN];
00225 
00226   if (NR_RegOpen(path.get(), &reg))
00227     goto cleanup;
00228 
00229   if (NR_RegGetKey(reg, ROOTKEY_COMMON, "Profiles", &profiles))
00230     goto cleanup;
00231 
00232   while (!NR_RegEnumSubkeys(reg, profiles, &enumstate,
00233                             profileName, MAXREGNAMELEN, REGENUM_CHILDREN)) {
00234 #ifdef DEBUG_bsmedberg
00235     printf("Found profile %s.\n", profileName);
00236 #endif
00237 
00238     RKEY profile = 0;
00239     if (NR_RegGetKey(reg, profiles, profileName, &profile)) {
00240       NS_ERROR("Could not get the key that was enumerated.");
00241       continue;
00242     }
00243 
00244     char profilePath[MAXPATHLEN];
00245     if (NR_RegGetEntryString(reg, profile, "directory",
00246                              profilePath, MAXPATHLEN))
00247       continue;
00248 
00249     nsCOMPtr<nsILocalFile> profileFile
00250       (do_CreateInstance("@mozilla.org/file/local;1"));
00251     if (!profileFile)
00252       continue;
00253 #if defined (XP_MACOSX)
00254     rv = profileFile->SetPersistentDescriptor(nsDependentCString(profilePath));
00255 #else
00256     NS_ConvertUTF8toUTF16 widePath(profilePath);
00257     rv = profileFile->InitWithPath(widePath);
00258 #endif
00259     if (NS_FAILED(rv)) continue;
00260 
00261     nsCOMPtr<nsIToolkitProfile> tprofile;
00262     profileSvc->CreateProfile(profileFile, nsnull,
00263                               nsDependentCString(profileName),
00264                               getter_AddRefs(tprofile));
00265     migrated = PR_TRUE;
00266   }
00267 
00268 cleanup:
00269   if (reg)
00270     NR_RegClose(reg);
00271   NR_ShutdownRegistry();
00272   return migrated;
00273 }