summaryrefslogtreecommitdiff
path: root/src/fr-init.c
blob: 33bdf53fcf3667e28f9862515ec5695067d50f78 (plain)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 *  Engrampa
 *
 *  Copyright (C) 2010 Free Software Foundation, Inc.
 *
 *  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 <stdlib.h>
#include <glib/gi18n.h>
#include "file-data.h"
#include "file-utils.h"
#include "glib-utils.h"
#include "fr-command.h"
#include "fr-command-ace.h"
#include "fr-command-alz.h"
#include "fr-command-ar.h"
#include "fr-command-arj.h"
#include "fr-command-cfile.h"
#include "fr-command-cpio.h"
#include "fr-command-dpkg.h"
#include "fr-command-iso.h"
#include "fr-command-jar.h"
#include "fr-command-lha.h"
#include "fr-command-rar.h"
#include "fr-command-rpm.h"
#include "fr-command-tar.h"
#if HAVE_JSON_GLIB
  #include "fr-command-unarchiver.h"
#endif
#include "fr-command-unstuff.h"
#include "fr-command-zip.h"
#include "fr-command-zoo.h"
#include "fr-command-7z.h"
#include "fr-command-lrzip.h"
#include "fr-init.h"
#include "fr-process.h"
#include "fr-stock.h"
#include "fr-window.h"
#include "typedefs.h"
#include "preferences.h"


/* The capabilities are computed automatically in
 * compute_supported_archive_types() so it's correct to initialize to 0 here. */
