?? nsdogbertprofilemigrator.cpp
字號(hào):
nsCOMPtr<nsIFileSpec> oldSubPath; NS_NewFileSpec(getter_AddRefs(oldSubPath)); oldSubPath->FromFileSpec(oldPath); rv = oldSubPath->AppendRelativeUnixPath(fileOrDirName); if (NS_FAILED(rv)) return rv; PRBool exist; rv = oldSubPath->Exists(&exist); if (NS_FAILED(rv)) return rv; if (!exist) { rv = oldSubPath->CreateDir(); if (NS_FAILED(rv)) return rv; } nsCOMPtr<nsIFileSpec> newSubPath; NS_NewFileSpec(getter_AddRefs(newSubPath)); newSubPath->FromFileSpec(newPath); rv = newSubPath->AppendRelativeUnixPath(fileOrDirName); if (NS_FAILED(rv)) return rv; rv = newSubPath->Exists(&exist); if (NS_FAILED(rv)) return rv; if (!exist) { rv = newSubPath->CreateDir(); if (NS_FAILED(rv)) return rv; } DoTheCopy(oldSubPath, newSubPath, PR_TRUE); } else { nsCOMPtr<nsIFileSpec> file; NS_NewFileSpec(getter_AddRefs(file)); file->FromFileSpec(oldPath); rv = file->AppendRelativeUnixPath(fileOrDirName); if( NS_FAILED(rv) ) return rv; PRBool exist; rv = file->Exists(&exist); if( NS_FAILED(rv) ) return rv; if( exist) { // convert back to nsFileSpec nsFileSpec oldPath; nsFileSpec newPathSpec; file->GetFileSpec(&oldPath); newPath->GetFileSpec(&newPathSpec); AddFileCopyToList(&oldPath, &newPathSpec, ""); } } return rv;}/*---------------------------------------------------------------------------- * DoSpecialUpdates updates is a routine that does some miscellaneous updates * like renaming certain files, etc. *--------------------------------------------------------------------------*/nsresult nsDogbertProfileMigrator::DoSpecialUpdates(nsIFileSpec * profilePath){ nsresult rv; PRInt32 serverType; nsFileSpec fs; rv = profilePath->GetFileSpec(&fs); if (NS_FAILED(rv)) return rv; fs += PREF_FILE_NAME_IN_5x; nsOutputFileStream fsStream(fs, (PR_WRONLY | PR_CREATE_FILE | PR_APPEND)); if (!fsStream.is_open()) { return NS_ERROR_FAILURE; } /* Need to add a string to the top of the prefs.js file to prevent it * from being loaded as a standard javascript file which would be a * security hole. */ fsStream << PREF_FILE_HEADER_STRING << nsEndl ; fsStream.close(); /* Create the new mail directory from the setting in prefs.js or a default */ rv = mPrefs->GetIntPref(PREF_MAIL_SERVER_TYPE, &serverType); if (NS_FAILED(rv)) return rv; if (serverType == POP_4X_MAIL_TYPE) { rv = RenameAndMove4xPopFilterFile(profilePath); if (NS_FAILED(rv)) return rv; rv = RenameAndMove4xPopStateFile(profilePath); if (NS_FAILED(rv)) return rv; }#ifdef IMAP_MAIL_FILTER_FILE_NAME_FORMAT_IN_4x else if (serverType == IMAP_4X_MAIL_TYPE) { rv = RenameAndMove4xImapFilterFiles(profilePath); if (NS_FAILED(rv)) return rv; }#endif /* IMAP_MAIL_FILTER_FILE_NAME_FORMAT_IN_4x */ return rv;}nsresult nsDogbertProfileMigrator::RenameAndMove4xPopFilterFile(nsIFileSpec * profilePath){ return RenameAndMove4xPopFile(profilePath, POP_MAIL_FILTER_FILE_NAME_IN_4x, POP_MAIL_FILTER_FILE_NAME_IN_5x);}nsresult nsDogbertProfileMigrator::RenameAndMove4xPopStateFile(nsIFileSpec * profilePath){#ifdef POPSTATE_FILE_IN_4x return RenameAndMove4xPopFile(profilePath, POPSTATE_FILE_IN_4x, POPSTATE_FILE_IN_5x);#else // on windows, popstate.dat was in Users\<profile>\MAIL\popstate.dat // which is the right place, unlike linux and mac. // so, when we migrate Users\<profile>\Mail to Users50\<profile>\Mail\<hostname> // it just works return NS_OK;#endif /* POPSTATE_FILE_IN_4x */}nsresult nsDogbertProfileMigrator::RenameAndMove4xPopFile(nsIFileSpec * profilePath, const char *fileNameIn4x, const char *fileNameIn5x){ nsFileSpec file; nsresult rv = profilePath->GetFileSpec(&file); if (NS_FAILED(rv)) return rv; // we assume the 4.x pop files live at <profile>/<fileNameIn4x> file += fileNameIn4x; // figure out where the 4.x pop mail directory got copied to char *popServerName = nsnull; nsFileSpec migratedPopDirectory; rv = profilePath->GetFileSpec(&migratedPopDirectory); migratedPopDirectory += NEW_MAIL_DIR_NAME; mPrefs->CopyCharPref(PREF_NETWORK_HOSTS_POP_SERVER, &popServerName); migratedPopDirectory += popServerName; PR_FREEIF(popServerName); // copy the 4.x file from <profile>/<fileNameIn4x> to the <profile>/Mail/<hostname>/<fileNameIn4x> rv = file.CopyToDir(migratedPopDirectory); NS_ASSERTION(NS_SUCCEEDED(rv),"failed to copy pop file"); // XXX todo, delete the old file // we are leaving it behind // make migratedPopDirectory point the the copied filter file, // <profile>/Mail/<hostname>/<fileNameIn4x> migratedPopDirectory += fileNameIn4x; // rename <profile>/Mail/<hostname>/<fileNameIn4x>to <profile>/Mail/<hostname>/<fileNameIn5x>, if necessary if (PL_strcmp(fileNameIn4x,fileNameIn5x)) { migratedPopDirectory.Rename(fileNameIn5x); } return NS_OK;}#ifdef IMAP_MAIL_FILTER_FILE_NAME_FORMAT_IN_4x#define BUFFER_LEN 128nsresult nsDogbertProfileMigrator::RenameAndMove4xImapFilterFile(nsIFileSpec * profilePath, const char *hostname){ nsresult rv = NS_OK; char imapFilterFileName[BUFFER_LEN]; // the 4.x imap filter file lives in "<profile>/<hostname> Rules" nsFileSpec file; rv = profilePath->GetFileSpec(&file); if (NS_FAILED(rv)) return rv; PR_snprintf(imapFilterFileName, BUFFER_LEN, IMAP_MAIL_FILTER_FILE_NAME_FORMAT_IN_4x, hostname); file += imapFilterFileName; // if that file didn't exist, because they didn't use filters for that server, return now if (!file.Exists()) return NS_OK; // figure out where the 4.x pop mail directory got copied to nsFileSpec migratedImapDirectory; rv = profilePath->GetFileSpec(&migratedImapDirectory); migratedImapDirectory += NEW_IMAPMAIL_DIR_NAME; migratedImapDirectory += hostname; // copy the 4.x file from "<profile>/<hostname> Rules" to <profile>/ImapMail/<hostname>/ rv = file.CopyToDir(migratedImapDirectory); NS_ASSERTION(NS_SUCCEEDED(rv),"failed to copy imap file"); // make migratedPopDirectory point the the copied filter file, // "<profile>/ImapMail/<hostname>/<hostname> Rules" migratedImapDirectory += imapFilterFileName; // rename "<profile>/ImapMail/<hostname>/<hostname> Rules" to "<profile>/ImapMail/<hostname>/rules.dat" migratedImapDirectory.Rename(IMAP_MAIL_FILTER_FILE_NAME_IN_5x); return NS_OK; }nsresult nsDogbertProfileMigrator::RenameAndMove4xImapFilterFiles(nsIFileSpec * profilePath){ nsresult rv; char *hostList=nsnull; rv = mPrefs->CopyCharPref(PREF_4X_NETWORK_HOSTS_IMAP_SERVER, &hostList); if (NS_FAILED(rv)) return rv; if (!hostList || !*hostList) return NS_OK; char *token = nsnull; char *rest = hostList; nsCAutoString str; token = nsCRT::strtok(rest, ",", &rest); while (token && *token) { str = token; str.StripWhitespace(); if (!str.IsEmpty()) { // str is the hostname rv = RenameAndMove4xImapFilterFile(profilePath,str.get()); if (NS_FAILED(rv)) { // failed to migrate. bail. return rv; } str = ""; } token = nsCRT::strtok(rest, ",", &rest); } PR_FREEIF(hostList); return NS_OK; }#endif /* IMAP_MAIL_FILTER_FILE_NAME_FORMAT_IN_4x */nsresultnsDogbertProfileMigrator::Rename4xFileAfterMigration(nsIFileSpec * profilePath, const char *oldFileName, const char *newFileName){ nsresult rv = NS_OK; // if they are the same, don't bother to rename the file. if (PL_strcmp(oldFileName, newFileName) == 0) { return rv; } nsFileSpec file; rv = profilePath->GetFileSpec(&file); if (NS_FAILED(rv)) return rv; file += oldFileName; // make sure it exists before you try to rename it if (file.Exists()) { file.Rename(newFileName); } return rv;}#ifdef NEED_TO_COPY_AND_RENAME_NEWSRC_FILESnsresult nsDogbertProfileMigrator::GetPremigratedFilePref(const char *pref_name, nsIFileSpec **path){ nsresult rv; if (!pref_name) return NS_ERROR_FAILURE; char premigration_pref[MAX_PREF_LEN]; PR_snprintf(premigration_pref,MAX_PREF_LEN,"%s%s",PREMIGRATION_PREFIX,pref_name); rv = mPrefs->GetFilePref((const char *)premigration_pref, path); return rv;}#endif /* NEED_TO_COPY_AND_RENAME_NEWSRC_FILES */nsresult nsDogbertProfileMigrator::DetermineOldPath(nsIFileSpec *profilePath, const char *oldPathName, const char *oldPathEntityName, nsIFileSpec *oldPath){ nsresult rv; /* set oldLocalFile to profilePath. need to convert nsIFileSpec->nsILocalFile */ nsCOMPtr<nsILocalFile> oldLocalFile; nsFileSpec pathSpec; profilePath->GetFileSpec(&pathSpec); rv = NS_FileSpecToIFile(&pathSpec, getter_AddRefs(oldLocalFile)); if (NS_FAILED(rv)) return rv; /* get the string bundle, and get the appropriate localized string out of it */ nsCOMPtr<nsIStringBundleService> bundleService = do_GetService(kStringBundleServiceCID, &rv); if (NS_FAILED(rv)) return rv; nsCOMPtr<nsIStringBundle> bundle; rv = bundleService->CreateBundle(MIGRATION_PROPERTIES_URL, getter_AddRefs(bundle)); if (NS_FAILED(rv)) return rv; nsXPIDLString localizedDirName; nsAutoString entityName; entityName.AssignWithConversion(oldPathEntityName); rv = bundle->GetStringFromName(entityName.get(), getter_Copies(localizedDirName)); if (NS_FAILED(rv)) return rv; rv = oldLocalFile->AppendRelativePath(localizedDirName); if (NS_FAILED(rv)) return rv; PRBool exists = PR_FALSE; rv = oldLocalFile->Exists(&exists); if (!exists) { /* if the localized name doesn't exist, use the english name */ rv = oldPath->FromFileSpec(profilePath); if (NS_FAILED(rv)) return rv; rv = oldPath->AppendRelativeUnixPath(oldPathName); if (NS_FAILED(rv)) return rv; return NS_OK; } /* at this point, the folder with the localized name exists, so use it */ nsCAutoString persistentDescriptor; rv = oldLocalFile->GetPersistentDescriptor(persistentDescriptor); if (NS_FAILED(rv)) return rv; rv = oldPath->SetPersistentDescriptorString(persistentDescriptor.get()); if (NS_FAILED(rv)) return rv; return NS_OK;}nsresult nsDogbertProfileMigrator::ConvertPersistentStringToFileSpec(const char *str, nsIFileSpec *path){ nsresult rv; if (!str || !path) return NS_ERROR_NULL_POINTER; rv = path->SetPersistentDescriptorString(str); return rv;}/*--------------------------------------------------------------------------------- * GetSizes reads the 4.x files in the profile tree and accumulates their sizes *--------------------------------------------------------------------------------*/nsresult nsDogbertProfileMigrator::GetSizes(nsFileSpec inputPath, PRBool readSubdirs, PRUint32 *sizeTotal){ char* folderName; nsCAutoString fileOrDirNameStr; for (nsDirectoryIterator dir(inputPath, PR_FALSE); dir.Exists(); dir++) { nsFileSpec fileOrDirName = dir.Spec(); folderName = fileOrDirName.GetLeafName(); fileOrDirNameStr.Assign(folderName); if (nsCStringEndsWith(fileOrDirNameStr, MAIL_SUMMARY_SUFFIX_IN_4x) || nsCStringEndsWith(fileOrDirNameStr, NEWS_SUMMARY_SUFFIX_IN_4x) || nsCStringEndsWith(fileOrDirNameStr, SUMMARY_SUFFIX_IN_5x)) /* Don't copy the summary files */ continue; else { if (fileOrDirName.IsDirectory()) { if(readSubdirs) { GetSizes(fileOrDirName, PR_TRUE, sizeTotal); /* re-enter the GetSizes function */ } else continue; } else *sizeTotal += fileOrDirName.GetFileSize(); } } return NS_OK;}/*--------------------------------------------------------------------------------- * GetDirFromPref gets a directory based on a preference set in the 4.x * preferences file, adds a 5 and resets the preference. * * INPUT: * oldProfilePath - the path to the old 4.x profile directory. * currently only used by UNIX * * newProfilePath - the path to the 5.0 profile directory * currently only used by UNIX * * newDirName - the leaf name of the directory in the 5.0 world that corresponds to * this pref. Examples: "Mail", "ImapMail", "News". * only used on UNIX. * * pref - the pref in the "dot" format (e.g. mail.directory) * * OUTPUT: newPath - The old path with a 5 added (on mac and windows) * the newProfilePath + "/" + newDirName (on UNIX) * oldPath - The old path from the pref (if any) * * * RETURNS: NS_OK if the pref was successfully pulled from the prefs file * *--------------------------------------------------------------------------------*/nsresultnsDogbertProfileMigrator::GetDirFromPref(nsIFileSpec * oldProfilePath, nsIFileSpec * newProfilePath, const char *newDirName, const char* pref, nsIFileSpec* newPath, n
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -