File: mate-seahorse-tool-files.c

package info (click to toggle)
caja-seahorse 1.18.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,876 kB
  • sloc: ansic: 4,278; xml: 469; makefile: 134; sh: 15
file content (928 lines) | stat: -rw-r--r-- 27,385 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
/*
 * Seahorse
 *
 * Copyright (C) 2006 Stefan Walter
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libintl.h>

#include <glib/gi18n.h>
#include <gio/gio.h>

#include "mate-seahorse-tool.h"
#include "seahorse-util.h"
#include "seahorse-widget.h"
#include "seahorse-vfs-data.h"

#define ONE_GIGABYTE 1024 * 1024 * 1024
#define FILE_ATTRIBUTES "standard::*"

typedef struct _FileInfo {
    GFile *file;
    GFileInfo *info;
    gchar *uri;
} FileInfo;

typedef struct _FilesCtx {
    GSList *uris;
    GList *finfos;
    FileInfo *cur;
    gboolean remote;

    guint64 total;
    guint64 done;
} FilesCtx;

static void
free_file_info (FileInfo *finfo, gpointer unused)
{
    if (finfo) {
        g_object_unref (finfo->file);
        g_object_unref (finfo->info);
        g_free (finfo->uri);
    }
    g_free (finfo);
}

/* Included from file-roller/src/main.c for file types */

typedef enum {
	FR_FILE_TYPE_ACE,
	FR_FILE_TYPE_AR,
	FR_FILE_TYPE_ARJ,
	FR_FILE_TYPE_BZIP,
	FR_FILE_TYPE_BZIP2,
	FR_FILE_TYPE_COMPRESS,
	FR_FILE_TYPE_CPIO,
	FR_FILE_TYPE_DEB,
	FR_FILE_TYPE_ISO,
	FR_FILE_TYPE_EAR,
	FR_FILE_TYPE_EXE,
	FR_FILE_TYPE_GZIP,
	FR_FILE_TYPE_JAR,
	FR_FILE_TYPE_LHA,
	FR_FILE_TYPE_LZOP,
	FR_FILE_TYPE_RAR,
	FR_FILE_TYPE_RPM,
	FR_FILE_TYPE_TAR,
	FR_FILE_TYPE_TAR_BZ,
	FR_FILE_TYPE_TAR_BZ2,
	FR_FILE_TYPE_TAR_GZ,
	FR_FILE_TYPE_TAR_LZOP,
	FR_FILE_TYPE_TAR_COMPRESS,
	FR_FILE_TYPE_STUFFIT,
	FR_FILE_TYPE_WAR,
	FR_FILE_TYPE_ZIP,
	FR_FILE_TYPE_ZOO,
	FR_FILE_TYPE_7ZIP,
	FR_FILE_TYPE_NULL
} FRFileType;

typedef struct {
	char       *command;
	gboolean    can_open;
	gboolean    can_save;
	gboolean    support_many_files;
	FRFileType  file_type;
} FRCommandDescription;

typedef struct {
	FRFileType  id;
	char       *ext;
	char       *mime_type;
	char       *name;
} FRFileTypeDescription;

