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;
68 static const FolderCols INBOX_COLUMNS_DETAILS[] = {
69 {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
70 {MODEST_HEADER_VIEW_COLUMN_ATTACH, 40},
71 {MODEST_HEADER_VIEW_COLUMN_FROM, 80},
72 {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80},
73 {MODEST_HEADER_VIEW_COLUMN_RECEIVED_DATE, 60},
74 {MODEST_HEADER_VIEW_COLUMN_SIZE, 50}
77 static const FolderCols INBOX_COLUMNS_TWOLINES[] = {
78 {MODEST_HEADER_VIEW_COLUMN_COMPACT_FLAG, 40},
79 {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_IN, 180},
80 {MODEST_HEADER_VIEW_COLUMN_COMPACT_RECEIVED_DATE, 240}
83 static const FolderCols OUTBOX_COLUMNS_DETAILS[] = {
84 {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
85 {MODEST_HEADER_VIEW_COLUMN_ATTACH, 40},
86 {MODEST_HEADER_VIEW_COLUMN_TO, 80},
87 {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80},
88 {MODEST_HEADER_VIEW_COLUMN_SENT_DATE, 80},
89 {MODEST_HEADER_VIEW_COLUMN_SIZE, 50}
92 static const FolderCols OUTBOX_COLUMNS_TWOLINES[] = {
93 {MODEST_HEADER_VIEW_COLUMN_COMPACT_FLAG, 40},
94 {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT,180},
95 {MODEST_HEADER_VIEW_COLUMN_STATUS, 240}
98 static const FolderCols SENT_COLUMNS_TWOLINES[] = {
99 {MODEST_HEADER_VIEW_COLUMN_COMPACT_FLAG, 40},
100 {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT,180},
101 {MODEST_HEADER_VIEW_COLUMN_COMPACT_SENT_DATE, 240}
104 #ifdef MODEST_PLATFORM_MAEMO
105 static const TnyFolderType LOCAL_FOLDERS[] = {
106 TNY_FOLDER_TYPE_OUTBOX,
107 TNY_FOLDER_TYPE_DRAFTS,
111 static const TnyFolderType LOCAL_FOLDERS[] = {
112 TNY_FOLDER_TYPE_OUTBOX,
113 TNY_FOLDER_TYPE_DRAFTS,
114 TNY_FOLDER_TYPE_SENT,
115 TNY_FOLDER_TYPE_TRASH,
116 TNY_FOLDER_TYPE_ARCHIVE
118 #endif /* MODEST_PLATFORM_MAEMO */
122 modest_init_init_core (void)
125 static gboolean invoked = FALSE;
128 g_printerr ("modest: modest_init_init_core may only be invoked once\n");
129 g_assert (!invoked); /* abort */
136 init_debug_logging();
141 if (!modest_runtime_init()) {
142 modest_init_uninit ();
143 g_printerr ("modest: failed to initialize the modest runtime\n");
148 /* do an initial guess for the device name */
149 init_device_name (modest_runtime_get_conf());
151 if (!modest_platform_init()) {
152 modest_init_uninit ();
153 g_printerr ("modest: failed to run platform-specific initialization\n");
157 /* based on the debug settings, we decide whether to overwrite old settings */
158 reset = modest_runtime_get_debug_flags () & MODEST_RUNTIME_DEBUG_FACTORY_SETTINGS;
159 if (!init_header_columns(modest_runtime_get_conf(), reset)) {
160 modest_init_uninit ();
161 g_printerr ("modest: failed to init header columns\n");
165 init_default_settings (modest_runtime_get_conf ());
167 if (!init_local_folders()) {
168 modest_init_uninit ();
169 g_printerr ("modest: failed to init local folders\n");
173 if (!init_default_account_maybe(modest_runtime_get_account_mgr ())) {
174 modest_init_uninit ();
175 g_printerr ("modest: failed to init default account\n");
184 modest_init_init_ui (gint argc, gchar** argv)
186 if (!gtk_init_check(&argc, &argv)) {
187 g_printerr ("modest: failed to initialize graphical ui\n");
191 /* Set application name */
192 g_set_application_name (modest_platform_get_app_name());
193 g_warning (modest_platform_get_app_name());
201 modest_init_uninit (void)
203 if (!modest_runtime_uninit())
204 g_printerr ("modest: failed to uninit runtime\n");
212 /* NOTE: the exact details of this format are important, as they
213 * are also used in modest-widget-memory. FIXME: make a shared function
214 * for this with widget-memory
217 save_header_settings (ModestConf *conf, TnyFolderType type,
218 ModestHeaderViewStyle style, const FolderCols* cols,
219 guint col_num, gboolean overwrite)
225 g_return_val_if_fail (cols, FALSE);
227 key = _modest_widget_memory_get_keyname_with_double_type ("header-view",
229 MODEST_WIDGET_MEMORY_PARAM_COLUMN_WIDTH);
230 /* if we're not in overwrite mode, only write stuff it
231 * there was nothing before */
232 if (!overwrite && modest_conf_key_exists(conf, key, NULL)) {
237 /* the format is necessarily the same as the one in modest-widget-memory */
238 str = g_string_new (NULL);
239 for (i = 0; i != col_num; ++i)
240 g_string_append_printf (str, "%d:%d ",
241 cols[i].col, cols[i].width);
243 modest_conf_set_string (conf, key, str->str, NULL);
245 g_string_free (str, TRUE);
251 * modest_init_header_columns:
252 * @overwrite: write the setting, even if it already exists
254 * will set defaults for the columns to show for folder,
255 * if there are no such settings yet (in ModestWidgetMemory)
257 * Returns: TRUE if succeeded, FALSE in case of error
260 init_header_columns (ModestConf *conf, gboolean overwrite)
264 for (folder_type = TNY_FOLDER_TYPE_UNKNOWN;
265 folder_type <= TNY_FOLDER_TYPE_CALENDAR; ++folder_type) {
267 switch (folder_type) {
268 case TNY_FOLDER_TYPE_SENT:
269 case TNY_FOLDER_TYPE_DRAFTS:
270 save_header_settings (conf, folder_type,
271 MODEST_HEADER_VIEW_STYLE_DETAILS,
272 OUTBOX_COLUMNS_DETAILS,
273 G_N_ELEMENTS(OUTBOX_COLUMNS_DETAILS),
275 save_header_settings (conf, folder_type,
276 MODEST_HEADER_VIEW_STYLE_TWOLINES,
277 SENT_COLUMNS_TWOLINES,
278 G_N_ELEMENTS(SENT_COLUMNS_TWOLINES),
281 case TNY_FOLDER_TYPE_OUTBOX:
282 save_header_settings (conf, folder_type,
283 MODEST_HEADER_VIEW_STYLE_TWOLINES,
284 OUTBOX_COLUMNS_TWOLINES,
285 G_N_ELEMENTS(OUTBOX_COLUMNS_TWOLINES),
290 save_header_settings (conf, folder_type,
291 MODEST_HEADER_VIEW_STYLE_DETAILS,
292 INBOX_COLUMNS_DETAILS,
293 G_N_ELEMENTS(INBOX_COLUMNS_DETAILS),
295 save_header_settings (conf, folder_type,
296 MODEST_HEADER_VIEW_STYLE_TWOLINES,
297 INBOX_COLUMNS_TWOLINES,
298 G_N_ELEMENTS(INBOX_COLUMNS_TWOLINES),
306 * init_local_folders:
308 * create the Local Folders folder under cache, if they
311 * Returns: TRUE if the folder were already there, or
312 * they were created, FALSE otherwise
315 init_local_folders (void)
319 static const gchar* maildirs[] = {
323 maildir_path = modest_local_folder_info_get_maildir_path ();
325 for (i = 0; i != G_N_ELEMENTS(LOCAL_FOLDERS); ++i) {
327 for (j = 0; j != G_N_ELEMENTS(maildirs); ++j) {
329 dir = g_build_filename (maildir_path,
330 modest_local_folder_info_get_type_name(LOCAL_FOLDERS[i]),
333 if (g_mkdir_with_parents (dir, 0755) < 0) {
334 g_printerr ("modest: failed to create %s\n", dir);
336 g_free (maildir_path);
343 g_free (maildir_path);
350 free_element (gpointer data, gpointer user_data)
358 * init_default_account_maybe:
360 * if there are accounts defined, but there is no default account,
361 * it will be defined.
363 * Returns: TRUE if there was a default account already,
364 * or one has been created or there are no accounts yet,
365 * returns FALSE in case of error
368 init_default_account_maybe (ModestAccountMgr *acc_mgr)
370 GSList *all_accounts = NULL;
371 gchar *default_account;
372 gboolean retval = TRUE;
374 all_accounts = modest_account_mgr_account_names (acc_mgr);
375 if (all_accounts) { /* if there are any accounts, there should be a default one */
377 modest_account_mgr_get_default_account (acc_mgr);
378 if (!default_account) {
379 gchar *first_account;
380 g_printerr ("modest: no default account defined\n");
381 first_account = (gchar*)all_accounts->data;
382 if ((retval = modest_account_mgr_set_default_account (acc_mgr, first_account)))
383 g_printerr ("modest: set '%s' as the default account\n",
386 g_printerr ("modest: failed to set '%s' as the default account\n",
388 g_free (default_account);
390 g_slist_foreach (all_accounts, free_element, NULL);
391 g_slist_free (all_accounts);
399 init_debug_g_type (void)
401 GTypeDebugFlags gflags;
402 ModestInitDebugFlags mflags;
405 mflags = modest_runtime_get_debug_flags ();
407 if (mflags & MODEST_INIT_DEBUG_DEBUG_OBJECTS)
408 gflags |= G_TYPE_DEBUG_OBJECTS;
409 if (mflags & MODEST_INIT_DEBUG_DEBUG_SIGNALS)
410 gflags |= G_TYPE_DEBUG_SIGNALS;
412 g_type_init_with_debug_flags (gflags);
416 init_debug_logging (void)
418 ModestInitDebugFlags mflags;
419 mflags = modest_runtime_get_debug_flags ();
421 if (mflags & MODEST_INIT_DEBUG_ABORT_ON_WARNING)
422 g_log_set_always_fatal (G_LOG_LEVEL_ERROR |
423 G_LOG_LEVEL_CRITICAL |
424 G_LOG_LEVEL_WARNING);
431 /* Setup gettext, to use our .po files: */
432 /* GETTEXT_PACKAGE and MODEST_LOCALE_DIR are defined in config.h */
433 bindtextdomain (GETTEXT_PACKAGE, MODEST_LOCALE_DIR);
434 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
435 textdomain (GETTEXT_PACKAGE);
440 * This function registers our custom toolbar icons, so they can be
441 * themed. The idea of this function was taken from the gtk-demo
444 init_stock_icons (void)
446 static gboolean registered = FALSE;
449 GtkIconTheme *current_theme;
451 GtkIconFactory *factory;
454 static GtkStockItem items[] = {
455 #ifdef MODEST_PLATFORM_MAEMO
456 { MODEST_STOCK_SPLIT_VIEW, "split view", 0, 0, NULL },
457 { MODEST_STOCK_SORT, "sort mail", 0, 0, NULL },
458 { MODEST_STOCK_REFRESH, "refresh mail", 0, 0, NULL },
459 #endif /*MODEST_PLATFORM_MAEMO*/
460 { MODEST_STOCK_MAIL_SEND, "send mail", 0, 0, NULL },
461 { MODEST_STOCK_NEW_MAIL, "new mail", 0, 0, NULL },
462 /* { MODEST_STOCK_SEND_RECEIVE, "send receive", 0, 0, NULL }, */
463 { MODEST_STOCK_REPLY, "reply", 0, 0, NULL },
464 { MODEST_STOCK_REPLY_ALL, "reply all", 0, 0, NULL },
465 { MODEST_STOCK_FORWARD, "forward", 0, 0, NULL },
466 { MODEST_STOCK_DELETE, "delete", 0, 0, NULL },
467 /* { MODEST_STOCK_NEXT, "next", 0, 0, NULL }, */
468 /* { MODEST_STOCK_PREV, "prev", 0, 0, NULL }, */
469 /* { MODEST_STOCK_STOP, "stop", 0, 0, NULL } */
472 static gchar *items_names [] = {
473 #ifdef MODEST_PLATFORM_MAEMO
474 MODEST_TOOLBAR_ICON_SPLIT_VIEW,
475 MODEST_TOOLBAR_ICON_SORT,
476 MODEST_TOOLBAR_ICON_REFRESH,
477 #endif /*MODEST_PLATFORM_MAEMO*/
478 MODEST_TOOLBAR_ICON_MAIL_SEND,
479 MODEST_TOOLBAR_ICON_NEW_MAIL,
480 /* MODEST_TOOLBAR_ICON_SEND_RECEIVE, */
481 MODEST_TOOLBAR_ICON_REPLY,
482 MODEST_TOOLBAR_ICON_REPLY_ALL,
483 MODEST_TOOLBAR_ICON_FORWARD,
484 MODEST_TOOLBAR_ICON_DELETE,
485 /* MODEST_TOOLBAR_ICON_NEXT, */
486 /* MODEST_TOOLBAR_ICON_PREV, */
487 /* MODEST_TOOLBAR_ICON_STOP */
488 MODEST_TOOLBAR_ICON_FORMAT_BULLETS,
493 /* Register our stock items */
494 gtk_stock_add (items, G_N_ELEMENTS (items));
496 /* Add our custom icon factory to the list of defaults */
497 factory = gtk_icon_factory_new ();
498 gtk_icon_factory_add_default (factory);
500 current_theme = gtk_icon_theme_get_default ();
502 /* Register icons to accompany stock items */
503 for (i = 0; i < G_N_ELEMENTS (items); i++) {
505 #ifdef MODEST_PLATFORM_MAEMO /* MODES_PLATFORM_ID: 1 ==> gnome, 2==> maemo */
506 pixbuf = gtk_icon_theme_load_icon (current_theme,
509 GTK_ICON_LOOKUP_NO_SVG,
512 pixbuf = gdk_pixbuf_new_from_file (items_names[i], NULL);
515 if (pixbuf != NULL) {
516 GtkIconSet *icon_set;
517 GdkPixbuf *transparent;
519 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
520 icon_set = gtk_icon_set_new_from_pixbuf (transparent);
521 gtk_icon_factory_add (factory, items[i].stock_id, icon_set);
522 gtk_icon_set_unref (icon_set);
523 g_object_unref (pixbuf);
524 g_object_unref (transparent);
527 g_warning ("failed to load %s icon", items_names[i]);
529 /* Drop our reference to the factory, GTK will hold a reference. */
530 g_object_unref (factory);
536 init_default_settings (ModestConf *conf)
538 if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_TOOLBAR, NULL))
539 modest_conf_set_bool (conf, MODEST_CONF_SHOW_TOOLBAR, TRUE, NULL);
541 if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_TOOLBAR_FULLSCREEN, NULL))
542 modest_conf_set_bool (conf, MODEST_CONF_SHOW_TOOLBAR_FULLSCREEN, TRUE, NULL);
544 if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_CC, NULL))
545 modest_conf_set_bool (conf, MODEST_CONF_SHOW_CC, TRUE, NULL);
547 if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_BCC, NULL))
548 modest_conf_set_bool (conf, MODEST_CONF_SHOW_BCC, FALSE, NULL);
550 if (!modest_conf_key_exists (conf, MODEST_CONF_CONNECT_AT_STARTUP, NULL))
551 modest_conf_set_bool (conf, MODEST_CONF_CONNECT_AT_STARTUP, TRUE, NULL);
556 /* set the device name -- note this is an initial guess from /etc/hostname
557 * on maemo-device it will most probably be replaced with the Bluetooth device
558 * name later during starting (see maemo/modest-maemo-utils.[ch])
561 init_device_name (ModestConf *conf)
563 int len = 255; /* max len */
564 gchar *devname = NULL;
566 if (!g_file_get_contents("/etc/hostname", &devname, &len, NULL) || len < 2 || len > 254) {
567 g_printerr ("modest: failed to read hostname\n");
568 modest_conf_set_string (conf, MODEST_CONF_DEVICE_NAME,
569 MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME,
572 /* remove the \n at the end */
573 if (devname[len-1] == '\n')
574 devname[len-1] = '\0';
578 modest_conf_set_string (conf, MODEST_CONF_DEVICE_NAME,devname, NULL);