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