~ubuntu-branches/debian/sid/file-roller/sid

« back to all changes in this revision

Viewing changes to src/server.c

  • Committer: Package Import Robot
  • Author(s): Michael Biebl, Josselin Mouette, Michael Biebl
  • Date: 2011-10-13 22:43:53 UTC
  • mfrom: (5.1.5 experimental)
  • Revision ID: package-import@ubuntu.com-20111013224353-7fub412oa8jwkcgt
Tags: 3.0.2-2
[ Josselin Mouette ]
* file-roller.mime: dropped. We don’t do necromancy anymore.
* Drop desktop-check-mime-types call too.

[ Michael Biebl ]
* Upload to unstable.
* debian/control.in:
  - Set pkg-gnome-maintainers@lists.alioth.debian.org as Maintainer.
* debian/watch:
  - Switch to .xz tarballs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 
2
 
 
3
/*
 
4
 *  File-Roller
 
5
 *
 
6
 *  Copyright (C) 2010 Free Software Foundation, Inc.
 
7
 *
 
8
 *  This program is free software; you can redistribute it and/or modify
 
9
 *  it under the terms of the GNU General Public License as published by
 
10
 *  the Free Software Foundation; either version 2 of the License, or
 
11
 *  (at your option) any later version.
 
12
 *
 
13
 *  This program is distributed in the hope that it will be useful,
 
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 *  GNU General Public License for more details.
 
17
 *
 
18
 *  You should have received a copy of the GNU General Public License
 
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
 */
 
21
 
 
22
#include <config.h>
 
23
#include <stdlib.h>
 
24
#include <glib/gi18n.h>
 
25
#include <glib.h>
 
26
#include <gio/gio.h>
 
27
#include "file-utils.h"
 
28
#include "fr-init.h"
 
29
 
 
30
 
 
31
#define FR_SERVICE_NAME "org.gnome.FileRoller"
 
32
#define FR_SERVICE_PATH "/org/gnome/FileRoller"
 
33
 
 
34
 
 
35
static const char introspection_xml[] =
 
36
        "<node>"
 
37
        "  <interface name='org.gnome.ArchiveManager'>"
 
38
 
 
39
        /**
 
40
         * GetSupportedTypes:
 
41
         *
 
42
         * Returns the supported archive types for a specific action.
 
43
         *
 
44
         * Input arguments:
 
45
         *
 
46
         * @action:
 
47
         *   Can be one of the following values:
 
48
         *   *) create: create an archive that can contain many files.
 
49
         *   *) create_single_file: create an archive that can contain a single file.
 
50
         *   *) extract: extract the content of an archive.
 
51
         *
 
52
         * Output arguments:
 
53
         *
 
54
         * @types:
 
55
         *   The supported archive types described as an array of hash tables,
 
56
         *   where each hash table has the following keys:
 
57
         *   *) mime-type: the mime type relative to the archive type.
 
58
         *   *) default-extension: the extension to use for newly created archives.
 
59
         *   *) description: a human readable description of the archive type.
 
60
         */
 
61
 
 
62
        "    <method name='GetSupportedTypes'>"
 
63
        "      <arg name='action' type='s' direction='in'/>"
 
64
        "      <arg name='types' type='aa{ss}' direction='out'/>"
 
65
        "    </method>"
 
66
 
 
67
        /**
 
68
         * AddToArchive:
 
69
         *
 
70
         * Adds the specified files to an archive.  If the archive already
 
71
         * exists the archive is updated.
 
72
         *
 
73
         * Input arguments:
 
74
         *
 
75
         * @archive:
 
76
         *   The archive URI.
 
77
         * @files:
 
78
         *   The files to add to the archive, as an array of URIs.
 
79
         * @use_progress_dialog:
 
80
         *   Whether to show the progress dialog.
 
81
         */
 
82
 
 
83
        "    <method name='AddToArchive'>"
 
84
        "      <arg name='archive' type='s' direction='in'/>"
 
85
        "      <arg name='files' type='as' direction='in'/>"
 
86
        "      <arg name='use_progress_dialog' type='b' direction='in'/>"
 
87
        "    </method>"
 
