[gimp/GimpFileDialog-refactoring: 2/3] app: GimpFileDialog state saving now implemented as a class method.



commit f3f78d2b2dbecc3f3efff5fbb5c5a3f7ff9b2bea
Author: Jehan <jehan girinstud io>
Date:   Tue Sep 1 13:49:59 2015 +0200

    app: GimpFileDialog state saving now implemented as a class method.
    
    By default, it only saves the current file filter, but it allows a child
    to implement its own state. It has been implemented by GimpSaveDialog to
    save the preferred compatibility mode as well.

 app/dialogs/file-open-dialog.c |   12 +--
 app/dialogs/file-save-dialog.c |   22 ++----
 app/widgets/gimpfiledialog.c   |  148 +++++++++++++++++++++++++++-------------
 app/widgets/gimpfiledialog.h   |   16 +++--
 app/widgets/gimpsavedialog.c   |  131 +++++++++++++++++++++++++++++++++---
 5 files changed, 243 insertions(+), 86 deletions(-)
---
diff --git a/app/dialogs/file-open-dialog.c b/app/dialogs/file-open-dialog.c
index 362025b..36e1ad6 100644
--- a/app/dialogs/file-open-dialog.c
+++ b/app/dialogs/file-open-dialog.c
@@ -69,7 +69,6 @@ GtkWidget *
 file_open_dialog_new (Gimp *gimp)
 {
   GtkWidget           *dialog;
-  GimpFileDialogState *state;
 
   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
 
@@ -77,10 +76,8 @@ file_open_dialog_new (Gimp *gimp)
 
   gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dialog), TRUE);
 
