nsdogbertprofilemigrator.cpp

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

CPP
1,963
字号
    rv = NS_NewISupportsArray(getter_AddRefs(mProfiles));    NS_ENSURE_SUCCESS(rv, rv);    nsCOMPtr<nsIFile> regFile;    rv = NS_GetSpecialDirectory(OLDREG_DIR, getter_AddRefs(regFile));    NS_ENSURE_SUCCESS(rv, rv);    regFile->AppendNative(NS_LITERAL_CSTRING(OLDREG_NAME));      nsCAutoString path;    rv = regFile->GetNativePath(path);    NS_ENSURE_SUCCESS(rv, rv);    if (NR_StartupRegistry())      return NS_ERROR_FAILURE;    HREG reg = nsnull;    REGENUM enumstate = 0;    if (NR_RegOpen(path.get(), &reg)) {      NR_ShutdownRegistry();      return NS_ERROR_FAILURE;    }    char profileName[MAXREGNAMELEN];    while (!NR_RegEnumSubkeys(reg, ROOTKEY_USERS, &enumstate,                              profileName, MAXREGNAMELEN, REGENUM_CHILDREN)) {      nsCOMPtr<nsISupportsString> nameString        (do_CreateInstance("@mozilla.org/supports-string;1"));      if (nameString) {        nameString->SetData(NS_ConvertUTF8toUTF16(profileName));        mProfiles->AppendElement(nameString);      }    }  }    NS_IF_ADDREF(*aResult = mProfiles);  return NS_OK;}#else // XP_UNIXNS_IMETHODIMPnsDogbertProfileMigrator::GetSourceProfiles(nsISupportsArray** aResult){  nsresult rv;  const char* profileDir  = PR_GetEnv(PROFILE_HOME_ENVIRONMENT_VARIABLE);  if (!profileDir) {    profileDir = PR_GetEnv(HOME_ENVIRONMENT_VARIABLE);  }  if (!profileDir) return NS_ERROR_FAILURE;  nsCAutoString profilePath(profileDir);  profilePath += "/.netscape";  nsCOMPtr<nsILocalFile> profileFile;  rv = NS_NewNativeLocalFile(profilePath, PR_TRUE, getter_AddRefs(profileFile));  NS_ENSURE_SUCCESS(rv, rv);  nsCOMPtr<nsIFile> prefFile;  rv = profileFile->Clone(getter_AddRefs(prefFile));  NS_ENSURE_SUCCESS(rv, rv);  prefFile->AppendNative(NS_LITERAL_CSTRING("preferences.js"));  PRBool exists;  rv = prefFile->Exists(&exists);  if (NS_FAILED(rv) || !exists) {    return NS_ERROR_FAILURE;  }  mSourceProfile = profileFile;  rv = NS_NewISupportsArray(getter_AddRefs(mProfiles));  NS_ENSURE_SUCCESS(rv, rv);  nsCOMPtr<nsISupportsString> nameString    (do_CreateInstance("@mozilla.org/supports-string;1"));  if (!nameString) return NS_ERROR_FAILURE;  nameString->SetData(NS_LITERAL_STRING("Netscape 4.x"));  mProfiles->AppendElement(nameString);  NS_ADDREF(*aResult = mProfiles);  return NS_OK;}#endif // GetSourceProfiles// on win/mac/os2, NS4x uses a registry to determine profile locations#if defined(XP_WIN) || defined(XP_MACOSX) || defined(XP_OS2)void nsDogbertProfileMigrator::GetSourceProfile(const PRUnichar* aProfile){  nsresult rv;  nsCOMPtr<nsIFile> regFile;  rv = NS_GetSpecialDirectory(OLDREG_DIR, getter_AddRefs(regFile));  if (NS_FAILED(rv)) return;  regFile->AppendNative(NS_LITERAL_CSTRING(OLDREG_NAME));    nsCAutoString path;  rv = regFile->GetNativePath(path);  if (NS_FAILED(rv)) return;  if (NR_StartupRegistry())    return;  HREG reg = nsnull;  RKEY profile = nsnull;  if (NR_RegOpen(path.get(), &reg))    goto cleanup;  {    // on macos, registry entries are UTF8 encoded    NS_ConvertUTF16toUTF8 profileName(aProfile);    if (NR_RegGetKey(reg, ROOTKEY_USERS, profileName.get(), &profile))      goto cleanup;  }  char profilePath[MAXPATHLEN];  if (NR_RegGetEntryString(reg, profile, "ProfileLocation", profilePath, MAXPATHLEN))    goto cleanup;  mSourceProfile = do_CreateInstance("@mozilla.org/file/local;1");  if (!mSourceProfile) goto cleanup;  {    // the string is UTF8 encoded, which forces us to do some strange string-do    rv = mSourceProfile->InitWithPath(NS_ConvertUTF8toUTF16(profilePath));  }  if (NS_FAILED(rv))    mSourceProfile = nsnull;cleanup:  if (reg)    NR_RegClose(reg);  NR_ShutdownRegistry();}#elsevoidnsDogbertProfileMigrator::GetSourceProfile(const PRUnichar* aProfile){  // if GetSourceProfiles didn't do its magic, we're screwed}#endifnsresult nsDogbertProfileMigrator::CopyPreferences(){  // Load the source pref file  mPrefs = do_GetService(kPrefServiceCID);  nsCAutoString oldProfDirStr;  nsCAutoString newProfDirStr;  nsCOMPtr<nsILocalFile> sourceProfile = do_QueryInterface(mSourceProfile);  nsCOMPtr<nsILocalFile> targetProfile = do_QueryInterface(mTargetProfile);  sourceProfile->GetPersistentDescriptor(oldProfDirStr);  targetProfile->GetPersistentDescriptor(newProfDirStr);   nsAutoString index;  index.AppendInt(nsIMailProfileMigrator::MAILDATA);   NOTIFY_OBSERVERS(MIGRATION_ITEMBEFOREMIGRATE, index.get());  ProcessPrefsCallback(oldProfDirStr.get(), newProfDirStr.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 NS_OK;}/*-------------------------------------------------------------------------- * ProcessPrefsCallback is the primary funtion for the class nsPrefMigration. * * Called by: The Profile Manager (nsProfile.cpp) * INPUT: The specific profile path (prefPath) and the 5.0 installed path * OUTPUT: The modified 5.0 prefs files * RETURN: Success or a failure code * *-------------------------------------------------------------------------*/nsresultnsDogbertProfileMigrator::ProcessPrefsCallback(const char* oldProfilePathStr, const char * newProfilePathStr){   nsresult rv;    nsCOMPtr<nsIFileSpec> oldProfilePath;  nsCOMPtr<nsIFileSpec> newProfilePath;   nsCOMPtr<nsIFileSpec> oldPOPMailPath;  nsCOMPtr<nsIFileSpec> newPOPMailPath;  nsCOMPtr<nsIFileSpec> oldIMAPMailPath;  nsCOMPtr<nsIFileSpec> newIMAPMailPath;  nsCOMPtr<nsIFileSpec> oldIMAPLocalMailPath;  nsCOMPtr<nsIFileSpec> newIMAPLocalMailPath;  nsCOMPtr<nsIFileSpec> oldNewsPath;  nsCOMPtr<nsIFileSpec> newNewsPath;  nsCOMPtr<nsILocalFile> newPrefsFile;#ifdef HAVE_MOVEMAIL  nsCOMPtr<nsIFileSpec> oldMOVEMAILMailPath;  nsCOMPtr<nsIFileSpec> newMOVEMAILMailPath;#endif /* HAVE_MOVEMAIL */  PRBool exists                  = PR_FALSE,          enoughSpace             = PR_TRUE,         localMailDriveDefault   = PR_FALSE,         summaryMailDriveDefault = PR_FALSE,         newsDriveDefault        = PR_FALSE,         copyMailFileInMigration = PR_TRUE;  nsFileSpec localMailSpec,             summaryMailSpec,             newsSpec,              oldProfileSpec, newProfileSpec;  PRInt32 serverType = POP_4X_MAIL_TYPE;   char *popServerName = nsnull;  PRUint32 totalLocalMailSize = 0,           totalSummaryFileSize = 0,           totalNewsSize = 0,            totalProfileSize = 0,           totalRequired = 0;  PRInt64  localMailDrive   = LL_Zero(),           summaryMailDrive = LL_Zero(),           newsDrive        = LL_Zero(),           profileDrive     = LL_Zero();  PRInt64  DriveID[MAX_DRIVES];  PRUint32 SpaceRequired[MAX_DRIVES];  #if defined(NS_DEBUG)  printf("*Entered Actual Migration routine*\n");#endif  for (int i=0; i < MAX_DRIVES; i++)  {    DriveID[i] = LL_Zero();    SpaceRequired[i] = 0;  }    rv = NS_NewFileSpec(getter_AddRefs(oldProfilePath));  if (NS_FAILED(rv)) return rv;  rv = NS_NewFileSpec(getter_AddRefs(newProfilePath));  if (NS_FAILED(rv)) return rv;        rv = ConvertPersistentStringToFileSpec(oldProfilePathStr, oldProfilePath);  if (NS_FAILED(rv)) return rv;  rv = ConvertPersistentStringToFileSpec(newProfilePathStr, newProfilePath);  if (NS_FAILED(rv)) return rv;  oldProfilePath->GetFileSpec(&oldProfileSpec);  newProfilePath->GetFileSpec(&newProfileSpec);    /* initialize prefs with the old prefs.js file (which is a copy of the 4.x preferences file) */  nsCOMPtr<nsIFileSpec> PrefsFile4x;  //Get the location of the 4.x prefs file  rv = NS_NewFileSpec(getter_AddRefs(PrefsFile4x));  if (NS_FAILED(rv)) return rv;    rv = PrefsFile4x->FromFileSpec(oldProfilePath);  if (NS_FAILED(rv)) return rv;  rv = PrefsFile4x->AppendRelativeUnixPath(PREF_FILE_NAME_IN_4x);  if (NS_FAILED(rv)) return rv;  //Need to convert PrefsFile4x to an IFile in order to copy it to a   //unique name in the system temp directory.  nsFileSpec PrefsFile4xAsFileSpec;  rv = PrefsFile4x->GetFileSpec(&PrefsFile4xAsFileSpec);  if (NS_FAILED(rv)) return rv;    nsCOMPtr<nsILocalFile> PrefsFile4xAsIFile;  rv = NS_FileSpecToIFile(&PrefsFile4xAsFileSpec,                     getter_AddRefs(PrefsFile4xAsIFile));  if (NS_FAILED(rv)) return rv;  nsCOMPtr<nsIFile> systemTempDir;  rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(systemTempDir));  if (NS_FAILED(rv)) return rv;  systemTempDir->AppendNative(NS_LITERAL_CSTRING("migrate"));    //Create a unique directory in the system temp dir based on the name of the 4.x prefs file  rv = systemTempDir->CreateUnique(nsIFile::DIRECTORY_TYPE, 0700);   if (NS_FAILED(rv)) return rv;  rv = PrefsFile4xAsIFile->CopyToNative(systemTempDir, NS_LITERAL_CSTRING(PREF_FILE_NAME_IN_4x));  if (NS_FAILED(rv)) return rv;    nsCOMPtr<nsIFile> cloneFile;  rv = systemTempDir->Clone(getter_AddRefs(cloneFile));  if (NS_FAILED(rv)) return rv;  m_prefsFile = do_QueryInterface(cloneFile, &rv);  if (NS_FAILED(rv)) return rv;  rv = m_prefsFile->AppendNative(NS_LITERAL_CSTRING(PREF_FILE_NAME_IN_4x));  if (NS_FAILED(rv)) return rv;  //Clear the prefs in case a previous set was read in.  mPrefs->ResetPrefs();  //Now read the prefs from the prefs file in the system directory  mPrefs->ReadUserPrefs(m_prefsFile);  // Start computing the sizes required for migration  //  rv = GetSizes(oldProfileSpec, PR_FALSE, &totalProfileSize);  profileDrive = newProfileSpec.GetDiskSpaceAvailable();  rv = mPrefs->GetIntPref(PREF_MAIL_SERVER_TYPE, &serverType);  if (NS_FAILED(rv)) return rv;  // get the migration mode for mail  rv = mPrefs->GetBoolPref(PREF_MIGRATION_MODE_FOR_MAIL, &copyMailFileInMigration);  if (NS_FAILED(rv))    return rv;  if (serverType == POP_4X_MAIL_TYPE) {    summaryMailDriveDefault = PR_TRUE; //summary files are only used in IMAP so just set it to true here.    summaryMailDrive = profileDrive;   //just set the drive for summary files to be the same as the new profile    rv = NS_NewFileSpec(getter_AddRefs(newPOPMailPath));    if (NS_FAILED(rv)) return rv;    rv = NS_NewFileSpec(getter_AddRefs(oldPOPMailPath));    if (NS_FAILED(rv)) return rv;        rv = GetDirFromPref(oldProfilePath,newProfilePath,NEW_MAIL_DIR_NAME, PREF_MAIL_DIRECTORY, newPOPMailPath, oldPOPMailPath);    if (NS_FAILED(rv)) {      rv = DetermineOldPath(oldProfilePath, OLD_MAIL_DIR_NAME, "mailDirName", oldPOPMailPath);      if (NS_FAILED(rv)) return rv;      rv = SetPremigratedFilePref(PREF_MAIL_DIRECTORY, oldPOPMailPath);      if (NS_FAILED(rv)) return rv;      rv = newPOPMailPath->FromFileSpec(newProfilePath);      if (NS_FAILED(rv)) return rv;      localMailDriveDefault = PR_TRUE;    }    oldPOPMailPath->GetFileSpec(&localMailSpec);    rv = GetSizes(localMailSpec, PR_TRUE, &totalLocalMailSize);    localMailDrive = localMailSpec.GetDiskSpaceAvailable();  }  else if(serverType == IMAP_4X_MAIL_TYPE) {    rv = NS_NewFileSpec(getter_AddRefs(newIMAPLocalMailPath));    if (NS_FAILED(rv)) return rv;          rv = NS_NewFileSpec(getter_AddRefs(oldIMAPLocalMailPath));    if (NS_FAILED(rv)) return rv;            /* First get the actual 4.x "Local Mail" files location */    rv = GetDirFromPref(oldProfilePath,newProfilePath, NEW_MAIL_DIR_NAME, PREF_MAIL_DIRECTORY, newIMAPLocalMailPath, oldIMAPLocalMailPath);    if (NS_FAILED(rv)) {      rv = DetermineOldPath(oldProfilePath, OLD_MAIL_DIR_NAME, "mailDirName", oldIMAPLocalMailPath);      if (NS_FAILED(rv)) return rv;      rv = SetPremigratedFilePref(PREF_MAIL_DIRECTORY, oldIMAPLocalMailPath);      if (NS_FAILED(rv)) return rv;      rv = newIMAPLocalMailPath->FromFileSpec(newProfilePath);      if (NS_FAILED(rv)) return rv;            localMailDriveDefault = PR_TRUE;    }    oldIMAPLocalMailPath->GetFileSpec(&localMailSpec);    rv = GetSizes(localMailSpec, PR_TRUE, &totalLocalMailSize);    localMailDrive = localMailSpec.GetDiskSpaceAvailable();    /* Next get IMAP mail summary files location */    rv = NS_NewFileSpec(getter_AddRefs(newIMAPMailPath));    if (NS_FAILED(rv)) return rv;

⌨️ 快捷键说明

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