Debian Codegraph @

src/cd-inhibit.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2011 Richard Hughes <[email protected]>
4 *
5 * Licensed under the GNU General Public License Version 2
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include "config.h"
23
24 #include <glib-object.h>
25 #include <gio/gio.h>
26
27 #include "cd-inhibit.h"
28
29 static void cd_inhibit_finalize (GObject *object);
30
31 #define GET_PRIVATE(o) (cd_inhibit_get_instance_private (o))
32
33 typedef struct
34 {
35 GPtrArray *array;
36 } CdInhibitPrivate;
37
38 typedef struct {
39 gchar *sender;
40 guint watcher_id;
41 } CdInhibitItem;
42
43
44 enum {
45 SIGNAL_CHANGED,
46 SIGNAL_LAST
47 };
48
49 static guint signals [SIGNAL_LAST] = { 0 };
50
51 G_DEFINE_TYPE_WITH_PRIVATE (CdInhibit, cd_inhibit, G_TYPE_OBJECT)
52
53 gboolean
54 cd_inhibit_valid (CdInhibit *inhibit)
55 {
56 CdInhibitPrivate *priv = GET_PRIVATE (inhibit);
57 g_return_val_if_fail (CD_IS_INHIBIT (inhibit), FALSE);
58 return priv->array->len == 0;
59 }
60
61 gchar **
62 cd_inhibit_get_bus_names (CdInhibit *inhibit)
63 {
64 CdInhibitItem *item_tmp;
65 CdInhibitPrivate *priv = GET_PRIVATE (inhibit);
66 gchar **bus_names;
67 guint i;
68
69 /* just copy senders */
70 bus_names = g_new0 (gchar *, priv->array->len + 1);
71 for (i = 0; i < priv->array->len; i++) {
72 item_tmp = g_ptr_array_index (priv->array, i);
73 bus_names[i] = g_strdup (item_tmp->sender);
74 }
75 return bus_names;
76 }
77
78 static void
79 cd_inhibit_item_free (CdInhibitItem *item)
80 {
81 if (item->watcher_id > 0)
82 g_bus_unwatch_name (item->watcher_id);
83 g_free (item->sender);
84 g_free (item);
85 }
86
87 static CdInhibitItem *
88 cd_inhibit_get_by_sender (CdInhibit *inhibit,
89 const gchar *sender)
90 {
91 CdInhibitItem *item_tmp;
92 CdInhibitPrivate *priv = GET_PRIVATE (inhibit);
93 guint i;
94
95 /* find sender */
96 for (i = 0; i < priv->array->len; i++) {
97 item_tmp = g_ptr_array_index (priv->array, i);
98 if (g_strcmp0 (item_tmp->sender, sender) == 0)
99 return item_tmp;
100 }
101 return NULL;
102 }
103
104 gboolean
105 cd_inhibit_remove (CdInhibit *inhibit, const gchar *sender, GError **error)
106 {
107 CdInhibitPrivate *priv = GET_PRIVATE (inhibit);
108 CdInhibitItem *item;
109
110 g_return_val_if_fail (CD_IS_INHIBIT (inhibit), FALSE);
111 g_return_val_if_fail (sender != NULL, FALSE);
112
113 /* do we already exist */
114 item = cd_inhibit_get_by_sender (inhibit, sender);
115 if (item == NULL) {
116 g_set_error (error, 1, 0,
117 "not set inhibitor for %s",
118 sender);
119 return FALSE;
120 }
121
122 /* remove */
123 if (!g_ptr_array_remove (priv->array, item)) {
124 g_set_error (error, 1, 0,
125 "cannot remove inhibit item for %s",
126 sender);
127 return FALSE;
128 }
129
130 /* emit signal */
131 g_debug ("CdInhibit: emit changed");
132 g_signal_emit (inhibit, signals[SIGNAL_CHANGED], 0);
133 return TRUE;
134 }
135
136 static void
137 cd_inhibit_name_vanished_cb (GDBusConnection *connection,
138 const gchar *name,
139 gpointer user_data)
140 {
141 CdInhibit *inhibit = CD_INHIBIT (user_data);
142 g_autoptr(GError) error = NULL;
143
144 /* just remove */
145 if (!cd_inhibit_remove (inhibit, name, &error)) {
146 g_warning ("CdInhibit: failed to remove when %s vanished: %s",
147 name, error->message);
148 } else {
149 g_debug ("CdInhibit: remove inhibit as %s vanished", name);
150 }
151 }
152
153 gboolean
154 cd_inhibit_add (CdInhibit *inhibit, const gchar *sender, GError **error)
155 {
156 CdInhibitPrivate *priv = GET_PRIVATE (inhibit);
157 CdInhibitItem *item;
158
159 g_return_val_if_fail (CD_IS_INHIBIT (inhibit), FALSE);
160 g_return_val_if_fail (sender != NULL, FALSE);
161
162 /* do we already exist */
163 item = cd_inhibit_get_by_sender (inhibit, sender);
164 if (item != NULL) {
165 g_set_error (error, 1, 0,
166 "already set inhibitor for %s",
167 sender);
168 return FALSE;
169 }
170
171 /* add */
172 item = g_new0 (CdInhibitItem, 1);
173 item->sender = g_strdup (sender);
174 item->watcher_id = g_bus_watch_name (G_BUS_TYPE_SYSTEM,
175 sender,
176 G_BUS_NAME_WATCHER_FLAGS_NONE,
177 NULL,
178 cd_inhibit_name_vanished_cb,
179 g_object_ref (inhibit),
180 g_object_unref);
181 g_ptr_array_add (priv->array, item);
182
183 /* emit signal */
184 g_debug ("CdInhibit: emit changed");
185 g_signal_emit (inhibit, signals[SIGNAL_CHANGED], 0);
186 return TRUE;
187 }
188
189 static void
190 cd_inhibit_class_init (CdInhibitClass *klass)
191 {
192 GObjectClass *object_class = G_OBJECT_CLASS (klass);
193 object_class->finalize = cd_inhibit_finalize;
194
195 signals [SIGNAL_CHANGED] =
196 g_signal_new ("changed",
197 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
198 G_STRUCT_OFFSET (CdInhibitClass, changed),
199 NULL, NULL, g_cclosure_marshal_VOID__VOID,
200 G_TYPE_NONE, 0);
201 }
202
203 static void
204 cd_inhibit_init (CdInhibit *inhibit)
205 {
206 CdInhibitPrivate *priv = GET_PRIVATE (inhibit);
207 priv->array = g_ptr_array_new_with_free_func ((GDestroyNotify) cd_inhibit_item_free);
208 }
209
210 static void
211 cd_inhibit_finalize (GObject *object)
212 {
213 CdInhibit *inhibit = CD_INHIBIT (object);
214 CdInhibitPrivate *priv = GET_PRIVATE (inhibit);
215
216 g_ptr_array_unref (priv->array);
217
218 G_OBJECT_CLASS (cd_inhibit_parent_class)->finalize (object);
219 }
220
221 CdInhibit *
222 cd_inhibit_new (void)
223 {
224 CdInhibit *inhibit;
225 inhibit = g_object_new (CD_TYPE_INHIBIT, NULL);
226 return CD_INHIBIT (inhibit);
227 }
228