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

« back to all changes in this revision

Viewing changes to panels/background/bg-pictures-source.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
/* bg-pictures-source.c */
 
2
/*
 
3
 * Copyright (C) 2010 Intel, Inc
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
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 Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 *
 
19
 * Author: Thomas Wood <thomas.wood@intel.com>
 
20
 *
 
21
 */
 
22
 
 
23
#include "bg-pictures-source.h"
 
24
 
 
25
#include "cc-background-item.h"
 
26
 
 
27
#include <string.h>
 
28
#include <gio/gio.h>
 
29
#include <libgnome-desktop/gnome-desktop-thumbnail.h>
 
30
#include <gdesktop-enums.h>
 
31
 
 
32
G_DEFINE_TYPE (BgPicturesSource, bg_pictures_source, BG_TYPE_SOURCE)
 
33
 
 
34
#define PICTURES_SOURCE_PRIVATE(o) \
 
35
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), BG_TYPE_PICTURES_SOURCE, BgPicturesSourcePrivate))
 
36
 
 
37
#define ATTRIBUTES G_FILE_ATTRIBUTE_STANDARD_NAME "," \
 
38
        G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
 
39
 
 
40
struct _BgPicturesSourcePrivate
 
41
{
 
42
  GCancellable *cancellable;
 
43
 
 
44
  GnomeDesktopThumbnailFactory *thumb_factory;
 
45
 
 
46
  GHashTable *known_items;
 
47
};
 
48
 
 
49
static char *bg_pictures_source_get_unique_filename (const char *uri);
 
50
 
 
51
static void
 
52
bg_pictures_source_get_property (GObject    *object,
 
53
                                  guint       property_id,
 
54
                                  GValue     *value,
 
55
                                  GParamSpec *pspec)
 
56
{
 
57
  switch (property_id)
 
58
    {
 
59
    default:
 
60
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
61
    }
 
62
}
 
63
 
 
64
static void
 
65
bg_pictures_source_set_property (GObject      *object,
 
66
                                  guint         property_id,
 
67
                                  const GValue *value,
 
68
                                  GParamSpec   *pspec)
 
69
{
 
70
  switch (property_id)
 
71
    {
 
72
    default:
 
73
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
74
    }
 
75
}
 
76
 
 
77
static void
 
78
bg_pictures_source_dispose (GObject *object)
 
79
{
 
80
  BgPicturesSourcePrivate *priv = BG_PICTURES_SOURCE (object)->priv;
 
81
 
 
82
  if (priv->cancellable)
 
83
    {
 
84
      g_cancellable_cancel (priv->cancellable);
 
85
      g_object_unref (priv->cancellable);
 
86
      priv->cancellable = NULL;
 
87
    }
 
88
 
 
89
  if (priv->thumb_factory)
 
90
    {
 
91
      g_object_unref (priv->thumb_factory);
 
92
      priv->thumb_factory = NULL;
 
93
    }
 
94
 
 
95
  G_OBJECT_CLASS (bg_pictures_source_parent_class)->dispose (object);
 
96
}
 
97
 
 
98
static void
 
99
bg_pictures_source_finalize (GObject *object)
 
100
{
 
101
  BgPicturesSource *bg_source = BG_PICTURES_SOURCE (object);
 
102
 
 
103
  if (bg_source->priv->thumb_factory)
 
104
    {
 
105
      g_object_unref (bg_source->priv->thumb_factory);
 
106
      bg_source->priv->thumb_factory = NULL;
 
107
    }
 
108
 
 
109
  if (bg_source->priv->known_items)
 
110
    {
 
111
      g_hash_table_destroy (bg_source->priv->known_items);
 
112
      bg_source->priv->known_items = NULL;
 
113
    }
 
114
 
 
115
  G_OBJECT_CLASS (bg_pictures_source_parent_class)->finalize (object);
 
116
}
 
117
 
 
118
static void
 
119
bg_pictures_source_class_init (BgPicturesSourceClass *klass)
 
120
{
 
121
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
122
 
 
123
  g_type_class_add_private (klass, sizeof (BgPicturesSourcePrivate));
 
124
 
 
125
  object_class->get_property = bg_pictures_source_get_property;
 
126
  object_class->set_property = bg_pictures_source_set_property;
 
127
  object_class->dispose = bg_pictures_source_dispose;
 
128
  object_class->finalize = bg_pictures_source_finalize;
 
129
}
 
130
 
 
131
static void
 
132
picture_scaled (GObject *source_object,
 
133
                GAsyncResult *res,
 
134
                gpointer user_data)
 
