[gedit/wip/merge-encoding-settings: 510/510] EncodingsDialog: remove candidates_list attribute (wip)



commit 99a966a5858f97243aa1a2a86f0de3daad97cacf
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Mar 15 18:23:16 2015 +0100

    EncodingsDialog: remove candidates_list attribute (wip)
    
    It contains duplicated data that is already available in the
    liststore_chosen.

 gedit/gedit-encodings-dialog.c |  161 +++++++++++++++++++++-------------------
 1 files changed, 84 insertions(+), 77 deletions(-)
---
diff --git a/gedit/gedit-encodings-dialog.c b/gedit/gedit-encodings-dialog.c
index 8a25687..e6ecda9 100644
--- a/gedit/gedit-encodings-dialog.c
+++ b/gedit/gedit-encodings-dialog.c
@@ -58,30 +58,54 @@ enum
 G_DEFINE_TYPE_WITH_PRIVATE (GeditEncodingsDialog, gedit_encodings_dialog, GTK_TYPE_DIALOG)
 
 static void
-gedit_encodings_dialog_finalize (GObject *object)
+gedit_encodings_dialog_dispose (GObject *object)
 {
        GeditEncodingsDialogPrivate *priv = GEDIT_ENCODINGS_DIALOG (object)->priv;
 
-       g_slist_free (priv->candidates_list);
+       g_clear_object (&priv->enc_settings);
 
-       G_OBJECT_CLASS (gedit_encodings_dialog_parent_class)->finalize (object);
+       G_OBJECT_CLASS (gedit_encodings_dialog_parent_class)->dispose (object);
 }
 
-static void
-gedit_encodings_dialog_dispose (GObject *object)
+static GSList *
+get_chosen_encodings_list (GeditEncodingsDialog *dialog)
 {
-       GeditEncodingsDialogPrivate *priv = GEDIT_ENCODINGS_DIALOG (object)->priv;
+       GtkTreeModel *model = GTK_TREE_MODEL (dialog->priv->liststore_chosen);
+       GtkTreeIter iter;
+       gboolean iter_set;
+       GSList *ret = NULL;
 
-       g_clear_object (&priv->enc_settings);
+       iter_set = gtk_tree_model_get_iter_first (model, &iter);
 
-       G_OBJECT_CLASS (gedit_encodings_dialog_parent_class)->dispose (object);
+       while (iter_set)
+       {
+               gchar *charset = NULL;
+               const GtkSourceEncoding *encoding;
+
+               gtk_tree_model_get (model, &iter,
+                                   COLUMN_CHARSET, &charset,
+                                   -1);
+
+               /* FIXME get_from_charset() has O(n) complexity, so calling it
+                * in a loop is O(n^2)... Add a COLUMN_ENCODING to the
+                * TreeModel.
+                */
+               encoding = gtk_source_encoding_get_from_charset (charset);
+               g_free (charset);
+
+               ret = g_slist_prepend (ret, (gpointer)encoding);
+
+               iter_set = gtk_tree_model_iter_next (model, &iter);
+       }
+
+       return ret;
 }
 
 static void
