mcabber/src/settings.c
author Mikael Berthe <mikael@lilotux.net>
Tue, 05 Jul 2005 23:50:50 +0100
changeset 279 f5dd437c057b
child 280 68ce34b4243b
permissions -rw-r--r--
Rewrite the settings system Settings now use glibc library, and can be used for aliases and bindings.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
279
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
/*
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
 * settings.c   -- Configuration stuff
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
 * 
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
 * Copyright (C) 2005 Mikael Berthe <bmikael@lists.lilotux.net>
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
 *
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License as published by
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
 * the Free Software Foundation; either version 2 of the License, or (at
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
 * your option) any later version.
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
 *
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
 * This program is distributed in the hope that it will be useful, but
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
 * WITHOUT ANY WARRANTY; without even the implied warranty of
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
 * General Public License for more details.
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
 *
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
 * along with this program; if not, write to the Free Software
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
 * USA
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
 */
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
#include <strings.h>
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
#include <stdlib.h>
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
#include "settings.h"
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
static GSList *option;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
static GSList *alias;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
static GSList *binding;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
typedef struct {
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
  gchar *name;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
  gchar *value;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
} T_setting;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
inline GSList **get_list_ptr(guint type)
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
{
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
  if      (type == SETTINGS_TYPE_OPTION)  return &option;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
  else if (type == SETTINGS_TYPE_ALIAS)   return &alias;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
  else if (type == SETTINGS_TYPE_BINDING) return &binding;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
  return NULL;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
}
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
// Return a pointer to the node with the requested key, or NULL if none found
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
GSList *settings_find(GSList *list, gchar *key)
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
{
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
  GSList *ptr;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
  
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
  if (!list) return NULL;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
  for (ptr = list ; ptr; ptr = g_slist_next(ptr))
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
    if (!strcasecmp(key, ((T_setting*)ptr->data)->name))
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
      break;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
  return ptr;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
}
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
/* -- */
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
void settings_set(guint type, gchar *key, gchar *value)
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
{
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
  GSList **plist;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
  GSList *sptr;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    65
  T_setting *setting;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
  plist = get_list_ptr(type);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    68
  if (!plist) return;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    69
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    70
  sptr = settings_find(*plist, key);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    71
  if (sptr) {
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    72
    // The setting has been found.  We will update it or delete it.
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    73
    setting = (T_setting*)sptr->data;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    74
    if (setting->value)
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    75
      g_free(setting->value);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    76
    if (!value) {
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    77
      // Let's remove the setting
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    78
      g_free(setting->name);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    79
      *plist = g_slist_delete_link(*plist, sptr);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    80
    } else {
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    81
      // Let's update the setting
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    82
      setting->value = g_strdup(value);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    83
    }
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    84
  } else if (value) {
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    85
    setting = g_new(T_setting, 1);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    86
    setting->name  = g_strdup(key);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    87
    setting->value = g_strdup(value);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    88
    *plist = g_slist_append(*plist, setting);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    89
  }
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    90
}
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    91
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    92
void settings_del(guint type, gchar *key)
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    93
{
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    94
  settings_set(type, key, NULL);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    95
}
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    96
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    97
gchar *settings_get(guint type, gchar *key)
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    98
{
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    99
  GSList **plist;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   100
  GSList *sptr;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   101
  T_setting *setting;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   102
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   103
  plist = get_list_ptr(type);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   104
  sptr = settings_find(*plist, key);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   105
  if (!sptr) return NULL;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   106
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   107
  setting = (T_setting*)sptr->data;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   108
  return setting->value;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   109
}
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   110
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   111
int settings_get_int(guint type, gchar *key)
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   112
{
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   113
  gchar *setval = settings_get(type, key);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   114
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   115
  if (setval) return atoi(setval);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   116
  return 0;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   117
}
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   118