[balsa] Save NetworkManager state in BalsaApp and use it



commit c046832a0558324b538a922d9bbd8f66000dfcc1
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Sat Jan 16 17:52:04 2010 -0500

    Save NetworkManager state in BalsaApp and use it
    
    2010-01-16  Peter Bloomfield
    
    	* src/balsa-app.h: new member NMState nm_state.
    	* src/main-window.c: use it to save NetworkManager state;
    	connect or disconnect IMAP on changes to/from
    	NM_STATE_CONNECTED.

 ChangeLog         |    7 +++++++
 src/balsa-app.h   |    8 ++++++++
 src/main-window.c |   48 +++++++++++++++++++++++++++++-------------------
 3 files changed, 44 insertions(+), 19 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index d912e6f..d542dd2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2010-01-16  Peter Bloomfield
 
+	* src/balsa-app.h: new member NMState nm_state.
+	* src/main-window.c: use it to save NetworkManager state;
+	connect or disconnect IMAP on changes to/from
+	NM_STATE_CONNECTED.
+
+2010-01-16  Peter Bloomfield
+
 	* libbalsa/body.c (libbalsa_message_body_save_temporary): make
 	temporary file read-only.
 
diff --git a/src/balsa-app.h b/src/balsa-app.h
index 2ac61a4..926063d 100644
--- a/src/balsa-app.h
+++ b/src/balsa-app.h
@@ -41,6 +41,10 @@
 #include <auth-client.h>		/* part of libESMTP */
 #endif
 
+#if defined(HAVE_LIBNM_GLIB)
+#include <nm-client.h>
+#endif
+
 /* Work around nonprivileged installs so we can find icons */
 #ifdef BALSA_LOCAL_INSTALL
 #define gnome_pixmap_file( s ) g_strconcat( BALSA_RESOURCE_PREFIX, "/pixmaps/", s, NULL ) 
@@ -421,6 +425,10 @@ extern struct BalsaApplication {
     /* use as default email client for GNOME */
     int default_client;
 
+#if defined(HAVE_LIBNM_GLIB)
+    /* NetworkManager state */
+    NMState nm_state;
+#endif                          /* defined(HAVE_LIBNM_GLIB) */
 } balsa_app;
 
 #define BALSA_IS_MAILBOX_SPECIAL(a) ((a)==balsa_app.inbox || (a)==balsa_app.trash || (a)==balsa_app.outbox||(a)==balsa_app.draftbox || (a)==balsa_app.sentbox)
diff --git a/src/main-window.c b/src/main-window.c
index bb2fa0a..b47ddee 100644
--- a/src/main-window.c
+++ b/src/main-window.c
@@ -1001,10 +1001,11 @@ balsa_window_init(BalsaWindow * window)
 {
 #if defined(HAVE_LIBNM_GLIB)
     NMClient *client = nm_client_new();
-    if (client)
+    if (client) {
+        balsa_app.nm_state = nm_client_get_state(client);
         g_signal_connect(client, "notify::state",
                          G_CALLBACK(bw_nm_client_state_changed_cb), NULL);
-    else
+    } else
         fprintf (stderr, "Could not get NetworkManager client.\n");
 #endif /* LIBNM_GLIB */
 }
@@ -3479,43 +3480,52 @@ bw_nm_client_state_changed_cb(GObject * gobject, GParamSpec * pspec,
 {
     NMClient *client = NM_CLIENT(gobject);
     NMState state = nm_client_get_state(client);
-    gboolean is_connected;
+
+    if (state == balsa_app.nm_state) {
+        /* Notify signal does not guarantee that anything really
+         * changed. */
+        return;
+    }
 
     switch (state) {
     case NM_STATE_ASLEEP:
-        fprintf(stderr, "Status: Asleep\n");
-        return;
+        fprintf(stderr, "Status: Asleep (%u)\n", (guint) time(NULL));
+        break;
     case NM_STATE_DISCONNECTED:
-        fprintf (stderr, "Status: Inactive Connection\n");
-        is_connected = FALSE;
+        fprintf(stderr, "Status: Disconnected (%u)\n", (guint) time(NULL));
         break;
     case NM_STATE_CONNECTING:
-        fprintf(stderr, "Status: Connecting...\n");
+        fprintf(stderr, "Status: Connecting... (%u)\n", (guint) time(NULL));
         return;
     case NM_STATE_CONNECTED:
-        fprintf (stderr, "Status: Active Connection\n");
-        is_connected = TRUE;
+        fprintf(stderr, "Status: Connected (%u)\n", (guint) time(NULL));
         break;
     case NM_STATE_UNKNOWN:
     default:
-        fprintf (stderr, "Status: unknown\n");
+        fprintf(stderr, "Status: unknown (%u)\n", (guint) time(NULL));
         return;
     }
 
+    if (state == NM_STATE_CONNECTED
+        || balsa_app.nm_state == NM_STATE_CONNECTED) {
+        gboolean is_connected = (state == NM_STATE_CONNECTED);
 #if BALSA_USE_THREADS
-    {
         pthread_t thread_id;
-        if (pthread_create(&thread_id,
-                   NULL, (void *) &bw_change_connection_status_thread,
+
+        if (pthread_create(&thread_id, NULL,
+                           (void *) &bw_change_connection_status_thread,
                            GINT_TO_POINTER(is_connected)) == 0)
             pthread_detach(thread_id);
-    }
 #else /* BALSA_USE_THREADS */
-    gtk_tree_model_foreach(GTK_TREE_MODEL(balsa_app.mblist_tree_store),
-			   (GtkTreeModelForeachFunc)
-                           mw_mbox_change_connection_status,
-			   GINT_TO_POINTER(is_connected));
+
+        gtk_tree_model_foreach(GTK_TREE_MODEL(balsa_app.mblist_tree_store),
+                               (GtkTreeModelForeachFunc)
+                               mw_mbox_change_connection_status,
+                               GINT_TO_POINTER(is_connected));
 #endif /* BALSA_USE_THREADS */
+    }
+
+    balsa_app.nm_state = state;
 }
 #endif /* LIBNM_GLIB */
 



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