* Add a new singleton to be used as headers clipboard
[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
49 static ModestSingletons *_singletons = NULL;
50
51 /*
52  * private functions declared in modest-runtime-priv.h -
53  * only to be called from modest-init.c 
54  */
55 /*-----------------------------------------------------------------------------*/
56 gboolean
57 modest_runtime_init (void)
58 {
59         if (_singletons) {
60                 g_printerr ("modest: modest_runtime_init can only be run once\n");
61                 return FALSE;
62         }
63         
64         _singletons = modest_singletons_new ();
65         if (!_singletons) {
66                 g_printerr ("modest: failed to create singletons\n");
67                 return FALSE;
68         }
69
70         return TRUE;
71 }
72
73 gboolean
74 modest_runtime_uninit (void)
75 {
76         if (!_singletons)
77                 return TRUE;    /* uninit maybe called if runtime_init failed */
78         
79         g_return_val_if_fail (MODEST_IS_SINGLETONS(_singletons), FALSE);
80         modest_runtime_verify_object_last_ref(_singletons,"");
81         g_object_unref(G_OBJECT(_singletons));
82         _singletons = NULL;
83                 
84         return TRUE;
85 }
86 /*-----------------------------------------------------------------------------*/
87         
88
89 ModestAccountMgr*
90 modest_runtime_get_account_mgr   (void)
91 {
92         g_return_val_if_fail (_singletons, NULL);
93         return modest_singletons_get_account_mgr (_singletons);
94 }
95
96 ModestEmailClipboard*
97 modest_runtime_get_email_clipboard   (void)
98 {
99         g_return_val_if_fail (_singletons, NULL);
100         return modest_singletons_get_email_clipboard (_singletons);
101 }
102
103 ModestTnyAccountStore*
104 modest_runtime_get_account_store   (void)
105 {
106         g_return_val_if_fail (_singletons, NULL);
107         return modest_singletons_get_account_store (_singletons);
108
109 }
110
111 ModestConf*
112 modest_runtime_get_conf (void)
113 {
114         g_return_val_if_fail (_singletons, NULL);
115         return modest_singletons_get_conf (_singletons);
116 }
117
118
119 ModestCacheMgr*
120 modest_runtime_get_cache_mgr (void)
121 {
122         g_return_val_if_fail (_singletons, NULL);
123         return modest_singletons_get_cache_mgr (_singletons);
124 }
125
126
127 ModestMailOperationQueue*
128 modest_runtime_get_mail_operation_queue (void)
129 {
130         g_return_val_if_fail (_singletons, NULL);
131         return modest_singletons_get_mail_operation_queue (_singletons);
132 }
133
134
135
136 TnyDevice*
137 modest_runtime_get_device (void)
138 {
139         g_return_val_if_fail (_singletons, NULL);
140         return modest_singletons_get_device (_singletons);
141 }
142
143
144 TnyPlatformFactory*
145 modest_runtime_get_platform_factory  (void)
146 {
147         g_return_val_if_fail (_singletons, NULL);
148         return modest_singletons_get_platform_factory (_singletons);
149 }
150
151 ModestTnySendQueue*
152 modest_runtime_get_send_queue  (TnyTransportAccount *account)
153 {
154         ModestCacheMgr *cache_mgr;
155         GHashTable     *send_queue_cache;
156         gpointer       orig_key, send_queue;
157         
158         g_return_val_if_fail (_singletons, NULL);
159         g_return_val_if_fail (TNY_IS_TRANSPORT_ACCOUNT(account), NULL);
160
161         cache_mgr = modest_singletons_get_cache_mgr (_singletons);
162         send_queue_cache = modest_cache_mgr_get_cache (cache_mgr,
163                                                        MODEST_CACHE_MGR_CACHE_TYPE_SEND_QUEUE);
164
165         /* Each modest account has its own send queue.
166          * Note that each modest account will have its own outbox folder, 
167          * returned by TnySendQueue::get_outbox_func().
168          */
169         if (!g_hash_table_lookup_extended (send_queue_cache, account, &orig_key, &send_queue)) {
170                 /* Note that this send queue will start sending messages from its outbox 
171                  * as soon as it is instantiated: */
172                 send_queue = (gpointer)modest_tny_send_queue_new (TNY_CAMEL_TRANSPORT_ACCOUNT(account));
173
174                 g_hash_table_insert (send_queue_cache, account, send_queue);
175                 g_object_ref (send_queue);
176         }
177
178         return MODEST_TNY_SEND_QUEUE(send_queue);
179 }
180
181 void modest_runtime_remove_all_send_queues ()
182 {
183         ModestCacheMgr *cache_mgr = modest_singletons_get_cache_mgr (_singletons);
184         
185         modest_cache_mgr_flush (cache_mgr, MODEST_CACHE_MGR_CACHE_TYPE_SEND_QUEUE);
186 }
187
188 ModestWindowMgr *
189 modest_runtime_get_window_mgr (void)
190 {
191         g_return_val_if_fail (_singletons, NULL);
192         return modest_singletons_get_window_mgr (_singletons);
193 }
194
195 /* http://primates.ximian.com/~federico/news-2006-04.html#memory-debugging-infrastructure*/
196 ModestRuntimeDebugFlags
197 modest_runtime_get_debug_flags ()
198 {
199         static const GDebugKey debug_keys[] = {
200                 { "abort-on-warning",   MODEST_RUNTIME_DEBUG_ABORT_ON_WARNING },
201                 { "log-actions",        MODEST_RUNTIME_DEBUG_LOG_ACTIONS },
202                 { "debug-objects",      MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS },
203                 { "debug-signals",      MODEST_RUNTIME_DEBUG_DEBUG_SIGNALS },
204                 { "factory-settings",   MODEST_RUNTIME_DEBUG_FACTORY_SETTINGS}
205         };
206         const gchar *str;
207         static ModestRuntimeDebugFlags debug_flags = -1;
208
209         if (debug_flags != -1)
210                 return debug_flags;
211         
212         str = g_getenv (MODEST_DEBUG);
213         
214         if (str != NULL)
215                 debug_flags = g_parse_debug_string (str, debug_keys, G_N_ELEMENTS (debug_keys));
216         else
217                 debug_flags = 0;
218         
219         return debug_flags;
220 }
221
222
223
224