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