~ubuntu-branches/ubuntu/trusty/unity-control-center/trusty

« back to all changes in this revision

Viewing changes to panels/appearance/cc-appearance-item.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2014-01-08 16:29:18 UTC
  • Revision ID: package-import@ubuntu.com-20140108162918-g29dd08tr913y2qh
Tags: upstream-14.04.0
ImportĀ upstreamĀ versionĀ 14.04.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2010-2011 Red Hat, 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, see <http://www.gnu.org/licenses/>.
 
17
 *
 
18
 */
 
19
 
 
20
#include "config.h"
 
21
 
 
22
#include <stdlib.h>
 
23
#include <stdio.h>
 
24
 
 
25
#include <gtk/gtk.h>
 
26
#include <gio/gio.h>
 
27
#include <glib/gi18n-lib.h>
 
28
 
 
29
#include <libgnome-desktop/gnome-bg.h>
 
30
#include <gdesktop-enums.h>
 
31
 
 
32
#include "cc-appearance-item.h"
 
33
#include "gdesktop-enums-types.h"
 
34
 
 
35
#define CC_APPEARANCE_ITEM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CC_TYPE_APPEARANCE_ITEM, CcAppearanceItemPrivate))
 
36
 
 
37
struct CcAppearanceItemPrivate
 
38
{
 
39
        /* properties */
 
40
        char            *name;
 
41
        char            *uri;
 
42
        char            *size;
 
43
        GDesktopBackgroundStyle placement;
 
44
        GDesktopBackgroundShading shading;
 
45
        char            *primary_color;
 
46
        char            *secondary_color;
 
47
        char            *source_url; /* Used by the Flickr source */
 
48
        char            *source_xml; /* Used by the Wallpapers source */
 
49
        gboolean         is_deleted;
 
50
        gboolean         needs_download;
 
51
        CcAppearanceItemFlags flags;
 
52
 
 
53
        /* internal */
 
54
        GnomeBG         *bg;
 
55
        char            *mime_type;
 
56
        int              width;
 
57
        int              height;
 
58
};
 
59
 
 
60
enum {
 
61
        PROP_0,
 
62
        PROP_NAME,
 
63
        PROP_URI,
 
64
        PROP_PLACEMENT,
 
65
        PROP_SHADING,
 
66
        PROP_PRIMARY_COLOR,
 
67
        PROP_SECONDARY_COLOR,
 
68
        PROP_IS_DELETED,
 
69
        PROP_SOURCE_URL,
 
70
        PROP_SOURCE_XML,
 
71
        PROP_FLAGS,
 
72
        PROP_SIZE,
 
73
        PROP_NEEDS_DOWNLOAD
 
74
};
 
75
 
 
76
static void     cc_appearance_item_class_init     (CcAppearanceItemClass *klass);
 
77
static void     cc_appearance_item_init           (CcAppearanceItem      *appearance_item);
 
78
static void     cc_appearance_item_finalize       (GObject               *object);
 
79
 
 
80
G_DEFINE_TYPE (CcAppearanceItem, cc_appearance_item, G_TYPE_OBJECT)
 
81
 
 
82
static GEmblem *
 
83
get_slideshow_icon (void)
 
84
{
 
85
        GIcon *themed;
 
86
        GEmblem *emblem;
 
87
        themed = g_themed_icon_new ("unity-slideshow-emblem");
 
88
        emblem = g_emblem_new_with_origin (themed, G_EMBLEM_ORIGIN_DEVICE);
 
89
        g_object_unref (themed);
 
90
        return emblem;
 
91
}
 
92
 
 
93
static void
 
94
set_bg_properties (CcAppearanceItem *item)
 
95
{
 
96
        GdkColor pcolor = { 0, 0, 0, 0 };
 
97
        GdkColor scolor = { 0, 0, 0, 0 };
 
98
 
 
99
        if (item->priv->uri) {
 
100
                GFile *file;
 
101
                char *filename;
 
102
 
 
103
                file = g_file_new_for_commandline_arg (item->priv->uri);
 
104
                filename = g_file_get_path (file);
 
105
                g_object_unref (file);
 
106
 
 
107
                gnome_bg_set_filename (item->priv->bg, filename);
 
108
                g_free (filename);
 
109
        }
 
110
 
 
111
        if (item->priv->primary_color != NULL) {
 
112
                gdk_color_parse (item->priv->primary_color, &pcolor);
 
113
        }
 
114
        if (item->priv->secondary_color != NULL) {
 
115
                gdk_color_parse (item->priv->secondary_color, &scolor);
 
116
        }
 
117
 
 
118
        gnome_bg_set_color (item->priv->bg, item->priv->shading, &pcolor, &scolor);
 
119
        gnome_bg_set_placement (item->priv->bg, item->priv->placement);
 
120
}
 
121
 
 
122
 
 
123
gboolean
 
