1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
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>
47 #if MODEST_PLATFORM_ID==2 /* maemo/hildon */
49 static gboolean init_hildon (void);
50 #endif /* MODEST_PLATFORM_ID==2 */
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);
60 static ModestSingletons *_singletons = NULL;
63 * defaults for the column headers
67 ModestHeaderViewColumn col;
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}
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}
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}
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},
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
109 modest_runtime_init (void)
111 ModestSingletons *my_singletons;
115 g_printerr ("modest: modest_runtime_init can only be called once\n");
121 init_debug_logging();
126 my_singletons = modest_singletons_new ();
127 if (!my_singletons) {
128 g_printerr ("modest: failed to initialize singletons\n");
132 #if MODEST_PLATFORM_ID==2
133 if (!init_hildon ()) {
134 modest_runtime_uninit ();
135 g_printerr ("modest: failed to initialize hildon\n");
138 #endif /* MODEST_PLATFORM_ID==2 */
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");
148 if (!init_local_folders()) {
149 modest_runtime_uninit ();
150 g_printerr ("modest: failed to init local folders\n");
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");
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
164 _singletons = my_singletons;
171 modest_runtime_init_ui (gint argc, gchar** argv)
173 if (!gtk_init_check(&argc, &argv)) {
174 g_printerr ("modest: failed to initialize graphical ui\n");
184 modest_runtime_uninit (void)
187 g_object_unref (G_OBJECT(_singletons));
190 if (G_IS_OBJECT(_singletons))
191 g_warning ("BUG: _singletons is still alive\n");
198 modest_runtime_get_account_mgr (void)
200 g_return_val_if_fail (_singletons, NULL);
201 return modest_singletons_get_account_mgr (_singletons);
204 ModestTnyAccountStore*
205 modest_runtime_get_account_store (void)
207 g_return_val_if_fail (_singletons, NULL);
208 return modest_singletons_get_account_store (_singletons);
213 modest_runtime_get_conf (void)
215 g_return_val_if_fail (_singletons, NULL);
216 return modest_singletons_get_conf (_singletons);
221 modest_runtime_get_cache_mgr (void)
223 g_return_val_if_fail (_singletons, NULL);
224 return modest_singletons_get_cache_mgr (_singletons);
228 ModestMailOperationQueue*
229 modest_runtime_get_mail_operation_queue (void)
231 g_return_val_if_fail (_singletons, NULL);
232 return modest_singletons_get_mail_operation_queue (_singletons);
238 modest_runtime_get_device (void)
240 g_return_val_if_fail (_singletons, NULL);
241 return modest_singletons_get_device (_singletons);
248 modest_runtime_get_send_queue (TnyTransportAccount *account)
250 ModestCacheMgr *cache_mgr;
251 GHashTable *send_queue_cache;
252 gpointer orig_key, send_queue;
254 g_return_val_if_fail (_singletons, NULL);
255 g_return_val_if_fail (TNY_IS_TRANSPORT_ACCOUNT(account), NULL);
257 cache_mgr = modest_singletons_get_cache_mgr (_singletons);
258 send_queue_cache = modest_cache_mgr_get_cache (cache_mgr,
259 MODEST_CACHE_MGR_CACHE_TYPE_SEND_QUEUE);
261 if (!g_hash_table_lookup_extended (send_queue_cache, account, &orig_key, &send_queue)) {
262 send_queue = (gpointer)modest_tny_send_queue_new (TNY_CAMEL_TRANSPORT_ACCOUNT(account));
263 g_hash_table_insert (send_queue_cache, account, send_queue);
266 return MODEST_TNY_SEND_QUEUE(send_queue);
272 /* http://primates.ximian.com/~federico/news-2006-04.html#memory-debugging-infrastructure*/
273 ModestRuntimeDebugFlags
274 modest_runtime_get_debug_flags ()
276 static const GDebugKey debug_keys[] = {
277 { "abort-on-warning", MODEST_RUNTIME_DEBUG_ABORT_ON_WARNING },
278 { "log-actions", MODEST_RUNTIME_DEBUG_LOG_ACTIONS },
279 { "debug-objects", MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS },
280 { "debug-signals", MODEST_RUNTIME_DEBUG_DEBUG_SIGNALS },
281 { "factory-settings", MODEST_RUNTIME_DEBUG_FACTORY_SETTINGS }
284 static ModestRuntimeDebugFlags debug_flags = -1;
286 if (debug_flags != -1)
289 str = g_getenv (MODEST_DEBUG);
292 debug_flags = g_parse_debug_string (str, debug_keys, G_N_ELEMENTS (debug_keys));
303 /* NOTE: the exact details of this format are important, as they
304 * are also used in modest-widget-memory. FIXME: make a shared function
305 * for this with widget-memory
308 save_header_settings (ModestConf *conf, TnyFolderType type,
309 ModestHeaderViewStyle style, const FolderCols* cols,
310 guint col_num, gboolean overwrite)
316 g_return_val_if_fail (cols, FALSE);
318 key = _modest_widget_memory_get_keyname_with_double_type ("header-view",
320 MODEST_WIDGET_MEMORY_PARAM_COLUMN_WIDTH);
321 /* if we're not in overwrite mode, only write stuff it
322 * there was nothing before */
323 if (!overwrite && modest_conf_key_exists(conf, key, NULL)) {
328 /* the format is necessarily the same as the one in modest-widget-memory */
329 str = g_string_new (NULL);
330 for (i = 0; i != col_num; ++i)
331 g_string_append_printf (str, "%d:%d ",
332 cols[i].col, cols[i].width);
334 modest_conf_set_string (conf, key, str->str, NULL);
336 g_string_free (str, TRUE);
342 * modest_init_header_columns:
343 * @overwrite: write the setting, even if it already exists
345 * will set defaults for the columns to show for folder,
346 * if there are no such settings yet (in ModestWidgetMemory)
348 * Returns: TRUE if succeeded, FALSE in case of error
351 init_header_columns (ModestConf *conf, gboolean overwrite)
355 for (folder_type = TNY_FOLDER_TYPE_UNKNOWN;
356 folder_type <= TNY_FOLDER_TYPE_CALENDAR; ++folder_type) {
358 switch (folder_type) {
359 case TNY_FOLDER_TYPE_OUTBOX:
360 case TNY_FOLDER_TYPE_SENT:
361 case TNY_FOLDER_TYPE_DRAFTS:
362 save_header_settings (conf, folder_type,
363 MODEST_HEADER_VIEW_STYLE_DETAILS,
364 OUTBOX_COLUMNS_DETAILS,
365 G_N_ELEMENTS(OUTBOX_COLUMNS_DETAILS),
367 save_header_settings (conf, folder_type,
368 MODEST_HEADER_VIEW_STYLE_TWOLINES,
369 OUTBOX_COLUMNS_TWOLINES,
370 G_N_ELEMENTS(OUTBOX_COLUMNS_TWOLINES),
375 save_header_settings (conf, folder_type,
376 MODEST_HEADER_VIEW_STYLE_DETAILS,
377 INBOX_COLUMNS_DETAILS,
378 G_N_ELEMENTS(INBOX_COLUMNS_DETAILS),
380 save_header_settings (conf, folder_type,
381 MODEST_HEADER_VIEW_STYLE_TWOLINES,
382 INBOX_COLUMNS_TWOLINES,
383 G_N_ELEMENTS(INBOX_COLUMNS_TWOLINES),
391 * init_local_folders:
393 * create the Local Folders folder under cache, if they
396 * Returns: TRUE if the folder were already there, or
397 * they were created, FALSE otherwise
400 init_local_folders (void)
404 static const gchar* maildirs[] = {
408 maildir_path = modest_local_folder_info_get_maildir_path ();
410 for (i = 0; i != G_N_ELEMENTS(LOCAL_FOLDERS); ++i) {
412 for (j = 0; j != G_N_ELEMENTS(maildirs); ++j) {
414 dir = g_build_filename (maildir_path,
415 modest_local_folder_info_get_type_name(LOCAL_FOLDERS[i]),
418 if (g_mkdir_with_parents (dir, 0755) < 0) {
419 g_printerr ("modest: failed to create %s\n", dir);
421 g_free (maildir_path);
428 g_free (maildir_path);
435 free_element (gpointer data, gpointer user_data)
443 * init_default_account_maybe:
445 * if there are accounts defined, but there is no default account,
446 * it will be defined.
448 * Returns: TRUE if there was a default account already,
449 * or one has been created or there are no accounts yet,
450 * returns FALSE in case of error
453 init_default_account_maybe (ModestAccountMgr *acc_mgr)
455 GSList *all_accounts = NULL;
456 gchar *default_account;
457 gboolean retval = TRUE;
459 all_accounts = modest_account_mgr_account_names (acc_mgr);
460 if (all_accounts) { /* if there are any accounts, there should be a default one */
462 modest_account_mgr_get_default_account (acc_mgr);
463 if (!default_account) {
464 gchar *first_account;
465 g_printerr ("modest: no default account defined\n");
466 first_account = (gchar*)all_accounts->data;
467 if ((retval = modest_account_mgr_set_default_account (acc_mgr, first_account)))
468 g_printerr ("modest: set '%s' as the default account\n",
471 g_printerr ("modest: failed to set '%s' as the default account\n",
473 g_free (default_account);
475 g_slist_foreach (all_accounts, free_element, NULL);
476 g_slist_free (all_accounts);
484 init_debug_g_type (void)
486 GTypeDebugFlags gflags;
487 ModestRuntimeDebugFlags mflags;
490 mflags = modest_runtime_get_debug_flags ();
492 if (mflags & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS)
493 gflags |= G_TYPE_DEBUG_OBJECTS;
494 if (mflags & MODEST_RUNTIME_DEBUG_DEBUG_SIGNALS)
495 gflags |= G_TYPE_DEBUG_SIGNALS;
497 g_type_init_with_debug_flags (gflags);
501 init_debug_logging (void)
503 ModestRuntimeDebugFlags mflags;
504 mflags = modest_runtime_get_debug_flags ();
506 if (mflags & MODEST_RUNTIME_DEBUG_ABORT_ON_WARNING)
507 g_log_set_always_fatal (G_LOG_LEVEL_ERROR |
508 G_LOG_LEVEL_CRITICAL |
509 G_LOG_LEVEL_WARNING);
516 bindtextdomain (GETTEXT_PACKAGE, MODEST_LOCALEDIR);
517 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
518 textdomain (GETTEXT_PACKAGE);
523 #if MODEST_PLATFORM_ID==2
527 osso_context_t *osso_context =
528 osso_initialize(PACKAGE, PACKAGE_VERSION,
531 g_printerr ("modest: failed to acquire osso context\n");
535 #endif /* MODEST_PLATFORM_ID==2 */
539 * This function registers our custom toolbar icons, so they can be
540 * themed. The idea of this function was taken from the gtk-demo
543 init_stock_icons (void)
545 static gboolean registered = FALSE;
549 GtkIconFactory *factory;
552 static GtkStockItem items[] = {
553 { MODEST_STOCK_MAIL_SEND, "send mail", 0, 0, NULL },
554 { MODEST_STOCK_NEW_MAIL, "new mail", 0, 0, NULL },
555 { MODEST_STOCK_SEND_RECEIVE, "send receive", 0, 0, NULL },
556 { MODEST_STOCK_REPLY, "reply", 0, 0, NULL },
557 { MODEST_STOCK_REPLY_ALL, "reply all", 0, 0, NULL },
558 { MODEST_STOCK_FORWARD, "forward", 0, 0, NULL },
559 { MODEST_STOCK_DELETE, "delete", 0, 0, NULL },
560 { MODEST_STOCK_NEXT, "next", 0, 0, NULL },
561 { MODEST_STOCK_PREV, "prev", 0, 0, NULL },
562 /* { MODEST_STOCK_STOP, "stop", 0, 0, NULL } */
565 static gchar *items_names [] = {
566 MODEST_TOOLBAR_ICON_MAIL_SEND,
567 MODEST_TOOLBAR_ICON_NEW_MAIL,
568 MODEST_TOOLBAR_ICON_SEND_RECEIVE,
569 MODEST_TOOLBAR_ICON_REPLY,
570 MODEST_TOOLBAR_ICON_REPLY_ALL,
571 MODEST_TOOLBAR_ICON_FORWARD,
572 MODEST_TOOLBAR_ICON_DELETE,
573 MODEST_TOOLBAR_ICON_NEXT,
574 MODEST_TOOLBAR_ICON_PREV,
575 /* MODEST_TOOLBAR_ICON_STOP */
580 /* Register our stock items */
581 gtk_stock_add (items, G_N_ELEMENTS (items));
583 /* Add our custom icon factory to the list of defaults */
584 factory = gtk_icon_factory_new ();
585 gtk_icon_factory_add_default (factory);
587 /* Register icons to accompany stock items */
588 for (i = 0; i < G_N_ELEMENTS (items); i++) {
590 pixbuf = gdk_pixbuf_new_from_file (items_names[i], NULL);
592 if (pixbuf != NULL) {
593 GtkIconSet *icon_set;
594 GdkPixbuf *transparent;
596 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
598 icon_set = gtk_icon_set_new_from_pixbuf (transparent);
599 gtk_icon_factory_add (factory, items[i].stock_id, icon_set);
600 gtk_icon_set_unref (icon_set);
601 g_object_unref (pixbuf);
602 g_object_unref (transparent);
605 g_warning ("failed to load %s icon", items_names[i]);
607 /* Drop our reference to the factory, GTK will hold a reference. */
608 g_object_unref (factory);