135
{
 
136
  BgPicturesSource *bg_source = BG_PICTURES_SOURCE (user_data);
 
137
  CcBackgroundItem *item;
 
138
  GError *error = NULL;
 
139
  GdkPixbuf *pixbuf;
 
140
  const char *source_url;
 
141
 
 
142
  GtkTreeIter iter;
 
143
  GtkListStore *store;
 
144
 
 
145
  store = bg_source_get_liststore (BG_SOURCE (bg_source));
 
146
  item = g_object_get_data (source_object, "item");
 
147
 
 
148
  pixbuf = gdk_pixbuf_new_from_stream_finish (res, &error);
 
149
  if (pixbuf == NULL)
 
150
    {
 
151
      g_warning ("Failed to load image: %s", error->message);
 
152
      g_error_free (error);
 
153
      g_object_unref (item);
 
154
      return;
 
155
    }
 
156
 
 
157
  /* insert the item into the liststore */
 
158
  gtk_list_store_insert_with_values (store, &iter, 0,
 
159
                                     0, pixbuf,
 
160
                                     1, item,
 
161
                                     -1);
 
162
  source_url = cc_background_item_get_source_url (item);
 
163
  if (source_url != NULL)
 
164
    {
 
165
      g_hash_table_insert (bg_source->priv->known_items,
 
166
                           bg_pictures_source_get_unique_filename (source_url), GINT_TO_POINTER (TRUE));
 
167
    }
 
168
  else
 
169
    {
 
170
      char *cache_path;
 
171
      GFile *file, *parent, *dir;
 
172
 
 
173
      cache_path = bg_pictures_source_get_cache_path ();
 
174
      dir = g_file_new_for_path (cache_path);
 
175
      g_free (cache_path);
 
176
 
 
177
      file = g_file_new_for_uri (cc_background_item_get_uri (item));
 
178
      parent = g_file_get_parent (file);
 
179
 
 
180
      if (g_file_equal (parent, dir))
 
181
        {
 
182
          char *basename;
 
183
          basename = g_file_get_basename (file);
 
184
          g_hash_table_insert (bg_source->priv->known_items,
 
185
                               basename, GINT_TO_POINTER (TRUE));
 
186
        }
 
187
      g_object_unref (file);
 
188
      g_object_unref (parent);
 
189
    }
 
190
 
 
191
  g_object_unref (pixbuf);
 
192
}
 
193
 
 
194
static void
 
195
picture_opened_for_read (GObject *source_object,
 
196
                         GAsyncResult *res,
 
197
                         gpointer user_data)
 
198
{
 
199
  BgPicturesSource *bg_source = BG_PICTURES_SOURCE (user_data);
 
200
  CcBackgroundItem *item;
 
201
  GFileInputStream *stream;
 
202
  GError *error = NULL;
 
203
 
 
204
  item = g_object_get_data (source_object, "item");
 
205
  stream = g_file_read_finish (G_FILE (source_object), res, &error);
 
206
  if (stream == NULL)
 
207
    {
 
208
      char *filename;
 
209
 
 
210
      filename = g_file_get_path (G_FILE (source_object));
 
211
      g_warning ("Failed to load picture '%s': %s", filename, error->message);
 
212
      g_free (filename);
 
213
      g_error_free (error);
 
214
      g_object_unref (item);
 
215
      return;
 
216
    }
 
217
 
 
218
  g_object_set_data (G_OBJECT (stream), "item", item);
 
219
 
 
220
  gdk_pixbuf_new_from_stream_at_scale_async (G_INPUT_STREAM (stream),
 
221
                                             THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT,
 
222
                                             TRUE,
 
223
                                             NULL,
 
224
                                             picture_scaled, bg_source);
 
225
  g_object_unref (stream);
 
226
}
 
227
 
 
228
static gboolean
 
229
add_single_file (BgPicturesSource *bg_source,
 
230
                 GFile            *file,
 
231
                 GFileInfo        *info,
 
232
                 const char       *source_uri)
 
233
{
 
234
  const gchar *content_type;
 
235
 
 
236
  /* find png and jpeg files */
 
237
  content_type = g_file_info_get_content_type (info);
 
238
 
 
239
  if (!content_type)
 
240
    return FALSE;
 
241
 
 
242
  if (g_str_equal ("image/png", content_type) ||
 
243
      g_str_equal ("image/jpeg", content_type))
 
244
    {
 
245
      CcBackgroundItem *item;
 
246
      char *uri;
 
247
 
 
248
      /* create a new CcBackgroundItem */
 
249
      uri = g_file_get_uri (file);
 
250
      item = cc_background_item_new (uri);
 
251
      g_free (uri);
 
252
      g_object_set (G_OBJECT (item),
 
253
                    "flags", CC_BACKGROUND_ITEM_HAS_URI | CC_BACKGROUND_ITEM_HAS_SHADING,
 
254
                    "shading", G_DESKTOP_BACKGROUND_SHADING_SOLID,
 
255
                    "placement", G_DESKTOP_BACKGROUND_STYLE_ZOOM,
 
256
                    NULL);
 
257
      if (source_uri != NULL)
 
258
        g_object_set (G_OBJECT (item), "source-url", source_uri, NULL);
 
259
 
 
260
      g_object_set_data (G_OBJECT (file), "item", item);
 
261
      g_file_read_async (file, 0, NULL, picture_opened_for_read, bg_source);
 
262
      g_object_unref (file);
 
263
      return TRUE;
 
264
    }
 
265
 
 
266
  return FALSE;
 
267
}
 