FRFileTypeDescription file_type_desc[] = {
	{ FR_FILE_TYPE_ACE,          ".ace",     "application/x-ace", N_("Ace (.ace)") },
	{ FR_FILE_TYPE_AR,           ".ar",      "application/x-ar", N_("Ar (.ar)") },
	{ FR_FILE_TYPE_ARJ,          ".arj",     "application/x-arj", N_("Arj (.arj)") },
	{ FR_FILE_TYPE_BZIP,         ".bz",      "application/x-bzip", NULL },
	{ FR_FILE_TYPE_BZIP2,        ".bz2",     "application/x-bzip", NULL },
	{ FR_FILE_TYPE_COMPRESS,     ".Z",       "application/x-compress", NULL },
	{ FR_FILE_TYPE_CPIO,         ".cpio",    "application/x-cpio", NULL },
	{ FR_FILE_TYPE_DEB,          ".deb",     "application/x-deb", NULL },
	{ FR_FILE_TYPE_ISO,          ".iso",     "application/x-cd-image", NULL },
	{ FR_FILE_TYPE_EAR,          ".ear",     "application/x-ear", N_("Ear (.ear)") },
	{ FR_FILE_TYPE_EXE,          ".exe",     "application/x-ms-dos-executable", N_("Self-extracting zip (.exe)") },
	{ FR_FILE_TYPE_GZIP,         ".gz",      "application/x-gzip", NULL},
	{ FR_FILE_TYPE_JAR,          ".jar",     "application/x-jar", N_("Jar (.jar)")},
	{ FR_FILE_TYPE_LHA,          ".lzh",     "application/x-lha", N_("Lha (.lzh)") },
	{ FR_FILE_TYPE_LZOP,         ".lzo",     "application/x-lzop", NULL },
	{ FR_FILE_TYPE_RAR,          ".rar",     "application/x-rar", N_("Rar (.rar)") },
	{ FR_FILE_TYPE_RPM,          ".rpm",     "application/x-rpm", NULL },
	{ FR_FILE_TYPE_TAR,          ".tar",     "application/x-tar", N_("Tar uncompressed (.tar)") },
	{ FR_FILE_TYPE_TAR_BZ,       ".tar.bz",  "application/x-bzip-compressed-tar", N_("Tar compressed with bzip (.tar.bz)") },
	{ FR_FILE_TYPE_TAR_BZ2,      ".tar.bz2", "application/x-bzip-compressed-tar", N_("Tar compressed with bzip2 (.tar.bz2)") },
	{ FR_FILE_TYPE_TAR_GZ,       ".tar.gz",  "application/x-compressed-tar", N_("Tar compressed with gzip (.tar.gz)") },
	{ FR_FILE_TYPE_TAR_LZOP,     ".tar.lzo", "application/x-lzop-compressed-tar", N_("Tar compressed with lzop (.tar.lzo)") },
	{ FR_FILE_TYPE_TAR_COMPRESS, ".tar.Z",   "application/x-compressed-tar", N_("Tar compressed with compress (.tar.Z)") },
	{ FR_FILE_TYPE_STUFFIT,      ".sit",     "application/x-stuffit", NULL },
	{ FR_FILE_TYPE_WAR,          ".war",     "application/zip", N_("War (.war)") },
	{ FR_FILE_TYPE_ZIP,          ".zip",     "application/zip", N_("Zip (.zip)") },
	{ FR_FILE_TYPE_ZOO,          ".zoo",     "application/x-zoo", N_("Zoo (.zoo)") },
	{ FR_FILE_TYPE_7ZIP,         ".7z",      "application/x-7z-compressed", N_("7-Zip (.7z)") }
};

