~kroq-gar78/ubuntu/precise/gnome-control-center/fix-885947

« back to all changes in this revision

Viewing changes to capplets/appearance/gnome-wp-xml.c

  • Committer: Bazaar Package Importer
  • Author(s): Rodrigo Moya
  • Date: 2011-05-17 10:47:27 UTC
  • mfrom: (0.1.11 experimental) (1.1.45 upstream)
  • Revision ID: james.westby@ubuntu.com-20110517104727-lqel6m8vhfw5jby1
Tags: 1:3.0.1.1-1ubuntu1
* Rebase on Debian, remaining Ubuntu changes:
* debian/control:
  - Build-Depend on hardening-wrapper, dpkg-dev and dh-autoreconf
  - Add dependency on ubuntu-system-service
  - Remove dependency on gnome-icon-theme-symbolic
  - Move dependency on apg, gnome-icon-theme-symbolic and accountsservice to
    be a Recommends: until we get them in main
* debian/rules:
  - Use autoreconf
  - Add binary-post-install rule for gnome-control-center-data
  - Run dh-autoreconf
* debian/gnome-control-center.dirs:
* debian/gnome-control-center.links:
  - Add a link to the control center shell for indicators
* debian/patches/00_disable-nm.patch:
  - Temporary patch to disable building with NetworkManager until we get
    the new one in the archive
* debian/patches/01_git_remove_gettext_calls.patch:
  - Remove calls to AM_GNU_GETTEXT, IT_PROG_INTLTOOL should be enough
* debian/patches/01_git_kill_warning.patch:
  - Kill warning
* debian/patches/50_ubuntu_systemwide_prefs.patch:
  - Ubuntu specific proxy preferences
* debian/patches/51_ubuntu_system_keyboard.patch:
  - Implement the global keyboard spec at https://wiki.ubuntu.com/DefaultKeyboardSettings

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  Authors: Rodney Dawes <dobey@ximian.com>
3
 
 *
4
 
 *  Copyright 2003-2006 Novell, Inc. (www.novell.com)
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of version 2 of the GNU General Public License
8
 
 *  as published by the Free Software Foundation
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
18
 
 *
19
 
 */
20
 
 
21
 
#include "appearance.h"
22
 
#include "gnome-wp-item.h"
23
 
#include <gio/gio.h>
24
 
#include <string.h>
25
 
#include <libxml/parser.h>
26
 
 
27
 
static gboolean gnome_wp_xml_get_bool (const xmlNode * parent,
28
 
                                       const gchar * prop_name) {
29
 
  xmlChar * prop;
30
 
  gboolean ret_val = FALSE;
31
 
 
32
 
  g_return_val_if_fail (parent != NULL, FALSE);
33
 
  g_return_val_if_fail (prop_name != NULL, FALSE);
34
 
 
35
 
  prop = xmlGetProp ((xmlNode *) parent, (xmlChar*)prop_name);
36
 
  if (prop != NULL) {
37
 
    if (!g_ascii_strcasecmp ((gchar *)prop, "true") || !g_ascii_strcasecmp ((gchar *)prop, "1")) {
38
 
      ret_val = TRUE;
39
 
    } else {
40
 
      ret_val = FALSE;
41
 
    }
42
 
    g_free (prop);
43
 
  }
44
 
 
45
 
  return ret_val;
46
 
}
47
 
 
48
 
static void gnome_wp_xml_set_bool (const xmlNode * parent,
49
 
                                   const xmlChar * prop_name, gboolean value) {
50
 
  g_return_if_fail (parent != NULL);
51
 
  g_return_if_fail (prop_name != NULL);
52
 
 
53
 
  if (value) {
54
 
    xmlSetProp ((xmlNode *) parent, prop_name, (xmlChar *)"true");
55
 
  } else {
56
 
    xmlSetProp ((xmlNode *) parent, prop_name, (xmlChar *)"false");
57
 
  }
58
 
}
59
 
 
60
 
