?? nsdogbertprofilemigrator.cpp
字號:
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- *//* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is The Mail Profile Migrator. * * The Initial Developer of the Original Code is Scott MacGregor * Portions created by the Initial Developer are Copyright (C) 2004 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Scott MacGregor <mscott@mozilla.org> * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */// this file is mostly a copy of nsPrefMigration.cpp...old nsFileSpec warts and all#include "nsMailProfileMigratorUtils.h"#include "nsCRT.h"#include "nsDirectoryServiceDefs.h"#include "nsIObserverService.h"#include "nsIPasswordManagerInternal.h"#include "nsIPrefLocalizedString.h"#include "nsIPrefService.h"#include "nsIServiceManager.h"#include "nsISupportsArray.h"#include "nsISupportsPrimitives.h"#include "nsNetCID.h"#include "nsNetUtil.h"#include "nsDogbertProfileMigrator.h"#include "nsIRelativeFilePref.h"#include "nsAppDirectoryServiceDefs.h"#include "nsVoidArray.h"#include "prprf.h"#include "prmem.h"#include "prio.h"#include "prenv.h"#include "NSReg.h"// lots of includes required for the nsPrefMigration.cpp code that we copied:#include "nsICharsetConverterManager.h"#include "nsIPlatformCharset.h"#include "nsIPref.h"#include "nsIFileSpec.h"#include "nsFileSpec.h"#include "nsFileStream.h"static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);static NS_DEFINE_CID(kPrefServiceCID, NS_PREF_CID);static NS_DEFINE_CID(kStringBundleServiceCID, NS_STRINGBUNDLESERVICE_CID);#define MIGRATION_PROPERTIES_URL "chrome://messenger/locale/migration/migration.properties"#ifndef MAXPATHLEN#ifdef _MAX_PATH#define MAXPATHLEN _MAX_PATH#elif defined(CCHMAXPATH)#define MAXPATHLEN CCHMAXPATH#else#define MAXPATHLEN 1024#endif#endif#if defined(XP_UNIX) && !defined(XP_MACOSX)#define PREF_FILE_NAME_IN_4x "preferences.js"#define IMAP_MAIL_FILTER_FILE_NAME_IN_4x "mailrule"#define POP_MAIL_FILTER_FILE_NAME_IN_4x "mailrule"#define MAIL_SUMMARY_SUFFIX_IN_4x ".summary"#define NEWS_SUMMARY_SUFFIX_IN_4x ".snm"#define NEWSRC_PREFIX_IN_4x ".newsrc-"#define SNEWSRC_PREFIX_IN_4x ".snewsrc-"#define POPSTATE_FILE_IN_4x "popstate"#define PSM_CERT7_DB "cert7.db"#define PSM_KEY3_DB "key3.db"#define PSM_SECMODULE_DB "secmodule.db"#define HOME_ENVIRONMENT_VARIABLE "HOME"#define PROFILE_HOME_ENVIRONMENT_VARIABLE "PROFILE_HOME"#define DEFAULT_UNIX_PROFILE_NAME "default"#elif defined(XP_MAC) || defined(XP_MACOSX)#define OLDREG_NAME "Netscape Registry"#define OLDREG_DIR NS_MAC_PREFS_DIR#define PREF_FILE_NAME_IN_4x "Netscape Preferences"#define MAC_RULES_FILE_ENDING_STRING_IN_4X " Rules"#define IMAP_MAIL_FILTER_FILE_NAME_IN_4x "<hostname> Rules"#define POP_MAIL_FILTER_FILE_NAME_IN_4x "Filter Rules"#define MAIL_SUMMARY_SUFFIX_IN_4x ".snm"#define NEWS_SUMMARY_SUFFIX_IN_4x ".snm"#define POPSTATE_FILE_IN_4x "Pop State"#define SECURITY_PATH "Security"#define PSM_CERT7_DB "Certificates7"#define PSM_KEY3_DB "Key Database3"#define PSM_SECMODULE_DB "Security Modules"#else /* XP_WIN || XP_OS2 */#define PREF_FILE_NAME_IN_4x "prefs.js"#define IMAP_MAIL_FILTER_FILE_NAME_IN_4x "rules.dat"#define POP_MAIL_FILTER_FILE_NAME_IN_4x "rules.dat"#define MAIL_SUMMARY_SUFFIX_IN_4x ".snm"#define NEWS_SUMMARY_SUFFIX_IN_4x ".snm"#define OLDREG_NAME "nsreg.dat"#ifdef XP_WIN#define OLDREG_DIR NS_WIN_WINDOWS_DIR#else#define OLDREG_DIR NS_OS2_DIR#endif// purposely not defined, since it was in the right place// and with the right name in 4.x//#define POPSTATE_FILE_IN_4x "popstate.dat"#define PSM_CERT7_DB "cert7.db"#define PSM_KEY3_DB "key3.db"#define PSM_SECMODULE_DB "secmod.db"#endif /* XP_UNIX */#define SUMMARY_SUFFIX_IN_5x ".msf"#define IMAP_MAIL_FILTER_FILE_NAME_IN_5x "rules.dat"#define POP_MAIL_FILTER_FILE_NAME_IN_5x "rules.dat"#define POPSTATE_FILE_IN_5x "popstate.dat"// only UNIX had movemail in 4.x#if defined(XP_UNIX) && !defined(XP_MACOSX)#define HAVE_MOVEMAIL 1#endif /* XP_UNIX */#define PREMIGRATION_PREFIX "premigration."#define ADDRBOOK_FILE_EXTENSION_IN_4X ".na2"#define PREF_FILE_HEADER_STRING "# Mozilla User Preferences " #define MAX_PREF_LEN 1024// this is for the hidden preference setting in mozilla/modules/libpref/src/init/mailnews.js// pref("mail.migration.copyMailFiles", true);//// see bugzilla bug 80035 (http://bugzilla.mozilla.org/show_bug.cgi?id=80035)//// the default value for this setting is true which means when migrating from// Netscape 4.x, mozilla will copy all the contents of Local Folders and Imap// Folder to the newly created subfolders of migrated mozilla profile// when this value is set to false, mozilla will not copy these contents and// still share them with Netscape 4.x//// Advantages of forbidding copy operation:// reduce the disk usage// quick migration// Disadvantage of forbidding copy operation:// without perfect lock mechamism, there is possibility of data corruption// when Netscape 4.x and mozilla run at the same time and access the same// mail file at the same time#define PREF_MIGRATION_MODE_FOR_MAIL "mail.migration.copyMailFiles"#define PREF_MAIL_DIRECTORY "mail.directory"#define PREF_NEWS_DIRECTORY "news.directory"#define PREF_MAIL_IMAP_ROOT_DIR "mail.imap.root_dir"#define PREF_NETWORK_HOSTS_POP_SERVER "network.hosts.pop_server"#define PREF_4X_NETWORK_HOSTS_IMAP_SERVER "network.hosts.imap_servers" #define PREF_MAIL_SERVER_TYPE "mail.server_type"#define PREF_BROWSER_CACHE_DIRECTORY "browser.cache.directory"#define POP_4X_MAIL_TYPE 0#define IMAP_4X_MAIL_TYPE 1#ifdef HAVE_MOVEMAIL#define MOVEMAIL_4X_MAIL_TYPE 2#define NEW_MOVEMAIL_DIR_NAME "movemail"#endif /* HAVE_MOVEMAIL */#if defined(XP_UNIX) && !defined(XP_MACOSX)/* a 4.x profile on UNIX is rooted at something like * "/u/sspitzer/.netscape" * profile + OLD_MAIL_DIR_NAME = "/u/sspitzer/.netscape/../nsmail" = "/u/sspitzer/nsmail" * profile + OLD_NEWS_DIR_NAME = "/u/sspitzer/.netscape/xover-cache" * profile + OLD_IMAPMAIL_DIR_NAME = "/u/sspitzer/.netscape/../ns_imap" = "/u/sspitzer/ns_imap" * which is as good as we're going to get for defaults on UNIX. */#define OLD_MAIL_DIR_NAME "/../nsmail"#define OLD_NEWS_DIR_NAME "/xover-cache"#define OLD_IMAPMAIL_DIR_NAME "/../ns_imap"#else#define OLD_MAIL_DIR_NAME "Mail"#define OLD_NEWS_DIR_NAME "News"#define OLD_IMAPMAIL_DIR_NAME "ImapMail"#endif /* XP_UNIX */#define NEW_MAIL_DIR_NAME "Mail"#define NEW_NEWS_DIR_NAME "News"#define NEW_IMAPMAIL_DIR_NAME "ImapMail"#define NEW_LOCAL_MAIL_DIR_NAME "Local Folders"#define NEW_DIR_SUFFIX "5"#define PREF_FILE_NAME_IN_5x "prefs.js"static PRBool nsCStringEndsWith(nsCString& name, const char *ending);#ifdef NEED_TO_COPY_AND_RENAME_NEWSRC_FILESstatic PRBool nsCStringStartsWith(nsCString& name, const char *starting);#endif///////////////////////////////////////////////////////////////////////////////// nsDogbertProfileMigratorstruct PrefBranchStruct { char* prefName; PRInt32 type; union { char* stringValue; PRInt32 intValue; PRBool boolValue; PRUnichar* wstringValue; };};NS_IMPL_ISUPPORTS2(nsDogbertProfileMigrator, nsIMailProfileMigrator, nsITimerCallback)nsDogbertProfileMigrator::nsDogbertProfileMigrator(){ mObserverService = do_GetService("@mozilla.org/observer-service;1"); mMaxProgress = LL_ZERO; mCurrentProgress = LL_ZERO;}nsDogbertProfileMigrator::~nsDogbertProfileMigrator(){ }///////////////////////////////////////////////////////////////////////////////// nsITimerCallbackNS_IMETHODIMPnsDogbertProfileMigrator::Notify(nsITimer *timer){ CopyNextFolder(); return NS_OK;}void nsDogbertProfileMigrator::CopyNextFolder() { if (mFileCopyTransactionIndex < mFileCopyTransactions->Count()) { PRUint32 percentage = 0; fileTransactionEntry* fileTransaction = (fileTransactionEntry*) mFileCopyTransactions->SafeElementAt(mFileCopyTransactionIndex++); if (fileTransaction) // copy the file { fileTransaction->srcFile->CopyTo(fileTransaction->destFile, fileTransaction->newName); // add to our current progress PRInt64 fileSize; fileTransaction->srcFile->GetFileSize(&fileSize); LL_ADD(mCurrentProgress, mCurrentProgress, fileSize); PRInt64 percentDone; LL_MUL(percentDone, mCurrentProgress, 100); LL_DIV(percentDone, percentDone, mMaxProgress); LL_L2UI(percentage, percentDone); nsAutoString index; index.AppendInt( percentage ); NOTIFY_OBSERVERS(MIGRATION_PROGRESS, index.get()); } // fire a timer to handle the next one. mFileIOTimer = do_CreateInstance("@mozilla.org/timer;1"); if (mFileIOTimer) mFileIOTimer->InitWithCallback(NS_STATIC_CAST(nsITimerCallback *, this), percentage == 100 ? 500 : 0, nsITimer::TYPE_ONE_SHOT); } else EndCopyFolders(); return;}void nsDogbertProfileMigrator::EndCopyFolders() { // clear out the file transaction array if (mFileCopyTransactions) { PRUint32 count = mFileCopyTransactions->Count(); for (PRUint32 i = 0; i < count; ++i) { fileTransactionEntry* fileTransaction = (fileTransactionEntry*) mFileCopyTransactions->ElementAt(i); if (fileTransaction) { fileTransaction->srcFile = nsnull; fileTransaction->destFile = nsnull; delete fileTransaction; } } mFileCopyTransactions->Clear(); delete mFileCopyTransactions; } // notify the UI that we are done with the migration process nsAutoString index; index.AppendInt(nsIMailProfileMigrator::MAILDATA); NOTIFY_OBSERVERS(MIGRATION_ITEMAFTERMIGRATE, index.get()); NOTIFY_OBSERVERS(MIGRATION_ENDED, nsnull);}///////////////////////////////////////////////////////////////////////////////// nsIMailProfileMigratorNS_IMETHODIMPnsDogbertProfileMigrator::Migrate(PRUint16 aItems, nsIProfileStartup* aStartup, const PRUnichar* aProfile){ nsresult rv = NS_OK; PRBool aReplace = aStartup ? PR_TRUE : PR_FALSE; if (!mTargetProfile) { GetProfilePath(aStartup, mTargetProfile); if (!mTargetProfile) return NS_ERROR_FAILURE; } if (!mSourceProfile) GetSourceProfile(aProfile); NOTIFY_OBSERVERS(MIGRATION_STARTED, nsnull); rv = CopyPreferences(); return rv;}NS_IMETHODIMPnsDogbertProfileMigrator::GetMigrateData(const PRUnichar* aProfile, PRBool aReplace, PRUint16* aResult){ // add some extra migration fields for things we also migrate *aResult |= nsIMailProfileMigrator::ACCOUNT_SETTINGS | nsIMailProfileMigrator::MAILDATA | nsIMailProfileMigrator::NEWSDATA | nsIMailProfileMigrator::ADDRESSBOOK_DATA; return NS_OK;}NS_IMETHODIMPnsDogbertProfileMigrator::GetSourceExists(PRBool* aResult){ nsCOMPtr<nsISupportsArray> profiles; GetSourceProfiles(getter_AddRefs(profiles)); if (profiles) { PRUint32 count; profiles->Count(&count); *aResult = count > 0; } else *aResult = PR_FALSE; return NS_OK;}NS_IMETHODIMPnsDogbertProfileMigrator::GetSourceHasMultipleProfiles(PRBool* aResult){ nsCOMPtr<nsISupportsArray> profiles; GetSourceProfiles(getter_AddRefs(profiles)); if (profiles) { PRUint32 count; profiles->Count(&count); *aResult = count > 1; } else *aResult = PR_FALSE; return NS_OK;}#if defined(XP_WIN) || defined(XP_OS2) || defined(XP_MACOSX)NS_IMETHODIMPnsDogbertProfileMigrator::GetSourceProfiles(nsISupportsArray** aResult){ if (!mProfiles) { nsresult rv;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -