diff options
author | Ting-Wei Lan <lantw44@gmail.com> | 2013-07-22 16:10:34 +0800 |
---|---|---|
committer | LAN-TW <lantw44@gmail.com> | 2013-07-22 16:10:34 +0800 |
commit | 37f21a2a97e25b51db40512f9b680deb6e078766 (patch) | |
tree | 224681a31fa26eb67c7a2d54c34ac21a405769fa | |
parent | a502b86e3ec519335b6363d4d4888148f8aede34 (diff) | |
download | gsoc2013-epiphany-37f21a2a97e25b51db40512f9b680deb6e078766.tar gsoc2013-epiphany-37f21a2a97e25b51db40512f9b680deb6e078766.tar.gz gsoc2013-epiphany-37f21a2a97e25b51db40512f9b680deb6e078766.tar.bz2 gsoc2013-epiphany-37f21a2a97e25b51db40512f9b680deb6e078766.tar.lz gsoc2013-epiphany-37f21a2a97e25b51db40512f9b680deb6e078766.tar.xz gsoc2013-epiphany-37f21a2a97e25b51db40512f9b680deb6e078766.tar.zst gsoc2013-epiphany-37f21a2a97e25b51db40512f9b680deb6e078766.zip |
Complete archive preferences support and integrate into AutoarExtract
-rw-r--r-- | autoarchive/Makefile.am | 14 | ||||
-rw-r--r-- | autoarchive/autoar-extract.c | 18 | ||||
-rw-r--r-- | autoarchive/autoar-extract.h | 6 | ||||
-rw-r--r-- | autoarchive/autoar-pref.c | 121 | ||||
-rw-r--r-- | autoarchive/autoar-pref.h | 9 | ||||
-rw-r--r-- | autoarchive/test-extract.c | 21 | ||||
-rw-r--r-- | autoarchive/test-pref.c | 26 |
7 files changed, 197 insertions, 18 deletions
diff --git a/autoarchive/Makefile.am b/autoarchive/Makefile.am index 0593793f0..b51291385 100644 --- a/autoarchive/Makefile.am +++ b/autoarchive/Makefile.am @@ -64,6 +64,20 @@ test_extract_LDADD = \ libautoarchive.la \ $(NULL) +noinst_PROGRAMS += test-pref +EXTRA_DIST += test-pref.c +test_pref_SOURCES = \ + test-pref.c \ + $(NULL) +test_pref_CFLAGS = \ + $(DEPENDENCIES_CFLAGS) \ + $(AM_CFLAGS) \ + $(NULL) +test_pref_LDADD = \ + $(DEPENDENCIES_LIBS) \ + libautoarchive.la \ + $(NULL) + BUILT_SOURCES = \ $(libautoarchive_generated_sources) \ $(NULL) diff --git a/autoarchive/autoar-extract.c b/autoarchive/autoar-extract.c index 0206bf4b7..edb0a6336 100644 --- a/autoarchive/autoar-extract.c +++ b/autoarchive/autoar-extract.c @@ -26,6 +26,7 @@ #include "config.h" #include "autoar-extract.h" +#include "autoar-pref.h" #include <archive.h> #include <archive_entry.h> @@ -842,8 +843,8 @@ autoar_extract_new (const char *source, } void -autoar_extract_start (AutoarExtract* arextract, - const char **pattern) +autoar_extract_start (AutoarExtract *arextract, + AutoarPref *arpref) { struct archive *a; struct archive_entry *entry; @@ -861,6 +862,7 @@ autoar_extract_start (AutoarExtract* arextract, GHashTable *grouphash; GHashTable *bad_filename; + const char **pattern; GPtrArray *pattern_compiled; GFile *source; @@ -881,6 +883,7 @@ autoar_extract_start (AutoarExtract* arextract, arextract->priv->files = 0; arextract->priv->completed_files = 0; + pattern = autoar_pref_get_pattern_to_ignore (arpref); pattern_compiled = g_ptr_array_new_with_free_func (_g_pattern_spec_free); if (pattern != NULL) { for (i = 0; pattern[i] != NULL; i++) @@ -1114,9 +1117,12 @@ autoar_extract_start (AutoarExtract* arextract, /* If the extraction is completed successfully, remove the source file. * Errors are not fatal because we have completed our work. */ g_signal_emit (arextract, autoar_extract_signals[PROGRESS], 0, 1.0, 1.0); - g_debug ("autoar_extract_start: Finalize, Delete"); - source = g_file_new_for_commandline_arg (arextract->priv->source); - g_file_delete (source, NULL, NULL); - g_object_unref (source); + g_debug ("autoar_extract_start: Finalize"); + if (autoar_pref_get_delete_if_succeed (arpref)) { + g_debug ("autoar_extract_start: Delete"); + source = g_file_new_for_commandline_arg (arextract->priv->source); + g_file_delete (source, NULL, NULL); + g_object_unref (source); + } g_signal_emit (arextract, autoar_extract_signals[COMPLETED], 0); } diff --git a/autoarchive/autoar-extract.h b/autoarchive/autoar-extract.h index 37c335ed5..455a89726 100644 --- a/autoarchive/autoar-extract.h +++ b/autoarchive/autoar-extract.h @@ -29,6 +29,8 @@ #include <glib-object.h> #include <gio/gio.h> +#include "autoar-pref.h" + G_BEGIN_DECLS #define AUTOAR_TYPE_EXTRACT autoar_extract_get_type () @@ -70,8 +72,8 @@ GType autoar_extract_get_type (void) G_GNUC_CONST; AutoarExtract *autoar_extract_new (const char *source, const char *output); -void autoar_extract_start (AutoarExtract* arextract, - const char **pattern); +void autoar_extract_start (AutoarExtract *arextract, + AutoarPref *arpref); char *autoar_extract_get_source (AutoarExtract *arextract); char *autoar_extract_get_output (AutoarExtract *arextract); diff --git a/autoarchive/autoar-pref.c b/autoarchive/autoar-pref.c index 4b6c7ba5b..4f4a07fad 100644 --- a/autoarchive/autoar-pref.c +++ b/autoarchive/autoar-pref.c @@ -28,9 +28,9 @@ #include "autoar-pref.h" #include "autoar-enum-types.h" -#include <glib.h> #include <gio/gio.h> - +#include <glib.h> +#include <string.h> G_DEFINE_TYPE (AutoarPref, autoar_pref, G_TYPE_OBJECT) @@ -437,10 +437,57 @@ autoar_pref_read_gsettings (AutoarPref *arpref, arpref->priv->modification_flags = MODIFIED_NONE; } -gboolean autoar_pref_write_gsettings (AutoarPref *arpref, - GSettings *settings); -gboolean autoar_pref_write_gsettings_all (AutoarPref *arpref, - GSettings *settings); +void +autoar_pref_write_gsettings (AutoarPref *arpref, + GSettings *settings) +{ + g_return_if_fail (AUTOAR_IS_PREF (arpref)); + g_return_if_fail (settings != NULL); + + if (arpref->priv->modification_enabled) { + if (arpref->priv->modification_flags & MODIFIED_DEFAULT_FORMAT) { + if (g_settings_set_enum (settings, KEY_DEFAULT_FORMAT, arpref->priv->default_format)) + arpref->priv->modification_flags ^= MODIFIED_DEFAULT_FORMAT; + } + if (arpref->priv->modification_flags & MODIFIED_DEFAULT_FILTER) { + if (g_settings_set_enum (settings, KEY_DEFAULT_FILTER, arpref->priv->default_filter)) + arpref->priv->modification_flags ^= MODIFIED_DEFAULT_FILTER; + } + if (arpref->priv->modification_flags & MODIFIED_FILE_NAME_SUFFIX) { + if (g_settings_set_strv (settings, KEY_FILE_NAME_SUFFIX, (const char* const*)(arpref->priv->file_name_suffix))) + arpref->priv->modification_flags ^= MODIFIED_FILE_NAME_SUFFIX; + } + if (arpref->priv->modification_flags & MODIFIED_FILE_MIME_TYPE) { + if (g_settings_set_strv (settings, KEY_FILE_MIME_TYPE, (const char* const*)(arpref->priv->file_mime_type))) + arpref->priv->modification_flags ^= MODIFIED_FILE_MIME_TYPE; + } + if (arpref->priv->modification_flags & MODIFIED_PATTERN_TO_IGNORE) { + if (g_settings_set_strv (settings, KEY_PATTERN_TO_IGNORE, (const char* const*)(arpref->priv->pattern_to_ignore))) + arpref->priv->modification_flags ^= MODIFIED_PATTERN_TO_IGNORE; + } + if (arpref->priv->modification_flags & MODIFIED_DELETE_IF_SUCCEED) { + if (g_settings_set_boolean (settings, KEY_DELETE_IF_SUCCEED, arpref->priv->delete_if_succeed)) + arpref->priv->modification_flags ^= MODIFIED_DELETE_IF_SUCCEED; + } + } else { + return autoar_pref_write_gsettings_force (arpref, settings); + } +} + +void +autoar_pref_write_gsettings_force (AutoarPref *arpref, + GSettings *settings) +{ + g_return_if_fail (AUTOAR_IS_PREF (arpref)); + g_return_if_fail (settings != NULL); + + g_settings_set_enum (settings, KEY_DEFAULT_FORMAT, arpref->priv->default_format); + g_settings_set_enum (settings, KEY_DEFAULT_FILTER, arpref->priv->default_filter); + g_settings_set_strv (settings, KEY_FILE_NAME_SUFFIX, (const char* const*)(arpref->priv->file_name_suffix)); + g_settings_set_strv (settings, KEY_FILE_MIME_TYPE, (const char* const*)(arpref->priv->file_mime_type)); + g_settings_set_strv (settings, KEY_PATTERN_TO_IGNORE, (const char* const*)(arpref->priv->pattern_to_ignore)); + g_settings_set_boolean (settings, KEY_DELETE_IF_SUCCEED, arpref->priv->delete_if_succeed); +} gboolean autoar_pref_has_changes (AutoarPref *arpref) @@ -455,3 +502,65 @@ autoar_pref_forget_changes (AutoarPref *arpref) g_return_if_fail (AUTOAR_IS_PREF (arpref)); arpref->priv->modification_flags = MODIFIED_NONE; } + +gboolean +autoar_pref_check_file_name (AutoarPref *arpref, + const char *filepath) +{ + char *dot_location; + int i; + + g_return_val_if_fail (AUTOAR_IS_PREF (arpref), FALSE); + g_return_val_if_fail (arpref->priv->file_name_suffix != NULL, FALSE); + + dot_location = strrchr (filepath, '.'); + if (dot_location == NULL) + return FALSE; + + for (i = 0; arpref->priv->file_name_suffix[i] != NULL; i++) { + if (strcmp (dot_location + 1, arpref->priv->file_name_suffix[i]) == 0) + return TRUE; + } + + return FALSE; +} + +gboolean +autoar_pref_check_mime_type (AutoarPref *arpref, + const char *filepath) +{ + int i; + GFile *file; + GFileInfo *fileinfo; + const char *content_type; + const char *mime_type; + + g_return_val_if_fail (AUTOAR_IS_PREF (arpref), FALSE); + g_return_val_if_fail (arpref->priv->file_mime_type != NULL, FALSE); + + file = g_file_new_for_commandline_arg (filepath); + fileinfo = g_file_query_info (file, + G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, + G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, + NULL, + NULL); + g_object_unref (file); + + if (fileinfo == NULL) + return FALSE; + + content_type = g_file_info_get_content_type (fileinfo); + g_debug ("Content Type: %s\n", content_type); + mime_type = g_content_type_get_mime_type (content_type); + g_debug ("MIME Type: %s\n", mime_type); + + for (i = 0; arpref->priv->file_mime_type[i] != NULL; i++) { + if (strcmp (mime_type, arpref->priv->file_mime_type[i]) == 0) { + g_object_unref (fileinfo); + return TRUE; + } + } + + g_object_unref (fileinfo); + return FALSE; +} diff --git a/autoarchive/autoar-pref.h b/autoarchive/autoar-pref.h index 8d904240e..357f0022c 100644 --- a/autoarchive/autoar-pref.h +++ b/autoarchive/autoar-pref.h @@ -84,14 +84,19 @@ AutoarPref *autoar_pref_new_with_gsettings (GSettings *settings); void autoar_pref_read_gsettings (AutoarPref *arpref, GSettings *settings); -gboolean autoar_pref_write_gsettings (AutoarPref *arpref, +void autoar_pref_write_gsettings (AutoarPref *arpref, GSettings *settings); -gboolean autoar_pref_write_gsettings_all (AutoarPref *arpref, +void autoar_pref_write_gsettings_force (AutoarPref *arpref, GSettings *settings); gboolean autoar_pref_has_changes (AutoarPref *arpref); void autoar_pref_forget_changes (AutoarPref *arpref); +gboolean autoar_pref_check_file_name (AutoarPref *arpref, + const char *filepath); +gboolean autoar_pref_check_mime_type (AutoarPref *arpref, + const char *filepath); + AutoarPrefFormat autoar_pref_get_default_format (AutoarPref *arpref); AutoarPrefFilter autoar_pref_get_default_filter (AutoarPref *arpref); const char **autoar_pref_get_file_name_suffix (AutoarPref *arpref); diff --git a/autoarchive/test-extract.c b/autoarchive/test-extract.c index 18728fbcf..fb70e9c5a 100644 --- a/autoarchive/test-extract.c +++ b/autoarchive/test-extract.c @@ -1,6 +1,7 @@ /* vim: set sw=2 ts=2 sts=2 et: */ -#include "autoar-extract.h" +#include <autoarchive/autoarchive.h> +#include <gio/gio.h> #include <stdlib.h> static void @@ -58,10 +59,13 @@ main (int argc, char *argv[]) { AutoarExtract *arextract; + AutoarPref *arpref; + GSettings *settings; const char *pattern[] = { "__MACOSX", ".DS_Store", "._.*", + "*.in", NULL }; @@ -70,6 +74,15 @@ main (int argc, return 255; } + settings = g_settings_new (AUTOAR_PREF_DEFAULT_GSCHEMA_ID); + + arpref = autoar_pref_new_with_gsettings (settings); + autoar_pref_set_delete_if_succeed (arpref, FALSE); + autoar_pref_set_pattern_to_ignore (arpref, pattern); + + autoar_pref_forget_changes (arpref); + autoar_pref_write_gsettings (arpref, settings); + arextract = autoar_extract_new (argv[1], argv[2]); g_signal_connect (arextract, "scanned", G_CALLBACK (my_handler_scanned), NULL); g_signal_connect (arextract, "decide-dest", G_CALLBACK (my_handler_decide_dest), NULL); @@ -77,7 +90,11 @@ main (int argc, g_signal_connect (arextract, "error", G_CALLBACK (my_handler_error), NULL); g_signal_connect (arextract, "completed", G_CALLBACK (my_handler_completed), NULL); - autoar_extract_start (arextract, pattern); + autoar_extract_start (arextract, arpref); + + g_object_unref (arextract); + g_object_unref (arpref); + g_object_unref (settings); return 0; } diff --git a/autoarchive/test-pref.c b/autoarchive/test-pref.c new file mode 100644 index 000000000..449e5eec8 --- /dev/null +++ b/autoarchive/test-pref.c @@ -0,0 +1,26 @@ +/* vim: set sw=2 ts=2 sts=2 et: */ + +#include <autoarchive/autoarchive.h> + +int +main (int argc, + char *argv[]) +{ + AutoarPref *arpref; + GSettings *settings; + + if (argc < 2) { + g_printerr ("Usage: %s archive_file\n", argv[0]); + return 255; + } + + settings = g_settings_new (AUTOAR_PREF_DEFAULT_GSCHEMA_ID); + arpref = autoar_pref_new_with_gsettings (settings); + + g_print ("file-name-suffix check: %d\n", autoar_pref_check_file_name (arpref, argv[1])); + g_print ("file-mime-type check: %d\n", autoar_pref_check_mime_type (arpref, argv[1])); + + g_object_unref (arpref); + + return 0; +} |