~awn-core/awn/core-icons

« back to all changes in this revision

Viewing changes to libawn/awn-config.c

Merge from <lp:~malept/awn/0.4-libdesktop-agnostic>, revision 847.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2009 Mark Lee <avant-wn@lazymalevolence.com>
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public
 
15
 * License along with this library; if not, write to the
 
16
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
17
 * Boston, MA 02111-1307, USA.
 
18
 *
 
19
 * Author : Mark Lee <avant-wn@lazymalevolence.com>
 
20
 */
 
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
#include "config.h"
 
24
#endif
 
25
 
 
26
#include "awn-config.h"
 
27
 
 
28
/**
 
29
 * SECTION: awn-config
 
30
 * @short_description: Convenience functions for handling dock/applet
 
31
 * configuration.
 
32
 * @see_also: #DesktopAgnosticConfigClient
 
33
 * @since: 0.4.0
 
34
 *
 
35
 * Functions used by the dock, applets, and preferences dialogs to
 
36
 * associate configuration options with the first two categories. Also
 
37
 * handles their memory management.
 
38
 */
 
39
 
 
40
#define SCHEMADIR PKGDATADIR "/schemas"
 
41
#define UID_SINGLE_INSTANCE_PREFIX "single-"
 
42
 
 
43
/* The config client cache. */
 
44
static GData* awn_config_clients = NULL;
 
45
 
 
46
 
 
47
static void
 
48
on_config_destroy (gpointer data)
 
49
{
 
50
  DesktopAgnosticConfigClient* cfg = (DesktopAgnosticConfigClient*)data;
 
51
  g_object_unref (cfg);
 
52
}
 
53
 
 
54
 
 
55
/**
 
56
 * awn_config_get_default:
 
57
 * @panel_id: The ID of the panel that is associated with the configuration
 
58
 * client.
 
59
 * @error: The address of the #GError object, if an error occurs.
 
60
 *
 
61
 * Looks up or creates a configuration client that is associated with the
 
62
 * panel specified.
 
63
 *
 
64
 * Returns: A borrowed reference to a configuration client object.
 
65
 */
 
66
DesktopAgnosticConfigClient*
 
67
awn_config_get_default (gint panel_id, GError** error)
 
68
{
 
69
  char *instance_id = NULL;
 
70
  DesktopAgnosticConfigClient *client = NULL;
 
71
 
 
72
  if (awn_config_clients == NULL)
 
73
  {
 
74
    /* initialize datalist */
 
75
    g_datalist_init (&awn_config_clients);
 
76
  }
 
77
  instance_id = g_strdup_printf ("panel-%d", panel_id);
 
78
  client = (DesktopAgnosticConfigClient*)g_datalist_get_data (&awn_config_clients,
 
79
                                                              instance_id);
 
80
  if (client == NULL)
 
81
  {
 
82
    char *schema_filename;
 
83
 
 
84
    schema_filename = g_build_filename (SCHEMADIR, "avant-window-navigator.schema-ini", NULL);
 
85
    client = desktop_agnostic_config_client_new_for_instance (schema_filename,
 
86
                                                              instance_id,
 
87
                                                              error);
 
88
    g_free (schema_filename);
 
89
    if (error && *error != NULL)
 
90
    {
 
91
      if (client != NULL)
 
92
      {
 
93
        g_object_unref (client);
 
94
      }
 
95
      g_free (instance_id);
 
96
      return NULL;
 
97
    }
 
98
    g_datalist_set_data_full (&awn_config_clients, instance_id, client,
 
99
                              on_config_destroy);
 
100
  }
 
101
  g_free (instance_id);
 
102
  return client;
 
103
}
 
104
 
 
105
 
 
106
/**
 
107
 * awn_config_free:
 
108
 *
 
109
 * Properly frees all of the config clients in the cache.
 
110
 *
 
111
 * Should be called on dock shutdown.
 
112
 */
 
113
void
 
114
awn_config_free (void)
 
115
{
 
116
  g_datalist_clear (&awn_config_clients);
 
117
}
 
118
 
 
119
 
 
120
/**
 
121
 * awn_config_get_default_for_applet:
 
122
 * @applet: The applet.
 
123
 * @error: The address of the #GError object, if an error occurs.
 
124
 *
 
125
 * Looks up or creates a configuration client that is associated with the
 
126
 * given applet.
 
127
 *
 
128
 * Returns: A borrowed reference to the configuration client associated with
 
129
 * the applet specified via the metadata.
 
130
 */
 