88
 
 
89
        /**
 
90
         * Compress:
 
91
         *
 
92
         * Compresses a series of files in an archive. The user is asked to
 
93
         * enter an archive name, archive type and other options.  In this case
 
94
         * it's used the same dialog used by the "Compress..." command from the
 
95
         * Nautilus context menu.
 
96
         * If the user chooses an existing archive, the archive is updated.
 
97
         *
 
98
         * Input arguments:
 
99
         *
 
100
         * @files:
 
101
         *   The files to add to the archive, as an array of URIs.
 
102
         * @destination:
 
103
         *   An optional destination, if not specified the folder of the first
 
104
         *   file in @files is used.
 
105
         * @use_progress_dialog:
 
106
         *   Whether to show the progress dialog.
 
107
         */
 
108
 
 
109
        "    <method name='Compress'>"
 
110
        "      <arg name='files' type='as' direction='in'/>"
 
111
        "      <arg name='destination' type='s' direction='in'/>"
 
112
        "      <arg name='use_progress_dialog' type='b' direction='in'/>"
 
113
        "    </method>"
 
114
 
 
115
        /**
 
116
         * Extract:
 
117
         *
 
118
         * Extract an archive in a specified location.
 
119
         *
 
120
         * Input arguments:
 
121
         *
 
122
         * @archive:
 
123
         *   The archive to extract.
 
124
         * @destination:
 
125
         *   The location where to extract the archive.
 
126
         * @use_progress_dialog:
 
127
         *   Whether to show the progress dialog.
 
128
         */
 
129
 
 
130
        "    <method name='Extract'>"
 
131
        "      <arg name='archive' type='s' direction='in'/>"
 
132
        "      <arg name='destination' type='s' direction='in'/>"
 
133
        "      <arg name='use_progress_dialog' type='b' direction='in'/>"
 
134
        "    </method>"
 
135
 
 
136
        /**
 
137
         * ExtractHere:
 
138
         *
 
139
         * Extract an archive in a specified location.
 
140
         *
 
141
         * Input arguments:
 
142
         *
 
143
         * @archive:
 
144
         *   The archive to extract.
 
145
         * @use_progress_dialog:
 
146
         *   Whether to show the progress dialog.
 
147
         */
 
148
 
 
149
        "    <method name='ExtractHere'>"
 
150
        "      <arg name='archive' type='s' direction='in'/>"
 
151
        "      <arg name='use_progress_dialog' type='b' direction='in'/>"
 
152
        "    </method>"
 
153
 
 
154
        /**
 
155
         * Progress (signal)
 
156
         *
 
157
         * Arguments:
 
158
         *
 
159
         * @fraction:
 
160
         *   number from 0.0 to 100.0 that indicates the percentage of
 
161
         *   completion of the operation.
 
162
         * @details:
 
163
         *   text message that describes the current operation.
 
164
         */
 
165
 
 
166
        "    <signal name='Progress'>"
 
167
        "      <arg name='fraction' type='d'/>"
 
168
        "      <arg name='details' type='s'/>"
 
169
        "    </signal>"
 
170
 
 
171
        "  </interface>"
 
172
        "</node>";
 
173
 
 
174
static GDBusNodeInfo *introspection_data = NULL;
 
175
 
 
176
 
 
177
static void
 
178
window_ready_cb (GtkWidget *widget,
 
179
                 GError    *error,
 
180
                 gpointer   user_data)
 
181
{
 
182
        if (error == NULL)
 
183
                g_dbus_method_invocation_return_value ((GDBusMethodInvocation *) user_data, NULL);
 
184
        else
 
185
                g_dbus_method_invocation_return_error ((GDBusMethodInvocation *) user_data,
 
186
                                                       error->domain,
 
187
                                                       error->code,
 
188
                                                       "%s",
 
189
                                                       error->message);
 
190
}
 
191
 
 
192
 
 
193
static gboolean
 
194
window_progress_cb (FrWindow *window,
 
195
                    double    fraction,
 
196
                    char     *details,
 
197
                    gpointer  user_data)
 