FrMimeTypeDescription mime_type_desc[] = {
	{ "application/vnd.debian.binary-package",                    ".deb",      N_("Debian package (.deb)"), 0 },
	{ "application/vnd.debian.binary-package",                    ".udeb",     N_("Debian package (.udeb)"), 0 },
	{ "application/vnd.ms-cab-compressed",                        ".cab",      N_("Cabinet (.cab)"), 0 },
	{ "application/vnd.oasis.opendocument.presentation",          ".odp",      N_("OpenDocument Presentation (.odp)"), 0 },
	{ "application/vnd.oasis.opendocument.spreadsheet",           ".ods",      N_("OpenDocument Spreadsheet (.ods)"), 0 },
	{ "application/vnd.oasis.opendocument.text",                  ".odt",      N_("OpenDocument Text (.odt)"), 0 },
	{ "application/vnd.oasis.opendocument.presentation-template", ".otp",      N_("OpenDocument Presentation Template (.otp)"), 0 },
	{ "application/vnd.oasis.opendocument.spreadsheet-template",  ".ots",      N_("OpenDocument Spreadsheet Template (.ots)"), 0 },
	{ "application/vnd.oasis.opendocument.text-template",         ".ott",      N_("OpenDocument Text Template (.ott)"), 0 },
	{ "application/x-7z-compressed",                              ".7z",       N_("7-Zip (.7z)"), 0 },
	{ "application/x-7z-compressed-tar",                          ".tar.7z",   N_("Tar compressed with 7z (.tar.7z)"), 0 },
	{ "application/x-ace",                                        ".ace",      N_("Ace (.ace)"), 0 },
	{ "application/x-alz",                                        ".alz",      N_("ALZip archive (.alz)"), 0 },
	{ "application/x-archive",                                    ".a",        N_("Ar (.a)"), 0 },
	{ "application/x-archive",                                    ".ar",       N_("Ar (.ar)"), 0 },
	{ "application/x-arj",                                        ".arj",      N_("Arj (.arj)"), 0 },
	{ "application/x-brotli",                                     ".br",       N_("brotli (.br)"), 0 },
	{ "application/x-brotli-compressed-tar",                      ".tar.br",   N_("Tar compressed with brotli (.tar.br)"), 0 },
	{ "application/x-bzip",                                       ".bz2",      N_("Bzip2 compressed file (.bz2)"), 0 },
	{ "application/x-bzip-compressed-tar",                        ".tar.bz2",  N_("Tar compressed with bzip2 (.tar.bz2)"), 0 },
	{ "application/x-bzip1",                                      ".bz",       N_("Bzip compressed file (.bz)"), 0 },
	{ "application/x-bzip1-compressed-tar",                       ".tar.bz",   N_("Tar compressed with bzip (.tar.bz)"), 0 },
	{ "application/x-cbr",                                        ".cbr",      N_("Rar Archived Comic Book (.cbr)"), 0 },
	{ "application/x-cbz",                                        ".cbz",      N_("Zip Archived Comic Book (.cbz)"), 0 },
	{ "application/x-cd-image",                                   ".iso",      N_("Disc Image File (.iso)"), 0 },
	{ "application/x-compress",                                   ".Z",        N_("Unix Compressed Archive file (.Z)"), 0 },
	{ "application/x-compressed-tar",                             ".tar.gz",   N_("Tar compressed with gzip (.tar.gz)"), 0 },
	{ "application/x-cpio",                                       ".cpio",     N_("Unix CPIO Archive (.cpio)"), 0 },
	{ "application/x-ear",                                        ".ear",      N_("Ear (.ear)"), 0 },
	{ "application/x-gzip",                                       ".gz",       N_("GZip compressed file (.gz)"), 0 },
	{ "application/x-java-archive",                               ".jar",      N_("Jar (.jar)"), 0 },
	{ "application/x-lzh-compressed",                             ".lzh",      N_("Lha (.lzh)"), 0 },
	{ "application/x-lrzip",                                      ".lrz",      N_("Lrzip (.lrz)"), 0},
	{ "application/x-lrzip-compressed-tar",                       ".tar.lrz",  N_("Tar compressed with lrzip (.tar.lrz)"), 0 },
	{ "application/x-lzip",                                       ".lz",       N_("LZip compressed file (.lz)"), 0 },
	{ "application/x-lzip-compressed-tar",                        ".tar.lz",   N_("Tar compressed with lzip (.tar.lz)"), 0 },
	{ "application/x-lzma",                                       ".lzma",     N_("LZMA compressed file (.lzma)"), 0 },
	{ "application/x-lzma-compressed-tar",                        ".tar.lzma", N_("Tar compressed with lzma (.tar.lzma)"), 0 },
	{ "application/x-lzop",                                       ".lzo",      N_("LZO compressed file (.lzo)"), 0 },
	{ "application/x-lzop-compressed-tar",                        ".tar.lzo",  N_("Tar compressed with lzop (.tar.lzo)"), 0 },
	{ "application/x-ms-dos-executable",                          ".exe",      N_("Self-extracting zip (.exe)"), 0 },
	{ "application/x-ms-wim",                                     ".wim",      N_("Windows Imaging Format (.wim)"), 0 },
	{ "application/x-rar",                                        ".rar",      N_("Rar (.rar)"), 0 },
	{ "application/x-rpm",                                        ".rpm",      N_("Red Hat Package Manager file (.rpm)"), 0 },
	{ "application/x-rzip",                                       ".rz",       N_("RZip compressed file (.rz)"), 0 },
	{ "application/x-stuffit",                                    ".sit",      N_("StuffIt Archive (.sit)"), 0 },
	{ "application/x-tar",                                        ".tar",      N_("Tar uncompressed (.tar)"), 0 },
	{ "application/x-tarz",                                       ".tar.Z",    N_("Tar compressed with compress (.tar.Z)"), 0 },
	{ "application/x-war",                                        ".war",      N_("War (.war)"), 0 },
	{ "application/x-xz",                                         ".xz",       N_("Xz (.xz)"), 0 },
	{ "application/x-xz-compressed-tar",                          ".tar.xz",   N_("Tar compressed with xz (.tar.xz)"), 0 },
	{ "application/x-zoo",                                        ".zoo",      N_("Zoo (.zoo)"), 0 },
	{ "application/zip",                                          ".zip",      N_("Zip (.zip)"), 0 },
	{ "application/zstd",                                         ".zst",      N_("Zstandard (.zst)"), 0 },
	{ NULL, NULL, NULL, 0 }
};

FrExtensionType file_ext_type[] = {
	{ ".7z", "application/x-7z-compressed" },
	{ ".a", "application/x-archive" },
	{ ".ace", "application/x-ace" },
	{ ".alz", "application/x-alz" },
	{ ".ar", "application/x-archive" },
	{ ".arj", "application/x-arj" },
	{ ".bin", "application/x-stuffit" },
	{ ".br", "application/x-brotli" },
	{ ".bz", "application/x-bzip" },
	{ ".bz2", "application/x-bzip" },
	{ ".cab", "application/vnd.ms-cab-compressed" },
	{ ".cbr", "application/x-cbr" },
	{ ".cbz", "application/x-cbz" },
	{ ".cpio", "application/x-cpio" },
	{ ".deb", "application/vnd.debian.binary-package" },
	{ ".ear", "application/x-ear" },
	{ ".exe", "application/x-ms-dos-executable" },
	{ ".gz", "application/x-gzip" },
	{ ".iso", "application/x-cd-image" },
	{ ".jar", "application/x-java-archive" },
	{ ".lha", "application/x-lzh-compressed" },
	{ ".lrz", "application/x-lrzip" },
	{ ".lzh", "application/x-lzh-compressed" },
	{ ".lz", "application/x-lzip" },
	{ ".lzma", "application/x-lzma" },
	{ ".lzo", "application/x-lzop" },
	{ ".odp", "application/vnd.oasis.opendocument.presentation" },
	{ ".ods", "application/vnd.oasis.opendocument.spreadsheet" },
	{ ".odt", "application/vnd.oasis.opendocument.text" },
	{ ".otp", "application/vnd.oasis.opendocument.presentation-template" },
	{ ".ots", "application/vnd.oasis.opendocument.spreadsheet-template" },
	{ ".ott", "application/vnd.oasis.opendocument.text-template" },
	{ ".rar", "application/x-rar" },
	{ ".rpm", "application/x-rpm" },
	{ ".rz", "application/x-rzip" },
	{ ".sit", "application/x-stuffit" },
	{ ".swm", "application/x-ms-wim" },
	{ ".tar", "application/x-tar" },
	{ ".tar.7z", "application/x-7z-compressed-tar" },
	{ ".tar.br", "application/x-brotli-compressed-tar" },
	{ ".tar.bz", "application/x-bzip-compressed-tar" },
	{ ".tar.bz2", "application/x-bzip-compressed-tar" },
	{ ".tar.gz", "application/x-compressed-tar" },
	{ ".tar.lrz", "application/x-lrzip-compressed-tar" },
	{ ".tar.lz", "application/x-lzip-compressed-tar" },
	{ ".tar.lzma", "application/x-lzma-compressed-tar" },
	{ ".tar.lzo", "application/x-lzop-compressed-tar" },
	{ ".tar.xz", "application/x-xz-compressed-tar" },
	{ ".tar.Z", "application/x-tarz" },
	{ ".taz", "application/x-tarz" },
	{ ".tbz", "application/x-bzip-compressed-tar" },
	{ ".tbz2", "application/x-bzip-compressed-tar" },
	{ ".tgz", "application/x-compressed-tar" },
	{ ".tlz", "application/x-lzip-compressed-tar" },
	{ ".txz", "application/x-xz-compressed-tar" },
	{ ".tzma", "application/x-lzma-compressed-tar" },
	{ ".tzo", "application/x-lzop-compressed-tar" },
	{ ".udeb", "application/vnd.debian.binary-package" },
	{ ".war", "application/x-war" },
	{ ".wim", "application/x-ms-wim" },
	{ ".xz", "application/x-xz" },
	{ ".z", "application/x-gzip" },
	{ ".Z", "application/x-compress" },
	{ ".zip", "application/zip" },
	{ ".zoo", "application/x-zoo" },
	{ ".zst", "application/zstd" },
	{ NULL, NULL }
};


GList        *CommandList;
gint          ForceDirectoryCreation;
GHashTable   *ProgramsCache;
GPtrArray    *Registered_Commands;
int           single_file_save_type[64];
int           save_type[64];
int           open_type[64];
int           create_type[64];


static void
migrate_options_directory (void)
{
	char *old_directory_path;
	GFile *old_directory;
	GFile *new_directory;

	old_directory_path = get_home_relative_path (".config/mate/engrampa/options");
	old_directory = g_file_new_for_path (old_directory_path);
	new_directory = get_user_config_subdirectory (ADD_FOLDER_OPTIONS_DIR, FALSE);
	if (g_file_query_exists (old_directory, NULL) && ! g_file_query_exists (new_directory, NULL)) {
		GFile *parent;

		parent = g_file_get_parent (new_directory);
		if (make_directory_tree (parent, 0700, NULL))
			g_file_move (old_directory, new_directory, 0, NULL, NULL, NULL, NULL);

		g_object_unref (parent);
	}

	g_object_unref (new_directory);
	g_object_unref (old_directory);
	g_free (old_directory_path);
}


/* -- FrRegisteredCommand -- */


static FrRegisteredCommand *
fr_registered_command_new (GType command_type)
{
	FrRegisteredCommand  *reg_com;
	FrCommand            *command;
	const char          **mime_types;
	int                   i;

	reg_com = g_new0 (FrRegisteredCommand, 1);
	reg_com->ref = 1;
	reg_com->type = command_type;
	reg_com->caps = g_ptr_array_new ();
	reg_com->packages = g_ptr_array_new ();

	command = (FrCommand*) g_object_new (reg_com->type, NULL);
	mime_types = fr_command_get_mime_types (command);
	for (i = 0; mime_types[i] != NULL; i++) {
		const char         *mime_type;
		FrMimeTypeCap      *cap;
		FrMimeTypePackages *packages;

		mime_type = get_static_string (mime_types[i]);

		cap = g_new0 (FrMimeTypeCap, 1);
		cap->mime_type = mime_type;
		cap->current_capabilities = fr_command_get_capabilities (command, mime_type, TRUE);
		cap->potential_capabilities = fr_command_get_capabilities (command, mime_type, FALSE);
		g_ptr_array_add (reg_com->caps, cap);

		packages = g_new0 (FrMimeTypePackages, 1);
		packages->mime_type = mime_type;
		packages->packages = fr_command_get_packages (command, mime_type);
		g_ptr_array_add (reg_com->packages, packages);
	}

	g_object_unref (command);

	return reg_com;
}


G_GNUC_UNUSED static void
fr_registered_command_ref (FrRegisteredCommand *reg_com)
{
	reg_com->ref++;
}


static void
fr_registered_command_unref (FrRegisteredCommand *reg_com)
{
	if (--(reg_com->ref) != 0)
		return;

	g_ptr_array_foreach (reg_com->caps, (GFunc) g_free, NULL);
	g_ptr_array_free (reg_com->caps, TRUE);
	g_free (reg_com);
}


static FrCommandCaps
fr_registered_command_get_capabilities (FrRegisteredCommand *reg_com,
				        const char          *mime_type)
{
	guint i;

	for (i = 0; i < reg_com->caps->len; i++) {
		FrMimeTypeCap *cap;

		cap = g_ptr_array_index (reg_com->caps, i);
		if (strcmp (mime_type, cap->mime_type) == 0)
			return cap->current_capabilities;
	}

	return FR_COMMAND_CAN_DO_NOTHING;
}


static FrCommandCaps
fr_registered_command_get_potential_capabilities (FrRegisteredCommand *reg_com,
						  const char          *mime_type)
{
	guint i;

	if (mime_type == NULL)
		return FR_COMMAND_CAN_DO_NOTHING;

	for (i = 0; i < reg_com->caps->len; i++) {
		FrMimeTypeCap *cap;

		cap = g_ptr_array_index (reg_com->caps, i);
		if ((cap->mime_type != NULL) && (strcmp (mime_type, cap->mime_type) == 0))
			return cap->potential_capabilities;
	}

	return FR_COMMAND_CAN_DO_NOTHING;
}


static void
register_command (GType command_type)
{
	if (Registered_Commands == NULL)
		Registered_Commands = g_ptr_array_sized_new (5);
	g_ptr_array_add (Registered_Commands, fr_registered_command_new (command_type));
}


G_GNUC_UNUSED static gboolean
unregister_command (GType command_type)
{
	guint i;

	for (i = 0; i < Registered_Commands->len; i++) {
		FrRegisteredCommand *command;

		command = g_ptr_array_index (Registered_Commands, i);
		if (command->type == command_type) {
			g_ptr_array_remove_index (Registered_Commands, i);
			fr_registered_command_unref (command);
			return TRUE;
		}
	}

	return FALSE;
}


static void
register_commands (void)
{
	/* The order here is important. Commands registered earlier have higher
	 * priority.  However commands that can read and write a file format
	 * have higher priority over commands that can only read the same
	 * format, regardless of the registration order. */

	register_command (FR_TYPE_COMMAND_TAR);
	register_command (FR_TYPE_COMMAND_CFILE);
	register_command (FR_TYPE_COMMAND_RAR);
	register_command (FR_TYPE_COMMAND_7Z);
	register_command (FR_TYPE_COMMAND_DPKG);

	register_command (FR_TYPE_COMMAND_ACE);
	register_command (FR_TYPE_COMMAND_ALZ);
	register_command (FR_TYPE_COMMAND_AR);
	register_command (FR_TYPE_COMMAND_ARJ);
	register_command (FR_TYPE_COMMAND_CPIO);
	register_command (FR_TYPE_COMMAND_ISO);
	register_command (FR_TYPE_COMMAND_JAR);
	register_command (FR_TYPE_COMMAND_LHA);
	register_command (FR_TYPE_COMMAND_RPM);
	register_command (FR_TYPE_COMMAND_UNSTUFF);
	register_command (FR_TYPE_COMMAND_ZIP);
	register_command (FR_TYPE_COMMAND_LRZIP);
	register_command (FR_TYPE_COMMAND_ZOO);
#if HAVE_JSON_GLIB
	register_command (FR_TYPE_COMMAND_UNARCHIVER);
#endif
}


GType
get_command_type_from_mime_type (const char    *mime_type,
				 FrCommandCaps  requested_capabilities)
{
	guint i;

	if (mime_type == NULL)
		return 0;

	for (i = 0; i < Registered_Commands->len; i++) {
		FrRegisteredCommand *command;
		FrCommandCaps        capabilities;

		command = g_ptr_array_index (Registered_Commands, i);
		capabilities = fr_registered_command_get_capabilities (command, mime_type);

		/* the command must support all the requested capabilities */
		if (((capabilities ^ requested_capabilities) & requested_capabilities) == 0)
			return command->type;
	}

	return 0;
}


GType
get_preferred_command_for_mime_type (const char    *mime_type,
				     FrCommandCaps  requested_capabilities)
{
	guint i;

	for (i = 0; i < Registered_Commands->len; i++) {
		FrRegisteredCommand *command;
		FrCommandCaps        capabilities;

		command = g_ptr_array_index (Registered_Commands, i);
		capabilities = fr_registered_command_get_potential_capabilities (command, mime_type);

		/* the command must support all the requested capabilities */
		if (((capabilities ^ requested_capabilities) & requested_capabilities) == 0)
			return command->type;
	}

	return 0;
}


void
update_registered_commands_capabilities (void)
{
	guint i;

	g_hash_table_remove_all (ProgramsCache);

	for (i = 0; i < Registered_Commands->len; i++) {
		FrRegisteredCommand *reg_com;
		FrCommand           *command;
		guint                j;

		reg_com = g_ptr_array_index (Registered_Commands, i);
		command = (FrCommand*) g_object_new (reg_com->type, NULL);
		for (j = 0; j < reg_com->caps->len; j++) {
			FrMimeTypeCap *cap = g_ptr_array_index (reg_com->caps, j);

			cap->current_capabilities = fr_command_get_capabilities (command, cap->mime_type, TRUE);
			cap->potential_capabilities = fr_command_get_capabilities (command, cap->mime_type, FALSE);
		}

		g_object_unref (command);
	}
}


const char *
get_mime_type_from_extension (const char *ext)
{
	int i;

	if (ext == NULL)
		return NULL;

	for (i = G_N_ELEMENTS (file_ext_type) - 1; i >= 0; i--) {
		if (file_ext_type[i].ext == NULL)
			continue;
		if (strcasecmp (ext, file_ext_type[i].ext) == 0)
			return get_static_string (file_ext_type[i].mime_type);
	}

	return NULL;
}


const char *
get_archive_filename_extension (const char *filename)
{
	const char *ext;
	int         i;

	if (filename == NULL)
		return NULL;

	ext = get_file_extension (filename);
	if (ext == NULL)
		return NULL;

	for (i = G_N_ELEMENTS (file_ext_type) - 1; i >= 0; i--) {
		if (file_ext_type[i].ext == NULL)
			continue;
		if (strcasecmp (ext, file_ext_type[i].ext) == 0)
			return ext;
	}

	return NULL;
}


int
get_mime_type_index (const char *mime_type)
{
	int i;

	for (i = 0; mime_type_desc[i].mime_type != NULL; i++)
		if (strcmp (mime_type_desc[i].mime_type, mime_type) == 0)
			return i;
	return -1;
}


static void
add_if_non_present (int *a,
	            int *n,
	            int  o)
{
	int i;

	for (i = 0; i < *n; i++) {
		if (a[i] == o)
			return;
	}
	a[*n] = o;
	*n = *n + 1;
}


static int
cmp_mime_type_by_extension (const void *p1,
			    const void *p2)
{
	int i1 = * (int*) p1;
	int i2 = * (int*) p2;

	return strcmp (mime_type_desc[i1].default_ext, mime_type_desc[i2].default_ext);
}


static int
cmp_mime_type_by_description (const void *p1,
			      const void *p2)
{
	int i1 = * (int*) p1;
	int i2 = * (int*) p2;

	return g_utf8_collate (_(mime_type_desc[i1].name), _(mime_type_desc[i2].name));
}


static void
sort_mime_types (int *a,
                 int(*compar)(const void *, const void *))
{
	int n = 0;

	while (a[n] != -1)
		n++;
	qsort (a, n, sizeof (int), compar);
}


void
sort_mime_types_by_extension (int *a)
{
	sort_mime_types (a, cmp_mime_type_by_extension);
}


void
sort_mime_types_by_description (int *a)
{
	sort_mime_types (a, cmp_mime_type_by_description);
}


static void
compute_supported_archive_types (void)
{
	int sf_i = 0, s_i = 0, o_i = 0, c_i = 0;
	guint i;

	for (i = 0; i < Registered_Commands->len; i++) {
		FrRegisteredCommand *reg_com;
		guint                j;

		reg_com = g_ptr_array_index (Registered_Commands, i);
		for (j = 0; j < reg_com->caps->len; j++) {
			FrMimeTypeCap *cap;
			int            idx;

			cap = g_ptr_array_index (reg_com->caps, j);
			idx = get_mime_type_index (cap->mime_type);
			if (idx < 0) {
				g_warning ("mime type not recognized: %s", cap->mime_type);
				continue;
			}
			mime_type_desc[idx].capabilities |= cap->current_capabilities;
			if (cap->current_capabilities & FR_COMMAND_CAN_READ)
				add_if_non_present (open_type, &o_i, idx);
			if (cap->current_capabilities & FR_COMMAND_CAN_WRITE) {
				if (cap->current_capabilities & FR_COMMAND_CAN_ARCHIVE_MANY_FILES) {
					add_if_non_present (save_type, &s_i, idx);
					if (cap->current_capabilities & FR_COMMAND_CAN_WRITE)
						add_if_non_present (create_type, &c_i, idx);
				}
				add_if_non_present (single_file_save_type, &sf_i, idx);
			}
		}
	}

	open_type[o_i] = -1;
	save_type[s_i] = -1;
	single_file_save_type[sf_i] = -1;
	create_type[c_i] = -1;
}


static gboolean initialized = FALSE;

void
initialize_data (void)
{
	if (initialized)
		return;
	initialized = TRUE;

	ProgramsCache = g_hash_table_new_full (g_str_hash,
					       g_str_equal,
					       g_free,
					       NULL);

	migrate_options_directory ();
	register_commands ();
	compute_supported_archive_types ();

	fr_stock_init ();
}


static void
command_done (CommandData *cdata)
{
	if (cdata == NULL)
		return;

	if ((cdata->temp_dir != NULL) && path_is_dir (cdata->temp_dir)) {
		char *argv[4];

		argv[0] = "rm";
		argv[1] = "-rf";
		argv[2] = cdata->temp_dir;
		argv[3] = NULL;
		g_spawn_sync (g_get_tmp_dir (), argv, NULL,
			      G_SPAWN_SEARCH_PATH,
			      NULL, NULL,
			      NULL, NULL, NULL,
			      NULL);
	}

	g_free (cdata->command);
	if (cdata->app != NULL)
		g_object_unref (cdata->app);
	path_list_free (cdata->file_list);
	g_free (cdata->temp_dir);
	if (cdata->process != NULL)
		g_object_unref (cdata->process);

	CommandList = g_list_remove (CommandList, cdata);
	g_free (cdata);
}


void
release_data (void)
{
	if (! initialized)
		return;

	while (CommandList != NULL) {
		CommandData *cdata = CommandList->data;
		command_done (cdata);
	}
}