268
 
 
269
gboolean
 
270
bg_pictures_source_add (BgPicturesSource *bg_source,
 
271
                        const char       *uri)
 
272
{
 
273
  GFile *file;
 
274
  GFileInfo *info;
 
275
  gboolean retval;
 
276
 
 
277
  file = g_file_new_for_uri (uri);
 
278
  info = g_file_query_info (file, ATTRIBUTES, G_FILE_QUERY_INFO_NONE, NULL, NULL);
 
279
  if (info == NULL)
 
280
    return FALSE;
 
281
 
 
282
  retval = add_single_file (bg_source, file, info, uri);
 
283
 
 
284
  return retval;
 
285
}
 
286
 
 
287
gboolean
 
288
bg_pictures_source_remove (BgPicturesSource *bg_source,
 
289
                           CcBackgroundItem *item)
 
290
{
 
291
  GtkTreeModel *model;
 
292
  GtkTreeIter iter;
 
293
  gboolean cont;
 
294
  const char *uri;
 
295
  gboolean retval;
 
296
 
 
297
  retval = FALSE;
 
298
  model = GTK_TREE_MODEL (bg_source_get_liststore (BG_SOURCE (bg_source)));
 
299
  uri = cc_background_item_get_uri (item);
 
300
 
 
301
  cont = gtk_tree_model_get_iter_first (model, &iter);
 
302
  while (cont)
 
303
    {
 
304
      CcBackgroundItem *tmp_item;
 
305
      const char *tmp_uri;
 
306
 
 
307
      gtk_tree_model_get (model, &iter, 1, &tmp_item, -1);
 
308
      tmp_uri = cc_background_item_get_uri (tmp_item);
 
309
      if (g_str_equal (tmp_uri, uri))
 
310
        {
 
311
          GFile *file;
 
312
          char *uuid;
 
313
 
 
314
          file = g_file_new_for_uri (uri);
 
315
          uuid = g_file_get_basename (file);
 
316
          g_hash_table_insert (bg_source->priv->known_items,
 
317
                               uuid, NULL);
 
318
 
 
319
          gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
 
320
          retval = TRUE;
 
321
          g_file_trash (file, NULL, NULL);
 
322
          g_object_unref (file);
 
323
          break;
 
324
        }
 
325
      g_object_unref (tmp_item);
 
326
      cont = gtk_tree_model_iter_next (model, &iter);
 
327
    }
 
328
  return retval;
 
329
}
 
330
 
 
331
static void
 
332
file_info_async_ready (GObject      *source,
 
333
                       GAsyncResult *res,
 
334
                       gpointer      user_data)
 
335
{
 
336
  BgPicturesSource *bg_source = BG_PICTURES_SOURCE (user_data);
 
337
  GList *files, *l;
 
338
  GError *err = NULL;
 
339
  GFile *parent;
 
340
  files = g_file_enumerator_next_files_finish (G_FILE_ENUMERATOR (source),
 
341
                                               res,
 
342
                                               &err);
 
343
 
 
344
  if (err)
 
345
    {
 
346
      g_warning ("Could not get pictures file information: %s", err->message);
 
347
      g_error_free (err);
 
348
 
 
349
      g_list_foreach (files, (GFunc) g_object_unref, NULL);
 
350
      g_list_free (files);
 
351
      return;
 
352
    }
 
353
 
 
354
  parent = g_file_enumerator_get_container (G_FILE_ENUMERATOR (source));
 
355
 
 
356
  /* iterate over the available files */
 
357
  for (l = files; l; l = g_list_next (l))
 
358
    {
 
359
      GFileInfo *info = l->data;
 
360
      GFile *file;
 
361
 
 
362
      file = g_file_get_child (parent, g_file_info_get_name (info));
 
363
 
 
364
      add_single_file (bg_source, file, info, NULL);
 
365
    }
 
366
 
 
367
  g_list_foreach (files, (GFunc) g_object_unref, NULL);
 
368
  g_list_free (files);
 
369
}
 
370
 
 
371
static void
 
372
dir_enum_async_ready (GObject      *source,
 
373
                      GAsyncResult *res,
 
374
                      gpointer      user_data)
 