-  state = g_object_get_data (G_OBJECT (gimp), "gimp-file-open-dialog-state");
-
-  if (state)
-    gimp_file_dialog_set_state (GIMP_FILE_DIALOG (dialog), state);
+  gimp_file_dialog_load_state (GIMP_FILE_DIALOG (dialog),
+                               "gimp-file-open-dialog-state");
 
   g_signal_connect (dialog, "response",
                     G_CALLBACK (file_open_dialog_response),
@@ -102,9 +99,8 @@ file_open_dialog_response (GtkWidget *open_dialog,
   GSList         *list;
   gboolean        success = FALSE;
 
-  g_object_set_data_full (G_OBJECT (gimp), "gimp-file-open-dialog-state",
-                          gimp_file_dialog_get_state (dialog),
-                          (GDestroyNotify) gimp_file_dialog_state_destroy);
+  gimp_file_dialog_save_state (GIMP_FILE_DIALOG (dialog),
+                               "gimp-file-open-dialog-state");
 
   if (response_id != GTK_RESPONSE_OK)
     {
diff --git a/app/dialogs/file-save-dialog.c b/app/dialogs/file-save-dialog.c
index aa48a66..42b47ec 100644
--- a/app/dialogs/file-save-dialog.c
+++ b/app/dialogs/file-save-dialog.c
@@ -94,26 +94,24 @@ file_save_dialog_new (Gimp     *gimp,
                       gboolean  export)
 {
   GtkWidget           *dialog;
-  GimpFileDialogState *state;
+
+  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
 
   if (! export)
     {
       dialog = gimp_save_dialog_new (gimp);
 
-      state = g_object_get_data (G_OBJECT (gimp), "gimp-file-save-dialog-state");
+      gimp_file_dialog_load_state (GIMP_FILE_DIALOG (dialog),
+                                   "gimp-file-save-dialog-state");
     }
   else
     {
       dialog = gimp_export_dialog_new (gimp);
 
-      state = g_object_get_data (G_OBJECT (gimp), "gimp-file-export-dialog-state");
+      gimp_file_dialog_load_state (GIMP_FILE_DIALOG (dialog),
+                                   "gimp-file-export-dialog-state");
     }
 
-  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
-
-  if (state)
-    gimp_file_dialog_set_state (GIMP_FILE_DIALOG (dialog), state);
-
   g_signal_connect (dialog, "confirm-overwrite",
                     G_CALLBACK (file_save_dialog_confirm_overwrite),
                     gimp);
@@ -155,15 +153,11 @@ file_save_dialog_response (GtkWidget *save_dialog,
 
   if (GIMP_IS_SAVE_DIALOG (dialog))
     {
-      g_object_set_data_full (G_OBJECT (gimp), "gimp-file-save-dialog-state",
-                              gimp_file_dialog_get_state (dialog),
-                              (GDestroyNotify) gimp_file_dialog_state_destroy);
+      gimp_file_dialog_save_state (dialog, "gimp-file-save-dialog-state");
     }
   else /* GIMP_IS_EXPORT_DIALOG (dialog) */
     {
-      g_object_set_data_full (G_OBJECT (gimp), "gimp-file-export-dialog-state",
-                              gimp_file_dialog_get_state (dialog),
-                              (GDestroyNotify) gimp_file_dialog_state_destroy);
+      gimp_file_dialog_save_state (dialog, "gimp-file-export-dialog-state");
     }
 
   if (response_id != GTK_RESPONSE_OK)
diff --git a/app/widgets/gimpfiledialog.c b/app/widgets/gimpfiledialog.c
index fc61abd..cf2c11a 100644
--- a/app/widgets/gimpfiledialog.c
+++ b/app/widgets/gimpfiledialog.c
@@ -52,6 +52,7 @@
 /*  an arbitrary limit to keep the file dialog from becoming too wide  */
 #define MAX_EXTENSIONS  4
 
+typedef struct _GimpFileDialogState GimpFileDialogState;
 struct _GimpFileDialogState
 {
   gchar *filter_name;
@@ -72,7 +73,6 @@ enum
 
 static void     gimp_file_dialog_progress_iface_init    (GimpProgressInterface *iface);
 
-
 static void     gimp_file_dialog_set_property           (GObject             *object,
                                                          guint                property_id,
                                                          const GValue        *value,
@@ -128,6 +128,17 @@ static void     gimp_file_dialog_help_clicked           (GtkWidget           *wi
 static gchar  * gimp_file_dialog_pattern_from_extension (const gchar         *extension);
 
 
+static GimpFileDialogState
+              * gimp_file_dialog_get_state              (GimpFileDialog      *dialog);
+static void     gimp_file_dialog_set_state              (GimpFileDialog      *dialog,
+                                                         GimpFileDialogState *state);
+static void     gimp_file_dialog_state_destroy          (GimpFileDialogState *state);
+
+static void     gimp_file_dialog_real_save_state        (GimpFileDialog      *dialog,
+                                                         const gchar         *state_name);
+static void     gimp_file_dialog_real_load_state        (GimpFileDialog      *dialog,
+                                                         const gchar         *state_name);
+
 G_DEFINE_TYPE_WITH_CODE (GimpFileDialog, gimp_file_dialog,
                          GTK_TYPE_FILE_CHOOSER_DIALOG,
                          G_IMPLEMENT_INTERFACE (GIMP_TYPE_PROGRESS,
@@ -152,6 +163,9 @@ gimp_file_dialog_class_init (GimpFileDialogClass *klass)
 
   dialog_class->response     = gimp_file_dialog_response;
 
+  klass->save_state          = gimp_file_dialog_real_save_state;
+  klass->load_state          = gimp_file_dialog_real_load_state;
+
   g_object_class_install_property (object_class, PROP_GIMP,
                                    g_param_spec_object ("gimp", NULL, NULL,
                                                         GIMP_TYPE_GIMP,
@@ -551,64 +565,24 @@ gimp_file_dialog_set_file_proc (GimpFileDialog      *dialog,
                                   file_proc);
 }
 
-GimpFileDialogState *
-gimp_file_dialog_get_state (GimpFileDialog *dialog)
-{
-  GimpFileDialogState *state;
-  GtkFileFilter       *filter;
-
-  g_return_val_if_fail (GIMP_IS_FILE_DIALOG (dialog), NULL);
-
-  state = g_slice_new0 (GimpFileDialogState);
-
-  filter = gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog));
-
-  if (filter)
-    state->filter_name = g_strdup (gtk_file_filter_get_name (filter));
-
-  return state;
-}
-
 void
-gimp_file_dialog_set_state (GimpFileDialog      *dialog,
-                            GimpFileDialogState *state)
+gimp_file_dialog_save_state (GimpFileDialog *dialog,
+                             const gchar    *state_name)
 {
   g_return_if_fail (GIMP_IS_FILE_DIALOG (dialog));
-  g_return_if_fail (state != NULL);
 
-  if (state->filter_name)
-    {
-      GSList *filters;
-      GSList *list;
-
-      filters = gtk_file_chooser_list_filters (GTK_FILE_CHOOSER (dialog));
-
-      for (list = filters; list; list = list->next)
-        {
-          GtkFileFilter *filter = GTK_FILE_FILTER (list->data);
-          const gchar   *name   = gtk_file_filter_get_name (filter);
-
-          if (name && strcmp (state->filter_name, name) == 0)
-            {
-              gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), filter);
-              break;
-            }
-        }
-
-      g_slist_free (filters);
-    }
+  GIMP_FILE_DIALOG_GET_CLASS (dialog)->save_state (dialog, state_name);
 }
 
 void
-gimp_file_dialog_state_destroy (GimpFileDialogState *state)
+gimp_file_dialog_load_state (GimpFileDialog *dialog,
+                             const gchar    *state_name)
 {
-  g_return_if_fail (state != NULL);
+  g_return_if_fail (GIMP_IS_FILE_DIALOG (dialog));
 
-  g_free (state->filter_name);
-  g_slice_free (GimpFileDialogState, state);
+  GIMP_FILE_DIALOG_GET_CLASS (dialog)->load_state (dialog, state_name);
 }
 
-
 /*  private functions  */
 
 static void
@@ -1006,3 +980,81 @@ gimp_file_dialog_pattern_from_extension (const gchar *extension)
 
   return pattern;
 }
+
+static GimpFileDialogState *
+gimp_file_dialog_get_state (GimpFileDialog *dialog)
+{
+  GimpFileDialogState *state;
+  GtkFileFilter       *filter;
+
+  g_return_val_if_fail (GIMP_IS_FILE_DIALOG (dialog), NULL);
+
+  state = g_slice_new0 (GimpFileDialogState);
+
+  filter = gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog));
+
+  if (filter)
+    state->filter_name = g_strdup (gtk_file_filter_get_name (filter));
+
+  return state;
+}
+
+static void
+gimp_file_dialog_set_state (GimpFileDialog      *dialog,
+                            GimpFileDialogState *state)
+{
+  g_return_if_fail (GIMP_IS_FILE_DIALOG (dialog));
+  g_return_if_fail (state != NULL);
+
+  if (state->filter_name)
+    {
+      GSList *filters;
+      GSList *list;
+
+      filters = gtk_file_chooser_list_filters (GTK_FILE_CHOOSER (dialog));
+
+      for (list = filters; list; list = list->next)
+        {
+          GtkFileFilter *filter = GTK_FILE_FILTER (list->data);
+          const gchar   *name   = gtk_file_filter_get_name (filter);
+
+          if (name && strcmp (state->filter_name, name) == 0)
+            {
+              gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), filter);
+              break;
+            }
+        }
+
+      g_slist_free (filters);
+    }
+}
+
+static void
+gimp_file_dialog_state_destroy (GimpFileDialogState *state)
+{
+  g_return_if_fail (state != NULL);
+
+  g_free (state->filter_name);
+  g_slice_free (GimpFileDialogState, state);
+}
+
+static void
+gimp_file_dialog_real_save_state (GimpFileDialog *dialog,
+                                  const gchar    *state_name)
+{
+  g_object_set_data_full (G_OBJECT (dialog->gimp), state_name,
+                          gimp_file_dialog_get_state (dialog),
+                          (GDestroyNotify) gimp_file_dialog_state_destroy);
+}
+
+static void
+gimp_file_dialog_real_load_state (GimpFileDialog *dialog,
+                                  const gchar    *state_name)
+{
+  GimpFileDialogState *state;
+
+  state = g_object_get_data (G_OBJECT (dialog->gimp), state_name);
+
+  if (state)
+    gimp_file_dialog_set_state (GIMP_FILE_DIALOG (dialog), state);
+}
diff --git a/app/widgets/gimpfiledialog.h b/app/widgets/gimpfiledialog.h
index 7dcf420..e49d34d 100644
--- a/app/widgets/gimpfiledialog.h
+++ b/app/widgets/gimpfiledialog.h
@@ -65,10 +65,12 @@ struct _GimpFileDialog
 struct _GimpFileDialogClass
 {
   GtkFileChooserDialogClass  parent_class;
-};
-
 
-typedef struct _GimpFileDialogState GimpFileDialogState;
+  void (* save_state) (GimpFileDialog *dialog,
+                       const gchar    *state_name);
+  void (* load_state) (GimpFileDialog *dialog,
+                       const gchar    *state_name);
+};
 
 
 GType       gimp_file_dialog_get_type       (void) G_GNUC_CONST;
@@ -85,10 +87,10 @@ void        gimp_file_dialog_set_sensitive    (GimpFileDialog       *dialog,
 void        gimp_file_dialog_set_file_proc    (GimpFileDialog       *dialog,
                                                GimpPlugInProcedure  *file_proc);
 
-GimpFileDialogState * gimp_file_dialog_get_state     (GimpFileDialog      *dialog);
-void                  gimp_file_dialog_set_state     (GimpFileDialog      *dialog,
-                                                      GimpFileDialogState *state);
-void                  gimp_file_dialog_state_destroy (GimpFileDialogState *state);
+void        gimp_file_dialog_save_state       (GimpFileDialog       *dialog,
+                                               const gchar          *state_name);
+void        gimp_file_dialog_load_state       (GimpFileDialog       *dialog,
+                                               const gchar          *state_name);
 
 
 G_END_DECLS
diff --git a/app/widgets/gimpsavedialog.c b/app/widgets/gimpsavedialog.c
index e9156da..5759e48 100644
--- a/app/widgets/gimpsavedialog.c
+++ b/app/widgets/gimpsavedialog.c
@@ -47,12 +47,31 @@
 #include "gimp-intl.h"
 
 
-static void     gimp_save_dialog_constructed         (GObject         *object);
+typedef struct _GimpSaveDialogState GimpSaveDialogState;
+struct _GimpSaveDialogState
+{
+  gchar    *filter_name;
+  gboolean  compat;
+};
+
+
+static void     gimp_save_dialog_constructed         (GObject             *object);
 
-static GFile  * gimp_save_dialog_get_default_folder  (Gimp            *gimp);
-static void     gimp_save_dialog_add_compat_toggle   (GimpSaveDialog  *dialog);
-static void     gimp_save_dialog_compat_toggled      (GtkToggleButton *button,
-                                                      GimpSaveDialog  *dialog);
+static GFile  * gimp_save_dialog_get_default_folder  (Gimp                *gimp);
+static void     gimp_save_dialog_add_compat_toggle   (GimpSaveDialog      *dialog);
+static void     gimp_save_dialog_compat_toggled      (GtkToggleButton     *button,
+                                                      GimpSaveDialog      *dialog);
+
+static GimpSaveDialogState
+              * gimp_save_dialog_get_state           (GimpSaveDialog      *dialog);
+static void     gimp_save_dialog_set_state           (GimpSaveDialog      *dialog,
+                                                      GimpSaveDialogState *state);
+static void     gimp_save_dialog_state_destroy       (GimpSaveDialogState *state);
+
+static void     gimp_save_dialog_real_save_state     (GimpFileDialog      *dialog,
+                                                      const gchar         *state_name);
+static void     gimp_save_dialog_real_load_state     (GimpFileDialog      *dialog,
+                                                      const gchar         *state_name);
 
 G_DEFINE_TYPE (GimpSaveDialog, gimp_save_dialog,
                GIMP_TYPE_FILE_DIALOG)
@@ -62,9 +81,13 @@ G_DEFINE_TYPE (GimpSaveDialog, gimp_save_dialog,
 static void
 gimp_save_dialog_class_init (GimpSaveDialogClass *klass)
 {
-  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GObjectClass        *object_class = G_OBJECT_CLASS (klass);
+  GimpFileDialogClass *fd_class     = GIMP_FILE_DIALOG_CLASS (klass);
 
   object_class->constructed  = gimp_save_dialog_constructed;
+
+  fd_class->save_state       = gimp_save_dialog_real_save_state;
+  fd_class->load_state       = gimp_save_dialog_real_load_state;
 }
 
 static void
@@ -236,9 +259,13 @@ gimp_save_dialog_set_image (GimpSaveDialog *dialog,
 
   gtk_widget_show (dialog->compat_toggle);
 
-  if (gtk_widget_get_sensitive (dialog->compat_toggle))
-      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->compat_toggle),
-                                    gimp_image_get_xcf_compat_mode (image));
+  /* We set the compatibility mode by default either if the image was
+  * previously saved with the compatibility mode, or if it has never been
+  * saved and the last GimpSaveDialogState had compatibility mode ON. */
+  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->compat_toggle),
+                                gtk_widget_get_sensitive (dialog->compat_toggle) &&
+                                (gimp_image_get_xcf_compat_mode (image) ||
+                                 (! gimp_image_get_file (image) && dialog->compat)));
 
   if (ext_file)
     {
@@ -331,3 +358,89 @@ gimp_save_dialog_compat_toggled (GtkToggleButton *button,
 {
   dialog->compat = gtk_toggle_button_get_active (button);
 }
+
+static GimpSaveDialogState *
+gimp_save_dialog_get_state (GimpSaveDialog *dialog)
+{
+  GimpSaveDialogState *state;
+  GtkFileFilter       *filter;
+
+  g_return_val_if_fail (GIMP_IS_SAVE_DIALOG (dialog), NULL);
+
+  state = g_slice_new0 (GimpSaveDialogState);
+
+  filter = gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog));
+
+  if (filter)
+    state->filter_name = g_strdup (gtk_file_filter_get_name (filter));
+
+  state->compat = dialog->compat;
+
+  return state;
+}
+
+static void
+gimp_save_dialog_set_state (GimpSaveDialog      *dialog,
+                            GimpSaveDialogState *state)
+{
+  g_return_if_fail (GIMP_IS_SAVE_DIALOG (dialog));
+  g_return_if_fail (state != NULL);
+
+  if (state->filter_name)
+    {
+      GSList *filters;
+      GSList *list;
+
+      filters = gtk_file_chooser_list_filters (GTK_FILE_CHOOSER (dialog));
+
+      for (list = filters; list; list = list->next)
+        {
+          GtkFileFilter *filter = GTK_FILE_FILTER (list->data);
+          const gchar   *name   = gtk_file_filter_get_name (filter);
+
+          if (name && strcmp (state->filter_name, name) == 0)
+            {
+              gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), filter);
+              break;
+            }
+        }
+
+      g_slist_free (filters);
+    }
+
+  dialog->compat = state->compat;
+}
+
+static void
+gimp_save_dialog_state_destroy (GimpSaveDialogState *state)
+{
+  g_return_if_fail (state != NULL);
+
+  g_free (state->filter_name);
+  g_slice_free (GimpSaveDialogState, state);
+}
+
+static void
+gimp_save_dialog_real_save_state (GimpFileDialog *dialog,
+                                  const gchar    *state_name)
+{
+  g_return_if_fail (GIMP_IS_SAVE_DIALOG (dialog));
+
+  g_object_set_data_full (G_OBJECT (dialog->gimp), state_name,
+                          gimp_save_dialog_get_state (GIMP_SAVE_DIALOG (dialog)),
+                          (GDestroyNotify) gimp_save_dialog_state_destroy);
+}
+
+static void
+gimp_save_dialog_real_load_state (GimpFileDialog *dialog,
+                                  const gchar    *state_name)
+{
+  GimpSaveDialogState *state;
+
+  g_return_if_fail (GIMP_IS_SAVE_DIALOG (dialog));
+
+  state = g_object_get_data (G_OBJECT (dialog->gimp), state_name);
+
+  if (state)
+    gimp_save_dialog_set_state (GIMP_SAVE_DIALOG (dialog), state);
+}


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