FRCommandDescription command_desc[] = {
	{ "tar",        TRUE,  TRUE,  TRUE,  FR_FILE_TYPE_TAR },
	{ "zip",        TRUE,  TRUE,  TRUE,  FR_FILE_TYPE_ZIP },
	{ "unzip",      TRUE,  FALSE, TRUE,  FR_FILE_TYPE_ZIP },
	{ "rar",        TRUE,  TRUE,  TRUE,  FR_FILE_TYPE_RAR },
	{ "gzip",       TRUE,  TRUE,  FALSE, FR_FILE_TYPE_GZIP },
	{ "bzip2",      TRUE,  TRUE,  FALSE, FR_FILE_TYPE_BZIP2 },
	{ "unace",      TRUE,  FALSE, TRUE,  FR_FILE_TYPE_ACE },
	{ "ar",         TRUE,  TRUE,  TRUE,  FR_FILE_TYPE_AR },
	{ "ar",         TRUE,  FALSE, TRUE,  FR_FILE_TYPE_DEB },
	{ "arj",        TRUE,  TRUE,  TRUE,  FR_FILE_TYPE_ARJ },
	{ "bzip2",      TRUE,  FALSE, FALSE, FR_FILE_TYPE_BZIP },
	{ "compress",   TRUE,  TRUE,  FALSE, FR_FILE_TYPE_COMPRESS },
	{ "cpio",       TRUE,  FALSE, FALSE, FR_FILE_TYPE_CPIO },
	{ "isoinfo",    TRUE,  FALSE, TRUE,  FR_FILE_TYPE_ISO },
	{ "zip",        TRUE,  TRUE,  TRUE,  FR_FILE_TYPE_EAR },
	{ "zip",        TRUE,  TRUE,  TRUE,  FR_FILE_TYPE_JAR },
	{ "zip",        TRUE,  FALSE,  TRUE,  FR_FILE_TYPE_EXE },
	{ "lha",        TRUE,  TRUE,  TRUE,  FR_FILE_TYPE_LHA },
	{ "lzop",       TRUE,  TRUE,  FALSE, FR_FILE_TYPE_LZOP },
	{ "rpm2cpio",   TRUE,  FALSE, TRUE,  FR_FILE_TYPE_RPM },
	{ "uncompress", TRUE,  FALSE, FALSE, FR_FILE_TYPE_COMPRESS },
	{ "unstuff",    TRUE,  FALSE, FALSE, FR_FILE_TYPE_STUFFIT },
	{ "zip",        TRUE,  TRUE,  TRUE,  FR_FILE_TYPE_WAR },
	{ "zoo",        TRUE,  TRUE,  TRUE,  FR_FILE_TYPE_ZOO },
	{ "7za",        TRUE,  TRUE,  TRUE,  FR_FILE_TYPE_7ZIP },
	{ "7zr",        TRUE,  TRUE,  TRUE,  FR_FILE_TYPE_7ZIP }
};

FRCommandDescription tar_command_desc[] = {
	{ "gzip",      TRUE,  TRUE,  TRUE,  FR_FILE_TYPE_TAR_GZ },
	{ "bzip2",     TRUE,  TRUE,  TRUE,  FR_FILE_TYPE_TAR_BZ2 },
	{ "bzip",      FALSE, TRUE,  TRUE,  FR_FILE_TYPE_TAR_BZ },
	{ "lzop",      TRUE,  TRUE,  TRUE,  FR_FILE_TYPE_TAR_LZOP },
	{ "compress",  TRUE,  TRUE,  TRUE,  FR_FILE_TYPE_TAR_COMPRESS }
};

FRFileType save_type[32];

static gboolean
is_program_in_path (const char *filename)
{
	char *str;
	int   result = FALSE;

	str = g_find_program_in_path (filename);
	if (str != NULL) {
		g_free (str);
		result = TRUE;
	}

	return result;
}

static void
compute_supported_archive_types (void)
{
	int i, j;
	int s_i = 0;

	for (i = 0; i < G_N_ELEMENTS (command_desc); i++) {
		FRCommandDescription com = command_desc[i];

		if (!is_program_in_path (com.command))
			continue;

		if (strcmp (com.command, "tar") == 0)
			for (j = 0; j < G_N_ELEMENTS (tar_command_desc); j++) {
				FRCommandDescription com2 = tar_command_desc[j];

				if (!is_program_in_path (com2.command))
					continue;
				save_type[s_i++] = com2.file_type;
			}

		if (com.can_save && com.support_many_files)
			save_type[s_i++] = com.file_type;
	}

	save_type[s_i++] = FR_FILE_TYPE_NULL;
}


/* -----------------------------------------------------------------------------
 * CHECK STEP
 */