375
{
 
376
  BgPicturesSourcePrivate *priv = BG_PICTURES_SOURCE (user_data)->priv;
 
377
  GFileEnumerator *enumerator;
 
378
  GError *err = NULL;
 
379
 
 
380
  enumerator = g_file_enumerate_children_finish (G_FILE (source), res, &err);
 
381
 
 
382
  if (err)
 
383
    {
 
384
      if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_NOT_FOUND) == FALSE)
 
385
        g_warning ("Could not fill pictures source: %s", err->message);
 
386
      g_error_free (err);
 
387
      return;
 
388
    }
 
389
 
 
390
  /* get the files */
 
391
  g_file_enumerator_next_files_async (enumerator,
 
392
                                      G_MAXINT,
 
393
                                      G_PRIORITY_LOW,
 
394
                                      priv->cancellable,
 
395
                                      file_info_async_ready,
 
396
                                      user_data);
 
397
}
 
398
 
 
399
char *
 
400
bg_pictures_source_get_cache_path (void)
 
401
{
 
402
  return g_build_filename (g_get_user_cache_dir (),
 
403
                           "gnome-control-center",
 
404
                           "backgrounds",
 
405
                           NULL);
 
406
}
 
407
 
 
408
static char *
 
409
bg_pictures_source_get_unique_filename (const char *uri)
 
410
{
 
411
  GChecksum *csum;
 
412
  char *ret;
 
413
 
 
414
  csum = g_checksum_new (G_CHECKSUM_SHA256);
 
415
  g_checksum_update (csum, (guchar *) uri, -1);
 
416
  ret = g_strdup (g_checksum_get_string (csum));
 
417
  g_checksum_free (csum);
 
418
 
 
419
  return ret;
 
420
}
 
421
 
 
422
char *
 
423
bg_pictures_source_get_unique_path (const char *uri)
 
424
{
 
425
  GFile *parent, *file;
 
426
  char *cache_path;
 
427
  char *filename;
 
428
  char *ret;
 
429
 
 
430
  cache_path = bg_pictures_source_get_cache_path ();
 
431
  parent = g_file_new_for_path (cache_path);
 
432
  g_free (cache_path);
 
433
 
 
434
  filename = bg_pictures_source_get_unique_filename (uri);
 
435
  file = g_file_get_child (parent, filename);
 
436
  g_free (filename);
 
437
  ret = g_file_get_path (file);
 
438
  g_object_unref (file);
 
439
 
 
440
  return ret;
 
441
}
 
442
 
 
443
gboolean
 
444
bg_pictures_source_is_known (BgPicturesSource *bg_source,
 
445
                             const char       *uri)
 
446
{
 
447
  gboolean retval;
 
448
  char *uuid;
 
449
 
 
450
  uuid = bg_pictures_source_get_unique_filename (uri);
 
451
  retval = (GPOINTER_TO_INT (g_hash_table_lookup (bg_source->priv->known_items, uuid)));
 
452
  g_free (uuid);
 
453
 
 
454
  return retval;
 
455
}
 
456
 
 
457
static void
 
458
bg_pictures_source_init (BgPicturesSource *self)
 
459
{
 
460
  const gchar *pictures_path;
 
461
  BgPicturesSourcePrivate *priv;
 
462
  GFile *dir;
 
463
  char *cache_path;
 
464
 
 
465
  priv = self->priv = PICTURES_SOURCE_PRIVATE (self);
 
466
 
 
467
  priv->cancellable = g_cancellable_new ();
 
468
  priv->known_items = g_hash_table_new_full (g_str_hash,
 
469
                                             g_str_equal,
 
470
                                             (GDestroyNotify) g_free,
 
471
                                             NULL);
 
472
 
 
473
  pictures_path = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
 
474
  dir = g_file_new_for_path (pictures_path);
 
475
  g_file_enumerate_children_async (dir,
 
476
                                   ATTRIBUTES,
 
477
                                   G_FILE_QUERY_INFO_NONE,
 
478
                                   G_PRIORITY_LOW, priv->cancellable,
 
479
                                   dir_enum_async_ready, self);
 
480
  g_object_unref (dir);
 
481
 
 
482
  cache_path = bg_pictures_source_get_cache_path ();
 
483
  dir = g_file_new_for_path (cache_path);
 
484
  g_file_enumerate_children_async (dir,
 
485
                                   ATTRIBUTES,
 
486
                                   G_FILE_QUERY_INFO_NONE,
 
487
                                   G_PRIORITY_LOW, priv->cancellable,
 
488
                                   dir_enum_async_ready, self);
 
489
  g_object_unref (dir);
 
490
 
 
491
  priv->thumb_factory =
 
492
    gnome_desktop_thumbnail_factory_new (GNOME_DESKTOP_THUMBNAIL_SIZE_NORMAL);
 
493
}
 
494
 
 
495
BgPicturesSource *
 
496
bg_pictures_source_new (void)
 
497
{
 
498
  return g_object_new (BG_TYPE_PICTURES_SOURCE, NULL);
 
499
}
 
500