~ubuntu-branches/ubuntu/lucid/gupnp-tools/lucid

« back to all changes in this revision

Viewing changes to src/upload/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Onkar Shinde
  • Date: 2009-02-16 22:17:19 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20090216221719-m23ajj6d2bhmyr40
Tags: 0.7-0ubuntu1
* New upstream release. (LP: #312551)
* debian/control
  - Added build dependency libglib2.0-dev (>= 2.12) as per upstream changes.
  - Added description for new tool.

Show diffs side-by-side

added added

removed removed

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