static gboolean
step_check_uris (FilesCtx *ctx,
                 const gchar **uris,
                 GError **err)
{
    GFile *file, *base;
    GFileInfo *info;
    gchar *t, *path;
    gboolean ret = TRUE;
    FileInfo *finfo;
    const gchar **k;
    GFileType type;

    g_assert (err && !*err);

    t = g_get_current_dir ();
    base = g_file_new_for_path (t);
    g_free (t);

    for (k = uris; *k; k++) {

        if (!mate_seahorse_tool_progress_check ()) {
            ret = FALSE;
            break;
        }

	t = g_uri_parse_scheme (*k);
	if (t)
		file = g_file_new_for_uri (*k);
	else
	        file = g_file_resolve_relative_path (base, *k);
        g_return_val_if_fail (file != NULL, FALSE);

        /* Find out if file can be accessed locally? */
        path = g_file_get_path (file);
        if (!path)
            ctx->remote = TRUE;
        g_free (path);

        info = g_file_query_info (file, FILE_ATTRIBUTES,
                                  G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, err);

        if (!info) {
        	ret = FALSE;
                g_object_unref (file);
        	break;
        }

        type = g_file_info_get_file_type (info);

        /* Only handle simple types */
        if (type == G_FILE_TYPE_REGULAR || type == G_FILE_TYPE_UNKNOWN ||
            type == G_FILE_TYPE_DIRECTORY) {

            finfo = g_new0 (FileInfo, 1);
            finfo->file = file;
            g_object_ref (file);
            finfo->info = info;
            g_object_ref (info);
            finfo->uri = g_file_get_uri (file);

            ctx->total += g_file_info_get_size (info);
            ctx->finfos = g_list_prepend (ctx->finfos, finfo);
        }

        g_object_unref (file);
        g_object_unref (info);
    }

    g_object_unref (base);

    ctx->finfos = g_list_reverse (ctx->finfos);
    return ret;
}

/* -----------------------------------------------------------------------------
 * PACKAGE STEP
 */

/* Build a message for a given combination of files and folders */
static gchar*
make_message (guint folders, guint files)
{
    gchar *msg, *s1, *s2;

    g_assert(folders > 0 || files > 0);

    /* Necessary hoopla for translations */
    if (folders > 0 && files > 0) {

        /* TRANSLATOR: This string will become
         * "You have selected %d files and %d folders" */
        s1 = g_strdup_printf(ngettext("You have selected %d file ", "You have selected %d files ", files),
                             files);

        /* TRANSLATOR: This string will become
         * "You have selected %d files and %d folders" */
        s2 = g_strdup_printf(ngettext("and %d folder", "and %d folders", folders),
                             folders);

        /* TRANSLATOR: "%s%s" are "You have selected %d files and %d folders"
         * Swap order with "%2$s%1$s" if needed */
        msg = g_strdup_printf(_("<b>%s%s</b>"), s1, s2);

        g_free (s1);
        g_free (s2);
        return msg;

    } else if (files > 0) {

        return g_strdup_printf (ngettext ("You have selected %d file", "You have selected %d files", files), files);

    } else if (folders > 0) {

        return g_strdup_printf (
            ngettext ("You have selected %d folder", "You have selected %d folders", folders), folders);

    } else {
        g_assert_not_reached ();
        return NULL; /* to fix warnings */
    }
}

/* Callback for main option buttons */
static void
seperate_toggled (GtkWidget *widget, GtkWidget *package)
{
    gtk_widget_set_sensitive (package,
        !gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)));
}