-gedit_encodings_dialog_response (GtkDialog *dialog,
+gedit_encodings_dialog_response (GtkDialog *gtk_dialog,
                                  gint       response_id)
 {
-       GeditEncodingsDialogPrivate *priv = GEDIT_ENCODINGS_DIALOG (dialog)->priv;
+       GeditEncodingsDialog *dialog = GEDIT_ENCODINGS_DIALOG (gtk_dialog);
 
        switch (response_id)
        {
@@ -94,13 +118,17 @@ gedit_encodings_dialog_response (GtkDialog *dialog,
 
                case GTK_RESPONSE_OK:
                {
+                       GSList *enc_list;
                        gchar **enc_strv;
 
-                       enc_strv = _gedit_utils_encoding_list_to_strv (priv->candidates_list);
-                       g_settings_set_strv (priv->enc_settings,
+                       enc_list = get_chosen_encodings_list (dialog);
+                       enc_strv = _gedit_utils_encoding_list_to_strv (enc_list);
+
+                       g_settings_set_strv (dialog->priv->enc_settings,
                                             GEDIT_SETTINGS_CANDIDATE_ENCODINGS,
                                             (const gchar * const *)enc_strv);
 
+                       g_slist_free (enc_list);
                        g_strfreev (enc_strv);
                        break;
                }
@@ -114,7 +142,6 @@ gedit_encodings_dialog_class_init (GeditEncodingsDialogClass *klass)
        GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
        GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (klass);
 
-       object_class->finalize = gedit_encodings_dialog_finalize;
        object_class->dispose = gedit_encodings_dialog_dispose;
 
        dialog_class->response = gedit_encodings_dialog_response;
@@ -154,42 +181,6 @@ update_remove_button_sensitivity (GeditEncodingsDialog *dialog)
 }
 
 static void
-get_selected_encodings_func (GtkTreeModel *model,
-                            GtkTreePath  *path,
-                            GtkTreeIter  *iter,
-                            gpointer      data)
-{
-       GSList **list = data;
-       gchar *charset = NULL;
-       const GtkSourceEncoding *enc;
-
-       gtk_tree_model_get (model, iter,
-                           COLUMN_CHARSET, &charset,
-                           -1);
-
-       enc = gtk_source_encoding_get_from_charset (charset);
-       g_free (charset);
-
-       *list = g_slist_prepend (*list, (gpointer)enc);
-}
-
-/* Returns a list of GtkSourceEncoding's. */
-static GSList *
-get_selected_encodings (GtkTreeView *treeview)
-{
-       GtkTreeSelection *selection;
-       GSList *encodings = NULL;
-
-       selection = gtk_tree_view_get_selection (treeview);
-
-       gtk_tree_selection_selected_foreach (selection,
-                                            get_selected_encodings_func,
-                                            &encodings);
-
-       return encodings;
-}
-
-static void
 update_liststore_chosen (GeditEncodingsDialog *dialog)
 {
        GSList *l;
@@ -213,50 +204,66 @@ update_liststore_chosen (GeditEncodingsDialog *dialog)
 }
 
 static void
-add_button_clicked_cb (GtkWidget            *button,
-                      GeditEncodingsDialog *dialog)
+transfer_encodings (GtkTreeSelection *selection,
+                   GtkListStore     *orig,
+                   GtkListStore     *dest)
 {
-       GSList *encodings;
-       GSList *l;
+       GtkTreeModel *model;
+       GList *paths;
+       GList *refs = NULL;
+       GList *l;
+
+       paths = gtk_tree_selection_get_selected_rows (selection, &model);
 
-       encodings = get_selected_encodings (dialog->priv->treeview_available);
+       g_return_if_fail (GTK_TREE_MODEL (orig) == model);
 
-       for (l = encodings; l != NULL; l = l->next)
+       for (l = paths; l != NULL; l = l->next)
        {
-               gpointer cur_encoding = l->data;
+               GtkTreePath *path = l->data;
+               refs = g_list_prepend (refs, gtk_tree_row_reference_new (model, path));
+       }
+
+       for (l = refs; l != NULL; l = l->next)
+       {
+               GtkTreeRowReference *ref = l->data;
+               GtkTreePath *path;
+               GtkTreeIter iter;
 
-               if (g_slist_find (dialog->priv->candidates_list, cur_encoding) == NULL)
+               path = gtk_tree_row_reference_get_path (ref);
+
+               if (!gtk_tree_model_get_iter (model, &iter, path))
                {
-                       dialog->priv->candidates_list = g_slist_prepend (dialog->priv->candidates_list,
-                                                                        cur_encoding);
+                       g_warning ("Remove encoding: invalid path");
+                       continue;
                }
+
+               /* TODO transfer encoding to 'dest'. */
+
+               gtk_list_store_remove (orig, &iter);
+
+               gtk_tree_path_free (path);
        }
 
-       g_slist_free (encodings);
+       g_list_free_full (paths, (GDestroyNotify) gtk_tree_path_free);
+       g_list_free_full (refs, (GDestroyNotify) gtk_tree_row_reference_free);
+}
 
-       update_liststore_chosen (dialog);
+static void
+add_button_clicked_cb (GtkWidget            *button,
+                      GeditEncodingsDialog *dialog)
+{
+       transfer_encodings (gtk_tree_view_get_selection (dialog->priv->treeview_available),
+                           dialog->priv->liststore_available,
+                           dialog->priv->liststore_chosen);
 }
 
 static void
 remove_button_clicked_cb (GtkWidget            *button,
                          GeditEncodingsDialog *dialog)
 {
-       GSList *encodings;
-       GSList *l;
-
-       encodings = get_selected_encodings (dialog->priv->treeview_chosen);
-
-       for (l = encodings; l != NULL; l = l->next)
-       {
-               gpointer cur_encoding = l->data;
-
-               dialog->priv->candidates_list = g_slist_remove (dialog->priv->candidates_list,
-                                                               cur_encoding);
-       }
-
-       g_slist_free (encodings);
-
-       update_liststore_chosen (dialog);
+       transfer_encodings (gtk_tree_view_get_selection (dialog->priv->treeview_chosen),
+                           dialog->priv->liststore_chosen,
+                           dialog->priv->liststore_available);
 }
 
 static void


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