+/**
+ * comics_regex_quote:
+ * @unquoted_string: a literal string
+ *
+ * Quotes a string so unzip will not interpret the regex expressions of
+ * @unquoted_string. Basically, this functions uses [] to disable regex
+ * expressions. The return value must be freed with * g_free()
+ *
+ * Return value: quoted and disabled-regex string
+ **/
+static gchar *
+comics_regex_quote (const gchar *unquoted_string)
+{
+ const gchar *p;
+ GString *dest;
+
+ dest = g_string_new ("'");
+
+ p = unquoted_string;
+
+ while (*p) {
+ switch (*p) {
+ /* * matches a sequence of 0 or more characters */
+ case ('*'):
+ /* ? matches exactly 1 charactere */
+ case ('?'):
+ /* [...] matches any single character found inside
+ * the brackets. Disabling the first bracket is enough.
+ */
+ case ('['):
+ g_string_append (dest, "[");
+ g_string_append_c (dest, *p);
+ g_string_append (dest, "]");
+ break;
+ /* Because \ escapes regex expressions that we are
+ * disabling for unzip, we need to disable \ too */
+ case ('\\'):
+ g_string_append (dest, "[\\\\]");
+ break;
+ /* Escape single quote inside the string */
+ case ('\''):
+ g_string_append (dest, "'\\''");
+ break;
+ default:
+ g_string_append_c (dest, *p);
+ break;
+ }
+ ++p;
+ }
+ g_string_append_c (dest, '\'');
+ return g_string_free (dest, FALSE);
+}
+
+
+/* This function manages the command for decompressing a comic book */
+static gboolean
+comics_decompress_temp_dir (const gchar *command_decompress_tmp,
+ const gchar *command,
+ GError **error)
+{
+ gboolean success;
+ gchar *std_out, *basename;
+ GError *err = NULL;
+ gint retval;
+
+ success = g_spawn_command_line_sync (command_decompress_tmp, &std_out,
+ NULL, &retval, &err);
+ basename = g_path_get_basename (command);
+ if (!success) {
+ g_set_error (error,
+ EV_DOCUMENT_ERROR,
+ EV_DOCUMENT_ERROR_INVALID,
+ _("Error launching the command “%s” in order to "
+ "decompress the comic book: %s"),
+ basename,
+ err->message);
+ g_error_free (err);
+ } else if (WIFEXITED (retval)) {
+ if (WEXITSTATUS (retval) == EXIT_SUCCESS) {
+ g_free (std_out);
+ g_free (basename);
+ return TRUE;
+ } else {
+ g_set_error (error,
+ EV_DOCUMENT_ERROR,
+ EV_DOCUMENT_ERROR_INVALID,
+ _("The command “%s” failed at "
+ "decompressing the comic book."),
+ basename);
+ g_free (std_out);
+ }
+ } else {
+ g_set_error (error,
+ EV_DOCUMENT_ERROR,
+ EV_DOCUMENT_ERROR_INVALID,
+ _("The command “%s” did not end normally."),
+ basename);
+ g_free (std_out);
+ }
+ g_free (basename);
+ return FALSE;
+}
+
+/* This function shows how to use the choosen command for decompressing a
+ * comic book file. It modifies fields of the ComicsDocument struct with
+ * this information */
+static gboolean
+comics_generate_command_lines (ComicsDocument *comics_document,
+ GError **error)
+{
+ gchar *quoted_file, *quoted_file_aux;
+ gchar *quoted_command;
+ ComicBookDecompressType type;
+
+ type = comics_document->command_usage;
+ comics_document->regex_arg = command_usage_def[type].regex_arg;
+ quoted_command = g_shell_quote (comics_document->selected_command);
+ if (comics_document->regex_arg) {
+ quoted_file = comics_regex_quote (comics_document->archive);
+ quoted_file_aux = g_shell_quote (comics_document->archive);
+ comics_document->list_command =
+ g_strdup_printf (command_usage_def[type].list,
+ comics_document->alternative_command,
+ quoted_file_aux);
+ g_free (quoted_file_aux);
+ } else {
+ quoted_file = g_shell_quote (comics_document->archive);
+ comics_document->list_command =
+ g_strdup_printf (command_usage_def[type].list,
+ quoted_command, quoted_file);
+ }
+ comics_document->extract_command =
+ g_strdup_printf (command_usage_def[type].extract,
+ quoted_command);
+ comics_document->offset = command_usage_def[type].offset;
+ if (command_usage_def[type].decompress_tmp) {
+ comics_document->dir = ev_mkdtemp ("evince-comics-XXXXXX", error);
+ if (comics_document->dir == NULL)
+ return FALSE;
+
+ /* unrar-free can't create directories, but ev_mkdtemp already created the dir */
+
+ comics_document->decompress_tmp =
+ g_strdup_printf (command_usage_def[type].decompress_tmp,
+ quoted_command, quoted_file,
+ comics_document->dir);
+ g_free (quoted_file);
+ g_free (quoted_command);
+
+ if (!comics_decompress_temp_dir (comics_document->decompress_tmp,
+ comics_document->selected_command, error))
+ return FALSE;
+ else
+ return TRUE;
+ } else {
+ g_free (quoted_file);
+ g_free (quoted_command);
+ return TRUE;
+ }
+
+}
+
+/* This function chooses an external command for decompressing a comic
+ * book based on its mime tipe. */
+static gboolean
+comics_check_decompress_command (gchar *mime_type,
+ ComicsDocument *comics_document,
+ GError **error)