static void gnome_wp_load_legacy (AppearanceData *data) {
61
 
  FILE * fp;
62
 
  gchar * foo, * filename;
63
 
 
64
 
  filename = g_build_filename (g_get_home_dir (), ".gnome2",
65
 
                               "wallpapers.list", NULL);
66
 
 
67
 
  if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
68
 
    if ((fp = fopen (filename, "r")) != NULL) {
69
 
      foo = (gchar *) g_malloc (sizeof (gchar) * 4096);
70
 
      while (fgets (foo, 4096, fp)) {
71
 
        GnomeWPItem * item;
72
 
 
73
 
        if (foo[strlen (foo) - 1] == '\n') {
74
 
          foo[strlen (foo) - 1] = '\0';
75
 
        }
76
 
 
77
 
        item = g_hash_table_lookup (data->wp_hash, foo);
78
 
        if (item != NULL) {
79
 
          continue;
80
 
        }
81
 
 
82
 
        if (!g_file_test (foo, G_FILE_TEST_EXISTS)) {
83
 
          continue;
84
 
        }
85
 
 
86
 
        item = gnome_wp_item_new (foo, data->wp_hash, data->thumb_factory);
87
 
        if (item != NULL && item->fileinfo == NULL) {
88
 
          gnome_wp_item_free (item);
89
 
        }
90
 
      }
91
 
      fclose (fp);
92
 
      g_free (foo);
93
 
    }
94
 
  }
95
 
 
96
 
  g_free (filename);
97
 
}
98
 
 
99
 
