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