Back to index

lightning-sunbird  0.9+nobinonly
nsMailGNOMEIntegration.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Mozilla GNOME integration code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * IBM Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Brian Ryner <bryner@brianryner.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsMailGNOMEIntegration.h"
00040 #include "nsIGenericFactory.h"
00041 #include "nsIGConfService.h"
00042 #include "nsCOMPtr.h"
00043 #include "nsIServiceManager.h"
00044 #include "prenv.h"
00045 #include "nsIFile.h"
00046 #include "nsIStringBundle.h"
00047 #include "nsIPromptService.h"
00048 #include "nsIPrefService.h"
00049 #include "nsIPrefBranch.h"
00050 #include "nsDirectoryServiceDefs.h"
00051 #include "nsDirectoryServiceUtils.h"
00052 #include "nsEmbedCID.h"
00053 
00054 #include <glib.h>
00055 #include <limits.h>
00056 #include <stdlib.h>
00057 
00058 static const char* const sMailProtocols[] = {
00059   "mailto"
00060 };
00061 
00062 static const char* const sNewsProtocols[] = {
00063   "news",
00064   "snews",
00065   "nntp"
00066 };
00067 
00068 static const char* const sFeedProtocols[] = {
00069   "feed"
00070 };
00071 
00072 nsMailGNOMEIntegration::nsMailGNOMEIntegration(): mCheckedThisSession(PR_FALSE)
00073 {}
00074 
00075 nsresult
00076 nsMailGNOMEIntegration::Init()
00077 {
00078   nsresult rv;
00079 
00080   // GConf _must_ be available, or we do not allow CreateInstance to succeed.
00081 
00082   nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID);
00083 
00084   if (!gconf)
00085     return NS_ERROR_NOT_AVAILABLE;
00086 
00087   // Check G_BROKEN_FILENAMES.  If it's set, then filenames in glib use
00088   // the locale encoding.  If it's not set, they use UTF-8.
00089   mUseLocaleFilenames = PR_GetEnv("G_BROKEN_FILENAMES") != nsnull;
00090 
00091   nsCOMPtr<nsIFile> appPath;
00092   rv = NS_GetSpecialDirectory(NS_XPCOM_CURRENT_PROCESS_DIR,
00093                               getter_AddRefs(appPath));
00094   NS_ENSURE_SUCCESS(rv, rv);
00095 
00096   rv = appPath->AppendNative(NS_LITERAL_CSTRING("thunderbird"));
00097   NS_ENSURE_SUCCESS(rv, rv);
00098 
00099   rv = appPath->GetNativePath(mAppPath);
00100   return rv;
00101 }
00102 
00103 NS_IMPL_ISUPPORTS1(nsMailGNOMEIntegration, nsIShellService)
00104 
00105 
00106 NS_IMETHODIMP
00107 nsMailGNOMEIntegration::IsDefaultClient(PRBool aStartupCheck, PRUint16 aApps, PRBool * aIsDefaultClient)
00108 {
00109   *aIsDefaultClient = PR_TRUE;
00110   if (aApps & nsIShellService::MAIL)
00111     *aIsDefaultClient &= checkDefault(sMailProtocols, NS_ARRAY_LENGTH(sMailProtocols));
00112   if (aApps & nsIShellService::NEWS)
00113     *aIsDefaultClient &= checkDefault(sNewsProtocols, NS_ARRAY_LENGTH(sNewsProtocols));
00114   if (aApps & nsIShellService::RSS)
00115     *aIsDefaultClient &= checkDefault(sFeedProtocols, NS_ARRAY_LENGTH(sFeedProtocols));
00116   
00117   // If this is the first mail window, maintain internal state that we've
00118   // checked this session (so that subsequent window opens don't show the 
00119   // default client dialog).
00120   if (aStartupCheck)
00121     mCheckedThisSession = PR_TRUE;
00122   return NS_OK;
00123 }
00124 
00125 NS_IMETHODIMP
00126 nsMailGNOMEIntegration::SetDefaultClient(PRBool aForAllUsers, PRUint16 aApps)
00127 {
00128   nsresult rv = NS_OK;
00129   if (aApps & nsIShellService::MAIL)
00130     rv |= MakeDefault(sMailProtocols, NS_ARRAY_LENGTH(sMailProtocols));
00131   if (aApps & nsIShellService::NEWS)
00132     rv |= MakeDefault(sNewsProtocols, NS_ARRAY_LENGTH(sNewsProtocols));
00133   if (aApps & nsIShellService::RSS)
00134     rv |= MakeDefault(sFeedProtocols, NS_ARRAY_LENGTH(sFeedProtocols));
00135 
00136   return rv;  
00137 }
00138 
00139 NS_IMETHODIMP
00140 nsMailGNOMEIntegration::GetShouldCheckDefaultClient(PRBool* aResult)
00141 {
00142   if (mCheckedThisSession) 
00143   {
00144     *aResult = PR_FALSE;
00145   return NS_OK;
00146 }
00147 
00148   nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
00149   return prefs->GetBoolPref("mail.shell.checkDefaultClient", aResult);
00150 }
00151 
00152 NS_IMETHODIMP
00153 nsMailGNOMEIntegration::SetShouldCheckDefaultClient(PRBool aShouldCheck)
00154 {
00155   nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
00156   return prefs->SetBoolPref("mail.shell.checkDefaultClient", aShouldCheck);
00157 }
00158 
00159 PRBool
00160 nsMailGNOMEIntegration::KeyMatchesAppName(const char *aKeyValue) const
00161 {
00162   gchar *commandPath;
00163   if (mUseLocaleFilenames) {
00164     gchar *nativePath = g_filename_from_utf8(aKeyValue, -1, NULL, NULL, NULL);
00165     if (!nativePath) {
00166       NS_ERROR("Error converting path to filesystem encoding");
00167       return PR_FALSE;
00168     }
00169 
00170     commandPath = g_find_program_in_path(nativePath);
00171     g_free(nativePath);
00172   } else {
00173     commandPath = g_find_program_in_path(aKeyValue);
00174   }
00175 
00176   if (!commandPath)
00177     return PR_FALSE;
00178 
00179   PRBool matches = mAppPath.Equals(commandPath);
00180   g_free(commandPath);
00181   return matches;
00182 }
00183 
00184 PRBool
00185 nsMailGNOMEIntegration::checkDefault(const char* const *aProtocols, unsigned int aLength)
00186 {
00187   nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID);
00188 
00189   PRBool enabled;
00190   nsCAutoString handler;
00191 
00192   for (unsigned int i = 0; i < aLength; ++i) {
00193     handler.Truncate();
00194     nsresult rv = gconf->GetAppForProtocol(nsDependentCString(aProtocols[i]),
00195                                            &enabled, handler);
00196     if (NS_SUCCEEDED(rv))
00197     {
00198     // The string will be something of the form: [/path/to/]app "%s"
00199     // We want to remove all of the parameters and get just the binary name.
00200 
00201     gint argc;
00202     gchar **argv;
00203 
00204     if (g_shell_parse_argv(handler.get(), &argc, &argv, NULL) && argc > 0) {
00205       handler.Assign(argv[0]);
00206       g_strfreev(argv);
00207       } else 
00208         return PR_FALSE;
00209 
00210     if (!KeyMatchesAppName(handler.get()) || !enabled)
00211         return PR_FALSE; // the handler is disabled or set to another app
00212     }
00213   }
00214 
00215   return PR_TRUE;
00216 }
00217 
00218 nsresult
00219 nsMailGNOMEIntegration::MakeDefault(const char* const *aProtocols,
00220                                     unsigned int aLength)
00221 {
00222   nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID);
00223   nsCAutoString appKeyValue(mAppPath + NS_LITERAL_CSTRING(" \"%s\""));
00224 
00225   for (unsigned int i = 0; i < aLength; ++i) {
00226     nsresult rv = gconf->SetAppForProtocol(nsDependentCString(aProtocols[i]),
00227                                            appKeyValue);
00228     NS_ENSURE_SUCCESS(rv, rv);
00229   }
00230 
00231   return NS_OK;
00232 }