Cancel account operations on disposing folder window.
[modest] / src / modest-runtime.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 <glib.h>
32 #include <glib-object.h>
33 #include <glib/gstdio.h>
34 #include <modest-runtime.h>
35 #include <modest-runtime-priv.h>
36 #include <modest-defs.h>
37 #include <modest-singletons.h>
38 #include <widgets/modest-header-view.h>
39 #include <widgets/modest-folder-view.h>
40 #include <modest-tny-platform-factory.h>
41 #include <modest-platform.h>
42 #include <modest-widget-memory.h>
43 #include <modest-widget-memory-priv.h>
44 #include <modest-local-folder-info.h>
45 #include <modest-account-mgr.h>
46 #include <modest-account-mgr-helpers.h>
47 #include <modest-icon-names.h>
48 #include <modest-ui-actions.h>
49 #include <modest-debug.h>
50
51 static ModestSingletons       *_singletons    = NULL;
52
53 // we get the account store here instead of in Singletons
54 // as it leads to various chicken & problems with initialization
55 static ModestTnyAccountStore  *_account_store  = NULL;
56
57 /* Signal handlers for the send queues */
58 static GSList *_sig_handlers = NULL;
59
60 /* will modest shut down when window list and mail op queue are empty? */
61 static gboolean _allow_shutdown = TRUE;
62
63 /*
64  * private functions declared in modest-runtime-priv.h -
65  * only to be called from modest-init.c 
66  */
67 /*-----------------------------------------------------------------------------*/
68 gboolean
69 modest_runtime_init (void)
70 {
71         if (_singletons) {
72                 g_printerr ("modest: modest_runtime_init can only be run once\n");
73                 return FALSE;
74         }
75
76         _singletons = modest_singletons_new ();
77         if (!_singletons) {
78                 g_printerr ("modest: failed to create singletons\n");
79                 return FALSE;
80         }
81
82         return TRUE;
83 }
84
85 gboolean
86 modest_runtime_uninit (void)
87 {
88         if (!_singletons)
89                 return TRUE;    /* uninit maybe called if runtime_init failed */
90         
91         g_return_val_if_fail (MODEST_IS_SINGLETONS(_singletons), FALSE);
92
93         g_debug ("%s: cleaning up", __FUNCTION__);
94
95         if (_sig_handlers) {
96                 modest_signal_mgr_disconnect_all_and_destroy (_sig_handlers);
97                 _sig_handlers = NULL;
98         }
99
100         g_debug ("%s: cleaned up signal manager", __FUNCTION__);
101
102         MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(_singletons,"");
103         g_object_unref(_singletons);
104         _singletons = NULL;
105
106         g_debug ("%s: cleaned up singletons", __FUNCTION__);
107
108         if (_account_store) {
109                 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(_account_store,"");
110                 g_object_unref(_account_store);
111                 _account_store = NULL;
112         }
113
114         g_debug ("%s: cleaned up the account store", __FUNCTION__);
115
116         
117         g_debug ("%s: all cleaned up", __FUNCTION__);
118
119         
120         return TRUE;
121 }
122 /*-----------------------------------------------------------------------------*/
123         
124
125 ModestAccountMgr*
126 modest_runtime_get_account_mgr   (void)
127 {
128         g_return_val_if_fail (_singletons, NULL);
129         return modest_singletons_get_account_mgr (_singletons);
130 }
131
132 TnyStreamCache*
133 modest_runtime_get_images_cache   (void)
134 {
135         g_return_val_if_fail (_singletons, NULL);
136         return modest_singletons_get_images_cache (_singletons);
137 }
138
139 ModestEmailClipboard*
140 modest_runtime_get_email_clipboard   (void)
141 {
142         g_return_val_if_fail (_singletons, NULL);
143         return modest_singletons_get_email_clipboard (_singletons);
144 }
145
146 ModestTnyAccountStore*
147 modest_runtime_get_account_store   (void)
148 {
149         g_return_val_if_fail (_singletons, NULL);
150         /* we get the account store here instead of in Singletons as
151            it leads to various chicken & egg problems with
152            initialization */
153         if (!_account_store) {
154                 _account_store  = modest_tny_account_store_new (modest_runtime_get_account_mgr(),
155                                                                 modest_runtime_get_device());
156                 if (!_account_store) {
157                         g_printerr ("modest: cannot create modest tny account store instance\n");
158                         return NULL;
159                 }
160         }
161         return _account_store;
162 }
163
164 ModestConf*
165 modest_runtime_get_conf (void)
166 {
167         g_return_val_if_fail (_singletons, NULL);
168         return modest_singletons_get_conf (_singletons);
169 }
170
171
172 ModestCacheMgr*
173 modest_runtime_get_cache_mgr (void)
174 {
175         g_return_val_if_fail (_singletons, NULL);
176         return modest_singletons_get_cache_mgr (_singletons);
177 }
178
179
180 ModestMailOperationQueue*
181 modest_runtime_get_mail_operation_queue (void)
182 {
183         g_return_val_if_fail (_singletons, NULL);
184         return modest_singletons_get_mail_operation_queue (_singletons);
185 }
186
187
188
189 TnyDevice*
190 modest_runtime_get_device (void)
191 {
192         g_return_val_if_fail (_singletons, NULL);
193         return modest_singletons_get_device (_singletons);
194 }
195
196
197 TnyPlatformFactory*
198 modest_runtime_get_platform_factory  (void)
199 {
200         g_return_val_if_fail (_singletons, NULL);
201         return modest_singletons_get_platform_factory (_singletons);
202 }
203
204 ModestTnySendQueue*
205 modest_runtime_get_send_queue  (TnyTransportAccount *account,
206                                 gboolean create)
207 {
208         ModestCacheMgr *cache_mgr;
209         GHashTable     *send_queue_cache;
210         gpointer       orig_key = NULL, send_queue = NULL;
211         
212         g_return_val_if_fail (_singletons, NULL);
213         g_return_val_if_fail (TNY_IS_TRANSPORT_ACCOUNT(account), NULL);
214
215         cache_mgr = modest_singletons_get_cache_mgr (_singletons);
216         send_queue_cache = modest_cache_mgr_get_cache (cache_mgr,
217                                                        MODEST_CACHE_MGR_CACHE_TYPE_SEND_QUEUE);
218
219         /* Each modest account has its own send queue.
220          * Note that each modest account will have its own outbox folder, 
221          * returned by TnySendQueue::get_outbox_func().
222          */
223         if (!g_hash_table_lookup_extended (send_queue_cache, account, &orig_key, &send_queue) &&
224             create) {
225                 /* Note that this send queue will start sending messages from its outbox 
226                  * as soon as it is instantiated: */
227                 send_queue = modest_tny_send_queue_new (TNY_CAMEL_TRANSPORT_ACCOUNT(account));
228
229                 if (send_queue) {
230                         _sig_handlers = 
231                                 modest_signal_mgr_connect (_sig_handlers, 
232                                                            send_queue, 
233                                                            "error_happened",
234                                                            G_CALLBACK (modest_ui_actions_on_send_queue_error_happened), 
235                                                            NULL);
236
237                         _sig_handlers = 
238                                 modest_signal_mgr_connect (_sig_handlers, 
239                                                            send_queue, 
240                                                            "status_changed",
241                                                            G_CALLBACK (modest_ui_actions_on_send_queue_status_changed), 
242                                                            NULL);
243
244                         g_hash_table_insert (send_queue_cache, 
245                                              g_object_ref (account), 
246                                              send_queue);
247                 }
248         }
249
250         return (send_queue) ? MODEST_TNY_SEND_QUEUE(send_queue) : NULL;
251 }
252
253 void modest_runtime_remove_all_send_queues ()
254 {
255         ModestCacheMgr *cache_mgr = modest_singletons_get_cache_mgr (_singletons);
256         
257         modest_cache_mgr_flush (cache_mgr, MODEST_CACHE_MGR_CACHE_TYPE_SEND_QUEUE);
258 }
259
260 void 
261 modest_runtime_remove_send_queue (TnyTransportAccount *account)
262 {
263
264         ModestCacheMgr *cache_mgr;
265         GHashTable     *send_queue_cache;
266
267         g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (account));  
268         g_return_if_fail (_singletons);
269
270         cache_mgr = modest_singletons_get_cache_mgr (_singletons);
271         send_queue_cache = modest_cache_mgr_get_cache (cache_mgr,
272                                                        MODEST_CACHE_MGR_CACHE_TYPE_SEND_QUEUE); 
273
274         if (g_hash_table_lookup (send_queue_cache, account))
275                 g_hash_table_remove (send_queue_cache, account);
276 }
277
278 ModestWindowMgr *
279 modest_runtime_get_window_mgr (void)
280 {
281         g_return_val_if_fail (_singletons, NULL);
282         return modest_singletons_get_window_mgr (_singletons);
283 }
284
285 ModestPluginFactory *
286 modest_runtime_get_plugin_factory (void)
287 {
288         g_return_val_if_fail (_singletons, NULL);
289         return modest_singletons_get_plugin_factory (_singletons);
290 }
291
292 ModestProtocolRegistry *
293 modest_runtime_get_protocol_registry (void)
294 {
295         g_return_val_if_fail (_singletons, NULL);
296         return modest_singletons_get_protocol_registry (_singletons);
297 }
298
299 /* http://primates.ximian.com/~federico/news-2006-04.html#memory-debugging-infrastructure*/
300 ModestRuntimeDebugFlags
301 modest_runtime_get_debug_flags ()
302 {
303         static const GDebugKey debug_keys[] = {
304                 { "abort-on-warning",   MODEST_RUNTIME_DEBUG_ABORT_ON_WARNING },
305                 { "log-actions",        MODEST_RUNTIME_DEBUG_LOG_ACTIONS },
306                 { "debug-objects",      MODEST_RUNTIME_DEBUG_OBJECTS },
307                 { "debug-signals",      MODEST_RUNTIME_DEBUG_SIGNALS },
308                 { "factory-settings",   MODEST_RUNTIME_DEBUG_FACTORY_SETTINGS},
309                 { "debug-code",         MODEST_RUNTIME_DEBUG_CODE}
310         };
311         const gchar *str;
312         static ModestRuntimeDebugFlags debug_flags = -1;
313
314         if (debug_flags != -1)
315                 return debug_flags;
316         
317         str = g_getenv (MODEST_DEBUG);
318         
319         if (str != NULL)
320                 debug_flags = g_parse_debug_string (str, debug_keys, G_N_ELEMENTS (debug_keys));
321         else
322                 debug_flags = 0;
323         
324         return debug_flags;
325 }
326
327 gboolean 
328 modest_runtime_get_allow_shutdown (void)
329 {
330         return _allow_shutdown;
331 }
332
333 void 
334 modest_runtime_set_allow_shutdown (gboolean allow)
335 {
336         _allow_shutdown = allow;
337 }