124
cc_appearance_item_changes_with_time (CcAppearanceItem *item)
 
125
{
 
126
        gboolean changes;
 
127
 
 
128
        g_return_val_if_fail (CC_IS_APPEARANCE_ITEM (item), FALSE);
 
129
 
 
130
        changes = FALSE;
 
131
        if (item->priv->bg != NULL) {
 
132
                changes = gnome_bg_changes_with_time (item->priv->bg);
 
133
        }
 
134
        return changes;
 
135
}
 
136
 
 
137
static void
 
138
update_size (CcAppearanceItem *item)
 
139
{
 
140
        g_free (item->priv->size);
 
141
        item->priv->size = NULL;
 
142
 
 
143
        if (item->priv->uri == NULL) {
 
144
                item->priv->size = g_strdup ("");
 
145
        } else {
 
146
                if (gnome_bg_has_multiple_sizes (item->priv->bg) || gnome_bg_changes_with_time (item->priv->bg)) {
 
147
                        item->priv->size = g_strdup (_("multiple sizes"));
 
148
                } else {
 
149
                        /* translators: 100 Ć— 100px
 
150
                         * Note that this is not an "x", but U+00D7 MULTIPLICATION SIGN */
 
151
                        item->priv->size = g_strdup_printf (_("%d \303\227 %d"),
 
152
                                                            item->priv->width,
 
153
                                                            item->priv->height);
 
154
                }
 
155
        }
 
156
}
 
157
 
 
158
static GdkPixbuf *
 
159
render_at_size (GnomeBG *bg,
 
160
                gint width,
 
161
                gint height)
 
162
{
 
163
        GdkPixbuf *pixbuf;
 
164
 
 
165
        pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height);
 
166
        gnome_bg_draw (bg, pixbuf, gdk_screen_get_default (), FALSE);
 
167
 
 
168
        return pixbuf;
 
169
}
 
170
 
 
171
GIcon *
 
172
cc_appearance_item_get_frame_thumbnail (CcAppearanceItem             *item,
 
173
                                        GnomeDesktopThumbnailFactory *thumbs,
 
174
                                        int                           width,
 
175
                                        int                           height,
 
176
                                        int                           frame,
 
177
                                        gboolean                      force_size)
 
178
{
 
179
        GdkPixbuf *pixbuf = NULL;
 
180
        GIcon *icon = NULL;
 
181
 
 
182
        g_return_val_if_fail (CC_IS_APPEARANCE_ITEM (item), NULL);
 
183
        g_return_val_if_fail (width > 0 && height > 0, NULL);
 
184
 
 
185
        set_bg_properties (item);
 
186
 
 
187
        if (force_size) {
 
188
                /* FIXME: this doesn't play nice with slideshow stepping at all,
 
189
                 * because it will always render the current slideshow frame, which
 
190
                 * might not be what we want.
 
191
                 * We're lacking an API to draw a high-res GnomeBG manually choosing
 
192
                 * the slideshow frame though, so we can't do much better than this
 
193
                 * for now.
 
194
                 */
 
195
                pixbuf = render_at_size (item->priv->bg, width, height);
 
196
        } else {
 
197
                if (frame >= 0) {
 
198
                        pixbuf = gnome_bg_create_frame_thumbnail (item->priv->bg,
 
199
                                                                  thumbs,
 
200
                                                                  gdk_screen_get_default (),
 
201
                                                                  width,
 
202
                                                                  height,
 
203
                                                                  frame);
 
204
                } else {
 
205
                        pixbuf = gnome_bg_create_thumbnail (item->priv->bg,
 
206
                                                            thumbs,
 
207
                                                            gdk_screen_get_default (),
 
208
                                                            width,
 
209
                                                            height);
 
210
                }
 
211
        }
 
212
 
 
213
        if (pixbuf != NULL
 
214
            && frame != -2
 
215
            && gnome_bg_changes_with_time (item->priv->bg)) {
 
216
                GEmblem *emblem;
 
217
 
 
218
                emblem = get_slideshow_icon ();
 
219
                icon = g_emblemed_icon_new (G_ICON (pixbuf), emblem);
 
220
                g_object_unref (emblem);
 
221
                g_object_unref (pixbuf);
 
222
        } else {
 
223
                icon = G_ICON (pixbuf);
 
224
        }
 
225
 
 
226
        gnome_bg_get_image_size (item->priv->bg,
 
227
                                 thumbs,
 
228
                                 width,
 
229
                                 height,
 
230
                                 &item->priv->width,
 
231
                                 &item->priv->height);
 
232
 
 
233
        update_size (item);
 
234
 
 
235
        return icon;
 
236
}
 
237
 
 
238
 
 
239
GIcon *
 
240
cc_appearance_item_get_thumbnail (CcAppearanceItem             *item,
 
241
                                  GnomeDesktopThumbnailFactory *thumbs,
 
242
                                  int                           width,
 
243
                                  int                           height)
 
