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