[balsa/wip/gtk4] Fix the build after rebasing



commit 8077427601a51ac9e3b84742e54e3290a6816def
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Mon Jun 25 15:27:26 2018 -0400

    Fix the build after rebasing
    
    We rebased on master to pick up the migration from libnotify to
    GNotification; naturally, there was some fallout :-)

 libbalsa/information.c | 126 +++++++++++--------------------------------------
 libbalsa/libbalsa.h    |   2 +-
 src/ab-main.c          |  44 ++++++++++++++---
 src/main-window.c      | 119 ++++++++++++----------------------------------
 src/main.c             |  73 +++++++++++++++-------------
 5 files changed, 138 insertions(+), 226 deletions(-)
---
diff --git a/libbalsa/information.c b/libbalsa/information.c
index edb2f6a35..7fdc5e83f 100644
--- a/libbalsa/information.c
+++ b/libbalsa/information.c
@@ -23,26 +23,9 @@
 #endif                          /* HAVE_CONFIG_H */
 #include "information.h"
 #include "libbalsa.h"
-
-#ifdef HAVE_NOTIFY
-#include <libnotify/notify.h>
-#include <gtk/gtk.h>
-#endif
 #include <string.h>
 
-struct information_data {
-    GtkWindow *parent;
-    LibBalsaInformationType message_type;
-    gchar *msg;
-};
-
-static gboolean libbalsa_information_idle_handler(struct information_data*);
-
-LibBalsaInformationFunc libbalsa_real_information_func;
-
-#ifdef HAVE_NOTIFY
-static void lbi_notification_closed_cb(NotifyNotification * note,
-                                       gpointer data);
+static GNotification *notification;
 
 static void
 lbi_notification_parent_weak_notify(gpointer data, GObject * parent)
@@ -50,14 +33,9 @@ lbi_notification_parent_weak_notify(gpointer data, GObject * parent)
     if (notification == NULL)
         return;
 
-static void
-lbi_notification_closed_cb(NotifyNotification * note, gpointer data)
-{
-    GObject *parent = G_OBJECT(data);
-    g_object_weak_unref(parent, lbi_notification_parent_weak_notify, note);
-    g_object_unref(note);
+    g_object_set_data(G_OBJECT(notification), "send", GINT_TO_POINTER(FALSE));
+    g_signal_emit_by_name(notification, "notify", NULL);
 }
-#endif
 
 void
 libbalsa_information_varg(GtkWindow *parent, LibBalsaInformationType type,
@@ -106,71 +84,31 @@ libbalsa_information_varg(GtkWindow *parent, LibBalsaInformationType type,
         case '>':
             g_string_append(escaped, "&gt;");
             break;
-        case LIBBALSA_INFORMATION_WARNING:
-            icon_str = "dialog-warning";
+        case '&':
+            g_string_append(escaped, "&amp;");
             break;
-        case LIBBALSA_INFORMATION_ERROR:
-            icon_str = "dialog-error";
+        case '"':
+            g_string_append(escaped, "&quot;");
             break;
         default:
             break;
         }
-        msg = g_strdup_vprintf(fmt, ap);
-        /* libnotify/DBUS uses HTML markup, so we must replace '<' and
-         * '&' with the corresponding entity in the message string. */
-        escaped = g_string_new(NULL);
-        for (p = msg; (q = strpbrk(p, "<>&\"")) != NULL; p = ++q) {
-            g_string_append_len(escaped, p, q - p);
-            switch (*q) {
-                case '<': g_string_append(escaped, "&lt;");   break;
-                case '>': g_string_append(escaped, "&gt;");   break;
-                case '&': g_string_append(escaped, "&amp;");  break;
-                case '"': g_string_append(escaped, "&quot;"); break;
-                default: break;
-            }
-        }
-        g_string_append(escaped, p);
-        g_free(msg);
-
-#if HAVE_NOTIFY >= 7
-        note = notify_notification_new("Balsa", escaped->str, icon_str);
-        notify_notification_set_hint(note, "desktop-entry",
-                                     g_variant_new_string("balsa"));
-#else
-        /* prior to 0.7.0 */
-        note = notify_notification_new("Balsa", escaped->str, icon_str, NULL);
-#endif
+    }
+    g_string_append(escaped, p);
+    g_free(msg);
 