244
{
 
245
        return cc_appearance_item_get_frame_thumbnail (item, thumbs, width, height, -1, FALSE);
 
246
}
 
247
 
 
248
static void
 
249
update_info (CcAppearanceItem *item,
 
250
             GFileInfo        *_info)
 
251
{
 
252
        GFile     *file;
 
253
        GFileInfo *info;
 
254
 
 
255
        if (_info == NULL) {
 
256
                file = g_file_new_for_uri (item->priv->uri);
 
257
 
 
258
                info = g_file_query_info (file,
 
259
                                          G_FILE_ATTRIBUTE_STANDARD_NAME ","
 
260
                                          G_FILE_ATTRIBUTE_STANDARD_SIZE ","
 
261
                                          G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
 
262
                                          G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
 
263
                                          G_FILE_ATTRIBUTE_TIME_MODIFIED,
 
264
                                          G_FILE_QUERY_INFO_NONE,
 
265
                                          NULL,
 
266
                                          NULL);
 
267
                g_object_unref (file);
 
268
        } else {
 
269
                info = g_object_ref (_info);
 
270
        }
 
271
 
 
272
        g_free (item->priv->mime_type);
 
273
        item->priv->mime_type = NULL;
 
274
 
 
275
        if (info == NULL
 
276
            || g_file_info_get_content_type (info) == NULL) {
 
277
                if (item->priv->uri == NULL) {
 
278
                        item->priv->mime_type = g_strdup ("image/x-no-data");
 
279
                        g_free (item->priv->name);
 
280
                        item->priv->name = g_strdup (_("No Desktop Background"));
 
281
                }
 
282
        } else {
 
283
                if (item->priv->name == NULL)
 
284
                        item->priv->name = g_strdup (g_file_info_get_display_name (info));
 
285
 
 
286
                item->priv->mime_type = g_strdup (g_file_info_get_content_type (info));
 
287
        }
 
288
 
 
289
        if (info != NULL)
 
290
                g_object_unref (info);
 
291
}
 
292
 
 
293
gboolean
 
294
cc_appearance_item_load (CcAppearanceItem *item,
 
295
                         GFileInfo        *info)
 
296
{
 
297
        g_return_val_if_fail (CC_IS_APPEARANCE_ITEM (item), FALSE);
 
298
 
 
299
        if (item->priv->uri == NULL)
 
300
                return TRUE;
 
301
 
 
302
        update_info (item, info);
 
303
 
 
304
        if (item->priv->mime_type != NULL
 
305
            && (g_str_has_prefix (item->priv->mime_type, "image/")
 
306
                || strcmp (item->priv->mime_type, "application/xml") == 0)) {
 
307
                set_bg_properties (item);
 
308
        } else {
 
309
                return FALSE;
 
310
        }
 
311
 
 
312
        /* FIXME we should handle XML files as well */
 
313
        if (item->priv->mime_type != NULL &&
 
314
            g_str_has_prefix (item->priv->mime_type, "image/")) {
 
315
                char *filename;
 
316
 
 
317
                filename = g_filename_from_uri (item->priv->uri, NULL, NULL);
 
318
                gdk_pixbuf_get_file_info (filename,
 
319
                                          &item->priv->width,
 
320
                                          &item->priv->height);
 
321
                g_free (filename);
 
322
                update_size (item);
 
323
        }
 
324
 
 
325
        return TRUE;
 
326
}
 
327
 
 
328
static void
 
329
_set_name (CcAppearanceItem *item,
 
330
           const char       *value)
 
331
{
 
332
        g_free (item->priv->name);
 
333
        item->priv->name = g_strdup (value);
 
334
}
 
335
 
 
336
const char *
 
337
cc_appearance_item_get_name (CcAppearanceItem *item)
 
338
{
 
339
        g_return_val_if_fail (CC_IS_APPEARANCE_ITEM (item), NULL);
 
340
 
 
341
        return item->priv->name;
 
342
}
 
343
 
 
344
static void
 
345
_set_uri (CcAppearanceItem *item,
 
346
          const char       *value)
 
347
{
 
348
        g_free (item->priv->uri);
 
349
        if (value && *value == '\0') {
 
350
                item->priv->uri = NULL;
 
351
        } else {
 
352
                if (value && strstr (value, "://") == NULL)
 
353
                        g_warning ("URI '%s' is invalid", value);
 
354
                item->priv->uri = g_strdup (value);
 
355
        }
 
356
}
 
357
 
 
358
const char *
 
359
cc_appearance_item_get_uri (CcAppearanceItem *item)
 
360
{
 
361
        g_return_val_if_fail (CC_IS_APPEARANCE_ITEM (item), NULL);
 
362
 
 
363
        return item->priv->uri;
 
364
}
 
365
 
 
366
static void
 