/* Build the multiple file dialog */
static SeahorseWidget*
prepare_dialog (FilesCtx *ctx, guint nfolders, guint nfiles, GFileInfo *info, gchar* ext)
{
    SeahorseWidget *swidget;
    const gchar* pkg;
    GtkWidget *tog;
    GtkWidget *w;
    GtkWidget *combo;
    gchar *msg, *display;
    gboolean sep;
    gint i;
    GtkCellRenderer *cell;
    GtkTreeModel *store;
	FRFileType *save_type_list;

    g_assert (info);

    swidget = seahorse_widget_new ("multi-encrypt", NULL);
    g_return_val_if_fail (swidget != NULL, NULL);

    /* The main 'selected' message */
    msg = make_message (nfolders, nfiles);
    w = GTK_WIDGET (seahorse_widget_get_widget (swidget, "message"));
    gtk_label_set_markup (GTK_LABEL(w), msg);
    g_free (msg);

    /* Setup the remote or local messages */
    w = GTK_WIDGET (seahorse_widget_get_widget (swidget,
            ctx->remote ? "remote-options" : "local-options"));
    gtk_widget_show (w);

    tog = GTK_WIDGET (seahorse_widget_get_widget (swidget, "do-separate"));

    if (ctx->remote) {
        /* Always use the seperate option */
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (tog), TRUE);

    /* The local stuff */
    } else {

        sep = g_settings_get_boolean (mate_seahorse_tool_settings, "separate-files");

        /* Setup the package */
        w = GTK_WIDGET (seahorse_widget_get_widget (swidget, "package-name"));
        display = g_strdup (g_file_info_get_display_name (info));
        pkg = seahorse_util_uri_split_last (display);
        gtk_entry_set_text (GTK_ENTRY (w), pkg);
        g_free (display);

        /* Setup the URI combo box */
        combo = GTK_WIDGET (seahorse_widget_get_widget (swidget, "package-extension"));
        store = GTK_TREE_MODEL (gtk_list_store_new (1, G_TYPE_STRING));
        gtk_combo_box_set_model (GTK_COMBO_BOX (combo), store);
        g_object_unref (store);

        gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo));
        cell = gtk_cell_renderer_text_new ();
        gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
        gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
                                        "text", 0,
                                        NULL);

        compute_supported_archive_types ();

        save_type_list = save_type;

        for (i = 0; save_type_list[i] != FR_FILE_TYPE_NULL; i++) {
		gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo),
		                                file_type_desc[save_type_list[i]].ext);
		    if (strcmp(ext, file_type_desc[save_type_list[i]].ext) == 0)
		        gtk_combo_box_set_active (GTK_COMBO_BOX (combo), i);
    	}

        if(sep == FALSE) {
            gtk_widget_grab_focus (w);
            gtk_editable_select_region (GTK_EDITABLE (w), 0, strlen (pkg));
        }

        /* Setup the main radio buttons */
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (tog), sep);
        g_signal_connect (tog, "toggled", G_CALLBACK (seperate_toggled), w);
        seperate_toggled (tog, w);
    }

    return swidget;
}

/* Get the package name and selection */
static gchar*
get_results (SeahorseWidget *swidget)
{
    const gchar* name;
    const gchar* t;
    gchar *full_name, *ext;
    GtkWidget *w;
    gboolean sep;

    w = GTK_WIDGET (seahorse_widget_get_widget (swidget, "do-separate"));
    sep = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w));
    g_settings_set_boolean (mate_seahorse_tool_settings, "separate-files", sep);

    /* no packaging */
    if(!sep) {

        w = GTK_WIDGET (seahorse_widget_get_widget (swidget, "package-name"));
        name = gtk_entry_get_text (GTK_ENTRY (w));

        w = GTK_WIDGET (seahorse_widget_get_widget (swidget, "package-extension"));
        ext = gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT (w));

        /* No paths */
        t = strrchr(name, '/');
        name = t ? ++t : name;

        /* If someone goes out of their way to delete the file name,
         * we're simply unimpressed and put back a default. */
        if(name[0] == 0)
            name = "encrypted-package";

        full_name = g_strdup_printf("%s%s", name, ext);

        /* Save the extension */
        g_settings_set_string (mate_seahorse_tool_settings, "package-extension", ext);

        return full_name;
    }

    return NULL;
}

