changeset 279:f5dd437c057b

Rewrite the settings system Settings now use glibc library, and can be used for aliases and bindings.
author Mikael Berthe <mikael@lilotux.net>
date Tue, 05 Jul 2005 23:50:50 +0100
parents a7af5517b453
children 68ce34b4243b
files mcabber/src/Makefile.am mcabber/src/main.c mcabber/src/parsecfg.c mcabber/src/screen.c mcabber/src/settings.c mcabber/src/settings.h
diffstat 6 files changed, 163 insertions(+), 76 deletions(-) [+]
line wrap: on
line diff
--- a/mcabber/src/Makefile.am	Tue Jul 05 23:30:10 2005 +0100
+++ b/mcabber/src/Makefile.am	Tue Jul 05 23:50:50 2005 +0100
@@ -2,9 +2,9 @@
 mcabber_SOURCES = main.c jabglue.c jabglue.h roster.c roster.h \
 		  commands.c commands.h compl.c compl.h \
 		  hbuf.c hbuf.h screen.c screen.h \
-		  parsecfg.c parsecfg.h utf8.c utf8.h \
+		  settings.c settings.h parsecfg.c parsecfg.h \
 		  hooks.c hooks.h histolog.c histolog.h \
-		  utils.c utils.h list.h harddefines.h
+		  utf8.c utf8.h utils.c utils.h list.h harddefines.h
 
 LDADD = -lglib-2.0 -lncurses -lpanel \
 	../libjabber/liblibjabber.a ../connwrap/libconnwrap.a
--- a/mcabber/src/main.c	Tue Jul 05 23:30:10 2005 +0100
+++ b/mcabber/src/main.c	Tue Jul 05 23:50:50 2005 +0100
@@ -13,6 +13,7 @@
 #include "jabglue.h"
 #include "screen.h"
 #include "parsecfg.h"
+#include "settings.h"
 #include "roster.h"
 #include "commands.h"
 #include "histolog.h"
@@ -132,13 +133,13 @@
   cfg_file(configFile);
   if (configFile) g_free(configFile);
 
-  optstring = cfg_read("debug");
+  optstring = settings_opt_get("debug");
   if (optstring) ut_InitDebug(1, optstring);
 
-  servername = cfg_read("server");
-  username   = cfg_read("username");
-  password   = cfg_read("password");
-  resource   = cfg_read("resource");
+  servername = settings_opt_get("server");
+  username   = settings_opt_get("username");
+  password   = settings_opt_get("password");
+  resource   = settings_opt_get("resource");
 
   if (!servername) {
       printf("Server name has not been specified in the config file!\n");
@@ -166,18 +167,20 @@
   ut_WriteLog("Drawing main window...\n");
   scr_DrawMainWindow(TRUE);
 
-  optval   = (cfg_read_int("logging") > 0);
-  optval2  = (cfg_read_int("load_logs") > 0);
+  optval   = (settings_opt_get_int("logging") > 0);
+  optval2  = (settings_opt_get_int("load_logs") > 0);
   if (optval || optval2)
-    hlog_enable(optval, cfg_read("logging_dir"), optval2);
+    hlog_enable(optval, settings_opt_get("logging_dir"),
+                optval2);
 
-  if ((optstring = cfg_read("events_command")) != NULL)
+  optstring = settings_opt_get("events_command");
+  if (optstring)
     hk_ext_cmd_init(optstring);
 
-  ssl  = (cfg_read_int("ssl") > 0);
-  port = (unsigned int) cfg_read_int("port");
+  ssl  = (settings_opt_get_int("ssl") > 0);
+  port = (unsigned int) settings_opt_get_int("port");
 
-  jb_set_priority(cfg_read_int("priority"));
+  jb_set_priority(settings_opt_get_int("priority"));
 
   /* Connect to server */
   ut_WriteLog("Connecting to server: %s:%d\n", servername, port);
@@ -194,12 +197,12 @@
   }
 
   ping = 40;