367
_set_placement (CcAppearanceItem        *item,
 
368
                GDesktopBackgroundStyle  value)
 
369
{
 
370
        item->priv->placement = value;
 
371
}
 
372
 
 
373
static void
 
374
_set_shading (CcAppearanceItem          *item,
 
375
              GDesktopBackgroundShading  value)
 
376
{
 
377
        item->priv->shading = value;
 
378
}
 
379
 
 
380
static void
 
381
_set_primary_color (CcAppearanceItem *item,
 
382
                    const char       *value)
 
383
{
 
384
        g_free (item->priv->primary_color);
 
385
        item->priv->primary_color = g_strdup (value);
 
386
}
 
387
 
 
388
const char *
 
389
cc_appearance_item_get_pcolor (CcAppearanceItem *item)
 
390
{
 
391
        g_return_val_if_fail (CC_IS_APPEARANCE_ITEM (item), NULL);
 
392
 
 
393
        return item->priv->primary_color;
 
394
}
 
395
 
 
396
static void
 
397
_set_secondary_color (CcAppearanceItem *item,
 
398
                      const char       *value)
 
399
{
 
400
        g_free (item->priv->secondary_color);
 
401
        item->priv->secondary_color = g_strdup (value);
 
402
}
 
403
 
 
404
const char *
 
405
cc_appearance_item_get_scolor (CcAppearanceItem *item)
 
406
{
 
407
        g_return_val_if_fail (CC_IS_APPEARANCE_ITEM (item), NULL);
 
408
 
 
409
        return item->priv->secondary_color;
 
410
}
 
411
 
 
412
GDesktopBackgroundStyle
 
413
cc_appearance_item_get_placement (CcAppearanceItem *item)
 
414
{
 
415
        g_return_val_if_fail (CC_IS_APPEARANCE_ITEM (item), G_DESKTOP_BACKGROUND_STYLE_SCALED);
 
416
 
 
417
        return item->priv->placement;
 
418
}
 
419
 
 
420
GDesktopBackgroundShading
 
421
cc_appearance_item_get_shading (CcAppearanceItem *item)
 
422
{
 
423
        g_return_val_if_fail (CC_IS_APPEARANCE_ITEM (item), G_DESKTOP_BACKGROUND_SHADING_SOLID);
 
424
 
 
425
        return item->priv->shading;
 
426
}
 
427
 
 
428
static void
 
429
_set_is_deleted (CcAppearanceItem *item,
 
430
                 gboolean          value)
 
431
{
 
432
        item->priv->is_deleted = value;
 
433
}
 
434
 
 
435
static void
 
436
_set_source_url (CcAppearanceItem *item,
 
437
                 const char       *value)
 
438
{
 
439
        g_free (item->priv->source_url);
 
440
        item->priv->source_url = g_strdup (value);
 
441
}
 
442
 
 
443
const char *
 
444
cc_appearance_item_get_source_url (CcAppearanceItem *item)
 
445
{
 
446
        g_return_val_if_fail (CC_IS_APPEARANCE_ITEM (item), NULL);
 
447
 
 
448
        return item->priv->source_url;
 
449
}
 
450
 
 
451
static void
 
452
_set_source_xml (CcAppearanceItem *item,
 
453
                 const char       *value)
 
454
{
 
455
        g_free (item->priv->source_xml);
 
456
        item->priv->source_xml = g_strdup (value);
 
457
}
 
458
 
 
459
const char *
 
460
cc_appearance_item_get_source_xml (CcAppearanceItem *item)
 
461
{
 
462
        g_return_val_if_fail (CC_IS_APPEARANCE_ITEM (item), NULL);
 
463
 
 
464
        return item->priv->source_xml;
 
465
}
 
466
 
 
467
static void
 
468
_set_flags (CcAppearanceItem      *item,
 
469
            CcAppearanceItemFlags  value)
 
470
{
 
471
        item->priv->flags = value;
 
472
}
 
473
 
 
474
CcAppearanceItemFlags
 
475
cc_appearance_item_get_flags (CcAppearanceItem *item)
 
476
{
 
477
        g_return_val_if_fail (CC_IS_APPEARANCE_ITEM (item), 0);
 
478
 
 
479
        return item->priv->flags;
 
480
}
 
481
 
 
482
const char *
 
483
cc_appearance_item_get_size (CcAppearanceItem *item)
 
484
{
 
485
        g_return_val_if_fail (CC_IS_APPEARANCE_ITEM (item), NULL);
 
486
 
 
487
        return item->priv->size;
 
488
}
 
489
 
 
490
static void
 
491
_set_needs_download (CcAppearanceItem *item,
 
492
                     gboolean          value)
 
493
{
 
494
        item->priv->needs_download = value;
 
495
}
 
496
 
 
497
gboolean
 
498
cc_appearance_item_get_needs_download (CcAppearanceItem *item)
 
