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