-  if (cfg_read("pinginterval"))
-    ping = (unsigned int) atoi(cfg_read("pinginterval"));
+  if (settings_opt_get("pinginterval"))
+    ping = (unsigned int) settings_opt_get_int("pinginterval");
   jb_set_keepalive_delay(ping);
   ut_WriteLog("Ping interval stablished: %d secs\n", ping);
 
-  if (cfg_read_int("hide_offline_buddies") > 0)
+  if (settings_opt_get_int("hide_offline_buddies") > 0)
     buddylist_set_hide_offline_buddies(TRUE);
 
   /* Initialize commands system */
--- a/mcabber/src/parsecfg.c	Tue Jul 05 23:30:10 2005 +0100
+++ b/mcabber/src/parsecfg.c	Tue Jul 05 23:50:50 2005 +0100
@@ -5,34 +5,9 @@
 #include <string.h>
 #include <glib.h>
 
-#include "list.h"
+#include "settings.h"
 #include "utils.h"
 
-#define MAX_LENGHT_INPUT 1024
-#define cfg_entry(n) list_entry(n, cfg_entry_t, list)
-
-typedef struct _cfg_entry_t {
-  char *key;
-  char *value;
-  struct list_head list;
-} cfg_entry_t;
-
-static LIST_HEAD(cfg_list);
-
-
-void push_in_list(char *key, char *value)
-{
-  cfg_entry_t *new_entry  = (cfg_entry_t*)g_new0(char, sizeof(cfg_entry_t));
-
-  new_entry->key          = g_new0(char, strlen(key) + 1);
-  new_entry->value        = g_new0(char, strlen(value) + 1);
-
-  strcpy(new_entry->key, key);
-  strcpy(new_entry->value, value);
-
-  list_add(&new_entry->list, &cfg_list);
-}
-
 int cfg_file(char *filename)
 {
   FILE *fp;
@@ -91,7 +66,7 @@
 	     && isspace((int) line[strlen(line) - 1]))
 	line[strlen(line) - 1] = '\0';
 
-      push_in_list(line, value);
+      settings_set(SETTINGS_TYPE_OPTION, line, value);
       continue;
     }
     fprintf(stderr, "CFG: orphaned line \"%s\"\n", line);
@@ -99,31 +74,3 @@
   g_free(buf);
   return 1;
 }
-
-char *cfg_read(char *key)
-{
-  struct list_head *n, *pos;
-  cfg_entry_t *search_entry = NULL;
-
-  list_for_each_safe(pos, n, &cfg_list) {
-    search_entry = cfg_entry(pos);
-    if (search_entry->key) {
-      if (!strcasecmp(search_entry->key, key)) {
-	return search_entry->value;
-      }
-    }
-  }
-  return NULL;
-}
-
-int cfg_read_int(char *key)
-{
-  char *optval;
-
-  optval = cfg_read(key);
-
-  if (optval)
-    return atoi(optval);
-
-  return 0;
-}
--- a/mcabber/src/screen.c	Tue Jul 05 23:30:10 2005 +0100
+++ b/mcabber/src/screen.c	Tue Jul 05 23:50:50 2005 +0100
@@ -14,7 +14,7 @@
 #include "compl.h"
 #include "roster.h"
 #include "histolog.h"
-#include "parsecfg.h"
+#include "settings.h"
 #include "utils.h"
 #include "list.h"
 
@@ -145,8 +145,8 @@
 
   char *tmp = malloc(1024);
   char *color;
-  char *background = cfg_read("color_background");
-  char *backselected = cfg_read("color_backselected");
+  char *background = settings_opt_get("color_background");
+  char *backselected = settings_opt_get("color_backselected");
   int i = 0;
 
   // Default values
