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