Branch data Line data Source code
1 : : // SPDX-License-Identifier: GPL-3.0-or-later
2 : : // SPDX-FileCopyrightText: Andy Holmes <andrew.g.r.holmes@gmail.com>
3 : :
4 : : #define G_LOG_DOMAIN "valent-channel-service"
5 : :
6 : : #include "config.h"
7 : :
8 : : #include <gio/gio.h>
9 : : #include <json-glib/json-glib.h>
10 : : #include <libpeas.h>
11 : : #include <libvalent-core.h>
12 : :
13 : : #include "valent-certificate.h"
14 : : #include "valent-channel.h"
15 : : #include "valent-device-common.h"
16 : : #include "valent-packet.h"
17 : :
18 : : #include "valent-channel-service.h"
19 : :
20 : : /**
21 : : * ValentChannelService:
22 : : *
23 : : * An abstract base class for connection backends.
24 : : *
25 : : * `ValentChannelService` is a base class for plugins that implement an interface
26 : : * to negotiate connections with other devices.
27 : : *
28 : : * ## Implementation Notes
29 : : *
30 : : * Implementations may safely invoke [method@Valent.ChannelService.channel] from
31 : : * any thread; it is guaranteed to be emitted in the main thread.
32 : : *
33 : : * ## `.plugin` File
34 : : *
35 : : * Channel services have no special fields in the `.plugin` file.
36 : : *
37 : : * Since: 1.0
38 : : */
39 : :
40 : : typedef struct
41 : : {
42 : : GTlsCertificate *certificate;
43 : : const char *id;
44 : : JsonNode *identity;
45 : : char *name;
46 : : GSettings *settings;
47 : : } ValentChannelServicePrivate;
48 : :
49 [ + + + - ]: 736 : G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ValentChannelService, valent_channel_service, VALENT_TYPE_EXTENSION);
50 : :
51 : : typedef enum {
52 : : PROP_CERTIFICATE = 1,
53 : : PROP_ID,
54 : : PROP_IDENTITY,
55 : : } ValentChannelServiceProperty;
56 : :
57 : : static GParamSpec *properties[PROP_IDENTITY + 1] = { NULL, };
58 : :
59 : : typedef enum {
60 : : CHANNEL,
61 : : } ValentChannelServiceSignal;
62 : :
63 : : static guint signals[CHANNEL + 1] = { 0, };
64 : :
65 : :
66 : : typedef struct
67 : : {
68 : : GRecMutex lock;
69 : : GWeakRef service;
70 : : ValentChannel *channel;
71 : : } ChannelEmission;
72 : :
73 : : static gboolean
74 : 3 : valent_channel_service_channel_main (gpointer data)
75 : : {
76 : 3 : ChannelEmission *emission = data;
77 : 6 : g_autoptr (ValentChannelService) service = NULL;
78 : :
79 [ + - ]: 3 : g_assert (VALENT_IS_MAIN_THREAD ());
80 : :
81 : 3 : g_rec_mutex_lock (&emission->lock);
82 [ + - ]: 3 : if ((service = g_weak_ref_get (&emission->service)) != NULL)
83 : 3 : valent_channel_service_channel (service, emission->channel);
84 : :
85 : 3 : g_weak_ref_clear (&emission->service);
86 [ + - ]: 3 : g_clear_object (&emission->channel);
87 : 3 : g_rec_mutex_unlock (&emission->lock);
88 : 3 : g_rec_mutex_clear (&emission->lock);
89 : 3 : g_clear_pointer (&emission, g_free);
90 : :
91 [ + - ]: 3 : return G_SOURCE_REMOVE;
92 : : }
93 : :
94 : : /*
95 : : * Identity Packet Helpers
96 : : */
97 : : static const char *
98 : 23 : get_chassis_type (void)
99 : : {
100 : 23 : static size_t guard = 0;
101 : 23 : static char *chassis = NULL;
102 : :
103 [ + + + - ]: 23 : if (g_once_init_enter (&guard))
104 : : {
105 : 6 : g_autoptr (GDBusConnection) connection = NULL;
106 [ + + ]: 6 : g_autoptr (GVariant) reply = NULL;
107 [ - + ]: 6 : g_autofree char *str = NULL;
108 : 6 : uint64_t type;
109 : :
110 : 6 : connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL);
111 : :
112 [ + + ]: 6 : if (connection == NULL)
113 : 5 : VALENT_GOTO (dmi_fallback);
114 : :
115 : 1 : reply = g_dbus_connection_call_sync (connection,
116 : : "org.freedesktop.hostname1",
117 : : "/org/freedesktop/hostname1",
118 : : "org.freedesktop.DBus.Properties",
119 : : "Get",
120 : : g_variant_new ("(ss)",
121 : : "org.freedesktop.hostname1",
122 : : "Chassis"),
123 : : G_VARIANT_TYPE ("(v)"),
124 : : G_DBUS_CALL_FLAGS_NONE,
125 : : -1,
126 : : NULL,
127 : : NULL);
128 : :
129 [ - + ]: 1 : if (reply != NULL)
130 : : {
131 : 0 : g_autoptr (GVariant) value = NULL;
132 : :
133 : 0 : g_variant_get (reply, "(v)", &value);
134 : 0 : g_variant_get (value, "s", &chassis);
135 : :
136 : : /* NOTE: "phone" is the KDE Connect deviceType */
137 [ # # ]: 0 : if (g_str_equal (chassis, "handset"))
138 : 0 : g_set_str (&chassis, "phone");
139 : :
140 [ # # ]: 0 : VALENT_GOTO (leave);
141 : : }
142 : :
143 : : /* Fallback to DMI. See the SMBIOS Specification 3.0 section 7.4.1:
144 : : * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.0.0.pdf
145 : : */
146 : 1 : dmi_fallback:
147 [ + - + - ]: 12 : if (!g_file_get_contents ("/sys/class/dmi/id/chassis_type", &str, NULL, NULL) ||
148 : 6 : !g_ascii_string_to_unsigned (str, 10, 0, G_MAXUINT64, &type, NULL))
149 : 6 : type = 0x3;
150 : :
151 [ + - - - : 6 : switch (type)
- ]
152 : : {
153 : 6 : case 0x3: /* Desktop */
154 : : case 0x4: /* Low Profile Desktop */
155 : : case 0x6: /* Mini Tower */
156 : : case 0x7: /* Tower */
157 : 6 : g_set_str (&chassis, "desktop");
158 : 6 : break;
159 : :
160 : 0 : case 0x8: /* Portable */
161 : : case 0x9: /* Laptop */
162 : : case 0xA: /* Notebook */
163 : : case 0xE: /* Sub Notebook */
164 : 0 : g_set_str (&chassis, "laptop");
165 : 0 : break;
166 : :
167 : 0 : case 0xB: /* Hand Held */
168 : 0 : g_set_str (&chassis, "phone");
169 : 0 : break;
170 : :
171 : 0 : case 0x1E: /* Tablet */
172 : 0 : g_set_str (&chassis, "tablet");
173 : 0 : break;
174 : :
175 : 0 : default:
176 : 0 : g_set_str (&chassis, "desktop");
177 : : }
178 : :
179 : 6 : leave:
180 : 6 : g_once_init_leave (&guard, 1);
181 : : }
182 : :
183 : 23 : return chassis;
184 : : }
185 : :
186 : : /**
187 : : * collect_capabilities:
188 : : * @info: a `PeasPluginInfo`
189 : : * @incoming: a `GHashTable`
190 : : * @outgoing: a `GHashTable`
191 : : *
192 : : * Collect the capabilities from @info and add them to @incoming and @outgoing,
193 : : * using g_hash_table_add() to coalesce duplicates.
194 : : */
195 : : static inline void
196 : 125 : collect_capabilities (PeasPluginInfo *info,
197 : : GHashTable *incoming,
198 : : GHashTable *outgoing)
199 : : {
200 : 125 : const char *data = NULL;
201 : :
202 [ + + ]: 125 : if ((data = peas_plugin_info_get_external_data (info, "DevicePluginIncoming")) != NULL)
203 : : {
204 : 68 : g_autofree char **capabilities = NULL;
205 : :
206 : 68 : capabilities = g_strsplit (data, ";", -1);
207 : :
208 [ + + ]: 199 : for (unsigned int i = 0; capabilities[i] != NULL; i++)
209 : 131 : g_hash_table_add (incoming, g_steal_pointer (&capabilities[i]));
210 : : }
211 : :
212 [ + + ]: 125 : if ((data = peas_plugin_info_get_external_data (info, "DevicePluginOutgoing")) != NULL)
213 : : {
214 : 68 : g_autofree char **capabilities = NULL;
215 : :
216 : 68 : capabilities = g_strsplit (data, ";", -1);
217 : :
218 [ + + ]: 209 : for (unsigned int i = 0; capabilities[i] != NULL; i++)
219 : 141 : g_hash_table_add (outgoing, g_steal_pointer (&capabilities[i]));
220 : : }
221 : 125 : }
222 : :
223 : : static void
224 : 26 : on_device_name_changed (GSettings *settings,
225 : : const char *key_name,
226 : : ValentChannelService *self)
227 : : {
228 : 26 : ValentChannelServicePrivate *priv = valent_channel_service_get_instance_private (self);
229 : 26 : g_autofree char *name = NULL;
230 : :
231 [ + - ]: 26 : g_return_if_fail (VALENT_IS_CHANNEL_SERVICE (self));
232 : :
233 : 26 : name = g_settings_get_string (settings, "name");
234 [ + - + + ]: 26 : if (name == NULL || *name == '\0')
235 : : {
236 : 6 : g_settings_set_string (settings, "name", g_get_host_name ());
237 : 6 : return;
238 : : }
239 : :
240 [ + - ]: 20 : if (g_set_str (&priv->name, name))
241 : : {
242 : 20 : valent_object_lock (VALENT_OBJECT (self));
243 [ - + ]: 20 : if (priv->identity)
244 : : {
245 : 0 : JsonObject *body = valent_packet_get_body (priv->identity);
246 : 0 : json_object_set_string_member (body, "deviceName", priv->name);
247 : 0 : g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_IDENTITY]);
248 : : }
249 : 20 : valent_object_unlock (VALENT_OBJECT (self));
250 : : }
251 : : }
252 : :
253 : : /* LCOV_EXCL_START */
254 : : static void
255 : : valent_channel_service_real_build_identity (ValentChannelService *service)
256 : : {
257 : : ValentChannelServicePrivate *priv = valent_channel_service_get_instance_private (service);
258 : : PeasEngine *engine;
259 : : g_autoptr (JsonBuilder) builder = NULL;
260 : : g_autoptr (GHashTable) incoming = NULL;
261 : : g_autoptr (GHashTable) outgoing = NULL;
262 : : GHashTableIter in_iter, out_iter;
263 : : const char *capability = NULL;
264 : : unsigned int n_plugins = 0;
265 : :
266 : : g_assert (VALENT_IS_CHANNEL_SERVICE (service));
267 : :
268 : : /* Filter the supported plugins and collect their capabilities */
269 : : incoming = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
270 : : outgoing = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
271 : :
272 : : engine = valent_get_plugin_engine ();
273 : : n_plugins = g_list_model_get_n_items (G_LIST_MODEL (engine));
274 : :
275 : : for (unsigned int i = 0; i < n_plugins; i++)
276 : : {
277 : : g_autoptr (PeasPluginInfo) info = NULL;
278 : :
279 : : info = g_list_model_get_item (G_LIST_MODEL (engine), i);
280 : : collect_capabilities (info, incoming, outgoing);
281 : : }
282 : :
283 : : /* Build the identity packet */
284 : : builder = json_builder_new ();
285 : : json_builder_begin_object (builder);
286 : :
287 : : /* Packet */
288 : : json_builder_set_member_name (builder, "id");
289 : : json_builder_add_int_value (builder, 0);
290 : : json_builder_set_member_name (builder, "type");
291 : : json_builder_add_string_value (builder, "kdeconnect.identity");
292 : :
293 : : /* Body */
294 : : json_builder_set_member_name (builder, "body");
295 : : json_builder_begin_object (builder);
296 : :
297 : : /* Metadata */
298 : : json_builder_set_member_name (builder, "deviceId");
299 : : json_builder_add_string_value (builder, priv->id);
300 : : json_builder_set_member_name (builder, "deviceName");
301 : : json_builder_add_string_value (builder, priv->name);
302 : : json_builder_set_member_name (builder, "deviceType");
303 : : json_builder_add_string_value (builder, get_chassis_type());
304 : : json_builder_set_member_name (builder, "protocolVersion");
305 : : json_builder_add_int_value (builder, VALENT_NETWORK_PROTOCOL_MAX);
306 : :
307 : : /* Incoming Capabilities */
308 : : json_builder_set_member_name (builder, "incomingCapabilities");
309 : : json_builder_begin_array (builder);
310 : :
311 : : g_hash_table_iter_init (&in_iter, incoming);
312 : :
313 : : while (g_hash_table_iter_next (&in_iter, (void **)&capability, NULL))
314 : : json_builder_add_string_value (builder, capability);
315 : :
316 : : json_builder_end_array (builder);
317 : :
318 : : /* Outgoing Capabilities */
319 : : json_builder_set_member_name (builder, "outgoingCapabilities");
320 : : json_builder_begin_array (builder);
321 : :
322 : : g_hash_table_iter_init (&out_iter, outgoing);
323 : :
324 : : while (g_hash_table_iter_next (&out_iter, (void **)&capability, NULL))
325 : : json_builder_add_string_value (builder, capability);
326 : :
327 : : json_builder_end_array (builder);
328 : :
329 : : /* End Body, Packet */
330 : : json_builder_end_object (builder);
331 : : json_builder_end_object (builder);
332 : :
333 : :
334 : : /* Store the identity */
335 : : valent_object_lock (VALENT_OBJECT (service));
336 : : g_clear_pointer (&priv->identity, json_node_unref);
337 : : priv->identity = json_builder_get_root (builder);
338 : : valent_object_unlock (VALENT_OBJECT (service));
339 : : }
340 : :
341 : : static void
342 : : valent_channel_service_real_identify (ValentChannelService *service,
343 : : const char *target)
344 : : {
345 : : g_assert (VALENT_IS_CHANNEL_SERVICE (service));
346 : : }
347 : : /* LCOV_EXCL_STOP */
348 : :
349 : : /*
350 : : * GObject
351 : : */
352 : : static void
353 : 20 : valent_channel_service_constructed (GObject *object)
354 : : {
355 : 20 : ValentChannelService *self = VALENT_CHANNEL_SERVICE (object);
356 : 20 : ValentChannelServicePrivate *priv = valent_channel_service_get_instance_private (self);
357 : :
358 : 20 : G_OBJECT_CLASS (valent_channel_service_parent_class)->constructed (object);
359 : :
360 [ + - ]: 20 : if (priv->certificate == NULL)
361 : : {
362 : 40 : g_autoptr (ValentContext) context = NULL;
363 [ + - ]: 20 : g_autoptr (GFile) config = NULL;
364 : :
365 : 20 : context = valent_context_new (NULL, NULL, NULL);
366 : 20 : config = valent_context_get_config_file (context, ".");
367 [ + - ]: 20 : priv->certificate = valent_certificate_new_sync (g_file_peek_path (config),
368 : : NULL);
369 : : }
370 : :
371 : 20 : priv->id = valent_certificate_get_common_name (priv->certificate);
372 : 20 : priv->settings = g_settings_new ("ca.andyholmes.Valent");
373 : 20 : g_signal_connect_object (priv->settings,
374 : : "changed::name",
375 : : G_CALLBACK (on_device_name_changed),
376 : : self,
377 : : G_CONNECT_DEFAULT);
378 : 20 : on_device_name_changed (priv->settings, NULL, self);
379 : :
380 : 20 : valent_channel_service_build_identity (self);
381 : 20 : }
382 : :
383 : : static void
384 : 20 : valent_channel_service_finalize (GObject *object)
385 : : {
386 : 20 : ValentChannelService *self = VALENT_CHANNEL_SERVICE (object);
387 : 20 : ValentChannelServicePrivate *priv = valent_channel_service_get_instance_private (self);
388 : :
389 [ + - ]: 20 : g_clear_object (&priv->certificate);
390 [ + - ]: 20 : g_clear_pointer (&priv->identity, json_node_unref);
391 [ + - ]: 20 : g_clear_pointer (&priv->name, g_free);
392 [ + - ]: 20 : g_clear_object (&priv->settings);
393 : :
394 : 20 : G_OBJECT_CLASS (valent_channel_service_parent_class)->finalize (object);
395 : 20 : }
396 : :
397 : : static void
398 : 5 : valent_channel_service_get_property (GObject *object,
399 : : guint prop_id,
400 : : GValue *value,
401 : : GParamSpec *pspec)
402 : : {
403 : 5 : ValentChannelService *self = VALENT_CHANNEL_SERVICE (object);
404 : 5 : ValentChannelServicePrivate *priv = valent_channel_service_get_instance_private (self);
405 : :
406 [ - + + - ]: 5 : switch ((ValentChannelServiceProperty)prop_id)
407 : : {
408 : 0 : case PROP_CERTIFICATE:
409 : 0 : g_value_take_object (value, valent_channel_service_ref_certificate (self));
410 : 0 : break;
411 : :
412 : 1 : case PROP_ID:
413 : 1 : g_value_set_string (value, priv->id);
414 : 1 : break;
415 : :
416 : 4 : case PROP_IDENTITY:
417 : 4 : g_value_take_boxed (value, valent_channel_service_ref_identity (self));
418 : 4 : break;
419 : :
420 : 0 : default:
421 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
422 : : }
423 : 5 : }
424 : :
425 : : static void
426 : 20 : valent_channel_service_set_property (GObject *object,
427 : : guint prop_id,
428 : : const GValue *value,
429 : : GParamSpec *pspec)
430 : : {
431 : 20 : ValentChannelService *self = VALENT_CHANNEL_SERVICE (object);
432 : 20 : ValentChannelServicePrivate *priv = valent_channel_service_get_instance_private (self);
433 : :
434 [ + - - ]: 20 : switch ((ValentChannelServiceProperty)prop_id)
435 : : {
436 : 20 : case PROP_CERTIFICATE:
437 : 20 : priv->certificate = g_value_dup_object (value);
438 : 20 : break;
439 : :
440 : 0 : case PROP_ID:
441 : : case PROP_IDENTITY:
442 : 0 : g_assert_not_reached ();
443 : :
444 : 0 : default:
445 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
446 : : }
447 : 20 : }
448 : :
449 : : static void
450 : 58 : valent_channel_service_class_init (ValentChannelServiceClass *klass)
451 : : {
452 : 58 : GObjectClass *object_class = G_OBJECT_CLASS (klass);
453 : 58 : ValentChannelServiceClass *service_class = VALENT_CHANNEL_SERVICE_CLASS (klass);
454 : :
455 : 58 : object_class->constructed = valent_channel_service_constructed;
456 : 58 : object_class->finalize = valent_channel_service_finalize;
457 : 58 : object_class->get_property = valent_channel_service_get_property;
458 : 58 : object_class->set_property = valent_channel_service_set_property;
459 : :
460 : 58 : service_class->build_identity = valent_channel_service_real_build_identity;
461 : 58 : service_class->identify = valent_channel_service_real_identify;
462 : :
463 : : /**
464 : : * ValentChannelService:certificate: (getter ref_certificate)
465 : : *
466 : : * The TLS certificate the service uses to authenticate with other devices.
467 : : */
468 : 116 : properties [PROP_CERTIFICATE] =
469 : 58 : g_param_spec_object ("certificate", NULL, NULL,
470 : : G_TYPE_TLS_CERTIFICATE,
471 : : (G_PARAM_READWRITE |
472 : : G_PARAM_CONSTRUCT_ONLY |
473 : : G_PARAM_EXPLICIT_NOTIFY |
474 : : G_PARAM_STATIC_STRINGS));
475 : :
476 : : /**
477 : : * ValentChannelService:id: (getter dup_id)
478 : : *
479 : : * The local ID.
480 : : *
481 : : * This is the ID used to identify the local device, which should be unique
482 : : * among devices in a given network.
483 : : *
484 : : * This property is thread-safe. Emissions of [signal@GObject.Object::notify]
485 : : * are guaranteed to happen in the main thread.
486 : : *
487 : : * Since: 1.0
488 : : */
489 : 116 : properties [PROP_ID] =
490 : 58 : g_param_spec_string ("id", NULL, NULL,
491 : : NULL,
492 : : (G_PARAM_READABLE |
493 : : G_PARAM_EXPLICIT_NOTIFY |
494 : : G_PARAM_STATIC_STRINGS));
495 : :
496 : : /**
497 : : * ValentChannelService:identity: (getter ref_identity)
498 : : *
499 : : * The local identity packet.
500 : : *
501 : : * This is the identity packet sent by the [class@Valent.ChannelService]
502 : : * implementation to describe the local device.
503 : : *
504 : : * This property is thread-safe. Emissions of [signal@GObject.Object::notify]
505 : : * are guaranteed to happen in the main thread.
506 : : *
507 : : * Since: 1.0
508 : : */
509 : 116 : properties [PROP_IDENTITY] =
510 : 58 : g_param_spec_boxed ("identity", NULL, NULL,
511 : : JSON_TYPE_NODE,
512 : : (G_PARAM_READABLE |
513 : : G_PARAM_EXPLICIT_NOTIFY |
514 : : G_PARAM_STATIC_STRINGS));
515 : :
516 : 58 : g_object_class_install_properties (object_class, G_N_ELEMENTS (properties), properties);
517 : :
518 : : /**
519 : : * ValentChannelService::channel:
520 : : * @service: a `ValentChannelService`
521 : : * @channel: a `ValentChannel`
522 : : *
523 : : * Emitted when a new channel has been negotiated.
524 : : *
525 : : * In practice, when this is emitted a [class@Valent.DeviceManager] will
526 : : * ensure a [class@Valent.Device] exists to take ownership of @channel.
527 : : *
528 : : * Since: 1.0
529 : : */
530 : 116 : signals [CHANNEL] =
531 : 58 : g_signal_new ("channel",
532 : : G_TYPE_FROM_CLASS (klass),
533 : : G_SIGNAL_RUN_LAST,
534 : : G_STRUCT_OFFSET (ValentChannelServiceClass, channel),
535 : : NULL, NULL,
536 : : g_cclosure_marshal_VOID__OBJECT,
537 : : G_TYPE_NONE, 1, VALENT_TYPE_CHANNEL);
538 : 58 : g_signal_set_va_marshaller (signals [CHANNEL],
539 : : G_TYPE_FROM_CLASS (klass),
540 : : g_cclosure_marshal_VOID__OBJECTv);
541 : 58 : }
542 : :
543 : : static void
544 : 20 : valent_channel_service_init (ValentChannelService *self)
545 : : {
546 : 20 : }
547 : :
548 : : /**
549 : : * valent_channel_service_ref_certificate: (get-property certificate)
550 : : * @self: a `ValentChannelService`
551 : : *
552 : : * Get the TLS certificate for the service.
553 : : *
554 : : * Returns: (transfer full) (not nullable): the service TLS certificate
555 : : */
556 : : GTlsCertificate *
557 : 8 : valent_channel_service_ref_certificate (ValentChannelService *service)
558 : : {
559 : 8 : ValentChannelServicePrivate *priv = valent_channel_service_get_instance_private (service);
560 : 8 : GTlsCertificate *ret = NULL;
561 : :
562 [ + - ]: 8 : g_return_val_if_fail (VALENT_IS_CHANNEL_SERVICE (service), NULL);
563 : :
564 : 8 : valent_object_lock (VALENT_OBJECT (service));
565 : 8 : ret = g_object_ref (priv->certificate);
566 : 8 : valent_object_unlock (VALENT_OBJECT (service));
567 : :
568 : 8 : return g_steal_pointer (&ret);
569 : : }
570 : :
571 : : /**
572 : : * valent_channel_service_dup_id: (get-property id)
573 : : * @service: a `ValentChannelService`
574 : : *
575 : : * Get the local ID.
576 : : *
577 : : * Returns: (transfer full) (not nullable): the service ID
578 : : *
579 : : * Since: 1.0
580 : : */
581 : : char *
582 : 14 : valent_channel_service_dup_id (ValentChannelService *service)
583 : : {
584 : 14 : ValentChannelServicePrivate *priv = valent_channel_service_get_instance_private (service);
585 : 14 : char *ret;
586 : :
587 [ + - ]: 14 : g_return_val_if_fail (VALENT_IS_CHANNEL_SERVICE (service), NULL);
588 : :
589 : 14 : valent_object_lock (VALENT_OBJECT (service));
590 [ - + ]: 14 : ret = g_strdup (priv->id);
591 : 14 : valent_object_unlock (VALENT_OBJECT (service));
592 : :
593 : 14 : return g_steal_pointer (&ret);
594 : : }
595 : :
596 : : /**
597 : : * valent_channel_service_ref_identity: (get-property identity)
598 : : * @service: a `ValentChannelService`
599 : : *
600 : : * Get the local identity packet.
601 : : *
602 : : * Returns: (transfer full): a KDE Connect packet
603 : : *
604 : : * Since: 1.0
605 : : */
606 : : JsonNode *
607 : 24 : valent_channel_service_ref_identity (ValentChannelService *service)
608 : : {
609 : 24 : ValentChannelServicePrivate *priv = valent_channel_service_get_instance_private (service);
610 : 24 : JsonNode *ret;
611 : :
612 [ + - ]: 24 : g_return_val_if_fail (VALENT_IS_CHANNEL_SERVICE (service), NULL);
613 : :
614 : 24 : valent_object_lock (VALENT_OBJECT (service));
615 : 24 : ret = json_node_ref (priv->identity);
616 : 24 : valent_object_unlock (VALENT_OBJECT (service));
617 : :
618 : 24 : return g_steal_pointer (&ret);
619 : : }
620 : :
621 : : /**
622 : : * valent_channel_service_build_identity: (virtual build_identity)
623 : : * @service: a `ValentChannelService`
624 : : *
625 : : * Rebuild the local KDE Connect identity packet.
626 : : *
627 : : * This method is called to rebuild the identity packet used to identify the
628 : : * host device to remote devices.
629 : : *
630 : : * Implementations that override [vfunc@Valent.ChannelService.build_identity]
631 : : * should chain-up first, then call [method@Valent.ChannelService.ref_identity]
632 : : * and modify that.
633 : : *
634 : : * Since: 1.0
635 : : */
636 : : void
637 : 23 : valent_channel_service_build_identity (ValentChannelService *service)
638 : : {
639 : 23 : VALENT_ENTRY;
640 : :
641 [ + - ]: 23 : g_return_if_fail (VALENT_IS_CHANNEL_SERVICE (service));
642 : :
643 : 23 : valent_object_lock (VALENT_OBJECT (service));
644 : 23 : VALENT_CHANNEL_SERVICE_GET_CLASS (service)->build_identity (service);
645 : 23 : valent_object_unlock (VALENT_OBJECT (service));
646 : :
647 : 23 : VALENT_EXIT;
648 : : }
649 : :
650 : : /**
651 : : * valent_channel_service_identify: (virtual identify)
652 : : * @service: a `ValentChannelService`
653 : : * @target: (nullable): a target string
654 : : *
655 : : * Identify the host device to the network.
656 : : *
657 : : * This method is called to announce the availability of the host device to
658 : : * other devices.
659 : : *
660 : : * Implementations that override [vfunc@Valent.ChannelService.identify] may
661 : : * ignore @target or use it to address a particular device.
662 : : *
663 : : * Since: 1.0
664 : : */
665 : : void
666 : 6 : valent_channel_service_identify (ValentChannelService *service,
667 : : const char *target)
668 : : {
669 : 6 : VALENT_ENTRY;
670 : :
671 [ + - ]: 6 : g_return_if_fail (VALENT_IS_CHANNEL_SERVICE (service));
672 : :
673 : 6 : VALENT_CHANNEL_SERVICE_GET_CLASS (service)->identify (service, target);
674 : :
675 : 6 : VALENT_EXIT;
676 : : }
677 : :
678 : : /**
679 : : * valent_channel_service_channel:
680 : : * @service: a `ValentChannelService`
681 : : * @channel: a `ValentChannel`
682 : : *
683 : : * Emit [signal@Valent.ChannelService::channel] on @service.
684 : : *
685 : : * This method should only be called by implementations of
686 : : * [class@Valent.ChannelService].
687 : : *
688 : : * Since: 1.0
689 : : */
690 : : void
691 : 11 : valent_channel_service_channel (ValentChannelService *service,
692 : : ValentChannel *channel)
693 : : {
694 : 11 : ChannelEmission *emission;
695 : :
696 [ + - ]: 11 : g_return_if_fail (VALENT_IS_CHANNEL_SERVICE (service));
697 [ - + ]: 11 : g_return_if_fail (VALENT_IS_CHANNEL (channel));
698 : :
699 [ + + ]: 11 : if G_LIKELY (VALENT_IS_MAIN_THREAD ())
700 : : {
701 : 8 : g_signal_emit (G_OBJECT (service), signals [CHANNEL], 0, channel);
702 : 8 : return;
703 : : }
704 : :
705 : 3 : emission = g_new0 (ChannelEmission, 1);
706 : 3 : g_rec_mutex_init (&emission->lock);
707 : 3 : g_rec_mutex_lock (&emission->lock);
708 : 3 : g_weak_ref_init (&emission->service, service);
709 : 3 : emission->channel = g_object_ref (channel);
710 : 3 : g_rec_mutex_unlock (&emission->lock);
711 : :
712 : 3 : g_timeout_add (0, valent_channel_service_channel_main, emission);
713 : : }
714 : :
|