Do not restart Modest every X minutes
[modest] / src / modest-main.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <config.h>
31 #include <gdk/gdk.h>
32 #include <string.h>
33 #include <glib.h>
34 #include "modest-runtime.h"
35 #include "modest-init.h"
36 #include "modest-platform.h"
37 #include "modest-ui-actions.h"
38
39 static gboolean show_ui = FALSE;
40 static GOptionEntry option_entries [] =
41 {
42         { "show-ui", 's', 0, G_OPTION_ARG_NONE, &show_ui, "Show UI immediately, so no wait for DBUS activation", NULL },
43         { NULL }
44 };
45
46 typedef struct {
47         gulong queue_handler;
48         gulong window_list_handler;
49         gulong get_password_handler;
50 } MainSignalHandlers;
51
52 static gboolean
53 on_idle_exit_modest (gpointer data)
54 {
55         MainSignalHandlers *handlers;
56         ModestMailOperationQueue *mail_op_queue;
57
58         /* Protect the Gtk calls */
59         gdk_threads_enter ();
60         mail_op_queue = modest_runtime_get_mail_operation_queue ();
61
62         if (modest_tny_account_store_is_shutdown (modest_runtime_get_account_store ()) &&
63             modest_mail_operation_queue_running_shutdown (mail_op_queue)) {
64
65                 /* Disconnect signals. Will be freed by the destroy notify */
66                 handlers = (MainSignalHandlers *) data;
67                 g_signal_handler_disconnect (modest_runtime_get_mail_operation_queue (),
68                                              handlers->queue_handler);
69                 g_signal_handler_disconnect (modest_runtime_get_window_mgr (),
70                                              handlers->window_list_handler);
71                 g_signal_handler_disconnect (modest_runtime_get_account_store (),
72                                              handlers->get_password_handler);
73                 g_free (handlers);
74
75                 /* Wait for remaining tasks */
76                 while (gtk_events_pending ())
77                         gtk_main_iteration ();
78
79                 gtk_main_quit ();
80         } else {
81                 ModestMailOperation *mail_op;
82                 mail_op = modest_mail_operation_new (NULL);
83                 modest_mail_operation_queue_add (mail_op_queue, mail_op);
84                 modest_mail_operation_shutdown (mail_op, modest_runtime_get_account_store ());
85                 g_object_unref (mail_op);
86         }
87
88         gdk_threads_leave ();
89
90         return FALSE;
91 }
92
93 static void
94 on_queue_empty (ModestMailOperationQueue *queue,
95                 gpointer user_data)
96 {
97         ModestWindowMgr *mgr = modest_runtime_get_window_mgr ();
98
99         if (!modest_runtime_get_allow_shutdown ())
100                 return;
101
102         /* Exit if the queue is empty and there are no more
103            windows. We can exit as well if the main window is hidden
104            and it's the only one */
105         if (modest_window_mgr_get_num_windows (mgr) == 0)
106                 g_idle_add_full (G_PRIORITY_LOW, on_idle_exit_modest, user_data, NULL);
107 }
108
109 static void
110 on_window_list_empty (ModestWindowMgr *window_mgr,
111                       gpointer user_data)
112 {
113         ModestMailOperationQueue *queue = modest_runtime_get_mail_operation_queue ();
114
115         if (!modest_runtime_get_allow_shutdown ())
116                 return;
117
118         /* Exit if there are no more windows and the queue is empty */
119         if (modest_mail_operation_queue_num_elements (queue) == 0)
120                 g_idle_add_full (G_PRIORITY_LOW, on_idle_exit_modest, user_data, NULL);
121 }
122
123 int
124 main (int argc, char *argv[])
125 {
126         /* Usually we don't show the application at first,
127          * because we wait for the top_application D-Bus method to
128          * be called. But that's annoying when starting from the
129          * command line.: */
130         int retval  = 0;
131         MainSignalHandlers *handlers;
132         ModestTnyAccountStore *acc_store;
133
134         GError *error = NULL;
135         GOptionContext *context;
136
137         ModestWindowMgr *mgr;
138
139         context = g_option_context_new ("- Modest email client");
140         g_option_context_add_main_entries (context, option_entries, GETTEXT_PACKAGE);
141         g_option_context_add_group (context, gtk_get_option_group (TRUE));
142         if (!g_option_context_parse (context, &argc, &argv, &error)) {
143                 g_print ("option parsing failed: %s\n", error->message);
144                 g_option_context_free (context);
145                 exit (1);
146         }
147         g_option_context_free (context);
148
149         if (!show_ui) {
150                 g_print ("modest: use 'modest -s' to start from cmdline  with UI\n");
151         }
152
153         if (!g_thread_supported())
154                 g_thread_init (NULL);
155
156         gdk_threads_init ();
157         gdk_threads_enter ();
158
159         if (!getenv("DISPLAY")) {
160                 g_printerr ("modest: DISPLAY env variable is not set\n");
161                 retval = 1;
162                 goto cleanup;
163         }
164
165         if (!gtk_init_check (&argc, &argv)) {
166                 g_printerr ("modest: failed to initialize gtk\n");
167                 retval = 1;
168                 goto cleanup;
169         }
170
171         if (!modest_init (argc, argv)) {
172                 g_printerr ("modest: cannot init modest\n");
173                 retval = 1;
174                 goto cleanup;
175         }
176
177         /* Create the account store & launch send queues */
178         acc_store = modest_runtime_get_account_store ();
179         modest_tny_account_store_start_send_queues (acc_store);
180
181         handlers = g_malloc0 (sizeof (MainSignalHandlers));
182         /* Connect to the "queue-emtpy" signal */
183         handlers->queue_handler =
184                 g_signal_connect (modest_runtime_get_mail_operation_queue (),
185                                   "queue-empty",
186                                   G_CALLBACK (on_queue_empty),
187                                   handlers);
188
189         /* Connect to the "window-list-emtpy" signal */
190         handlers->window_list_handler =
191                 g_signal_connect (modest_runtime_get_window_mgr (),
192                                   "window-list-empty",
193                                   G_CALLBACK (on_window_list_empty),
194                                   handlers);
195
196         /* Connect to the "password-requested" signal */
197         handlers->get_password_handler =
198                 g_signal_connect (acc_store,
199                                   "password_requested",
200                                   G_CALLBACK (modest_ui_actions_on_password_requested),
201                                   NULL);
202
203         /* Create cached windows */
204         mgr = modest_runtime_get_window_mgr ();
205         modest_window_mgr_create_caches (mgr);
206
207         /* Usually, we only show the UI when we get the "top_application" D-Bus method.
208          * This allows modest to start via D-Bus activation to provide a service,
209          * without showing the UI.
210          * The UI will be shown later (or just after starting if no otehr D-Bus method was used),
211          * when we receive the "top_application" D-Bus method.
212          */
213         if (show_ui) {
214                 ModestWindow *window;
215
216                 mgr = modest_runtime_get_window_mgr();
217                 window = modest_window_mgr_show_initial_window (mgr);
218                 if (!window) {
219                         g_printerr ("modest: failed to get main window instance\n");
220                         retval = 1;
221                         goto cleanup;
222                 }
223                 /* Remove new mail notifications if exist */
224                 modest_platform_remove_new_mail_notifications (FALSE);
225         }
226
227         gtk_main ();
228
229 cleanup:
230         gdk_threads_leave ();
231
232         if (!modest_init_uninit ()) {
233                 g_printerr ("modest: modest_init_uninit failed\n");
234                 retval = 1;
235         }
236
237         return retval;
238 }