[easytag/wip/application-window: 3/10] Store the file extension case mode in GSettings



commit f0aa90a305ca8bdebf55834d2d6340b4ce0f3eb0
Author: David King <amigadave amigadave com>
Date:   Fri May 9 09:12:37 2014 +0100

    Store the file extension case mode in GSettings

 data/org.gnome.EasyTAG.gschema.xml |    2 +
 src/et_core.c                      |   21 +++++++---
 src/preferences_dialog.c           |   28 ++++++++++++-
 src/preferences_dialog.h           |    4 --
 src/setting.c                      |   72 ++++++++++++++++++++++++++++++-----
 src/setting.h                      |   16 ++++++--
 6 files changed, 114 insertions(+), 29 deletions(-)
---
diff --git a/data/org.gnome.EasyTAG.gschema.xml b/data/org.gnome.EasyTAG.gschema.xml
index 7addb72..6f0a0d3 100644
--- a/data/org.gnome.EasyTAG.gschema.xml
+++ b/data/org.gnome.EasyTAG.gschema.xml
@@ -696,6 +696,7 @@
       files</description>
       <default>'spaces'</default>
     </key>
+    -->
 
     <key name="rename-extension-mode" enum="org.gnome.EasyTAG.EtFilenameExtensionMode">
       <summary>How to modify filename extensions when renaming</summary>
@@ -704,6 +705,7 @@
       <default>'lower-case'</default>
     </key>
 
+    <!-- TODO: Generate enums with glib-mkenums.
     <key name="rename-encoding" enum="org.gnome.EasyTAG.EtRenameEncoding">
       <summary>Encoding options when renaming files</summary>
       <description>If the encoding of the filename does not match the
diff --git a/src/et_core.c b/src/et_core.c
index 3101b47..99e8b6c 100644
--- a/src/et_core.c
+++ b/src/et_core.c
@@ -4964,18 +4964,25 @@ gchar *ET_File_Name_Generate (ET_File *ETFile, gchar *new_file_name_utf8)
 #endif
 
 
+/* Convert filename extension (lower/upper/no change). */
 static gchar *
 ET_File_Name_Format_Extension (ET_File *ETFile)
 {
-    // Convert filename extension (lower/upper/no change)
-    if (FILENAME_EXTENSION_LOWER_CASE)
-        return g_utf8_strdown(ETFile->ETFileDescription->Extension,-1);
+    EtFilenameExtensionMode mode;
 
-    else if (FILENAME_EXTENSION_UPPER_CASE)
-        return g_utf8_strup(ETFile->ETFileDescription->Extension,-1);
+    mode = g_settings_get_enum (MainSettings, "rename-extension-mode");
 
-    else // FILENAME_EXTENSION_NO_CHANGE
-        return g_strdup(ETFile->ETFileExtension);
+    switch (mode)
+    {
+        /* FIXME: Should use filename encoding, not UTF-8! */
+        case ET_FILENAME_EXTENSION_LOWER_CASE:
+            return g_utf8_strdown (ETFile->ETFileDescription->Extension, -1);
+        case ET_FILENAME_EXTENSION_UPPER_CASE:
+            return g_utf8_strup (ETFile->ETFileDescription->Extension, -1);
+        case ET_FILENAME_EXTENSION_NO_CHANGE:
+        default:
+            return g_strdup (ETFile->ETFileExtension);
+    };
 }
 
 
diff --git a/src/preferences_dialog.c b/src/preferences_dialog.c
index 2fb734f..12a0762 100644
--- a/src/preferences_dialog.c
+++ b/src/preferences_dialog.c
@@ -172,6 +172,9 @@ create_preferences_dialog (EtPreferencesDialog *self)
     GtkWidget *ConfirmDeleteFile;
     GtkWidget *ConfirmWritePlayList;
     GtkWidget *ConfirmWhenUnsavedFiles;
+    GtkWidget *FilenameExtensionNoChange;
+    GtkWidget *FilenameExtensionLowerCase;
+    GtkWidget *FilenameExtensionUpperCase;
     gchar *path_utf8;
     gchar *program_path;
     gchar *default_comment;
@@ -522,22 +525,41 @@ create_preferences_dialog (EtPreferencesDialog *self)
     gtk_box_pack_start(GTK_BOX(hbox),Label,FALSE,FALSE,0);
 
     FilenameExtensionLowerCase = gtk_radio_button_new_with_label(NULL,_("Lower Case"));
+    gtk_widget_set_name (FilenameExtensionLowerCase, "lower-case");
     gtk_box_pack_start(GTK_BOX(hbox),FilenameExtensionLowerCase,FALSE,FALSE,2);
