~onli/simdock/master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*  
 *   Copyright 2007 Simone Della Longa <simonedll@yahoo.it>
 *
 *  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 "sim_gconf.h"


static gboolean
gconf_key_exist(GConfClient *gconf_client, const gchar *key)
{
    GConfValue *value;
    gboolean result = FALSE;
    
    value = gconf_client_get(gconf_client, key, NULL);
    if (value != NULL) {
        gconf_value_free(value);
        result = TRUE;
    }
    return result;
}


bool
SimGconf::init ()
{
  /*
   * TODO: Add argc and argv to gconf init ?
   */
  g_type_init ();
  GError *err = NULL;

  if (!gconf_init (0, NULL, &err))
    {
      printf ("Failed to init GConf: %s", err->message);
      g_error_free (err);
      err = NULL;

      return false;
    }
  //initialized = true;
  return true;
}

GConfClient *
SimGconf::GetClient ()
{
  return gconf_client_get_default ();
}

bool
SimGconf::CreateDirectory (GConfClient * client, const char *key)
{
  if (!gconf_client_dir_exists (client, key, NULL))
    {
      gconf_client_add_dir (client, key, GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
    }
  return true;
}

bool
SimGconf::GetString (GConfClient * client, const char *key, wxString * result)
{
  gchar *str;
  str = gconf_client_get_string (client, key, NULL);
  if (str == NULL)
    return false;
  *result = wxString::FromAscii (str);
  g_free (str);
  return true;
}

bool
SimGconf::SetString (GConfClient * client, const char *key, wxString * value)
{
    std::string tmp = wx2std(*value);
    const gchar *data = tmp.c_str ();
    bool result = gconf_client_set_string (client, key, data, NULL);
    return result;
}

bool
SimGconf::GetInt (GConfClient * client, const char *key, int *result)
{
  if (!gconf_key_exist(client, key))
	return false;
	
  GError *err = NULL;
  int v = gconf_client_get_int (client, key, &err);
  
  if (err)
    {
      printf ("Failed to get value: %s", err->message);
      g_error_free (err);
      return false;
    }
#ifdef SIMDOCK_DEBUG
  printf ("GCONF: %s=%d\n", key, v);
#endif
  *result = v;
  return true;
}

bool
SimGconf::SetInt (GConfClient * client, const char *key, int *value)
{
#ifdef SIMDOCK_DEBUG
  printf ("setting %s\n", key);
#endif
  return gconf_client_set_int (client, key, *value, NULL);
}

bool
SimGconf::GetColour (GConfClient * client, const char *key, wxColour * result)
{
  gchar *str;
  str = gconf_client_get_string (client, key, NULL);
  if (str == NULL)
    return false;

  result->Set (wxString::FromAscii (str));
  g_free (str);
  return true;

}

bool
SimGconf::SetColour (GConfClient * client, const char *key, wxColour * value)
{
  wxString val = value->GetAsString (wxC2S_HTML_SYNTAX);
  return SimGconf::SetString (client, key, &val);

}

bool
SimGconf::GetBool (GConfClient * client, const char *key, bool * result)
{
  int res;
  if (SimGconf::GetInt (client, key, &res))
    {
      if (res)
	*result = true;
      else
	*result = false;
      return true;

    }
  return false;
}

bool
SimGconf::SetBool (GConfClient * client, const char *key, bool * value)
{
  int v = (int) *value;
  return SimGconf::SetInt (client, key, &v);

}