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