~golfish/netbook-remix-launcher/desktop

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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#ifndef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <string.h>

#include <glib-object.h>
#include <gobject/gvaluecollector.h>

#include <clutter/clutter-color.h>

#include "tidy-style.h"
#include "tidy-marshal.h"
#include "tidy-debug.h"

enum
{
  CHANGED,

  LAST_SIGNAL
};

#define TIDY_STYLE_GET_PRIVATE(obj) \
        (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TIDY_TYPE_STYLE, TidyStylePrivate))

typedef struct {
  GType value_type;
  gchar *value_name;
  GValue value;
} StyleProperty;

struct _TidyStylePrivate
{
  GHashTable *properties;
};

static guint style_signals[LAST_SIGNAL] = { 0, };

static const gchar *tidy_default_font_name          = "Sans 12px";
static const ClutterColor tidy_default_text_color   = { 0x00, 0x00, 0x00, 0xff };
static const ClutterColor tidy_default_bg_color     = { 0xcc, 0xcc, 0xcc, 0xff };
static const ClutterColor tidy_default_active_color = { 0xf5, 0x79, 0x00, 0xff };

static TidyStyle *default_style = NULL;

G_DEFINE_TYPE (TidyStyle, tidy_style, G_TYPE_OBJECT);

static StyleProperty *
style_property_new (const gchar *value_name,
                    GType        value_type)
{
  StyleProperty *retval;

  retval = g_slice_new0 (StyleProperty);
  retval->value_type = value_type;
  retval->value_name = g_strdup (value_name);
  g_value_init (&retval->value, value_type);

  return retval;
}

static void
style_property_free (gpointer data)
{
  if (G_LIKELY (data))
    {
      StyleProperty *sp = data;

      g_free (sp->value_name);
      g_value_unset (&sp->value);
    }
}

static void
init_defaults (TidyStyle *style)
{
  TidyStylePrivate *priv = style->priv;
  
  {
    StyleProperty *sp;

    sp = style_property_new (TIDY_FONT_NAME, G_TYPE_STRING);
    g_value_set_string (&sp->value, tidy_default_font_name);

    g_hash_table_insert (priv->properties, sp->value_name, sp);
  }

  {
    StyleProperty *sp;

    sp = style_property_new (TIDY_BACKGROUND_COLOR, CLUTTER_TYPE_COLOR);
    g_value_set_boxed (&sp->value, &tidy_default_bg_color);

    g_hash_table_insert (priv->properties, sp->value_name, sp);
  }

  {
    StyleProperty *sp;

    sp = style_property_new (TIDY_ACTIVE_COLOR, CLUTTER_TYPE_COLOR);
    g_value_set_boxed (&sp->value, &tidy_default_active_color);

    g_hash_table_insert (priv->properties, sp->value_name, sp);
  }

  {
    StyleProperty *sp;

    sp = style_property_new (TIDY_TEXT_COLOR, CLUTTER_TYPE_COLOR);
    g_value_set_boxed (&sp->value, &tidy_default_text_color);

    g_hash_table_insert (priv->properties, sp->value_name, sp);
  }
}

static gboolean
tidy_style_load_from_file (TidyStyle    *style,
                           const gchar  *filename,
                           GError      **error)
{
  GKeyFile *rc_file;
  GError *internal_error;

  rc_file = g_key_file_new ();
  
  internal_error = NULL;
  g_key_file_load_from_file (rc_file, filename, 0, &internal_error);
  if (internal_error)
    {
      /* if the specified files does not exist then just ignore it
       * and fall back to the default values; if, instead, the file
       * is not accessible or is malformed, propagate the error
       */
      if (internal_error->domain == G_FILE_ERROR &&
          internal_error->code == G_FILE_ERROR_NOENT)
        {
          g_error_free (internal_error);
          return TRUE;
        }

      g_propagate_error (error, internal_error);
      return FALSE;
    }

  g_key_file_free (rc_file);

  return TRUE;
}

