[yelp] [yelp-view] Add validity-checking function to link actions



commit e37a3c8f4840d204f1f58d6b262c992b67ac4924
Author: Shaun McCance <shaunm gnome org>
Date:   Wed Aug 11 09:32:44 2010 -0400

    [yelp-view] Add validity-checking function to link actions

 libyelp/yelp-view.c |   45 ++++++++++++++++++++++++++++++++++++++-------
 libyelp/yelp-view.h |   29 ++++++++++++++++-------------
 src/yelp-window.c   |   17 ++++++++++++++++-
 3 files changed, 70 insertions(+), 21 deletions(-)
---
diff --git a/libyelp/yelp-view.c b/libyelp/yelp-view.c
index 51dfe77..632afb0 100644
--- a/libyelp/yelp-view.c
+++ b/libyelp/yelp-view.c
@@ -167,6 +167,20 @@ G_DEFINE_TYPE (YelpView, yelp_view, WEBKIT_TYPE_WEB_VIEW);
 
 static WebKitWebSettings *websettings;
 
+typedef struct _YelpActionEntry YelpActionEntry;
+struct _YelpActionEntry {
+    GtkAction               *action;
+    YelpViewActionValidFunc  func;
+    gpointer                 data;
+};
+action_entry_free (YelpActionEntry *entry)
+{
+    if (entry == NULL)
+        return;
+    g_object_unref (entry->action);
+    g_free (entry);
+}
+
 typedef struct _YelpBackEntry YelpBackEntry;
 struct _YelpBackEntry {
     YelpUri *uri;
@@ -296,7 +310,7 @@ yelp_view_dispose (GObject *object)
     }
 
     while (priv->link_actions) {
-        g_object_unref (priv->link_actions->data);
+        action_entry_free (priv->link_actions->data);
         priv->link_actions = g_slist_delete_link (priv->link_actions, priv->link_actions);
     }
 
@@ -626,12 +640,20 @@ yelp_view_get_action_group (YelpView *view)
 /******************************************************************************/
 
 void
-yelp_view_add_link_action (YelpView *view, GtkAction *action)
+yelp_view_add_link_action (YelpView                *view,
+                           GtkAction               *action,
+                           YelpViewActionValidFunc  func,
+                           gpointer                 data)
 {
+    YelpActionEntry *entry;
     YelpViewPrivate *priv = GET_PRIV (view);
 
-    priv->link_actions = g_slist_append (priv->link_actions,
-                                         g_object_ref (action));
+    entry = g_new0 (YelpActionEntry, 1);
+    entry->action = g_object_ref (action);
+    entry->func = func;
+    entry->data = data;
+
+    priv->link_actions = g_slist_append (priv->link_actions, entry);
 }
 
 YelpUri *
@@ -1052,9 +1074,18 @@ view_populate_popup (YelpView *view,
             gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
 
             for (cur = priv->link_actions; cur != NULL; cur = cur->next) {
-                GtkAction *action = (GtkAction *) cur->data;
-                item = gtk_action_create_menu_item (action);
-                gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
+                gboolean add;
+                YelpActionEntry *entry = (YelpActionEntry *) cur->data;
+                if (entry->func == NULL)
+                    add = TRUE;
+                else
+                    add = (* entry->func) (view, entry->action,
+                                           priv->popup_link_uri,
+                                           entry->data);
+                if (add) {
+                    item = gtk_action_create_menu_item (entry->action);
+                    gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
+                }
             }
         }
     }
diff --git a/libyelp/yelp-view.h b/libyelp/yelp-view.h
index 572b085..6227574 100644
--- a/libyelp/yelp-view.h
+++ b/libyelp/yelp-view.h
@@ -40,6 +40,7 @@ G_BEGIN_DECLS
 
 typedef struct _YelpView       YelpView;
 typedef struct _YelpViewClass  YelpViewClass;
+typedef gboolean (* YelpViewActionValidFunc) (YelpView *view, GtkAction *action, gchar *uri, gpointer data);
 
 struct _YelpView
 {
@@ -62,20 +63,22 @@ GType              yelp_view_get_type             (void);
 GType              yelp_view_state_get_type       (void);
 
 GtkWidget *        yelp_view_new                  (void);
-void               yelp_view_load                 (YelpView        *view,
-                                                   const gchar     *uri);
-void               yelp_view_load_uri             (YelpView        *view,
-                                                   YelpUri         *uri);
-void               yelp_view_load_document        (YelpView        *view,
-                                                   YelpUri         *uri,
-                                                   YelpDocument    *document);
-YelpDocument *     yelp_view_get_document         (YelpView        *view);
-GtkActionGroup *   yelp_view_get_action_group     (YelpView        *view);
+void               yelp_view_load                 (YelpView                *view,
+                                                   const gchar             *uri);
+void               yelp_view_load_uri             (YelpView                *view,
+                                                   YelpUri                 *uri);
+void               yelp_view_load_document        (YelpView                *view,
+                                                   YelpUri                 *uri,
+                                                   YelpDocument            *document);
+YelpDocument *     yelp_view_get_document         (YelpView                *view);
+GtkActionGroup *   yelp_view_get_action_group     (YelpView                *view);
 
-void               yelp_view_add_link_action      (YelpView        *view,
-                                                   GtkAction       *action);
-YelpUri *          yelp_view_get_active_link_uri  (YelpView        *view);
-gchar *            yelp_view_get_active_link_text (YelpView        *view);
+void               yelp_view_add_link_action      (YelpView                *view,
+                                                   GtkAction               *action,
+                                                   YelpViewActionValidFunc  func,
+                                                   gpointer                 data);
+YelpUri *          yelp_view_get_active_link_uri  (YelpView                *view);
+gchar *            yelp_view_get_active_link_text (YelpView                *view);
 
 G_END_DECLS
 
diff --git a/src/yelp-window.c b/src/yelp-window.c
index 042277a..1cc0df7 100644
--- a/src/yelp-window.c
+++ b/src/yelp-window.c
@@ -128,6 +128,10 @@ static void          view_uri_selected            (YelpView           *view,
 static void          view_root_title              (YelpView           *view,
                                                    GParamSpec         *pspec,
                                                    YelpWindow         *window);
+static gboolean      view_is_xref_uri             (YelpView           *view,
+                                                   GtkAction          *action,
+                                                   const gchar        *uri,
+                                                   YelpWindow         *window);
 
 static void          hidden_entry_activate        (GtkEntry           *entry,
                                                    YelpWindow         *window);
@@ -462,7 +466,9 @@ window_construct (YelpWindow *window)
 
     action = gtk_action_new ("ReadLinkLater", "Read Link _Later", NULL, NULL);
     g_signal_connect (action, "activate", G_CALLBACK (window_read_later), window);
-    yelp_view_add_link_action (priv->view, action);
+    yelp_view_add_link_action (priv->view, action,
+                               (YelpViewActionValidFunc) view_is_xref_uri,
+                               window);
     g_signal_connect (priv->application, "read-later-changed", G_CALLBACK (app_read_later_changed), window);
 
     priv->vbox_full = gtk_vbox_new (FALSE, 3);
@@ -1486,6 +1492,15 @@ view_uri_selected (YelpView     *view,
     g_object_unref (uri);
 }
 
+static gboolean
+view_is_xref_uri (YelpView    *view,
+                  GtkAction   *action,
+                  const gchar *uri,
+                  YelpWindow  *window)
+{
+    return g_str_has_prefix (uri, "xref:");
+}
+
 static void
 view_root_title (YelpView    *view,
                  GParamSpec  *pspec,



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