static void gnome_wp_xml_load_xml (AppearanceData *data,
100
 
                                   const gchar * filename) {
101
 
  xmlDoc * wplist;
102
 
  xmlNode * root, * list, * wpa;
103
 
  xmlChar * nodelang;
104
 
  const gchar * const * syslangs;
105
 
  GdkColor color1, color2;
106
 
  gint i;
107
 
 
108
 
  wplist = xmlParseFile (filename);
109
 
 
110
 
  if (!wplist)
111
 
    return;
112
 
 
113
 
  syslangs = g_get_language_names ();
114
 
 
115
 
  root = xmlDocGetRootElement (wplist);
116
 
 
117
 
  for (list = root->children; list != NULL; list = list->next) {
118
 
    if (!strcmp ((gchar *)list->name, "wallpaper")) {
119
 
      GnomeWPItem * wp;
120
 
      gchar *pcolor = NULL, *scolor = NULL;
121
 
      gchar *s;
122
 
      gboolean have_scale = FALSE, have_shade = FALSE;
123
 
 
124
 
      wp = g_new0 (GnomeWPItem, 1);
125
 
 
126
 
      wp->deleted = gnome_wp_xml_get_bool (list, "deleted");
127
 
 
128
 
      for (wpa = list->children; wpa != NULL; wpa = wpa->next) {
129
 
        if (wpa->type == XML_COMMENT_NODE) {
130
 
          continue;
131
 
        } else if (!strcmp ((gchar *)wpa->name, "filename")) {
132
 
          if (wpa->last != NULL && wpa->last->content != NULL) {
133
 
            const char * none = "(none)";
134
 
            gchar *content = g_strstrip ((gchar *)wpa->last->content);
135
 
 
136
 
            if (!strcmp (content, none))
137
 
              wp->filename = g_strdup (content);
138
 
            else if (g_utf8_validate (content, -1, NULL) &&
139
 
                     g_file_test (content, G_FILE_TEST_EXISTS))
140
 
              wp->filename = g_strdup (content);
141
 
            else
142
 
              wp->filename = g_filename_from_utf8 (content, -1, NULL, NULL, NULL);
143
 
          } else {
144
 
            break;
145
 
          }
146
 
        } else if (!strcmp ((gchar *)wpa->name, "name")) {
147
 
          if (wpa->last != NULL && wpa->last->content != NULL) {
148
 
            nodelang = xmlNodeGetLang (wpa->last);
149
 
 
150
 
            if (wp->name == NULL && nodelang == NULL) {
151
 
               wp->name = g_strdup (g_strstrip ((gchar *)wpa->last->content));
152
 
            } else {
153
 
               for (i = 0; syslangs[i] != NULL; i++) {
154
 
                 if (!strcmp (syslangs[i], (gchar *)nodelang)) {
155
 
                   g_free (wp->name);
156
 
                   wp->name = g_strdup (g_strstrip ((gchar *)wpa->last->content));
157
 
                   break;
158
 
                 }
159
 
               }
160
 
            }
161
 
 
162
 
            xmlFree (nodelang);
163
 
          } else {
164
 
            break;
165
 
          }
166
 
        } else if (!strcmp ((gchar *)wpa->name, "options")) {
167
 
          if (wpa->last != NULL) {
168
 
            wp->options = wp_item_string_to_option (g_strstrip ((gchar *)wpa->last->content));
169
 
            have_scale = TRUE;
170
 
          }
171
 
        } else if (!strcmp ((gchar *)wpa->name, "shade_type")) {
172
 
          if (wpa->last != NULL) {
173
 
            wp->shade_type = wp_item_string_to_shading (g_strstrip ((gchar *)wpa->last->content));
174
 
            have_shade = TRUE;
175
 
          }
176
 
        } else if (!strcmp ((gchar *)wpa->name, "pcolor")) {
177
 
          if (wpa->last != NULL) {
178
 
            pcolor = g_strdup (g_strstrip ((gchar *)wpa->last->content));
179
 
          }
180
 
        } else if (!strcmp ((gchar *)wpa->name, "scolor")) {
181
 
          if (wpa->last != NULL) {
182
 
            scolor = g_strdup (g_strstrip ((gchar *)wpa->last->content));
183
 
          }
184
 
        } else if (!strcmp ((gchar *)wpa->name, "text")) {
185
 
          /* Do nothing here, libxml2 is being weird */
186
 
        } else {
187
 
          g_warning ("Unknown Tag: %s", wpa->name);
188
 
        }
189
 
      }
190
 
 
191
 
      /* Make sure we don't already have this one and that filename exists */
192
 
      if (wp->filename == NULL ||
193
 
          g_hash_table_lookup (data->wp_hash, wp->filename) != NULL) {
194
 
 
195
 
        gnome_wp_item_free (wp);
196
 
        g_free (pcolor);
197
 
        g_free (scolor);
198
 
        continue;
199
 
      }
200
 
 
201
 
      /* Verify the colors and alloc some GdkColors here */
202
 
      if (!have_scale) {
203
 
        s = gconf_client_get_string (data->client, WP_OPTIONS_KEY, NULL);
204
 
        wp->options = wp_item_string_to_option (s);
205
 
        g_free (s);
206
 
      }
207
 
 
208
 
      if (!have_shade) {
209
 
        s = gconf_client_get_string (data->client, WP_SHADING_KEY, NULL);
210
 
        wp->shade_type = wp_item_string_to_shading (s);
211
 
        g_free (s);
212
 
      }
213
 
 
214
 
      if (pcolor == NULL) {
215
 
        pcolor = gconf_client_get_string (data->client,
216
 
                                          WP_PCOLOR_KEY, NULL);
217
 
      }
218
 
      if (scolor == NULL) {
219
 
        scolor = gconf_client_get_string (data->client,
220
 
                                          WP_SCOLOR_KEY, NULL);
221
 
      }
222
 
      gdk_color_parse (pcolor, &color1);
223
 
      gdk_color_parse (scolor, &color2);
224
 
      g_free (pcolor);
225
 
      g_free (scolor);
226
 
 
227
 
      wp->pcolor = gdk_color_copy (&color1);
228
 
      wp->scolor = gdk_color_copy (&color2);
229
 
 
230
 
      if ((wp->filename != NULL &&
231
 
           g_file_test (wp->filename, G_FILE_TEST_EXISTS)) ||
232
 
          !strcmp (wp->filename, "(none)")) {
233
 
        wp->fileinfo = gnome_wp_info_new (wp->filename, data->thumb_factory);
234
 
 
235
 
        if (wp->name == NULL || !strcmp (wp->filename, "(none)")) {
236
 
          g_free (wp->name);
237
 
          wp->name = g_strdup (wp->fileinfo->name);
238
 
        }
239
 
 
240
 
        gnome_wp_item_ensure_gnome_bg (wp);
241
 
        gnome_wp_item_update_description (wp);
242
 
        g_hash_table_insert (data->wp_hash, wp->filename, wp);
243
 
      } else {
244
 
        gnome_wp_item_free (wp);
245
 
        wp = NULL;
246
 
      }
247
 
    }
248
 
  }
249
 
  xmlFreeDoc (wplist);
250
 
}
251
 
 
252
 
static void gnome_wp_file_changed (GFileMonitor *monitor,
253
 
                                   GFile *file,
254
 
                                   GFile *other_file,
255
 
                                   GFileMonitorEvent event_type,
256
 
                                   AppearanceData *data) {
257
 
  gchar * filename;
258
 
 
259
 
  switch (event_type) {
260
 
  case G_FILE_MONITOR_EVENT_CHANGED:
261
 
  case G_FILE_MONITOR_EVENT_CREATED:
262
 
    filename = g_file_get_path (file);
263
 
    gnome_wp_xml_load_xml (data, filename);
264
 
    g_free (filename);
265
 
    break;
266
 
  default:
267
 
    break;
268
 
  }
269
 
}
270
 
 
271
 
