We assume that, if we cannot write temporary file to view, it's that
[modest] / src / modest-init.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
30 #include <sys/utsname.h>
31 #include <config.h>
32 #include <glib.h>
33 #include <glib-object.h>
34 #include <glib/gstdio.h>
35 #include <modest-runtime.h>
36 #include <modest-runtime-priv.h>
37 #include <modest-init.h>
38 #include <modest-defs.h>
39 #include <modest-singletons.h>
40 #include <widgets/modest-header-view.h>
41 #include <widgets/modest-folder-view.h>
42 #include <modest-tny-platform-factory.h>
43 #include <modest-platform.h>
44 #include <modest-widget-memory.h>
45 #include <modest-widget-memory-priv.h>
46 #include <modest-local-folder-info.h>
47 #include <modest-account-mgr.h>
48 #include <modest-account-mgr-helpers.h>
49 #include <modest-icon-names.h>
50 #include "widgets/modest-global-settings-dialog.h"
51 #include "modest-tny-msg.h"
52 #include <libgnomevfs/gnome-vfs.h>
53 #include <string.h>
54 #include "modest-text-utils.h"
55
56 #ifndef MODEST_TOOLKIT_GTK
57 #include "modest-hildon-includes.h"
58 #endif
59 #include <locale.h>
60
61 static gboolean init_header_columns (ModestConf *conf, gboolean overwrite);
62 static gboolean init_default_account_maybe  (ModestAccountMgr *acc_mgr);
63 static void     init_i18n (void);
64 static void     init_stock_icons (void);
65 static void     init_debug_g_type (void);
66 static void     init_debug_logging (void);
67 static void     init_default_settings (ModestConf *conf);
68 static void     init_device_name (ModestConf *conf);
69 static gboolean init_ui (gint argc, gchar** argv);
70
71
72 static gboolean _is_initialized = FALSE;
73
74 /*
75  * defaults for the column headers
76  */
77 typedef struct {
78         ModestHeaderViewColumn col;
79         guint                  width;
80         gint                  sort;
81 } FolderCols;
82
83
84 static const guint MODEST_MAIN_PANED_POS_PERCENTAGE = 30;
85 static const guint MODEST_MSG_PANED_POS_PERCENTAGE = 50;
86
87 static const FolderCols INBOX_COLUMNS_DETAILS[] = {
88         {MODEST_HEADER_VIEW_COLUMN_ATTACH,  40, 0},
89         {MODEST_HEADER_VIEW_COLUMN_FROM,    80, 0},
90         {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80, 0},
91         {MODEST_HEADER_VIEW_COLUMN_RECEIVED_DATE, 60, 0},
92         {MODEST_HEADER_VIEW_COLUMN_SIZE, 50, 0}
93 };
94
95 static const FolderCols INBOX_COLUMNS_TWOLINES[] = {
96         {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_IN, 180, 0},
97 };
98
99 static const FolderCols OUTBOX_COLUMNS_DETAILS[] = {
100         {MODEST_HEADER_VIEW_COLUMN_ATTACH,  40, 0},
101         {MODEST_HEADER_VIEW_COLUMN_TO,    80, 0},
102         {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80, 0},
103         {MODEST_HEADER_VIEW_COLUMN_SENT_DATE, 80, 0},
104         {MODEST_HEADER_VIEW_COLUMN_SIZE, 50, 0}
105 };
106
107 static const FolderCols OUTBOX_COLUMNS_TWOLINES[] = {
108         {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT,180, 0}
109 //      {MODEST_HEADER_VIEW_COLUMN_STATUS, 240, 0}
110 };
111
112 static const FolderCols SENT_COLUMNS_TWOLINES[] = {
113         {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT,180, 0},
114 };
115
116 #ifndef MODEST_TOOLKIT_GTK
117 static const TnyFolderType LOCAL_FOLDERS[] = {
118 /*      TNY_FOLDER_TYPE_OUTBOX, */
119         TNY_FOLDER_TYPE_DRAFTS,
120         TNY_FOLDER_TYPE_SENT
121 };
122 #else
123 static const TnyFolderType LOCAL_FOLDERS[] = {
124 /*      TNY_FOLDER_TYPE_OUTBOX, */
125         TNY_FOLDER_TYPE_DRAFTS,
126         TNY_FOLDER_TYPE_SENT,
127         TNY_FOLDER_TYPE_TRASH,
128         TNY_FOLDER_TYPE_ARCHIVE 
129 };
130 #endif /* MODEST_TOOLKIT_GTK */
131
132 static GList*
133 new_cold_ids_gslist_from_array( const FolderCols* cols, guint col_num)
134 {
135         GList *result = NULL;
136         
137         guint i = 0;
138         for (i = 0; i < col_num; ++i) {
139                 result = g_list_append (result, GINT_TO_POINTER (cols[i].col));
140         }
141         
142         return result;
143 }
144
145 GList* 
146 modest_init_get_default_header_view_column_ids (TnyFolderType folder_type, ModestHeaderViewStyle style)
147 {
148                 GList *result = NULL;
149                 
150                 switch (folder_type) {
151                 case TNY_FOLDER_TYPE_SENT:
152                 case TNY_FOLDER_TYPE_DRAFTS:
153                         if (style == MODEST_HEADER_VIEW_STYLE_DETAILS)
154                                 result = new_cold_ids_gslist_from_array (OUTBOX_COLUMNS_DETAILS,
155                                       G_N_ELEMENTS(OUTBOX_COLUMNS_DETAILS));
156                         else if (style == MODEST_HEADER_VIEW_STYLE_TWOLINES)
157                                 result = new_cold_ids_gslist_from_array (SENT_COLUMNS_TWOLINES,
158                                       G_N_ELEMENTS(SENT_COLUMNS_TWOLINES));
159                 break;
160                 case TNY_FOLDER_TYPE_OUTBOX:
161                         if (style == MODEST_HEADER_VIEW_STYLE_TWOLINES)
162                                 result = new_cold_ids_gslist_from_array (OUTBOX_COLUMNS_TWOLINES,
163                                       G_N_ELEMENTS(OUTBOX_COLUMNS_TWOLINES));
164                 break;
165
166                 default:
167                         if (style == MODEST_HEADER_VIEW_STYLE_DETAILS)
168                                 result =  new_cold_ids_gslist_from_array (INBOX_COLUMNS_DETAILS,
169                                       G_N_ELEMENTS(INBOX_COLUMNS_DETAILS));
170                         else if (style == MODEST_HEADER_VIEW_STYLE_TWOLINES)
171                                 result = new_cold_ids_gslist_from_array (INBOX_COLUMNS_TWOLINES,
172                                       G_N_ELEMENTS(INBOX_COLUMNS_TWOLINES));
173                 };
174                 
175                 if (!result) {
176                         g_warning("DEBUG: %s: No default columns IDs found for "
177                                 "folder_type=%d, style=%d\n", __FUNCTION__, folder_type, style);        
178                 }
179                 
180                 return result;
181 }
182
183
184 static gboolean
185 force_ke_recv_load (void)
186 {
187         if (strcmp ("cerm_device_memory_full",
188                     _KR("cerm_device_memory_full")) == 0) {
189                 g_warning ("%s: cannot get translation for cerm_device_memory_full",
190                            __FUNCTION__);
191                 return FALSE;
192         }
193
194         return TRUE;
195 }
196
197
198 gboolean
199 modest_init (int argc, char *argv[])
200 {
201         gboolean reset;
202
203         if (_is_initialized) {
204                 g_printerr ("modest: %s may only be invoked once\n", __FUNCTION__);
205                 return FALSE;
206         } 
207         
208         init_i18n();
209
210         if (!force_ke_recv_load()) {
211                 g_printerr ("modest: %s: ke-recv is missing "
212                             "or memory is very low\n", __FUNCTION__);
213                 /* don't return FALSE here, because it might be that ke-recv is 
214                    missing. TODO: find a way to verify that
215                 */
216         }
217
218         init_debug_g_type();
219         init_debug_logging();
220
221         /* initialize the prng, we need it when creating random files */
222         srandom((int)getpid());
223         
224         if (!gnome_vfs_initialized()) {
225                 if (!gnome_vfs_init ()) {
226                         g_printerr ("modest: failed to init gnome-vfs\n");
227                         return FALSE;
228                 }
229         }
230         
231         if (!modest_runtime_init()) {
232                 modest_init_uninit ();
233                 g_printerr ("modest: failed to initialize the modest runtime\n");
234                 return FALSE;
235         }
236         
237         modest_plugin_factory_load_all (modest_runtime_get_plugin_factory ());
238
239         /* do an initial guess for the device name */
240         init_device_name (modest_runtime_get_conf());
241         
242         if (!modest_platform_init(argc, argv)) {
243                 modest_init_uninit ();
244                 g_printerr ("modest: failed to run platform-specific initialization\n");
245                 return FALSE;
246         }
247
248         reset = modest_runtime_get_debug_flags () & MODEST_RUNTIME_DEBUG_FACTORY_SETTINGS;
249         if (!init_header_columns(modest_runtime_get_conf(), reset)) {
250                 modest_init_uninit ();
251                 g_printerr ("modest: failed to init header columns\n");
252                 return FALSE;
253         }
254
255         init_default_settings (modest_runtime_get_conf ());
256         
257         if (!modest_init_local_folders(NULL)) {
258                 modest_init_uninit ();
259                 g_printerr ("modest: failed to init local folders\n");
260                 return FALSE;
261         }
262         
263         if (!init_default_account_maybe (modest_runtime_get_account_mgr ())) {
264                 modest_init_uninit ();
265                 g_printerr ("modest: failed to init default account\n");
266                 return FALSE;
267         }       
268         
269         if (!init_ui (argc, argv)) {
270                 modest_init_uninit ();
271                 g_printerr ("modest: failed to init ui\n");
272                 return FALSE;
273         }
274
275         return _is_initialized = TRUE;
276 }
277
278
279 static gboolean
280 init_ui (gint argc, gchar** argv)
281 {
282         /* Set application name */
283         g_set_application_name (modest_platform_get_app_name());
284         /* g_message (modest_platform_get_app_name()); */
285
286         /* Init stock icons */
287         init_stock_icons ();
288
289                 /* Init notification system */
290 #ifdef MODEST_HAVE_HILDON_NOTIFY
291         notify_init ("Basics");
292 #endif
293         return TRUE;
294 }
295
296
297 gboolean
298 modest_init_uninit (void)
299 {
300         if (!_is_initialized)
301                 return TRUE; 
302         
303         if (!modest_runtime_uninit())
304                 g_printerr ("modest: failed to uninit runtime\n");
305
306         if (!modest_platform_uninit())
307                 g_printerr ("modest: failed to uninit platform\n");
308         
309         if (gnome_vfs_initialized()) /* apparently, this returns TRUE, even after a shutdown */
310                 gnome_vfs_shutdown ();
311
312         _is_initialized = FALSE;
313         return TRUE;
314 }
315
316
317
318
319 /* NOTE: the exact details of this format are important, as they
320  * are also used in modest-widget-memory. FIXME: make a shared function
321  * for this with widget-memory
322  */
323 static gboolean
324 save_header_settings (ModestConf *conf, TnyFolderType type,
325                       ModestHeaderViewStyle style,  const FolderCols* cols,
326                       guint col_num, gboolean overwrite)
327 {
328         int i;
329         gchar *key;
330         gchar *sort_key;
331         gchar *sort_value;
332         GString *str;
333
334         g_return_val_if_fail (cols, FALSE);
335
336         key = _modest_widget_memory_get_keyname_with_double_type ("header-view",
337                                                                   type, style,
338                                                                   MODEST_WIDGET_MEMORY_PARAM_COLUMN_WIDTH);
339         sort_key = _modest_widget_memory_get_keyname_with_double_type ("header-view",
340                                                                        type, style,
341                                                                        MODEST_WIDGET_MEMORY_PARAM_COLUMN_SORT);
342         /* if we're not in overwrite mode, only write stuff it
343          * there was nothing before */
344         if (!overwrite &&  modest_conf_key_exists(conf, key, NULL)) {
345                 g_free (key);
346                 g_free (sort_key);
347                 return TRUE;
348         }
349
350         /* the format is necessarily the same as the one in modest-widget-memory */
351         str = g_string_new (NULL);
352         for (i = 0; i != col_num; ++i) 
353                 g_string_append_printf (str, "%d:%d:%d ",
354                                         cols[i].col, cols[i].width, cols[i].sort); 
355
356         modest_conf_set_string (conf, key, str->str, NULL);
357         g_free (key);
358         g_string_free (str, TRUE);
359
360         if ( col_num > 0 ) {
361                 gint sort_col_id;
362                 if (cols[0].col == MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT)
363                         sort_col_id = TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN;
364                 else
365                         sort_col_id = TNY_GTK_HEADER_LIST_MODEL_DATE_RECEIVED_TIME_T_COLUMN;
366                 sort_value = g_strdup_printf("%d:%d:%d", sort_col_id, GTK_SORT_DESCENDING, 0);
367                 modest_conf_set_string (conf, sort_key, sort_value, NULL);
368                 g_free (sort_value);
369         }
370         g_free (sort_key);
371         
372         return TRUE;
373 }
374
375 /**
376  * modest_init_header_columns:
377  * @overwrite: write the setting, even if it already exists
378  * 
379  * will set defaults for the columns to show for folder,
380  * if there are no such settings yet (in ModestWidgetMemory)
381  * 
382  * Returns: TRUE if succeeded, FALSE in case of error
383  */
384 static gboolean
385 init_header_columns (ModestConf *conf, gboolean overwrite)
386 {
387         int folder_type;
388         gchar *key;
389         
390         for (folder_type = TNY_FOLDER_TYPE_UNKNOWN;
391              folder_type < TNY_FOLDER_TYPE_NUM; ++folder_type) {                
392                 
393                 switch (folder_type) {
394                 case TNY_FOLDER_TYPE_SENT:
395                 case TNY_FOLDER_TYPE_DRAFTS:
396                 save_header_settings (conf, folder_type,
397                                       MODEST_HEADER_VIEW_STYLE_DETAILS,
398                                       OUTBOX_COLUMNS_DETAILS,
399                                       G_N_ELEMENTS(OUTBOX_COLUMNS_DETAILS),
400                                       overwrite);
401                 save_header_settings (conf, folder_type,
402                                       MODEST_HEADER_VIEW_STYLE_TWOLINES,
403                                       SENT_COLUMNS_TWOLINES,
404                                       G_N_ELEMENTS(SENT_COLUMNS_TWOLINES),
405                                       overwrite);
406                 break;
407                 case TNY_FOLDER_TYPE_OUTBOX:
408                 save_header_settings (conf, folder_type,
409                                       MODEST_HEADER_VIEW_STYLE_TWOLINES,
410                                       OUTBOX_COLUMNS_TWOLINES,
411                                       G_N_ELEMENTS(OUTBOX_COLUMNS_TWOLINES),
412                                       overwrite);
413                 break;
414
415                 default:
416                 save_header_settings (conf, folder_type,
417                                       MODEST_HEADER_VIEW_STYLE_DETAILS,
418                                       INBOX_COLUMNS_DETAILS,
419                                       G_N_ELEMENTS(INBOX_COLUMNS_DETAILS),
420                                       overwrite);
421                 save_header_settings (conf, folder_type,
422                                       MODEST_HEADER_VIEW_STYLE_TWOLINES,
423                                       INBOX_COLUMNS_TWOLINES,
424                                       G_N_ELEMENTS(INBOX_COLUMNS_TWOLINES),
425                                       overwrite);
426                 };
427         }
428         
429         key = _modest_widget_memory_get_keyname (MODEST_CONF_MAIN_PANED_KEY, 
430                                                  MODEST_WIDGET_MEMORY_PARAM_POS);
431         /* if we're not in overwrite mode, only write stuff it
432          * there was nothing before */
433         if (overwrite || !modest_conf_key_exists(conf, key, NULL)) 
434                 modest_conf_set_float (conf, key, MODEST_MAIN_PANED_POS_PERCENTAGE, NULL);
435         
436         g_free (key);
437
438         key = _modest_widget_memory_get_keyname (MODEST_CONF_MSG_PANED_KEY, 
439                                                  MODEST_WIDGET_MEMORY_PARAM_POS);
440         /* if we're not in overwrite mode, only write stuff it
441          * there was nothing before */
442         if (overwrite || !modest_conf_key_exists(conf, key, NULL)) 
443                 modest_conf_set_float (conf, key, MODEST_MSG_PANED_POS_PERCENTAGE, NULL);
444         
445         g_free (key);
446         return TRUE;
447 }
448
449 gboolean modest_init_one_local_folder (gchar *maildir_path)
450 {
451         static const gchar* maildirs[] = {
452                 "cur", "new", "tmp"
453         };
454         
455         int j;
456         for (j = 0; j != G_N_ELEMENTS(maildirs); ++j) {
457                 gchar *dir = g_build_filename (maildir_path,
458                                         maildirs[j],
459                                         NULL);
460                 if (g_mkdir_with_parents (dir, 0755) < 0) {
461                         g_printerr ("modest: %s: failed to create %s\n", __FUNCTION__, dir);
462                         g_free (dir);
463                         return FALSE;
464                 }
465                 
466                 g_free (dir);
467         }
468
469         return TRUE;
470 }
471
472 /**
473  * modest_init_local_folders:
474  * 
475  * create the Local Folders folder under cache, if they
476  * do not exist yet.
477  * 
478  * Returns: TRUE if the folder were already there, or
479  * they were created, FALSE otherwise
480  */
481 gboolean
482 modest_init_local_folders (const gchar* location_filepath)
483 {
484         gboolean retval = TRUE;
485
486         gchar *maildir_path = modest_local_folder_info_get_maildir_path (location_filepath);
487
488         if (location_filepath) {
489                 /* For instance, for memory card, just create the top-level .modest folder
490                  * and one "archive" folder (so that messages can be put somewhere):
491                  */
492
493                 gchar *dir = g_build_filename (maildir_path,
494                                                modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_ARCHIVE),
495                                                NULL);
496                 const gboolean created = modest_init_one_local_folder (dir);
497                 g_free(dir);
498
499                 if (!created) {
500                         retval = FALSE;
501                 }
502         }
503         else {
504                 /* Create each of the standard on-disk folders.
505                  * Per-account outbox folders will be created when first needed. */
506                 int i;
507                 for (i = 0; i != G_N_ELEMENTS(LOCAL_FOLDERS); ++i) {
508                         gchar *dir = g_build_filename (maildir_path,
509                                                         modest_local_folder_info_get_type_name(LOCAL_FOLDERS[i]),
510                                                         NULL);
511                         const gboolean created = modest_init_one_local_folder (dir);
512                         g_free(dir);
513
514                         if (!created) {
515                                 retval = FALSE;
516                         }
517                 }
518         }
519
520         g_free (maildir_path);
521         return retval;
522 }
523
524 /**
525  * init_default_account_maybe:
526  *
527  * if there are accounts defined, but there is no default account,
528  * it will be defined.
529  * 
530  * Returns: TRUE if there was a default account already,
531  *  or one has been created or there are no accounts yet,
532  *  returns FALSE in case of error
533  */
534 static gboolean
535 init_default_account_maybe  (ModestAccountMgr *acc_mgr)
536 {
537         gchar *default_account;
538         gboolean retval = TRUE;
539
540         default_account =  modest_account_mgr_get_default_account (acc_mgr);
541         if (!default_account)
542                 retval = modest_account_mgr_set_first_account_as_default (acc_mgr);
543         g_free (default_account);
544
545         return retval;
546 }
547
548
549
550 static void
551 init_debug_g_type (void)
552 {
553         GTypeDebugFlags gflags;
554         ModestRuntimeDebugFlags mflags;
555         
556         gflags = 0;
557         mflags = modest_runtime_get_debug_flags ();
558
559         if (mflags & MODEST_RUNTIME_DEBUG_OBJECTS)
560                 gflags |= G_TYPE_DEBUG_OBJECTS;
561         if (mflags & MODEST_RUNTIME_DEBUG_SIGNALS)
562                 gflags |= G_TYPE_DEBUG_SIGNALS;
563
564         g_type_init_with_debug_flags (gflags);
565 }
566
567 static void
568 init_debug_logging (void)
569 {
570         ModestRuntimeDebugFlags mflags;
571         mflags = modest_runtime_get_debug_flags ();
572         
573         if (mflags & MODEST_RUNTIME_DEBUG_ABORT_ON_WARNING)
574                 g_log_set_always_fatal (G_LOG_LEVEL_ERROR |
575                                         G_LOG_LEVEL_CRITICAL |
576                                         G_LOG_LEVEL_WARNING);
577 }
578
579
580 static void
581 init_i18n (void)
582 {
583        /* little trick make en_GB the fallback language, instead
584         * of the logical IDs
585         * we need the ugly ifdefs, because modest_platform_init is
586         * too late.
587         */
588         const gchar *lc_messages = getenv ("LC_MESSAGES");
589
590         if (!lc_messages) {
591                 setenv ("LANGUAGE", "en_GB", 1);
592                 setenv ("LC_MESSAGES", "en_GB", 1);
593         } else {
594                 gchar *language = g_strdup_printf ("%s:en_GB", lc_messages);
595                 setenv ("LC_MESSAGES", language, 1);
596                 g_free (language);
597         }
598         /* end of little trick */
599
600         bindtextdomain (GETTEXT_PACKAGE, MODEST_LOCALE_DIR);
601         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
602         textdomain (GETTEXT_PACKAGE);
603 }
604
605
606 /* 
607  *  This function registers our custom toolbar icons, so they can be
608  *  themed. The idea of this function was taken from the gtk-demo
609  */
610 static void
611 init_stock_icons (void)
612 {
613         static gboolean registered = FALSE;
614
615         if (!registered) {
616                 GtkIconTheme *current_theme;
617                 GdkPixbuf *pixbuf;
618                 GtkIconFactory *factory;
619                 gint i;
620
621                 static GtkStockItem items[] = {
622 #ifndef MODEST_TOOLKIT_GTK
623                         { MODEST_STOCK_SORT, "sort mail", 0, 0, NULL },
624                         { MODEST_STOCK_REFRESH, "refresh mail", 0, 0, NULL },
625 #endif /*MODEST_TOOLKIT_GTK*/
626                         { MODEST_STOCK_SPLIT_VIEW, "split view", 0, 0, NULL },
627                         { MODEST_STOCK_MAIL_SEND, "send mail", 0, 0, NULL },
628                         { MODEST_STOCK_NEW_MAIL, "new mail", 0, 0, NULL },
629                         { MODEST_STOCK_REPLY, "reply", 0, 0, NULL },
630                         { MODEST_STOCK_REPLY_ALL, "reply all", 0, 0, NULL },
631                         { MODEST_STOCK_FORWARD, "forward", 0, 0, NULL },
632                         { MODEST_STOCK_DELETE, "delete", 0, 0, NULL },
633                 };
634
635                 static gchar *items_names [] = {
636 #ifndef MODEST_TOOLKIT_GTK
637                         MODEST_TOOLBAR_ICON_SORT,
638                         MODEST_TOOLBAR_ICON_REFRESH,
639 #endif /*MODEST_TOOLKIT_GTK*/
640                         MODEST_TOOLBAR_ICON_SPLIT_VIEW,
641                         MODEST_TOOLBAR_ICON_MAIL_SEND,
642                         MODEST_TOOLBAR_ICON_NEW_MAIL,
643                         MODEST_TOOLBAR_ICON_REPLY,
644                         MODEST_TOOLBAR_ICON_REPLY_ALL,
645                         MODEST_TOOLBAR_ICON_FORWARD,
646                         MODEST_TOOLBAR_ICON_DELETE,
647                 };
648
649                 registered = TRUE;
650
651                 /* Register our stock items */
652                 gtk_stock_add (items, G_N_ELEMENTS (items));
653
654                 /* Add our custom icon factory to the list of defaults */
655                 factory = gtk_icon_factory_new ();
656                 gtk_icon_factory_add_default (factory);
657
658                 current_theme = gtk_icon_theme_get_default ();
659
660                 /* Register icons to accompany stock items */
661                 for (i = 0; i < G_N_ELEMENTS (items); i++) {
662
663 #ifndef MODEST_PLATFORM_GTK
664                         pixbuf = gtk_icon_theme_load_icon (current_theme,
665                                                            items_names[i],
666 #ifdef MODEST_TOOLKIT_HILDON2
667                                                            MODEST_ICON_SIZE_BIG,
668 #else
669                                                            MODEST_ICON_SIZE_SMALL,
670 #endif
671                                                            GTK_ICON_LOOKUP_NO_SVG,
672                                                            NULL);
673 #else
674                         pixbuf = gdk_pixbuf_new_from_file (items_names[i], NULL);
675 #endif
676
677                         if (pixbuf != NULL) {
678                                 GtkIconSet *icon_set;
679
680 #ifndef MODEST_TOOLKIT_HILDON2
681                                 GdkPixbuf *transparent;
682                                 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
683                                 icon_set = gtk_icon_set_new_from_pixbuf (transparent);
684                                 g_object_unref (transparent);
685 #else
686                                 icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
687 #endif
688                                 gtk_icon_factory_add (factory, items[i].stock_id, icon_set);
689                                 gtk_icon_set_unref (icon_set);
690                                 g_object_unref (pixbuf);
691                         }
692                         else
693                                 g_warning ("Modest: %s: failed to load %s icon", __FUNCTION__, items_names[i]);
694                 }
695                 /* Drop our reference to the factory, GTK will hold a reference. */
696                 g_object_unref (factory);
697         }
698 }
699
700
701 static void
702 init_default_settings (ModestConf *conf)
703 {
704         /* Show toolbar keys */
705         if (!modest_conf_key_exists (conf, MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR, NULL))
706                 modest_conf_set_bool (conf, MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR, TRUE, NULL);
707
708         if (!modest_conf_key_exists (conf, MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR_FULLSCREEN, NULL))
709                 modest_conf_set_bool (conf, MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR_FULLSCREEN, TRUE, NULL);
710
711         if (!modest_conf_key_exists (conf, MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR, NULL))
712                 modest_conf_set_bool (conf, MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR, TRUE, NULL);
713
714         if (!modest_conf_key_exists (conf, MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR_FULLSCREEN, NULL))
715                 modest_conf_set_bool (conf, MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR_FULLSCREEN, TRUE, NULL);
716
717         if (!modest_conf_key_exists (conf, MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR, NULL))
718                 modest_conf_set_bool (conf, MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR, TRUE, NULL);
719         
720         if (!modest_conf_key_exists (conf, MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR_FULLSCREEN, NULL))
721                 modest_conf_set_bool (conf, MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR_FULLSCREEN, TRUE, NULL);
722
723         /* Editor keys */
724         if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_CC, NULL))
725                 modest_conf_set_bool (conf, MODEST_CONF_SHOW_CC, FALSE, NULL);
726
727         if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_BCC, NULL))
728                 modest_conf_set_bool (conf, MODEST_CONF_SHOW_BCC, FALSE, NULL);
729
730         /* Global settings */
731         if (!modest_conf_key_exists (conf, MODEST_CONF_AUTO_UPDATE, NULL))
732                 modest_conf_set_bool (conf, MODEST_CONF_AUTO_UPDATE, TRUE, NULL);
733
734         if (!modest_conf_key_exists (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, NULL))
735                 modest_conf_set_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, MODEST_CONNECTED_VIA_WLAN_OR_WIMAX, NULL);
736
737         if (!modest_conf_key_exists (conf, MODEST_CONF_UPDATE_INTERVAL, NULL))
738                 modest_conf_set_int (conf, MODEST_CONF_UPDATE_INTERVAL, MODEST_UPDATE_INTERVAL_15_MIN, NULL);
739
740         if (!modest_conf_key_exists (conf, MODEST_CONF_MSG_SIZE_LIMIT, NULL))
741                 modest_conf_set_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, 1000, NULL);
742
743         if (!modest_conf_key_exists (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, NULL))
744                 modest_conf_set_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, FALSE, NULL);
745
746         if (!modest_conf_key_exists (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, NULL))
747                 modest_conf_set_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, TRUE, NULL);
748
749         if (!modest_conf_key_exists (conf, MODEST_CONF_REPLY_TYPE, NULL))
750                 modest_conf_set_int (conf, MODEST_CONF_REPLY_TYPE, MODEST_TNY_MSG_REPLY_TYPE_QUOTE, NULL);
751
752         if (!modest_conf_key_exists (conf, MODEST_CONF_FETCH_HTML_EXTERNAL_IMAGES, NULL))
753                 modest_conf_set_bool (conf, MODEST_CONF_FETCH_HTML_EXTERNAL_IMAGES, FALSE, NULL);
754 }
755
756
757 /* set the device name -- note this is an initial guess from /etc/hostname
758  * on maemo-device it will most probably be replaced with the Bluetooth device
759  * name later during starting (see maemo/modest-maemo-utils.[ch])
760  */
761 static void
762 init_device_name (ModestConf *conf)
763 {
764         struct utsname name;
765
766         if (uname (&name) == 0) {
767                 modest_conf_set_string (modest_runtime_get_conf(),
768                                         MODEST_CONF_DEVICE_NAME, name.nodename,
769                                         NULL);
770         } else {
771                 modest_conf_set_string (modest_runtime_get_conf(),
772                                         MODEST_CONF_DEVICE_NAME,
773                                         MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME,
774                                         NULL);
775         }
776 }