From 71ee02905c83ab1eb1f0b4236384cdcc9ed2c8a3 Mon Sep 17 00:00:00 2001 From: Sergio Villar Senin Date: Tue, 6 Nov 2007 10:16:38 +0000 Subject: [PATCH] * Added "queue-empty" signal to the mail operation queue * Added "window-list-empty" signal to the window manager * Moved the code that closes modest to modest-main.c * Now modest is closed when there are no more windows and there are no more pending mail operations * Now Modest is properly finalized when a D-Bus action is performed, event if there is no main window * Fixes NB#74402, Modest is now closed if compose mail is invoked through D-Bus pmo-trunk-r3652 --- src/modest-mail-operation-queue.c | 21 ++++++++ src/modest-mail-operation-queue.h | 2 + src/modest-main.c | 71 +++++++++++++++++++++++---- src/widgets/modest-window-mgr.c | 96 +++++++++++++------------------------ src/widgets/modest-window-mgr.h | 13 +++++ 5 files changed, 130 insertions(+), 73 deletions(-) diff --git a/src/modest-mail-operation-queue.c b/src/modest-mail-operation-queue.c index 7b4b25d..e730712 100644 --- a/src/modest-mail-operation-queue.c +++ b/src/modest-mail-operation-queue.c @@ -44,6 +44,7 @@ on_operation_finished (ModestMailOperation *mail_op, /* list my signals */ enum { QUEUE_CHANGED_SIGNAL, + QUEUE_EMPTY_SIGNAL, NUM_SIGNALS }; @@ -115,6 +116,23 @@ modest_mail_operation_queue_class_init (ModestMailOperationQueueClass *klass) NULL, NULL, modest_marshal_VOID__POINTER_INT, G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_INT); + + /** + * ModestMailOperationQueue::queue-empty + * @self: the #ModestMailOperationQueue that emits the signal + * @user_data: user data set when the signal handler was connected + * + * Issued whenever the queue is empty + */ + signals[QUEUE_EMPTY_SIGNAL] = + g_signal_new ("queue-empty", + G_TYPE_FROM_CLASS (gobject_class), + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (ModestMailOperationQueueClass, queue_empty), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); + } static void @@ -276,6 +294,9 @@ modest_mail_operation_queue_remove (ModestMailOperationQueue *self, * until the signal emission is complete. armin. */ /* modest_runtime_verify_object_last_ref (mail_op, ""); */ g_object_unref (G_OBJECT (mail_op)); + + /* Emit the queue empty-signal */ + g_signal_emit (self, signals[QUEUE_EMPTY_SIGNAL], 0); } guint diff --git a/src/modest-mail-operation-queue.h b/src/modest-mail-operation-queue.h index 302c378..7f45317 100644 --- a/src/modest-mail-operation-queue.h +++ b/src/modest-mail-operation-queue.h @@ -64,6 +64,8 @@ struct _ModestMailOperationQueueClass { void (*queue_changed) (ModestMailOperationQueue *self, ModestMailOperation *mail_op, ModestMailOperationQueueNotification type); + + void (*queue_empty) (ModestMailOperationQueue *self); }; /* member functions */ diff --git a/src/modest-main.c b/src/modest-main.c index 14e2c8d..ebfe628 100644 --- a/src/modest-main.c +++ b/src/modest-main.c @@ -37,6 +37,45 @@ #include #include +static gboolean +on_idle_exit_modest (gpointer data) +{ + /* Protect the Gtk calls */ + gdk_threads_enter (); + + /* Wait for remaining tasks */ + while (gtk_events_pending ()) + gtk_main_iteration (); + + gtk_main_quit (); + + gdk_threads_leave (); + + return FALSE; +} + +static void +on_queue_empty (ModestMailOperationQueue *queue, + gpointer user_data) +{ + ModestWindowMgr *mgr = modest_runtime_get_window_mgr (); + + /* Exit if the queue is empty and there are no more windows */ + if (modest_window_mgr_num_windows (mgr) == 0) + g_idle_add_full (G_PRIORITY_LOW, on_idle_exit_modest, NULL, NULL); +} + +static void +on_window_list_empty (ModestWindowMgr *window_mgr, + gpointer user_data) +{ + ModestMailOperationQueue *queue = modest_runtime_get_mail_operation_queue (); + + /* Exit if there are no more windows and the queue is empty */ + if (modest_mail_operation_queue_num_elements (queue) == 0) + g_idle_add_full (G_PRIORITY_LOW, on_idle_exit_modest, NULL, NULL); +} + int main (int argc, char *argv[]) { @@ -45,8 +84,6 @@ main (int argc, char *argv[]) * be called. But that's annoying when starting from the * command line.: */ gboolean show_ui_without_top_application_method = FALSE; - - ModestWindow *main_win; int retval = 0; if (argc >= 2) { @@ -72,14 +109,17 @@ main (int argc, char *argv[]) goto cleanup; } - /* this will create & register the window */ - main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr(), TRUE); - if (!main_win) { - g_printerr ("modest: failed to get main window instance\n"); - retval = 1; - goto cleanup; - } - + /* Connect to the queue-emtpy signal */ + g_signal_connect (modest_runtime_get_mail_operation_queue (), + "queue-empty", + G_CALLBACK (on_queue_empty), + NULL); + + g_signal_connect (modest_runtime_get_window_mgr (), + "window-list-empty", + G_CALLBACK (on_window_list_empty), + NULL); + /* Usually, we only show the UI when we get the "top_application" D-Bus method. * This allows modest to start via D-Bus activation to provide a service, * without showing the UI. @@ -87,6 +127,17 @@ main (int argc, char *argv[]) * when we receive the "top_application" D-Bus method. */ if (show_ui_without_top_application_method) { + ModestWindow *main_win; + + /* this will create & register the window */ + main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr(), + TRUE); + if (!main_win) { + g_printerr ("modest: failed to get main window instance\n"); + retval = 1; + goto cleanup; + } + gtk_widget_show_all (GTK_WIDGET(main_win)); /* Remove new mail notifications if exist */ diff --git a/src/widgets/modest-window-mgr.c b/src/widgets/modest-window-mgr.c index bce17ba..caf781e 100644 --- a/src/widgets/modest-window-mgr.c +++ b/src/widgets/modest-window-mgr.c @@ -60,9 +60,8 @@ static const gchar* get_show_toolbar_key (GType window_type, /* list my signals */ enum { - /* MY_SIGNAL_1, */ - /* MY_SIGNAL_2, */ - LAST_SIGNAL + WINDOW_LIST_EMPTY_SIGNAL, + NUM_SIGNALS }; typedef struct _ModestWindowMgrPrivate ModestWindowMgrPrivate; @@ -92,7 +91,7 @@ struct _ModestWindowMgrPrivate { static GObjectClass *parent_class = NULL; /* uncomment the following if you have defined any signals */ -/* static guint signals[LAST_SIGNAL] = {0}; */ +static guint signals[NUM_SIGNALS] = {0}; GType modest_window_mgr_get_type (void) @@ -128,6 +127,23 @@ modest_window_mgr_class_init (ModestWindowMgrClass *klass) gobject_class->finalize = modest_window_mgr_finalize; g_type_class_add_private (gobject_class, sizeof(ModestWindowMgrPrivate)); + + + /** + * ModestWindowMgr::window-list-empty + * @self: the #ModestWindowMgr that emits the signal + * @user_data: user data set when the signal handler was connected + * + * Issued whenever the window list becomes empty + */ + signals[WINDOW_LIST_EMPTY_SIGNAL] = + g_signal_new ("window-list-empty", + G_TYPE_FROM_CLASS (gobject_class), + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (ModestWindowMgrClass, window_list_empty), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); } static void @@ -643,62 +659,6 @@ disconnect_msg_changed (gpointer key, g_signal_handler_disconnect (G_OBJECT (key), handler_id); } - - -/* interval before retrying to close the application */ -#define CLOSING_RETRY_INTERVAL 3000 -/* interval before cancel whatever is left in the queue, and closing anyway */ -#define MAX_WAIT_FOR_CLOSING 30 * 1000 - -static gboolean -on_quit_maybe (ModestWindowMgr *self) -{ - ModestWindowMgrPrivate *priv; - guint queue_num; - - priv = MODEST_WINDOW_MGR_GET_PRIVATE (self); - - /* it seems, in the meantime some windows were - * created. in that case, stop 'on_quit_maybe' */ - if (priv->window_list) { - priv->closing_time = 0; - return FALSE; - } - - if (priv->closing_time >= MAX_WAIT_FOR_CLOSING) { - /* we waited long enough: cancel all remaining operations */ - g_debug ("%s: we waited long enough (%ds), cancelling queue and quiting", - __FUNCTION__, priv->closing_time/1000); - /* FIXME: below gives me a lot of: - * GLIB CRITICAL ** default - modest_mail_operation_cancel: - * assertion `priv->account' failed - * which means there is no account for the given operation - * so, we're not trying to be nice, we're just quiting. - */ - //modest_mail_operation_queue_cancel_all - // (modest_runtime_get_mail_operation_queue()); - } else { - - /* if there is anything left in our operation queue, - * wait another round - */ - queue_num = modest_mail_operation_queue_num_elements - (modest_runtime_get_mail_operation_queue()); - if (queue_num > 0) { - g_debug ("%s: waiting, there are still %d operation(s) queued", - __FUNCTION__, queue_num); - priv->closing_time += CLOSING_RETRY_INTERVAL; - return TRUE; - } - } - - /* so: no windows left, nothing in the queue: quit */ - priv->closing_time = 0; - gtk_main_quit (); - return FALSE; -} - - void modest_window_mgr_unregister_window (ModestWindowMgr *self, ModestWindow *window) @@ -764,10 +724,9 @@ modest_window_mgr_unregister_window (ModestWindowMgr *self, /* Destroy the window */ gtk_widget_destroy (win->data); - /* If there are no more windows registered then exit program */ + /* If there are no more windows registered emit the signal */ if (priv->window_list == NULL) - g_timeout_add (CLOSING_RETRY_INTERVAL, - (GSourceFunc)on_quit_maybe, self); + g_signal_emit (self, signals[WINDOW_LIST_EMPTY_SIGNAL], 0); } @@ -1113,3 +1072,14 @@ on_modal_dialog_close (GtkDialog *dialog, remove_modal_from_queue (GTK_WIDGET (dialog), self); } +gint +modest_window_mgr_num_windows (ModestWindowMgr *self) +{ + ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE(self); + gint num_windows = 0; + + if (priv->window_list) + num_windows = g_list_length (priv->window_list); + + return num_windows; +} diff --git a/src/widgets/modest-window-mgr.h b/src/widgets/modest-window-mgr.h index 24cbf54..3f697b9 100644 --- a/src/widgets/modest-window-mgr.h +++ b/src/widgets/modest-window-mgr.h @@ -52,6 +52,9 @@ struct _ModestWindowMgr { struct _ModestWindowMgrClass { GObjectClass parent_class; + + /* Signals */ + void (*window_list_empty) (ModestWindowMgr *self); }; @@ -286,6 +289,16 @@ gboolean modest_window_mgr_get_hibernation_is_prevented (ModestWindowMgr *self); * application hibernation. **/ void modest_window_mgr_save_state_for_all_windows (ModestWindowMgr *self); + +/** + * modest_window_mgr_num_windows: + * @self: a #ModestWindowMgr + * + * Gets the number of already registered windows + * + * Returns: the number of already registered windows + **/ +gint modest_window_mgr_num_windows (ModestWindowMgr *self); G_END_DECLS -- 1.7.9.5