nsseamonkeyprofilemigrator.cpp

来自「现在很火的邮件客户端软件thunderbird的源码」· C++ 代码 · 共 934 行 · 第 1/3 页

CPP
934
字号
/* -*- 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 Ben Goodger. * Portions created by the Initial Developer are Copyright (C) 2004 * the Initial Developer. All Rights Reserved. * * Contributor(s): *  Ben Goodger <ben@bengoodger.com> *  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 ***** */#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 "nsSeamonkeyProfileMigrator.h"#include "nsIRelativeFilePref.h"#include "nsAppDirectoryServiceDefs.h"#include "prprf.h"#include "nsVoidArray.h"static PRUint32 StringHash(const char *ubuf);nsresult NS_MsgHashIfNecessary(nsCString &name);// Mail specific folder paths#define MAIL_DIR_50_NAME             NS_LITERAL_STRING("Mail")#define IMAP_MAIL_DIR_50_NAME        NS_LITERAL_STRING("ImapMail")#define NEWS_DIR_50_NAME             NS_LITERAL_STRING("News")///////////////////////////////////////////////////////////////////////////////// nsSeamonkeyProfileMigrator#define FILE_NAME_JUNKTRAINING    NS_LITERAL_STRING("training.dat")#define FILE_NAME_PERSONALDICTIONARY NS_LITERAL_STRING("persdict.dat")#define FILE_NAME_PERSONAL_ADDRESSBOOK NS_LITERAL_STRING("abook.mab")#define FILE_NAME_MAILVIEWS       NS_LITERAL_STRING("mailviews.dat")#define FILE_NAME_CERT8DB         NS_LITERAL_STRING("cert8.db")#define FILE_NAME_KEY3DB          NS_LITERAL_STRING("key3.db")#define FILE_NAME_SECMODDB        NS_LITERAL_STRING("secmod.db")#define FILE_NAME_MIMETYPES       NS_LITERAL_STRING("mimeTypes.rdf")#define FILE_NAME_PREFS           NS_LITERAL_STRING("prefs.js")#define FILE_NAME_USER_PREFS      NS_LITERAL_STRING("user.js")struct PrefBranchStruct {  char*         prefName;  PRInt32       type;  union {    char*       stringValue;    PRInt32     intValue;    PRBool      boolValue;    PRUnichar*  wstringValue;  };};NS_IMPL_ISUPPORTS2(nsSeamonkeyProfileMigrator, nsIMailProfileMigrator, nsITimerCallback)nsSeamonkeyProfileMigrator::nsSeamonkeyProfileMigrator(){  mObserverService = do_GetService("@mozilla.org/observer-service;1");  mMaxProgress = LL_ZERO;  mCurrentProgress = LL_ZERO;}nsSeamonkeyProfileMigrator::~nsSeamonkeyProfileMigrator(){           }///////////////////////////////////////////////////////////////////////////////// nsITimerCallbackNS_IMETHODIMPnsSeamonkeyProfileMigrator::Notify(nsITimer *timer){  CopyNextFolder();  return NS_OK;}void nsSeamonkeyProfileMigrator::CopyNextFolder() {  if (mFileCopyTransactionIndex < mFileCopyTransactions->Count())  {    PRUint32 percentage = 0;    fileTransactionEntry* fileTransaction = (fileTransactionEntry*) mFileCopyTransactions->SafeElementAt(mFileCopyTransactionIndex++);    if (fileTransaction) // copy the file    {      fileTransaction->srcFile->CopyTo(fileTransaction->destFile, EmptyString());      // 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 nsSeamonkeyProfileMigrator::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_IMETHODIMPnsSeamonkeyProfileMigrator::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);  COPY_DATA(CopyPreferences,  aReplace, nsIMailProfileMigrator::SETTINGS);  // fake notifications for things we've already imported as part of CopyPreferences  COPY_DATA(DummyCopyRoutine, aReplace, nsIMailProfileMigrator::ACCOUNT_SETTINGS);    COPY_DATA(DummyCopyRoutine, aReplace, nsIMailProfileMigrator::NEWSDATA);  // copy junk mail training file  COPY_DATA(CopyJunkTraining, aReplace, nsIMailProfileMigrator::JUNKTRAINING);  COPY_DATA(CopyPasswords,    aReplace, nsIMailProfileMigrator::PASSWORDS);  // the last thing to do is to actually copy over any mail folders we have marked for copying  // we want to do this last and it will be asynchronous so the UI doesn't freeze up while we perform   // this potentially very long operation.     nsAutoString index;  index.AppendInt(nsIMailProfileMigrator::MAILDATA);   NOTIFY_OBSERVERS(MIGRATION_ITEMBEFOREMIGRATE, index.get());  // Generate the max progress value now that we know all of the files we need to copy  PRUint32 count = mFileCopyTransactions->Count();  for (PRUint32 i = 0; i < count; ++i)   {    fileTransactionEntry* fileTransaction = (fileTransactionEntry*) mFileCopyTransactions->ElementAt(i);    if (fileTransaction)    {      PRInt64 fileSize;       fileTransaction->srcFile->GetFileSize(&fileSize);      LL_ADD(mMaxProgress, mMaxProgress, fileSize);    }  }  CopyNextFolder();  return rv;}NS_IMETHODIMPnsSeamonkeyProfileMigrator::GetMigrateData(const PRUnichar* aProfile,                                            PRBool aReplace,                                            PRUint16* aResult){  *aResult = 0;  if (!mSourceProfile) {    GetSourceProfile(aProfile);    if (!mSourceProfile)      return NS_ERROR_FILE_NOT_FOUND;  }  MigrationData data[] = { { ToNewUnicode(FILE_NAME_PREFS),                             nsIMailProfileMigrator::SETTINGS,                             PR_TRUE },                           { ToNewUnicode(FILE_NAME_JUNKTRAINING),                             nsIMailProfileMigrator::JUNKTRAINING,                             PR_TRUE },                          };                                                                    // Frees file name strings allocated above.  GetMigrateDataFromArray(data, sizeof(data)/sizeof(MigrationData),                           aReplace, mSourceProfile, aResult);  // Now locate passwords  nsXPIDLCString signonsFileName;  GetSignonFileName(aReplace, getter_Copies(signonsFileName));  if (!signonsFileName.IsEmpty()) {    nsAutoString fileName; fileName.AssignWithConversion(signonsFileName);    nsCOMPtr<nsIFile> sourcePasswordsFile;    mSourceProfile->Clone(getter_AddRefs(sourcePasswordsFile));    sourcePasswordsFile->Append(fileName);        PRBool exists;    sourcePasswordsFile->Exists(&exists);    if (exists)      *aResult |= nsIMailProfileMigrator::PASSWORDS;  }  // 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_IMETHODIMPnsSeamonkeyProfileMigrator::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_IMETHODIMPnsSeamonkeyProfileMigrator::GetSourceHasMultipleProfiles(PRBool* aResult){  nsCOMPtr<nsISupportsArray> profiles;  GetSourceProfiles(getter_AddRefs(profiles));  if (profiles) {    PRUint32 count;    profiles->Count(&count);    *aResult = count > 1;  }  else

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?