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>
50 static gboolean init_header_columns (ModestConf *conf, gboolean overwrite);
51 static gboolean init_local_folders (void);
52 static gboolean init_default_account_maybe (ModestAccountMgr *acc_mgr);
53 static void init_i18n (void);
54 static void init_stock_icons (void);
55 static void init_debug_g_type (void);
56 static void init_debug_logging (void);
57 static void init_default_settings (ModestConf *conf);
58 static void init_device_name (ModestConf *conf);
61 * defaults for the column headers
64 ModestHeaderViewColumn col;
69 static const FolderCols INBOX_COLUMNS_DETAILS[] = {
70 {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40, 0},
71 {MODEST_HEADER_VIEW_COLUMN_ATTACH, 40, 0},
72 {MODEST_HEADER_VIEW_COLUMN_FROM, 80, 0},
73 {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80, 0},
74 {MODEST_HEADER_VIEW_COLUMN_RECEIVED_DATE, 60, 0},
75 {MODEST_HEADER_VIEW_COLUMN_SIZE, 50, 0}
78 static const FolderCols INBOX_COLUMNS_TWOLINES[] = {
79 {MODEST_HEADER_VIEW_COLUMN_COMPACT_FLAG, 40, 0},
80 {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_IN, 180, 0},
81 {MODEST_HEADER_VIEW_COLUMN_COMPACT_RECEIVED_DATE, 240, 1}
84 static const FolderCols OUTBOX_COLUMNS_DETAILS[] = {
85 {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40, 0},
86 {MODEST_HEADER_VIEW_COLUMN_ATTACH, 40, 0},
87 {MODEST_HEADER_VIEW_COLUMN_TO, 80, 0},
88 {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80, 0},
89 {MODEST_HEADER_VIEW_COLUMN_SENT_DATE, 80, 0},
90 {MODEST_HEADER_VIEW_COLUMN_SIZE, 50, 0}
93 static const FolderCols OUTBOX_COLUMNS_TWOLINES[] = {
94 {MODEST_HEADER_VIEW_COLUMN_COMPACT_FLAG, 40, 0},
95 {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT,180, 0},
96 {MODEST_HEADER_VIEW_COLUMN_STATUS, 240, 0}
99 static const FolderCols SENT_COLUMNS_TWOLINES[] = {
100 {MODEST_HEADER_VIEW_COLUMN_COMPACT_FLAG, 40, 0},
101 {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT,180, 0},
102 {MODEST_HEADER_VIEW_COLUMN_COMPACT_SENT_DATE, 240, 1}
105 #ifdef MODEST_PLATFORM_MAEMO
106 static const TnyFolderType LOCAL_FOLDERS[] = {
107 TNY_FOLDER_TYPE_OUTBOX,
108 TNY_FOLDER_TYPE_DRAFTS,
112 static const TnyFolderType LOCAL_FOLDERS[] = {
113 TNY_FOLDER_TYPE_OUTBOX,
114 TNY_FOLDER_TYPE_DRAFTS,
115 TNY_FOLDER_TYPE_SENT,
116 TNY_FOLDER_TYPE_TRASH,
117 TNY_FOLDER_TYPE_ARCHIVE
119 #endif /* MODEST_PLATFORM_MAEMO */
123 modest_init_init_core (void)
126 static gboolean invoked = FALSE;
129 g_printerr ("modest: modest_init_init_core may only be invoked once\n");
130 g_assert (!invoked); /* abort */
137 init_debug_logging();
142 if (!modest_runtime_init()) {
143 modest_init_uninit ();
144 g_printerr ("modest: failed to initialize the modest runtime\n");
149 /* do an initial guess for the device name */
150 init_device_name (modest_runtime_get_conf());
152 if (!modest_platform_init()) {
153 modest_init_uninit ();
154 g_printerr ("modest: failed to run platform-specific initialization\n");
158 /* based on the debug settings, we decide whether to overwrite old settings */
159 reset = modest_runtime_get_debug_flags () & MODEST_RUNTIME_DEBUG_FACTORY_SETTINGS;
160 if (!init_header_columns(modest_runtime_get_conf(), reset)) {
161 modest_init_uninit ();
162 g_printerr ("modest: failed to init header columns\n");
166 init_default_settings (modest_runtime_get_conf ());
168 if (!init_local_folders()) {
169 modest_init_uninit ();
170 g_printerr ("modest: failed to init local folders\n");
174 if (!init_default_account_maybe(modest_runtime_get_account_mgr ())) {
175 modest_init_uninit ();
176 g_printerr ("modest: failed to init default account\n");
185 modest_init_init_ui (gint argc, gchar** argv)
187 if (!gtk_init_check(&argc, &argv)) {
188 g_printerr ("modest: failed to initialize graphical ui\n");
192 /* Set application name */
193 g_set_application_name (modest_platform_get_app_name());
194 g_warning (modest_platform_get_app_name());
202 modest_init_uninit (void)
204 if (!modest_runtime_uninit())
205 g_printerr ("modest: failed to uninit runtime\n");
213 /* NOTE: the exact details of this format are important, as they
214 * are also used in modest-widget-memory. FIXME: make a shared function
215 * for this with widget-memory
218 save_header_settings (ModestConf *conf, TnyFolderType type,
219 ModestHeaderViewStyle style, const FolderCols* cols,
220 guint col_num, gboolean overwrite)
226 g_return_val_if_fail (cols, FALSE);
228 key = _modest_widget_memory_get_keyname_with_double_type ("header-view",
230 MODEST_WIDGET_MEMORY_PARAM_COLUMN_WIDTH);
231 /* if we're not in overwrite mode, only write stuff it
232 * there was nothing before */
233 if (!overwrite && modest_conf_key_exists(conf, key, NULL)) {
238 /* the format is necessarily the same as the one in modest-widget-memory */
239 str = g_string_new (NULL);
240 for (i = 0; i != col_num; ++i)
241 g_string_append_printf (str, "%d:%d:%d ",
242 cols[i].col, cols[i].width, cols[i].sort);
244 modest_conf_set_string (conf, key, str->str, NULL);
246 g_string_free (str, TRUE);
252 * modest_init_header_columns:
253 * @overwrite: write the setting, even if it already exists
255 * will set defaults for the columns to show for folder,
256 * if there are no such settings yet (in ModestWidgetMemory)
258 * Returns: TRUE if succeeded, FALSE in case of error
261 init_header_columns (ModestConf *conf, gboolean overwrite)
265 for (folder_type = TNY_FOLDER_TYPE_UNKNOWN;
266 folder_type <= TNY_FOLDER_TYPE_CALENDAR; ++folder_type) {
268 switch (folder_type) {
269 case TNY_FOLDER_TYPE_SENT:
270 case TNY_FOLDER_TYPE_DRAFTS:
271 save_header_settings (conf, folder_type,
272 MODEST_HEADER_VIEW_STYLE_DETAILS,
273 OUTBOX_COLUMNS_DETAILS,
274 G_N_ELEMENTS(OUTBOX_COLUMNS_DETAILS),
276 save_header_settings (conf, folder_type,
277 MODEST_HEADER_VIEW_STYLE_TWOLINES,
278 SENT_COLUMNS_TWOLINES,
279 G_N_ELEMENTS(SENT_COLUMNS_TWOLINES),
282 case TNY_FOLDER_TYPE_OUTBOX:
283 save_header_settings (conf, folder_type,
284 MODEST_HEADER_VIEW_STYLE_TWOLINES,
285 OUTBOX_COLUMNS_TWOLINES,
286 G_N_ELEMENTS(OUTBOX_COLUMNS_TWOLINES),
291 save_header_settings (conf, folder_type,
292 MODEST_HEADER_VIEW_STYLE_DETAILS,
293 INBOX_COLUMNS_DETAILS,
294 G_N_ELEMENTS(INBOX_COLUMNS_DETAILS),
296 save_header_settings (conf, folder_type,
297 MODEST_HEADER_VIEW_STYLE_TWOLINES,
298 INBOX_COLUMNS_TWOLINES,
299 G_N_ELEMENTS(INBOX_COLUMNS_TWOLINES),
307 * init_local_folders:
309 * create the Local Folders folder under cache, if they
312 * Returns: TRUE if the folder were already there, or
313 * they were created, FALSE otherwise
316 init_local_folders (void)
320 static const gchar* maildirs[] = {
324 maildir_path = modest_local_folder_info_get_maildir_path ();
326 for (i = 0; i != G_N_ELEMENTS(LOCAL_FOLDERS); ++i) {
328 for (j = 0; j != G_N_ELEMENTS(maildirs); ++j) {
330 dir = g_build_filename (maildir_path,
331 modest_local_folder_info_get_type_name(LOCAL_FOLDERS[i]),
334 if (g_mkdir_with_parents (dir, 0755) < 0) {
335 g_printerr ("modest: failed to create %s\n", dir);
337 g_free (maildir_path);
344 g_free (maildir_path);
351 free_element (gpointer data, gpointer user_data)
359 * init_default_account_maybe:
361 * if there are accounts defined, but there is no default account,
362 * it will be defined.
364 * Returns: TRUE if there was a default account already,
365 * or one has been created or there are no accounts yet,
366 * returns FALSE in case of error
369 init_default_account_maybe (ModestAccountMgr *acc_mgr)
371 GSList *all_accounts = NULL;
372 gchar *default_account;
373 gboolean retval = TRUE;
375 all_accounts = modest_account_mgr_account_names (acc_mgr);
376 if (all_accounts) { /* if there are any accounts, there should be a default one */
378 modest_account_mgr_get_default_account (acc_mgr);
379 if (!default_account) {
380 gchar *first_account;
381 g_printerr ("modest: no default account defined\n");
382 first_account = (gchar*)all_accounts->data;
383 if ((retval = modest_account_mgr_set_default_account (acc_mgr, first_account)))
384 g_printerr ("modest: set '%s' as the default account\n",
387 g_printerr ("modest: failed to set '%s' as the default account\n",
389 g_free (default_account);
391 g_slist_foreach (all_accounts, free_element, NULL);
392 g_slist_free (all_accounts);
400 init_debug_g_type (void)
402 GTypeDebugFlags gflags;
403 ModestInitDebugFlags mflags;
406 mflags = modest_runtime_get_debug_flags ();
408 if (mflags & MODEST_INIT_DEBUG_DEBUG_OBJECTS)
409 gflags |= G_TYPE_DEBUG_OBJECTS;
410 if (mflags & MODEST_INIT_DEBUG_DEBUG_SIGNALS)
411 gflags |= G_TYPE_DEBUG_SIGNALS;
413 g_type_init_with_debug_flags (gflags);
417 init_debug_logging (void)
419 ModestInitDebugFlags mflags;
420 mflags = modest_runtime_get_debug_flags ();
422 if (mflags & MODEST_INIT_DEBUG_ABORT_ON_WARNING)
423 g_log_set_always_fatal (G_LOG_LEVEL_ERROR |
424 G_LOG_LEVEL_CRITICAL |
425 G_LOG_LEVEL_WARNING);
432 /* Setup gettext, to use our .po files: */
433 /* GETTEXT_PACKAGE and MODEST_LOCALE_DIR are defined in config.h */
434 bindtextdomain (GETTEXT_PACKAGE, MODEST_LOCALE_DIR);
435 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
436 textdomain (GETTEXT_PACKAGE);
441 * This function registers our custom toolbar icons, so they can be
442 * themed. The idea of this function was taken from the gtk-demo
445 init_stock_icons (void)
447 static gboolean registered = FALSE;
450 GtkIconTheme *current_theme;
452 GtkIconFactory *factory;
455 static GtkStockItem items[] = {
456 #ifdef MODEST_PLATFORM_MAEMO
457 { MODEST_STOCK_SPLIT_VIEW, "split view", 0, 0, NULL },
458 { MODEST_STOCK_SORT, "sort mail", 0, 0, NULL },
459 { MODEST_STOCK_REFRESH, "refresh mail", 0, 0, NULL },
460 #endif /*MODEST_PLATFORM_MAEMO*/
461 { MODEST_STOCK_MAIL_SEND, "send mail", 0, 0, NULL },
462 { MODEST_STOCK_NEW_MAIL, "new mail", 0, 0, NULL },
463 /* { MODEST_STOCK_SEND_RECEIVE, "send receive", 0, 0, NULL }, */
464 { MODEST_STOCK_REPLY, "reply", 0, 0, NULL },
465 { MODEST_STOCK_REPLY_ALL, "reply all", 0, 0, NULL },
466 { MODEST_STOCK_FORWARD, "forward", 0, 0, NULL },
467 { MODEST_STOCK_DELETE, "delete", 0, 0, NULL },
468 /* { MODEST_STOCK_NEXT, "next", 0, 0, NULL }, */
469 /* { MODEST_STOCK_PREV, "prev", 0, 0, NULL }, */
470 /* { MODEST_STOCK_STOP, "stop", 0, 0, NULL } */
473 static gchar *items_names [] = {
474 #ifdef MODEST_PLATFORM_MAEMO
475 MODEST_TOOLBAR_ICON_SPLIT_VIEW,
476 MODEST_TOOLBAR_ICON_SORT,
477 MODEST_TOOLBAR_ICON_REFRESH,
478 #endif /*MODEST_PLATFORM_MAEMO*/
479 MODEST_TOOLBAR_ICON_MAIL_SEND,
480 MODEST_TOOLBAR_ICON_NEW_MAIL,
481 /* MODEST_TOOLBAR_ICON_SEND_RECEIVE, */
482 MODEST_TOOLBAR_ICON_REPLY,
483 MODEST_TOOLBAR_ICON_REPLY_ALL,
484 MODEST_TOOLBAR_ICON_FORWARD,
485 MODEST_TOOLBAR_ICON_DELETE,
486 /* MODEST_TOOLBAR_ICON_NEXT, */
487 /* MODEST_TOOLBAR_ICON_PREV, */
488 /* MODEST_TOOLBAR_ICON_STOP */
489 MODEST_TOOLBAR_ICON_FORMAT_BULLETS,
494 /* Register our stock items */
495 gtk_stock_add (items, G_N_ELEMENTS (items));
497 /* Add our custom icon factory to the list of defaults */
498 factory = gtk_icon_factory_new ();
499 gtk_icon_factory_add_default (factory);
501 current_theme = gtk_icon_theme_get_default ();
503 /* Register icons to accompany stock items */
504 for (i = 0; i < G_N_ELEMENTS (items); i++) {
506 #ifdef MODEST_PLATFORM_MAEMO /* MODES_PLATFORM_ID: 1 ==> gnome, 2==> maemo */
507 pixbuf = gtk_icon_theme_load_icon (current_theme,
510 GTK_ICON_LOOKUP_NO_SVG,
513 pixbuf = gdk_pixbuf_new_from_file (items_names[i], NULL);
516 if (pixbuf != NULL) {
517 GtkIconSet *icon_set;
518 GdkPixbuf *transparent;
520 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
521 icon_set = gtk_icon_set_new_from_pixbuf (transparent);
522 gtk_icon_factory_add (factory, items[i].stock_id, icon_set);
523 gtk_icon_set_unref (icon_set);
524 g_object_unref (pixbuf);
525 g_object_unref (transparent);
528 g_warning ("failed to load %s icon", items_names[i]);
530 /* Drop our reference to the factory, GTK will hold a reference. */
531 g_object_unref (factory);
537 init_default_settings (ModestConf *conf)
539 if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_TOOLBAR, NULL))
540 modest_conf_set_bool (conf, MODEST_CONF_SHOW_TOOLBAR, TRUE, NULL);
542 if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_TOOLBAR_FULLSCREEN, NULL))
543 modest_conf_set_bool (conf, MODEST_CONF_SHOW_TOOLBAR_FULLSCREEN, TRUE, NULL);
545 if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_CC, NULL))
546 modest_conf_set_bool (conf, MODEST_CONF_SHOW_CC, TRUE, NULL);
548 if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_BCC, NULL))
549 modest_conf_set_bool (conf, MODEST_CONF_SHOW_BCC, FALSE, NULL);
551 if (!modest_conf_key_exists (conf, MODEST_CONF_CONNECT_AT_STARTUP, NULL))
552 modest_conf_set_bool (conf, MODEST_CONF_CONNECT_AT_STARTUP, TRUE, NULL);
557 /* set the device name -- note this is an initial guess from /etc/hostname
558 * on maemo-device it will most probably be replaced with the Bluetooth device
559 * name later during starting (see maemo/modest-maemo-utils.[ch])
562 init_device_name (ModestConf *conf)
564 int len = 255; /* max len */
565 gchar *devname = NULL;
567 if (!g_file_get_contents("/etc/hostname", &devname, &len, NULL) || len < 2 || len > 254) {
568 g_printerr ("modest: failed to read hostname\n");
569 modest_conf_set_string (conf, MODEST_CONF_DEVICE_NAME,
570 MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME,
573 /* remove the \n at the end */
574 if (devname[len-1] == '\n')
575 devname[len-1] = '\0';
579 modest_conf_set_string (conf, MODEST_CONF_DEVICE_NAME,devname, NULL);