File: cd-inhibit.c

package info (click to toggle)
colord 1.3.3-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 12,488 kB
  • sloc: ansic: 68,616; sh: 4,784; xml: 2,616; makefile: 1,921
file content (266 lines) | stat: -rw-r--r-- 6,134 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2011 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <glib-object.h>
#include <gio/gio.h>

#include "cd-inhibit.h"

static void     cd_inhibit_finalize	(GObject     *object);

#define GET_PRIVATE(o) (cd_inhibit_get_instance_private (o))

/**
 * CdInhibitPrivate:
 *
 * Private #CdInhibit data
 **/
typedef struct
{
	GPtrArray			*array;
} CdInhibitPrivate;

typedef struct {
	gchar				*sender;
	guint				 watcher_id;
} CdInhibitItem;


enum {
	SIGNAL_CHANGED,
	SIGNAL_LAST
};

static guint signals [SIGNAL_LAST] = { 0 };

G_DEFINE_TYPE_WITH_PRIVATE (CdInhibit, cd_inhibit, G_TYPE_OBJECT)

/**
 * cd_inhibit_valid:
 **/
gboolean
cd_inhibit_valid (CdInhibit *inhibit)
{
	CdInhibitPrivate *priv = GET_PRIVATE (inhibit);
	g_return_val_if_fail (CD_IS_INHIBIT (inhibit), FALSE);
	return priv->array->len == 0;
}

/**
 * cd_inhibit_get_bus_names:
 **/
gchar **
cd_inhibit_get_bus_names (CdInhibit *inhibit)
{
	CdInhibitItem *item_tmp;
	CdInhibitPrivate *priv = GET_PRIVATE (inhibit);
	gchar **bus_names;
	guint i;

	/* just copy senders */
	bus_names = g_new0 (gchar *, priv->array->len + 1);
	for (i = 0; i < priv->array->len; i++) {
		item_tmp = g_ptr_array_index (priv->array, i);
		bus_names[i] = g_strdup (item_tmp->sender);
	}
	return bus_names;
}

/**
 * cd_inhibit_valid:
 **/
static void
cd_inhibit_item_free (CdInhibitItem *item)
{
	if (item->watcher_id > 0)
		g_bus_unwatch_name (item->watcher_id);
	g_free (item->sender);
	g_free (item);
}

/**
 * cd_inhibit_get_by_sender:
 **/
static CdInhibitItem *
cd_inhibit_get_by_sender (CdInhibit *inhibit,
			  const gchar *sender)
{
	CdInhibitItem *item_tmp;
	CdInhibitPrivate *priv = GET_PRIVATE (inhibit);
	guint i;

	/* find sender */
	for (i = 0; i < priv->array->len; i++) {
		item_tmp = g_ptr_array_index (priv->array, i);
		if (g_strcmp0 (item_tmp->sender, sender) == 0)
			return item_tmp;
	}
	return NULL;
}

/**
 * cd_inhibit_remove:
 **/
gboolean
cd_inhibit_remove (CdInhibit *inhibit, const gchar *sender, GError **error)
{
	CdInhibitPrivate *priv = GET_PRIVATE (inhibit);
	CdInhibitItem *item;

	g_return_val_if_fail (CD_IS_INHIBIT (inhibit), FALSE);
	g_return_val_if_fail (sender != NULL, FALSE);

	/* do we already exist */
	item = cd_inhibit_get_by_sender (inhibit, sender);
	if (item == NULL) {
		g_set_error (error, 1, 0,
			     "not set inhibitor for %s",
			     sender);
		return FALSE;
	}
 
	/* remove */
	if (!g_ptr_array_remove (priv->array, item)) {
		g_set_error (error, 1, 0,
			     "cannot remove inhibit item for %s",
			     sender);
		return FALSE;
	}

	/* emit signal */
	g_debug ("CdInhibit: emit changed");
	g_signal_emit (inhibit, signals[SIGNAL_CHANGED], 0);
	return TRUE;
}

/**
 * cd_inhibit_name_vanished_cb:
 **/
static void
cd_inhibit_name_vanished_cb (GDBusConnection *connection,
			     const gchar *name,
			     gpointer user_data)
{
	CdInhibit *inhibit = CD_INHIBIT (user_data);
	g_autoptr(GError) error = NULL;

	/* just remove */
	if (!cd_inhibit_remove (inhibit, name, &error)) {
		g_warning ("CdInhibit: failed to remove when %s vanished: %s",
			   name, error->message);
	} else {
		g_debug ("CdInhibit: remove inhibit as %s vanished", name);
	}
}

/**
 * cd_inhibit_add:
 **/
gboolean
cd_inhibit_add (CdInhibit *inhibit, const gchar *sender, GError **error)
{
	CdInhibitPrivate *priv = GET_PRIVATE (inhibit);
	CdInhibitItem *item;

	g_return_val_if_fail (CD_IS_INHIBIT (inhibit), FALSE);
	g_return_val_if_fail (sender != NULL, FALSE);

	/* do we already exist */
	item = cd_inhibit_get_by_sender (inhibit, sender);
	if (item != NULL) {
		g_set_error (error, 1, 0,
			     "already set inhibitor for %s",
			     sender);
		return FALSE;
	}

	/* add */
	item = g_new0 (CdInhibitItem, 1);
	item->sender = g_strdup (sender);
	item->watcher_id = g_bus_watch_name (G_BUS_TYPE_SYSTEM,
					     sender,
					     G_BUS_NAME_WATCHER_FLAGS_NONE,
					     NULL,
					     cd_inhibit_name_vanished_cb,
					     g_object_ref (inhibit),
					     g_object_unref);
	g_ptr_array_add (priv->array, item);

	/* emit signal */
	g_debug ("CdInhibit: emit changed");
	g_signal_emit (inhibit, signals[SIGNAL_CHANGED], 0);
	return TRUE;
}

/**
 * cd_inhibit_class_init:
 **/
static void
cd_inhibit_class_init (CdInhibitClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = cd_inhibit_finalize;

	signals [SIGNAL_CHANGED] =
		g_signal_new ("changed",
			      G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (CdInhibitClass, changed),
			      NULL, NULL, g_cclosure_marshal_VOID__VOID,
			      G_TYPE_NONE, 0);
}

/**
 * cd_inhibit_init:
 **/
static void
cd_inhibit_init (CdInhibit *inhibit)
{
	CdInhibitPrivate *priv = GET_PRIVATE (inhibit);
	priv->array = g_ptr_array_new_with_free_func ((GDestroyNotify) cd_inhibit_item_free);
}

/**
 * cd_inhibit_finalize:
 **/
static void
cd_inhibit_finalize (GObject *object)
{
	CdInhibit *inhibit = CD_INHIBIT (object);
	CdInhibitPrivate *priv = GET_PRIVATE (inhibit);

	g_ptr_array_unref (priv->array);

	G_OBJECT_CLASS (cd_inhibit_parent_class)->finalize (object);
}

/**
 * cd_inhibit_new:
 **/
CdInhibit *
cd_inhibit_new (void)
{
	CdInhibit *inhibit;
	inhibit = g_object_new (CD_TYPE_INHIBIT, NULL);
	return CD_INHIBIT (inhibit);
}