198
{
 
199
        GDBusConnection *connection = user_data;
 
200
 
 
201
        g_dbus_connection_emit_signal (connection,
 
202
                                       NULL,
 
203
                                       FR_SERVICE_PATH,
 
204
                                       "org.gnome.ArchiveManager",
 
205
                                       "Progress",
 
206
                                       g_variant_new ("(ds)",
 
207
                                                      fraction,
 
208
                                                      details),
 
209
                                       NULL);
 
210
 
 
211
        return TRUE;
 
212
}
 
213
 
 
214
 
 
215
static void
 
216
handle_method_call (GDBusConnection       *connection,
 
217
                    const char            *sender,
 
218
                    const char            *object_path,
 
219
                    const char            *interface_name,
 
220
                    const char            *method_name,
 
221
                    GVariant              *parameters,
 
222
                    GDBusMethodInvocation *invocation,
 
223
                    gpointer               user_data)
 
224
{
 
225
        update_registered_commands_capabilities ();
 
226
 
 
227
        if (g_strcmp0 (method_name, "GetSupportedTypes") == 0) {
 
228
                char *action;
 
229
                int  *supported_types = NULL;
 
230
 
 
231
                g_variant_get (parameters, "(s)", &action);
 
232
 
 
233
                if (g_strcmp0 (action, "create") == 0) {
 
234
                        supported_types = save_type;
 
235
                }
 
236
                else if (g_strcmp0 (action, "create_single_file") == 0) {
 
237
                        supported_types = single_file_save_type;
 
238
                }
 
239
                else if (g_strcmp0 (action, "extract") == 0) {
 
240
                        supported_types = open_type;
 
241
                }
 
242
 
 
243
                if (supported_types == NULL) {
 
244
                        g_dbus_method_invocation_return_error (invocation,
 
245
                                                               G_IO_ERROR,
 
246
                                                               G_IO_ERROR_INVALID_ARGUMENT,
 
247
                                                               "Action not valid %s, valid values are: create, create_single_file, extract",
 
248
                                                               action);
 
249
                }
 
250
                else {
 
251
                        GVariantBuilder builder;
 
252
                        int             i;
 
253
 
 
254
                        g_variant_builder_init (&builder, G_VARIANT_TYPE ("(aa{ss})"));
 
255
                        g_variant_builder_open (&builder, G_VARIANT_TYPE ("aa{ss}"));
 
256
                        for (i = 0; supported_types[i] != -1; i++) {
 
257
                                g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{ss}"));
 
258
                                g_variant_builder_add (&builder, "{ss}",
 
259
                                                       "mime-type",
 
260
                                                       mime_type_desc[supported_types[i]].mime_type);
 
261
                                g_variant_builder_add (&builder, "{ss}",
 
262
                                                       "default-extension",
 
263
                                                       mime_type_desc[supported_types[i]].default_ext);
 
264
                                g_variant_builder_add (&builder, "{ss}",
 
265
                                                       "description",
 
266
                                                       _(mime_type_desc[supported_types[i]].name));
 
267
                                g_variant_builder_close (&builder);
 
268
                        }
 
269
                        g_variant_builder_close (&builder);
 
270
 
 
271
                        g_dbus_method_invocation_return_value (invocation, g_variant_builder_end (&builder));
 
272
                }
 
273
 
 
274
                g_free (action);
 
275
        }
 
276
        else if (g_strcmp0 (method_name, "AddToArchive") == 0) {
 
277
                char       *archive;
 
278
                char      **files;
 
279
                gboolean    use_progress_dialog;
 
280
                int         i;
 
281
                GList      *file_list = NULL;
 
282
                GtkWidget  *window;
 
283
 
 
284
                g_variant_get (parameters, "(s^asb)", &archive, &files, &use_progress_dialog);
 
285
 
 
286
                for (i = 0; files[i] != NULL; i++)
 
287
                        file_list = g_list_prepend (file_list, files[i]);
 
288
                file_list = g_list_reverse (file_list);
 
289
 
 
290
                window = fr_window_new ();
 
291
                fr_window_use_progress_dialog (FR_WINDOW (window), use_progress_dialog);
 
292
 
 
293
                g_signal_connect (window, "progress", G_CALLBACK (window_progress_cb), connection);
 
294
                g_signal_connect (window, "ready", G_CALLBACK (window_ready_cb), invocation);
 
295
 
 
296
                fr_window_new_batch (FR_WINDOW (window));
 
297
                fr_window_set_batch__add (FR_WINDOW (window), archive, file_list);
 
298
                fr_window_append_batch_action (FR_WINDOW (window), FR_BATCH_ACTION_QUIT, NULL, NULL);
 
299
                fr_window_start_batch (FR_WINDOW (window));
 
300
 
 
301
                g_free (archive);
 
302
 
 
303
                gtk_main ();
 
304
        }
 
305
        else if (g_strcmp0 (method_name, "Compress") == 0) {
 
306
                char      **files;
 
307
                char       *destination;
 
308
                gboolean    use_progress_dialog;
 
309
                int         i;
 
310
                GList      *file_list = NULL;
 
311
                GtkWidget  *window;
 
312
 
 
313
                g_variant_get (parameters, "(^assb)", &files, &destination, &use_progress_dialog);
 
314
 
 
315
                for (i = 0; files[i] != NULL; i++)
 
316
                        file_list = g_list_prepend (file_list, files[i]);
 
317
                file_list = g_list_reverse (file_list);
 
318
 
 
319
                if ((destination == NULL) || (strcmp (destination, "") == 0))
 
320
                        destination = remove_level_from_path (file_list->data);
 
321
 
 
322
                window = fr_window_new ();
 
323
                fr_window_use_progress_dialog (FR_WINDOW (window), use_progress_dialog);
 
324
                fr_window_set_default_dir (FR_WINDOW (window), destination, TRUE);
 
325
 
 
326
                g_signal_connect (window, "progress", G_CALLBACK (window_progress_cb), connection);
 
327
                g_signal_connect (window, "ready", G_CALLBACK (window_ready_cb), invocation);
 
328
 
 
329
                fr_window_new_batch (FR_WINDOW (window));
 
330
                fr_window_set_batch__add (FR_WINDOW (window), NULL, file_list);
 
331
                fr_window_append_batch_action (FR_WINDOW (window), FR_BATCH_ACTION_QUIT, NULL, NULL);
 
332
                fr_window_start_batch (FR_WINDOW (window));
 
333
 
 
334
                g_free (destination);
 
335
 
 
336
                gtk_main ();
 
337
        }
 
338
        else if (g_strcmp0 (method_name, "Extract") == 0) {
 
339
                char      *archive;
 
340
                char      *destination;
 
341
                gboolean   use_progress_dialog;
 
342
                GtkWidget *window;
 
343
 
 
344
                g_variant_get (parameters, "(ssb)", &archive, &destination, &use_progress_dialog);
 
345
 
 
346
                window = fr_window_new ();
 
347
                fr_window_use_progress_dialog (FR_WINDOW (window), use_progress_dialog);
 
348
                if ((destination != NULL) & (strcmp (destination, "") != 0))
 
349
                        fr_window_set_default_dir (FR_WINDOW (window), destination, TRUE);
 
350
 
 
351
                g_signal_connect (window, "progress", G_CALLBACK (window_progress_cb), connection);
 
352
                g_signal_connect (window, "ready", G_CALLBACK (window_ready_cb), invocation);
 
353
 
 
354
                fr_window_new_batch (FR_WINDOW (window));
 
355
                fr_window_set_batch__extract (FR_WINDOW (window), archive, destination);
 
356
                fr_window_append_batch_action (FR_WINDOW (window), FR_BATCH_ACTION_QUIT, NULL, NULL);
 
357
                fr_window_start_batch (FR_WINDOW (window));
 
358
 
 
359
                g_free (destination);
 
360
                g_free (archive);
 
361
 
 
362
                gtk_main ();
 
363
        }
 
364
        else if (g_strcmp0 (method_name, "ExtractHere") == 0) {
 
365
                char      *archive;
 
366
                gboolean   use_progress_dialog;
 
367
                GtkWidget *window;
 
368
 
 
369
                g_variant_get (parameters, "(sb)", &archive, &use_progress_dialog);
 
370
 
 
371
                window = fr_window_new ();
 
372
                fr_window_use_progress_dialog (FR_WINDOW (window), use_progress_dialog);
 
373
 
 
374
                g_signal_connect (window, "progress", G_CALLBACK (window_progress_cb), connection);
 
375
                g_signal_connect (window, "ready", G_CALLBACK (window_ready_cb), invocation);
 
376
 
 
377
                fr_window_new_batch (FR_WINDOW (window));
 
378
                fr_window_set_batch__extract_here (FR_WINDOW (window), archive);
 
379
                fr_window_append_batch_action (FR_WINDOW (window), FR_BATCH_ACTION_QUIT, NULL, NULL);
 
380
                fr_window_start_batch (FR_WINDOW (window));
 
381
 
 
382
                g_free (archive);
 
383
 
 
384
                gtk_main ();
 
385
        }
 
386
}
 
