61750db271c5e1d481cd5112d32f3114e2ccdccd
[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-defs.h>
36 #include <modest-singletons.h>
37 #include <widgets/modest-header-view.h>
38 #include <widgets/modest-folder-view.h>
39 #include <modest-tny-platform-factory.h>
40 #include <modest-widget-memory.h>
41 #include <modest-widget-memory-priv.h>
42 #include <modest-local-folder-info.h>
43 #include <modest-account-mgr.h>
44 #include <modest-account-mgr-helpers.h>
45
46 #if MODEST_PLATFORM_ID==2 /* maemo/hildon */
47 #include <libosso.h>
48 static gboolean hildon_init (void);
49 #endif /* MODEST_PLATFORM_ID==2 */
50
51 static gboolean init_header_columns (ModestConf *conf, gboolean overwrite);
52 static gboolean init_local_folders  (void);
53 static gboolean init_default_account_maybe  (ModestAccountMgr *acc_mgr);
54 static void     init_i18n (void);
55 static void     debug_g_type_init (void);
56 static void     debug_logging_init (void);
57
58 static ModestSingletons *_singletons = NULL;
59
60 /*
61  * defaults for the column headers
62  */
63
64 typedef struct {
65         ModestHeaderViewColumn col;
66         guint                  width;
67 } FolderCols;
68
69 static const FolderCols INBOX_COLUMNS_DETAILS[] = {
70         {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
71         {MODEST_HEADER_VIEW_COLUMN_ATTACH,  40},
72         {MODEST_HEADER_VIEW_COLUMN_FROM,    80},
73         {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80},
74         {MODEST_HEADER_VIEW_COLUMN_RECEIVED_DATE, 60},
75         {MODEST_HEADER_VIEW_COLUMN_SIZE, 50}
76 };
77 static const FolderCols INBOX_COLUMNS_TWOLINES[] = {
78         {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
79         {MODEST_HEADER_VIEW_COLUMN_ATTACH,  40},
80         {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_IN, 200}
81 };
82
83 static const FolderCols OUTBOX_COLUMNS_DETAILS[] = {
84          {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
85          {MODEST_HEADER_VIEW_COLUMN_ATTACH,  40},
86          {MODEST_HEADER_VIEW_COLUMN_TO,    80},
87          {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80},
88          {MODEST_HEADER_VIEW_COLUMN_SENT_DATE, 80},
89          {MODEST_HEADER_VIEW_COLUMN_SIZE, 50}
90 };
91 static const FolderCols OUTBOX_COLUMNS_TWOLINES[] = {
92          {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
93          {MODEST_HEADER_VIEW_COLUMN_ATTACH,  40},
94          {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT,200},
95 };
96          
97 static const TnyFolderType LOCAL_FOLDERS[] = {
98         TNY_FOLDER_TYPE_OUTBOX,
99         TNY_FOLDER_TYPE_DRAFTS,
100         TNY_FOLDER_TYPE_SENT,
101         TNY_FOLDER_TYPE_TRASH,
102         TNY_FOLDER_TYPE_ARCHIVE 
103 };
104
105
106 gboolean
107 modest_runtime_init (void)
108 {
109         ModestSingletons *my_singletons;
110         gboolean reset;
111         
112         if (_singletons) {
113                 g_printerr ("modest: modest_runtime_init can only be called once\n");
114                 return FALSE;
115         }
116         
117         init_i18n();
118         debug_g_type_init();
119         debug_logging_init();
120
121         g_thread_init(NULL);
122         gdk_threads_init ();
123
124         my_singletons = modest_singletons_new ();
125         if (!my_singletons) {
126                 g_printerr ("modest: failed to initialize singletons\n");
127                 return FALSE;
128         }
129
130 #if MODEST_PLATFORM_ID==2 
131         if (!hildon_init ()) {
132                 modest_runtime_uninit ();
133                 g_printerr ("modest: failed to initialize hildon\n");
134                 return FALSE;
135         }
136 #endif /* MODEST_PLATFORM_ID==2 */
137
138         /* based on the debug settings, we decide whether to overwrite old settings */
139         reset = modest_runtime_get_debug_flags () & MODEST_RUNTIME_DEBUG_FACTORY_SETTINGS;
140         if (!init_header_columns(modest_singletons_get_conf (my_singletons), reset)) {
141                 modest_runtime_uninit ();
142                 g_printerr ("modest: failed to init header columns\n");
143                 return FALSE;
144         }
145
146         if (!init_local_folders()) {
147                 modest_runtime_uninit ();
148                 g_printerr ("modest: failed to init local folders\n");
149                 return FALSE;
150         }
151         
152         if (!init_default_account_maybe(modest_singletons_get_account_mgr (my_singletons))) {
153                 modest_runtime_uninit ();
154                 g_printerr ("modest: failed to init default account\n");
155                 return FALSE;
156         }
157
158         /* don't initialize _singletons before all the other init stuff
159          * is done; thus, using any of the singleton stuff before
160          * runtime is fully init'ed  is avoided
161          */
162         _singletons = my_singletons;
163         
164         return TRUE;
165 }
166
167
168 gboolean
169 modest_runtime_init_ui (gint argc, gchar** argv)
170 {
171         if (!gtk_init_check(&argc, &argv)) {
172                 g_printerr ("modest: failed to initialize graphical ui\n");
173                 return FALSE;
174         }
175         return TRUE;
176 }
177
178
179 gboolean
180 modest_runtime_uninit (void)
181 {
182         if (!_singletons) {
183                 g_printerr ("modest: modest_runtime is not initialized\n");
184                 return FALSE;
185         }
186         
187         g_object_unref (G_OBJECT(_singletons));
188         _singletons = NULL;
189
190         return TRUE;
191 }
192
193
194 ModestAccountMgr*
195 modest_runtime_get_account_mgr   (void)
196 {
197         g_return_val_if_fail (_singletons, NULL);
198         return modest_singletons_get_account_mgr (_singletons);
199 }
200
201 ModestTnyAccountStore*
202 modest_runtime_get_account_store   (void)
203 {
204         g_return_val_if_fail (_singletons, NULL);
205         return modest_singletons_get_account_store (_singletons);
206
207 }
208
209 ModestConf*
210 modest_runtime_get_conf (void)
211 {
212         g_return_val_if_fail (_singletons, NULL);
213         return modest_singletons_get_conf (_singletons);
214 }
215
216
217 ModestCacheMgr*
218 modest_runtime_get_cache_mgr (void)
219 {
220         g_return_val_if_fail (_singletons, NULL);
221         return modest_singletons_get_cache_mgr (_singletons);
222 }
223
224
225 ModestMailOperationQueue*
226 modest_runtime_get_mail_operation_queue (void)
227 {
228         g_return_val_if_fail (_singletons, NULL);
229         return modest_singletons_get_mail_operation_queue (_singletons);
230 }
231
232
233 ModestWidgetFactory*
234 modest_runtime_get_widget_factory     (void)
235 {
236         g_return_val_if_fail (_singletons, NULL);
237         return modest_singletons_get_widget_factory (_singletons);
238 }
239
240
241 ModestTnySendQueue*
242 modest_runtime_get_send_queue  (TnyTransportAccount *account)
243 {
244         ModestCacheMgr *cache_mgr;
245         GHashTable     *send_queue_cache;
246         gpointer       orig_key, send_queue;
247         
248         g_return_val_if_fail (_singletons, NULL);
249         g_return_val_if_fail (TNY_IS_TRANSPORT_ACCOUNT(account), NULL);
250
251         cache_mgr = modest_singletons_get_cache_mgr (_singletons);
252         send_queue_cache = modest_cache_mgr_get_cache (cache_mgr,
253                                                        MODEST_CACHE_MGR_CACHE_TYPE_SEND_QUEUE);
254
255         if (!g_hash_table_lookup_extended (send_queue_cache, account, &orig_key, &send_queue)) {
256                 send_queue = (gpointer)modest_tny_send_queue_new (TNY_CAMEL_TRANSPORT_ACCOUNT(account));
257                 g_hash_table_insert (send_queue_cache, account, send_queue);
258         }
259
260         return MODEST_TNY_SEND_QUEUE(send_queue);
261 }
262
263
264
265
266 /* http://primates.ximian.com/~federico/news-2006-04.html#memory-debugging-infrastructure*/
267 ModestRuntimeDebugFlags
268 modest_runtime_get_debug_flags ()
269 {
270         static const GDebugKey debug_keys[] = {
271                 { "abort-on-warning", MODEST_RUNTIME_DEBUG_ABORT_ON_WARNING },
272                 { "log-actions",      MODEST_RUNTIME_DEBUG_LOG_ACTIONS },
273                 { "debug-objects",    MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS },
274                 { "debug-signals",    MODEST_RUNTIME_DEBUG_DEBUG_SIGNALS },
275                 { "factory-settings", MODEST_RUNTIME_DEBUG_FACTORY_SETTINGS }
276         };
277         const gchar *str;
278         static ModestRuntimeDebugFlags debug_flags = -1;
279
280         if (debug_flags != -1)
281                 return debug_flags;
282         
283         str = g_getenv (MODEST_DEBUG);
284         
285         if (str != NULL)
286                 debug_flags = g_parse_debug_string (str, debug_keys, G_N_ELEMENTS (debug_keys));
287         else
288                 debug_flags = 0;
289         
290         return debug_flags;
291 }
292
293
294
295
296
297 /* NOTE: the exact details of this format are important, as they
298  * are also used in modest-widget-memory. FIXME: make a shared function
299  * for this with widget-memory
300  */
301 static gboolean
302 save_header_settings (ModestConf *conf, TnyFolderType type,
303                       ModestHeaderViewStyle style,  const FolderCols* cols,
304                       guint col_num, gboolean overwrite)
305 {
306         int i;
307         gchar *key;
308         GString *str;
309
310         g_return_val_if_fail (cols, FALSE);
311
312         key = _modest_widget_memory_get_keyname_with_double_type ("header-view",
313                                                                   type, style,
314                                                                   MODEST_WIDGET_MEMORY_PARAM_COLUMN_WIDTH);
315         /* if we're not in overwrite mode, only write stuff it
316          * there was nothing before */
317         if (!overwrite &&  modest_conf_key_exists(conf, key, NULL)) {
318                 g_free (key);
319                 return TRUE;
320         }
321
322         /* the format is necessarily the same as the one in modest-widget-memory */
323         str = g_string_new (NULL);
324         for (i = 0; i != col_num; ++i) 
325                 g_string_append_printf (str, "%d:%d ",
326                                         cols[i].col, cols[i].width); 
327
328         modest_conf_set_string (conf, key, str->str, NULL);
329         g_free (key);
330         g_string_free (str, TRUE);
331         
332         return TRUE;
333 }
334
335 /**
336  * modest_init_header_columns:
337  * @overwrite: write the setting, even if it already exists
338  * 
339  * will set defaults for the columns to show for folder,
340  * if there are no such settings yet (in ModestWidgetMemory)
341  * 
342  * Returns: TRUE if succeeded, FALSE in case of error
343  */
344 static gboolean
345 init_header_columns (ModestConf *conf, gboolean overwrite)
346 {
347         int folder_type;
348         
349         for (folder_type = TNY_FOLDER_TYPE_UNKNOWN;
350              folder_type <= TNY_FOLDER_TYPE_CALENDAR; ++folder_type) {          
351                 
352                 switch (folder_type) {
353                 case TNY_FOLDER_TYPE_OUTBOX:
354                 case TNY_FOLDER_TYPE_SENT:
355                 case TNY_FOLDER_TYPE_DRAFTS:
356                 save_header_settings (conf, folder_type,
357                                       MODEST_HEADER_VIEW_STYLE_DETAILS,
358                                       OUTBOX_COLUMNS_DETAILS,
359                                       G_N_ELEMENTS(OUTBOX_COLUMNS_DETAILS),
360                                       overwrite);
361                 save_header_settings (conf, folder_type,
362                                       MODEST_HEADER_VIEW_STYLE_TWOLINES,
363                                       OUTBOX_COLUMNS_TWOLINES,
364                                       G_N_ELEMENTS(OUTBOX_COLUMNS_TWOLINES),
365                                       overwrite);
366                 break;
367
368                 default:
369                 save_header_settings (conf, folder_type,
370                                       MODEST_HEADER_VIEW_STYLE_DETAILS,
371                                       INBOX_COLUMNS_DETAILS,
372                                       G_N_ELEMENTS(INBOX_COLUMNS_DETAILS),
373                                       overwrite);
374                 save_header_settings (conf, folder_type,
375                                       MODEST_HEADER_VIEW_STYLE_TWOLINES,
376                                       INBOX_COLUMNS_TWOLINES,
377                                       G_N_ELEMENTS(INBOX_COLUMNS_TWOLINES),
378                                       overwrite);
379                 };
380         }
381         return TRUE;
382 }
383
384 /**
385  * init_local_folders:
386  * 
387  * create the Local Folders folder under cache, if they
388  * do not exist yet.
389  * 
390  * Returns: TRUE if the folder were already there, or
391  * they were created, FALSE otherwise
392  */
393 static gboolean
394 init_local_folders  (void)
395 {
396         int i;
397         gchar *maildir_path;
398         static const gchar* maildirs[] = {
399                 "cur", "new", "tmp"
400         };
401         
402         maildir_path = modest_local_folder_info_get_maildir_path ();
403
404         for (i = 0; i != G_N_ELEMENTS(LOCAL_FOLDERS); ++i) {
405                 int j;
406                 for (j = 0; j != G_N_ELEMENTS(maildirs); ++j) {
407                         gchar *dir;
408                         dir = g_build_filename (maildir_path,
409                                                 modest_local_folder_info_get_type_name(LOCAL_FOLDERS[i]),
410                                                 maildirs[j],
411                                                 NULL);
412                         if (g_mkdir_with_parents (dir, 0755) < 0) {
413                                 g_printerr ("modest: failed to create %s\n", dir);
414                                 g_free (dir);
415                                 g_free (maildir_path);
416                                 return FALSE;
417                         }
418                         g_free(dir);
419                 }
420         }
421         
422         g_free (maildir_path);
423         return TRUE;
424 }
425
426
427
428 static void
429 free_element (gpointer data, gpointer user_data)
430 {
431         g_free (data);
432 }
433
434
435
436 /**
437  * init_default_account_maybe:
438  *
439  * if there are accounts defined, but there is no default account,
440  * it will be defined.
441  * 
442  * Returns: TRUE if there was a default account already,
443  *  or one has been created or there are no accounts yet,
444  *  returns FALSE in case of error
445  */
446 static gboolean
447 init_default_account_maybe  (ModestAccountMgr *acc_mgr)
448 {
449         GSList *all_accounts = NULL;
450         gchar *default_account;
451         gboolean retval = TRUE;
452         
453         all_accounts = modest_account_mgr_account_names (acc_mgr, NULL);
454         if (all_accounts) { /* if there are any accounts, there should be a default one */
455                 default_account = 
456                         modest_account_mgr_get_default_account (acc_mgr);
457                 if (!default_account) {
458                         gchar *first_account;
459                         g_printerr ("modest: no default account defined\n");
460                         first_account = (gchar*)all_accounts->data;
461                         if ((retval = modest_account_mgr_set_default_account (acc_mgr, first_account)))
462                                 g_printerr ("modest: set '%s' as the default account\n",
463                                             first_account);
464                         else
465                                 g_printerr ("modest: failed to set '%s' as the default account\n",
466                                             first_account);
467                         g_free (default_account);
468                 }
469                 g_slist_foreach (all_accounts, free_element, NULL);
470                 g_slist_free    (all_accounts);
471         }
472         return retval;
473 }
474
475
476
477 static void
478 debug_g_type_init (void)
479 {
480         GTypeDebugFlags gflags;
481         ModestRuntimeDebugFlags mflags;
482         
483         gflags = 0;
484         mflags = modest_runtime_get_debug_flags ();
485
486         if (mflags & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS)
487                 gflags |= G_TYPE_DEBUG_OBJECTS;
488         if (mflags & MODEST_RUNTIME_DEBUG_DEBUG_SIGNALS)
489                 gflags |= G_TYPE_DEBUG_SIGNALS;
490
491         g_type_init_with_debug_flags (gflags);
492 }
493
494 static void
495 debug_logging_init (void)
496 {
497         ModestRuntimeDebugFlags mflags;
498         mflags = modest_runtime_get_debug_flags ();
499         
500         if (mflags & MODEST_RUNTIME_DEBUG_ABORT_ON_WARNING)
501                 g_log_set_always_fatal (G_LOG_LEVEL_ERROR |
502                                         G_LOG_LEVEL_CRITICAL |
503                                         G_LOG_LEVEL_WARNING);
504 }
505
506
507 static void
508 init_i18n (void)
509 {
510         bindtextdomain (GETTEXT_PACKAGE, MODEST_LOCALEDIR);
511         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
512         textdomain (GETTEXT_PACKAGE);
513
514 }
515
516
517 #if MODEST_PLATFORM_ID==2 
518 static gboolean
519 hildon_init (void)
520 {
521         osso_context_t *osso_context =
522                 osso_initialize(PACKAGE, PACKAGE_VERSION,
523                                 TRUE, NULL);    
524         if (!osso_context) {
525                 g_printerr ("modest: failed to acquire osso context\n");
526                 return FALSE;
527         }
528 }
529 #endif /* MODEST_PLATFORM_ID==2 */