-    
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(FilenameExtensionLowerCase),FILENAME_EXTENSION_LOWER_CASE);
     gtk_widget_set_tooltip_text(FilenameExtensionLowerCase,_("For example, the extension will be converted 
to '.mp3'"));
 
     FilenameExtensionUpperCase = gtk_radio_button_new_with_label(
         gtk_radio_button_get_group(GTK_RADIO_BUTTON(FilenameExtensionLowerCase)),_("Upper Case"));
+    gtk_widget_set_name (FilenameExtensionUpperCase, "upper-case");
     gtk_box_pack_start(GTK_BOX(hbox),FilenameExtensionUpperCase,FALSE,FALSE,2);
-    
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(FilenameExtensionUpperCase),FILENAME_EXTENSION_UPPER_CASE);
     gtk_widget_set_tooltip_text(FilenameExtensionUpperCase,_("For example, the extension will be converted 
to '.MP3'"));
 
     FilenameExtensionNoChange = gtk_radio_button_new_with_label(
         gtk_radio_button_get_group(GTK_RADIO_BUTTON(FilenameExtensionLowerCase)),_("No Change"));
+    gtk_widget_set_name (FilenameExtensionNoChange, "no-change");
     gtk_box_pack_start(GTK_BOX(hbox),FilenameExtensionNoChange,FALSE,FALSE,2);
-    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(FilenameExtensionNoChange),FILENAME_EXTENSION_NO_CHANGE);
     gtk_widget_set_tooltip_text(FilenameExtensionNoChange,_("The extension will not be converted"));
 
+    g_settings_bind_with_mapping (MainSettings, "rename-extension-mode",
+                                  FilenameExtensionLowerCase, "active",
+                                  G_SETTINGS_BIND_DEFAULT,
+                                  et_settings_enum_radio_get,
+                                  et_settings_enum_radio_set,
+                                  FilenameExtensionLowerCase, NULL);
+    g_settings_bind_with_mapping (MainSettings, "rename-extension-mode",
+                                  FilenameExtensionUpperCase, "active",
+                                  G_SETTINGS_BIND_DEFAULT,
+                                  et_settings_enum_radio_get,
+                                  et_settings_enum_radio_set,
+                                  FilenameExtensionUpperCase, NULL);
+    g_settings_bind_with_mapping (MainSettings, "rename-extension-mode",
+                                  FilenameExtensionNoChange, "active",
+                                  G_SETTINGS_BIND_DEFAULT,
+                                  et_settings_enum_radio_get,
+                                  et_settings_enum_radio_set,
+                                  FilenameExtensionNoChange, NULL);
+
     /* Preserve modification time */
     PreserveModificationTime = gtk_check_button_new_with_label(_("Preserve modification time of the file"));
     gtk_box_pack_start(GTK_BOX(vbox),PreserveModificationTime,FALSE,FALSE,0);
diff --git a/src/preferences_dialog.h b/src/preferences_dialog.h
index eb729bb..d6c938b 100644
--- a/src/preferences_dialog.h
+++ b/src/preferences_dialog.h
@@ -71,10 +71,6 @@ GtkWidget *MessageBoxPositionCenterOnParent;
 GtkWidget *FilePlayerCombo;
 
 /* File Settings */
-GtkWidget *FilenameExtensionNoChange;
-GtkWidget *FilenameExtensionLowerCase;
-GtkWidget *FilenameExtensionUpperCase;
-
 GtkWidget *FilenameCharacterSetOther;
 GtkWidget *FilenameCharacterSetApproximate;
 GtkWidget *FilenameCharacterSetDiscard;
diff --git a/src/setting.c b/src/setting.c
index bca50f3..6964af4 100644
--- a/src/setting.c
+++ b/src/setting.c
@@ -121,9 +121,6 @@ static const tConfigVariable Config_Variables[] =
     {"pad_disc_number_digits", CV_TYPE_INT, &PAD_DISC_NUMBER_DIGITS },
     {"sorting_file_case_sensitive",          CV_TYPE_BOOL,    &SORTING_FILE_CASE_SENSITIVE              },
 
-    {"filename_extension_lower_case",                  CV_TYPE_BOOL,    &FILENAME_EXTENSION_LOWER_CASE       
     },
-    {"filename_extension_upper_case",                  CV_TYPE_BOOL,    &FILENAME_EXTENSION_UPPER_CASE       
     },
-    {"filename_extension_no_change",                   CV_TYPE_BOOL,    &FILENAME_EXTENSION_NO_CHANGE        
     },
     {"filename_character_set_other",                   CV_TYPE_BOOL,    &FILENAME_CHARACTER_SET_OTHER        
     },
     {"filename_character_set_approximate",             CV_TYPE_BOOL,    &FILENAME_CHARACTER_SET_APPROXIMATE  
     },
     {"filename_character_set_discard",                 CV_TYPE_BOOL,    &FILENAME_CHARACTER_SET_DISCARD      
     },
@@ -245,10 +242,6 @@ void Init_Config_Variables (void)
     /*
      * File Settings
      */
-    FILENAME_EXTENSION_LOWER_CASE               = 1;
-    FILENAME_EXTENSION_UPPER_CASE               = 0;
-    FILENAME_EXTENSION_NO_CHANGE                = 0;
-
     FILENAME_CHARACTER_SET_OTHER                = 1;
     FILENAME_CHARACTER_SET_APPROXIMATE          = 0;
     FILENAME_CHARACTER_SET_DISCARD              = 0;
@@ -412,10 +405,6 @@ Apply_Changes_Of_Preferences_Window (void)
         AUDIO_FILE_PLAYER                       = 
g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(FilePlayerCombo)))));
 
         /* File Settings */
