Debian Codegraph @

src/upload/control_point.c

raw
1 /*
2 * Copyright (C) 2007 Zeeshan Ali (Khattak) <[email protected]>
3 *
4 * Authors: Zeeshan Ali (Khattak) <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21 #include <config.h>
22
23 #include <libgupnp/gupnp.h>
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include "control_point.h"
28 #include "main.h"
29
30 #define CDS "urn:schemas-upnp-org:service:ContentDirectory"
31
32 static GUPnPControlPoint *cp;
33 static GUPnPServiceProxy *cds_proxy = NULL;
34
35 static guint timeout_id = 0;
36
37 static void
38 device_proxy_unavailable_cb (GUPnPControlPoint *control_point,
39 GUPnPDeviceProxy *proxy)
40 {
41 /* We don't need to check if it's our proxy */
42 application_exit ();
43 }
44
45 static void
46 device_proxy_available_cb (GUPnPControlPoint *control_point,
47 GUPnPDeviceProxy *proxy)
48 {
49 GUPnPDeviceInfo *info;
50
51 info = GUPNP_DEVICE_INFO (proxy);
52
53 cds_proxy = GUPNP_SERVICE_PROXY (
54 gupnp_device_info_get_service (info, CDS));
55 if (G_UNLIKELY (cds_proxy == NULL)) {
56 g_critical ("MediaServer '%s' doesn't provide ContentDirectory"
57 "service", gupnp_device_info_get_udn (info));
58
59 application_exit ();
60
61 return;
62 }
63
64 if (timeout_id > 0) {
65 /* No need to timeout anymore */
66 g_source_remove (timeout_id);
67 }
68
69 /* No need to bother searching anymore */
70 g_signal_handlers_disconnect_by_func (
71 cp,
72 G_CALLBACK (device_proxy_available_cb),
73 NULL);
74
75 /* But we still want to know when our device goes away */
76 g_signal_connect (cp,
77 "device-proxy-unavailable",
78 G_CALLBACK (device_proxy_unavailable_cb),
79 NULL);
80
81 target_cds_found (cds_proxy);
82 }
83
84 gboolean
85 init_control_point (GUPnPContext *context,
86 const char *udn,
87 guint timeout)
88 {
89 /* We're interested in everything at this stage */
90 cp = gupnp_control_point_new (context, udn);
91
92 g_signal_connect (cp,
93 "device-proxy-available",
94 G_CALLBACK (device_proxy_available_cb),
95 NULL);
96
97 gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (cp), TRUE);
98
99 if (timeout > 0) {
100 timeout_id =
101 g_timeout_add_seconds (timeout,
102 (GSourceFunc) application_exit,
103 NULL);
104 }
105
106 return TRUE;
107 }
108
109 void
110 deinit_control_point (void)
111 {
112 if (cds_proxy != NULL) {
113 g_object_unref (cds_proxy);
114 }
115
116 g_object_unref (cp);
117 }
118