Debian Codegraph @

src/upload/main.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 <stdlib.h>
25 #include <string.h>
26
27 #include "control_point.h"
28 #include "item-creation.h"
29 #include "transfer.h"
30 #include "container-search.h"
31 #include "main.h"
32
33 static GMainLoop *main_loop;
34 static GUPnPContext *upnp_context;
35 static GUPnPServiceProxy *cds_proxy;
36
37 static GList *files = NULL;
38 static char *udn = NULL;
39 static char *interface = NULL;
40
41 static const char *title = NULL;
42 static char *dest_container = NULL;
43 static guint search_timeout = 0;
44
45 static GOptionEntry entries[] =
46 {
47 { "search-timeout", 't', 0,
48 G_OPTION_ARG_INT, &search_timeout,
49 "Search Timeout", "seconds" },
50 { "container-id", 'c', 0,
51 G_OPTION_ARG_STRING, &dest_container,
52 "Destination Container ID", "CONTAINER_ID" },
53 { "title", 'i', 0,
54 G_OPTION_ARG_STRING, &title,
55 "Title for item", "TITLE" },
56 { "interface", 'e', 0,
57 G_OPTION_ARG_STRING, &interface,
58 "Network interface to search MediaServer on", "INTERFACE" },
59 { "udn", 'u', 0,
60 G_OPTION_ARG_STRING, &udn,
61 "UDN of the device to upload to", "UDN" },
62 { NULL }
63 };
64
65 static void
66 goto_next_file (void)
67 {
68 files = g_list_next (files);
69
70 if (files != NULL) {
71 create_item ((char *) files->data,
72 title,
73 cds_proxy,
74 dest_container);
75 } else {
76 /* Exit if there are no more files to upload */
77 application_exit ();
78 }
79 }
80
81 void
82 transfer_completed (void)
83 {
84 goto_next_file ();
85 }
86
87 void
88 container_found (const char *container_id)
89 {
90 dest_container = g_strdup (container_id);
91
92 /* Now create the item container */
93 create_item ((char *) files->data,
94 title,
95 cds_proxy,
96 dest_container);
97 }
98
99 void
100 item_created (const char *import_uri) {
101 if (import_uri == NULL) {
102 /* Item creation failed, lets try next file */
103 goto_next_file ();
104 } else {
105 start_transfer ((char *) files->data,
106 import_uri,
107 cds_proxy,
108 upnp_context);
109 }
110 }
111
112 void
113 target_cds_found (GUPnPServiceProxy *target_cds_proxy)
114 {
115 cds_proxy = target_cds_proxy;
116
117 if (dest_container != NULL) {
118 container_found (dest_container);
119 } else {
120 /* Find a suitable container */
121 search_container (target_cds_proxy);
122 }
123 }
124
125 void
126 application_exit (void)
127 {
128 if (main_loop != NULL)
129 g_main_loop_quit (main_loop);
130 }
131
132 gint
133 main (gint argc,
134 gchar *argv[])
135 {
136 g_autoptr (GError) error = NULL;
137 gint i;
138 g_autoptr (GOptionContext) context;
139
140 context = g_option_context_new ("- Upload files to UPnP MediaServer");
141 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
142 if (!g_option_context_parse (context, &argc, &argv, &error))
143 {
144 g_print ("option parsing failed: %s\n", error->message);
145 return -1;
146 }
147
148 if (argc < 2) {
149 g_autofree char *help = NULL;
150
151 help = g_option_context_get_help (context, TRUE, NULL);
152 g_print ("%s\n", help);
153
154 return -4;
155 }
156
157 if (udn == NULL) {
158 g_print ("Error: Please provide UDN for the target server\n");
159 return -4;
160 }
161
162 /* Get the list of files to upload */
163 for (i = 1; i < argc; i++) {
164 if (!g_file_test (argv[i],
165 G_FILE_TEST_EXISTS |
166 G_FILE_TEST_IS_REGULAR)) {
167 g_printerr ("File %s does not exist\n", argv[i]);
168 } else {
169 files = g_list_append (files, argv[i]);
170 }
171 }
172
173 if (files == NULL) {
174 return -5;
175 }
176
177 error = NULL;
178 upnp_context = gupnp_context_new_full (interface,
179 NULL,
180 0,
181 GSSDP_UDA_VERSION_1_0,
182 &error);
183 if (error) {
184 g_printerr ("Error creating the GUPnP context: %s\n",
185 error->message);
186
187 return -6;
188 }
189
190 g_print ("UPnP context created for interface %s (%s)\n",
191 gssdp_client_get_interface (GSSDP_CLIENT (upnp_context)),
192 gssdp_client_get_host_ip (GSSDP_CLIENT (upnp_context)));
193
194 if (!init_control_point (upnp_context, udn, search_timeout)) {
195 return -3;
196 }
197
198 main_loop = g_main_loop_new (NULL, FALSE);
199
200 g_main_loop_run (main_loop);
201
202 /* Clean-up */
203 g_clear_pointer (&main_loop, g_main_loop_unref);
204 deinit_control_point ();
205 g_object_unref (upnp_context);
206 g_free (dest_container);
207 g_free (interface);
208
209 return 0;
210 }
211