499
{
 
500
        g_return_val_if_fail (CC_IS_APPEARANCE_ITEM (item), 0);
 
501
 
 
502
        return item->priv->needs_download;
 
503
}
 
504
 
 
505
static void
 
506
cc_appearance_item_set_property (GObject      *object,
 
507
                                 guint         prop_id,
 
508
                                 const GValue *value,
 
509
                                 GParamSpec   *pspec)
 
510
{
 
511
        CcAppearanceItem *self;
 
512
 
 
513
        self = CC_APPEARANCE_ITEM (object);
 
514
 
 
515
        switch (prop_id) {
 
516
        case PROP_NAME:
 
517
                _set_name (self, g_value_get_string (value));
 
518
                break;
 
519
        case PROP_URI:
 
520
                _set_uri (self, g_value_get_string (value));
 
521
                break;
 
522
        case PROP_PLACEMENT:
 
523
                _set_placement (self, g_value_get_enum (value));
 
524
                break;
 
525
        case PROP_SHADING:
 
526
                _set_shading (self, g_value_get_enum (value));
 
527
                break;
 
528
        case PROP_PRIMARY_COLOR:
 
529
                _set_primary_color (self, g_value_get_string (value));
 
530
                break;
 
531
        case PROP_SECONDARY_COLOR:
 
532
                _set_secondary_color (self, g_value_get_string (value));
 
533
                break;
 
534
        case PROP_IS_DELETED:
 
535
                _set_is_deleted (self, g_value_get_boolean (value));
 
536
                break;
 
537
        case PROP_SOURCE_URL:
 
538
                _set_source_url (self, g_value_get_string (value));
 
539
                break;
 
540
        case PROP_SOURCE_XML:
 
541
                _set_source_xml (self, g_value_get_string (value));
 
542
                break;
 
543
        case PROP_FLAGS:
 
544
                _set_flags (self, g_value_get_flags (value));
 
545
                break;
 
546
        case PROP_NEEDS_DOWNLOAD:
 
547
                _set_needs_download (self, g_value_get_boolean (value));
 
548
                break;
 
549
        default:
 
550
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
551
                break;
 
552
        }
 
553
}
 
554
 
 
555
static void
 
556
cc_appearance_item_get_property (GObject    *object,
 
557
                                 guint       prop_id,
 
558
                                 GValue     *value,
 
559
                                 GParamSpec *pspec)
 
560
{
 
561
        CcAppearanceItem *self;
 
562
 
 
563
        self = CC_APPEARANCE_ITEM (object);
 
564
 
 
565
        switch (prop_id) {
 
566
        case PROP_NAME:
 
567
                g_value_set_string (value, self->priv->name);
 
568
                break;
 
569
        case PROP_URI:
 
570
                g_value_set_string (value, self->priv->uri);
 
571
                break;
 
572
        case PROP_PLACEMENT:
 
573
                g_value_set_enum (value, self->priv->placement);
 
574
                break;
 
575
        case PROP_SHADING:
 
576
                g_value_set_enum (value, self->priv->shading);
 
577
                break;
 
578
        case PROP_PRIMARY_COLOR:
 
579
                g_value_set_string (value, self->priv->primary_color);
 
580
                break;
 
581
        case PROP_SECONDARY_COLOR:
 
582
                g_value_set_string (value, self->priv->secondary_color);
 
583
                break;
 
584
        case PROP_IS_DELETED:
 
585
                g_value_set_boolean (value, self->priv->is_deleted);
 
586
                break;
 
587
        case PROP_SOURCE_URL:
 
588
                g_value_set_string (value, self->priv->source_url);
 
589
                break;
 
590
        case PROP_SOURCE_XML:
 
591
                g_value_set_string (value, self->priv->source_xml);
 
592
                break;
 
593
        case PROP_FLAGS:
 
594
                g_value_set_flags (value, self->priv->flags);
 
595
                break;
 
596
        case PROP_SIZE:
 
597
                g_value_set_string (value, self->priv->size);
 
598
                break;
 
599
        case PROP_NEEDS_DOWNLOAD:
 
600
                g_value_set_boolean (value, self->priv->needs_download);
 
601
                break;
 
602
        default:
 
603
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
604
                break;
 
605
        }
 
606
}
 
607
 
 
608
static GObject *
 
609
cc_appearance_item_constructor (GType                  type,
 
610
                                guint                  n_construct_properties,
 
611
                                GObjectConstructParam *construct_properties)
 
612
{
 
613
        CcAppearanceItem      *appearance_item;
 
614
 
 
615
        appearance_item = CC_APPEARANCE_ITEM (G_OBJECT_CLASS (cc_appearance_item_parent_class)->constructor (type,
 
616
                                                                                                                         n_construct_properties,
 
617
                                                                                                                         construct_properties));
 
618
 
 
619
        return G_OBJECT (appearance_item);
 
620
}
 
621
 
 
622
static void
 
