?? nsseamonkeyprofilemigrator.cpp
字號:
prefName.Cut( prefName.Length() - strlen("directory"), strlen("directory")); prefName.Insert("mail.server.", 0); nsCOMPtr<nsIPrefBranch> serverBranch; aPrefService->GetBranch(prefName.get(), getter_AddRefs(serverBranch)); if (!serverBranch) break; // should we clear out this server pref from aMailServers? nsXPIDLCString serverType; serverBranch->GetCharPref("type", getter_Copies(serverType)); nsCOMPtr<nsILocalFile> sourceMailFolder; serverBranch->GetComplexValue("directory", NS_GET_IID(nsILocalFile), getter_AddRefs(sourceMailFolder)); // now based on type, we need to build a new destination path for the mail folders for this server nsCOMPtr<nsIFile> targetMailFolder; if (serverType.Equals("imap")) { mTargetProfile->Clone(getter_AddRefs(targetMailFolder)); targetMailFolder->Append(IMAP_MAIL_DIR_50_NAME); } else if (serverType.Equals("none") || serverType.Equals("pop3")) { // local folders and POP3 servers go under <profile>\Mail mTargetProfile->Clone(getter_AddRefs(targetMailFolder)); targetMailFolder->Append(MAIL_DIR_50_NAME); } else if (serverType.Equals("nntp")) { mTargetProfile->Clone(getter_AddRefs(targetMailFolder)); targetMailFolder->Append(NEWS_DIR_50_NAME); nsCAutoString alteredHost; alteredHost = "host-"; nsXPIDLCString hostName; serverBranch->GetCharPref("hostname", getter_Copies(hostName)); alteredHost += hostName; NS_MsgHashIfNecessary(alteredHost); targetMailFolder->Append(NS_ConvertASCIItoUCS2(alteredHost)); } if (targetMailFolder) { // for all of our server types, append the host name to the directory as part of the new location if (!serverType.Equals("nntp")) { nsXPIDLCString hostName; serverBranch->GetCharPref("hostname", getter_Copies(hostName)); targetMailFolder->Append(NS_ConvertASCIItoUCS2(hostName)); // we should make sure the host name based directory we are going to migrate // the accounts into is unique. This protects against the case where the user // has multiple servers with the same host name. targetMailFolder->CreateUnique(nsIFile::DIRECTORY_TYPE, 0777); } rv = RecursiveCopy(sourceMailFolder, targetMailFolder); // now we want to make sure the actual directory pref that gets transformed // into the new profile's pref.js has the right file location. nsCAutoString descriptorString; nsCOMPtr<nsILocalFile> localFile = do_QueryInterface(targetMailFolder); localFile->GetPersistentDescriptor(descriptorString); nsCRT::free(pref->stringValue); pref->stringValue = ToNewCString(descriptorString); } } else if (StringEndsWith(prefName, nsDependentCString(".newsrc.file"))) { // copy the news RC file into \News. this won't work if the user has different newsrc files for each account // I don't know what to do in that situation. nsCOMPtr<nsIFile> targetNewsRCFile; mTargetProfile->Clone(getter_AddRefs(targetNewsRCFile)); targetNewsRCFile->Append(NEWS_DIR_50_NAME); // turn the pref into a nsILocalFile nsCOMPtr<nsILocalFile> srcNewsRCFile = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID); srcNewsRCFile->SetPersistentDescriptor(nsDependentCString(pref->stringValue)); // now make the copy PRBool exists; srcNewsRCFile->Exists(&exists); if (exists) { nsAutoString leafName; srcNewsRCFile->GetLeafName(leafName); srcNewsRCFile->CopyTo(targetNewsRCFile,leafName); // will fail if we've already copied a newsrc file here targetNewsRCFile->Append(leafName); // now write out the new descriptor nsCAutoString descriptorString; nsCOMPtr<nsILocalFile> localFile = do_QueryInterface(targetNewsRCFile); localFile->GetPersistentDescriptor(descriptorString); nsCRT::free(pref->stringValue); pref->stringValue = ToNewCString(descriptorString); } } } return NS_OK;}nsresultnsSeamonkeyProfileMigrator::CopyPreferences(PRBool aReplace){ nsresult rv = NS_OK; if (!aReplace) return rv; rv |= TransformPreferences(FILE_NAME_PREFS, FILE_NAME_PREFS); rv |= CopyFile(FILE_NAME_USER_PREFS, FILE_NAME_USER_PREFS); // Security Stuff rv |= CopyFile(FILE_NAME_CERT8DB, FILE_NAME_CERT8DB); rv |= CopyFile(FILE_NAME_KEY3DB, FILE_NAME_KEY3DB); rv |= CopyFile(FILE_NAME_SECMODDB, FILE_NAME_SECMODDB); // User MIME Type overrides rv |= CopyFile(FILE_NAME_MIMETYPES, FILE_NAME_MIMETYPES); rv |= CopyFile(FILE_NAME_PERSONALDICTIONARY, FILE_NAME_PERSONALDICTIONARY); rv |= CopyFile(FILE_NAME_MAILVIEWS, FILE_NAME_MAILVIEWS); return rv;}void nsSeamonkeyProfileMigrator::ReadBranch(const char * branchName, nsIPrefService* aPrefService, nsVoidArray* aPrefs){ // Enumerate the branch nsCOMPtr<nsIPrefBranch> branch; aPrefService->GetBranch(branchName, getter_AddRefs(branch)); PRUint32 count; char** prefs = nsnull; nsresult rv = branch->GetChildList("", &count, &prefs); if (NS_FAILED(rv)) return; for (PRUint32 i = 0; i < count; ++i) { // Save each pref's value into an array char* currPref = prefs[i]; PRInt32 type; branch->GetPrefType(currPref, &type); PrefBranchStruct* pref = new PrefBranchStruct; pref->prefName = currPref; pref->type = type; switch (type) { case nsIPrefBranch::PREF_STRING: rv = branch->GetCharPref(currPref, &pref->stringValue); break; case nsIPrefBranch::PREF_BOOL: rv = branch->GetBoolPref(currPref, &pref->boolValue); break; case nsIPrefBranch::PREF_INT: rv = branch->GetIntPref(currPref, &pref->intValue); break; case nsIPrefBranch::PREF_INVALID: { nsCOMPtr<nsIPrefLocalizedString> str; rv = branch->GetComplexValue(currPref, NS_GET_IID(nsIPrefLocalizedString), getter_AddRefs(str)); if (NS_SUCCEEDED(rv) && str) str->ToString(&pref->wstringValue); } break; } if (NS_SUCCEEDED(rv)) aPrefs->AppendElement((void*)pref); }}voidnsSeamonkeyProfileMigrator::WriteBranch(const char * branchName, nsIPrefService* aPrefService, nsVoidArray* aPrefs){ nsresult rv; // Enumerate the branch nsCOMPtr<nsIPrefBranch> branch; aPrefService->GetBranch(branchName, getter_AddRefs(branch)); PRUint32 count = aPrefs->Count(); for (PRUint32 i = 0; i < count; ++i) { PrefBranchStruct* pref = (PrefBranchStruct*)aPrefs->ElementAt(i); switch (pref->type) { case nsIPrefBranch::PREF_STRING: rv = branch->SetCharPref(pref->prefName, pref->stringValue); nsCRT::free(pref->stringValue); pref->stringValue = nsnull; break; case nsIPrefBranch::PREF_BOOL: rv = branch->SetBoolPref(pref->prefName, pref->boolValue); break; case nsIPrefBranch::PREF_INT: rv = branch->SetIntPref(pref->prefName, pref->intValue); break; case nsIPrefBranch::PREF_INVALID: nsCOMPtr<nsIPrefLocalizedString> pls(do_CreateInstance("@mozilla.org/pref-localizedstring;1")); pls->SetData(pref->wstringValue); rv = branch->SetComplexValue(pref->prefName, NS_GET_IID(nsIPrefLocalizedString), pls); nsCRT::free(pref->wstringValue); pref->wstringValue = nsnull; break; } nsCRT::free(pref->prefName); pref->prefName = nsnull; delete pref; pref = nsnull; } aPrefs->Clear();}nsresult nsSeamonkeyProfileMigrator::DummyCopyRoutine(PRBool aReplace){ // place holder function only to fake the UI out into showing some migration process. return NS_OK;}nsresultnsSeamonkeyProfileMigrator::CopyJunkTraining(PRBool aReplace){ return aReplace ? CopyFile(FILE_NAME_JUNKTRAINING, FILE_NAME_JUNKTRAINING) : NS_OK;}nsresultnsSeamonkeyProfileMigrator::CopyPasswords(PRBool aReplace){ nsresult rv; nsXPIDLCString signonsFileName; GetSignonFileName(aReplace, getter_Copies(signonsFileName)); if (signonsFileName.IsEmpty()) return NS_ERROR_FILE_NOT_FOUND; nsAutoString fileName; fileName.AssignWithConversion(signonsFileName); if (aReplace) rv = CopyFile(fileName, fileName); else { // don't do anything right now } return rv;}// helper functions for news migrationstatic PRUint32 StringHash(const char *ubuf){ unsigned char * buf = (unsigned char*) ubuf; PRUint32 h=1; while(*buf) { h = 0x63c63cd9*h + 0x9c39c33d + (int32)*buf; buf++; } return h;}nsresult NS_MsgHashIfNecessary(nsCString &name){#if defined(XP_MAC) const PRUint32 MAX_LEN = 25;#elif defined(XP_UNIX) || defined(XP_BEOS) const PRUint32 MAX_LEN = 55;#elif defined(XP_WIN32) const PRUint32 MAX_LEN = 55;#elif defined(XP_OS2) const PRUint32 MAX_LEN = 55;#else #error need_to_define_your_max_filename_length#endif nsCAutoString illegalChars(FILE_PATH_SEPARATOR FILE_ILLEGAL_CHARACTERS); nsCAutoString str(name); // Given a filename, make it safe for filesystem // certain filenames require hashing because they // are too long or contain illegal characters PRInt32 illegalCharacterIndex = str.FindCharInSet(illegalChars); char hashedname[MAX_LEN + 1]; if (illegalCharacterIndex == kNotFound) { // no illegal chars, it's just too long // keep the initial part of the string, but hash to make it fit if (str.Length() > MAX_LEN) { PL_strncpy(hashedname, str.get(), MAX_LEN + 1); PR_snprintf(hashedname + MAX_LEN - 8, 9, "%08lx", (unsigned long) StringHash(str.get())); name = hashedname; } } else { // found illegal chars, hash the whole thing // if we do substitution, then hash, two strings // could hash to the same value. // for example, on mac: "foo__bar", "foo:_bar", "foo::bar" // would map to "foo_bar". this way, all three will map to // different values PR_snprintf(hashedname, 9, "%08lx", (unsigned long) StringHash(str.get())); name = hashedname; } return NS_OK;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -