1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 
3 /*
4  *  Engrampa
5  *
6  *  Copyright (C) 2004, 2008 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, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #include <stdio.h>
24 #include <config.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 
29 #include <glib.h>
30 #include <glib/gi18n.h>
31 
32 #include "file-data.h"
33 #include "file-utils.h"
34 #include "glib-utils.h"
35 #include "fr-command.h"
36 #include "fr-command-7z.h"
37 #include "rar-utils.h"
38 
39 static void fr_command_7z_class_init  (FrCommand7zClass *class);
40 static void fr_command_7z_init        (FrCommand        *afile);
41 static void fr_command_7z_finalize    (GObject          *object);
42 
43 static gboolean spd_support = FALSE;
44 static gboolean unexpected_end_of_archive = FALSE;
45 static gboolean password_required = FALSE;
46 static gboolean password_handled = FALSE;
47 
48 /* Parent Class */
49 
50 static FrCommandClass *parent_class = NULL;
51 
52 
53 /* -- list -- */
54 
55 
56 static time_t
mktime_from_string(char * date_s,char * time_s)57 mktime_from_string (char *date_s,
58 		    char *time_s)
59 {
60 	struct tm   tm = {0, };
61 	char      **fields;
62 
63 	tm.tm_isdst = -1;
64 
65 	/* date */
66 
67 	fields = g_strsplit (date_s, "-", 3);
68 	if (fields[0] != NULL) {
69 		tm.tm_year = atoi (fields[0]) - 1900;
70 		tm.tm_mon = atoi (fields[1]) - 1;
71 		tm.tm_mday = atoi (fields[2]);
72 	}
73 	g_strfreev (fields);
74 
75 	/* time */
76 
77 	fields = g_strsplit (time_s, ":", 3);
78 	if (fields[0] != NULL) {
79 		tm.tm_hour = atoi (fields[0]);
80 		if (fields[1] != NULL) {
81 			tm.tm_min = atoi (fields[1]);
82 			if (fields[2] != NULL)
83 				tm.tm_sec = atoi (fields[2]);
84 		}
85 	}
86 	g_strfreev (fields);
87 
88 	return mktime (&tm);
89 }
90 
91 
92 static void
list__process_line(char * line,gpointer data)93 list__process_line (char     *line,
94 		    gpointer  data)
95 {
96 	FrCommand    *comm = FR_COMMAND (data);
97 	FrCommand7z  *p7z_comm = FR_COMMAND_7Z (comm);
98 	char        **fields;
99 	FileData     *fdata;
100 
101 	g_return_if_fail (line != NULL);
102 
103 	if (! p7z_comm->list_started) {
104 		if (strncmp (line, "p7zip Version ", 14) == 0) {
105 			const char *ver_start;
106 			int         ver_len;
107 			char        version[256];
108 
109 			ver_start = eat_spaces (line + 14);
110 			ver_len = strchr (ver_start, ' ') - ver_start;
111 			strncpy (version, ver_start, ver_len);
112 			version[ver_len] = 0;
113 
114 			if ((strcmp (version, "4.55") < 0) && (ver_len > 1) && (version[1] == '.'))
115 				p7z_comm->old_style = TRUE;
116 			else
117 				p7z_comm->old_style = FALSE;
118 			if ((strcmp (version, "9.38") < 0) && (ver_len > 1) && (version[1] == '.'))
119 				spd_support = FALSE;
120 			else
121 				spd_support = TRUE;
122 		}
123 		else if (p7z_comm->old_style && (strncmp (line, "Listing archive: ", 17) == 0))
124 			p7z_comm->list_started = TRUE;
125 		else if (! p7z_comm->old_style && (strcmp (line, "----------") == 0))
126 			p7z_comm->list_started = TRUE;
127 		else if (strncmp (line, "Multivolume = ", 14) == 0) {
128 			fields = g_strsplit (line, " = ", 2);
129 			comm->multi_volume = (strcmp (fields[1], "+") == 0);
130 			g_strfreev (fields);
131 		}
132 		else if (strncmp (line, "Unexpected end of archive", 25) == 0)  {
133 			unexpected_end_of_archive = TRUE;
134 		}
135 		return;
136 	}
137 
138 	if (strcmp (line, "") == 0) {
139 		if (p7z_comm->fdata != NULL) {
140 			if (p7z_comm->fdata->original_path == NULL) {
141 				file_data_free (p7z_comm->fdata);
142 				p7z_comm->fdata = NULL;
143 			}
144 			else {
145 				fdata = p7z_comm->fdata;
146 				if (fdata->dir)
147 					fdata->name = dir_name_from_path (fdata->full_path);
148 				else
149 					fdata->name = g_strdup (file_name_from_path (fdata->full_path));
150 				fdata->path = remove_level_from_path (fdata->full_path);
151 				fr_command_add_file (comm, fdata);
152 				p7z_comm->fdata = NULL;
153 			}
154 		}
155 		return;
156 	}
157 
158 	if (p7z_comm->fdata == NULL)
159 		p7z_comm->fdata = file_data_new ();
160 
161 	fields = g_strsplit (line, " = ", 2);
162 
163 	if (g_strv_length (fields) < 2) {
164 		g_strfreev (fields);
165 		return;
166 	}
167 
168 	fdata = p7z_comm->fdata;
169 
170 	if (strcmp (fields[0], "Path") == 0) {
171 		fdata->free_original_path = TRUE;
172 		fdata->original_path = g_strdup (fields[1]);
173 		fdata->full_path = g_strconcat ((fdata->original_path[0] != '/') ? "/" : "",
174 						fdata->original_path,
175 						(fdata->dir && (fdata->original_path[strlen (fdata->original_path) - 1] != '/')) ? "/" : "",
176 						NULL);
177 	}
178 	else if (strcmp (fields[0], "Folder") == 0) {
179 		fdata->dir = (strcmp (fields[1], "+") == 0);
180 	}
181 	else if (strcmp (fields[0], "Size") == 0) {
182 		fdata->size = g_ascii_strtoull (fields[1], NULL, 10);
183 	}
184 	else if (strcmp (fields[0], "Modified") == 0) {
185 		char **modified_fields;
186 
187 		modified_fields = g_strsplit (fields[1], " ", 2);
188 		if (modified_fields[0] != NULL)
189 			fdata->modified = mktime_from_string (modified_fields[0], modified_fields[1]);
190 		g_strfreev (modified_fields);
191 	}
192 	else if (strcmp (fields[0], "Encrypted") == 0) {
193 		if (strcmp (fields[1], "+") == 0)
194 			fdata->encrypted = TRUE;
195 	}
196 	else if (strcmp (fields[0], "Method") == 0) {
197 		if (strstr (fields[1], "AES") != NULL)
198 			fdata->encrypted = TRUE;
199 	}
200 	else if (strcmp (fields[0], "Attributes") == 0) {
201 		if (fields[1][0] == 'D')
202 			fdata->dir = TRUE;
203 	}
204 	g_strfreev (fields);
205 }
206 
207 
208 static void
fr_command_7z_begin_command(FrCommand * comm)209 fr_command_7z_begin_command (FrCommand *comm)
210 {
211 	if (is_program_in_path ("7z"))
212 		fr_process_begin_command (comm->process, "7z");
213 	else if (is_program_in_path ("7za"))
214 		fr_process_begin_command (comm->process, "7za");
215 	else if (is_program_in_path ("7zr"))
216 		fr_process_begin_command (comm->process, "7zr");
217 }
218 
219 
220 static void
add_password_arg(FrCommand * comm,const char * password,gboolean always_specify)221 add_password_arg (FrCommand     *comm,
222 		  const char    *password,
223 		  gboolean       always_specify)
224 {
225 	if (always_specify || ((password != NULL) && (*password != 0))) {
226 		char *arg;
227 
228 		arg = g_strconcat ("-p", password, NULL);
229 		fr_process_add_arg (comm->process, arg);
230 		g_free (arg);
231 		password_handled = TRUE;
232 	}
233 }
234 
235 
236 static void
list__begin(gpointer data)237 list__begin (gpointer data)
238 {
239 	FrCommand7z *p7z_comm = data;
240 
241 	if (p7z_comm->fdata != NULL) {
242 		file_data_free (p7z_comm->fdata);
243 		p7z_comm->fdata = NULL;
244 	}
245 	p7z_comm->list_started = FALSE;
246 }
247 
248 
249 static void
fr_command_7z_list(FrCommand * comm)250 fr_command_7z_list (FrCommand  *comm)
251 {
252 	rar_check_multi_volume (comm);
253 
254 	fr_process_set_out_line_func (comm->process, list__process_line, comm);
255 
256 	fr_command_7z_begin_command (comm);
257 	fr_process_set_begin_func (comm->process, list__begin, comm);
258 	fr_process_add_arg (comm->process, "l");
259 	fr_process_add_arg (comm->process, "-slt");
260 	fr_process_add_arg (comm->process, "-bd");
261 	fr_process_add_arg (comm->process, "-y");
262 	add_password_arg (comm, comm->password, FALSE);
263 	fr_process_add_arg (comm->process, "--");
264 	fr_process_add_arg (comm->process, comm->filename);
265 	fr_process_end_command (comm->process);
266 
267 	fr_process_start (comm->process);
268 }
269 
270 
271 static void
parse_progress_line(FrCommand * comm,const char * prefix,const char * message_prefix,const char * line)272 parse_progress_line (FrCommand  *comm,
273 		     const char *prefix,
274 		     const char *message_prefix,
275 		     const char *line)
276 {
277 	int prefix_len;
278 
279 	prefix_len = strlen (prefix);
280 	if (strncmp (line, prefix, prefix_len) == 0)
281 		fr_command_progress (comm, (double) ++comm->n_file / (comm->n_files + 1));
282 }
283 
284 
285 static void
process_line__add(char * line,gpointer data)286 process_line__add (char     *line,
287 		   gpointer  data)
288 {
289 	FrCommand *comm = FR_COMMAND (data);
290 
291 	if (strstr (line, "Enter password") != NULL)
292 		password_required = TRUE;
293 
294 	if ((comm->volume_size > 0) && (strncmp (line, "Creating archive", 16) == 0)) {
295 		char  *volume_filename;
296 		GFile *volume_file;
297 
298 		volume_filename = g_strconcat (comm->filename, ".001", NULL);
299 		volume_file = g_file_new_for_path (volume_filename);
300 		fr_command_set_multi_volume (comm, volume_file);
301 
302 		g_object_unref (volume_file);
303 		g_free (volume_filename);
304 	}
305 
306 	if (comm->n_files != 0)
307 		parse_progress_line (comm, "Compressing  ", _("Adding file: "), line);
308 }
309 
310 
311 static void
fr_command_7z_add(FrCommand * comm,const char * from_file,GList * file_list,const char * base_dir,gboolean update,gboolean recursive)312 fr_command_7z_add (FrCommand     *comm,
313 		   const char    *from_file,
314 		   GList         *file_list,
315 		   const char    *base_dir,
316 		   gboolean       update,
317 		   gboolean       recursive)
318 {
319 	GList *scan;
320 
321 	fr_process_use_standard_locale (comm->process, TRUE);
322 	fr_process_set_out_line_func (comm->process,
323 				      process_line__add,
324 				      comm);
325 
326 	fr_command_7z_begin_command (comm);
327 
328 	if (update)
329 		fr_process_add_arg (comm->process, "u");
330 	else
331 		fr_process_add_arg (comm->process, "a");
332 
333 	if (base_dir != NULL) {
334 		fr_process_set_working_dir (comm->process, base_dir);
335 	}
336 
337 	if (is_mime_type (comm->mime_type, "application/zip")
338 	    || is_mime_type (comm->mime_type, "application/x-cbz"))
339 	{
340 		fr_process_add_arg (comm->process, "-tzip");
341 		fr_process_add_arg (comm->process, "-mem=AES128");
342 	}
343 
344 	if (spd_support) fr_process_add_arg (comm->process, "-spd");
345 	fr_process_add_arg (comm->process, "-bd");
346 	fr_process_add_arg (comm->process, "-y");
347 	fr_process_add_arg (comm->process, "-l");
348 	add_password_arg (comm, comm->password, FALSE);
349 	if ((comm->password != NULL)
350 	    && (*comm->password != 0)
351 	    && comm->encrypt_header
352 	    && fr_command_is_capable_of (comm, FR_COMMAND_CAN_ENCRYPT_HEADER))
353 	{
354 		fr_process_add_arg (comm->process, "-mhe=on");
355 	}
356 
357 	/* fr_process_add_arg (comm->process, "-ms=off"); FIXME: solid mode off? */
358 
359 	switch (comm->compression) {
360 	case FR_COMPRESSION_VERY_FAST:
361 		fr_process_add_arg (comm->process, "-mx=1");
362 		break;
363 	case FR_COMPRESSION_FAST:
364 		fr_process_add_arg (comm->process, "-mx=5");
365 		break;
366 	case FR_COMPRESSION_NORMAL:
367 		fr_process_add_arg (comm->process, "-mx=7");
368 		break;
369 	case FR_COMPRESSION_MAXIMUM:
370 		fr_process_add_arg (comm->process, "-mx=9");
371 		if (! is_mime_type (comm->mime_type, "application/zip")
372 		    && ! is_mime_type (comm->mime_type, "application/x-cbz"))
373 		{
374 			fr_process_add_arg (comm->process, "-m0=lzma2");;
375 		}
376 		break;
377 	}
378 
379 	if (is_mime_type (comm->mime_type, "application/x-ms-dos-executable"))
380 		fr_process_add_arg (comm->process, "-sfx");
381 
382 	if (comm->volume_size > 0)
383 		fr_process_add_arg_printf (comm->process, "-v%ub", comm->volume_size);
384 
385 	if (from_file != NULL)
386 		fr_process_add_arg_concat (comm->process, "-i@", from_file, NULL);
387 
388 	if (from_file == NULL)
389 		for (scan = file_list; scan; scan = scan->next)
390 			/* Files prefixed with '@' need to be handled specially */
391 			if (g_str_has_prefix (scan->data, "@"))
392 				fr_process_add_arg_concat (comm->process, "-i!", scan->data, NULL);
393 
394 	fr_process_add_arg (comm->process, "--");
395 	fr_process_add_arg (comm->process, comm->filename);
396 
397 	if (from_file == NULL)
398 		for (scan = file_list; scan; scan = scan->next)
399 			/* Skip files prefixed with '@', already added */
400 			if (!g_str_has_prefix (scan->data, "@"))
401 				fr_process_add_arg (comm->process, scan->data);
402 
403 	fr_process_end_command (comm->process);
404 }
405 
406 
407 static void
fr_command_7z_delete(FrCommand * comm,const char * from_file,GList * file_list)408 fr_command_7z_delete (FrCommand  *comm,
409 		      const char *from_file,
410 		      GList      *file_list)
411 {
412 	GList *scan;
413 
414 	fr_command_7z_begin_command (comm);
415 	fr_process_add_arg (comm->process, "d");
416 	if (spd_support) fr_process_add_arg (comm->process, "-spd");
417 	fr_process_add_arg (comm->process, "-bd");
418 	fr_process_add_arg (comm->process, "-y");
419 	if (is_mime_type (comm->mime_type, "application/x-ms-dos-executable"))
420 		fr_process_add_arg (comm->process, "-sfx");
421 
422 	if (from_file != NULL)
423 		fr_process_add_arg_concat (comm->process, "-i@", from_file, NULL);
424 
425 	if (from_file == NULL)
426 		for (scan = file_list; scan; scan = scan->next)
427 			/* Files prefixed with '@' need to be handled specially */
428 			if (g_str_has_prefix (scan->data, "@"))
429 				fr_process_add_arg_concat (comm->process, "-i!", scan->data, NULL);
430 
431 	add_password_arg (comm, FR_COMMAND (comm)->password, FALSE);
432 
433 	fr_process_add_arg (comm->process, "--");
434 	fr_process_add_arg (comm->process, comm->filename);
435 
436 	if (from_file == NULL)
437 		for (scan = file_list; scan; scan = scan->next)
438 			/* Skip files prefixed with '@', already added */
439 			if (!g_str_has_prefix (scan->data, "@"))
440 				fr_process_add_arg (comm->process, scan->data);
441 
442 	fr_process_end_command (comm->process);
443 }
444 
445 
446 static void
process_line__extract(char * line,gpointer data)447 process_line__extract (char     *line,
448 		       gpointer  data)
449 {
450 	FrCommand *comm = FR_COMMAND (data);
451 
452 	if (comm->n_files != 0)
453 		parse_progress_line (comm, "Extracting  ", _("Extracting file: "), line);
454 }
455 
456 
457 static void
fr_command_7z_extract(FrCommand * comm,const char * from_file,GList * file_list,const char * dest_dir,gboolean overwrite,gboolean skip_older,gboolean junk_paths)458 fr_command_7z_extract (FrCommand  *comm,
459 		       const char *from_file,
460 		       GList      *file_list,
461 		       const char *dest_dir,
462 		       gboolean    overwrite,
463 		       gboolean    skip_older,
464 		       gboolean    junk_paths)
465 {
466 	GList *scan;
467 
468 	fr_process_use_standard_locale (comm->process, TRUE);
469 	fr_process_set_out_line_func (comm->process,
470 				      process_line__extract,
471 				      comm);
472 	fr_command_7z_begin_command (comm);
473 
474 	if (junk_paths)
475 		fr_process_add_arg (comm->process, "e");
476 	else
477 		fr_process_add_arg (comm->process, "x");
478 
479 	if (spd_support) fr_process_add_arg (comm->process, "-spd");
480 	fr_process_add_arg (comm->process, "-bd");
481 	fr_process_add_arg (comm->process, "-y");
482 	add_password_arg (comm, comm->password, FALSE);
483 
484 	if (dest_dir != NULL)
485 		fr_process_add_arg_concat (comm->process, "-o", dest_dir, NULL);
486 
487 	if (from_file != NULL)
488 		fr_process_add_arg_concat (comm->process, "-i@", from_file, NULL);
489 
490 	if (from_file == NULL)
491 		for (scan = file_list; scan; scan = scan->next)
492 			/* Files prefixed with '@' need to be handled specially */
493 			if (g_str_has_prefix (scan->data, "@"))
494 				fr_process_add_arg_concat (comm->process, "-i!", scan->data, NULL);
495 
496 	fr_process_add_arg (comm->process, "--");
497 	fr_process_add_arg (comm->process, comm->filename);
498 
499 	if (from_file == NULL)
500 		for (scan = file_list; scan; scan = scan->next)
501 			/* Skip files prefixed with '@', already added */
502 			if (!g_str_has_prefix (scan->data, "@"))
503 				fr_process_add_arg (comm->process, scan->data);
504 
505 	if (unexpected_end_of_archive) fr_process_set_ignore_error (comm->process, TRUE);
506 
507 	fr_process_end_command (comm->process);
508 }
509 
510 
511 static void
fr_command_7z_test(FrCommand * comm)512 fr_command_7z_test (FrCommand   *comm)
513 {
514 	fr_command_7z_begin_command (comm);
515 	fr_process_add_arg (comm->process, "t");
516 	fr_process_add_arg (comm->process, "-bd");
517 	fr_process_add_arg (comm->process, "-y");
518 	add_password_arg (comm, comm->password, FALSE);
519 	fr_process_add_arg (comm->process, "--");
520 	fr_process_add_arg (comm->process, comm->filename);
521 	fr_process_end_command (comm->process);
522 }
523 
524 
525 static void
fr_command_7z_handle_error(FrCommand * comm,FrProcError * error)526 fr_command_7z_handle_error (FrCommand   *comm,
527 			    FrProcError *error)
528 {
529 	if (error->type == FR_PROC_ERROR_NONE) {
530 		FileData *first;
531 		char     *basename;
532 		char     *testname;
533 
534 		/* This is a way to fix bug #582712. */
535 
536 		if (comm->files->len != 1)
537 			return;
538 
539 		if (! g_str_has_suffix (comm->filename, ".001"))
540 			return;
541 
542 		first = g_ptr_array_index (comm->files, 0);
543 		basename = g_path_get_basename (comm->filename);
544 		testname = g_strconcat (first->original_path, ".001", NULL);
545 
546 		if (strcmp (basename, testname) == 0)
547 			error->type = FR_PROC_ERROR_ASK_PASSWORD;
548 
549 		g_free (testname);
550 		g_free (basename);
551 
552 		return;
553 	}
554 
555 	if ((error->status <= 1) || (unexpected_end_of_archive)) {
556 		error->type = FR_PROC_ERROR_NONE;
557 	}
558 	else {
559 		if (password_required && (!password_handled))
560 		{
561 			error->type = FR_PROC_ERROR_ASK_PASSWORD;
562 			return;
563 		}
564 
565 		GList *scan;
566 
567 		for (scan = g_list_last (comm->process->out.raw); scan; scan = scan->prev) {
568 			char *line = scan->data;
569 
570 			if ((strstr (line, "Wrong password?") != NULL)
571 			    || (strstr (line, "Enter password") != NULL))
572 			{
573 				error->type = FR_PROC_ERROR_ASK_PASSWORD;
574 				break;
575 			}
576 		}
577 	}
578 }
579 
580 
581 const char *sevenz_mime_types[] = { "application/epub+zip",
582 				    "application/x-7z-compressed",
583 				    "application/x-arj",
584 				    "application/vnd.ms-cab-compressed",
585 				    "application/x-cd-image",
586 				    /*"application/x-cbr",*/
587 				    "application/x-cbz",
588 				    "application/x-ms-dos-executable",
589 				    "application/x-ms-wim",
590 				    "application/x-rar",
591 				    "application/zip", /* zip always at the end and the number of */
592 				    NULL };            /* place in fr_command_7z_get_mime_types   */
593 
594 
595 static const char **
fr_command_7z_get_mime_types(FrCommand * comm)596 fr_command_7z_get_mime_types (FrCommand *comm)
597 {
598 	GSettings *settings;
599 	settings = g_settings_new ("org.mate.engrampa.general");
600 
601 	if (g_settings_get_boolean (settings, "unar-open-zip") &&
602 	    is_program_in_path ("unar") && is_program_in_path ("lsar"))
603 		sevenz_mime_types [8] = NULL;
604 	else
605 		g_settings_set_boolean (settings, "unar-open-zip", FALSE);
606 
607 	g_object_unref (settings);
608 
609 	return sevenz_mime_types;
610 }
611 
612 
613 static FrCommandCap
fr_command_7z_get_capabilities(FrCommand * comm,const char * mime_type,gboolean check_command)614 fr_command_7z_get_capabilities (FrCommand  *comm,
615 				const char *mime_type,
616 				gboolean    check_command)
617 {
618 	FrCommandCap capabilities;
619 
620 	capabilities = FR_COMMAND_CAN_ARCHIVE_MANY_FILES;
621 	if (! is_program_available ("7za", check_command) && ! is_program_available ("7zr", check_command) && ! is_program_available ("7z", check_command))
622 		return capabilities;
623 
624 	if (is_mime_type (mime_type, "application/x-7z-compressed")) {
625 		capabilities |= FR_COMMAND_CAN_READ_WRITE | FR_COMMAND_CAN_CREATE_VOLUMES;
626 		if (is_program_available ("7z", check_command))
627 			capabilities |= FR_COMMAND_CAN_ENCRYPT | FR_COMMAND_CAN_ENCRYPT_HEADER;
628 	}
629 	else if (is_mime_type (mime_type, "application/x-7z-compressed-tar")) {
630 		capabilities |= FR_COMMAND_CAN_READ_WRITE;
631 		if (is_program_available ("7z", check_command))
632 			capabilities |= FR_COMMAND_CAN_ENCRYPT | FR_COMMAND_CAN_ENCRYPT_HEADER;
633 	}
634 	else if (is_program_available ("7z", check_command)) {
635 		if (is_mime_type (mime_type, "application/x-rar")
636 		    || is_mime_type (mime_type, "application/x-cbr"))
637 		{
638 			if (! check_command || g_file_test ("/usr/lib/p7zip/Codecs/Rar29.so", G_FILE_TEST_EXISTS) || g_file_test ("/usr/lib/p7zip/Codecs/Rar.so", G_FILE_TEST_EXISTS)
639 			    || g_file_test ("/usr/libexec/p7zip/Codecs/Rar29.so", G_FILE_TEST_EXISTS) || g_file_test ("/usr/libexec/p7zip/Codecs/Rar.so", G_FILE_TEST_EXISTS))
640 				capabilities |= FR_COMMAND_CAN_READ;
641 		}
642 		else
643 			capabilities |= FR_COMMAND_CAN_READ;
644 
645 		if (is_mime_type (mime_type, "application/epub+zip")
646 		    || is_mime_type (mime_type, "application/x-cbz")
647 		    || is_mime_type (mime_type, "application/x-ms-dos-executable")
648 		    || is_mime_type (mime_type, "application/zip"))
649 		{
650 			capabilities |= FR_COMMAND_CAN_WRITE | FR_COMMAND_CAN_ENCRYPT;
651 		}
652 	}
653 	else if (is_program_available ("7za", check_command)) {
654 		if (is_mime_type (mime_type, "application/vnd.ms-cab-compressed")
655 		    || is_mime_type (mime_type, "application/zip"))
656 		{
657 			capabilities |= FR_COMMAND_CAN_READ;
658 		}
659 
660 		if (is_mime_type (mime_type, "application/zip"))
661 			capabilities |= FR_COMMAND_CAN_WRITE;
662 	}
663 
664 	/* multi-volumes are read-only */
665 	if ((comm->files->len > 0) && comm->multi_volume && (capabilities & FR_COMMAND_CAN_WRITE))
666 		capabilities ^= FR_COMMAND_CAN_WRITE;
667 
668 	return capabilities;
669 }
670 
671 
672 static const char *
fr_command_7z_get_packages(FrCommand * comm,const char * mime_type)673 fr_command_7z_get_packages (FrCommand  *comm,
674 			    const char *mime_type)
675 {
676 	if (is_mime_type (mime_type, "application/x-rar"))
677 		return PACKAGES ("p7zip,p7zip-rar");
678 	else if (is_mime_type (mime_type, "application/zip") || is_mime_type (mime_type, "application/vnd.ms-cab-compressed"))
679 		return PACKAGES ("p7zip,p7zip-full");
680 	else
681 		return PACKAGES ("p7zip");
682 }
683 
684 
685 static void
fr_command_7z_class_init(FrCommand7zClass * class)686 fr_command_7z_class_init (FrCommand7zClass *class)
687 {
688 	GObjectClass   *gobject_class = G_OBJECT_CLASS (class);
689 	FrCommandClass *afc;
690 
691 	parent_class = g_type_class_peek_parent (class);
692 	afc = (FrCommandClass*) class;
693 
694 	gobject_class->finalize = fr_command_7z_finalize;
695 
696 	afc->list             = fr_command_7z_list;
697 	afc->add              = fr_command_7z_add;
698 	afc->delete           = fr_command_7z_delete;
699 	afc->extract          = fr_command_7z_extract;
700 	afc->test             = fr_command_7z_test;
701 	afc->handle_error     = fr_command_7z_handle_error;
702 	afc->get_mime_types   = fr_command_7z_get_mime_types;
703 	afc->get_capabilities = fr_command_7z_get_capabilities;
704 	afc->get_packages     = fr_command_7z_get_packages;
705 }
706 
707 
708 static void
fr_command_7z_init(FrCommand * comm)709 fr_command_7z_init (FrCommand *comm)
710 {
711 	comm->propAddCanUpdate             = TRUE;
712 	comm->propAddCanReplace            = TRUE;
713 	comm->propAddCanStoreFolders       = TRUE;
714 	comm->propExtractCanAvoidOverwrite = FALSE;
715 	comm->propExtractCanSkipOlder      = FALSE;
716 	comm->propExtractCanJunkPaths      = TRUE;
717 	comm->propPassword                 = TRUE;
718 	comm->propTest                     = TRUE;
719 	comm->propListFromFile             = TRUE;
720 }
721 
722 
723 static void
fr_command_7z_finalize(GObject * object)724 fr_command_7z_finalize (GObject *object)
725 {
726 	g_return_if_fail (object != NULL);
727 	g_return_if_fail (FR_IS_COMMAND_7Z (object));
728 
729 	/* Chain up */
730 	if (G_OBJECT_CLASS (parent_class)->finalize)
731 		G_OBJECT_CLASS (parent_class)->finalize (object);
732 }
733 
734 
735 GType
fr_command_7z_get_type()736 fr_command_7z_get_type ()
737 {
738 	static GType type = 0;
739 
740 	if (! type) {
741 		GTypeInfo type_info = {
742 			sizeof (FrCommand7zClass),
743 			NULL,
744 			NULL,
745 			(GClassInitFunc) fr_command_7z_class_init,
746 			NULL,
747 			NULL,
748 			sizeof (FrCommand7z),
749 			0,
750 			(GInstanceInitFunc) fr_command_7z_init,
751 			NULL
752 		};
753 
754 		type = g_type_register_static (FR_TYPE_COMMAND,
755 					       "FRCommand7z",
756 					       &type_info,
757 					       0);
758 	}
759 
760 	return type;
761 }
762