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