File: xwrited-unique.c

package info (click to toggle)
xwrited 2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 188 kB
  • ctags: 152
  • sloc: ansic: 772; xml: 135; makefile: 131; sed: 26
file content (290 lines) | stat: -rw-r--r-- 8,104 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * Copyright (C) 2014 Guido Berhoerster <guido+xwrited@berhoerster.name>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#define	_XOPEN_SOURCE	600

#include <glib.h>
#ifdef	HAVE_GLIB_GDBUS
#include <gio/gio.h>
#else
#include <dbus/dbus-glib.h>
#endif /* HAVE_GLIB_GDBUS */
#include <dbus/dbus.h>

#include "xwrited-unique.h"

G_DEFINE_TYPE(XWritedUnique, xwrited_unique, G_TYPE_OBJECT)

#define	XWRITED_UNIQUE_GET_PRIVATE(obj)	(G_TYPE_INSTANCE_GET_PRIVATE((obj), \
    XWRITED_TYPE_UNIQUE, XWritedUniquePrivate))

struct	_XWritedUniquePrivate {
#ifdef HAVE_GLIB_GDBUS
	GDBusProxy	*session_bus_proxy;
#else
	DBusGConnection	*session_bus;
	DBusGProxy	*session_bus_proxy;
#endif /* HAVE_GLIB_GDBUS */
	gchar		*name;
	gboolean	is_unique;
};

enum {
	PROP_0,
	PROP_NAME,
	PROP_IS_XWRITED_UNIQUE
};

static gboolean
request_name(XWritedUnique *self)
{
	guint32	request_name_response;
	GError	*error = NULL;
#ifdef HAVE_GLIB_GDBUS
	GVariant *result;

	g_return_val_if_fail(self->priv->session_bus_proxy != NULL, FALSE);

	result = g_dbus_proxy_call_sync(self->priv->session_bus_proxy,
	    "RequestName", g_variant_new("(su)", self->priv->name,
	    DBUS_NAME_FLAG_DO_NOT_QUEUE), G_DBUS_CALL_FLAGS_NONE, -1, NULL,
	    &error);
	if (result == NULL) {
		g_warning("failed to acquire service name \"%s\": %s",
		    self->priv->name, error->message);
		g_error_free(error);
		return (FALSE);
	}

	g_variant_get(result, "(u)", &request_name_response);
	g_variant_unref(result);
#else

	g_return_val_if_fail(self->priv->session_bus != NULL, FALSE);
	g_return_val_if_fail(self->priv->session_bus_proxy != NULL, FALSE);
	if (dbus_g_proxy_call(self->priv->session_bus_proxy, "RequestName",
	    &error, G_TYPE_STRING, self->priv->name, G_TYPE_UINT,
	    DBUS_NAME_FLAG_DO_NOT_QUEUE, G_TYPE_INVALID, G_TYPE_UINT,
	    &request_name_response, G_TYPE_INVALID) == 0) {
		g_warning("failed to acquire service name \"%s\": %s",
		    self->priv->name, error->message);
		g_error_free(error);
		return (FALSE);
	}
#endif /* HAVE_GLIB_GDBUS */

	switch (request_name_response) {
	case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
		return (TRUE);
	case DBUS_REQUEST_NAME_REPLY_EXISTS:
		break;
	default:
		g_warning("failed to acquire service name \"%s\"",
		    self->priv->name);
	}

	return (FALSE);
}

static void
xwrited_unique_get_property(GObject *gobject, guint property_id, GValue *value,
    GParamSpec *pspec)
{
	XWritedUnique	*app = XWRITED_UNIQUE(gobject);

	switch (property_id) {
	case PROP_NAME:
		g_value_set_string(value, app->priv->name);
		break;
	case PROP_IS_XWRITED_UNIQUE:
		g_value_set_boolean(value, app->priv->is_unique);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id,
		    pspec);
	}
}

