[balsa/gtk4: 64/193] Various: Improve the MRU menu helpers




commit 342e5ead0726f840b48704f324ce4b4253e533a8
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Sun Jun 28 12:53:00 2020 -0400

    Various: Improve the MRU menu helpers
    
    Users of the menu don't need to know how the action's state is obtained
    from the action and the user-data.

 src/balsa-index.c               |  6 +-----
 src/balsa-mblist.c              | 31 +++++++++++++++++++++----------
 src/balsa-mblist.h              | 10 ++++++----
 src/balsa-message.c             |  5 +----
 src/balsa-mime-widget-message.c |  5 +----
 src/message-window.c            |  6 +-----
 6 files changed, 31 insertions(+), 32 deletions(-)
---
diff --git a/src/balsa-index.c b/src/balsa-index.c
index 3abd16231..21f194a5a 100644
--- a/src/balsa-index.c
+++ b/src/balsa-index.c
@@ -2037,13 +2037,9 @@ move_to_activated(GSimpleAction *action,
                   gpointer       user_data)
 {
     BalsaIndex *bindex = user_data;
-    const gchar *action_name;
-    const gchar *url;
     LibBalsaMailbox *mailbox;
 
-    action_name = g_action_get_name(G_ACTION(action));
-    url = g_object_get_data(G_OBJECT(bindex), action_name);
-    mailbox = balsa_mblist_mru_get_mailbox_from_url(url, GTK_WIDGET(bindex));
+    mailbox = balsa_mblist_mru_get_mailbox(action, GTK_WIDGET(bindex), user_data);
 
     if (mailbox != NULL) {
         balsa_mblist_mru_add(&balsa_app.folder_mru, libbalsa_mailbox_get_url(mailbox));
diff --git a/src/balsa-mblist.c b/src/balsa-mblist.c
index f47bc87be..3f90dccae 100644
--- a/src/balsa-mblist.c
+++ b/src/balsa-mblist.c
@@ -1690,6 +1690,8 @@ static void bmbl_mru_activated_cb(GtkTreeView * tree_view,
  * last entry that pops up the whole mailbox tree.
  */
 
+#define BALSA_MBLIST_MRU_MENU_OTHER "-"
+
 static void
 bmbl_add_action(GMenu       *section,
                 const gchar *url,
@@ -1718,7 +1720,8 @@ bmbl_add_action(GMenu       *section,
     g_action_map_add_action(action_map, G_ACTION(action));
     g_object_unref(action);
 
-    g_object_set_data_full(user_data, action_name, g_strdup(url), g_free);
+    if (strcmp(url, BALSA_MBLIST_MRU_MENU_OTHER) != 0)
+        g_object_set_data_full(user_data, action_name, g_strdup(url), g_free);
 
     if (action_namespace != NULL) {
         gchar *prefixed_action_name;
@@ -1766,8 +1769,8 @@ balsa_mblist_mru_menu(GList      **url_list,
 
     /* Other… section */
     section = g_menu_new();
-    bmbl_add_action(section, "-", action_map, action_enabled, action_namespace,
-                    _("_Other…"), callback, user_data);
+    bmbl_add_action(section, BALSA_MBLIST_MRU_MENU_OTHER, action_map, action_enabled,
+                    action_namespace, _("_Other…"), callback, user_data);
     g_menu_append_section(menu, NULL, G_MENU_MODEL(section));
     g_object_unref(section);
 
@@ -2424,9 +2427,15 @@ bmbl_choose_mailbox(GtkWidget *widget)
  */
 
 const gchar *
-balsa_mblist_mru_get_url(const gchar *url, GtkWidget *widget)
+balsa_mblist_mru_get_url(GSimpleAction *action, GtkWidget *widget, GObject *object)
 {
-    if (url[0] == '-' && url[1] == '\0') {
+    const gchar *action_name;
+    const gchar *url;
+
+    action_name = g_action_get_name(G_ACTION(action));
+    url = g_object_get_data(object, action_name);
+
+    if (url == NULL) {
         LibBalsaMailbox *mailbox;
 
         mailbox = bmbl_choose_mailbox(widget);
@@ -2438,14 +2447,16 @@ balsa_mblist_mru_get_url(const gchar *url, GtkWidget *widget)
 }
 
 LibBalsaMailbox *
-balsa_mblist_mru_get_mailbox_from_url(const gchar *url, GtkWidget *widget)
+balsa_mblist_mru_get_mailbox(GSimpleAction *action, GtkWidget *widget, GObject *object)
 {
+    const gchar *action_name;
+    const gchar *url;
     LibBalsaMailbox *mailbox;
 
-    if (url[0] == '-' && url[1] == '\0')
-        mailbox = bmbl_choose_mailbox(widget);
-    else
-        mailbox = balsa_find_mailbox_by_url(url);
+    action_name = g_action_get_name(G_ACTION(action));
+    url = g_object_get_data(object, action_name);
+
+    mailbox = url == NULL ? bmbl_choose_mailbox(widget) : balsa_find_mailbox_by_url(url);
 
     return mailbox;
 }
diff --git a/src/balsa-mblist.h b/src/balsa-mblist.h
index 23b667582..165118592 100644
--- a/src/balsa-mblist.h
+++ b/src/balsa-mblist.h
@@ -59,10 +59,12 @@ GMenu * balsa_mblist_mru_menu(GList      **url_list,
                               gpointer     user_data);
 void balsa_mblist_mru_add(GList ** url_list, const gchar * url);
 void balsa_mblist_mru_drop(GList ** url_list, const gchar * url);
-const gchar     *balsa_mblist_mru_get_url             (const gchar *url,
-                                                       GtkWidget   *widget);
-LibBalsaMailbox *balsa_mblist_mru_get_mailbox_from_url(const gchar *url,
-                                                       GtkWidget   *widget);
+const gchar     *balsa_mblist_mru_get_url    (GSimpleAction *action,
+                                              GtkWidget     *widget,
+                                              GObject       *object);
+LibBalsaMailbox *balsa_mblist_mru_get_mailbox(GSimpleAction *action,
+                                              GtkWidget     *widget,
+                                              GObject       *object);
 
 GtkWidget *balsa_mblist_mru_option_menu(GtkWindow * window, 
                                         GList ** url_list);
diff --git a/src/balsa-message.c b/src/balsa-message.c
index 13a47f0bc..f028a7733 100644
--- a/src/balsa-message.c
+++ b/src/balsa-message.c
@@ -1825,12 +1825,9 @@ copy_part_activated(GSimpleAction *action,
                     gpointer       user_data)
 {
     BalsaPartInfo *info = user_data;
-    const gchar *action_name;
     const gchar *url;
 
-    action_name = g_action_get_name(G_ACTION(action));
-    url = g_object_get_data(G_OBJECT(info), action_name);
-    url = balsa_mblist_mru_get_url(url, info->popup_widget);
+    url = balsa_mblist_mru_get_url(action, info->popup_widget, user_data);
 
     if (url != NULL) {
         balsa_mblist_mru_add(&balsa_app.folder_mru, url);
diff --git a/src/balsa-mime-widget-message.c b/src/balsa-mime-widget-message.c
index 9de7f4bb9..a448a51c6 100644
--- a/src/balsa-mime-widget-message.c
+++ b/src/balsa-mime-widget-message.c
@@ -444,12 +444,9 @@ copy_activated(GSimpleAction *action,
 {
     LibBalsaMessageBody *part = g_object_get_data(user_data, "part");
     GtkWidget *widget = user_data;
-    const gchar *action_name;
     const gchar *url;
 
-    action_name = g_action_get_name(G_ACTION(action));
-    url = g_object_get_data(G_OBJECT(widget), action_name);
-    url = balsa_mblist_mru_get_url(url, widget);
+    url = balsa_mblist_mru_get_url(action, widget, user_data);
 
     if (url != NULL) {
         balsa_mblist_mru_add(&balsa_app.folder_mru, url);
diff --git a/src/message-window.c b/src/message-window.c
index bcd04bfd1..ef071403d 100644
--- a/src/message-window.c
+++ b/src/message-window.c
@@ -800,13 +800,9 @@ move_to_activated(GSimpleAction *action,
                   gpointer       user_data)
 {
     GtkWidget *window = user_data;
-    const gchar *action_name;
-    const gchar *url;
     LibBalsaMailbox *mailbox;
 
-    action_name = g_action_get_name(G_ACTION(action));
-    url = g_object_get_data(G_OBJECT(window), action_name);
-    mailbox = balsa_mblist_mru_get_mailbox_from_url(url, window);
+    mailbox = balsa_mblist_mru_get_mailbox(action, window, user_data);
 
     if (mailbox != NULL) {
         MessageWindow *mw = g_object_get_data(G_OBJECT(window), "mw");


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