diff options
-rw-r--r-- | autoarchive/Makefile.am | 12 | ||||
-rw-r--r-- | autoarchive/autoar-pref.c | 206 | ||||
-rw-r--r-- | autoarchive/autoar-pref.h | 13 | ||||
-rw-r--r-- | autoarchive/org.gnome.desktop.archives.gschema.xml | 35 |
4 files changed, 188 insertions, 78 deletions
diff --git a/autoarchive/Makefile.am b/autoarchive/Makefile.am index 7bd48d54a..0593793f0 100644 --- a/autoarchive/Makefile.am +++ b/autoarchive/Makefile.am @@ -5,10 +5,18 @@ NULL = noinst_PROGRAMS = EXTRA_DIST = \ - autoar-enum-types.h.template \ - autoar-enum-types.c.template \ + autoar-enum-types.h.template \ + autoar-enum-types.c.template \ + org.gnome.desktop.archives.gschema.xml \ $(NULL) +gsettings_ENUM_NAMESPACE = org.gnome.desktop.archives +gsettings_ENUM_FILES = $(top_srcdir)/autoarchive/autoar-pref.h + +gsettings_SCHEMAS = org.gnome.desktop.archives.gschema.xml + +@GSETTINGS_RULES@ + noinst_LTLIBRARIES = libautoarchive.la libautoarchive_h_sources = \ diff --git a/autoarchive/autoar-pref.c b/autoarchive/autoar-pref.c index cc2c71a0c..4b6c7ba5b 100644 --- a/autoarchive/autoar-pref.c +++ b/autoarchive/autoar-pref.c @@ -39,14 +39,17 @@ G_DEFINE_TYPE (AutoarPref, autoar_pref, G_TYPE_OBJECT) struct _AutoarPrefPrivate { + unsigned int modification_flags; + gboolean modification_enabled; + /* Archive creating preferences */ AutoarPrefFormat default_format; AutoarPrefFilter default_filter; /* Archive extracting preferences */ - GPtrArray *file_name_suffix; - GPtrArray *file_mime_type; - GPtrArray *pattern_to_ignore; + char **file_name_suffix; + char **file_mime_type; + char **pattern_to_ignore; gboolean delete_if_succeed; }; @@ -61,6 +64,24 @@ enum PROP_DELETE_IF_SUCCEED }; +enum +{ + MODIFIED_NONE = 0, + MODIFIED_DEFAULT_FORMAT = 1 << 0, + MODIFIED_DEFAULT_FILTER = 1 << 1, + MODIFIED_FILE_NAME_SUFFIX = 1 << 2, + MODIFIED_FILE_MIME_TYPE = 1 << 3, + MODIFIED_PATTERN_TO_IGNORE = 1 << 4, + MODIFIED_DELETE_IF_SUCCEED = 1 << 5 +}; + +#define KEY_DEFAULT_FORMAT "default-format" +#define KEY_DEFAULT_FILTER "default-filter" +#define KEY_FILE_NAME_SUFFIX "file-name-suffix" +#define KEY_FILE_MIME_TYPE "file-mime-type" +#define KEY_PATTERN_TO_IGNORE "pattern-to-ignore" +#define KEY_DELETE_IF_SUCCEED "delete-if-succeed" + static void autoar_pref_get_property (GObject *object, guint property_id, @@ -73,7 +94,6 @@ autoar_pref_get_property (GObject *object, GVariant *variant; const char* const* strv; - gssize len; arpref = AUTOAR_PREF (object); priv = arpref->priv; @@ -86,21 +106,18 @@ autoar_pref_get_property (GObject *object, g_value_set_enum (value, priv->default_filter); break; case PROP_FILE_NAME_SUFFIX: - strv = (const char* const*)(priv->file_name_suffix->pdata); - len = (gssize)(priv->file_name_suffix->len - 1); - variant = g_variant_new_strv (strv, len); + strv = (const char* const*)(priv->file_name_suffix); + variant = g_variant_new_strv (strv, -1); g_value_take_variant (value, variant); break; case PROP_FILE_MIME_TYPE: - strv = (const char* const*)(priv->file_mime_type->pdata); - len = (gssize)(priv->file_mime_type->len - 1); - variant = g_variant_new_strv (strv, len); + strv = (const char* const*)(priv->file_mime_type); + variant = g_variant_new_strv (strv, -1); g_value_take_variant (value, variant); break; case PROP_PATTERN_TO_IGNORE: - strv = (const char* const*)(priv->pattern_to_ignore->pdata); - len = (gssize)(priv->pattern_to_ignore->len - 1); - variant = g_variant_new_strv (strv, len); + strv = (const char* const*)(priv->pattern_to_ignore); + variant = g_variant_new_strv (strv, -1); g_value_take_variant (value, variant); break; case PROP_DELETE_IF_SUCCEED: @@ -119,12 +136,9 @@ autoar_pref_set_property (GObject *object, GParamSpec *pspec) { AutoarPref *arpref; - AutoarPrefPrivate *priv; const char **strv; - gsize len; arpref = AUTOAR_PREF (object); - priv = arpref->priv; switch (property_id) { case PROP_DEFAULT_FORMAT: @@ -134,19 +148,19 @@ autoar_pref_set_property (GObject *object, autoar_pref_set_default_filter (arpref, g_value_get_enum (value)); break; case PROP_FILE_NAME_SUFFIX: - strv = g_variant_get_strv (g_value_get_variant (value), &len); - autoar_pref_set_file_name_suffix (arpref, strv, len); + strv = g_variant_get_strv (g_value_get_variant (value), NULL); + autoar_pref_set_file_name_suffix (arpref, strv); break; case PROP_FILE_MIME_TYPE: strv = g_variant_get_strv (g_value_get_variant (value), NULL); - autoar_pref_set_file_mime_type (arpref, strv, len); + autoar_pref_set_file_mime_type (arpref, strv); break; case PROP_PATTERN_TO_IGNORE: - strv = g_variant_get_strv (g_value_get_variant (value), &len); - autoar_pref_set_pattern_to_ignore (arpref, strv, len); + strv = g_variant_get_strv (g_value_get_variant (value), NULL); + autoar_pref_set_pattern_to_ignore (arpref, strv); break; case PROP_DELETE_IF_SUCCEED: - priv->delete_if_succeed = g_value_get_boolean (value); + autoar_pref_set_delete_if_succeed (arpref, g_value_get_boolean (value)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); @@ -172,21 +186,21 @@ const char** autoar_pref_get_file_name_suffix (AutoarPref *arpref) { g_return_val_if_fail (AUTOAR_IS_PREF (arpref), NULL); - return (const char**)(arpref->priv->file_name_suffix->pdata); + return (const char**)(arpref->priv->file_name_suffix); } const char** autoar_pref_get_file_mime_type (AutoarPref *arpref) { g_return_val_if_fail (AUTOAR_IS_PREF (arpref), NULL); - return (const char**)(arpref->priv->file_mime_type->pdata); + return (const char**)(arpref->priv->file_mime_type); } const char** autoar_pref_get_pattern_to_ignore (AutoarPref *arpref) { g_return_val_if_fail (AUTOAR_IS_PREF (arpref), NULL); - return (const char**)(arpref->priv->pattern_to_ignore->pdata); + return (const char**)(arpref->priv->pattern_to_ignore); } gboolean @@ -202,6 +216,8 @@ autoar_pref_set_default_format (AutoarPref *arpref, { g_return_if_fail (AUTOAR_IS_PREF (arpref)); g_return_if_fail (format > 0 && format < AUTOAR_PREF_FORMAT_LAST); + if (arpref->priv->modification_enabled && format != arpref->priv->default_format) + arpref->priv->modification_flags |= MODIFIED_DEFAULT_FORMAT; arpref->priv->default_format = format; } @@ -211,59 +227,57 @@ autoar_pref_set_default_filter (AutoarPref *arpref, { g_return_if_fail (AUTOAR_IS_PREF (arpref)); g_return_if_fail (filter > 0 && filter < AUTOAR_PREF_FILTER_LAST); + if (arpref->priv->modification_enabled && filter != arpref->priv->default_filter) + arpref->priv->modification_flags |= MODIFIED_DEFAULT_FILTER; arpref->priv->default_filter = filter; } - -static void -autoar_pref_set_strv (AutoarPref *arpref, - GPtrArray **ptr, - const char **strv, - size_t len) -{ - int i; - - g_strfreev ((char**)g_ptr_array_free (*ptr, FALSE)); - - if (len > 0) - *ptr = g_ptr_array_sized_new (len + 1); - else - *ptr = g_ptr_array_new (); - - for (i = 0; strv[i] != NULL; i++) - g_ptr_array_add (*ptr, g_strdup (strv[i])); - - g_ptr_array_add (*ptr, NULL); -} - void autoar_pref_set_file_name_suffix (AutoarPref *arpref, - const char **strv, - size_t len) + const char **strv) { g_return_if_fail (AUTOAR_IS_PREF (arpref)); g_return_if_fail (strv != NULL); - autoar_pref_set_strv (arpref, &(arpref->priv->file_name_suffix), strv, len); + if (arpref->priv->modification_enabled) + arpref->priv->modification_flags |= MODIFIED_FILE_NAME_SUFFIX; + g_strfreev (arpref->priv->file_name_suffix); + arpref->priv->file_name_suffix = g_strdupv ((char**)strv); } void autoar_pref_set_file_mime_type (AutoarPref *arpref, - const char **strv, - size_t len) + const char **strv) { g_return_if_fail (AUTOAR_IS_PREF (arpref)); g_return_if_fail (strv != NULL); - autoar_pref_set_strv (arpref, &(arpref->priv->file_mime_type), strv, len); + if (arpref->priv->modification_enabled) + arpref->priv->modification_flags |= MODIFIED_FILE_MIME_TYPE; + g_strfreev (arpref->priv->file_mime_type); + arpref->priv->file_mime_type = g_strdupv ((char**)strv); } void autoar_pref_set_pattern_to_ignore (AutoarPref *arpref, - const char **strv, - size_t len) + const char **strv) { g_return_if_fail (AUTOAR_IS_PREF (arpref)); g_return_if_fail (strv != NULL); - autoar_pref_set_strv (arpref, &(arpref->priv->pattern_to_ignore), strv, len); + if (arpref->priv->modification_enabled) + arpref->priv->modification_flags |= MODIFIED_PATTERN_TO_IGNORE; + g_strfreev (arpref->priv->pattern_to_ignore); + arpref->priv->pattern_to_ignore = g_strdupv ((char**)strv); +} + +void +autoar_pref_set_delete_if_succeed (AutoarPref *arpref, + gboolean delete_yes) +{ + g_return_if_fail (AUTOAR_IS_PREF (arpref)); + if (delete_yes) + delete_yes = TRUE; + if (arpref->priv->modification_enabled && delete_yes != arpref->priv->delete_if_succeed) + arpref->priv->modification_flags |= MODIFIED_DELETE_IF_SUCCEED; + arpref->priv->delete_if_succeed = delete_yes; } static void @@ -275,9 +289,9 @@ autoar_pref_finalize (GObject *object) arpref = AUTOAR_PREF (object); priv = arpref->priv; - g_strfreev ((char**)g_ptr_array_free (priv->file_name_suffix, FALSE)); - g_strfreev ((char**)g_ptr_array_free (priv->file_mime_type, FALSE)); - g_strfreev ((char**)g_ptr_array_free (priv->pattern_to_ignore, FALSE)); + g_strfreev (priv->file_name_suffix); + g_strfreev (priv->file_mime_type); + g_strfreev (priv->pattern_to_ignore); G_OBJECT_CLASS (autoar_pref_parent_class)->finalize (object); } @@ -297,9 +311,9 @@ autoar_pref_class_init (AutoarPrefClass *klass) object_class->finalize = autoar_pref_finalize; g_object_class_install_property (object_class, PROP_DEFAULT_FORMAT, - g_param_spec_enum ("default-format", + g_param_spec_enum (KEY_DEFAULT_FORMAT, "Default format", - "Default format to create archives", + "Default file format for new archives", AUTOAR_TYPE_PREF_FORMAT, AUTOAR_PREF_FORMAT_ZIP, G_PARAM_READWRITE | @@ -308,7 +322,7 @@ autoar_pref_class_init (AutoarPrefClass *klass) G_PARAM_STATIC_BLURB)); g_object_class_install_property (object_class, PROP_DEFAULT_FILTER, - g_param_spec_enum ("default-filter", + g_param_spec_enum (KEY_DEFAULT_FILTER, "Default format", "Default filter to create archives", AUTOAR_TYPE_PREF_FORMAT, @@ -322,7 +336,7 @@ autoar_pref_class_init (AutoarPrefClass *klass) g_ptr_array_add (tmparr, NULL); g_object_class_install_property (object_class, PROP_FILE_NAME_SUFFIX, - g_param_spec_variant ("file-name-suffix", + g_param_spec_variant (KEY_FILE_NAME_SUFFIX, "File name suffix", "File name suffix whitelist for automatic extraction", G_VARIANT_TYPE_STRING_ARRAY, @@ -333,7 +347,7 @@ autoar_pref_class_init (AutoarPrefClass *klass) G_PARAM_STATIC_BLURB)); g_object_class_install_property (object_class, PROP_FILE_MIME_TYPE, - g_param_spec_variant ("file-mime-type", + g_param_spec_variant (KEY_FILE_MIME_TYPE, "File MIME type", "File MIME type whitelist for automatic extraction", G_VARIANT_TYPE_STRING_ARRAY, @@ -344,7 +358,7 @@ autoar_pref_class_init (AutoarPrefClass *klass) G_PARAM_STATIC_BLURB)); g_object_class_install_property (object_class, PROP_PATTERN_TO_IGNORE, - g_param_spec_variant ("pattern-to-ignore", + g_param_spec_variant (KEY_PATTERN_TO_IGNORE, "Pattern to ignore", "Pattern of file name to skip when extracting files", G_VARIANT_TYPE_STRING_ARRAY, @@ -355,7 +369,7 @@ autoar_pref_class_init (AutoarPrefClass *klass) G_PARAM_STATIC_BLURB)); g_object_class_install_property (object_class, PROP_DELETE_IF_SUCCEED, - g_param_spec_boolean ("delete-if-succeed", + g_param_spec_boolean (KEY_DELETE_IF_SUCCEED, "Delete if succeed", "Delete the archive file if extraction is succeeded", TRUE, @@ -373,12 +387,15 @@ autoar_pref_init (AutoarPref *arpref) priv = AUTOAR_PREF_GET_PRIVATE (arpref); arpref->priv = priv; + priv->modification_flags = MODIFIED_NONE; + priv->modification_enabled = FALSE; + priv->default_format = AUTOAR_PREF_FORMAT_ZIP; priv->default_filter = AUTOAR_PREF_FILTER_NONE; - priv->file_name_suffix = g_ptr_array_new (); - priv->file_mime_type = g_ptr_array_new (); - priv->pattern_to_ignore = g_ptr_array_new (); + priv->file_name_suffix = NULL; + priv->file_mime_type = NULL; + priv->pattern_to_ignore = NULL; priv->delete_if_succeed = TRUE; } @@ -387,3 +404,54 @@ autoar_pref_new (void) { return g_object_new (AUTOAR_TYPE_PREF, NULL); } + +AutoarPref* +autoar_pref_new_with_gsettings (GSettings *settings) +{ + AutoarPref *arpref; + arpref = autoar_pref_new (); + autoar_pref_read_gsettings (arpref, settings); + return arpref; +} + +void +autoar_pref_read_gsettings (AutoarPref *arpref, + GSettings *settings) +{ + g_return_if_fail (AUTOAR_IS_PREF (arpref)); + g_return_if_fail (settings != NULL); + + arpref->priv->default_format = g_settings_get_enum (settings, KEY_DEFAULT_FORMAT); + arpref->priv->default_filter = g_settings_get_enum (settings, KEY_DEFAULT_FILTER); + + g_strfreev (arpref->priv->file_name_suffix); + arpref->priv->file_name_suffix = g_settings_get_strv (settings, KEY_FILE_NAME_SUFFIX); + g_strfreev (arpref->priv->file_mime_type); + arpref->priv->file_mime_type = g_settings_get_strv (settings, KEY_FILE_MIME_TYPE); + g_strfreev (arpref->priv->pattern_to_ignore); + arpref->priv->pattern_to_ignore = g_settings_get_strv (settings, KEY_PATTERN_TO_IGNORE); + + arpref->priv->delete_if_succeed = g_settings_get_boolean (settings, KEY_DELETE_IF_SUCCEED); + + arpref->priv->modification_enabled = TRUE; + 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); + +gboolean +autoar_pref_has_changes (AutoarPref *arpref) +{ + g_return_val_if_fail (AUTOAR_IS_PREF (arpref), FALSE); + return (arpref->priv->modification_enabled && arpref->priv->modification_flags); +} + +void +autoar_pref_forget_changes (AutoarPref *arpref) +{ + g_return_if_fail (AUTOAR_IS_PREF (arpref)); + arpref->priv->modification_flags = MODIFIED_NONE; +} diff --git a/autoarchive/autoar-pref.h b/autoarchive/autoar-pref.h index b549d66ba..8d904240e 100644 --- a/autoarchive/autoar-pref.h +++ b/autoarchive/autoar-pref.h @@ -31,6 +31,8 @@ G_BEGIN_DECLS +#define AUTOAR_PREF_DEFAULT_GSCHEMA_ID "org.gnome.desktop.archives" + typedef enum { AUTOAR_PREF_FORMAT_0, /*< skip >*/ AUTOAR_PREF_FORMAT_ZIP, @@ -80,7 +82,7 @@ GType autoar_pref_get_type (void) G_GNUC_CONST; AutoarPref *autoar_pref_new (void); AutoarPref *autoar_pref_new_with_gsettings (GSettings *settings); -gboolean autoar_pref_read_gsettings (AutoarPref *arpref, +void autoar_pref_read_gsettings (AutoarPref *arpref, GSettings *settings); gboolean autoar_pref_write_gsettings (AutoarPref *arpref, GSettings *settings); @@ -102,14 +104,11 @@ void autoar_pref_set_default_format (AutoarPref *arpref, void autoar_pref_set_default_filter (AutoarPref *arpref, AutoarPrefFilter filter); void autoar_pref_set_file_name_suffix (AutoarPref *arpref, - const char **strv, - size_t len); + const char **strv); void autoar_pref_set_file_mime_type (AutoarPref *arpref, - const char **strv, - size_t len); + const char **strv); void autoar_pref_set_pattern_to_ignore (AutoarPref *arpref, - const char **strv, - size_t len); + const char **strv); void autoar_pref_set_delete_if_succeed (AutoarPref *arpref, gboolean delete_yes); diff --git a/autoarchive/org.gnome.desktop.archives.gschema.xml b/autoarchive/org.gnome.desktop.archives.gschema.xml new file mode 100644 index 000000000..f026c2f05 --- /dev/null +++ b/autoarchive/org.gnome.desktop.archives.gschema.xml @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="UTF-8"?> +<schemalist> + <schema path="/org/gnome/desktop/archives/" id="org.gnome.desktop.archives"> + <key name="default-format" enum="org.gnome.desktop.archives.AutoarPrefFormat"> + <default>'zip'</default> + <summary>Default file format for new archives (archive creation)</summary> + <description>The default file format used to create new archives.</description> + </key> + <key name="default-filter" enum="org.gnome.desktop.archives.AutoarPrefFilter"> + <default>'none'</default> + <summary>Default filter to create archives (archive creation)</summary> + <description>The default filter used to compress new archives.</description> + </key> + <key name="file-name-suffix" type="as"> + <default>['zip', '7z', 'rar', 'lha', 'lzh', 'xar', 'Z', 'gz', 'bzip2', 'uue', 'xz', 'lzip', 'lzma']</default> + <summary>File extensions of archives (archive extraction)</summary> + <description>This list is used by applications to determine whether a file should be automatically extracted.</description> + </key> + <key name="file-mime-type" type="as"> + <default>['application/x-7z-compressed', 'application/x-7z-compressed-tar', 'application/x-bzip', 'application/x-bzip-compressed-tar', 'application/x-compress', 'application/x-compressed-tar', 'application/x-cpio', 'application/x-gzip', 'application/x-lha', 'application/x-lzip', 'application/x-lzip-compressed-tar', 'application/x-lzma', 'application/x-lzma-compressed-tar', 'application/x-rar', 'application/x-tar', 'application/x-tarz', 'application/x-xar', 'application/x-xz', 'application/x-xz-compressed-tar', 'application/zip']</default> + <summary>>File MIME types of archives (archive extraction)</summary> + <description>This list is used by applications to determine whether a file should be automatically extracted.</description> + </key> + <key name="pattern-to-ignore" type="as"> + <default>['__MACOSX', '.DS_Store', '._.*']</default> + <summary>Patterns of file names will not be extracted (archive extraction)</summary> + <description>This list contains glob-style patterns, and it is used by applications to determine whether an entry in an archive should be written to disk.</description> + </key> + <key name="delete-if-succeed" type="b"> + <default>true</default> + <summary>Whether to delete the source archive if the extraction is successful (archive extraction)</summary> + <description>If TRUE, the source archive will be deleted after the extraction succeed</description> + </key> + </schema> +</schemalist> |