nsmailwinintegration.cpp
来自「现在很火的邮件客户端软件thunderbird的源码」· C++ 代码 · 共 483 行 · 第 1/2 页
CPP
483 行
NS_IMETHODIMPnsWindowsShellService::IsDefaultClient(PRBool aStartupCheck, PRUint16 aApps, PRBool *aIsDefaultClient){ *aIsDefaultClient = PR_TRUE; // for each type, if (aApps & nsIShellService::MAIL) *aIsDefaultClient &= TestForDefault(gMailSettings, sizeof(gMailSettings)/sizeof(SETTING)); if (aApps & nsIShellService::NEWS) *aIsDefaultClient &= TestForDefault(gNewsSettings, sizeof(gNewsSettings)/sizeof(SETTING)); if (aApps & nsIShellService::RSS) *aIsDefaultClient &= TestForDefault(gFeedSettings, sizeof(gFeedSettings)/sizeof(SETTING)); // If this is the first mail window, maintain internal state that we've // checked this session (so that subsequent window opens don't show the // default client dialog). if (aStartupCheck) mCheckedThisSession = PR_TRUE; return NS_OK;}NS_IMETHODIMPnsWindowsShellService::SetDefaultClient(PRBool aForAllUsers, PRUint16 aApps){ nsresult rv = NS_OK; if (aApps & nsIShellService::MAIL) rv |= setDefaultMail(aForAllUsers); if (aApps & nsIShellService::NEWS) rv |= setDefaultNews(aForAllUsers); if (aApps & nsIShellService::RSS) setKeysForSettings(gFeedSettings, sizeof(gFeedSettings)/sizeof(SETTING), NS_ConvertUTF16toUTF8(mBrandFullName).get(), aForAllUsers); // Refresh the Shell SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0); return rv;}NS_IMETHODIMPnsWindowsShellService::GetShouldCheckDefaultClient(PRBool* aResult){ if (mCheckedThisSession) { *aResult = PR_FALSE; return NS_OK; } nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID)); return prefs->GetBoolPref("mail.shell.checkDefaultClient", aResult);}NS_IMETHODIMPnsWindowsShellService::SetShouldCheckDefaultClient(PRBool aShouldCheck){ nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID)); return prefs->SetBoolPref("mail.shell.checkDefaultClient", aShouldCheck);}nsresultnsWindowsShellService::setDefaultMail(PRBool aForAllUsers){ nsresult rv; NS_ConvertUTF16toUTF8 appName(mBrandFullName); setKeysForSettings(gMailSettings, sizeof(gMailSettings)/sizeof(SETTING), appName.get(), aForAllUsers); // at least for now, this key needs to be written to HKLM instead of HKCU // which is where the windows operating system looks (at least on Win XP and earlier) // for Tools / Internet Options / Programs) so pass in // true for all users. SetRegKey(NS_LITERAL_CSTRING(MOZ_CLIENT_MAIL_KEY).get(), "", appName.get(), PR_TRUE, /* aForAllUsers */ PR_TRUE); nsCAutoString nativeFullName; // For now, we use 'A' APIs (see bug 240272, 239279) NS_CopyUnicodeToNative(mBrandFullName, nativeFullName); nsCAutoString key1(NS_LITERAL_CSTRING(MAILCLIENTS)); key1.Append(appName); key1.Append("\\"); SetRegKey(key1.get(), "", nativeFullName.get(), PR_TRUE, aForAllUsers); // Set the Options and Safe Mode start menu context menu item labels nsCOMPtr<nsIStringBundle> bundle; nsCOMPtr<nsIStringBundleService> bundleService(do_GetService("@mozilla.org/intl/stringbundle;1", &rv)); NS_ENSURE_SUCCESS(rv, rv); rv = bundleService->CreateBundle("chrome://messenger/locale/shellservice.properties", getter_AddRefs(bundle)); NS_ENSURE_SUCCESS(rv, rv); nsCAutoString optionsKey(NS_LITERAL_CSTRING(MAILCLIENTS "%APPNAME%\\shell\\properties")); optionsKey.ReplaceSubstring("%APPNAME%", appName.get()); const PRUnichar* brandNameStrings[] = { mBrandShortName.get() }; // Set the Options menu item nsXPIDLString optionsTitle; bundle->FormatStringFromName(NS_LITERAL_STRING("optionsLabel").get(), brandNameStrings, 1, getter_Copies(optionsTitle)); // Set the registry keys nsCAutoString nativeTitle; // For the now, we use 'A' APIs (see bug 240272, 239279) NS_CopyUnicodeToNative(optionsTitle, nativeTitle); SetRegKey(optionsKey.get(), "", nativeTitle.get(), PR_TRUE, aForAllUsers);#ifndef __MINGW32__ // Tell the MAPI Service to register the mapi proxy dll now that we are the default mail application nsCOMPtr<nsIMapiSupport> mapiService (do_GetService(NS_IMAPISUPPORT_CONTRACTID, &rv)); NS_ENSURE_SUCCESS(rv, rv); return mapiService->RegisterServer();#else return NS_OK;#endif}nsresultnsWindowsShellService::setDefaultNews(PRBool aForAllUsers){ NS_ConvertUTF16toUTF8 appName(mBrandFullName); setKeysForSettings(gNewsSettings, sizeof(gNewsSettings)/sizeof(SETTING), appName.get(), aForAllUsers); // at least for now, this key needs to be written to HKLM instead of HKCU // which is where the windows operating system looks (at least on Win XP and earlier) // for Tools / Internet Options / Programs) so pass in // true for all users. SetRegKey(NS_LITERAL_CSTRING(MOZ_CLIENT_NEWS_KEY).get(), "", appName.get(), PR_TRUE, /* aForAllUsers */ PR_TRUE); nsCAutoString nativeFullName; // For now, we use 'A' APIs (see bug 240272, 239279) NS_CopyUnicodeToNative(mBrandFullName, nativeFullName); nsCAutoString key1(NS_LITERAL_CSTRING(NEWSCLIENTS)); key1.Append(appName); key1.Append("\\"); SetRegKey(key1.get(), "", nativeFullName.get(), PR_TRUE, aForAllUsers); return NS_OK;}voidnsWindowsShellService::SetRegKey(const char* aKeyName, const char* aValueName, const char* aValue, PRBool aReplaceExisting, PRBool aForAllUsers){ char buf[MAX_BUF]; DWORD len = sizeof buf; HKEY theKey; nsresult rv = OpenKeyForWriting(aKeyName, &theKey, aForAllUsers, PR_TRUE); if (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND) return; // If we're not allowed to replace an existing key, and one exists (i.e. the // result isn't ERROR_FILE_NOT_FOUND, then just return now. if (!aReplaceExisting && rv != NS_ERROR_FILE_NOT_FOUND) return; // Get the old value DWORD result = ::RegQueryValueEx(theKey, aValueName, NULL, NULL, (LPBYTE)buf, &len); // Set the new value if (REG_FAILED(result) || strcmp(buf, aValue) != 0) ::RegSetValueEx(theKey, aValueName, 0, REG_SZ, (LPBYTE)aValue, nsDependentCString(aValue).Length()); // Close the key we opened. ::RegCloseKey(theKey);}/* helper routine. Iterate over the passed in settings object, testing each key with the USE_FOR_DEFAULT_TEST to see if we are handling it. */PRBoolnsWindowsShellService::TestForDefault(SETTING aSettings[], PRInt32 aSize){ PRBool isDefault = PR_TRUE; NS_ConvertUTF16toUTF8 appName(mBrandFullName); char currValue[MAX_BUF]; SETTING* end = aSettings + aSize; for (SETTING * settings = aSettings; settings < end; ++settings) { if (settings->flags & USE_FOR_DEFAULT_TEST) { nsCAutoString data(settings->valueData); nsCAutoString key(settings->keyName); if (settings->flags & PATH_SUBSTITUTION) { PRInt32 offset = data.Find("%APPPATH%"); data.Replace(offset, 9, mAppPath); } if (settings->flags & APPNAME_SUBSTITUTION) { PRInt32 offset = key.Find("%APPNAME%"); key.Replace(offset, 9, appName); } ::ZeroMemory(currValue, sizeof(currValue)); HKEY theKey; nsresult rv = OpenUserKeyForReading(HKEY_CURRENT_USER, key.get(), &theKey); if (NS_SUCCEEDED(rv)) { DWORD len = sizeof currValue; DWORD result = ::RegQueryValueEx(theKey, settings->valueName, NULL, NULL, (LPBYTE)currValue, &len); // Close the key we opened. ::RegCloseKey(theKey); if (REG_FAILED(result) || strcmp(data.get(), currValue) != 0) { // Key wasn't set, or was set to something else (something else became the default browser) isDefault = PR_FALSE; break; } } } } // for each registry key we want to look at return isDefault;}/* helper routine. Iterate over the passed in settings array, setting each key * in the windows registry.*/void nsWindowsShellService::setKeysForSettings(SETTING aSettings[], PRInt32 aSize, const char * aAppName, PRBool aForAllUsers){ SETTING* settings; SETTING* end = aSettings + aSize; for (settings = aSettings; settings < end; ++settings) { nsCAutoString data(settings->valueData); nsCAutoString key(settings->keyName); if (settings->flags & PATH_SUBSTITUTION) { PRInt32 offset = data.Find("%APPPATH%"); data.Replace(offset, 9, mAppPath); } if (settings->flags & MAPIDLLPATH_SUBSTITUTION) { PRInt32 offset = data.Find("%MAPIDLLPATH%"); data.Replace(offset, 13, mMapiDLLPath); } if (settings->flags & APPNAME_SUBSTITUTION) { PRInt32 offset = key.Find("%APPNAME%"); key.Replace(offset, 9, aAppName); } SetRegKey(key.get(), settings->valueName, data.get(), PR_TRUE, aForAllUsers); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?