mcabber/src/settings.c
author Mikael Berthe <mikael@lilotux.net>
Mon, 18 Jul 2005 21:47:05 +0100
changeset 337 3a25be278864
parent 336 eb994ee40029
child 341 dea407d53fe6
permissions -rw-r--r--
Values can be enclosed by quotes in assignments Thus, passwords with leading/trailing spaces can be typed. Examples: set password = " with spaces " set password = "with "quotes""
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>
280
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    24
#include <ctype.h>
279
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
#include "settings.h"
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
static GSList *option;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
static GSList *alias;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
static GSList *binding;
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
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
typedef struct {
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
  gchar *name;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
  gchar *value;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
} T_setting;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
336
eb994ee40029 Make some functions static
Mikael Berthe <mikael@lilotux.net>
parents: 294
diff changeset
    38
static inline GSList **get_list_ptr(guint type)
279
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
{
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
  if      (type == SETTINGS_TYPE_OPTION)  return &option;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
  else if (type == SETTINGS_TYPE_ALIAS)   return &alias;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
  else if (type == SETTINGS_TYPE_BINDING) return &binding;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
  return NULL;
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
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
// Return a pointer to the node with the requested key, or NULL if none found
336
eb994ee40029 Make some functions static
Mikael Berthe <mikael@lilotux.net>
parents: 294
diff changeset
    47
static GSList *settings_find(GSList *list, const gchar *key)
279
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
{
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
  GSList *ptr;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
  
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
  if (!list) return NULL;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
  for (ptr = list ; ptr; ptr = g_slist_next(ptr))
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
    if (!strcasecmp(key, ((T_setting*)ptr->data)->name))
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
      break;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
  return ptr;
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
280
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    62
//  parse_assigment(assignment, pkey, pval)
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    63
// Read assignment and split it to key, value
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    64
//
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    65
// If this is an assignment, the function will return TRUE and
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    66
// set *pkey and *pval (*pval is set to NULL if value field is empty).
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    67
//
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    68
// If this isn't a assignment (no = char), the function will set *pval
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    69
// to NULL and return FALSE.
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    70
//
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    71
// The called should g_free() *pkey and *pval (if not NULL) after use.
281
f562b9af2de7 Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents: 280
diff changeset
    72
guint parse_assigment(gchar *assignment, const gchar **pkey, const gchar **pval)
280
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    73
{
293
d0295e735768 Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents: 288
diff changeset
    74
  char *key, *val, *t, *p;
280
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    75
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    76
  *pkey = *pval = NULL;
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    77
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    78
  key = assignment;
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    79
  // Remove leading spaces in option name
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    80
  while ((!isalnum(*key)) && (*key != '=') && *key) {
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    81
    //if (!isblank(*key))
293
d0295e735768 Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents: 288
diff changeset
    82
    //  scr_LogPrint("Error in assignment parsing!");
280
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    83
    key++;
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    84
  }
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    85
  if (!*key) return FALSE; // Empty assignment
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    86
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    87
  if (*key == '=') {
293
d0295e735768 Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents: 288
diff changeset
    88
    //scr_LogPrint("Cannot parse assignment!");
280
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    89
    return FALSE;
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    90
  }
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    91
  // Ok, key points to the option name
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    92
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    93
  for (val = key+1 ; *val && (*val != '=') ; val++)
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    94
    if (!isalnum(*val) && !isblank(*val) && (*val != '_') && (*val != '-')) {
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    95
      // Key should only have alnum chars...
293
d0295e735768 Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents: 288
diff changeset
    96
      //scr_LogPrint("Error in assignment parsing!");
280
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    97
      return FALSE;
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    98
    }
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
    99
  // Remove trailing spaces in option name:
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
   100
  for (t = val-1 ; t > key && isblank(*t) ; t--)
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
   101
    ;
293
d0295e735768 Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents: 288
diff changeset
   102
  // Check for embedded whitespace characters
d0295e735768 Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents: 288
diff changeset
   103
  for (p = key; p < t; p++) {
d0295e735768 Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents: 288
diff changeset
   104
    if (isblank(*p)) {
d0295e735768 Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents: 288
diff changeset
   105
      //scr_LogPrint("Error in assignment parsing!"
d0295e735768 Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents: 288
diff changeset
   106
      //             " (Name should not contain space chars)");
d0295e735768 Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents: 288
diff changeset
   107
      return FALSE;
d0295e735768 Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents: 288
diff changeset
   108
    }
d0295e735768 Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents: 288
diff changeset
   109
  }
d0295e735768 Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents: 288
diff changeset
   110
280
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
   111
  *pkey = g_strndup(key, t+1-key);
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
   112
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
   113
  if (!*val) return FALSE; // Not an assignment
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
   114
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
   115
  // Remove leading and trailing spaces in option value:
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
   116
  for (val++; *val && isblank(*val) ; val++) ;
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
   117
  for (t = val ; *t ; t++) ;
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
   118
  for (t-- ; t >= val && isblank(*t) ; t--) ;
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
   119
282
87d6ac21cd1b Fix "Cannot unset option" bug
Mikael Berthe <mikael@lilotux.net>
parents: 281
diff changeset
   120
  if (t < val) return TRUE; // no value (variable reset for example)
280
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
   121
337
3a25be278864 Values can be enclosed by quotes in assignments
Mikael Berthe <mikael@lilotux.net>
parents: 336
diff changeset
   122
  // If the value begins and ends with quotes ("), these quotes are
3a25be278864 Values can be enclosed by quotes in assignments
Mikael Berthe <mikael@lilotux.net>
parents: 336
diff changeset
   123
  // removed and whitespace is not stripped
3a25be278864 Values can be enclosed by quotes in assignments
Mikael Berthe <mikael@lilotux.net>
parents: 336
diff changeset
   124
  if ((t>val) && (*val == '"' && *t == '"')) {
3a25be278864 Values can be enclosed by quotes in assignments
Mikael Berthe <mikael@lilotux.net>
parents: 336
diff changeset
   125
    val++;
3a25be278864 Values can be enclosed by quotes in assignments
Mikael Berthe <mikael@lilotux.net>
parents: 336
diff changeset
   126
    t--;
3a25be278864 Values can be enclosed by quotes in assignments
Mikael Berthe <mikael@lilotux.net>
parents: 336
diff changeset
   127
  }
280
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
   128
  *pval = g_strndup(val, t+1-val);
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
   129
  return TRUE;
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
   130
}
68ce34b4243b Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents: 279
diff changeset
   131
281
f562b9af2de7 Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents: 280
diff changeset
   132
void settings_set(guint type, const gchar *key, const gchar *value)
279
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   133
{
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   134
  GSList **plist;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   135
  GSList *sptr;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   136
  T_setting *setting;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   137
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   138
  plist = get_list_ptr(type);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   139
  if (!plist) return;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   140
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   141
  sptr = settings_find(*plist, key);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   142
  if (sptr) {
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   143
    // 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
   144
    setting = (T_setting*)sptr->data;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   145
    if (setting->value)
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   146
      g_free(setting->value);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   147
    if (!value) {
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   148
      // Let's remove the setting
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   149
      g_free(setting->name);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   150
      *plist = g_slist_delete_link(*plist, sptr);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   151
    } else {
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   152
      // Let's update the setting
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   153
      setting->value = g_strdup(value);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   154
    }
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   155
  } else if (value) {
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   156
    setting = g_new(T_setting, 1);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   157
    setting->name  = g_strdup(key);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   158
    setting->value = g_strdup(value);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   159
    *plist = g_slist_append(*plist, setting);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   160
  }
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   161
}
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   162
281
f562b9af2de7 Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents: 280
diff changeset
   163
void settings_del(guint type, const gchar *key)
279
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   164
{
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   165
  settings_set(type, key, NULL);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   166
}
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   167
281
f562b9af2de7 Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents: 280
diff changeset
   168
const gchar *settings_get(guint type, const gchar *key)
279
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   169
{
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   170
  GSList **plist;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   171
  GSList *sptr;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   172
  T_setting *setting;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   173
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   174
  plist = get_list_ptr(type);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   175
  sptr = settings_find(*plist, key);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   176
  if (!sptr) return NULL;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   177
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   178
  setting = (T_setting*)sptr->data;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   179
  return setting->value;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   180
}
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   181
281
f562b9af2de7 Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents: 280
diff changeset
   182
int settings_get_int(guint type, const gchar *key)
279
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   183
{
281
f562b9af2de7 Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents: 280
diff changeset
   184
  const gchar *setval = settings_get(type, key);
279
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   185
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   186
  if (setval) return atoi(setval);
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   187
  return 0;
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   188
}
f5dd437c057b Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   189
288
1eea0fa0955e Add /bind command
Mikael Berthe <mikael@lilotux.net>
parents: 282
diff changeset
   190
// Return the command the key is bound to, or NULL.
1eea0fa0955e Add /bind command
Mikael Berthe <mikael@lilotux.net>
parents: 282
diff changeset
   191
const gchar *isbound(int key)
1eea0fa0955e Add /bind command
Mikael Berthe <mikael@lilotux.net>
parents: 282
diff changeset
   192
{
1eea0fa0955e Add /bind command
Mikael Berthe <mikael@lilotux.net>
parents: 282
diff changeset
   193
  gchar asciikey[16];
1eea0fa0955e Add /bind command
Mikael Berthe <mikael@lilotux.net>
parents: 282
diff changeset
   194
  g_snprintf(asciikey, 15, "%d", key);
1eea0fa0955e Add /bind command
Mikael Berthe <mikael@lilotux.net>
parents: 282
diff changeset
   195
  return settings_get(SETTINGS_TYPE_BINDING, asciikey);
1eea0fa0955e Add /bind command
Mikael Berthe <mikael@lilotux.net>
parents: 282
diff changeset
   196
}
1eea0fa0955e Add /bind command
Mikael Berthe <mikael@lilotux.net>
parents: 282
diff changeset
   197
294
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   198
//  settings_get_status_msg(status)
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   199
// Return a string with the current status message:
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   200
// - if there is a user-defined message ("message" option),
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   201
//   return this message
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   202
// - if there is a user-defined message for the given status (and no
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   203
//   generic user message), it is returned
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   204
// - if no user-defined message is found, return the mcabber default msg
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   205
// - if there is no default (offline, invisible), return an empty string
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   206
const gchar *settings_get_status_msg(enum imstatus status)
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   207
{
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   208
  const gchar *rstatus = settings_opt_get("message");
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   209
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   210
  if (rstatus) return rstatus;
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   211
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   212
  switch(status) {
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   213
    case available:
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   214
        if ((rstatus = settings_opt_get("message_avail")) == NULL)
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   215
          rstatus = MSG_AVAIL;
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   216
        break;
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   217
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   218
    case freeforchat:
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   219
        if ((rstatus = settings_opt_get("message_free")) == NULL)
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   220
          rstatus = MSG_FREE;
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   221
        break;
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   222
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   223
    case dontdisturb:
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   224
        if ((rstatus = settings_opt_get("message_dnd")) == NULL)
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   225
          rstatus = MSG_DND;
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   226
        break;
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   227
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   228
    case notavail:
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   229
        if ((rstatus = settings_opt_get("message_notavail")) == NULL)
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   230
          rstatus = MSG_NOTAVAIL;
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   231
        break;
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   232
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   233
    case away:
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   234
        if ((rstatus = settings_opt_get("message_away")) == NULL)
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   235
          rstatus = MSG_AWAY;
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   236
        break;
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   237
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   238
    default:
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   239
        rstatus = "";
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   240
  }
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   241
  return rstatus;
871e53769084 Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents: 293
diff changeset
   242
}