diff options
author | Xan Lopez <xan@igalia.com> | 2012-07-31 19:02:54 +0800 |
---|---|---|
committer | Xan Lopez <xan@igalia.com> | 2012-07-31 19:05:46 +0800 |
commit | 3ffcaee1656e55de26030f9c9a0c14518cfce048 (patch) | |
tree | 51f49c58acb42e47c37568dabd0d793ff0437acb /src | |
parent | c4824507c2216c665dab58fce03afd67e9e9ec7d (diff) | |
download | gsoc2013-epiphany-3ffcaee1656e55de26030f9c9a0c14518cfce048.tar.gz gsoc2013-epiphany-3ffcaee1656e55de26030f9c9a0c14518cfce048.tar.zst gsoc2013-epiphany-3ffcaee1656e55de26030f9c9a0c14518cfce048.zip |
ephy-encodings: rewrite to drop usage of EphyNode
We have a new 'EphyEncoding' object holding the encoding data, and the
EphyEncodings object is now just a hash table holding a bunch of
these.
Change all the UI code to use the new APIs.
https://bugzilla.gnome.org/show_bug.cgi?id=680735
Diffstat (limited to 'src')
-rw-r--r-- | src/ephy-encoding-dialog.c | 171 | ||||
-rw-r--r-- | src/ephy-encoding-menu.c | 74 | ||||
-rw-r--r-- | src/prefs-dialog.c | 66 |
3 files changed, 194 insertions, 117 deletions
diff --git a/src/ephy-encoding-dialog.c b/src/ephy-encoding-dialog.c index 95a0b070c..58e03f5c3 100644 --- a/src/ephy-encoding-dialog.c +++ b/src/ephy-encoding-dialog.c @@ -2,6 +2,7 @@ /* * Copyright © 2000, 2001, 2002, 2003 Marco Pesenti Gritti * Copyright © 2003, 2004 Christian Persch + * Copyright © 2012 Igalia S.L. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -30,8 +31,6 @@ #include "ephy-encodings.h" #include "ephy-file-helpers.h" #include "ephy-gui.h" -#include "ephy-node-view.h" -#include "ephy-node.h" #include "ephy-shell.h" #include <glib/gi18n.h> @@ -50,21 +49,59 @@ struct _EphyEncodingDialogPrivate EphyWindow *window; EphyEmbed *embed; GtkWidget *enc_view; - EphyNodeFilter *filter; - EphyNode *selected_node; gboolean update_tag; + char *selected_encoding; }; -static void ephy_encoding_dialog_class_init (EphyEncodingDialogClass *klass); -static void ephy_encoding_dialog_init (EphyEncodingDialog *ge); - +enum { + COL_TITLE_ELIDED, + COL_ENCODING, + NUM_COLS +}; + G_DEFINE_TYPE (EphyEncodingDialog, ephy_encoding_dialog, EPHY_TYPE_EMBED_DIALOG) static void +select_encoding_row (GtkTreeView *view, EphyEncoding *encoding) +{ + GtkTreeIter iter; + GtkTreeModel *model; + gboolean valid_iter = FALSE; + const char *target_encoding; + + model = gtk_tree_view_get_model (view); + valid_iter = gtk_tree_model_get_iter_first (model, &iter); + + target_encoding = ephy_encoding_get_encoding (encoding); + + while (valid_iter) + { + char *encoding_string = NULL; + + gtk_tree_model_get (model, &iter, + COL_ENCODING, &encoding_string, -1); + + if (g_str_equal (encoding_string, + target_encoding)) + { + GtkTreeSelection *selection; + + selection = gtk_tree_view_get_selection (view); + gtk_tree_selection_select_iter (selection, &iter); + g_free (encoding_string); + + return; + } + + g_free (encoding_string); + valid_iter = gtk_tree_model_iter_next (model, &iter); + } +} + +static void sync_encoding_against_embed (EphyEncodingDialog *dialog) { EphyEmbed *embed; - EphyNode *node; GtkTreeSelection *selection; GtkTreeModel *model; GList *rows; @@ -72,6 +109,7 @@ sync_encoding_against_embed (EphyEncodingDialog *dialog) const char *encoding; gboolean is_automatic = FALSE; WebKitWebView *view; + EphyEncoding *node; dialog->priv->update_tag = TRUE; @@ -92,12 +130,11 @@ sync_encoding_against_embed (EphyEncodingDialog *dialog) } #endif - node = ephy_encodings_get_node (dialog->priv->encodings, encoding, TRUE); - g_assert (EPHY_IS_NODE (node)); + node = ephy_encodings_get_encoding (dialog->priv->encodings, encoding, TRUE); + g_assert (EPHY_IS_ENCODING (node)); - /* select the current encoding in the list view */ - ephy_node_view_select_node (EPHY_NODE_VIEW (dialog->priv->enc_view), - node); + /* Select the current encoding in the list view. */ + select_encoding_row (GTK_TREE_VIEW (dialog->priv->enc_view), node); /* scroll the view so the active encoding is visible */ selection = gtk_tree_view_get_selection @@ -210,12 +247,11 @@ activate_choice (EphyEncodingDialog *dialog) webkit_web_view_set_custom_encoding (view, NULL); #endif } - else if (dialog->priv->selected_node != NULL) + else if (dialog->priv->selected_encoding != NULL) { const char *code; - code = ephy_node_get_property_string (dialog->priv->selected_node, - EPHY_NODE_ENCODING_PROP_ENCODING); + code = dialog->priv->selected_encoding; #ifdef HAVE_WEBKIT2 webkit_web_view_set_custom_charset (view, code); @@ -242,15 +278,28 @@ ephy_encoding_dialog_response_cb (GtkWidget *widget, } static void -view_node_selected_cb (EphyNodeView *view, - EphyNode *node, - EphyEncodingDialog *dialog) +view_row_selected_cb (GtkTreeSelection *selection, + EphyEncodingDialog *dialog) { GtkWidget *button; + GtkTreeModel *model; + GtkTreeIter iter; + EphyEncodingDialogPrivate *priv = dialog->priv; - dialog->priv->selected_node = node; + if (gtk_tree_selection_get_selected (selection, &model, &iter)) + { + char *encoding; + + gtk_tree_model_get (model, &iter, + COL_ENCODING, &encoding, + -1); + + g_free (priv->selected_encoding); + priv->selected_encoding = encoding; + } - if (dialog->priv->update_tag) return; + if (dialog->priv->update_tag) + return; button = ephy_dialog_get_control (EPHY_DIALOG (dialog), "manual_button"); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE); @@ -259,15 +308,29 @@ view_node_selected_cb (EphyNodeView *view, } static void -view_node_activated_cb (GtkWidget *view, - EphyNode *node, - EphyEncodingDialog *dialog) +view_row_activated_cb (GtkTreeView *treeview, + GtkTreePath *path, + GtkTreeViewColumn *column, + EphyEncodingDialog *dialog) { GtkWidget *button; + GtkTreeIter iter; + char *encoding; + GtkTreeModel *model; + EphyEncodingDialogPrivate *priv = dialog->priv; - dialog->priv->selected_node = node; + model = gtk_tree_view_get_model (treeview); - if (dialog->priv->update_tag) return; + gtk_tree_model_get_iter (model, &iter, path); + gtk_tree_model_get (model, &iter, + COL_ENCODING, &encoding, + -1); + + g_free (priv->selected_encoding); + priv->selected_encoding = encoding; + + if (dialog->priv->update_tag) + return; button = ephy_dialog_get_control (EPHY_DIALOG (dialog), "manual_button"); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE); @@ -292,7 +355,10 @@ ephy_encoding_dialog_init (EphyEncodingDialog *dialog) { GtkWidget *treeview, *scroller, *button, *window, *child; GtkTreeSelection *selection; - EphyNode *node; + GList *encodings, *p; + GtkListStore *store; + GtkTreeIter iter; + GtkCellRenderer *renderer; dialog->priv = EPHY_ENCODING_DIALOG_GET_PRIVATE (dialog); @@ -310,32 +376,47 @@ ephy_encoding_dialog_init (EphyEncodingDialog *dialog) g_signal_connect (window, "response", G_CALLBACK (ephy_encoding_dialog_response_cb), dialog); - dialog->priv->filter = ephy_node_filter_new (); + encodings = ephy_encodings_get_all (dialog->priv->encodings); + store = gtk_list_store_new (NUM_COLS, G_TYPE_STRING, G_TYPE_STRING); + for (p = encodings; p; p = p->next) + { + EphyEncoding *encoding = EPHY_ENCODING (p->data); + + gtk_list_store_append (store, &iter); + gtk_list_store_set (store, &iter, + COL_TITLE_ELIDED, + ephy_encoding_get_title_elided (encoding), + -1); + gtk_list_store_set (store, &iter, + COL_ENCODING, + ephy_encoding_get_encoding (encoding), + -1); + } + g_list_free (encodings); - node = ephy_encodings_get_all (dialog->priv->encodings); - treeview = ephy_node_view_new (node, dialog->priv->filter); + gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store), COL_TITLE_ELIDED, + GTK_SORT_ASCENDING); - ephy_node_view_add_column (EPHY_NODE_VIEW (treeview), _("Encodings"), - G_TYPE_STRING, - EPHY_NODE_ENCODING_PROP_TITLE_ELIDED, - EPHY_NODE_VIEW_SEARCHABLE, - NULL, NULL); + treeview = gtk_tree_view_new (); + renderer = gtk_cell_renderer_text_new (); - ephy_node_view_set_sort (EPHY_NODE_VIEW (treeview), G_TYPE_STRING, - EPHY_NODE_ENCODING_PROP_TITLE_ELIDED, - GTK_SORT_ASCENDING); + gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview), + -1, + _("Encodings"), + renderer, + "text", COL_TITLE_ELIDED, + NULL); + gtk_tree_view_set_model (GTK_TREE_VIEW (treeview), GTK_TREE_MODEL (store)); selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)); gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE); gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(treeview), FALSE); - g_signal_connect (G_OBJECT (treeview), - "node_selected", - G_CALLBACK (view_node_selected_cb), + g_signal_connect (selection, "changed", + G_CALLBACK (view_row_selected_cb), dialog); - g_signal_connect (G_OBJECT (treeview), - "node_activated", - G_CALLBACK (view_node_activated_cb), + g_signal_connect (treeview, "row-activated", + G_CALLBACK (view_row_activated_cb), dialog); gtk_widget_show (treeview); @@ -382,7 +463,7 @@ ephy_encoding_dialog_finalize (GObject *object) dialog); } - g_object_unref (dialog->priv->filter); + g_free (dialog->priv->selected_encoding); G_OBJECT_CLASS (ephy_encoding_dialog_parent_class)->finalize (object); } diff --git a/src/ephy-encoding-menu.c b/src/ephy-encoding-menu.c index 86fe2c293..e276f4a2c 100644 --- a/src/ephy-encoding-menu.c +++ b/src/ephy-encoding-menu.c @@ -81,26 +81,23 @@ ephy_encoding_menu_init (EphyEncodingMenu *menu) static int sort_encodings (gconstpointer a, gconstpointer b) { - EphyNode *node1 = (EphyNode *) a; - EphyNode *node2 = (EphyNode *) b; + EphyEncoding *enc1 = (EphyEncoding*)a; + EphyEncoding *enc2 = (EphyEncoding*)b; const char *key1, *key2; - key1 = ephy_node_get_property_string - (node1, EPHY_NODE_ENCODING_PROP_COLLATION_KEY); - key2 = ephy_node_get_property_string - (node2, EPHY_NODE_ENCODING_PROP_COLLATION_KEY); + key1 = ephy_encoding_get_collation_key (enc1); + key2 = ephy_encoding_get_collation_key (enc2); return strcmp (key1, key2); } static void -add_menu_item (EphyNode *node, EphyEncodingMenu *menu) +add_menu_item (EphyEncoding *encoding, EphyEncodingMenu *menu) { const char *code; char action[128], name[128]; - code = ephy_node_get_property_string - (node, EPHY_NODE_ENCODING_PROP_ENCODING); + code = ephy_encoding_get_encoding (encoding); g_snprintf (action, sizeof (action), "Encoding%s", code); g_snprintf (name, sizeof (name), "%sItem", action); @@ -119,7 +116,7 @@ update_encoding_menu_cb (GtkAction *dummy, EphyEncodingMenu *menu) GtkAction *action; char name[128]; const char *encoding; - EphyNode *enc_node; + EphyEncoding *enc_node; GList *recent, *related = NULL, *l; EphyLanguageGroup groups; gboolean is_automatic = FALSE; @@ -151,8 +148,8 @@ update_encoding_menu_cb (GtkAction *dummy, EphyEncodingMenu *menu) } #endif - enc_node = ephy_encodings_get_node (p->encodings, encoding, TRUE); - g_assert (EPHY_IS_NODE (enc_node)); + enc_node = ephy_encodings_get_encoding (p->encodings, encoding, TRUE); + g_assert (EPHY_IS_ENCODING (enc_node)); action = gtk_action_group_get_action (p->action_group, "ViewEncodingAutomatic"); @@ -165,11 +162,10 @@ update_encoding_menu_cb (GtkAction *dummy, EphyEncodingMenu *menu) gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE); /* get encodings related to the current encoding */ - groups = ephy_node_get_property_int - (enc_node, EPHY_NODE_ENCODING_PROP_LANGUAGE_GROUPS); + groups = ephy_encoding_get_language_groups (enc_node); related = ephy_encodings_get_encodings (p->encodings, groups); - related = g_list_sort (related, (GCompareFunc) sort_encodings); + related = g_list_sort (related, (GCompareFunc)sort_encodings); /* add the current encoding to the list of * things to display, making sure we don't add it more than once @@ -186,7 +182,7 @@ update_encoding_menu_cb (GtkAction *dummy, EphyEncodingMenu *menu) recent = g_list_remove (recent, l->data); } - recent = g_list_sort (recent, (GCompareFunc) sort_encodings); + recent = g_list_sort (recent, (GCompareFunc)sort_encodings); build_menu: /* clear the menu */ @@ -210,14 +206,14 @@ build_menu: "Sep1Item", "Sep1", GTK_UI_MANAGER_SEPARATOR, FALSE); - g_list_foreach (recent, (GFunc) add_menu_item, menu); + g_list_foreach (recent, (GFunc)add_menu_item, menu); gtk_ui_manager_add_ui (p->manager, p->merge_id, ENCODING_PLACEHOLDER_PATH, "Sep2Item", "Sep2", GTK_UI_MANAGER_SEPARATOR, FALSE); - g_list_foreach (related, (GFunc) add_menu_item, menu); + g_list_foreach (related, (GFunc)add_menu_item, menu); gtk_ui_manager_add_ui (p->manager, p->merge_id, ENCODING_PLACEHOLDER_PATH, @@ -268,20 +264,18 @@ encoding_activate_cb (GtkAction *action, EphyEncodingMenu *menu) } static void -add_action (EphyNode *encodings, EphyNode *node, EphyEncodingMenu *menu) +add_action (EphyEncodings *encodings, EphyEncoding *encoding, EphyEncodingMenu *menu) { GtkAction *action; - char name[128]; - const char *encoding, *title; + char name[128] = ""; + const char *encoding_str, *title; - encoding = ephy_node_get_property_string - (node, EPHY_NODE_ENCODING_PROP_ENCODING); - title = ephy_node_get_property_string - (node, EPHY_NODE_ENCODING_PROP_TITLE); + encoding_str = ephy_encoding_get_encoding (encoding); + title = ephy_encoding_get_title (encoding); - LOG ("add_action for encoding '%s'", encoding); + LOG ("add_action for encoding '%s'", encoding_str); - g_snprintf (name, sizeof (name), "Encoding%s", encoding); + g_snprintf (name, sizeof (name), "Encoding%s", encoding_str); action = g_object_new (GTK_TYPE_RADIO_ACTION, "name", name, @@ -361,9 +355,7 @@ ephy_encoding_menu_set_window (EphyEncodingMenu *menu, EphyWindow *window) { GtkActionGroup *action_group; GtkAction *action; - EphyNode *encodings; - GPtrArray *children; - int i; + GList *encodings, *p; g_return_if_fail (EPHY_IS_WINDOW (window)); @@ -381,23 +373,21 @@ ephy_encoding_menu_set_window (EphyEncodingMenu *menu, EphyWindow *window) /* add actions for the existing encodings */ encodings = ephy_encodings_get_all (menu->priv->encodings); - children = ephy_node_get_children (encodings); - for (i = 0; i < children->len; i++) + for (p = encodings; p; p = p->next) { - EphyNode *encoding; + EphyEncoding *encoding; - encoding = (EphyNode *) g_ptr_array_index (children, i); - add_action (encodings, encoding, menu); + encoding = (EphyEncoding *)p->data; + add_action (menu->priv->encodings, encoding, menu); } + g_list_free (encodings); - /* when we encounter an unknown encoding, it is added to the database, - * so we need to listen to child_added on the encodings node to - * add an action for it + /* When we encounter an unknown encoding, it is added to the + * database, so we need to listen to child_added on the + * encodings node to add an action for it. */ - ephy_node_signal_connect_object (encodings, - EPHY_NODE_CHILD_ADDED, - (EphyNodeCallback) add_action, - G_OBJECT (menu)); + g_signal_connect (menu->priv->encodings, "encoding-added", + G_CALLBACK (add_action), menu); gtk_ui_manager_insert_action_group (menu->priv->manager, action_group, 0); diff --git a/src/prefs-dialog.c b/src/prefs-dialog.c index c1e11a918..f95620031 100644 --- a/src/prefs-dialog.c +++ b/src/prefs-dialog.c @@ -39,8 +39,6 @@ #include "ephy-session.h" #include "ephy-settings.h" #include "ephy-shell.h" -#include "ephy-tree-model-node.h" -#include "ephy-tree-model-sort.h" #include "pdm-dialog.h" #include <glib/gi18n.h> @@ -75,6 +73,12 @@ struct PrefsDialogPrivate GHashTable *iso_3166_table; }; +enum { + COL_TITLE_ELIDED, + COL_ENCODING, + NUM_COLS +}; + G_DEFINE_TYPE (PrefsDialog, prefs_dialog, EPHY_TYPE_DIALOG) static void @@ -214,19 +218,17 @@ combo_set_mapping (const GValue *value, static void create_node_combo (EphyDialog *dialog, EphyEncodings *encodings, - EphyNode *node, const char *default_value) { - EphyTreeModelNode *nodemodel; - GtkTreeModel *sortmodel; GtkComboBox *combo; GtkCellRenderer *renderer; + GtkListStore *store; + GList *all_encodings, *p; char *code; - int title_col; code = g_settings_get_string (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_DEFAULT_ENCODING); - if (code == NULL || ephy_encodings_get_node (encodings, code, FALSE) == NULL) + if (code == NULL || ephy_encodings_get_encoding (encodings, code, FALSE) == NULL) { /* safe default */ g_settings_set_string (EPHY_SETTINGS_WEB, @@ -238,26 +240,34 @@ create_node_combo (EphyDialog *dialog, combo = GTK_COMBO_BOX (ephy_dialog_get_control (dialog, "default_encoding_combo")); - nodemodel = ephy_tree_model_node_new (node); - - title_col = ephy_tree_model_node_add_prop_column - (nodemodel, G_TYPE_STRING, EPHY_NODE_ENCODING_PROP_TITLE_ELIDED); - - ephy_tree_model_node_add_prop_column - (nodemodel, G_TYPE_STRING, EPHY_NODE_ENCODING_PROP_ENCODING); - - sortmodel = ephy_tree_model_sort_new (GTK_TREE_MODEL (nodemodel)); - - gtk_tree_sortable_set_sort_column_id - (GTK_TREE_SORTABLE (sortmodel), title_col, GTK_SORT_ASCENDING); + store = gtk_list_store_new (NUM_COLS, G_TYPE_STRING, G_TYPE_STRING); + all_encodings = ephy_encodings_get_all (encodings); + for (p = all_encodings; p; p = p->next) + { + GtkTreeIter iter; + EphyEncoding *encoding = EPHY_ENCODING (p->data); + + gtk_list_store_append (store, &iter); + gtk_list_store_set (store, &iter, + COL_TITLE_ELIDED, + ephy_encoding_get_title_elided (encoding), + -1); + gtk_list_store_set (store, &iter, + COL_ENCODING, + ephy_encoding_get_encoding (encoding), + -1); + } + g_list_free (all_encodings); - gtk_combo_box_set_model (combo, GTK_TREE_MODEL (sortmodel)); + gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store), COL_TITLE_ELIDED, + GTK_SORT_ASCENDING); + gtk_combo_box_set_model (combo, GTK_TREE_MODEL (store)); - renderer = gtk_cell_renderer_text_new (); - gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE); - gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer, - "text", title_col, - NULL); + renderer = gtk_cell_renderer_text_new (); + gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE); + gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer, + "text", COL_TITLE_ELIDED, + NULL); g_settings_bind_with_mapping (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_DEFAULT_ENCODING, @@ -267,9 +277,6 @@ create_node_combo (EphyDialog *dialog, combo_set_mapping, combo, NULL); - - g_object_unref (nodemodel); - g_object_unref (sortmodel); } static void @@ -1082,8 +1089,7 @@ prefs_dialog_init (PrefsDialog *pd) encodings = EPHY_ENCODINGS (ephy_embed_shell_get_encodings (EPHY_EMBED_SHELL (ephy_shell))); - create_node_combo (dialog, encodings, - ephy_encodings_get_all (encodings), "ISO-8859-1"); + create_node_combo (dialog, encodings, "ISO-8859-1"); create_language_section (dialog); |