static void
tidy_style_load (TidyStyle *style)
{
  const gchar *env_var;
  gchar *rc_file = NULL;
  GError *error;
  
  init_defaults (style);

  env_var = g_getenv ("TIDY_RC_FILE");
  if (env_var && *env_var)
    rc_file = g_strdup (env_var);
  
  if (!rc_file)
    rc_file = g_build_filename (g_get_user_config_dir (),
                                "tidy",
                                "tidyrc",
                                NULL);

  error = NULL;
  if (!tidy_style_load_from_file (style, rc_file, &error))
    {
      g_critical ("Unable to load resource file `%s': %s",
                  rc_file,
                  error->message);
      g_error_free (error);
    }
}

static void
tidy_style_finalize (GObject *gobject)
{
  TidyStylePrivate *priv = TIDY_STYLE (gobject)->priv;

  g_hash_table_destroy (priv->properties);

  G_OBJECT_CLASS (tidy_style_parent_class)->finalize (gobject);
}

static void
tidy_style_class_init (TidyStyleClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  g_type_class_add_private (klass, sizeof (TidyStylePrivate));

  gobject_class->finalize = tidy_style_finalize;

  style_signals[CHANGED] =
    g_signal_new ("changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (TidyStyleClass, changed),
                  NULL, NULL,
                  _tidy_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
}

static void
tidy_style_init (TidyStyle *style)
{
  TidyStylePrivate *priv;

  style->priv = priv = TIDY_STYLE_GET_PRIVATE (style);

  priv->properties = g_hash_table_new_full (g_str_hash, g_str_equal,
                                            NULL,
                                            style_property_free);

  tidy_style_load (style);
}

/* need to unref */
TidyStyle *
tidy_style_new (void)
{
  return g_object_new (TIDY_TYPE_STYLE, NULL);
}

/* never ref/unref */
TidyStyle *
tidy_style_get_default (void)
{
  if (G_LIKELY (default_style))
    return default_style;

  default_style = g_object_new (TIDY_TYPE_STYLE, NULL);
  
  return default_style;
}

gboolean
tidy_style_has_property (TidyStyle   *style,
                         const gchar *property_name)
{
  g_return_val_if_fail (TIDY_IS_STYLE (style), FALSE);
  g_return_val_if_fail (property_name != NULL, FALSE);

  return (g_hash_table_lookup (style->priv->properties, property_name) != NULL);
}

void
tidy_style_add_property (TidyStyle   *style,
                         const gchar *property_name,
                         GType        property_type)
{
  StyleProperty *property;

  g_return_if_fail (TIDY_IS_STYLE (style));
  g_return_if_fail (property_name != NULL);
  g_return_if_fail (property_type != G_TYPE_INVALID);

  property = g_hash_table_lookup (style->priv->properties, property_name);
  if (property)
    {
      g_warning ("A property named `%s', with type %s already exists.",
                 property->value_name,
                 g_type_name (property->value_type));
      return;
    }

  property = style_property_new (property_name, property_type);
  g_hash_table_insert (style->priv->properties, property->value_name, property);

  g_signal_emit (style, style_signals[CHANGED], 0);
}

void
tidy_style_get_property (TidyStyle   *style,
                         const gchar *property_name,
                         GValue      *value)
{
  StyleProperty *property;

  g_return_if_fail (TIDY_IS_STYLE (style));
  g_return_if_fail (property_name != NULL);
  g_return_if_fail (value != NULL);

  property = g_hash_table_lookup (style->priv->properties, property_name);
  if (!property)
    {
      g_warning ("No style property named `%s' found.", property_name);
      return;
    }

  g_value_init (value, property->value_type);
  g_value_copy (&property->value, value);
}

void
tidy_style_set_property (TidyStyle    *style,
                         const gchar  *property_name,
                         const GValue *value)
{
  StyleProperty *property;

  g_return_if_fail (TIDY_IS_STYLE (style));
  g_return_if_fail (property_name != NULL);
  g_return_if_fail (value != NULL);

  property = g_hash_table_lookup (style->priv->properties, property_name);
  if (!property)
    {
      g_warning ("No style property named `%s' found.", property_name);
      return;
    }

  g_value_copy (value, &property->value);

  g_signal_emit (style, style_signals[CHANGED], 0);
}