-        g_string_free(escaped, TRUE);
-
-        notify_notification_set_timeout(note, 7000);    /* 7 seconds */
-        notify_notification_show(note, NULL);
-        if (parent) {
-            /* Close with parent if earlier. */
-            g_object_weak_ref(G_OBJECT(parent),
-                              lbi_notification_parent_weak_notify, note);
-            g_signal_connect(note, "closed",
-                             G_CALLBACK(lbi_notification_closed_cb),
-                             parent);
-        } else
-            g_object_unref(note);
-        return;
+    g_notification_set_body(notification, escaped->str);
+    send = *escaped->str != '\0';
+    g_string_free(escaped, TRUE);
+
+    g_object_set_data(G_OBJECT(notification), "send", GINT_TO_POINTER(send));
+    g_signal_emit_by_name(notification, "notify", NULL);
+
+    if (parent != NULL) {
+        /* Close with parent if earlier. */
+        g_object_weak_ref(G_OBJECT(parent),
+                          lbi_notification_parent_weak_notify, NULL);
     }
-    /* Fall through to the ordinary notification scheme */
-#endif
-    data = g_new(struct information_data, 1);
-    data->parent = parent;
-    data->message_type = type;
-
-    /* We format the string here. It must be free()d in the idle
-     * handler We parse the args here because by the time the idle
-     * function runs we will no longer be in this stack frame. 
-     */
-    data->msg = g_strdup_vprintf(fmt, ap);
-    if (parent)
-        g_object_add_weak_pointer(G_OBJECT(parent),
-                                  (gpointer) & data->parent);
-    g_idle_add((GSourceFunc) libbalsa_information_idle_handler, data);
 }
 
 void
@@ -198,20 +136,12 @@ libbalsa_information_parented(GtkWindow *parent, LibBalsaInformationType type,
     va_end(va_args);
 }
 
-/*
- * This is an idle handler, so we need to grab the GDK lock 
- */
-static gboolean
-libbalsa_information_idle_handler(struct information_data *data)
+GNotification *
+libbalsa_notification_new(const gchar *title)
 {
-    libbalsa_real_information_func(data->parent,
-                                   data->message_type,
-                                   data->msg);
-
-    if(data->parent)
-        g_object_remove_weak_pointer(G_OBJECT(data->parent), 
-                                     (gpointer) &data->parent);
-    g_free(data->msg);
-    g_free(data);
-    return FALSE;
+    notification = g_notification_new(title);
+
+    g_object_add_weak_pointer(G_OBJECT(notification), (gpointer *) &notification);
+
+    return notification;
 }
diff --git a/libbalsa/libbalsa.h b/libbalsa/libbalsa.h
index 4e9a78215..74d40e647 100644
--- a/libbalsa/libbalsa.h
+++ b/libbalsa/libbalsa.h
@@ -109,7 +109,7 @@ enum {
 /*
  * Initialize the library
  */
-void libbalsa_init(LibBalsaInformationFunc information_callback);
+void libbalsa_init(void);
 void libbalsa_set_spool(const gchar * spool);
 
 void libbalsa_show_message_source(GtkApplication * application,
diff --git a/src/ab-main.c b/src/ab-main.c
index 7944363e2..683624ccd 100644
--- a/src/ab-main.c
+++ b/src/ab-main.c
@@ -1045,12 +1045,6 @@ bab_init(void)
     memset(&contacts_app, 0, sizeof(contacts_app));
 }
 
-static void
-information_real(void)
-{
-    /* FIXME */
-}
-
 static void
 bab_set_intial_address_book(LibBalsaAddressBook * ab,
                             GtkWidget           * window)
@@ -1082,6 +1076,44 @@ libbalsa_dialog_flags(void)
        return dialog_flags;
 }
 