623
cc_appearance_item_class_init (CcAppearanceItemClass *klass)
 
624
{
 
625
        GObjectClass  *object_class = G_OBJECT_CLASS (klass);
 
626
 
 
627
        object_class->get_property = cc_appearance_item_get_property;
 
628
        object_class->set_property = cc_appearance_item_set_property;
 
629
        object_class->constructor = cc_appearance_item_constructor;
 
630
        object_class->finalize = cc_appearance_item_finalize;
 
631
 
 
632
        g_object_class_install_property (object_class,
 
633
                                         PROP_NAME,
 
634
                                         g_param_spec_string ("name",
 
635
                                                              "name",
 
636
                                                              "name",
 
637
                                                              NULL,
 
638
                                                              G_PARAM_READWRITE));
 
639
        g_object_class_install_property (object_class,
 
640
                                         PROP_URI,
 
641
                                         g_param_spec_string ("uri",
 
642
                                                              "uri",
 
643
                                                              "uri",
 
644
                                                              NULL,
 
645
                                                              G_PARAM_READWRITE));
 
646
        g_object_class_install_property (object_class,
 
647
                                         PROP_PLACEMENT,
 
648
                                         g_param_spec_enum ("placement",
 
649
                                                            "placement",
 
650
                                                            "placement",
 
651
                                                            G_DESKTOP_TYPE_DESKTOP_BACKGROUND_STYLE,
 
652
                                                            G_DESKTOP_BACKGROUND_STYLE_SCALED,
 
653
                                                            G_PARAM_READWRITE));
 
654
 
 
655
        g_object_class_install_property (object_class,
 
656
                                         PROP_SHADING,
 
657
                                         g_param_spec_enum ("shading",
 
658
                                                            "shading",
 
659
                                                            "shading",
 
660
                                                            G_DESKTOP_TYPE_DESKTOP_BACKGROUND_SHADING,
 
661
                                                            G_DESKTOP_BACKGROUND_SHADING_SOLID,
 
662
                                                            G_PARAM_READWRITE));
 
663
        g_object_class_install_property (object_class,
 
664
                                         PROP_PRIMARY_COLOR,
 
665
                                         g_param_spec_string ("primary-color",
 
666
                                                              "primary-color",
 
667
                                                              "primary-color",
 
668
                                                              "#000000000000",
 
669
                                                              G_PARAM_READWRITE));
 
670
        g_object_class_install_property (object_class,
 
671
                                         PROP_SECONDARY_COLOR,
 
672
                                         g_param_spec_string ("secondary-color",
 
673
                                                              "secondary-color",
 
674
                                                              "secondary-color",
 
675
                                                              "#000000000000",
 
676
                                                              G_PARAM_READWRITE));
 
677
 
 
678
        g_object_class_install_property (object_class,
 
679
                                         PROP_IS_DELETED,
 
680
                                         g_param_spec_boolean ("is-deleted",
 
681
                                                               NULL,
 
682
                                                               NULL,
 
683
                                                               FALSE,
 
684
                                                               G_PARAM_READWRITE));
 
685
 
 
686
        g_object_class_install_property (object_class,
 
687
                                         PROP_SOURCE_URL,
 
688
                                         g_param_spec_string ("source-url",
 
689
                                                              "source-url",
 
690
                                                              "source-url",
 
691
                                                              NULL,
 
692
                                                              G_PARAM_READWRITE));
 
693
 
 
694
        g_object_class_install_property (object_class,
 
695
                                         PROP_SOURCE_XML,
 
696
                                         g_param_spec_string ("source-xml",
 
697
                                                              "source-xml",
 
698
                                                              "source-xml",
 
699
                                                              NULL,
 
700
                                                              G_PARAM_READWRITE));
 
701
 
 
702
        g_object_class_install_property (object_class,
 
703
                                         PROP_FLAGS,
 
704
                                         g_param_spec_flags ("flags",
 
705
                                                             "flags",
 
706
                                                             "flags",
 
707
                                                             G_DESKTOP_TYPE_APPEARANCE_ITEM_FLAGS,
 
708
                                                             0,
 
709
                                                             G_PARAM_READWRITE));
 
710
 
 
711
        g_object_class_install_property (object_class,
 
712
                                         PROP_SIZE,
 
713
                                         g_param_spec_string ("size",
 
714
                                                              "size",
 
715
                                                              "size",
 
716
                                                              NULL,
 
717
                                                              G_PARAM_READABLE));
 
718
 
 
719
        g_object_class_install_property (object_class,
 
720
                                         PROP_NEEDS_DOWNLOAD,
 
721
                                         g_param_spec_boolean ("needs-download",
 
722
                                                               NULL,
 
723
                                                               NULL,
 
724
                                                               TRUE,
 
725
                                                               G_PARAM_READWRITE));
 
726
 
 
727
 
 
728
        g_type_class_add_private (klass, sizeof (CcAppearanceItemPrivate));
 
729
}
 