static gboolean
step_process_multiple (FilesCtx *ctx,
                       const gchar **orig_uris,
                       GError **err)
{
    SeahorseWidget *swidget;
    gboolean done = FALSE;
    FileInfo *pkg_info = NULL;
    gchar *package = NULL;
    gchar *ext;
    GFile *file, *parent;
    gboolean ok = FALSE;
    GtkWidget *dlg;
    guint nfolders, nfiles;
    gchar *uris[2];
    gchar *uri;
    GList *l;

    g_assert (err && !*err);

    for (l = ctx->finfos, nfolders = nfiles = 0; l; l = g_list_next (l)) {
        FileInfo *finfo = (FileInfo*)l->data;
        if (g_file_info_get_file_type (finfo->info) == G_FILE_TYPE_DIRECTORY)
            ++nfolders;
        else
            ++nfiles;
    }

    /* In the case of one or less files, no dialog */
    if(nfolders == 0 && nfiles <= 1)
        return TRUE;

    /* The package extension */
    if ((ext = g_settings_get_string (mate_seahorse_tool_settings, "package-extension")) == NULL)
        ext = g_strdup (".zip"); /* Yes this happens when the schema isn't installed */

    /* Figure out a good URI for our package */
    for (l = ctx->finfos; l; l = g_list_next (l)) {
        if (l->data) {
            pkg_info = (FileInfo*)(l->data);
            break;
        }
    }

    /* This sets up but doesn't run the dialog */
    swidget = prepare_dialog (ctx, nfolders, nfiles, pkg_info->info, ext);

    g_free (ext);

    dlg = seahorse_widget_get_toplevel (swidget);

    /* Inhibit popping up of progress dialog */
    mate_seahorse_tool_progress_block (TRUE);

    while (!done) {
        switch (gtk_dialog_run (GTK_DIALOG (dlg)))
        {
        case GTK_RESPONSE_HELP:
            /* TODO: Implement help */
            break;

        case GTK_RESPONSE_OK:
            package = get_results (swidget);
            ok = TRUE;
            /* Fall through */

        default:
            done = TRUE;
            break;
        }
    }

    /* Let progress dialog pop up */
    mate_seahorse_tool_progress_block (FALSE);

    seahorse_widget_destroy (swidget);

    /* Cancelled */
    if (!ok)
        return FALSE;

    /* No package was selected? */
    if (!package)
        return TRUE;

    /* A package was selected */

    /* Make a new path based on the first uri */
    parent = g_file_get_parent (pkg_info->file);
    if (!parent)
    	parent = pkg_info->file;
    file = g_file_get_child_for_display_name (parent, package, err);
    if (!file)
	    return FALSE;

    uri = g_file_get_uri (file);
    g_return_val_if_fail (uri, FALSE);
    g_object_unref (file);

    if (!seahorse_util_uris_package (uri, orig_uris)) {
        g_free (uri);
        return FALSE;
    }

    /* Free all file info */
    g_list_foreach (ctx->finfos, (GFunc)free_file_info, NULL);
    g_list_free (ctx->finfos);
    ctx->finfos = NULL;

    /* Reload up the new file, as what to encrypt */
    uris[0] = uri;
    uris[1] = NULL;
    ok = step_check_uris (ctx, (const gchar**)uris, err);
    g_free (uri);

    return ok;
}

/* -----------------------------------------------------------------------------
 * EXPAND STEP
 */