+/*
+ * Set up GNotification for libbalsa
+ */
+
+#define BALSA_AB_NOTIFICATION "balsa-ab-notification"
+
+static void
+balsa_ab_notification_notify_cb(GNotification *notification, GApplication *application)
+{
+    gboolean send;
+
+    send = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(notification), "send"));
+    if (send) {
+        g_application_send_notification(application,
+                                        BALSA_AB_NOTIFICATION, notification);
+    } else {
+        g_application_withdraw_notification(application, BALSA_AB_NOTIFICATION);
+    }
+}
+
+static void
+balsa_ab_notification_shutdown_cb(GApplication *application, GNotification *notification)
+{
+    g_object_unref(notification);
+}
+
+static void
+balsa_ab_setup_libbalsa_notification(GApplication *application)
+{
+    GNotification *notification;
+
+    notification = libbalsa_notification_new("BalsaAb");
+    g_signal_connect(notification, "notify",
+                     G_CALLBACK(balsa_ab_notification_notify_cb), application);
+    g_signal_connect(application, "shutdown",
+                     G_CALLBACK(balsa_ab_notification_shutdown_cb), notification);
+}
+
 int
 main(int argc, char *argv[])
 {
diff --git a/src/main-window.c b/src/main-window.c
index 67f76b98d..1d2393f00 100644
--- a/src/main-window.c
+++ b/src/main-window.c
@@ -211,9 +211,7 @@ struct _BalsaWindowPrivate {
     guint activity_counter;
     GSList *activity_messages;
 
-#ifdef HAVE_NOTIFY
-    NotifyNotification *new_mail_note;
-#endif                         /* HAVE_NOTIFY */
+    gboolean new_mail_notification_sent;
 
     /* Support GNetworkMonitor: */
     gboolean network_available;
@@ -674,6 +672,8 @@ balsa_window_get_toolbar_model(void)
     return model;
 }
 
+#define NEW_MAIL_NOTIFICATION "new-mail-notification"
+
 static void
 bw_is_active_notify(GObject * gobject, GParamSpec * pspec,
                     gpointer user_data)
@@ -684,9 +684,15 @@ bw_is_active_notify(GObject * gobject, GParamSpec * pspec,
         BalsaWindow *window = BALSA_WINDOW(gobject);
         BalsaWindowPrivate *priv = balsa_window_get_instance_private(window);
 
-        if (priv->new_mail_note)
-            notify_notification_close(priv->new_mail_note, NULL);
-#endif                          /* HAVE_NOTIFY */
+        if (priv->new_mail_notification_sent) {
+            GtkApplication *application = gtk_window_get_application(gtk_window);
+
+            g_application_withdraw_notification(G_APPLICATION(application),
+                                                NEW_MAIL_NOTIFICATION);
+
+            priv->new_mail_notification_sent = FALSE;
+        }
+
         gtk_window_set_urgency_hint(gtk_window, FALSE);
     }
 }