730
 
 
731
static void
 
732
cc_appearance_item_init (CcAppearanceItem *item)
 
733
{
 
734
        item->priv = CC_APPEARANCE_ITEM_GET_PRIVATE (item);
 
735
 
 
736
        item->priv->bg = gnome_bg_new ();
 
737
 
 
738
        item->priv->shading = G_DESKTOP_BACKGROUND_SHADING_SOLID;
 
739
        item->priv->placement = G_DESKTOP_BACKGROUND_STYLE_SCALED;
 
740
        item->priv->primary_color = g_strdup ("#000000000000");
 
741
        item->priv->secondary_color = g_strdup ("#000000000000");
 
742
        item->priv->needs_download = TRUE;
 
743
        item->priv->flags = 0;
 
744
}
 
745
 
 
746
static void
 
747
cc_appearance_item_finalize (GObject *object)
 
748
{
 
749
        CcAppearanceItem *item;
 
750
 
 
751
        g_return_if_fail (object != NULL);
 
752
        g_return_if_fail (CC_IS_APPEARANCE_ITEM (object));
 
753
 
 
754
        item = CC_APPEARANCE_ITEM (object);
 
755
 
 
756
        g_return_if_fail (item->priv != NULL);
 
757
 
 
758
        g_free (item->priv->name);
 
759
        g_free (item->priv->uri);
 
760
        g_free (item->priv->primary_color);
 
761
        g_free (item->priv->secondary_color);
 
762
        g_free (item->priv->mime_type);
 
763
        g_free (item->priv->size);
 
764
 
 
765
        if (item->priv->bg != NULL)
 
766
                g_object_unref (item->priv->bg);
 
767
 
 
768
        G_OBJECT_CLASS (cc_appearance_item_parent_class)->finalize (object);
 
769
}
 
770
 
 
771
CcAppearanceItem *
 
772
cc_appearance_item_new (const char *uri)
 
773
{
 
774
        GObject *object;
 
775
 
 
776
        object = g_object_new (CC_TYPE_APPEARANCE_ITEM,
 
777
                               "uri", uri,
 
778
                               NULL);
 
779
 
 
780
        return CC_APPEARANCE_ITEM (object);
 
781
}
 
782
 
 
783
CcAppearanceItem *
 
784
cc_appearance_item_copy (CcAppearanceItem *item)
 
785
{
 
786
        CcAppearanceItem *ret;
 
787
 
 
788
        ret = cc_appearance_item_new (item->priv->uri);
 
789
        ret->priv->name = g_strdup (item->priv->name);
 
790
        ret->priv->size = g_strdup (item->priv->size);
 
791
        ret->priv->placement = item->priv->placement;
 
792
        ret->priv->shading = item->priv->shading;
 
793
        ret->priv->primary_color = g_strdup (item->priv->primary_color);
 
794
        ret->priv->secondary_color = g_strdup (item->priv->secondary_color);
 
795
        ret->priv->source_url = g_strdup (item->priv->source_url);
 
796
        ret->priv->source_xml = g_strdup (item->priv->source_xml);
 
797
        ret->priv->is_deleted = item->priv->is_deleted;
 
798
        ret->priv->needs_download = item->priv->needs_download;
 
799
        ret->priv->flags = item->priv->flags;
 
800
 
 
801
        return ret;
 
802
}
 
803
 
 
804
static const char *
 
805
flags_to_str (CcAppearanceItemFlags flag)
 
806
{
 
807
        GFlagsClass *fclass;
 
808
        GFlagsValue *value;
 
809
 
 
810
        fclass = G_FLAGS_CLASS (g_type_class_peek (G_DESKTOP_TYPE_APPEARANCE_ITEM_FLAGS));
 
811
        value = g_flags_get_first_value (fclass, flag);
 
812
 
 
813
        g_assert (value);
 
814
 
 
815
        return value->value_nick;
 
816
}
 
817
 
 
818
static const char *
 
819
enum_to_str (GType type,
 
820
             int   v)
 
821
{
 
822
        GEnumClass *eclass;
 
823
        GEnumValue *value;
 
824
 
 
825
        eclass = G_ENUM_CLASS (g_type_class_peek (type));
 
826
        value = g_enum_get_value (eclass, v);
 
827
 
 
828
        g_assert (value);
 
829
 
 
830
        return value->value_nick;
 
831
}
 
832
 
 
833
void
 
834
cc_appearance_item_dump (CcAppearanceItem *item)
 