387
 
 
388
 
 
389
static const GDBusInterfaceVTable interface_vtable = {
 
390
        handle_method_call,
 
391
        NULL,                   /* handle_get_property */
 
392
        NULL                    /* handle_set_property */
 
393
};
 
394
 
 
395
 
 
396
static void
 
397
on_bus_acquired (GDBusConnection *connection,
 
398
                 const char      *name,
 
399
                 gpointer         user_data)
 
400
{
 
401
        guint registration_id;
 
402
 
 
403
        registration_id = g_dbus_connection_register_object (connection,
 
404
                                                             FR_SERVICE_PATH,
 
405
                                                             introspection_data->interfaces[0],
 
406
                                                             &interface_vtable,
 
407
                                                             NULL,
 
408
                                                             NULL,  /* user_data_free_func */
 
409
                                                             NULL); /* GError** */
 
410
        g_assert (registration_id > 0);
 
411
 
 
412
        initialize_data ();
 
413
}
 
414
 
 
415
 
 
416
static void
 
417
on_name_acquired (GDBusConnection *connection,
 
418
                  const char      *name,
 
419
                  gpointer         user_data)
 
420
{
 
421
}
 
422
 
 
423
 
 
424
static void
 
425
on_name_lost (GDBusConnection *connection,
 
426
              const char      *name,
 
427
              gpointer         user_data)
 
