?? nsseamonkeyprofilemigrator.cpp
字號:
*aResult = PR_FALSE; return NS_OK;}NS_IMETHODIMPnsSeamonkeyProfileMigrator::GetSourceProfiles(nsISupportsArray** aResult){ if (!mProfileNames && !mProfileLocations) { nsresult rv = NS_NewISupportsArray(getter_AddRefs(mProfileNames)); if (NS_FAILED(rv)) return rv; rv = NS_NewISupportsArray(getter_AddRefs(mProfileLocations)); if (NS_FAILED(rv)) return rv; // Fills mProfileNames and mProfileLocations FillProfileDataFromSeamonkeyRegistry(); } NS_IF_ADDREF(*aResult = mProfileNames); return NS_OK;}///////////////////////////////////////////////////////////////////////////////// nsSeamonkeyProfileMigratornsresultnsSeamonkeyProfileMigrator::GetSourceProfile(const PRUnichar* aProfile){ PRUint32 count; mProfileNames->Count(&count); for (PRUint32 i = 0; i < count; ++i) { nsCOMPtr<nsISupportsString> str(do_QueryElementAt(mProfileNames, i)); nsXPIDLString profileName; str->GetData(profileName); if (profileName.Equals(aProfile)) { mSourceProfile = do_QueryElementAt(mProfileLocations, i); break; } } return NS_OK;}nsresultnsSeamonkeyProfileMigrator::FillProfileDataFromSeamonkeyRegistry(){ // Find the Seamonkey Registry nsCOMPtr<nsIProperties> fileLocator(do_GetService("@mozilla.org/file/directory_service;1")); nsCOMPtr<nsILocalFile> seamonkeyRegistry;#ifdef XP_WIN fileLocator->Get(NS_WIN_APPDATA_DIR, NS_GET_IID(nsILocalFile), getter_AddRefs(seamonkeyRegistry)); NS_ENSURE_TRUE(seamonkeyRegistry, NS_ERROR_FAILURE); seamonkeyRegistry->Append(NS_LITERAL_STRING("Mozilla")); seamonkeyRegistry->Append(NS_LITERAL_STRING("registry.dat"));#elif defined(XP_MACOSX) fileLocator->Get(NS_MAC_USER_LIB_DIR, NS_GET_IID(nsILocalFile), getter_AddRefs(seamonkeyRegistry)); NS_ENSURE_TRUE(seamonkeyRegistry, NS_ERROR_FAILURE); seamonkeyRegistry->Append(NS_LITERAL_STRING("Mozilla")); seamonkeyRegistry->Append(NS_LITERAL_STRING("Application Registry"));#elif defined(XP_UNIX) fileLocator->Get(NS_UNIX_HOME_DIR, NS_GET_IID(nsILocalFile), getter_AddRefs(seamonkeyRegistry)); NS_ENSURE_TRUE(seamonkeyRegistry, NS_ERROR_FAILURE); seamonkeyRegistry->Append(NS_LITERAL_STRING(".mozilla")); seamonkeyRegistry->Append(NS_LITERAL_STRING("appreg"));#elif defined(XP_OS2) fileLocator->Get(NS_OS2_HOME_DIR, NS_GET_IID(nsILocalFile), getter_AddRefs(seamonkeyRegistry)); NS_ENSURE_TRUE(seamonkeyRegistry, NS_ERROR_FAILURE); seamonkeyRegistry->Append(NS_LITERAL_STRING("Mozilla")); seamonkeyRegistry->Append(NS_LITERAL_STRING("registry.dat"));#endif return GetProfileDataFromRegistry(seamonkeyRegistry, mProfileNames, mProfileLocations);}#define F(a) nsSeamonkeyProfileMigrator::a#define MAKEPREFTRANSFORM(pref, newpref, getmethod, setmethod) \ { pref, newpref, F(Get##getmethod), F(Set##setmethod), PR_FALSE, -1 }#define MAKESAMETYPEPREFTRANSFORM(pref, method) \ { pref, 0, F(Get##method), F(Set##method), PR_FALSE, -1 }static nsSeamonkeyProfileMigrator::PrefTransform gTransforms[] = { MAKESAMETYPEPREFTRANSFORM("signon.SignonFileName", String), MAKESAMETYPEPREFTRANSFORM("mailnews.headers.showUserAgent", Bool), MAKESAMETYPEPREFTRANSFORM("mailnews.headers.showOrganization", Bool), MAKESAMETYPEPREFTRANSFORM("mail.collect_addressbook", String), MAKESAMETYPEPREFTRANSFORM("mail.collect_email_address_outgoing", Bool), MAKESAMETYPEPREFTRANSFORM("mail.wrap_long_lines", Bool), MAKESAMETYPEPREFTRANSFORM("news.wrap_long_lines", Bool), MAKESAMETYPEPREFTRANSFORM("mailnews.customHeaders", String), MAKESAMETYPEPREFTRANSFORM("mail.default_html_action", Int), MAKESAMETYPEPREFTRANSFORM("mail.forward_message_mode", Int), MAKESAMETYPEPREFTRANSFORM("mail.SpellCheckBeforeSend", Bool), MAKESAMETYPEPREFTRANSFORM("mail.warn_on_send_accel_key", Bool), MAKESAMETYPEPREFTRANSFORM("mailnews.html_domains", String), MAKESAMETYPEPREFTRANSFORM("mailnews.plaintext_domains", String), MAKESAMETYPEPREFTRANSFORM("mailnews.headers.showUserAgent", Bool), MAKESAMETYPEPREFTRANSFORM("mailnews.headers.showOrganization", Bool), MAKESAMETYPEPREFTRANSFORM("mail.biff.play_sound", Bool), MAKESAMETYPEPREFTRANSFORM("mail.biff.play_sound.type", Int), MAKESAMETYPEPREFTRANSFORM("mail.biff.play_sound.url", String), MAKESAMETYPEPREFTRANSFORM("mail.biff.show_alert", Bool), MAKESAMETYPEPREFTRANSFORM("network.proxy.type", Int), MAKESAMETYPEPREFTRANSFORM("network.proxy.http", String), MAKESAMETYPEPREFTRANSFORM("network.proxy.http_port", Int), MAKESAMETYPEPREFTRANSFORM("network.proxy.ftp", String), MAKESAMETYPEPREFTRANSFORM("network.proxy.ftp_port", Int), MAKESAMETYPEPREFTRANSFORM("network.proxy.ssl", String), MAKESAMETYPEPREFTRANSFORM("network.proxy.ssl_port", Int), MAKESAMETYPEPREFTRANSFORM("network.proxy.socks", String), MAKESAMETYPEPREFTRANSFORM("network.proxy.socks_port", Int), MAKESAMETYPEPREFTRANSFORM("network.proxy.gopher", String), MAKESAMETYPEPREFTRANSFORM("network.proxy.gopher_port", Int), MAKESAMETYPEPREFTRANSFORM("network.proxy.no_proxies_on", String), MAKESAMETYPEPREFTRANSFORM("network.proxy.autoconfig_url", String), MAKESAMETYPEPREFTRANSFORM("mail.accountmanager.accounts", String), MAKESAMETYPEPREFTRANSFORM("mail.accountmanager.defaultaccount", String), MAKESAMETYPEPREFTRANSFORM("mail.accountmanager.localfoldersserver", String), MAKESAMETYPEPREFTRANSFORM("mail.smtp.defaultserver", String), MAKESAMETYPEPREFTRANSFORM("msgcompose.font_face", String), MAKESAMETYPEPREFTRANSFORM("msgcompose.font_size", String), MAKESAMETYPEPREFTRANSFORM("msgcompose.text_color", String), MAKESAMETYPEPREFTRANSFORM("msgcompose.background_color", String), MAKEPREFTRANSFORM("mail.pane_config","mail.pane_config.dynamic", Int, Int)};nsresultnsSeamonkeyProfileMigrator::TransformPreferences(const nsAString& aSourcePrefFileName, const nsAString& aTargetPrefFileName){ PrefTransform* transform; PrefTransform* end = gTransforms + sizeof(gTransforms)/sizeof(PrefTransform); // Load the source pref file nsCOMPtr<nsIPrefService> psvc(do_GetService(NS_PREFSERVICE_CONTRACTID)); psvc->ResetPrefs(); nsCOMPtr<nsIFile> sourcePrefsFile; mSourceProfile->Clone(getter_AddRefs(sourcePrefsFile)); sourcePrefsFile->Append(aSourcePrefFileName); psvc->ReadUserPrefs(sourcePrefsFile); nsCOMPtr<nsIPrefBranch> branch(do_QueryInterface(psvc)); for (transform = gTransforms; transform < end; ++transform) transform->prefGetterFunc(transform, branch); // read in the various pref branch trees for accounts, identities, servers, etc. nsVoidArray* accounts = new nsVoidArray(); nsVoidArray* identities = new nsVoidArray(); nsVoidArray* servers = new nsVoidArray(); nsVoidArray* smtpservers = new nsVoidArray(); nsVoidArray* ldapservers = new nsVoidArray(); nsVoidArray* labelPrefs = new nsVoidArray(); if (!accounts || !identities || !servers || !smtpservers || !ldapservers) return NS_ERROR_OUT_OF_MEMORY; ReadBranch("mail.account.", psvc, accounts); ReadBranch("mail.identity.", psvc, identities); ReadBranch("mail.server.", psvc, servers); ReadBranch("mail.smtpserver.", psvc, smtpservers); ReadBranch("ldap_2.servers.", psvc, ldapservers); ReadBranch("mailnews.labels.", psvc, labelPrefs); // the signature file prefs may be paths to files in the seamonkey profile path // so we need to copy them over and fix these paths up before we write them out to the new prefs.js CopySignatureFiles(identities, psvc); // certain mail prefs may actually be absolute paths instead of profile relative paths // we need to fix these paths up before we write them out to the new prefs.js CopyMailFolders(servers, psvc); CopyAddressBookDirectories(ldapservers, psvc); // Now that we have all the pref data in memory, load the target pref file, // and write it back out psvc->ResetPrefs(); for (transform = gTransforms; transform < end; ++transform) transform->prefSetterFunc(transform, branch); WriteBranch("mail.account.", psvc, accounts); WriteBranch("mail.identity.", psvc, identities); WriteBranch("mail.server.", psvc, servers); WriteBranch("mail.smtpserver.", psvc, smtpservers); WriteBranch("ldap_2.servers.", psvc, ldapservers); WriteBranch("mailnews.labels.", psvc, labelPrefs); delete accounts; delete identities; delete servers; delete smtpservers; delete ldapservers; delete labelPrefs; nsCOMPtr<nsIFile> targetPrefsFile; mTargetProfile->Clone(getter_AddRefs(targetPrefsFile)); targetPrefsFile->Append(aTargetPrefFileName); psvc->SavePrefFile(targetPrefsFile); return NS_OK;}nsresult nsSeamonkeyProfileMigrator::CopyAddressBookDirectories(nsVoidArray* aLdapServers, nsIPrefService* aPrefService){ // each server has a pref ending with .filename. The value of that pref points to a profile which we // need to migrate. nsAutoString index; index.AppendInt(nsIMailProfileMigrator::ADDRESSBOOK_DATA); NOTIFY_OBSERVERS(MIGRATION_ITEMBEFOREMIGRATE, index.get()); PRUint32 count = aLdapServers->Count(); for (PRUint32 i = 0; i < count; ++i) { PrefBranchStruct* pref = (PrefBranchStruct*) aLdapServers->ElementAt(i); nsDependentCString prefName (pref->prefName); if (StringEndsWith(prefName, nsDependentCString(".filename"))) { // should we be assuming utf-8 or ascii here? CopyFile(NS_ConvertUTF8toUCS2(pref->stringValue), NS_ConvertUTF8toUCS2(pref->stringValue)); } // we don't need to do anything to the fileName pref itself } NOTIFY_OBSERVERS(MIGRATION_ITEMAFTERMIGRATE, index.get()); return NS_OK;}nsresult nsSeamonkeyProfileMigrator::CopySignatureFiles(nsVoidArray* aIdentities, nsIPrefService* aPrefService){ nsresult rv = NS_OK; PRUint32 count = aIdentities->Count(); for (PRUint32 i = 0; i < count; ++i) { PrefBranchStruct* pref = (PrefBranchStruct*)aIdentities->ElementAt(i); nsDependentCString prefName (pref->prefName); // a partial fix for bug #255043 // if the user's signature file from seamonkey lives in the // seamonkey profile root, we'll copy it over to the new // thunderbird profile root and thenn set the pref to the new value // note, this doesn't work for multiple signatures that live // below the seamonkey profile root if (StringEndsWith(prefName, nsDependentCString(".sig_file"))) { // turn the pref into a nsILocalFile nsCOMPtr<nsILocalFile> srcSigFile = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID); srcSigFile->SetPersistentDescriptor(nsDependentCString(pref->stringValue)); nsCOMPtr<nsIFile> targetSigFile; rv = mTargetProfile->Clone(getter_AddRefs(targetSigFile)); NS_ENSURE_SUCCESS(rv, rv); // now make the copy PRBool exists; srcSigFile->Exists(&exists); if (exists) { nsAutoString leafName; srcSigFile->GetLeafName(leafName); srcSigFile->CopyTo(targetSigFile,leafName); // will fail if we've already copied a sig file here targetSigFile->Append(leafName); // now write out the new descriptor nsCAutoString descriptorString; nsCOMPtr<nsILocalFile> localFile = do_QueryInterface(targetSigFile); localFile->GetPersistentDescriptor(descriptorString); nsCRT::free(pref->stringValue); pref->stringValue = ToNewCString(descriptorString); } } } return NS_OK;}nsresult nsSeamonkeyProfileMigrator::CopyMailFolders(nsVoidArray* aMailServers, nsIPrefService* aPrefService){ // Each server has a .directory pref which points to the location of the mail data // for that server. We need to do two things for that case... // (1) Fix up the directory path for the new profile // (2) copy the mail folder data from the source directory pref to the destination directory pref nsresult rv = NS_OK; PRUint32 count = aMailServers->Count(); for (PRUint32 i = 0; i < count; ++i) { PrefBranchStruct* pref = (PrefBranchStruct*)aMailServers->ElementAt(i); nsDependentCString prefName (pref->prefName); if (StringEndsWith(prefName, nsDependentCString(".directory"))) { // let's try to get a branch for this particular server to simplify things
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -