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