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