static gboolean
visit_enumerator (FilesCtx *ctx, GFile *parent, GFileEnumerator *enumerator, GError **err)
{
    GFileEnumerator *children;
    gboolean ret = TRUE;
    GFileInfo *info;
    FileInfo *finfo;
    GFile *file;

    for (;;) {
	if (!mate_seahorse_tool_progress_check ()) {
            ret = FALSE;
            break;
	}

	info = g_file_enumerator_next_file (enumerator, NULL, err);
        if (!info) {
            if (err && *err)
                ret = FALSE;
            break;
        }

        file = g_file_resolve_relative_path (parent, g_file_info_get_name (info));
        g_return_val_if_fail (file, FALSE);

        /* Enumerate child directories */
        if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) {
                children = g_file_enumerate_children (file, FILE_ATTRIBUTES,
                                                      G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
                                                      NULL, err);
                if (!enumerator) {
                    ret = FALSE;
                    break;
                }

                ret = visit_enumerator (ctx, file, children, err);
                if (!ret)
                    break;

        /* A file, add it */
        } else {

            finfo = g_new0 (FileInfo, 1);
            finfo->info = info;
            finfo->file = file;
            finfo->uri = g_file_get_uri (file);
            g_object_ref (info);
            g_object_ref (file);

            ctx->total += g_file_info_get_size (info);
            ctx->finfos = g_list_append (ctx->finfos, finfo);
        }

        g_object_unref (file);
        file = NULL;
    }

    if (file != NULL)
        g_object_unref (file);

    g_object_unref (enumerator);
    return ret;
}

static gboolean
step_expand_uris (FilesCtx *ctx,
                  GError **err)
{
    GFileEnumerator *enumerator;
    gboolean ret = TRUE;
    FileInfo *finfo;
    GList *l;

    g_assert (err && !*err);

    for (l = ctx->finfos; l; l = g_list_next (l)) {

        if (!mate_seahorse_tool_progress_check ()) {
            ret = FALSE;
            break;
        }

        finfo = (FileInfo*)(l->data);
        if (!finfo || !finfo->uri)
            continue;

        if (g_file_info_get_file_type (finfo->info) == G_FILE_TYPE_DIRECTORY) {

            enumerator = g_file_enumerate_children (finfo->file, FILE_ATTRIBUTES,
                                                    G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
                                                    NULL, err);
            if (!enumerator)
                return FALSE;

            if (!visit_enumerator (ctx, finfo->file, enumerator, err))
                return FALSE;

            /* We don't actually do operations on the dirs */
            free_file_info (finfo, NULL);
            l->data = NULL;
        }
    }

    return ret;
}

/* -----------------------------------------------------------------------------
 * ACTUAL OPERATION STEP
 */

static void
progress_cb (gpgme_data_t data, goffset pos, FilesCtx *ctx)
{
    gdouble total, done, size, portion;
    goffset fsize;

    g_assert (ctx && ctx->cur);

    total = ctx->total > ONE_GIGABYTE ? ctx->total / 1000 : ctx->total;
    done = ctx->total > ONE_GIGABYTE ? ctx->done / 1000 : ctx->done;
    fsize = g_file_info_get_size (ctx->cur->info);
    size = ctx->total > ONE_GIGABYTE ? fsize / 1000 : fsize;
    portion = ctx->total > ONE_GIGABYTE ? pos / 1000 : pos;

    total = total <= 0 ? 1 : total;
    size = size <= 0 ? 1 : size;

    /* The cancel check is done elsewhere */
    mate_seahorse_tool_progress_update ((done / total) + ((size / total) * (portion / size)),
                                   seahorse_util_uri_get_last (g_file_info_get_display_name (ctx->cur->info)));
}

