Audacious $Id:Doxyfile42802007-03-2104:39:00Znenolod$
configdb.c
Go to the documentation of this file.
00001 /*
00002  *  This program is free software; you can redistribute it and/or modify
00003  *  it under the terms of the GNU General Public License as published by
00004  *  the Free Software Foundation; under version 3 of the License.
00005  *
00006  *  This program is distributed in the hope that it will be useful,
00007  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00008  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00009  *  GNU General Public License for more details.
00010  *
00011  *  You should have received a copy of the GNU General Public License
00012  *  along with this program.  If not, see <http://www.gnu.org/licenses>.
00013  *
00014  *  The Audacious team does not consider modular code linking to
00015  *  Audacious or using our public API to be a derived work.
00016  */
00017 
00018 #ifdef HAVE_CONFIG_H
00019 #  include "config.h"
00020 #endif
00021 
00022 #include "configdb.h"
00023 #include <libmcs/mcs.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 
00027 
00028 #define RCFILE_DEFAULT_SECTION_NAME "audacious"
00029 
00030 static gboolean mcs_initted = FALSE;
00031 static mcs_handle_t * config_handle = NULL;
00032 static gint config_refcount = 0;
00033 
00039 mcs_handle_t *
00040 cfg_db_open()
00041 {
00042     if (!mcs_initted)
00043     {
00044         mcs_init();
00045         mcs_initted = TRUE;
00046     }
00047 
00048     if (! config_handle)
00049     {
00050         config_handle = mcs_new (RCFILE_DEFAULT_SECTION_NAME);
00051 
00052         if (! config_handle)
00053         {
00054             fprintf (stderr, "MCS failure.  Configuration will not be saved.\n");
00055             return NULL;
00056         }
00057     }
00058 
00059     config_refcount ++;
00060     return config_handle;
00061 }
00062 
00067 void cfg_db_close (mcs_handle_t * handle)
00068 {
00069     g_return_if_fail (handle && handle == config_handle);
00070     g_return_if_fail (config_refcount > 0);
00071     config_refcount --;
00072 }
00073 
00074 void cfg_db_flush (void)
00075 {
00076     if (! config_handle)
00077         return; /* nothing to do */
00078 
00079     g_return_if_fail (! config_refcount);
00080     mcs_destroy (config_handle);
00081     config_handle = NULL;
00082 }
00083 
00093 gboolean
00094 cfg_db_get_string(mcs_handle_t * db,
00095                   const gchar * section,
00096                   const gchar * key,
00097                   gchar ** value)
00098 {
00099     g_return_val_if_fail (db && db == config_handle, FALSE);
00100 
00101     if (!section)
00102         section = RCFILE_DEFAULT_SECTION_NAME;
00103 
00104     if (! mcs_get_string (db, section, key, value))
00105         return FALSE;
00106 
00107     /* Prior to 2.3, NULL values were saved as "(null)". -jlindgren */
00108     if (! strcmp (* value, "(null)"))
00109     {
00110         * value = NULL;
00111         return FALSE;
00112     }
00113 
00114     return TRUE;
00115 }
00116 
00126 gboolean
00127 cfg_db_get_int(mcs_handle_t * db,
00128                const gchar * section, const gchar * key, gint * value)
00129 {
00130     g_return_val_if_fail (db && db == config_handle, FALSE);
00131 
00132     if (!section)
00133         section = RCFILE_DEFAULT_SECTION_NAME;
00134 
00135     return mcs_get_int(db, section, key, value);
00136 }
00137 
00147 gboolean
00148 cfg_db_get_bool(mcs_handle_t * db,
00149                     const gchar * section,
00150                     const gchar * key,
00151                     gboolean * value)
00152 {
00153     g_return_val_if_fail (db && db == config_handle, FALSE);
00154 
00155     if (!section)
00156         section = RCFILE_DEFAULT_SECTION_NAME;
00157 
00158     return mcs_get_bool(db, section, key, value);
00159 }
00160 
00171 gboolean
00172 cfg_db_get_float(mcs_handle_t * db,
00173                      const gchar * section,
00174                      const gchar * key,
00175                      gfloat * value)
00176 {
00177     g_return_val_if_fail (db && db == config_handle, FALSE);
00178 
00179     if (!section)
00180         section = RCFILE_DEFAULT_SECTION_NAME;
00181 
00182     return mcs_get_float(db, section, key, value);
00183 }
00184 
00195 gboolean
00196 cfg_db_get_double(mcs_handle_t * db,
00197                       const gchar * section,
00198                       const gchar * key,
00199                       gdouble * value)
00200 {
00201     g_return_val_if_fail (db && db == config_handle, FALSE);
00202 
00203     if (!section)
00204         section = RCFILE_DEFAULT_SECTION_NAME;
00205 
00206     return mcs_get_double(db, section, key, value);
00207 }
00208 
00218 void
00219 cfg_db_set_string(mcs_handle_t * db,
00220                       const gchar * section,
00221                       const gchar * key,
00222                       const gchar * value)
00223 {
00224     g_return_if_fail (db && db == config_handle);
00225 
00226     if (!section)
00227         section = RCFILE_DEFAULT_SECTION_NAME;
00228 
00229     if (value == NULL)
00230         mcs_unset_key (db, section, key);
00231     else
00232         mcs_set_string (db, section, key, value);
00233 }
00234 
00244 void
00245 cfg_db_set_int(mcs_handle_t * db,
00246                    const gchar * section,
00247                    const gchar * key,
00248                    gint value)
00249 {
00250     g_return_if_fail (db && db == config_handle);
00251 
00252     if (!section)
00253         section = RCFILE_DEFAULT_SECTION_NAME;
00254 
00255     mcs_set_int(db, section, key, value);
00256 }
00257 
00267 void
00268 cfg_db_set_bool(mcs_handle_t * db,
00269                     const gchar * section,
00270                     const gchar * key,
00271                     gboolean value)
00272 {
00273     g_return_if_fail (db && db == config_handle);
00274 
00275     if (!section)
00276         section = RCFILE_DEFAULT_SECTION_NAME;
00277 
00278     mcs_set_bool(db, section, key, value);
00279 }
00280 
00290 void
00291 cfg_db_set_float(mcs_handle_t * db,
00292                      const gchar * section,
00293                      const gchar * key,
00294                      gfloat value)
00295 {
00296     g_return_if_fail (db && db == config_handle);
00297 
00298     if (!section)
00299         section = RCFILE_DEFAULT_SECTION_NAME;
00300 
00301     mcs_set_float(db, section, key, value);
00302 }
00303 
00313 void
00314 cfg_db_set_double(mcs_handle_t * db,
00315                       const gchar * section,
00316                       const gchar * key,
00317                       gdouble value)
00318 {
00319     g_return_if_fail (db && db == config_handle);
00320 
00321     if (!section)
00322         section = RCFILE_DEFAULT_SECTION_NAME;
00323 
00324     mcs_set_double(db, section, key, value);
00325 }
00326 
00334 void
00335 cfg_db_unset_key(mcs_handle_t * db,
00336                      const gchar * section,
00337                      const gchar * key)
00338 {
00339     g_return_if_fail (db && db == config_handle);
00340     g_return_if_fail(key != NULL);
00341 
00342     if (!section)
00343         section = RCFILE_DEFAULT_SECTION_NAME;
00344 
00345     mcs_unset_key(db, section, key);
00346 }