-        FILENAME_EXTENSION_LOWER_CASE             = 
gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(FilenameExtensionLowerCase));
-        FILENAME_EXTENSION_UPPER_CASE             = 
gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(FilenameExtensionUpperCase));
-        FILENAME_EXTENSION_NO_CHANGE              = 
gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(FilenameExtensionNoChange));
-
         FILENAME_CHARACTER_SET_OTHER              = 
gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(FilenameCharacterSetOther));
         FILENAME_CHARACTER_SET_APPROXIMATE        = 
gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(FilenameCharacterSetApproximate));
         FILENAME_CHARACTER_SET_DISCARD            = 
gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(FilenameCharacterSetDiscard));
@@ -1360,3 +1349,64 @@ et_settings_enum_set (const GValue *value, const GVariantType *expected_type,
     return g_variant_new (g_variant_type_peek_string (expected_type),
                           enum_value->value_nick);
 }
+
+/*
+ * et_settings_enum_radio_get:
+ * @value: the property value to be set
+ * @variant: the variant to set the @value from
+ * @user_data: the widget on which the setting should be applied
+ *
+ * Wrapper function to convert an enum-type GSettings key state to the active
+ * radio button.
+ *
+ * Returns: %TRUE
+ */
+gboolean
+et_settings_enum_radio_get (GValue *value, GVariant *variant,
+                            gpointer user_data)
+{
+    const gchar *name;
+    const gchar *setting;
+
+    name = gtk_widget_get_name (GTK_WIDGET (user_data));
+    setting = g_variant_get_string (variant, NULL);
+
+    /* Only set the radio button which matches the setting to active. */
+    if (g_strcmp0 (name, setting) == 0)
+    {
+        g_value_set_boolean (value, TRUE);
+    }
+
+    return TRUE;
+}
+
+/*
+ * et_settings_enum_radio_set:
+ * @value: the property value to set the @variant from
+ * @expected_type: the expected type of the returned variant
+ * @user_data: the widget which the setting should be taken from
+ *
+ * Wrapper function to convert the active radiobutton to the value of an
+ * enum-type GSettings key.
+ *
+ * Returns: a new GVariant containing the mapped value, or %NULL upon failure
+ */
+GVariant *
+et_settings_enum_radio_set (const GValue *value,
+                            const GVariantType *expected_type,
+                            gpointer user_data)
+{
+    GVariant *variant = NULL;
+    const gchar *name;
+
+    /* Ignore buttons that are not active. */
+    if (!g_value_get_boolean (value))
+    {
+        return variant;
+    }
+
+    name = gtk_widget_get_name (GTK_WIDGET (user_data));
+    variant = g_variant_new_string (name);
+
+    return variant;
+}
diff --git a/src/setting.h b/src/setting.h
index 63bd90e..302ca79 100644
--- a/src/setting.h
+++ b/src/setting.h
@@ -43,6 +43,13 @@ struct _tConfigVariable
     void *pointer;              /* Pointer to our variable */
 };
 
+typedef enum
+{
+    ET_FILENAME_EXTENSION_LOWER_CASE,
+    ET_FILENAME_EXTENSION_UPPER_CASE,
+    ET_FILENAME_EXTENSION_NO_CHANGE
+} EtFilenameExtensionMode;
+
 /*
  * The mode for the scanner window.
  */
@@ -116,10 +123,6 @@ gint    SORTING_FILE_CASE_SENSITIVE;
 gchar  *AUDIO_FILE_PLAYER;
 
 /* File Settings */
-gint    FILENAME_EXTENSION_LOWER_CASE;
-gint    FILENAME_EXTENSION_UPPER_CASE;
-gint    FILENAME_EXTENSION_NO_CHANGE;
-
 gint    FILENAME_CHARACTER_SET_OTHER;
 gint    FILENAME_CHARACTER_SET_APPROXIMATE;
 gint    FILENAME_CHARACTER_SET_DISCARD;
@@ -290,5 +293,10 @@ gboolean et_settings_enum_get (GValue *value, GVariant *variant,
 GVariant *et_settings_enum_set (const GValue *value,
                                 const GVariantType *expected_type,
                                 gpointer user_data);
+gboolean et_settings_enum_radio_get (GValue *value, GVariant *variant,
+                                     gpointer user_data);
+GVariant *et_settings_enum_radio_set (const GValue *value,
+                                      const GVariantType *expected_type,
+                                      gpointer user_data);
 
 #endif /* ET_SETTINGS_H_ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]