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/>.
21 #include "gc-client-monitor.h"
26 GDBusConnection
*connection
;
27 GDBusProxy
*dbus_proxy
;
30 } GcClientMonitorPrivate
;
32 static void gc_client_monitor_async_initable_iface_init (GAsyncInitableIface
*iface
);
34 G_DEFINE_TYPE_WITH_CODE (GcClientMonitor
,
37 G_ADD_PRIVATE (GcClientMonitor
)
38 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE
, gc_client_monitor_async_initable_iface_init
))
54 static GParamSpec
*properties
[N_PROPERTIES
] = { NULL
, };
55 static guint signals
[N_SIGNALS
];
58 on_name_vanished (GDBusConnection
*connection
,
62 g_signal_emit (GC_CLIENT_MONITOR (user_data
), signals
[PEER_VANISHED
], 0);
66 on_get_user_id_ready (GObject
*source_object
,
71 GcClientMonitor
*self
;
72 GcClientMonitorPrivate
*priv
;
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
),
85 g_task_return_error (task
, error
);
86 g_object_unref (task
);
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
,
97 G_BUS_NAME_WATCHER_FLAGS_NONE
,
103 g_task_return_boolean (task
, TRUE
);
105 g_object_unref (task
);
109 on_dbus_proxy_ready (GObject
*source_object
,
110 GAsyncResult
*result
,
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
);
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
,
142 gc_client_monitor_finalize (GObject
*object
)
144 GcClientMonitorPrivate
*priv
;
146 priv
= gc_client_monitor_get_instance_private (GC_CLIENT_MONITOR (object
));
150 G_OBJECT_CLASS (gc_client_monitor_parent_class
)->finalize (object
);
154 gc_client_monitor_get_property (GObject
*object
,
159 GcClientMonitorPrivate
*priv
;
161 priv
= gc_client_monitor_get_instance_private (GC_CLIENT_MONITOR (object
));
165 case PROP_CONNECTION
:
166 g_value_set_object (value
, priv
->connection
);
169 g_value_set_string (value
, priv
->peer
);
172 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
177 gc_client_monitor_set_property (GObject
*object
,
182 GcClientMonitorPrivate
*priv
;
184 priv
= gc_client_monitor_get_instance_private (GC_CLIENT_MONITOR (object
));
188 case PROP_CONNECTION
:
189 priv
->connection
= g_value_dup_object (value
);
193 priv
->peer
= g_value_dup_string (value
);
196 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
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",
214 G_TYPE_DBUS_CONNECTION
,
216 G_PARAM_CONSTRUCT_ONLY
|
217 G_PARAM_STATIC_STRINGS
);
219 properties
[PROP_PEER
] = g_param_spec_string ("peer",
221 "D-Bus object path of peer to monitor",
224 G_PARAM_CONSTRUCT_ONLY
|
225 G_PARAM_STATIC_STRINGS
);
227 g_object_class_install_properties (object_class
, N_PROPERTIES
, properties
);
231 gc_client_monitor_init (GcClientMonitor
*self
)
236 gc_client_monitor_async_initable_init_async (GAsyncInitable
*initable
,
238 GCancellable
*cancellable
,
239 GAsyncReadyCallback callback
,
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
,
249 "org.freedesktop.DBus",
250 "/org/freedesktop/DBus",
251 "org.freedesktop.DBus",
258 gc_client_monitor_async_initable_init_finish (GAsyncInitable
*initable
,
259 GAsyncResult
*result
,
262 return g_task_propagate_boolean (G_TASK (result
), error
);
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
;
273 gc_client_monitor_new_async (const gchar
*peer
,
274 GDBusConnection
*connection
,
275 GCancellable
*cancellable
,
276 GAsyncReadyCallback callback
,
279 g_async_initable_new_async (GC_TYPE_CLIENT_MONITOR
,
292 gc_client_monitor_new_finish (GAsyncResult
*result
, GError
**error
)
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
),
301 g_object_unref (source_object
);
305 return GC_CLIENT_MONITOR (object
);