static void gnome_wp_xml_add_monitor (GFile *directory,
272
 
                                      AppearanceData *data) {
273
 
  GFileMonitor *monitor;
274
 
  GError *error = NULL;
275
 
 
276
 
  monitor = g_file_monitor_directory (directory,
277
 
                                      G_FILE_MONITOR_NONE,
278
 
                                      NULL,
279
 
                                      &error);
280
 
  if (error != NULL) {
281
 
    gchar *path;
282
 
 
283
 
    path = g_file_get_parse_name (directory);
284
 
    g_warning ("Unable to monitor directory %s: %s",
285
 
               path, error->message);
286
 
    g_error_free (error);
287
 
    g_free (path);
288
 
    return;
289
 
  }
290
 
 
291
 
  g_signal_connect (monitor, "changed",
292
 
                    G_CALLBACK (gnome_wp_file_changed),
293
 
                    data);
294
 
}
295
 
 
296
 
static void gnome_wp_xml_load_from_dir (const gchar *path,
297
 
                                        AppearanceData *data) {
298
 
  GFile *directory;
299
 
  GFileEnumerator *enumerator;
300
 
  GError *error = NULL;
301
 
  GFileInfo *info;
302
 
 
303
 
  if (!g_file_test (path, G_FILE_TEST_IS_DIR)) {
304
 
    return;
305
 
  }
306
 
 
307
 
  directory = g_file_new_for_path (path);
308
 
  enumerator = g_file_enumerate_children (directory,
309
 
                                          G_FILE_ATTRIBUTE_STANDARD_NAME,
310
 
                                          G_FILE_QUERY_INFO_NONE,
311
 
                                          NULL,
312
 
                                          &error);
313
 
  if (error != NULL) {
314
 
    g_warning ("Unable to check directory %s: %s", path, error->message);
315
 
    g_error_free (error);
316
 
    g_object_unref (directory);
317
 
    return;
318
 
  }
319
 
 
320
 
  while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL))) {
321
 
    const gchar *filename;
322
 
    gchar *fullpath;
323
 
 
324
 
    filename = g_file_info_get_name (info);
325
 
    fullpath = g_build_filename (path, filename, NULL);
326
 
    g_object_unref (info);
327
 
 
328
 
    gnome_wp_xml_load_xml (data, fullpath);
329
 
    g_free (fullpath);
330
 
  }
331
 
  g_file_enumerator_close (enumerator, NULL, NULL);
332
 
 
333
 
  gnome_wp_xml_add_monitor (directory, data);
334
 
 
335
 
  g_object_unref (directory);
336
 
}
337
 
 
338
 
void gnome_wp_xml_load_list (AppearanceData *data) {
339
 
  const char * const *system_data_dirs;
340
 
  gchar * datadir;
341
 
  gchar * wpdbfile;
342
 
  gint i;
343
 
 
344
 
  wpdbfile = g_build_filename (g_get_home_dir (),
345
 
                               ".gnome2",
346
 
                               "backgrounds.xml",
347
 
                               NULL);
348
 
 
349
 
  if (g_file_test (wpdbfile, G_FILE_TEST_EXISTS)) {
350
 
    gnome_wp_xml_load_xml (data, wpdbfile);
351
 
  } else {
352
 
    g_free (wpdbfile);
353
 
    wpdbfile = g_build_filename (g_get_home_dir (),
354
 
                                 ".gnome2",
355
 
                                 "wp-list.xml",
356
 
                                 NULL);
357
 
    if (g_file_test (wpdbfile, G_FILE_TEST_EXISTS)) {
358
 
      gnome_wp_xml_load_xml (data, wpdbfile);
359
 
    }
360
 
  }
361
 
  g_free (wpdbfile);
362
 
 
363
 
  datadir = g_build_filename (g_get_user_data_dir (),
364
 
                              "gnome-background-properties",
365
 
                              NULL);
366
 
  gnome_wp_xml_load_from_dir (datadir, data);
367
 
  g_free (datadir);
368
 
 
369
 
  system_data_dirs = g_get_system_data_dirs ();
370
 
  for (i = 0; system_data_dirs[i]; i++) {
371
 
    datadir = g_build_filename (system_data_dirs[i],
372
 
                                "gnome-background-properties",
373
 
                                NULL);
374
 
    gnome_wp_xml_load_from_dir (datadir, data);
375
 
    g_free (datadir);
376
 
  }
377
 
 
378
 
  gnome_wp_xml_load_from_dir (WALLPAPER_DATADIR, data);
379
 
 
380
 
  gnome_wp_load_legacy (data);
381
 
}
382
 
 
383
 
