Update Hungarian translation
[cheese.git] / service / gc-client-monitor.c
blob61ce70ae8177786c62905ce2a622543253deb716
1 /*
2 * GNOME Camera - Access camera devices on a system via D-Bus
3 * Copyright (C) 2014 Red Hat, Inc.
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 3 of the License, or
8 * (at your option) any later version.
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.
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/>.
19 #include "config.h"
21 #include "gc-client-monitor.h"
23 typedef struct
25 gchar *peer;
26 GDBusConnection *connection;
27 GDBusProxy *dbus_proxy;
28 guint watch_id;
29 guint32 user_id;
30 } GcClientMonitorPrivate;
32 static void gc_client_monitor_async_initable_iface_init (GAsyncInitableIface *iface);
34 G_DEFINE_TYPE_WITH_CODE (GcClientMonitor,
35 gc_client_monitor,
36 G_TYPE_OBJECT,
37 G_ADD_PRIVATE (GcClientMonitor)
38 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, gc_client_monitor_async_initable_iface_init))
40 enum
42 PROP_0,
43 PROP_CONNECTION,
44 PROP_PEER,
45 N_PROPERTIES
48 enum
50 PEER_VANISHED,
51 N_SIGNALS
54 static GParamSpec *properties[N_PROPERTIES] = { NULL, };
55 static guint signals[N_SIGNALS];
57 static void
58 on_name_vanished (GDBusConnection *connection,
59 const gchar *name,
60 gpointer user_data)
62 g_signal_emit (GC_CLIENT_MONITOR (user_data), signals[PEER_VANISHED], 0);
65 static void
66 on_get_user_id_ready (GObject *source_object,
67 GAsyncResult *result,
68 gpointer user_data)
70 GTask *task;
71 GcClientMonitor *self;
72 GcClientMonitorPrivate *priv;
73 GError *error = NULL;
74 GVariant *results = NULL;
76 task = G_TASK (user_data);
77 self = GC_CLIENT_MONITOR (g_task_get_source_object (task));
78 priv = gc_client_monitor_get_instance_private (self);
80 results = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object),
81 result,
82 &error);
83 if (results == NULL)
85 g_task_return_error (task, error);
86 g_object_unref (task);
88 return;
91 g_assert (g_variant_n_children (results) > 0);
92 g_variant_get_child (results, 0, "u", &priv->user_id);
93 g_variant_unref (results);
95 priv->watch_id = g_bus_watch_name_on_connection (priv->connection,
96 priv->peer,
97 G_BUS_NAME_WATCHER_FLAGS_NONE,
98 NULL,
99 on_name_vanished,
100 self,
101 NULL);
103 g_task_return_boolean (task, TRUE);
105 g_object_unref (task);
108 static void
109 on_dbus_proxy_ready (GObject *source_object,
110 GAsyncResult *result,
111 gpointer user_data)
113 GTask *task;
114 GcClientMonitor *self;
115 GcClientMonitorPrivate *priv;
116 GError *error = NULL;
118 task = G_TASK (user_data);
119 self = GC_CLIENT_MONITOR (g_task_get_source_object (task));
120 priv = gc_client_monitor_get_instance_private (self);
122 priv->dbus_proxy = g_dbus_proxy_new_for_bus_finish (result, &error);
124 if (priv->dbus_proxy == NULL)
126 g_task_return_error (task, error);
127 g_object_unref (task);
128 return;
131 g_dbus_proxy_call (priv->dbus_proxy,
132 "GetConnectionUnixUser",
133 g_variant_new ("(s)", priv->peer),
134 G_DBUS_CALL_FLAGS_NONE,
136 g_task_get_cancellable (task),
137 on_get_user_id_ready,
138 task);
141 static void
142 gc_client_monitor_finalize (GObject *object)
144 GcClientMonitorPrivate *priv;
146 priv = gc_client_monitor_get_instance_private (GC_CLIENT_MONITOR (object));
148 g_free (priv->peer);
150 G_OBJECT_CLASS (gc_client_monitor_parent_class)->finalize (object);
153 static void
154 gc_client_monitor_get_property (GObject *object,
155 guint prop_id,
156 GValue *value,
157 GParamSpec *pspec)
159 GcClientMonitorPrivate *priv;
161 priv = gc_client_monitor_get_instance_private (GC_CLIENT_MONITOR (object));
163 switch (prop_id)
165 case PROP_CONNECTION:
166 g_value_set_object (value, priv->connection);
167 break;
168 case PROP_PEER:
169 g_value_set_string (value, priv->peer);
170 break;
171 default:
172 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176 static void
177 gc_client_monitor_set_property (GObject *object,
178 guint prop_id,
179 const GValue *value,
180 GParamSpec *pspec)
182 GcClientMonitorPrivate *priv;
184 priv = gc_client_monitor_get_instance_private (GC_CLIENT_MONITOR (object));
186 switch (prop_id)
188 case PROP_CONNECTION:
189 priv->connection = g_value_dup_object (value);
190 break;
191 case PROP_PEER:
192 g_free (priv->peer);
193 priv->peer = g_value_dup_string (value);
194 break;
195 default:
196 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200 static void
201 gc_client_monitor_class_init (GcClientMonitorClass *klass)
203 GObjectClass *object_class;
205 object_class = G_OBJECT_CLASS (klass);
207 object_class->finalize = gc_client_monitor_finalize;
208 object_class->get_property = gc_client_monitor_get_property;
209 object_class->set_property = gc_client_monitor_set_property;
211 properties[PROP_CONNECTION] = g_param_spec_object ("connection",
212 "Connection",
213 "DBus Connection",
214 G_TYPE_DBUS_CONNECTION,
215 G_PARAM_READWRITE |
216 G_PARAM_CONSTRUCT_ONLY |
217 G_PARAM_STATIC_STRINGS);
219 properties[PROP_PEER] = g_param_spec_string ("peer",
220 "Peer",
221 "D-Bus object path of peer to monitor",
222 NULL,
223 G_PARAM_READWRITE |
224 G_PARAM_CONSTRUCT_ONLY |
225 G_PARAM_STATIC_STRINGS);
227 g_object_class_install_properties (object_class, N_PROPERTIES, properties);
230 static void
231 gc_client_monitor_init (GcClientMonitor *self)
235 static void
236 gc_client_monitor_async_initable_init_async (GAsyncInitable *initable,
237 gint io_priority,
238 GCancellable *cancellable,
239 GAsyncReadyCallback callback,
240 gpointer user_data)
242 GTask *task;
244 task = g_task_new (initable, cancellable, callback, user_data);
246 g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION,
247 G_DBUS_PROXY_FLAGS_NONE,
248 NULL,
249 "org.freedesktop.DBus",
250 "/org/freedesktop/DBus",
251 "org.freedesktop.DBus",
252 cancellable,
253 on_dbus_proxy_ready,
254 task);
257 static gboolean
258 gc_client_monitor_async_initable_init_finish (GAsyncInitable *initable,
259 GAsyncResult *result,
260 GError **error)
262 return g_task_propagate_boolean (G_TASK (result), error);
265 static void
266 gc_client_monitor_async_initable_iface_init (GAsyncInitableIface *iface)
268 iface->init_async = gc_client_monitor_async_initable_init_async;
269 iface->init_finish = gc_client_monitor_async_initable_init_finish;
272 void
273 gc_client_monitor_new_async (const gchar *peer,
274 GDBusConnection *connection,
275 GCancellable *cancellable,
276 GAsyncReadyCallback callback,
277 gpointer user_data)
279 g_async_initable_new_async (GC_TYPE_CLIENT_MONITOR,
280 G_PRIORITY_DEFAULT,
281 cancellable,
282 callback,
283 user_data,
284 "connection",
285 connection,
286 "peer",
287 peer,
288 NULL);
291 GcClientMonitor *
292 gc_client_monitor_new_finish (GAsyncResult *result, GError **error)
294 GObject *object;
295 GObject *source_object;
297 source_object = g_async_result_get_source_object (result);
298 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
299 result,
300 error);
301 g_object_unref (source_object);
303 if (object != NULL)
305 return GC_CLIENT_MONITOR (object);
307 else
309 return NULL;