Back to index

lightning-sunbird  0.9+nobinonly
nsProfileMigratorBase.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 Mail Profile Migrator.
00016  *
00017  * The Initial Developer of the Original Code is Scott MacGregor.
00018  * Portions created by the Initial Developer are Copyright (C) 2004
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *  Scott MacGregor <mscott@mozilla.org>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsMailProfileMigratorUtils.h"
00039 #include "nsCRT.h"
00040 #include "nsDirectoryServiceDefs.h"
00041 #include "nsIObserverService.h"
00042 #include "nsIPasswordManagerInternal.h"
00043 #include "nsIPrefLocalizedString.h"
00044 #include "nsIPrefService.h"
00045 #include "nsIServiceManager.h"
00046 #include "nsISupportsArray.h"
00047 #include "nsISupportsPrimitives.h"
00048 #include "nsNetCID.h"
00049 #include "nsNetUtil.h"
00050 #include "nsProfileMigratorBase.h"
00051 #include "nsIProfileMigrator.h"
00052 #include "nsVoidArray.h"
00053 #include "nsIMailProfileMigrator.h"
00054 
00055 #include "nsIImportSettings.h"
00056 
00057 #define kPersonalAddressbookUri "moz-abmdbdirectory://abook.mab"
00058 
00059 nsProfileMigratorBase::nsProfileMigratorBase()
00060 {
00061   mObserverService = do_GetService("@mozilla.org/observer-service;1");
00062   mProcessingMailFolders = PR_FALSE;
00063 }
00064 
00065 nsProfileMigratorBase::~nsProfileMigratorBase()
00066 {           
00067   if (mFileIOTimer)
00068     mFileIOTimer->Cancel();
00069 }
00070 
00071 nsresult nsProfileMigratorBase::ImportSettings(nsIImportModule * aImportModule)
00072 {  
00073   nsresult rv; 
00074   
00075   nsAutoString index;
00076   index.AppendInt(nsIMailProfileMigrator::ACCOUNT_SETTINGS); 
00077   NOTIFY_OBSERVERS(MIGRATION_ITEMBEFOREMIGRATE, index.get());
00078 
00079   nsCOMPtr<nsIImportSettings> importSettings;
00080   rv = aImportModule->GetImportInterface(NS_IMPORT_SETTINGS_STR, getter_AddRefs(importSettings));
00081   NS_ENSURE_SUCCESS(rv, rv);
00082 
00083   PRBool importedSettings = PR_FALSE;
00084 
00085   rv = importSettings->Import(getter_AddRefs(mLocalFolderAccount), &importedSettings);
00086 
00087   NOTIFY_OBSERVERS(MIGRATION_ITEMAFTERMIGRATE, index.get());
00088 
00089   return rv;
00090 }
00091 
00092 nsresult nsProfileMigratorBase::ImportAddressBook(nsIImportModule * aImportModule)
00093 {  
00094   nsresult rv; 
00095   
00096   nsAutoString index;
00097   index.AppendInt(nsIMailProfileMigrator::ADDRESSBOOK_DATA); 
00098   NOTIFY_OBSERVERS(MIGRATION_ITEMBEFOREMIGRATE, index.get());
00099 
00100   rv = aImportModule->GetImportInterface(NS_IMPORT_ADDRESS_STR, getter_AddRefs(mGenericImporter));
00101   NS_ENSURE_SUCCESS(rv, rv);
00102 
00103   nsCOMPtr<nsISupportsCString> pabString = do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
00104   NS_ENSURE_SUCCESS(rv, rv);
00105 
00106   // we want to migrate the outlook express addressbook into our personal address book
00107   pabString->SetData(nsDependentCString(kPersonalAddressbookUri));
00108   mGenericImporter->SetData("addressDestination", pabString);
00109 
00110   PRBool importResult;
00111   PRBool wantsProgress;
00112   mGenericImporter->WantsProgress(&wantsProgress);
00113   rv = mGenericImporter->BeginImport(nsnull, nsnull, PR_TRUE, &importResult);
00114 
00115   if (wantsProgress)
00116     ContinueImport();
00117   else
00118     FinishCopyingAddressBookData();
00119 
00120   return rv;
00121 }
00122 
00123 nsresult nsProfileMigratorBase::FinishCopyingAddressBookData()
00124 {
00125   nsAutoString index;
00126   index.AppendInt(nsIMailProfileMigrator::ADDRESSBOOK_DATA); 
00127   NOTIFY_OBSERVERS(MIGRATION_ITEMAFTERMIGRATE, index.get());
00128 
00129   // now kick off the mail migration code
00130   ImportMailData(mImportModule);
00131 
00132   return NS_OK;
00133 }
00134 
00135 nsresult nsProfileMigratorBase::ImportMailData(nsIImportModule * aImportModule)
00136 {
00137   nsresult rv; 
00138   
00139   nsAutoString index;
00140   index.AppendInt(nsIMailProfileMigrator::MAILDATA); 
00141   NOTIFY_OBSERVERS(MIGRATION_ITEMBEFOREMIGRATE, index.get());
00142 
00143   rv = aImportModule->GetImportInterface(NS_IMPORT_MAIL_STR, getter_AddRefs(mGenericImporter));
00144   NS_ENSURE_SUCCESS(rv, rv);
00145 
00146   nsCOMPtr<nsISupportsPRBool> migrating = do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID, &rv);
00147   NS_ENSURE_SUCCESS(rv, rv);
00148 
00149   // by setting the migration flag, we force the import utility to install local folders from OE
00150   // directly into Local Folders and not as a subfolder
00151   migrating->SetData(PR_TRUE);
00152   mGenericImporter->SetData("migration", migrating);
00153 
00154   PRBool importResult;
00155   PRBool wantsProgress;
00156   mGenericImporter->WantsProgress(&wantsProgress);
00157   rv = mGenericImporter->BeginImport(nsnull, nsnull, PR_TRUE, &importResult);
00158 
00159   mProcessingMailFolders = PR_TRUE;
00160 
00161   if (wantsProgress)
00162     ContinueImport();
00163   else
00164     FinishCopyingMailFolders();
00165 
00166   return rv;
00167 }
00168 
00169 nsresult nsProfileMigratorBase::FinishCopyingMailFolders()
00170 {
00171   nsAutoString index;
00172   index.AppendInt(nsIMailProfileMigrator::MAILDATA); 
00173   NOTIFY_OBSERVERS(MIGRATION_ITEMAFTERMIGRATE, index.get());
00174 
00175   // migration is now done...notify the UI.
00176   NOTIFY_OBSERVERS(MIGRATION_ENDED, nsnull);
00177   return NS_OK;
00178 }
00179 
00180