835
{
 
836
        CcAppearanceItemPrivate *priv;
 
837
        GString *flags;
 
838
        int i;
 
839
 
 
840
        g_return_if_fail (CC_IS_APPEARANCE_ITEM (item));
 
841
 
 
842
        priv = item->priv;
 
843
 
 
844
        g_debug ("name:\t\t\t%s", priv->name);
 
845
        g_debug ("URI:\t\t\t%s", priv->uri ? priv->uri : "NULL");
 
846
        if (priv->size)
 
847
                g_debug ("size:\t\t\t'%s'", priv->size);
 
848
        flags = g_string_new (NULL);
 
849
        for (i = 0; i < 5; i++) {
 
850
                if (priv->flags & (1 << i)) {
 
851
                        g_string_append (flags, flags_to_str (1 << i));
 
852
                        g_string_append_c (flags, ' ');
 
853
                }
 
854
        }
 
855
        if (flags->len == 0)
 
856
                g_string_append (flags, "-none-");
 
857
        g_debug ("flags:\t\t\t%s", flags->str);
 
858
        g_string_free (flags, TRUE);
 
859
        if (priv->primary_color)
 
860
                g_debug ("pcolor:\t\t%s", priv->primary_color);
 
861
        if (priv->secondary_color)
 
862
                g_debug ("scolor:\t\t%s", priv->secondary_color);
 
863
        g_debug ("placement:\t\t%s", enum_to_str (G_DESKTOP_TYPE_DESKTOP_BACKGROUND_STYLE, priv->placement));
 
864
        g_debug ("shading:\t\t%s", enum_to_str (G_DESKTOP_TYPE_DESKTOP_BACKGROUND_SHADING, priv->shading));
 
865
        if (priv->source_url)
 
866
                g_debug ("source URL:\t\t%s", priv->source_url);
 
867
        if (priv->source_xml)
 
868
                g_debug ("source XML:\t\t%s", priv->source_xml);
 
869
        g_debug ("deleted:\t\t%s", priv->is_deleted ? "yes" : "no");
 
870
        if (priv->mime_type)
 
871
                g_debug ("mime-type:\t\t%s", priv->mime_type);
 
872
        g_debug ("dimensions:\t\t%d x %d", priv->width, priv->height);
 
873
        g_debug (" ");
 
874
}
 
875
 
 
876
static gboolean
 
877
files_equal (const char *a,
 
878
             const char *b)
 
879
{
 
880
        GFile *file1, *file2;
 
881
        gboolean retval;
 
882
 
 
883
        if (a == NULL &&
 
884
            b == NULL)
 
885
                return TRUE;
 
886
 
 
887
        if (a == NULL ||
 
888
            b == NULL)
 
889
                return FALSE;
 
890
 
 
891
        file1 = g_file_new_for_commandline_arg (a);
 
892
        file2 = g_file_new_for_commandline_arg (b);
 
893
        if (g_file_equal (file1, file2) == FALSE)
 
894
                retval = FALSE;
 
895
        else
 
896
                retval = TRUE;
 
897
        g_object_unref (file1);
 
898
        g_object_unref (file2);
 
899
 
 
900
        return retval;
 
901
}
 
902
 
 
903
static gboolean
 
904
colors_equal (const char *a,
 
905
              const char *b)
 
906
{
 
907
        GdkColor color1, color2;
 
908
 
 
909
        gdk_color_parse (a, &color1);
 
910
        gdk_color_parse (b, &color2);
 
911
 
 
912
        return gdk_color_equal (&color1, &color2);
 
913
}
 
914
 
 
915
gboolean
 
916
cc_appearance_item_compare (CcAppearanceItem *saved,
 
917
                            CcAppearanceItem *configured)
 
918
{
 
919
        CcAppearanceItemFlags flags;
 
920
 
 
921
        flags = saved->priv->flags;
 
922
        if (flags == 0)
 
923
                return FALSE;
 
924
 
 
925
        if (flags & CC_APPEARANCE_ITEM_HAS_URI) {
 
926
                if (files_equal (saved->priv->uri, configured->priv->uri) == FALSE)
 
927
                        return FALSE;
 
928
        }
 
929
        if (flags & CC_APPEARANCE_ITEM_HAS_SHADING) {
 
930
                if (saved->priv->shading != configured->priv->shading)
 
931
                        return FALSE;
 
932
        }
 
933
        if (flags & CC_APPEARANCE_ITEM_HAS_PLACEMENT) {
 
934
                if (saved->priv->placement != configured->priv->placement)
 
935
                        return FALSE;
 
936
        }
 
937
        if (flags & CC_APPEARANCE_ITEM_HAS_PCOLOR) {
 
938
                if (colors_equal (saved->priv->primary_color,
 
939
                                  configured->priv->primary_color) == FALSE) {
 
940
                        return FALSE;
 
941
                }
 
942
        }
 
943
        if (flags & CC_APPEARANCE_ITEM_HAS_SCOLOR) {
 
944
                if (colors_equal (saved->priv->secondary_color,
 
945
                                  configured->priv->secondary_color) == FALSE) {
 
946
                        return FALSE;
 
947
                }
 
948
        }
 
949
 
 
950
        return TRUE;
 
951
}