static void gnome_wp_list_flatten (const gchar * key, GnomeWPItem * item,
384
 
                                   GSList ** list) {
385
 
  g_return_if_fail (key != NULL);
386
 
  g_return_if_fail (item != NULL);
387
 
 
388
 
  *list = g_slist_prepend (*list, item);
389
 
}
390
 
 
391
 
void gnome_wp_xml_save_list (AppearanceData *data) {
392
 
  xmlDoc * wplist;
393
 
  xmlNode * root, * wallpaper, * item;
394
 
  GSList * list = NULL;
395
 
  gchar * wpfile;
396
 
 
397
 
  g_hash_table_foreach (data->wp_hash,
398
 
                        (GHFunc) gnome_wp_list_flatten, &list);
399
 
  g_hash_table_destroy (data->wp_hash);
400
 
  list = g_slist_reverse (list);
401
 
 
402
 
  wpfile = g_build_filename (g_get_home_dir (),
403
 
                             "/.gnome2",
404
 
                             "backgrounds.xml",
405
 
                             NULL);
406
 
 
407
 
  xmlKeepBlanksDefault (0);
408
 
 
409
 
  wplist = xmlNewDoc ((xmlChar *)"1.0");
410
 
  xmlCreateIntSubset (wplist, (xmlChar *)"wallpapers", NULL, (xmlChar *)"gnome-wp-list.dtd");
411
 
  root = xmlNewNode (NULL, (xmlChar *)"wallpapers");
412
 
  xmlDocSetRootElement (wplist, root);
413
 
 
414
 
  while (list != NULL) {
415
 
    GnomeWPItem * wpitem = list->data;
416
 
    const char * none = "(none)";
417
 
    gchar * filename;
418
 
    const gchar * scale, * shade;
419
 
    gchar * pcolor, * scolor;
420
 
 
421
 
    if (!strcmp (wpitem->filename, none) ||
422
 
        (g_utf8_validate (wpitem->filename, -1, NULL) &&
423
 
         g_file_test (wpitem->filename, G_FILE_TEST_EXISTS)))
424
 
      filename = g_strdup (wpitem->filename);
425
 
    else
426
 
      filename = g_filename_to_utf8 (wpitem->filename, -1, NULL, NULL, NULL);
427
 
 
428
 
    pcolor = gdk_color_to_string (wpitem->pcolor);
429
 
    scolor = gdk_color_to_string (wpitem->scolor);
430
 
    scale = wp_item_option_to_string (wpitem->options);
431
 
    shade = wp_item_shading_to_string (wpitem->shade_type);
432
 
 
433
 
    wallpaper = xmlNewChild (root, NULL, (xmlChar *)"wallpaper", NULL);
434
 
    gnome_wp_xml_set_bool (wallpaper, (xmlChar *)"deleted", wpitem->deleted);
435
 
    item = xmlNewTextChild (wallpaper, NULL, (xmlChar *)"name", (xmlChar *)wpitem->name);
436
 
    item = xmlNewTextChild (wallpaper, NULL, (xmlChar *)"filename", (xmlChar *)filename);
437
 
    item = xmlNewTextChild (wallpaper, NULL, (xmlChar *)"options", (xmlChar *)scale);
438
 
    item = xmlNewTextChild (wallpaper, NULL, (xmlChar *)"shade_type", (xmlChar *)shade);
439
 
    item = xmlNewTextChild (wallpaper, NULL, (xmlChar *)"pcolor", (xmlChar *)pcolor);
440
 
    item = xmlNewTextChild (wallpaper, NULL, (xmlChar *)"scolor", (xmlChar *)scolor);
441
 
    g_free (pcolor);
442
 
    g_free (scolor);
443
 
    g_free (filename);
444
 
 
445
 
    list = g_slist_delete_link (list, list);
446
 
    gnome_wp_item_free (wpitem);
447
 
  }
448
 
  xmlSaveFormatFile (wpfile, wplist, 1);
449
 
  xmlFreeDoc (wplist);
450
 
  g_free (wpfile);
451
 
}