GCC Code Coverage Report


Directory: ./
File: panels/background/bg-wallpapers-source.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 0 55 0.0%
Functions: 0 13 0.0%
Branches: 0 27 0.0%

Line Branch Exec Source
1 /* bg-wallpapers-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, see <http://www.gnu.org/licenses/>.
17 *
18 * Author: Thomas Wood <thomas.wood@intel.com>
19 *
20 */
21
22 #include "bg-wallpapers-source.h"
23
24 #include "cc-background-item.h"
25 #include "cc-background-xml.h"
26
27 #include <cairo-gobject.h>
28 #include <gio/gio.h>
29
30 struct _BgWallpapersSource
31 {
32 BgSource parent_instance;
33 CcBackgroundXml *xml;
34 };
35
36 G_DEFINE_TYPE (BgWallpapersSource, bg_wallpapers_source, BG_TYPE_SOURCE)
37
38 static int
39 sort_func (gconstpointer a,
40 gconstpointer b,
41 gpointer user_data)
42 {
43 CcBackgroundItem *item_a;
44 CcBackgroundItem *item_b;
45 const char *name_a;
46 const char *name_b;
47
48 item_a = (CcBackgroundItem *) a;
49 item_b = (CcBackgroundItem *) b;
50
51 name_a = cc_background_item_get_name (item_a);
52 name_b = cc_background_item_get_name (item_b);
53
54 if (name_a && strcmp (name_a, "Default Background") == 0)
55 return -1;
56 if (name_b && strcmp (name_b, "Default Background") == 0)
57 return 1;
58
59
60 return strcmp (cc_background_item_get_name (item_a),
61 cc_background_item_get_name (item_b));
62 }
63
64 static void
65 load_wallpapers (gchar *key,
66 CcBackgroundItem *item,
67 BgWallpapersSource *source)
68 {
69 GListStore *store = bg_source_get_liststore (BG_SOURCE (source));
70 gboolean deleted;
71
72 g_object_get (G_OBJECT (item), "is-deleted", &deleted, NULL);
73
74 if (deleted)
75 return;
76
77 g_list_store_insert_sorted (store, item, sort_func, NULL);
78 }
79
80 static void
81 list_load_cb (GObject *source_object,
82 GAsyncResult *res,
83 gpointer user_data)
84 {
85 g_autoptr(GError) error = NULL;
86 if (!cc_background_xml_load_list_finish (CC_BACKGROUND_XML (source_object), res, &error))
87 g_warning ("Failed to load background list: %s", error->message);
88 }
89
90 static void
91 item_added (BgWallpapersSource *self,
92 CcBackgroundItem *item)
93 {
94 load_wallpapers (NULL, item, self);
95 }
96
97 static void
98 load_default_bg (BgWallpapersSource *self)
99 {
100 const char * const *system_data_dirs;
101 guint i;
102
103 /* FIXME We could do this nicer if we had the XML source in GSettings */
104
105 system_data_dirs = g_get_system_data_dirs ();
106 for (i = 0; system_data_dirs[i]; i++) {
107 g_autofree gchar *filename = NULL;
108
109 filename = g_build_filename (system_data_dirs[i],
110 "gnome-background-properties",
111 "adwaita.xml",
112 NULL);
113 if (cc_background_xml_load_xml (self->xml, filename))
114 break;
115 }
116 }
117
118 static void
119 bg_wallpapers_source_constructed (GObject *object)
120 {
121 BgWallpapersSource *self = BG_WALLPAPERS_SOURCE (object);
122
123 G_OBJECT_CLASS (bg_wallpapers_source_parent_class)->constructed (object);
124
125 g_signal_connect_object (G_OBJECT (self->xml), "added",
126 G_CALLBACK (item_added), self, G_CONNECT_SWAPPED);
127
128 /* Try adding the default background first */
129 load_default_bg (self);
130
131 cc_background_xml_load_list_async (self->xml, NULL, list_load_cb, self);
132 }
133
134 static void
135 bg_wallpapers_source_dispose (GObject *object)
136 {
137 BgWallpapersSource *self = BG_WALLPAPERS_SOURCE (object);
138
139 g_clear_object (&self->xml);
140
141 G_OBJECT_CLASS (bg_wallpapers_source_parent_class)->dispose (object);
142 }
143
144 static void
145 bg_wallpapers_source_init (BgWallpapersSource *self)
146 {
147 self->xml = cc_background_xml_new ();
148 }
149
150 static void
151 bg_wallpapers_source_class_init (BgWallpapersSourceClass *klass)
152 {
153 GObjectClass *object_class = G_OBJECT_CLASS (klass);
154
155 object_class->constructed = bg_wallpapers_source_constructed;
156 object_class->dispose = bg_wallpapers_source_dispose;
157 }
158
159 BgWallpapersSource *
160 bg_wallpapers_source_new (void)
161 {
162 return g_object_new (BG_TYPE_WALLPAPERS_SOURCE, NULL);
163 }
164