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-runtime-priv.h>
36 #include <modest-init.h>
37 #include <modest-defs.h>
38 #include <modest-singletons.h>
39 #include <widgets/modest-header-view.h>
40 #include <widgets/modest-folder-view.h>
41 #include <modest-tny-platform-factory.h>
42 #include <modest-platform.h>
43 #include <modest-widget-memory.h>
44 #include <modest-widget-memory-priv.h>
45 #include <modest-local-folder-info.h>
46 #include <modest-account-mgr.h>
47 #include <modest-account-mgr-helpers.h>
48 #include <modest-icon-names.h>
49 #include "widgets/modest-global-settings-dialog.h"
50 #include "modest-tny-msg.h"
52 static gboolean init_header_columns (ModestConf *conf, gboolean overwrite);
53 static gboolean init_default_account_maybe (ModestAccountMgr *acc_mgr);
54 static void init_i18n (void);
55 static void init_stock_icons (void);
56 static void init_debug_g_type (void);
57 static void init_debug_logging (void);
58 static void init_default_settings (ModestConf *conf);
59 static void init_device_name (ModestConf *conf);
62 * defaults for the column headers
65 ModestHeaderViewColumn col;
70 static const FolderCols INBOX_COLUMNS_DETAILS[] = {
71 {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40, 0},
72 {MODEST_HEADER_VIEW_COLUMN_ATTACH, 40, 0},
73 {MODEST_HEADER_VIEW_COLUMN_FROM, 80, 0},
74 {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80, 0},
75 {MODEST_HEADER_VIEW_COLUMN_RECEIVED_DATE, 60, 0},
76 {MODEST_HEADER_VIEW_COLUMN_SIZE, 50, 0}
79 static const FolderCols INBOX_COLUMNS_TWOLINES[] = {
80 {MODEST_HEADER_VIEW_COLUMN_COMPACT_FLAG, 40, 0},
81 {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_IN, 180, 0},
82 {MODEST_HEADER_VIEW_COLUMN_COMPACT_RECEIVED_DATE, 240, -1}
85 static const FolderCols OUTBOX_COLUMNS_DETAILS[] = {
86 {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40, 0},
87 {MODEST_HEADER_VIEW_COLUMN_ATTACH, 40, 0},
88 {MODEST_HEADER_VIEW_COLUMN_TO, 80, 0},
89 {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80, 0},
90 {MODEST_HEADER_VIEW_COLUMN_SENT_DATE, 80, 0},
91 {MODEST_HEADER_VIEW_COLUMN_SIZE, 50, 0}
94 static const FolderCols OUTBOX_COLUMNS_TWOLINES[] = {
95 {MODEST_HEADER_VIEW_COLUMN_COMPACT_FLAG, 40, 0},
96 {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT,180, 0},
97 {MODEST_HEADER_VIEW_COLUMN_STATUS, 240, 0}
100 static const FolderCols SENT_COLUMNS_TWOLINES[] = {
101 {MODEST_HEADER_VIEW_COLUMN_COMPACT_FLAG, 40, 0},
102 {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT,180, 0},
103 {MODEST_HEADER_VIEW_COLUMN_COMPACT_SENT_DATE, 240, -1}
106 #ifdef MODEST_PLATFORM_MAEMO
107 static const TnyFolderType LOCAL_FOLDERS[] = {
108 /* TNY_FOLDER_TYPE_OUTBOX, */
109 TNY_FOLDER_TYPE_DRAFTS,
113 static const TnyFolderType LOCAL_FOLDERS[] = {
114 /* TNY_FOLDER_TYPE_OUTBOX, */
115 TNY_FOLDER_TYPE_DRAFTS,
116 TNY_FOLDER_TYPE_SENT,
117 TNY_FOLDER_TYPE_TRASH,
118 TNY_FOLDER_TYPE_ARCHIVE
120 #endif /* MODEST_PLATFORM_MAEMO */
124 modest_init_init_core (void)
127 static gboolean invoked = FALSE;
130 g_printerr ("modest: modest_init_init_core may only be invoked once\n");
131 g_assert (!invoked); /* abort */
138 init_debug_logging();
140 if (!g_thread_supported())
145 if (!modest_runtime_init()) {
146 modest_init_uninit ();
147 g_printerr ("modest: failed to initialize the modest runtime\n");
152 /* do an initial guess for the device name */
153 init_device_name (modest_runtime_get_conf());
155 if (!modest_platform_init()) {
156 modest_init_uninit ();
157 g_printerr ("modest: failed to run platform-specific initialization\n");
161 /* based on the debug settings, we decide whether to overwrite old settings */
162 reset = modest_runtime_get_debug_flags () & MODEST_RUNTIME_DEBUG_FACTORY_SETTINGS;
163 if (!init_header_columns(modest_runtime_get_conf(), reset)) {
164 modest_init_uninit ();
165 g_printerr ("modest: failed to init header columns\n");
169 init_default_settings (modest_runtime_get_conf ());
171 if (!modest_init_local_folders(NULL /* means $HOME */)) {
172 modest_init_uninit ();
173 g_printerr ("modest: failed to init local folders\n");
177 if (!init_default_account_maybe(modest_runtime_get_account_mgr ())) {
178 modest_init_uninit ();
179 g_printerr ("modest: failed to init default account\n");
188 modest_init_init_ui (gint argc, gchar** argv)
190 if (!gtk_init_check(&argc, &argv)) {
191 g_printerr ("modest: failed to initialize graphical ui\n");
195 /* Set application name */
196 g_set_application_name (modest_platform_get_app_name());
197 /* g_message (modest_platform_get_app_name()); */
205 modest_init_uninit (void)
207 if (!modest_runtime_uninit())
208 g_printerr ("modest: failed to uninit runtime\n");
216 /* NOTE: the exact details of this format are important, as they
217 * are also used in modest-widget-memory. FIXME: make a shared function
218 * for this with widget-memory
221 save_header_settings (ModestConf *conf, TnyFolderType type,
222 ModestHeaderViewStyle style, const FolderCols* cols,
223 guint col_num, gboolean overwrite)
229 g_return_val_if_fail (cols, FALSE);
231 key = _modest_widget_memory_get_keyname_with_double_type ("header-view",
233 MODEST_WIDGET_MEMORY_PARAM_COLUMN_WIDTH);
234 /* if we're not in overwrite mode, only write stuff it
235 * there was nothing before */
236 if (!overwrite && modest_conf_key_exists(conf, key, NULL)) {
241 /* the format is necessarily the same as the one in modest-widget-memory */
242 str = g_string_new (NULL);
243 for (i = 0; i != col_num; ++i)
244 g_string_append_printf (str, "%d:%d:%d ",
245 cols[i].col, cols[i].width, cols[i].sort);
247 modest_conf_set_string (conf, key, str->str, NULL);
249 g_string_free (str, TRUE);
255 * modest_init_header_columns:
256 * @overwrite: write the setting, even if it already exists
258 * will set defaults for the columns to show for folder,
259 * if there are no such settings yet (in ModestWidgetMemory)
261 * Returns: TRUE if succeeded, FALSE in case of error
264 init_header_columns (ModestConf *conf, gboolean overwrite)
268 for (folder_type = TNY_FOLDER_TYPE_UNKNOWN;
269 folder_type <= TNY_FOLDER_TYPE_CALENDAR; ++folder_type) {
271 switch (folder_type) {
272 case TNY_FOLDER_TYPE_SENT:
273 case TNY_FOLDER_TYPE_DRAFTS:
274 save_header_settings (conf, folder_type,
275 MODEST_HEADER_VIEW_STYLE_DETAILS,
276 OUTBOX_COLUMNS_DETAILS,
277 G_N_ELEMENTS(OUTBOX_COLUMNS_DETAILS),
279 save_header_settings (conf, folder_type,
280 MODEST_HEADER_VIEW_STYLE_TWOLINES,
281 SENT_COLUMNS_TWOLINES,
282 G_N_ELEMENTS(SENT_COLUMNS_TWOLINES),
285 case TNY_FOLDER_TYPE_OUTBOX:
286 save_header_settings (conf, folder_type,
287 MODEST_HEADER_VIEW_STYLE_TWOLINES,
288 OUTBOX_COLUMNS_TWOLINES,
289 G_N_ELEMENTS(OUTBOX_COLUMNS_TWOLINES),
294 save_header_settings (conf, folder_type,
295 MODEST_HEADER_VIEW_STYLE_DETAILS,
296 INBOX_COLUMNS_DETAILS,
297 G_N_ELEMENTS(INBOX_COLUMNS_DETAILS),
299 save_header_settings (conf, folder_type,
300 MODEST_HEADER_VIEW_STYLE_TWOLINES,
301 INBOX_COLUMNS_TWOLINES,
302 G_N_ELEMENTS(INBOX_COLUMNS_TWOLINES),
309 gboolean modest_init_one_local_folder (gchar *maildir_path)
311 static const gchar* maildirs[] = {
316 for (j = 0; j != G_N_ELEMENTS(maildirs); ++j) {
317 gchar *dir = g_build_filename (maildir_path,
320 if (g_mkdir_with_parents (dir, 0755) < 0) {
321 g_printerr ("modest: failed to create %s\n", dir);
333 * modest_init_local_folders:
334 * @location_filepath: The location at which the local-folders directory should be created,
335 * or NULL to specify $HOME.
337 * create the Local Folders folder under cache, if they
340 * Returns: TRUE if the folder were already there, or
341 * they were created, FALSE otherwise
344 modest_init_local_folders (const gchar* location_filepath)
346 gchar *maildir_path = modest_local_folder_info_get_maildir_path (location_filepath);
348 /* Create each of the standard on-disk folders.
349 * Per-account outbox folders will be created when first needed. */
351 for (i = 0; i != G_N_ELEMENTS(LOCAL_FOLDERS); ++i) {
352 gchar *dir = g_build_filename (maildir_path,
353 modest_local_folder_info_get_type_name(LOCAL_FOLDERS[i]),
355 const gboolean created = modest_init_one_local_folder (dir);
359 g_free (maildir_path);
364 g_free (maildir_path);
371 free_element (gpointer data, gpointer user_data)
377 /* TODO: This is a duplicate of modest_account_mgr_set_first_account_as_default(). */
379 * init_default_account_maybe:
381 * if there are accounts defined, but there is no default account,
382 * it will be defined.
384 * Returns: TRUE if there was a default account already,
385 * or one has been created or there are no accounts yet,
386 * returns FALSE in case of error
389 init_default_account_maybe (ModestAccountMgr *acc_mgr)
391 GSList *all_accounts = NULL;
392 gchar *default_account;
393 gboolean retval = TRUE;
395 all_accounts = modest_account_mgr_account_names (acc_mgr, TRUE /* enabled accounts only */);
396 if (all_accounts) { /* if there are any accounts, there should be a default one */
398 modest_account_mgr_get_default_account (acc_mgr);
399 if (!default_account) {
400 gchar *first_account;
401 g_printerr ("modest: no default account defined\n");
402 first_account = (gchar*)all_accounts->data;
403 if ((retval = modest_account_mgr_set_default_account (acc_mgr, first_account)))
404 g_printerr ("modest: set '%s' as the default account\n",
407 g_printerr ("modest: failed to set '%s' as the default account\n",
409 g_free (default_account);
411 g_slist_foreach (all_accounts, free_element, NULL);
412 g_slist_free (all_accounts);
420 init_debug_g_type (void)
422 GTypeDebugFlags gflags;
423 ModestRuntimeDebugFlags mflags;
426 mflags = modest_runtime_get_debug_flags ();
428 if (mflags & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS)
429 gflags |= G_TYPE_DEBUG_OBJECTS;
430 if (mflags & MODEST_RUNTIME_DEBUG_DEBUG_SIGNALS)
431 gflags |= G_TYPE_DEBUG_SIGNALS;
433 g_type_init_with_debug_flags (gflags);
437 init_debug_logging (void)
439 ModestRuntimeDebugFlags mflags;
440 mflags = modest_runtime_get_debug_flags ();
442 if (mflags & MODEST_RUNTIME_DEBUG_ABORT_ON_WARNING)
443 g_log_set_always_fatal (G_LOG_LEVEL_ERROR |
444 G_LOG_LEVEL_CRITICAL |
445 G_LOG_LEVEL_WARNING);
452 const gchar* gettext_package;
453 /* Setup gettext, to use our .po files: */
454 /* GETTEXT_PACKAGE and MODEST_LOCALE_DIR are defined in config.h */
455 #ifdef MODEST_HILDON_VERSION_0
456 gettext_package = GETTEXT_PACKAGE;
457 bindtextdomain (gettext_package, MODEST_LOCALE_DIR);
459 gettext_package = "osso-email"; /* HACK to use the localizations */
460 bindtextdomain (gettext_package, "/usr/share/locale");
461 #endif /*MODEST_HILDON_VERSION_0*/
463 bind_textdomain_codeset (gettext_package, "UTF-8");
464 textdomain (gettext_package);
469 * This function registers our custom toolbar icons, so they can be
470 * themed. The idea of this function was taken from the gtk-demo
473 init_stock_icons (void)
475 static gboolean registered = FALSE;
478 GtkIconTheme *current_theme;
480 GtkIconFactory *factory;
483 static GtkStockItem items[] = {
484 #ifdef MODEST_PLATFORM_MAEMO
485 { MODEST_STOCK_SPLIT_VIEW, "split view", 0, 0, NULL },
486 { MODEST_STOCK_SORT, "sort mail", 0, 0, NULL },
487 { MODEST_STOCK_REFRESH, "refresh mail", 0, 0, NULL },
488 #endif /*MODEST_PLATFORM_MAEMO*/
489 { MODEST_STOCK_MAIL_SEND, "send mail", 0, 0, NULL },
490 { MODEST_STOCK_NEW_MAIL, "new mail", 0, 0, NULL },
491 /* { MODEST_STOCK_SEND_RECEIVE, "send receive", 0, 0, NULL }, */
492 { MODEST_STOCK_REPLY, "reply", 0, 0, NULL },
493 { MODEST_STOCK_REPLY_ALL, "reply all", 0, 0, NULL },
494 { MODEST_STOCK_FORWARD, "forward", 0, 0, NULL },
495 { MODEST_STOCK_DELETE, "delete", 0, 0, NULL },
496 /* { MODEST_STOCK_NEXT, "next", 0, 0, NULL }, */
497 /* { MODEST_STOCK_PREV, "prev", 0, 0, NULL }, */
498 /* { MODEST_STOCK_STOP, "stop", 0, 0, NULL } */
501 static gchar *items_names [] = {
502 #ifdef MODEST_PLATFORM_MAEMO
503 MODEST_TOOLBAR_ICON_SPLIT_VIEW,
504 MODEST_TOOLBAR_ICON_SORT,
505 MODEST_TOOLBAR_ICON_REFRESH,
506 #endif /*MODEST_PLATFORM_MAEMO*/
507 MODEST_TOOLBAR_ICON_MAIL_SEND,
508 MODEST_TOOLBAR_ICON_NEW_MAIL,
509 /* MODEST_TOOLBAR_ICON_SEND_RECEIVE, */
510 MODEST_TOOLBAR_ICON_REPLY,
511 MODEST_TOOLBAR_ICON_REPLY_ALL,
512 MODEST_TOOLBAR_ICON_FORWARD,
513 MODEST_TOOLBAR_ICON_DELETE,
514 /* MODEST_TOOLBAR_ICON_NEXT, */
515 /* MODEST_TOOLBAR_ICON_PREV, */
516 /* MODEST_TOOLBAR_ICON_STOP */
517 MODEST_TOOLBAR_ICON_FORMAT_BULLETS,
522 /* Register our stock items */
523 gtk_stock_add (items, G_N_ELEMENTS (items));
525 /* Add our custom icon factory to the list of defaults */
526 factory = gtk_icon_factory_new ();
527 gtk_icon_factory_add_default (factory);
529 current_theme = gtk_icon_theme_get_default ();
531 /* Register icons to accompany stock items */
532 for (i = 0; i < G_N_ELEMENTS (items); i++) {
534 #ifdef MODEST_PLATFORM_MAEMO /* MODES_PLATFORM_ID: 1 ==> gnome, 2==> maemo */
535 pixbuf = gtk_icon_theme_load_icon (current_theme,
538 GTK_ICON_LOOKUP_NO_SVG,
541 pixbuf = gdk_pixbuf_new_from_file (items_names[i], NULL);
544 if (pixbuf != NULL) {
545 GtkIconSet *icon_set;
546 GdkPixbuf *transparent;
548 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
549 icon_set = gtk_icon_set_new_from_pixbuf (transparent);
550 gtk_icon_factory_add (factory, items[i].stock_id, icon_set);
551 gtk_icon_set_unref (icon_set);
552 g_object_unref (pixbuf);
553 g_object_unref (transparent);
556 g_warning ("failed to load %s icon", items_names[i]);
558 /* Drop our reference to the factory, GTK will hold a reference. */
559 g_object_unref (factory);
565 init_default_settings (ModestConf *conf)
567 if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_TOOLBAR, NULL))
568 modest_conf_set_bool (conf, MODEST_CONF_SHOW_TOOLBAR, TRUE, NULL);
570 if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_TOOLBAR_FULLSCREEN, NULL))
571 modest_conf_set_bool (conf, MODEST_CONF_SHOW_TOOLBAR_FULLSCREEN, TRUE, NULL);
573 if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_CC, NULL))
574 modest_conf_set_bool (conf, MODEST_CONF_SHOW_CC, TRUE, NULL);
576 if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_BCC, NULL))
577 modest_conf_set_bool (conf, MODEST_CONF_SHOW_BCC, FALSE, NULL);
579 if (!modest_conf_key_exists (conf, MODEST_CONF_CONNECT_AT_STARTUP, NULL))
580 modest_conf_set_bool (conf, MODEST_CONF_CONNECT_AT_STARTUP, TRUE, NULL);
582 /* Global settings */
583 if (!modest_conf_key_exists (conf, MODEST_CONF_AUTO_UPDATE, NULL))
584 modest_conf_set_bool (conf, MODEST_CONF_AUTO_UPDATE, TRUE, NULL);
586 if (!modest_conf_key_exists (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, NULL))
587 modest_conf_set_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, MODEST_CONNECTED_VIA_WLAN, NULL);
589 if (!modest_conf_key_exists (conf, MODEST_CONF_UPDATE_INTERVAL, NULL))
590 modest_conf_set_int (conf, MODEST_CONF_UPDATE_INTERVAL, MODEST_UPDATE_INTERVAL_15_MIN, NULL);
592 if (!modest_conf_key_exists (conf, MODEST_CONF_MSG_SIZE_LIMIT, NULL))
593 modest_conf_set_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, 1000, NULL);
595 if (!modest_conf_key_exists (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, NULL))
596 modest_conf_set_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, FALSE, NULL);
598 if (!modest_conf_key_exists (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, NULL))
599 modest_conf_set_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, TRUE, NULL);
601 if (!modest_conf_key_exists (conf, MODEST_CONF_REPLY_TYPE, NULL))
602 modest_conf_set_int (conf, MODEST_CONF_REPLY_TYPE, MODEST_TNY_MSG_REPLY_TYPE_QUOTE, NULL);
606 /* set the device name -- note this is an initial guess from /etc/hostname
607 * on maemo-device it will most probably be replaced with the Bluetooth device
608 * name later during starting (see maemo/modest-maemo-utils.[ch])
611 init_device_name (ModestConf *conf)
613 int len = 255; /* max len */
614 gchar *devname = NULL;
616 if (!g_file_get_contents("/etc/hostname", &devname, &len, NULL) || len < 2 || len > 254) {
617 g_printerr ("modest: failed to read hostname\n");
618 modest_conf_set_string (conf, MODEST_CONF_DEVICE_NAME,
619 MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME,
622 /* remove the \n at the end */
623 if (devname[len-1] == '\n')
624 devname[len-1] = '\0';
629 if (!modest_conf_set_string (conf, MODEST_CONF_DEVICE_NAME,devname, &err)) {
630 g_printerr ("modest: error setting device name '%s': %s",
631 devname, err ? err->message: "?");