static void
xwrited_unique_set_property(GObject *gobject, guint property_id,
    const GValue *value, GParamSpec *pspec)
{
	XWritedUnique	*app = XWRITED_UNIQUE(gobject);

	switch (property_id) {
	case PROP_NAME:
		app->priv->name = g_strdup(g_value_get_string(value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id,
		    pspec);
	}
}

static GObject *
xwrited_unique_constructor(GType gtype, guint n_params,
    GObjectConstructParam *params)
{
	GObjectClass	*parent_class;
	GObject		*gobject;
	XWritedUnique	*app;
	GError		*error = NULL;

	parent_class = G_OBJECT_CLASS(xwrited_unique_parent_class);
	gobject = parent_class->constructor(gtype, n_params, params);
	app = XWRITED_UNIQUE(gobject);

#ifdef HAVE_GLIB_GDBUS
	app->priv->session_bus_proxy =
	    g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,
		G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
		G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, NULL, DBUS_SERVICE_DBUS,
		DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, NULL, &error);
	if (app->priv->session_bus_proxy == NULL) {
		g_warning("failed to create DBus proxy: %s", error->message);
		g_error_free(error);
		goto out;
	}
#else
	app->priv->session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
	if (app->priv->session_bus == NULL) {
		g_warning("failed to connect to DBus session bus: %s",
		    error->message);
		g_error_free(error);
		goto out;
	}

	app->priv->session_bus_proxy =
	    dbus_g_proxy_new_for_name(app->priv->session_bus,
	    DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
	if (app->priv->session_bus_proxy == NULL) {
		g_warning("failed to create DBus proxy");
		goto out;
	}
#endif /* HAVE_GLIB_GDBUS */

	if (request_name(app)) {
		app->priv->is_unique = TRUE;
	}

out:
	return (gobject);
}

static void
xwrited_unique_dispose(GObject *gobject)
{
	XWritedUnique	*self = XWRITED_UNIQUE(gobject);

	if (self->priv->session_bus_proxy != NULL) {
		g_object_unref(self->priv->session_bus_proxy);
		self->priv->session_bus_proxy = NULL;
	}

#ifndef HAVE_GLIB_GDBUS
	if (self->priv->session_bus != NULL) {
		dbus_g_connection_unref(self->priv->session_bus);
		self->priv->session_bus = NULL;
	}

#endif /* !HAVE_GLIB_GDBUS */
	G_OBJECT_CLASS(xwrited_unique_parent_class)->dispose(gobject);
}

static void
xwrited_unique_finalize(GObject *gobject)
{
	XWritedUnique	*self = XWRITED_UNIQUE(gobject);

	g_free(self->priv->name);

	G_OBJECT_CLASS(xwrited_unique_parent_class)->finalize(gobject);
}

static void
xwrited_unique_class_init(XWritedUniqueClass *klass)
{
	GObjectClass	*gobject_class = G_OBJECT_CLASS(klass);
	GParamSpec	*pspec;

	gobject_class->constructor = xwrited_unique_constructor;
	gobject_class->get_property = xwrited_unique_get_property;
	gobject_class->set_property = xwrited_unique_set_property;
	gobject_class->dispose = xwrited_unique_dispose;
	gobject_class->finalize = xwrited_unique_finalize;

	pspec = g_param_spec_string("name", "Name",
	    "The unique name of the application", NULL, G_PARAM_READABLE |
	    G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME |
	    G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB);
	g_object_class_install_property(gobject_class, PROP_NAME, pspec);

	pspec = g_param_spec_boolean("is-unique", "Is unique",
	    "Whether the current application instance is unique", FALSE,
	    G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK |
	    G_PARAM_STATIC_BLURB);
	g_object_class_install_property(gobject_class, PROP_IS_XWRITED_UNIQUE,
	    pspec);

	g_type_class_add_private(klass, sizeof (XWritedUniquePrivate));
}

static void
xwrited_unique_init(XWritedUnique *self)
{
	self->priv = XWRITED_UNIQUE_GET_PRIVATE(self);

	self->priv->is_unique = FALSE;
#ifndef HAVE_GLIB_GDBUS
	self->priv->session_bus = NULL;
#endif /* !HAVE_GLIB_GDBUS */
	self->priv->session_bus_proxy = NULL;
}

XWritedUnique *
xwrited_unique_new(const gchar *name)
{
	XWritedUnique	*app;

	g_return_val_if_fail(name != NULL, NULL);

	app = g_object_new(XWRITED_TYPE_UNIQUE, "name", name, NULL);
	if (
#ifndef HAVE_GLIB_GDBUS
	    app->priv->session_bus == NULL ||
#endif /* !HAVE_GLIB_GDBUS */
	    app->priv->session_bus_proxy == NULL) {
		g_object_unref(app);
		return (NULL);
	}

	return (app);
}

gboolean
xwrited_unique_is_unique(XWritedUnique *self)
{
	g_return_val_if_fail(XWRITED_IS_UNIQUE(self), FALSE);

	return (self->priv->is_unique);
}