@@ -1580,7 +1586,7 @@ view_source_activated(GSimpleAction * action,
     for (list = messages; list; list = list->next) {
        LibBalsaMessage *message = list->data;
 
-       libbalsa_show_message_source(balsa_app.application,
+       libbalsa_show_message_source(application,
                                      message, balsa_app.message_font,
                                     &balsa_app.source_escape_specials,
                                      &balsa_app.source_width,
@@ -2089,6 +2095,7 @@ balsa_window_add_action_entries(GActionMap * action_map)
 static void
 bw_set_menus(BalsaWindow * window)
 {
+    GtkApplication *application = gtk_window_get_application(GTK_WINDOW(window));
     BalsaWindowPrivate *priv = balsa_window_get_instance_private(window);
     GtkBuilder *builder;
     static const gchar resource_path[] =
@@ -2102,11 +2109,11 @@ bw_set_menus(BalsaWindow * window)
     if (gtk_builder_add_from_resource(builder, resource_path, &err)) {
         GtkWidget *menubar;
 
-        gtk_application_set_app_menu(balsa_app.application,
+        gtk_application_set_app_menu(application,
                                      G_MENU_MODEL(gtk_builder_get_object
                                                   (builder, "app-menu")));
 #ifdef SET_MENUBAR_SETS_A_VISIBLE_MENUBAR
-        gtk_application_set_menubar(balsa_app.application,
+        gtk_application_set_menubar(application,
                                     G_MENU_MODEL(gtk_builder_get_object
                                                  (builder, "menubar")));
 #else /* SET_MENUBAR_SETS_A_VISIBLE_MENUBAR */
@@ -3592,103 +3599,37 @@ bw_display_new_mail_notification(int num_new, int has_new)
     GtkWindow *window = GTK_WINDOW(balsa_app.main_window);
     BalsaWindowPrivate *priv =
         balsa_window_get_instance_private(balsa_app.main_window);
-    static GtkWidget *dlg = NULL;
     static gint num_total = 0;
     gchar *msg = NULL;
     static GNotification *notification;
     GtkApplication *application;
 
-    if (num_new <= 0 && has_new <= 0)
+    if (!balsa_app.notify_new_mail_dialog)
         return;
 
-    if (!gtk_window_is_active(window))
-        gtk_window_set_urgency_hint(window, TRUE);
-
-    if (!balsa_app.notify_new_mail_dialog)
+    if (gtk_window_is_active(window))
         return;
+    else
+         gtk_window_set_urgency_hint(window, TRUE);
 
-#ifdef HAVE_NOTIFY
-    /* Before attemtping to use the notifications check whether they
-       are actually available - perhaps the underlying connection to
-       dbus could not be created? In any case, we must not continue or
-       ugly things will happen, at least with libnotify-0.4.2. */
-    if (notify_is_initted()) {
-        if (gtk_window_is_active(window))
-            return;
+    if (g_once_init_enter(&notification)) {
+        GNotification *tmp;
+        GIcon *icon;
 
-        if (priv->new_mail_note) {
-            /* the user didn't acknowledge the last info, so we'll
-             * accumulate the count */
-            num_total += num_new;
-        } else {
-            num_total = num_new;
-#if HAVE_NOTIFY >=7
-            priv->new_mail_note =
-                notify_notification_new("Balsa", NULL, NULL);
-            notify_notification_set_hint(priv-> new_mail_note, "desktop-entry",
-                                         g_variant_new_string("balsa"));
-#else
-            priv->new_mail_note =
-                notify_notification_new("Balsa", NULL, NULL, NULL);
-#endif
-            g_object_add_weak_pointer(G_OBJECT(priv-> new_mail_note),
-                                      (gpointer) & priv-> new_mail_note);
-            g_signal_connect(priv->new_mail_note,
-                             "closed", G_CALLBACK(g_object_unref), NULL);
-        }
-    } else {
-        if (dlg) {
-            /* the user didn't acknowledge the last info, so we'll
-             * accumulate the count */
-            num_total += num_new;
-            gtk_window_present(GTK_WINDOW(dlg));
-        } else {
-            num_total = num_new;
-            dlg = gtk_message_dialog_new(NULL, /* NOT transient for
-                                                * Balsa's main window */
-                    (GtkDialogFlags) 0,
-                    GTK_MESSAGE_INFO,
-                    GTK_BUTTONS_OK, "%s", msg);
-            gtk_window_set_title(GTK_WINDOW(dlg), _("Balsa: New mail"));
-            gtk_window_set_role(GTK_WINDOW(dlg), "new_mail_dialog");
-            gtk_window_set_type_hint(GTK_WINDOW(dlg),
-                    GDK_SURFACE_TYPE_HINT_NORMAL);
-            g_signal_connect(G_OBJECT(dlg), "response",
-                    G_CALLBACK(gtk_widget_destroy), NULL);
-            g_object_add_weak_pointer(G_OBJECT(dlg), (gpointer) & dlg);
-            gtk_widget_show(GTK_WIDGET(dlg));
-        }
+        tmp = g_notification_new("Balsa");
+        icon = g_themed_icon_new("dialog-information");
+        g_notification_set_icon(tmp, icon);
+        g_object_unref(icon);
+        g_once_init_leave(&notification, tmp);
     }
 
-    msg = bw_get_new_message_notification_string(num_new, num_total);
-    if (priv->new_mail_note != NULL) {
-        notify_notification_update(priv->new_mail_note,
-                                   "Balsa", msg, "dialog-information");
-        /* 30 seconds: */
-        notify_notification_set_timeout(priv->new_mail_note, 30000);
-        notify_notification_show(priv->new_mail_note, NULL);
-    } else
-        gtk_message_dialog_set_markup(GTK_MESSAGE_DIALOG(dlg), msg);
-#else
-    if (dlg) {
+    if (priv->new_mail_notification_sent) {
         /* the user didn't acknowledge the last info, so we'll
          * accumulate the count */
         num_total += num_new;
     } else {
         num_total = num_new;
-        dlg = gtk_message_dialog_new(NULL, /* NOT transient for
-                                            * Balsa's main window */
-                                     (GtkDialogFlags) 0,
-                                     GTK_MESSAGE_INFO,
-                                     GTK_BUTTONS_OK, "%s", msg);
-        gtk_window_set_title(GTK_WINDOW(dlg), _("Balsa: New mail"));
-        gtk_window_set_role(GTK_WINDOW(dlg), "new_mail_dialog");
-        gtk_window_set_type_hint(GTK_WINDOW(dlg),
-                                 GDK_SURFACE_TYPE_HINT_NORMAL);
-        g_signal_connect(G_OBJECT(dlg), "response",
-                         G_CALLBACK(gtk_widget_destroy), NULL);
-        g_object_add_weak_pointer(G_OBJECT(dlg), (gpointer) & dlg);
-        gtk_widget_show(GTK_WIDGET(dlg));
+        priv->new_mail_notification_sent = TRUE;
     }
 
     msg = bw_get_new_message_notification_string(num_new, num_total);
diff --git a/src/main.c b/src/main.c
index 2a1b07e57..0ec803253 100644
--- a/src/main.c
+++ b/src/main.c
@@ -472,9 +472,44 @@ balsa_check_open_compose_window(void)
     return FALSE;
 }
 
+/*
+ * Set up GNotification for libbalsa
+ */
+
+#define BALSA_NOTIFICATION "balsa-notification"
+
+static void
+balsa_notification_notify_cb(GNotification *notification,
+                             GParamSpec *pspec,
+                             GApplication *application)
+{
+    gboolean send;
+
+    send = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(notification), "send"));
+    if (send) {
+        g_application_send_notification(application,
+                                        BALSA_NOTIFICATION, notification);
+    } else {
+        g_application_withdraw_notification(application, BALSA_NOTIFICATION);
+    }
+}
+
+static void
+balsa_setup_libbalsa_notification(GApplication *application)
+{
+    GNotification *notification;
+
+    notification = libbalsa_notification_new("Balsa");
+    g_signal_connect(notification, "notify",
+                     G_CALLBACK(balsa_notification_notify_cb), application);
+    g_signal_connect_swapped(application, "shutdown",
+                             G_CALLBACK(g_object_unref), notification);
+}
+
 /* -------------------------- main --------------------------------- */
-static int
-real_main(int argc, char *argv[])
+static void
+balsa_startup_cb(GApplication *application,
+           gpointer      user_data)
 {
     gchar *default_icon;
 
@@ -520,17 +555,6 @@ real_main(int argc, char *argv[])
         g_free(default_icon);
     }
 
-    signal( SIGPIPE, SIG_IGN );
-
-    window = balsa_window_new(balsa_app.application);
-    balsa_app.main_window = BALSA_WINDOW(window);
-    g_object_add_weak_pointer(G_OBJECT(window),
-                             (gpointer) &balsa_app.main_window);
-
-    /* load mailboxes */
-    config_load_sections();
-    mailboxes_init(cmd_get_stats);
-
     if (cmd_get_stats) {
         long unread, unsent;
         balsa_get_stats(&unread, &unsent);
@@ -596,22 +620,9 @@ balsa_activate_cb(GApplication *application,
         g_idle_add((GSourceFunc) balsa_main_check_new_messages,
                    balsa_app.main_window);
 
-    accel_map_load();
-    gtk_main();
-
-    balsa_cleanup();
-    accel_map_save();
-
-    libbalsa_imap_server_close_all_connections();
-    return 0;
-}
-
-static void
-balsa_cleanup(void)
-{
-    balsa_app_destroy();
-
-    libbalsa_conf_drop_all();
+    /* load mailboxes */
+    config_load_sections();
+    mailboxes_init(cmd_get_stats);
 }
 
 /*
@@ -754,7 +765,7 @@ handle_remote(int argc, char **argv,
 }
 
 static int
-command_line_cb(GApplication            * application,
+balsa_command_line_cb(GApplication            * application,
                 GApplicationCommandLine * command_line,
                 gpointer                  user_data)
 {
@@ -797,8 +808,6 @@ main(int argc, char **argv)
     balsa_app.application = application =
         gtk_application_new("org.desktop.Balsa",
                             G_APPLICATION_HANDLES_COMMAND_LINE);
-    g_signal_connect(application, "command-line",
-                     G_CALLBACK(command_line_cb), NULL);
     g_object_set(application, "register-session", TRUE, NULL);
 
     g_signal_connect(application, "command-line",


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