Fix for bug NB#74675. Now we limit the retrieval size for texts shown,
[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
50 static ModestSingletons       *_singletons    = NULL;
51
52 // we get the account store here instead of in Singletons
53 // as it leads to various chicken & problems with initialization
54 static ModestTnyAccountStore  *_account_store  = NULL;
55
56 /* Signal handlers for the send queues */
57 static GSList *_sig_handlers = NULL;
58
59 /*
60  * private functions declared in modest-runtime-priv.h -
61  * only to be called from modest-init.c 
62  */
63 /*-----------------------------------------------------------------------------*/
64 gboolean
65 modest_runtime_init (void)
66 {
67         if (_singletons) {
68                 g_printerr ("modest: modest_runtime_init can only be run once\n");
69                 return FALSE;
70         }
71         
72         _singletons = modest_singletons_new ();
73         if (!_singletons) {
74                 g_printerr ("modest: failed to create singletons\n");
75                 return FALSE;
76         }
77         
78         return TRUE;
79 }
80
81 gboolean
82 modest_runtime_uninit (void)
83 {
84         if (!_singletons)
85                 return TRUE;    /* uninit maybe called if runtime_init failed */
86         
87         g_return_val_if_fail (MODEST_IS_SINGLETONS(_singletons), FALSE);
88         modest_runtime_verify_object_last_ref(_singletons,"");
89         g_object_unref(G_OBJECT(_singletons));
90         _singletons = NULL;
91
92         if (_account_store) {
93                 modest_runtime_verify_object_last_ref(_account_store,"");
94                 g_object_unref(G_OBJECT(_account_store));
95                 _account_store = NULL;
96         }
97
98         
99         if (_sig_handlers) {
100                 modest_signal_mgr_disconnect_all_and_destroy (_sig_handlers);
101                 _sig_handlers = NULL;
102         }
103         
104         return TRUE;
105 }
106 /*-----------------------------------------------------------------------------*/
107         
108
109 ModestAccountMgr*
110 modest_runtime_get_account_mgr   (void)
111 {
112         g_return_val_if_fail (_singletons, NULL);
113         return modest_singletons_get_account_mgr (_singletons);
114 }
115
116 ModestEmailClipboard*
117 modest_runtime_get_email_clipboard   (void)
118 {
119         g_return_val_if_fail (_singletons, NULL);
120         return modest_singletons_get_email_clipboard (_singletons);
121 }
122
123 ModestTnyAccountStore*
124 modest_runtime_get_account_store   (void)
125 {
126         // we get the account store here instead of in Singletons
127         // as it leads to various chicken & problems with initialization
128         g_return_val_if_fail (_singletons, NULL);       
129         if (!_account_store) {
130                 _account_store  = modest_tny_account_store_new (modest_runtime_get_account_mgr(),
131                                                                 modest_runtime_get_device());
132                 if (!_account_store) {
133                         g_printerr ("modest: cannot create modest tny account store instance\n");
134                         return NULL;
135                 }
136         }
137         return _account_store;
138 }
139
140 ModestConf*
141 modest_runtime_get_conf (void)
142 {
143         g_return_val_if_fail (_singletons, NULL);
144         return modest_singletons_get_conf (_singletons);
145 }
146
147
148 ModestCacheMgr*
149 modest_runtime_get_cache_mgr (void)
150 {
151         g_return_val_if_fail (_singletons, NULL);
152         return modest_singletons_get_cache_mgr (_singletons);
153 }
154
155
156 ModestMailOperationQueue*
157 modest_runtime_get_mail_operation_queue (void)
158 {
159         g_return_val_if_fail (_singletons, NULL);
160         return modest_singletons_get_mail_operation_queue (_singletons);
161 }
162
163
164
165 TnyDevice*
166 modest_runtime_get_device (void)
167 {
168         g_return_val_if_fail (_singletons, NULL);
169         return modest_singletons_get_device (_singletons);
170 }
171
172
173 TnyPlatformFactory*
174 modest_runtime_get_platform_factory  (void)
175 {
176         g_return_val_if_fail (_singletons, NULL);
177         return modest_singletons_get_platform_factory (_singletons);
178 }
179
180 ModestTnySendQueue*
181 modest_runtime_get_send_queue  (TnyTransportAccount *account)
182 {
183         ModestCacheMgr *cache_mgr;
184         GHashTable     *send_queue_cache;
185         gpointer       orig_key, send_queue;
186         
187         g_return_val_if_fail (_singletons, NULL);
188         g_return_val_if_fail (TNY_IS_TRANSPORT_ACCOUNT(account), NULL);
189
190         cache_mgr = modest_singletons_get_cache_mgr (_singletons);
191         send_queue_cache = modest_cache_mgr_get_cache (cache_mgr,
192                                                        MODEST_CACHE_MGR_CACHE_TYPE_SEND_QUEUE);
193
194         /* Each modest account has its own send queue.
195          * Note that each modest account will have its own outbox folder, 
196          * returned by TnySendQueue::get_outbox_func().
197          */
198         if (!g_hash_table_lookup_extended (send_queue_cache, account, &orig_key, &send_queue)) {
199                 /* Note that this send queue will start sending messages from its outbox 
200                  * as soon as it is instantiated: */
201                 send_queue = (gpointer)modest_tny_send_queue_new (TNY_CAMEL_TRANSPORT_ACCOUNT(account));
202
203                 _sig_handlers = 
204                         modest_signal_mgr_connect (_sig_handlers, 
205                                                    send_queue, 
206                                                    "error_happened",
207                                                    G_CALLBACK (modest_ui_actions_on_send_queue_error_happened), 
208                                                    NULL);
209
210                 _sig_handlers = 
211                         modest_signal_mgr_connect (_sig_handlers, 
212                                                    send_queue, 
213                                                    "status_changed",
214                                                    G_CALLBACK (modest_ui_actions_on_send_queue_status_changed), 
215                                                    NULL);
216
217
218                 g_hash_table_insert (send_queue_cache, 
219                                      g_object_ref (account), 
220                                      g_object_ref (send_queue));
221         }
222
223         return MODEST_TNY_SEND_QUEUE(send_queue);
224 }
225
226 void modest_runtime_remove_all_send_queues ()
227 {
228         ModestCacheMgr *cache_mgr = modest_singletons_get_cache_mgr (_singletons);
229         
230         modest_cache_mgr_flush (cache_mgr, MODEST_CACHE_MGR_CACHE_TYPE_SEND_QUEUE);
231 }
232
233 ModestWindowMgr *
234 modest_runtime_get_window_mgr (void)
235 {
236         g_return_val_if_fail (_singletons, NULL);
237         return modest_singletons_get_window_mgr (_singletons);
238 }
239
240 /* http://primates.ximian.com/~federico/news-2006-04.html#memory-debugging-infrastructure*/
241 ModestRuntimeDebugFlags
242 modest_runtime_get_debug_flags ()
243 {
244         static const GDebugKey debug_keys[] = {
245                 { "abort-on-warning",   MODEST_RUNTIME_DEBUG_ABORT_ON_WARNING },
246                 { "log-actions",        MODEST_RUNTIME_DEBUG_LOG_ACTIONS },
247                 { "debug-objects",      MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS },
248                 { "debug-signals",      MODEST_RUNTIME_DEBUG_DEBUG_SIGNALS },
249                 { "factory-settings",   MODEST_RUNTIME_DEBUG_FACTORY_SETTINGS}
250         };
251         const gchar *str;
252         static ModestRuntimeDebugFlags debug_flags = -1;
253
254         if (debug_flags != -1)
255                 return debug_flags;
256         
257         str = g_getenv (MODEST_DEBUG);
258         
259         if (str != NULL)
260                 debug_flags = g_parse_debug_string (str, debug_keys, G_N_ELEMENTS (debug_keys));
261         else
262                 debug_flags = 0;
263         
264         return debug_flags;
265 }
266
267
268
269