131
DesktopAgnosticConfigClient*
 
132
awn_config_get_default_for_applet (AwnApplet *applet, GError **error)
 
133
{
 
134
  g_return_val_if_fail (applet != NULL, NULL);
 
135
 
 
136
  gchar *canonical_name = NULL;
 
137
  gchar *uid = NULL;
 
138
  DesktopAgnosticConfigClient *client = NULL;
 
139
 
 
140
  g_object_get (G_OBJECT (applet),
 
141
                "canonical-name", &canonical_name,
 
142
                "uid", &uid,
 
143
                NULL);
 
144
 
 
145
  g_return_val_if_fail (canonical_name != NULL, NULL);
 
146
 
 
147
  client = awn_config_get_default_for_applet_by_info (canonical_name,
 
148
                                                      uid,
 
149
                                                      error);
 
150
 
 
151
  if (uid != NULL)
 
152
  {
 
153
    g_free (uid);
 
154
  }
 
155
 
 
156
  g_free (canonical_name);
 
157
 
 
158
  return client;
 
159
}
 
160
 
 
161
 
 
162
/**
 
163
 * awn_config_get_default_for_applet_by_info:
 
164
 * @name: The canonical applet name.
 
165
 * @uid: The UID of the applet (may be %NULL).
 
166
 * @error: The address of the #GError object, if an error occurs.
 
167
 *
 
168
 * Looks up or creates a configuration client that is associated with the
 
169
 * canonical name of an applet and an optional UID.
 
170
 * Should only be used by code where the #AwnApplet object is not present,
 
171
 * such as the dock's preferences dialog.
 
172
 *
 
173
 * Returns: A borrowed reference to the configuration client associated with
 
174
 * the applet specified via the metadata.
 
175
 */
 
176
DesktopAgnosticConfigClient*
 
177
awn_config_get_default_for_applet_by_info (const gchar  *name,
 
178
                                           const gchar  *uid,
 
179
                                           GError      **error)
 
180
{
 
181
  g_return_val_if_fail (name != NULL, NULL);
 
182
 
 
183
  gboolean single_instance;
 
184
  gchar *instance_id;
 
185
  DesktopAgnosticConfigClient *client = NULL;
 
186
 
 
187
  if (awn_config_clients == NULL)
 
188
  {
 
189
    /* initialize datalist */
 
190
    g_datalist_init (&awn_config_clients);
 
191
  }
 
192
 
 
193
  single_instance = (uid == NULL ||
 
194
                     g_str_has_prefix (uid, UID_SINGLE_INSTANCE_PREFIX));
 
195
 
 
196
  if (single_instance)
 
197
  {
 
198
    instance_id = g_strdup_printf ("awn-applet-%s", name);
 
199
  }
 
200
  else
 
201
  {
 
202
    instance_id = g_strdup_printf ("awn-applet-%s-%s", name, uid);
 
203
  }
 
204
 
 
205
  client = (DesktopAgnosticConfigClient*)g_datalist_get_data (&awn_config_clients,
 
206
                                                              instance_id);
 
207
 
 
208
  if (client == NULL)
 
209
  {
 
210
    gchar *schema_basename;
 
211
    gchar *schema_filename;
 
212
 
 
213
    schema_basename = g_strdup_printf ("awn-applet-%s.schema-ini", name);
 
214
 
 
215
    schema_filename = g_build_filename (SCHEMADIR, schema_basename, NULL);
 
216
 
 
217
    g_free (schema_basename);
 
218
 
 
219
    if (single_instance)
 
220
    {
 
221
      client = desktop_agnostic_config_client_new (schema_filename);
 
222
    }
 
223
    else
 
224
    {
 
225
      client = desktop_agnostic_config_client_new_for_instance (schema_filename,
 
226
                                                                instance_id,
 
227
                                                                error);
 
228
    }
 
229
 
 
230
    g_free (schema_filename);
 
231
    if (error && *error != NULL)
 
232
    {
 
233
      if (client != NULL)
 
234
      {
 
235
        g_object_unref (client);
 
236
      }
 
237
      g_free (instance_id);
 
238
      return NULL;
 
239
    }
 
240
    g_datalist_set_data_full (&awn_config_clients, instance_id, client,
 
241
                              on_config_destroy);
 
242
  }
 
243
  g_free (instance_id);
 
244
  return client;
 
245
}