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-platform.h>
41 #include <modest-widget-memory.h>
42 #include <modest-widget-memory-priv.h>
43 #include <modest-local-folder-info.h>
44 #include <modest-account-mgr.h>
45 #include <modest-account-mgr-helpers.h>
46 #include <modest-icon-names.h>
48 static gboolean init_header_columns (ModestConf *conf, gboolean overwrite);
49 static gboolean init_local_folders (void);
50 static gboolean init_default_account_maybe (ModestAccountMgr *acc_mgr);
51 static void init_i18n (void);
52 static void init_stock_icons (void);
53 static void init_debug_g_type (void);
54 static void init_debug_logging (void);
55 static void init_test_accounts (ModestAccountMgr *acc_mgr);
56 static void init_default_settings (ModestConf *conf);
59 static ModestSingletons *_singletons = NULL;
63 * defaults for the column headers
66 ModestHeaderViewColumn col;
70 static const FolderCols INBOX_COLUMNS_DETAILS[] = {
71 {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
72 {MODEST_HEADER_VIEW_COLUMN_ATTACH, 40},
73 {MODEST_HEADER_VIEW_COLUMN_FROM, 80},
74 {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80},
75 {MODEST_HEADER_VIEW_COLUMN_RECEIVED_DATE, 60},
76 {MODEST_HEADER_VIEW_COLUMN_SIZE, 50}
78 static const FolderCols INBOX_COLUMNS_TWOLINES[] = {
79 {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
80 {MODEST_HEADER_VIEW_COLUMN_ATTACH, 40},
81 {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_IN, 200}
84 static const FolderCols OUTBOX_COLUMNS_DETAILS[] = {
85 {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
86 {MODEST_HEADER_VIEW_COLUMN_ATTACH, 40},
87 {MODEST_HEADER_VIEW_COLUMN_TO, 80},
88 {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80},
89 {MODEST_HEADER_VIEW_COLUMN_SENT_DATE, 80},
90 {MODEST_HEADER_VIEW_COLUMN_SIZE, 50}
92 static const FolderCols OUTBOX_COLUMNS_TWOLINES[] = {
93 {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
94 {MODEST_HEADER_VIEW_COLUMN_ATTACH, 40},
95 {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT,200},
98 #ifdef MODEST_PLATFORM_MAEMO
99 static const TnyFolderType LOCAL_FOLDERS[] = {
100 TNY_FOLDER_TYPE_OUTBOX,
101 TNY_FOLDER_TYPE_DRAFTS,
105 static const TnyFolderType LOCAL_FOLDERS[] = {
106 TNY_FOLDER_TYPE_OUTBOX,
107 TNY_FOLDER_TYPE_DRAFTS,
108 TNY_FOLDER_TYPE_SENT,
109 TNY_FOLDER_TYPE_TRASH,
110 TNY_FOLDER_TYPE_ARCHIVE
112 #endif /* MODEST_PLATFORM_MAEMO */
117 modest_runtime_init (void)
119 ModestSingletons *my_singletons;
123 g_printerr ("modest: modest_runtime_init can only be called once\n");
129 init_debug_logging();
134 my_singletons = modest_singletons_new ();
135 if (!my_singletons) {
136 g_printerr ("modest: failed to initialize singletons\n");
140 if (!modest_platform_init()) {
141 modest_runtime_uninit ();
142 g_printerr ("modest: failed to run platform-specific initialization\n");
146 /* based on the debug settings, we decide whether to overwrite old settings */
147 reset = modest_runtime_get_debug_flags () & MODEST_RUNTIME_DEBUG_FACTORY_SETTINGS;
148 if (!init_header_columns(modest_singletons_get_conf (my_singletons), reset)) {
149 modest_runtime_uninit ();
150 g_printerr ("modest: failed to init header columns\n");
154 init_default_settings (modest_singletons_get_conf (my_singletons));
156 if (!init_local_folders()) {
157 modest_runtime_uninit ();
158 g_printerr ("modest: failed to init local folders\n");
162 if (!init_default_account_maybe(modest_singletons_get_account_mgr (my_singletons))) {
163 modest_runtime_uninit ();
164 g_printerr ("modest: failed to init default account\n");
168 /* create the test accounts if the debug flag for that is set */
169 if (modest_runtime_get_debug_flags () & MODEST_RUNTIME_DEBUG_TINY_TEST_ACCOUNTS)
170 init_test_accounts (modest_singletons_get_account_mgr (my_singletons));
172 /* don't initialize _singletons before all the other init stuff
173 * is done; thus, using any of the singleton stuff before
174 * runtime is fully init'ed is avoided
176 _singletons = my_singletons;
183 modest_runtime_init_ui (gint argc, gchar** argv)
185 if (!gtk_init_check(&argc, &argv)) {
186 g_printerr ("modest: failed to initialize graphical ui\n");
196 modest_runtime_uninit (void)
199 g_object_unref (G_OBJECT(_singletons));
202 modest_runtime_verify_object_death (_singletons, "_singletons");
209 modest_runtime_get_account_mgr (void)
211 g_return_val_if_fail (_singletons, NULL);
212 return modest_singletons_get_account_mgr (_singletons);
215 ModestTnyAccountStore*
216 modest_runtime_get_account_store (void)
218 g_return_val_if_fail (_singletons, NULL);
219 return modest_singletons_get_account_store (_singletons);
224 modest_runtime_get_conf (void)
226 g_return_val_if_fail (_singletons, NULL);
227 return modest_singletons_get_conf (_singletons);
232 modest_runtime_get_cache_mgr (void)
234 g_return_val_if_fail (_singletons, NULL);
235 return modest_singletons_get_cache_mgr (_singletons);
239 ModestMailOperationQueue*
240 modest_runtime_get_mail_operation_queue (void)
242 g_return_val_if_fail (_singletons, NULL);
243 return modest_singletons_get_mail_operation_queue (_singletons);
249 modest_runtime_get_device (void)
251 g_return_val_if_fail (_singletons, NULL);
252 return modest_singletons_get_device (_singletons);
257 modest_runtime_get_platform_factory (void)
259 g_return_val_if_fail (_singletons, NULL);
260 return modest_singletons_get_platform_factory (_singletons);
267 modest_runtime_get_send_queue (TnyTransportAccount *account)
269 ModestCacheMgr *cache_mgr;
270 GHashTable *send_queue_cache;
271 gpointer orig_key, send_queue;
273 g_return_val_if_fail (_singletons, NULL);
274 g_return_val_if_fail (TNY_IS_TRANSPORT_ACCOUNT(account), NULL);
276 cache_mgr = modest_singletons_get_cache_mgr (_singletons);
277 send_queue_cache = modest_cache_mgr_get_cache (cache_mgr,
278 MODEST_CACHE_MGR_CACHE_TYPE_SEND_QUEUE);
280 if (!g_hash_table_lookup_extended (send_queue_cache, account, &orig_key, &send_queue)) {
281 send_queue = (gpointer)modest_tny_send_queue_new (TNY_CAMEL_TRANSPORT_ACCOUNT(account));
282 g_hash_table_insert (send_queue_cache, account, send_queue);
285 return MODEST_TNY_SEND_QUEUE(send_queue);
291 /* http://primates.ximian.com/~federico/news-2006-04.html#memory-debugging-infrastructure*/
292 ModestRuntimeDebugFlags
293 modest_runtime_get_debug_flags ()
295 static const GDebugKey debug_keys[] = {
296 { "abort-on-warning", MODEST_RUNTIME_DEBUG_ABORT_ON_WARNING },
297 { "log-actions", MODEST_RUNTIME_DEBUG_LOG_ACTIONS },
298 { "debug-objects", MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS },
299 { "debug-signals", MODEST_RUNTIME_DEBUG_DEBUG_SIGNALS },
300 { "factory-settings", MODEST_RUNTIME_DEBUG_FACTORY_SETTINGS},
301 { "tiny-test-accounts", MODEST_RUNTIME_DEBUG_TINY_TEST_ACCOUNTS}
304 static ModestRuntimeDebugFlags debug_flags = -1;
306 if (debug_flags != -1)
309 str = g_getenv (MODEST_DEBUG);
312 debug_flags = g_parse_debug_string (str, debug_keys, G_N_ELEMENTS (debug_keys));
323 /* NOTE: the exact details of this format are important, as they
324 * are also used in modest-widget-memory. FIXME: make a shared function
325 * for this with widget-memory
328 save_header_settings (ModestConf *conf, TnyFolderType type,
329 ModestHeaderViewStyle style, const FolderCols* cols,
330 guint col_num, gboolean overwrite)
336 g_return_val_if_fail (cols, FALSE);
338 key = _modest_widget_memory_get_keyname_with_double_type ("header-view",
340 MODEST_WIDGET_MEMORY_PARAM_COLUMN_WIDTH);
341 /* if we're not in overwrite mode, only write stuff it
342 * there was nothing before */
343 if (!overwrite && modest_conf_key_exists(conf, key, NULL)) {
348 /* the format is necessarily the same as the one in modest-widget-memory */
349 str = g_string_new (NULL);
350 for (i = 0; i != col_num; ++i)
351 g_string_append_printf (str, "%d:%d ",
352 cols[i].col, cols[i].width);
354 modest_conf_set_string (conf, key, str->str, NULL);
356 g_string_free (str, TRUE);
362 * modest_init_header_columns:
363 * @overwrite: write the setting, even if it already exists
365 * will set defaults for the columns to show for folder,
366 * if there are no such settings yet (in ModestWidgetMemory)
368 * Returns: TRUE if succeeded, FALSE in case of error
371 init_header_columns (ModestConf *conf, gboolean overwrite)
375 for (folder_type = TNY_FOLDER_TYPE_UNKNOWN;
376 folder_type <= TNY_FOLDER_TYPE_CALENDAR; ++folder_type) {
378 switch (folder_type) {
379 case TNY_FOLDER_TYPE_OUTBOX:
380 case TNY_FOLDER_TYPE_SENT:
381 case TNY_FOLDER_TYPE_DRAFTS:
382 save_header_settings (conf, folder_type,
383 MODEST_HEADER_VIEW_STYLE_DETAILS,
384 OUTBOX_COLUMNS_DETAILS,
385 G_N_ELEMENTS(OUTBOX_COLUMNS_DETAILS),
387 save_header_settings (conf, folder_type,
388 MODEST_HEADER_VIEW_STYLE_TWOLINES,
389 OUTBOX_COLUMNS_TWOLINES,
390 G_N_ELEMENTS(OUTBOX_COLUMNS_TWOLINES),
395 save_header_settings (conf, folder_type,
396 MODEST_HEADER_VIEW_STYLE_DETAILS,
397 INBOX_COLUMNS_DETAILS,
398 G_N_ELEMENTS(INBOX_COLUMNS_DETAILS),
400 save_header_settings (conf, folder_type,
401 MODEST_HEADER_VIEW_STYLE_TWOLINES,
402 INBOX_COLUMNS_TWOLINES,
403 G_N_ELEMENTS(INBOX_COLUMNS_TWOLINES),
411 * init_local_folders:
413 * create the Local Folders folder under cache, if they
416 * Returns: TRUE if the folder were already there, or
417 * they were created, FALSE otherwise
420 init_local_folders (void)
424 static const gchar* maildirs[] = {
428 maildir_path = modest_local_folder_info_get_maildir_path ();
430 for (i = 0; i != G_N_ELEMENTS(LOCAL_FOLDERS); ++i) {
432 for (j = 0; j != G_N_ELEMENTS(maildirs); ++j) {
434 dir = g_build_filename (maildir_path,
435 modest_local_folder_info_get_type_name(LOCAL_FOLDERS[i]),
438 if (g_mkdir_with_parents (dir, 0755) < 0) {
439 g_printerr ("modest: failed to create %s\n", dir);
441 g_free (maildir_path);
448 g_free (maildir_path);
455 free_element (gpointer data, gpointer user_data)
463 * init_default_account_maybe:
465 * if there are accounts defined, but there is no default account,
466 * it will be defined.
468 * Returns: TRUE if there was a default account already,
469 * or one has been created or there are no accounts yet,
470 * returns FALSE in case of error
473 init_default_account_maybe (ModestAccountMgr *acc_mgr)
475 GSList *all_accounts = NULL;
476 gchar *default_account;
477 gboolean retval = TRUE;
479 all_accounts = modest_account_mgr_account_names (acc_mgr);
480 if (all_accounts) { /* if there are any accounts, there should be a default one */
482 modest_account_mgr_get_default_account (acc_mgr);
483 if (!default_account) {
484 gchar *first_account;
485 g_printerr ("modest: no default account defined\n");
486 first_account = (gchar*)all_accounts->data;
487 if ((retval = modest_account_mgr_set_default_account (acc_mgr, first_account)))
488 g_printerr ("modest: set '%s' as the default account\n",
491 g_printerr ("modest: failed to set '%s' as the default account\n",
493 g_free (default_account);
495 g_slist_foreach (all_accounts, free_element, NULL);
496 g_slist_free (all_accounts);
504 init_debug_g_type (void)
506 GTypeDebugFlags gflags;
507 ModestRuntimeDebugFlags mflags;
510 mflags = modest_runtime_get_debug_flags ();
512 if (mflags & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS)
513 gflags |= G_TYPE_DEBUG_OBJECTS;
514 if (mflags & MODEST_RUNTIME_DEBUG_DEBUG_SIGNALS)
515 gflags |= G_TYPE_DEBUG_SIGNALS;
517 g_type_init_with_debug_flags (gflags);
521 init_debug_logging (void)
523 ModestRuntimeDebugFlags mflags;
524 mflags = modest_runtime_get_debug_flags ();
526 if (mflags & MODEST_RUNTIME_DEBUG_ABORT_ON_WARNING)
527 g_log_set_always_fatal (G_LOG_LEVEL_ERROR |
528 G_LOG_LEVEL_CRITICAL |
529 G_LOG_LEVEL_WARNING);
536 /* Setup gettext, to use our .po files: */
537 /* GETTEXT_PACKAGE and MODEST_LOCALE_DIR are defined in config.h */
538 bindtextdomain (GETTEXT_PACKAGE, MODEST_LOCALE_DIR);
539 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
540 textdomain (GETTEXT_PACKAGE);
545 * This function registers our custom toolbar icons, so they can be
546 * themed. The idea of this function was taken from the gtk-demo
549 init_stock_icons (void)
551 static gboolean registered = FALSE;
554 GtkIconTheme *current_theme;
556 GtkIconFactory *factory;
559 static GtkStockItem items[] = {
560 #if MODEST_PLATFORM_ID==2
561 { MODEST_STOCK_SPLIT_VIEW, "split view", 0, 0, NULL },
562 { MODEST_STOCK_SORT, "sort mail", 0, 0, NULL },
563 { MODEST_STOCK_REFRESH, "refresh mail", 0, 0, NULL },
565 { MODEST_STOCK_MAIL_SEND, "send mail", 0, 0, NULL },
566 { MODEST_STOCK_NEW_MAIL, "new mail", 0, 0, NULL },
567 /* { MODEST_STOCK_SEND_RECEIVE, "send receive", 0, 0, NULL }, */
568 { MODEST_STOCK_REPLY, "reply", 0, 0, NULL },
569 { MODEST_STOCK_REPLY_ALL, "reply all", 0, 0, NULL },
570 { MODEST_STOCK_FORWARD, "forward", 0, 0, NULL },
571 { MODEST_STOCK_DELETE, "delete", 0, 0, NULL },
572 /* { MODEST_STOCK_NEXT, "next", 0, 0, NULL }, */
573 /* { MODEST_STOCK_PREV, "prev", 0, 0, NULL }, */
574 /* { MODEST_STOCK_STOP, "stop", 0, 0, NULL } */
577 static gchar *items_names [] = {
578 #if MODEST_PLATFORM_ID==2
579 MODEST_TOOLBAR_ICON_SPLIT_VIEW,
580 MODEST_TOOLBAR_ICON_SORT,
581 MODEST_TOOLBAR_ICON_REFRESH,
583 MODEST_TOOLBAR_ICON_MAIL_SEND,
584 MODEST_TOOLBAR_ICON_NEW_MAIL,
585 /* MODEST_TOOLBAR_ICON_SEND_RECEIVE, */
586 MODEST_TOOLBAR_ICON_REPLY,
587 MODEST_TOOLBAR_ICON_REPLY_ALL,
588 MODEST_TOOLBAR_ICON_FORWARD,
589 MODEST_TOOLBAR_ICON_DELETE,
590 /* MODEST_TOOLBAR_ICON_NEXT, */
591 /* MODEST_TOOLBAR_ICON_PREV, */
592 /* MODEST_TOOLBAR_ICON_STOP */
593 MODEST_TOOLBAR_ICON_FORMAT_BULLETS,
598 /* Register our stock items */
599 gtk_stock_add (items, G_N_ELEMENTS (items));
601 /* Add our custom icon factory to the list of defaults */
602 factory = gtk_icon_factory_new ();
603 gtk_icon_factory_add_default (factory);
605 current_theme = gtk_icon_theme_get_default ();
607 /* Register icons to accompany stock items */
608 for (i = 0; i < G_N_ELEMENTS (items); i++) {
610 #if MODEST_PLATFORM_ID==1 /* MODES_PLATFORM_ID: 1 ==> gnome, 2==> maemo */
611 pixbuf = gdk_pixbuf_new_from_file (items_names[i], NULL);
613 pixbuf = gtk_icon_theme_load_icon (current_theme,
616 GTK_ICON_LOOKUP_NO_SVG,
619 if (pixbuf != NULL) {
620 GtkIconSet *icon_set;
621 GdkPixbuf *transparent;
623 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
624 icon_set = gtk_icon_set_new_from_pixbuf (transparent);
625 gtk_icon_factory_add (factory, items[i].stock_id, icon_set);
626 gtk_icon_set_unref (icon_set);
627 g_object_unref (pixbuf);
628 g_object_unref (transparent);
631 g_warning ("failed to load %s icon", items_names[i]);
633 /* Drop our reference to the factory, GTK will hold a reference. */
634 g_object_unref (factory);
640 init_test_accounts (ModestAccountMgr *acc_mgr)
642 const gchar *imap_test = "TnyImapTest";
643 const gchar *pop_test = "TnyPopTest";
644 const gchar *smtp_test = "TnySmtpTest";
646 if (!modest_account_mgr_account_exists (acc_mgr, imap_test, FALSE))
647 modest_account_mgr_add_server_account (acc_mgr,
649 "imap2.tinymail.org",
650 "tnytest", "tnytest",
651 MODEST_PROTOCOL_STORE_IMAP,
652 MODEST_PROTOCOL_SECURITY_NONE,
653 MODEST_PROTOCOL_AUTH_NONE);
654 if (!modest_account_mgr_account_exists (acc_mgr, pop_test, FALSE))
655 modest_account_mgr_add_server_account (acc_mgr,
657 "imap2.tinymail.org",
658 "tnytest", "tnytest",
659 MODEST_PROTOCOL_STORE_POP,
660 MODEST_PROTOCOL_SECURITY_NONE,
661 MODEST_PROTOCOL_AUTH_NONE);
662 if (!modest_account_mgr_account_exists (acc_mgr, smtp_test, FALSE))
663 modest_account_mgr_add_server_account (acc_mgr,
667 MODEST_PROTOCOL_TRANSPORT_SMTP,
668 MODEST_PROTOCOL_SECURITY_NONE,
669 MODEST_PROTOCOL_AUTH_NONE);
671 modest_account_mgr_add_account (acc_mgr, imap_test, imap_test, smtp_test);
672 modest_account_mgr_add_account (acc_mgr, pop_test, pop_test, smtp_test);
677 init_default_settings (ModestConf *conf)
679 if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_TOOLBAR, NULL))
680 modest_conf_set_bool (conf, MODEST_CONF_SHOW_TOOLBAR, TRUE, NULL);
682 if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_CC, NULL))
683 modest_conf_set_bool (conf, MODEST_CONF_SHOW_CC, TRUE, NULL);
685 if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_BCC, NULL))
686 modest_conf_set_bool (conf, MODEST_CONF_SHOW_BCC, FALSE, NULL);
688 if (!modest_conf_key_exists (conf, MODEST_CONF_CONNECT_AT_STARTUP, NULL))
689 modest_conf_set_bool (conf, MODEST_CONF_CONNECT_AT_STARTUP, TRUE, NULL);