@@ -155,7 +155,7 @@
 
   while (colors[i]) {
     sprintf(tmp, "color_%s", colors[i]);
-    color = cfg_read(tmp);
+    color = settings_opt_get(tmp);
 
     switch (i + 1) {
     case 1:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mcabber/src/settings.c	Tue Jul 05 23:50:50 2005 +0100
@@ -0,0 +1,118 @@
+/*
+ * settings.c   -- Configuration stuff
+ * 
+ * Copyright (C) 2005 Mikael Berthe <bmikael@lists.lilotux.net>
+ *
+ * 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
+ */
+
+#include <strings.h>
+#include <stdlib.h>
+
+#include "settings.h"
+
+static GSList *option;
+static GSList *alias;
+static GSList *binding;
+
+
+typedef struct {
+  gchar *name;
+  gchar *value;
+} T_setting;
+
+inline GSList **get_list_ptr(guint type)
+{
+  if      (type == SETTINGS_TYPE_OPTION)  return &option;
+  else if (type == SETTINGS_TYPE_ALIAS)   return &alias;
+  else if (type == SETTINGS_TYPE_BINDING) return &binding;
+  return NULL;
+}
+
+// Return a pointer to the node with the requested key, or NULL if none found
+GSList *settings_find(GSList *list, gchar *key)
+{
+  GSList *ptr;
+  
+  if (!list) return NULL;
+
+  for (ptr = list ; ptr; ptr = g_slist_next(ptr))
+    if (!strcasecmp(key, ((T_setting*)ptr->data)->name))
+      break;
+
+  return ptr;
+}
+
+/* -- */
+
+void settings_set(guint type, gchar *key, gchar *value)
+{
+  GSList **plist;
+  GSList *sptr;
+  T_setting *setting;
+
+  plist = get_list_ptr(type);
+  if (!plist) return;
+
+  sptr = settings_find(*plist, key);
+  if (sptr) {
+    // The setting has been found.  We will update it or delete it.
+    setting = (T_setting*)sptr->data;
+    if (setting->value)
+      g_free(setting->value);
+    if (!value) {
+      // Let's remove the setting
+      g_free(setting->name);
+      *plist = g_slist_delete_link(*plist, sptr);
+    } else {
+      // Let's update the setting
+      setting->value = g_strdup(value);
+    }
+  } else if (value) {
+    setting = g_new(T_setting, 1);
+    setting->name  = g_strdup(key);
+    setting->value = g_strdup(value);
+    *plist = g_slist_append(*plist, setting);
+  }
+}
+
+void settings_del(guint type, gchar *key)
+{
+  settings_set(type, key, NULL);
+}
+
+gchar *settings_get(guint type, gchar *key)
+{
+  GSList **plist;
+  GSList *sptr;
+  T_setting *setting;
+
+  plist = get_list_ptr(type);
+  sptr = settings_find(*plist, key);
+  if (!sptr) return NULL;
+
+  setting = (T_setting*)sptr->data;
+  return setting->value;
+}
+
+int settings_get_int(guint type, gchar *key)
+{
+  gchar *setval = settings_get(type, key);
+
+  if (setval) return atoi(setval);
+  return 0;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mcabber/src/settings.h	Tue Jul 05 23:50:50 2005 +0100
@@ -0,0 +1,19 @@
+#ifndef __SETTINGS_H__
+#define __SETTINGS_H__ 1
+
+#include <glib.h>
+
+#define SETTINGS_TYPE_OPTION    1
+#define SETTINGS_TYPE_ALIAS     2
+#define SETTINGS_TYPE_BINDING   3
+
+#define settings_opt_get(k)     settings_get(SETTINGS_TYPE_OPTION, k)
+#define settings_opt_get_int(k) settings_get_int(SETTINGS_TYPE_OPTION, k)
+
+void    settings_set(guint type, gchar *key, gchar *value);
+void    settings_del(guint type, gchar *key);
+gchar  *settings_get(guint type, gchar *key);
+int     settings_get_int(guint type, gchar *key);
+
+#endif /* __SETTINGS_H__ */
+