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"
51 #ifdef MODEST_PLATFORM_MAEMO
52 #include <hildon/hildon-notification.h>
54 #include <libnotify/notify.h>
57 static gboolean init_header_columns (ModestConf *conf, gboolean overwrite);
58 static gboolean init_default_account_maybe (ModestAccountMgr *acc_mgr);
59 static void init_i18n (void);
60 static void init_stock_icons (void);
61 static void init_debug_g_type (void);
62 static void init_debug_logging (void);
63 static void init_default_settings (ModestConf *conf);
64 static void init_device_name (ModestConf *conf);
67 * defaults for the column headers
70 ModestHeaderViewColumn col;
75 static const FolderCols INBOX_COLUMNS_DETAILS[] = {
76 {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40, 0},
77 {MODEST_HEADER_VIEW_COLUMN_ATTACH, 40, 0},
78 {MODEST_HEADER_VIEW_COLUMN_FROM, 80, 0},
79 {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80, 0},
80 {MODEST_HEADER_VIEW_COLUMN_RECEIVED_DATE, 60, 0},
81 {MODEST_HEADER_VIEW_COLUMN_SIZE, 50, 0}
84 static const FolderCols INBOX_COLUMNS_TWOLINES[] = {
85 {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_IN, 180, 0},
88 static const FolderCols OUTBOX_COLUMNS_DETAILS[] = {
89 {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40, 0},
90 {MODEST_HEADER_VIEW_COLUMN_ATTACH, 40, 0},
91 {MODEST_HEADER_VIEW_COLUMN_TO, 80, 0},
92 {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80, 0},
93 {MODEST_HEADER_VIEW_COLUMN_SENT_DATE, 80, 0},
94 {MODEST_HEADER_VIEW_COLUMN_SIZE, 50, 0}
97 static const FolderCols OUTBOX_COLUMNS_TWOLINES[] = {
98 {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT,180, 0},
99 {MODEST_HEADER_VIEW_COLUMN_STATUS, 240, 0}
102 static const FolderCols SENT_COLUMNS_TWOLINES[] = {
103 {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT,180, 0},
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()) {
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()); */
199 /* Init stock icons */
202 /* Init notification system */
203 notify_init ("Basics");
210 modest_init_uninit (void)
212 if (!modest_runtime_uninit())
213 g_printerr ("modest: failed to uninit runtime\n");
221 /* NOTE: the exact details of this format are important, as they
222 * are also used in modest-widget-memory. FIXME: make a shared function
223 * for this with widget-memory
226 save_header_settings (ModestConf *conf, TnyFolderType type,
227 ModestHeaderViewStyle style, const FolderCols* cols,
228 guint col_num, gboolean overwrite)
234 g_return_val_if_fail (cols, FALSE);
236 key = _modest_widget_memory_get_keyname_with_double_type ("header-view",
238 MODEST_WIDGET_MEMORY_PARAM_COLUMN_WIDTH);
239 /* if we're not in overwrite mode, only write stuff it
240 * there was nothing before */
241 if (!overwrite && modest_conf_key_exists(conf, key, NULL)) {
246 /* the format is necessarily the same as the one in modest-widget-memory */
247 str = g_string_new (NULL);
248 for (i = 0; i != col_num; ++i)
249 g_string_append_printf (str, "%d:%d:%d ",
250 cols[i].col, cols[i].width, cols[i].sort);
252 modest_conf_set_string (conf, key, str->str, NULL);
254 g_string_free (str, TRUE);
260 * modest_init_header_columns:
261 * @overwrite: write the setting, even if it already exists
263 * will set defaults for the columns to show for folder,
264 * if there are no such settings yet (in ModestWidgetMemory)
266 * Returns: TRUE if succeeded, FALSE in case of error
269 init_header_columns (ModestConf *conf, gboolean overwrite)
273 for (folder_type = TNY_FOLDER_TYPE_UNKNOWN;
274 folder_type <= TNY_FOLDER_TYPE_CALENDAR; ++folder_type) {
276 switch (folder_type) {
277 case TNY_FOLDER_TYPE_SENT:
278 case TNY_FOLDER_TYPE_DRAFTS:
279 save_header_settings (conf, folder_type,
280 MODEST_HEADER_VIEW_STYLE_DETAILS,
281 OUTBOX_COLUMNS_DETAILS,
282 G_N_ELEMENTS(OUTBOX_COLUMNS_DETAILS),
284 save_header_settings (conf, folder_type,
285 MODEST_HEADER_VIEW_STYLE_TWOLINES,
286 SENT_COLUMNS_TWOLINES,
287 G_N_ELEMENTS(SENT_COLUMNS_TWOLINES),
290 case TNY_FOLDER_TYPE_OUTBOX:
291 save_header_settings (conf, folder_type,
292 MODEST_HEADER_VIEW_STYLE_TWOLINES,
293 OUTBOX_COLUMNS_TWOLINES,
294 G_N_ELEMENTS(OUTBOX_COLUMNS_TWOLINES),
299 save_header_settings (conf, folder_type,
300 MODEST_HEADER_VIEW_STYLE_DETAILS,
301 INBOX_COLUMNS_DETAILS,
302 G_N_ELEMENTS(INBOX_COLUMNS_DETAILS),
304 save_header_settings (conf, folder_type,
305 MODEST_HEADER_VIEW_STYLE_TWOLINES,
306 INBOX_COLUMNS_TWOLINES,
307 G_N_ELEMENTS(INBOX_COLUMNS_TWOLINES),
314 gboolean modest_init_one_local_folder (gchar *maildir_path)
316 static const gchar* maildirs[] = {
321 for (j = 0; j != G_N_ELEMENTS(maildirs); ++j) {
322 gchar *dir = g_build_filename (maildir_path,
325 if (g_mkdir_with_parents (dir, 0755) < 0) {
326 g_printerr ("modest: %s: failed to create %s\n", __FUNCTION__, dir);
338 * modest_init_local_folders:
340 * create the Local Folders folder under cache, if they
343 * Returns: TRUE if the folder were already there, or
344 * they were created, FALSE otherwise
347 modest_init_local_folders (const gchar* location_filepath)
349 gchar *maildir_path = modest_local_folder_info_get_maildir_path (location_filepath);
351 if (location_filepath) {
352 /* For instance, for memory card, just create the top-level .modest folder: */
353 if (g_mkdir_with_parents (maildir_path, 0755) < 0) {
354 g_printerr ("modest: %s: failed to create %s\n", __FUNCTION__, dir);
355 g_free (maildir_path);
360 /* Create each of the standard on-disk folders.
361 * Per-account outbox folders will be created when first needed. */
363 for (i = 0; i != G_N_ELEMENTS(LOCAL_FOLDERS); ++i) {
364 gchar *dir = g_build_filename (maildir_path,
365 modest_local_folder_info_get_type_name(LOCAL_FOLDERS[i]),
367 const gboolean created = modest_init_one_local_folder (dir);
371 g_free (maildir_path);
377 g_free (maildir_path);
382 * init_default_account_maybe:
384 * if there are accounts defined, but there is no default account,
385 * it will be defined.
387 * Returns: TRUE if there was a default account already,
388 * or one has been created or there are no accounts yet,
389 * returns FALSE in case of error
392 init_default_account_maybe (ModestAccountMgr *acc_mgr)
394 gchar *default_account;
395 gboolean retval = TRUE;
397 default_account = modest_account_mgr_get_default_account (acc_mgr);
398 if (!default_account)
399 retval = modest_account_mgr_set_first_account_as_default (acc_mgr);
400 g_free (default_account);
408 init_debug_g_type (void)
410 GTypeDebugFlags gflags;
411 ModestRuntimeDebugFlags mflags;
414 mflags = modest_runtime_get_debug_flags ();
416 if (mflags & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS)
417 gflags |= G_TYPE_DEBUG_OBJECTS;
418 if (mflags & MODEST_RUNTIME_DEBUG_DEBUG_SIGNALS)
419 gflags |= G_TYPE_DEBUG_SIGNALS;
421 g_type_init_with_debug_flags (gflags);
425 init_debug_logging (void)
427 ModestRuntimeDebugFlags mflags;
428 mflags = modest_runtime_get_debug_flags ();
430 if (mflags & MODEST_RUNTIME_DEBUG_ABORT_ON_WARNING)
431 g_log_set_always_fatal (G_LOG_LEVEL_ERROR |
432 G_LOG_LEVEL_CRITICAL |
433 G_LOG_LEVEL_WARNING);
440 const gchar* gettext_package;
441 /* Setup gettext, to use our .po files: */
442 /* GETTEXT_PACKAGE and MODEST_LOCALE_DIR are defined in config.h */
443 #ifdef MODEST_HILDON_VERSION_0
444 gettext_package = GETTEXT_PACKAGE;
445 bindtextdomain (gettext_package, MODEST_LOCALE_DIR);
447 gettext_package = "osso-email"; /* HACK to use the localizations */
448 bindtextdomain (gettext_package, "/usr/share/locale");
449 #endif /*MODEST_HILDON_VERSION_0*/
451 bind_textdomain_codeset (gettext_package, "UTF-8");
452 textdomain (gettext_package);
457 * This function registers our custom toolbar icons, so they can be
458 * themed. The idea of this function was taken from the gtk-demo
461 init_stock_icons (void)
463 static gboolean registered = FALSE;
466 GtkIconTheme *current_theme;
468 GtkIconFactory *factory;
471 static GtkStockItem items[] = {
472 #ifdef MODEST_PLATFORM_MAEMO
473 { MODEST_STOCK_SPLIT_VIEW, "split view", 0, 0, NULL },
474 { MODEST_STOCK_SORT, "sort mail", 0, 0, NULL },
475 { MODEST_STOCK_REFRESH, "refresh mail", 0, 0, NULL },
476 #endif /*MODEST_PLATFORM_MAEMO*/
477 { MODEST_STOCK_MAIL_SEND, "send mail", 0, 0, NULL },
478 { MODEST_STOCK_NEW_MAIL, "new mail", 0, 0, NULL },
479 /* { MODEST_STOCK_SEND_RECEIVE, "send receive", 0, 0, NULL }, */
480 { MODEST_STOCK_REPLY, "reply", 0, 0, NULL },
481 { MODEST_STOCK_REPLY_ALL, "reply all", 0, 0, NULL },
482 { MODEST_STOCK_FORWARD, "forward", 0, 0, NULL },
483 { MODEST_STOCK_DELETE, "delete", 0, 0, NULL },
484 /* { MODEST_STOCK_NEXT, "next", 0, 0, NULL }, */
485 /* { MODEST_STOCK_PREV, "prev", 0, 0, NULL }, */
486 /* { MODEST_STOCK_STOP, "stop", 0, 0, NULL } */
489 static gchar *items_names [] = {
490 #ifdef MODEST_PLATFORM_MAEMO
491 MODEST_TOOLBAR_ICON_SPLIT_VIEW,
492 MODEST_TOOLBAR_ICON_SORT,
493 MODEST_TOOLBAR_ICON_REFRESH,
494 #endif /*MODEST_PLATFORM_MAEMO*/
495 MODEST_TOOLBAR_ICON_MAIL_SEND,
496 MODEST_TOOLBAR_ICON_NEW_MAIL,
497 /* MODEST_TOOLBAR_ICON_SEND_RECEIVE, */
498 MODEST_TOOLBAR_ICON_REPLY,
499 MODEST_TOOLBAR_ICON_REPLY_ALL,
500 MODEST_TOOLBAR_ICON_FORWARD,
501 MODEST_TOOLBAR_ICON_DELETE,
502 /* MODEST_TOOLBAR_ICON_NEXT, */
503 /* MODEST_TOOLBAR_ICON_PREV, */
504 /* MODEST_TOOLBAR_ICON_STOP */
505 MODEST_TOOLBAR_ICON_FORMAT_BULLETS,
510 /* Register our stock items */
511 gtk_stock_add (items, G_N_ELEMENTS (items));
513 /* Add our custom icon factory to the list of defaults */
514 factory = gtk_icon_factory_new ();
515 gtk_icon_factory_add_default (factory);
517 current_theme = gtk_icon_theme_get_default ();
519 /* Register icons to accompany stock items */
520 for (i = 0; i < G_N_ELEMENTS (items); i++) {
522 #ifdef MODEST_PLATFORM_MAEMO /* MODES_PLATFORM_ID: 1 ==> gnome, 2==> maemo */
523 pixbuf = gtk_icon_theme_load_icon (current_theme,
526 GTK_ICON_LOOKUP_NO_SVG,
529 pixbuf = gdk_pixbuf_new_from_file (items_names[i], NULL);
532 if (pixbuf != NULL) {
533 GtkIconSet *icon_set;
534 GdkPixbuf *transparent;
536 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
537 icon_set = gtk_icon_set_new_from_pixbuf (transparent);
538 gtk_icon_factory_add (factory, items[i].stock_id, icon_set);
539 gtk_icon_set_unref (icon_set);
540 g_object_unref (pixbuf);
541 g_object_unref (transparent);
544 g_warning ("failed to load %s icon", items_names[i]);
546 /* Drop our reference to the factory, GTK will hold a reference. */
547 g_object_unref (factory);
553 init_default_settings (ModestConf *conf)
555 if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_TOOLBAR, NULL))
556 modest_conf_set_bool (conf, MODEST_CONF_SHOW_TOOLBAR, TRUE, NULL);
558 if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_TOOLBAR_FULLSCREEN, NULL))
559 modest_conf_set_bool (conf, MODEST_CONF_SHOW_TOOLBAR_FULLSCREEN, TRUE, NULL);
561 if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_CC, NULL))
562 modest_conf_set_bool (conf, MODEST_CONF_SHOW_CC, TRUE, NULL);
564 if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_BCC, NULL))
565 modest_conf_set_bool (conf, MODEST_CONF_SHOW_BCC, FALSE, NULL);
567 if (!modest_conf_key_exists (conf, MODEST_CONF_CONNECT_AT_STARTUP, NULL))
568 modest_conf_set_bool (conf, MODEST_CONF_CONNECT_AT_STARTUP, TRUE, NULL);
570 /* Global settings */
571 if (!modest_conf_key_exists (conf, MODEST_CONF_AUTO_UPDATE, NULL))
572 modest_conf_set_bool (conf, MODEST_CONF_AUTO_UPDATE, TRUE, NULL);
574 if (!modest_conf_key_exists (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, NULL))
575 modest_conf_set_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, MODEST_CONNECTED_VIA_WLAN, NULL);
577 if (!modest_conf_key_exists (conf, MODEST_CONF_UPDATE_INTERVAL, NULL))
578 modest_conf_set_int (conf, MODEST_CONF_UPDATE_INTERVAL, MODEST_UPDATE_INTERVAL_15_MIN, NULL);
580 if (!modest_conf_key_exists (conf, MODEST_CONF_MSG_SIZE_LIMIT, NULL))
581 modest_conf_set_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, 1000, NULL);
583 if (!modest_conf_key_exists (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, NULL))
584 modest_conf_set_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, FALSE, NULL);
586 if (!modest_conf_key_exists (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, NULL))
587 modest_conf_set_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, TRUE, NULL);
589 if (!modest_conf_key_exists (conf, MODEST_CONF_REPLY_TYPE, NULL))
590 modest_conf_set_int (conf, MODEST_CONF_REPLY_TYPE, MODEST_TNY_MSG_REPLY_TYPE_QUOTE, NULL);
594 /* set the device name -- note this is an initial guess from /etc/hostname
595 * on maemo-device it will most probably be replaced with the Bluetooth device
596 * name later during starting (see maemo/modest-maemo-utils.[ch])
599 init_device_name (ModestConf *conf)
601 int len = 255; /* max len */
602 gchar *devname = NULL;
604 if (!g_file_get_contents("/etc/hostname", &devname, &len, NULL) || len < 2 || len > 254) {
605 g_printerr ("modest: failed to read hostname\n");
606 modest_conf_set_string (conf, MODEST_CONF_DEVICE_NAME,
607 MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME,
610 /* remove the \n at the end */
611 if (devname[len-1] == '\n')
612 devname[len-1] = '\0';
617 if (!modest_conf_set_string (conf, MODEST_CONF_DEVICE_NAME,devname, &err)) {
618 g_printerr ("modest: error setting device name '%s': %s",
619 devname, err ? err->message: "?");