Back to index

lightning-sunbird  0.9+nobinonly
nsGConfService.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 "nsGConfService.h"
00040 #include "nsCRT.h"
00041 #include "nsString.h"
00042 
00043 #include <gconf/gconf-client.h>
00044 
00045 nsGConfService::~nsGConfService()
00046 {
00047   if (mClient)
00048     g_object_unref(mClient);
00049 }
00050 
00051 nsresult
00052 nsGConfService::Init()
00053 {
00054   mClient = gconf_client_get_default();
00055   return mClient ? NS_OK : NS_ERROR_FAILURE;
00056 }
00057 
00058 NS_IMPL_ISUPPORTS1(nsGConfService, nsIGConfService)
00059 
00060 NS_IMETHODIMP
00061 nsGConfService::GetBool(const nsACString &aKey, PRBool *aResult)
00062 {
00063   GError* error = nsnull;
00064   *aResult = gconf_client_get_bool(mClient, PromiseFlatCString(aKey).get(),
00065                                    &error);
00066 
00067   if (error) {
00068     g_error_free(error);
00069     return NS_ERROR_FAILURE;
00070   }
00071 
00072   return NS_OK;
00073 }
00074 
00075 NS_IMETHODIMP
00076 nsGConfService::GetString(const nsACString &aKey, nsACString &aResult)
00077 {
00078   GError* error = nsnull;
00079   gchar *result = gconf_client_get_string(mClient,
00080                                           PromiseFlatCString(aKey).get(),
00081                                           &error);
00082 
00083   if (error) {
00084     g_error_free(error);
00085     return NS_ERROR_FAILURE;
00086   }
00087 
00088   // We do a string copy here so that the caller doesn't need to worry about
00089   // freeing the string with g_free().
00090 
00091   aResult.Assign(result);
00092   g_free(result);
00093 
00094   return NS_OK;
00095 }
00096 
00097 NS_IMETHODIMP
00098 nsGConfService::GetInt(const nsACString &aKey, PRInt32* aResult)
00099 {
00100   GError* error = nsnull;
00101   *aResult = gconf_client_get_int(mClient, PromiseFlatCString(aKey).get(),
00102                                   &error);
00103 
00104   if (error) {
00105     g_error_free(error);
00106     return NS_ERROR_FAILURE;
00107   }
00108 
00109   return NS_OK;
00110 }
00111 
00112 NS_IMETHODIMP
00113 nsGConfService::GetFloat(const nsACString &aKey, float* aResult)
00114 {
00115   GError* error = nsnull;
00116   *aResult = gconf_client_get_float(mClient, PromiseFlatCString(aKey).get(),
00117                                     &error);
00118 
00119   if (error) {
00120     g_error_free(error);
00121     return NS_ERROR_FAILURE;
00122   }
00123 
00124   return NS_OK;
00125 }
00126 
00127 NS_IMETHODIMP
00128 nsGConfService::SetBool(const nsACString &aKey, PRBool aValue)
00129 {
00130   PRBool res = gconf_client_set_bool(mClient, PromiseFlatCString(aKey).get(),
00131                                      aValue, nsnull);
00132 
00133   return res ? NS_OK : NS_ERROR_FAILURE;
00134 }
00135 
00136 NS_IMETHODIMP
00137 nsGConfService::SetString(const nsACString &aKey, const nsACString &aValue)
00138 {
00139   PRBool res = gconf_client_set_string(mClient, PromiseFlatCString(aKey).get(),
00140                                        PromiseFlatCString(aValue).get(),
00141                                        nsnull);
00142 
00143   return res ? NS_OK : NS_ERROR_FAILURE;
00144 }
00145 
00146 NS_IMETHODIMP
00147 nsGConfService::SetInt(const nsACString &aKey, PRInt32 aValue)
00148 {
00149   PRBool res = gconf_client_set_int(mClient, PromiseFlatCString(aKey).get(),
00150                                     aValue, nsnull);
00151 
00152   return res ? NS_OK : NS_ERROR_FAILURE;
00153 }
00154 
00155 NS_IMETHODIMP
00156 nsGConfService::SetFloat(const nsACString &aKey, float aValue)
00157 {
00158   PRBool res = gconf_client_set_float(mClient, PromiseFlatCString(aKey).get(),
00159                                       aValue, nsnull);
00160 
00161   return res ? NS_OK : NS_ERROR_FAILURE;
00162 }
00163 
00164 NS_IMETHODIMP
00165 nsGConfService::GetAppForProtocol(const nsACString &aScheme, PRBool *aEnabled,
00166                                   nsACString &aHandler)
00167 {
00168   nsCAutoString key(NS_LITERAL_CSTRING("/desktop/gnome/url-handlers/")
00169                   + aScheme + NS_LITERAL_CSTRING("/command"));
00170 
00171   GError *err = nsnull;
00172   gchar *command = gconf_client_get_string(mClient, key.get(), &err);
00173   if (!err && command) {
00174     key.Replace(key.Length() - 7, 7, NS_LITERAL_CSTRING("enabled"));
00175     *aEnabled = gconf_client_get_bool(mClient, key.get(), &err);
00176   } else {
00177     *aEnabled = PR_FALSE;
00178   }
00179 
00180   aHandler.Assign(command);
00181   if (command)
00182     g_free(command);
00183 
00184   if (err) {
00185     g_error_free(err);
00186     return NS_ERROR_FAILURE;
00187   }
00188 
00189   return NS_OK;
00190 }
00191 
00192 NS_IMETHODIMP
00193 nsGConfService::HandlerRequiresTerminal(const nsACString &aScheme,
00194                                         PRBool *aResult)
00195 {
00196   nsCAutoString key(NS_LITERAL_CSTRING("/desktop/gnome/url-handlers/")
00197                   + aScheme + NS_LITERAL_CSTRING("/requires_terminal"));
00198 
00199   GError *err = nsnull;
00200   *aResult = gconf_client_get_bool(mClient, key.get(), &err);
00201   if (err) {
00202     g_error_free(err);
00203     return NS_ERROR_FAILURE;
00204   }
00205 
00206   return NS_OK;
00207 }
00208 
00209 NS_IMETHODIMP
00210 nsGConfService::SetAppForProtocol(const nsACString &aScheme,
00211                                   const nsACString &aCommand)
00212 {
00213   nsCAutoString key(NS_LITERAL_CSTRING("/desktop/gnome/url-handlers/")
00214                   + aScheme + NS_LITERAL_CSTRING("/command"));
00215 
00216   PRBool res = gconf_client_set_string(mClient, key.get(),
00217                                        PromiseFlatCString(aCommand).get(),
00218                                        nsnull);
00219   if (res) {
00220     key.Replace(key.Length() - 7, 7, NS_LITERAL_CSTRING("enabled"));
00221     res = gconf_client_set_bool(mClient, key.get(), PR_TRUE, nsnull);
00222     if (res) {
00223       key.Replace(key.Length() - 7, 7, NS_LITERAL_CSTRING("needs_terminal"));
00224       res = gconf_client_set_bool(mClient, key.get(), PR_FALSE, nsnull);
00225       if (res) {
00226         key.Replace(key.Length() - 14, 14, NS_LITERAL_CSTRING("command-id"));
00227         res = gconf_client_unset(mClient, key.get(), nsnull);
00228       }
00229     }
00230   }
00231 
00232   return res ? NS_OK : NS_ERROR_FAILURE;
00233 }