Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=gservice.git;a=commitdiff;h=d05833cb1986270436ed29bf0bb2e891b7ebf6f8
commit d05833cb1986270436ed29bf0bb2e891b7ebf6f8 Author: Priyank <priy...@frugalware.org> Date: Mon Jan 19 18:04:14 2009 +0530 gservice-config: initial import * Added a configuration file parser diff --git a/src/Makefile.am b/src/Makefile.am index 0a5fe69..606ff70 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -7,6 +7,7 @@ bin_PROGRAMS=gservice gservice_SOURCES= \ gservice.c \ gservice-backend.c \ + gservice-config.c \ gservice-icon-mapping.c \ gservice-interface.c \ gservice-messages.c \ diff --git a/src/gservice-config.c b/src/gservice-config.c new file mode 100644 index 0000000..8adb735 --- /dev/null +++ b/src/gservice-config.c @@ -0,0 +1,189 @@ +/* + * gservice-config.c for gService + * + * Copyright (C) 2008-2009 by Priyank Gosalia <priyan...@gmail.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <gtk/gtk.h> +#include "gservice-config.h" + +#define CONFIG_FILE "/etc/gservice/main.conf" + +struct _gsvc_config_t { + gboolean show_warning; + gboolean show_icons; + gboolean show_status; +}; + +static gboolean _gsvc_write_config (gsvc_config_t *cfg); + +gsvc_config_t * +gsvc_config_new (void) +{ + gsvc_config_t *ret = NULL; + + ret = (gsvc_config_t*) malloc (sizeof(gsvc_config_t)); + if (ret) + { + memset (ret, 0, sizeof(gsvc_config_t)); + } + + return ret; +} + +guint +gsvc_config_init (gsvc_config_t *cfg) +{ + FILE *fp = NULL; + char line[PATH_MAX+1] = ""; + char *tmp = NULL; + + if (cfg == NULL) + { + return 1; + } + fp = fopen (CONFIG_FILE, "r"); + if (fp) + { + while (fgets(line,PATH_MAX,fp)) + { + g_strstrip (line); + if (line[0] == '#' || strlen(line) == 0) + continue; + tmp = strtok (line, "="); + if (tmp != NULL) + { + char *var = g_strdup (tmp); + char *val = strtok (NULL, "="); + char *tval = NULL; + if (!val) continue; + tval = g_strdup (val); + g_strstrip (tval); + g_strstrip (var); + if (!strcmp(var,"show_warning")) + { + if (!strcmp("true",tval)) + cfg->show_warning = TRUE; + else + cfg->show_warning = FALSE; + } + else + if (!strcmp(var,"show_icons")) + { + if (!strcmp("true",tval)) + cfg->show_icons = TRUE; + else + cfg->show_warning = FALSE; + } + else + if (!strcmp(var,"show_status")) + { + if (!strcmp("true",tval)) + cfg->show_status = TRUE; + else + cfg->show_warning = FALSE; + } + else + { + g_free (var); + g_free (tval); + continue; + } + g_free (var); + g_free (tval); + } + } + fclose (fp); + return 0; + } + + return 1; +} + +gboolean +gsvc_config_get_bool (gsvc_config_t *cfg, const char *key) +{ + gsvc_config_t *c = cfg; + + if (!c) + { + return FALSE; + } + if (!strcmp(key,"show_warning")) + return c->show_warning; + else + if (!strcmp(key,"show_icons")) + return c->show_icons; + else + if (!strcmp(key,"show_status")) + return c->show_status; + + return FALSE; +} + +gboolean +gsvc_config_set_bool (gsvc_config_t *cfg, const char *key, gboolean val) +{ + gsvc_config_t *c = cfg; + + if (!c || !key) + { + return FALSE; + } + if (!strcmp(key,"show_warning")) + c->show_warning = val; + else + if (!strcmp(key,"show_icons")) + c->show_warning = val; + else + if (!strcmp(key,"show_status")) + c->show_warning = val; + + /* write the config to file */ + return (_gsvc_write_config(c)); +} + +/* private functions */ + +/* stores config values to a file */ +static gboolean +_gsvc_write_config (gsvc_config_t *cfg) +{ + FILE *fp = NULL; + + if (!cfg) return FALSE; + fp = fopen (CONFIG_FILE,"w"); + if (!fp) return FALSE; + + fprintf (fp, "# gService configuration file\n\n"); + fprintf (fp, "# show a warning at startup"); + fprintf (fp, "show_warning = %s", (cfg->show_warning)?"true":"false"); + fprintf (fp, "# show service icons"); + fprintf (fp, "show_icons = %s", (cfg->show_icons)?"true":"false"); + fprintf (fp, "# show status window"); + fprintf (fp, "show_status = %s", (cfg->show_status)?"true":"false"); + fclose (fp); + + return TRUE; +} + + + diff --git a/src/gservice-config.h b/src/gservice-config.h new file mode 100644 index 0000000..0f5295e --- /dev/null +++ b/src/gservice-config.h @@ -0,0 +1,22 @@ +#ifndef __GSERVICE_CONFIG_H__ +#define __GSERVICE_CONFIG_H__ + +#include "gservice.h" + +typedef struct _gsvc_config_t gsvc_config_t; + +/* parses the config file and populates the config structure */ +/* returns 0 on success, 1 on failure */ +guint gsvc_config_init (gsvc_config_t *cfg); + +/* returns a newly allocated conf structure */ +gsvc_config_t *gsvc_config_new (void); + +/* set a boolean value for a configuration key + * returns TRUE on success, FALSE on error */ +gboolean gsvc_config_set_bool (gsvc_config_t *cfg, const char *key, gboolean val); + +/* get the boolean value for a specified key */ +gboolean gsvc_config_get_bool (gsvc_config_t *cfg, const char *key); + +#endif diff --git a/src/gservice-interface.c b/src/gservice-interface.c index 3aad5ed..1d7aaae 100644 --- a/src/gservice-interface.c +++ b/src/gservice-interface.c @@ -20,6 +20,7 @@ #include "gservice-interface.h" #include "gservice-backend.h" +#include "gservice-config.h" #include "gservice-icon-mapping.h" #include "gservice-about.h" @@ -50,6 +51,8 @@ extern GladeXML *xml; static GList *gsvc_service_list = NULL; +static gsvc_config_t *conf = NULL; + /* callbacks */ static void cb_gsvc_r0_toggled (GtkCellRendererToggle *toggle, gchar *path_str, gpointer data); static void cb_gsvc_r1_toggled (GtkCellRendererToggle *toggle, gchar *path_str, gpointer data); @@ -303,6 +306,14 @@ gservice_push_status (const char *status) void gservice_interface_init (void) { + /* initialize configuration */ + conf = gsvc_config_new (); + if (gsvc_config_init(conf)) + { + gsvc_error (_("Error"), _("Failed to initialize configuration. Please check that a valid configuration file exists in /etc/gservice/main.conf")); + gtk_main_quit (); + } + gsvc_main_window = gservice_get_widget ("gsvc_main_window"); gsvc_splash_screen = gservice_get_widget ("gsvc_splash_screen"); gsvc_treeview_services = gservice_get_widget ("gsvc_treeview_services"); _______________________________________________ Frugalware-git mailing list Frugalware-git@frugalware.org http://frugalware.org/mailman/listinfo/frugalware-git