428
{
 
429
        gtk_main_quit ();
 
430
}
 
431
 
 
432
 
 
433
int
 
434
main (int argc, char *argv[])
 
435
{
 
436
        GOptionContext *context = NULL;
 
437
        GError         *error = NULL;
 
438
        guint           owner_id;
 
439
 
 
440
        bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
 
441
        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 
442
        textdomain (GETTEXT_PACKAGE);
 
443
 
 
444
        context = g_option_context_new (N_("- Create and modify an archive"));
 
445
        g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
 
446
        g_option_context_add_group (context, gtk_get_option_group (TRUE));
 
447
 
 
448
        if (! g_option_context_parse (context, &argc, &argv, &error)) {
 
449
                g_critical ("Failed to parse arguments: %s", error->message);
 
450
                g_error_free (error);
 
451
                g_option_context_free (context);
 
452
                return EXIT_FAILURE;
 
453
        }
 
454
 
 
455
        g_option_context_free (context);
 
456
 
 
457
        g_set_application_name (_("File Roller"));
 
458
        gtk_window_set_default_icon_name ("file-roller");
 
459
 
 
460
        gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
 
461
                                           PKG_DATA_DIR G_DIR_SEPARATOR_S "icons");
 
462
 
 
463
        introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
 
464
        g_assert (introspection_data != NULL);
 
465
 
 
466
        owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
 
467
                                   FR_SERVICE_NAME,
 
468
                                   G_BUS_NAME_OWNER_FLAGS_NONE,
 
469
                                   on_bus_acquired,
 
470
                                   on_name_acquired,
 
471
                                   on_name_lost,
 
472
                                   NULL,
 
473
                                   NULL);
 
474
 
 
475
        gtk_main ();
 
476
 
 
477
        g_bus_unown_name (owner_id);
 
478
        g_dbus_node_info_unref (introspection_data);
 
479
 
 
480
        return 0;
 
481
}