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., 59 Temple Street #330, 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 "tr-wrapper.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 		fr_process_add_arg_concat (comm->process, "-w", base_dir, NULL);
336 	}
337 
338 	if (is_mime_type (comm->mime_type, "application/zip")
339 	    || is_mime_type (comm->mime_type, "application/x-cbz"))
340 	{
341 		fr_process_add_arg (comm->process, "-tzip");
342 		fr_process_add_arg (comm->process, "-mem=AES128");
343 	}
344 
345 	if (spd_support) fr_process_add_arg (comm->process, "-spd");
346 	fr_process_add_arg (comm->process, "-bd");
347 	fr_process_add_arg (comm->process, "-y");
348 	fr_process_add_arg (comm->process, "-l");
349 	add_password_arg (comm, comm->password, FALSE);
350 	if ((comm->password != NULL)
351 	    && (*comm->password != 0)
352 	    && comm->encrypt_header
353 	    && fr_command_is_capable_of (comm, FR_COMMAND_CAN_ENCRYPT_HEADER))
354 	{
355 		fr_process_add_arg (comm->process, "-mhe=on");
356 	}
357 
358 	/* fr_process_add_arg (comm->process, "-ms=off"); FIXME: solid mode off? */
359 
360 	switch (comm->compression) {
361 	case FR_COMPRESSION_VERY_FAST:
362 		fr_process_add_arg (comm->process, "-mx=1");
363 		break;
364 	case FR_COMPRESSION_FAST:
365 		fr_process_add_arg (comm->process, "-mx=5");
366 		break;
367 	case FR_COMPRESSION_NORMAL:
368 		fr_process_add_arg (comm->process, "-mx=7");
369 		break;
370 	case FR_COMPRESSION_MAXIMUM:
371 		fr_process_add_arg (comm->process, "-mx=9");
372 		if (! is_mime_type (comm->mime_type, "application/zip")
373 		    && ! is_mime_type (comm->mime_type, "application/x-cbz"))
374 		{
375 			fr_process_add_arg (comm->process, "-m0=lzma2");;
376 		}
377 		break;
378 	}
379 
380 	if (is_mime_type (comm->mime_type, "application/x-ms-dos-executable"))
381 		fr_process_add_arg (comm->process, "-sfx");
382 
383 	if (comm->volume_size > 0)
384 		fr_process_add_arg_printf (comm->process, "-v%ub", comm->volume_size);
385 
386 	if (from_file != NULL)
387 		fr_process_add_arg_concat (comm->process, "-i@", from_file, NULL);
388 
389 	if (from_file == NULL)
390 		for (scan = file_list; scan; scan = scan->next)
391 			/* Files prefixed with '@' need to be handled specially */
392 			if (g_str_has_prefix (scan->data, "@"))
393 				fr_process_add_arg_concat (comm->process, "-i!", scan->data, NULL);
394 
395 	fr_process_add_arg (comm->process, "--");
396 	fr_process_add_arg (comm->process, comm->filename);
397 
398 	if (from_file == NULL)
399 		for (scan = file_list; scan; scan = scan->next)
400 			/* Skip files prefixed with '@', already added */
401 			if (!g_str_has_prefix (scan->data, "@"))
402 				fr_process_add_arg (comm->process, scan->data);
403 
404 	fr_process_end_command (comm->process);
405 }
406 
407 
408 static void
fr_command_7z_delete(FrCommand * comm,const char * from_file,GList * file_list)409 fr_command_7z_delete (FrCommand  *comm,
410 		      const char *from_file,
411 		      GList      *file_list)
412 {
413 	GList *scan;
414 
415 	fr_command_7z_begin_command (comm);
416 	fr_process_add_arg (comm->process, "d");
417 	if (spd_support) fr_process_add_arg (comm->process, "-spd");
418 	fr_process_add_arg (comm->process, "-bd");
419 	fr_process_add_arg (comm->process, "-y");
420 	if (is_mime_type (comm->mime_type, "application/x-ms-dos-executable"))
421 		fr_process_add_arg (comm->process, "-sfx");
422 
423 	if (from_file != NULL)
424 		fr_process_add_arg_concat (comm->process, "-i@", from_file, NULL);
425 
426 	if (from_file == NULL)
427 		for (scan = file_list; scan; scan = scan->next)
428 			/* Files prefixed with '@' need to be handled specially */
429 			if (g_str_has_prefix (scan->data, "@"))
430 				fr_process_add_arg_concat (comm->process, "-i!", scan->data, NULL);
431 
432 	add_password_arg (comm, FR_COMMAND (comm)->password, FALSE);
433 
434 	fr_process_add_arg (comm->process, "--");
435 	fr_process_add_arg (comm->process, comm->filename);
436 
437 	if (from_file == NULL)
438 		for (scan = file_list; scan; scan = scan->next)
439 			/* Skip files prefixed with '@', already added */
440 			if (!g_str_has_prefix (scan->data, "@"))
441 				fr_process_add_arg (comm->process, scan->data);
442 
443 	fr_process_end_command (comm->process);
444 }
445 
446 
447 static void
process_line__extract(char * line,gpointer data)448 process_line__extract (char     *line,
449 		       gpointer  data)
450 {
451 	FrCommand *comm = FR_COMMAND (data);
452 
453 	if (comm->n_files != 0)
454 		parse_progress_line (comm, "Extracting  ", _("Extracting file: "), line);
455 }
456 
457 
458 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)459 fr_command_7z_extract (FrCommand  *comm,
460 		       const char *from_file,
461 		       GList      *file_list,
462 		       const char *dest_dir,
463 		       gboolean    overwrite,
464 		       gboolean    skip_older,
465 		       gboolean    junk_paths)
466 {
467 	GList *scan;
468 
469 	fr_process_use_standard_locale (comm->process, TRUE);
470 	fr_process_set_out_line_func (comm->process,
471 				      process_line__extract,
472 				      comm);
473 	fr_command_7z_begin_command (comm);
474 
475 	if (junk_paths)
476 		fr_process_add_arg (comm->process, "e");
477 	else
478 		fr_process_add_arg (comm->process, "x");
479 
480 	if (spd_support) fr_process_add_arg (comm->process, "-spd");
481 	fr_process_add_arg (comm->process, "-bd");
482 	fr_process_add_arg (comm->process, "-y");
483 	add_password_arg (comm, comm->password, FALSE);
484 
485 	if (dest_dir != NULL)
486 		fr_process_add_arg_concat (comm->process, "-o", dest_dir, NULL);
487 
488 	if (from_file != NULL)
489 		fr_process_add_arg_concat (comm->process, "-i@", from_file, NULL);
490 
491 	if (from_file == NULL)
492 		for (scan = file_list; scan; scan = scan->next)
493 			/* Files prefixed with '@' need to be handled specially */
494 			if (g_str_has_prefix (scan->data, "@"))
495 				fr_process_add_arg_concat (comm->process, "-i!", scan->data, NULL);
496 
497 	fr_process_add_arg (comm->process, "--");
498 	fr_process_add_arg (comm->process, comm->filename);
499 
500 	if (from_file == NULL)
501 		for (scan = file_list; scan; scan = scan->next)
502 			/* Skip files prefixed with '@', already added */
503 			if (!g_str_has_prefix (scan->data, "@"))
504 				fr_process_add_arg (comm->process, scan->data);
505 
506 	if (unexpected_end_of_archive) fr_process_set_ignore_error (comm->process, TRUE);
507 
508 	fr_process_end_command (comm->process);
509 }
510 
511 
512 static void
fr_command_7z_test(FrCommand * comm)513 fr_command_7z_test (FrCommand   *comm)
514 {
515 	fr_command_7z_begin_command (comm);
516 	fr_process_add_arg (comm->process, "t");
517 	fr_process_add_arg (comm->process, "-bd");
518 	fr_process_add_arg (comm->process, "-y");
519 	add_password_arg (comm, comm->password, FALSE);
520 	fr_process_add_arg (comm->process, "--");
521 	fr_process_add_arg (comm->process, comm->filename);
522 	fr_process_end_command (comm->process);
523 }
524 
525 
526 static void
fr_command_7z_handle_error(FrCommand * comm,FrProcError * error)527 fr_command_7z_handle_error (FrCommand   *comm,
528 			    FrProcError *error)
529 {
530 	if (error->type == FR_PROC_ERROR_NONE) {
531 		FileData *first;
532 		char     *basename;
533 		char     *testname;
534 
535 		/* This is a way to fix bug #582712. */
536 
537 		if (comm->files->len != 1)
538 			return;
539 
540 		if (! g_str_has_suffix (comm->filename, ".001"))
541 			return;
542 
543 		first = g_ptr_array_index (comm->files, 0);
544 		basename = g_path_get_basename (comm->filename);
545 		testname = g_strconcat (first->original_path, ".001", NULL);
546 
547 		if (strcmp (basename, testname) == 0)
548 			error->type = FR_PROC_ERROR_ASK_PASSWORD;
549 
550 		g_free (testname);
551 		g_free (basename);
552 
553 		return;
554 	}
555 
556 	if ((error->status <= 1) || (unexpected_end_of_archive)) {
557 		error->type = FR_PROC_ERROR_NONE;
558 	}
559 	else {
560 		if (password_required && (!password_handled))
561 		{
562 			error->type = FR_PROC_ERROR_ASK_PASSWORD;
563 			return;
564 		}
565 
566 		GList *scan;
567 
568 		for (scan = g_list_last (comm->process->out.raw); scan; scan = scan->prev) {
569 			char *line = scan->data;
570 
571 			if ((strstr (line, "Wrong password?") != NULL)
572 			    || (strstr (line, "Enter password") != NULL))
573 			{
574 				error->type = FR_PROC_ERROR_ASK_PASSWORD;
575 				break;
576 			}
577 		}
578 	}
579 }
580 
581 
582 const char *sevenz_mime_types[] = { "application/x-7z-compressed",
583 				    "application/x-arj",
584 				    "application/vnd.ms-cab-compressed",
585 				    "application/x-cd-image",
586 				    "application/x-raw-disk-image",
587 				    /*"application/x-cbr",*/
588 				    "application/x-cbz",
589 				    "application/x-ms-dos-executable",
590 				    "application/x-ms-wim",
591 				    "application/x-rar",
592 				    "application/zip",
593 				    NULL };
594 
595 
596 static const char **
fr_command_7z_get_mime_types(FrCommand * comm)597 fr_command_7z_get_mime_types (FrCommand *comm)
598 {
599 	return sevenz_mime_types;
600 }
601 
602 
603 static FrCommandCap
fr_command_7z_get_capabilities(FrCommand * comm,const char * mime_type,gboolean check_command)604 fr_command_7z_get_capabilities (FrCommand  *comm,
605 				const char *mime_type,
606 				gboolean    check_command)
607 {
608 	FrCommandCap capabilities;
609 
610 	capabilities = FR_COMMAND_CAN_ARCHIVE_MANY_FILES;
611 	if (! is_program_available ("7za", check_command) && ! is_program_available ("7zr", check_command) && ! is_program_available ("7z", check_command))
612 		return capabilities;
613 
614 	if (is_mime_type (mime_type, "application/x-7z-compressed")) {
615 		capabilities |= FR_COMMAND_CAN_READ_WRITE | FR_COMMAND_CAN_CREATE_VOLUMES;
616 		if (is_program_available ("7z", check_command))
617 			capabilities |= FR_COMMAND_CAN_ENCRYPT | FR_COMMAND_CAN_ENCRYPT_HEADER;
618 	}
619 	else if (is_mime_type (mime_type, "application/x-7z-compressed-tar")) {
620 		capabilities |= FR_COMMAND_CAN_READ_WRITE;
621 		if (is_program_available ("7z", check_command))
622 			capabilities |= FR_COMMAND_CAN_ENCRYPT | FR_COMMAND_CAN_ENCRYPT_HEADER;
623 	}
624 	else if (is_program_available ("7z", check_command)) {
625 		if (is_mime_type (mime_type, "application/x-rar")
626 		    || is_mime_type (mime_type, "application/x-cbr"))
627 		{
628 			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)
629 			    || 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))
630 				capabilities |= FR_COMMAND_CAN_READ;
631 		}
632 		else
633 			capabilities |= FR_COMMAND_CAN_READ;
634 
635 		if (is_mime_type (mime_type, "application/x-cbz")
636 		    || is_mime_type (mime_type, "application/x-ms-dos-executable")
637 		    || is_mime_type (mime_type, "application/zip"))
638 		{
639 			capabilities |= FR_COMMAND_CAN_WRITE | FR_COMMAND_CAN_ENCRYPT;
640 		}
641 	}
642 	else if (is_program_available ("7za", check_command)) {
643 		if (is_mime_type (mime_type, "application/vnd.ms-cab-compressed")
644 		    || is_mime_type (mime_type, "application/zip"))
645 		{
646 			capabilities |= FR_COMMAND_CAN_READ;
647 		}
648 
649 		if (is_mime_type (mime_type, "application/zip"))
650 			capabilities |= FR_COMMAND_CAN_WRITE;
651 	}
652 
653 	/* multi-volumes are read-only */
654 	if ((comm->files->len > 0) && comm->multi_volume && (capabilities & FR_COMMAND_CAN_WRITE))
655 		capabilities ^= FR_COMMAND_CAN_WRITE;
656 
657 	return capabilities;
658 }
659 
660 
661 static const char *
fr_command_7z_get_packages(FrCommand * comm,const char * mime_type)662 fr_command_7z_get_packages (FrCommand  *comm,
663 			    const char *mime_type)
664 {
665 	if (is_mime_type (mime_type, "application/x-rar"))
666 		return PACKAGES ("p7zip,p7zip-rar");
667 	else if (is_mime_type (mime_type, "application/zip") || is_mime_type (mime_type, "application/vnd.ms-cab-compressed"))
668 		return PACKAGES ("p7zip,p7zip-full");
669 	else
670 		return PACKAGES ("p7zip");
671 }
672 
673 
674 static void
fr_command_7z_class_init(FrCommand7zClass * class)675 fr_command_7z_class_init (FrCommand7zClass *class)
676 {
677 	GObjectClass   *gobject_class = G_OBJECT_CLASS (class);
678 	FrCommandClass *afc;
679 
680 	parent_class = g_type_class_peek_parent (class);
681 	afc = (FrCommandClass*) class;
682 
683 	gobject_class->finalize = fr_command_7z_finalize;
684 
685 	afc->list             = fr_command_7z_list;
686 	afc->add              = fr_command_7z_add;
687     afc->delete_           = fr_command_7z_delete;
688 	afc->extract          = fr_command_7z_extract;
689 	afc->test             = fr_command_7z_test;
690 	afc->handle_error     = fr_command_7z_handle_error;
691 	afc->get_mime_types   = fr_command_7z_get_mime_types;
692 	afc->get_capabilities = fr_command_7z_get_capabilities;
693 	afc->get_packages     = fr_command_7z_get_packages;
694 }
695 
696 
697 static void
fr_command_7z_init(FrCommand * comm)698 fr_command_7z_init (FrCommand *comm)
699 {
700 	comm->propAddCanUpdate             = TRUE;
701 	comm->propAddCanReplace            = TRUE;
702 	comm->propAddCanStoreFolders       = TRUE;
703 	comm->propExtractCanAvoidOverwrite = FALSE;
704 	comm->propExtractCanSkipOlder      = FALSE;
705 	comm->propExtractCanJunkPaths      = TRUE;
706 	comm->propPassword                 = TRUE;
707 	comm->propTest                     = TRUE;
708 	comm->propListFromFile             = TRUE;
709 }
710 
711 
712 static void
fr_command_7z_finalize(GObject * object)713 fr_command_7z_finalize (GObject *object)
714 {
715 	g_return_if_fail (object != NULL);
716 	g_return_if_fail (FR_IS_COMMAND_7Z (object));
717 
718 	/* Chain up */
719 	if (G_OBJECT_CLASS (parent_class)->finalize)
720 		G_OBJECT_CLASS (parent_class)->finalize (object);
721 }
722 
723 
724 GType
fr_command_7z_get_type()725 fr_command_7z_get_type ()
726 {
727 	static GType type = 0;
728 
729 	if (! type) {
730 		GTypeInfo type_info = {
731 			sizeof (FrCommand7zClass),
732 			NULL,
733 			NULL,
734 			(GClassInitFunc) fr_command_7z_class_init,
735 			NULL,
736 			NULL,
737 			sizeof (FrCommand7z),
738 			0,
739 			(GInstanceInitFunc) fr_command_7z_init
740 		};
741 
742 		type = g_type_register_static (FR_TYPE_COMMAND,
743 					       "FRCommand7z",
744 					       &type_info,
745 					       0);
746 	}
747 
748 	return type;
749 }
750