static gboolean
step_operation (FilesCtx *ctx,
                SeahorseToolMode *mode,
                GError **err)
{
    SeahorsePGPOperation *pop = NULL;
    gpgme_data_t data = NULL;
    gboolean ret = FALSE;

    SeahorseOperation *op;
    FileInfo *finfo;
    GList *l;
    gchar *filename;

    /* Reset our done counter */
    ctx->done = 0;

    for (l = ctx->finfos; l; l = g_list_next (l)) {

        finfo = (FileInfo*)l->data;
        if (!finfo || !finfo->file)
            continue;

        ctx->cur = finfo;

        /* A new operation for each context */
        pop = seahorse_pgp_operation_new (NULL);
        op = SEAHORSE_OPERATION (pop);

        data = seahorse_vfs_data_create_full (finfo->file, SEAHORSE_VFS_READ,
                                              (SeahorseVfsProgressCb)progress_cb,
                                              ctx, err);
        if (!data)
            goto finally;

        /* Inhibit popping up of progress dialog */
        mate_seahorse_tool_progress_block (TRUE);

        /* Embed filename during encryption */
        if (mode_encrypt)
        {
            filename = g_file_get_basename (finfo->file);
            gpgme_data_set_file_name (data, filename);
            g_free (filename);
        }

        /* The start callback */
        if (mode->startcb) {
            if (!(mode->startcb) (mode, finfo->uri, data, pop, err))
                goto finally;
        }

        /* Let progress dialog pop up */
        mate_seahorse_tool_progress_block (FALSE);

        /* Run until the operation completes */
        seahorse_util_wait_until ((!seahorse_operation_is_running (op) ||
                                   !mate_seahorse_tool_progress_check ()));

        /* If cancel then reflect that */
        if (seahorse_operation_is_running (op)) {
            seahorse_operation_cancel (op);
            goto finally;
        }

        if (!seahorse_operation_is_successful (op)) {
            seahorse_operation_copy_error (op, err);
            goto finally;
        }

        /* The done callback */
        if (mode->donecb) {
            if (!(mode->donecb) (mode, finfo->uri, data, pop, err))
                goto finally;
        }

        ctx->done += g_file_info_get_size (finfo->info);
        ctx->cur = NULL;

        g_object_unref (pop);
        pop = NULL;

        gpgme_data_release (data);
        data = NULL;
    }

    mate_seahorse_tool_progress_update (1.0, "");
    ret = TRUE;

finally:
    if (pop)
        g_object_unref (pop);
    if (data)
        gpgme_data_release (data);

    return ret;
}

/* -----------------------------------------------------------------------------
 * MAIN COORDINATION
 */

int
mate_seahorse_tool_files_process (SeahorseToolMode *mode, const gchar **uris)
{
    const gchar *errdesc = NULL;
    GError *err = NULL;
    FilesCtx ctx;
    int ret = 1;

    memset (&ctx, 0, sizeof (ctx));

    /* Start progress bar */
    mate_seahorse_tool_progress_start (mode->title);
    if (!mate_seahorse_tool_progress_update (-1, _("Preparing...")))
        goto finally;

    /*
     * 1. Check all the uris, format them properly, and figure
     * out what they are (ie: files, directories, size etc...)
     */

    if (!step_check_uris (&ctx, uris, &err)) {
        errdesc = _("Couldn't list files");
        goto finally;
    }

    /*
     * 2. Prompt user and see what they want to do
     * with multiple files.
     */
    if (mode->package) {
        if (!step_process_multiple (&ctx, uris, &err)) {
            errdesc = _("Couldn't package files");
            goto finally;
        }
    }

    if (!mate_seahorse_tool_progress_check ())
        goto finally;

    /*
     * 2. Expand remaining URIs, so we know what we're
     * dealing with. This also gets file size info.
     */
    if (!step_expand_uris (&ctx, &err)) {
        errdesc = _("Couldn't list files");
        goto finally;
    }

    if (!mate_seahorse_tool_progress_update (0.0, NULL))
        goto finally;

    /*
     * 3. Now execute enc operation on every file
     */
    if (!step_operation (&ctx, mode, &err)) {
        errdesc = mode->errmsg;
        goto finally;
    }

    mate_seahorse_tool_progress_update (1.0, NULL);
    ret = 0;

finally:

    mate_seahorse_tool_progress_stop ();

    if (err) {
        seahorse_util_handle_error (err, errdesc, ctx.cur ?
                                    g_file_info_get_display_name (ctx.cur->info) : "");
    }

    if (ctx.finfos) {
        g_list_foreach (ctx.finfos, (GFunc)free_file_info, NULL);
        g_list_free (ctx.finfos);
    }

    return ret;
}