* Added ModestWindowMgr object
[modest] / src / modest-runtime.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-defs.h>
36 #include <modest-singletons.h>
37 #include <widgets/modest-header-view.h>
38 #include <widgets/modest-folder-view.h>
39 #include <modest-tny-platform-factory.h>
40 #include <modest-platform.h>
41 #include <modest-widget-memory.h>
42 #include <modest-widget-memory-priv.h>
43 #include <modest-local-folder-info.h>
44 #include <modest-account-mgr.h>
45 #include <modest-account-mgr-helpers.h>
46 #include <modest-icon-names.h>
47
48 static gboolean init_header_columns (ModestConf *conf, gboolean overwrite);
49 static gboolean init_local_folders  (void);
50 static gboolean init_default_account_maybe  (ModestAccountMgr *acc_mgr);
51 static void     init_i18n (void);
52 static void     init_stock_icons (void);
53 static void     init_debug_g_type (void);
54 static void     init_debug_logging (void);
55 static void     init_test_accounts (ModestAccountMgr *acc_mgr);
56 static void     init_default_settings (ModestConf *conf);
57
58
59 static ModestSingletons *_singletons = NULL;
60
61
62 /*
63  * defaults for the column headers
64  */
65 typedef struct {
66         ModestHeaderViewColumn col;
67         guint                  width;
68 } FolderCols;
69
70 static const FolderCols INBOX_COLUMNS_DETAILS[] = {
71         {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
72         {MODEST_HEADER_VIEW_COLUMN_ATTACH,  40},
73         {MODEST_HEADER_VIEW_COLUMN_FROM,    80},
74         {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80},
75         {MODEST_HEADER_VIEW_COLUMN_RECEIVED_DATE, 60},
76         {MODEST_HEADER_VIEW_COLUMN_SIZE, 50}
77 };
78 static const FolderCols INBOX_COLUMNS_TWOLINES[] = {
79         {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
80         {MODEST_HEADER_VIEW_COLUMN_ATTACH,  40},
81         {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_IN, 200}
82 };
83
84 static const FolderCols OUTBOX_COLUMNS_DETAILS[] = {
85          {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
86          {MODEST_HEADER_VIEW_COLUMN_ATTACH,  40},
87          {MODEST_HEADER_VIEW_COLUMN_TO,    80},
88          {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80},
89          {MODEST_HEADER_VIEW_COLUMN_SENT_DATE, 80},
90          {MODEST_HEADER_VIEW_COLUMN_SIZE, 50}
91 };
92 static const FolderCols OUTBOX_COLUMNS_TWOLINES[] = {
93          {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
94          {MODEST_HEADER_VIEW_COLUMN_ATTACH,  40},
95          {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT,200},
96 };
97
98 #ifdef MODEST_PLATFORM_MAEMO
99 static const TnyFolderType LOCAL_FOLDERS[] = {
100         TNY_FOLDER_TYPE_OUTBOX,
101         TNY_FOLDER_TYPE_DRAFTS,
102         TNY_FOLDER_TYPE_SENT
103 };
104 #else
105 static const TnyFolderType LOCAL_FOLDERS[] = {
106         TNY_FOLDER_TYPE_OUTBOX,
107         TNY_FOLDER_TYPE_DRAFTS,
108         TNY_FOLDER_TYPE_SENT,
109         TNY_FOLDER_TYPE_TRASH,
110         TNY_FOLDER_TYPE_ARCHIVE 
111 };
112 #endif /* MODEST_PLATFORM_MAEMO */
113
114
115
116 gboolean
117 modest_runtime_init (void)
118 {
119         ModestSingletons *my_singletons;
120         gboolean reset;
121         
122         if (_singletons) {
123                 g_printerr ("modest: modest_runtime_init can only be called once\n");
124                 return FALSE;
125         }
126         
127         init_i18n();
128         init_debug_g_type();
129         init_debug_logging();
130
131         g_thread_init(NULL);
132         gdk_threads_init ();
133
134         my_singletons = modest_singletons_new ();
135         if (!my_singletons) {
136                 g_printerr ("modest: failed to initialize singletons\n");
137                 return FALSE;
138         }
139         
140         if (!modest_platform_init()) {
141                 modest_runtime_uninit ();
142                 g_printerr ("modest: failed to run platform-specific initialization\n");
143                 return FALSE;
144         }
145
146         /* based on the debug settings, we decide whether to overwrite old settings */
147         reset = modest_runtime_get_debug_flags () & MODEST_RUNTIME_DEBUG_FACTORY_SETTINGS;
148         if (!init_header_columns(modest_singletons_get_conf (my_singletons), reset)) {
149                 modest_runtime_uninit ();
150                 g_printerr ("modest: failed to init header columns\n");
151                 return FALSE;
152         }
153
154         init_default_settings (modest_singletons_get_conf (my_singletons));
155         
156         if (!init_local_folders()) {
157                 modest_runtime_uninit ();
158                 g_printerr ("modest: failed to init local folders\n");
159                 return FALSE;
160         }
161         
162         if (!init_default_account_maybe(modest_singletons_get_account_mgr (my_singletons))) {
163                 modest_runtime_uninit ();
164                 g_printerr ("modest: failed to init default account\n");
165                 return FALSE;
166         }
167
168         /* create the test accounts if the debug flag for that is set */
169         if (modest_runtime_get_debug_flags () & MODEST_RUNTIME_DEBUG_TINY_TEST_ACCOUNTS)
170                 init_test_accounts (modest_singletons_get_account_mgr (my_singletons)); 
171         
172         /* don't initialize _singletons before all the other init stuff
173          * is done; thus, using any of the singleton stuff before
174          * runtime is fully init'ed  is avoided
175          */
176         _singletons = my_singletons;
177         
178         return TRUE;
179 }
180
181
182 gboolean
183 modest_runtime_init_ui (gint argc, gchar** argv)
184 {
185         if (!gtk_init_check(&argc, &argv)) {
186                 g_printerr ("modest: failed to initialize graphical ui\n");
187                 return FALSE;
188         }
189
190         init_stock_icons ();
191         return TRUE;
192 }
193
194
195 gboolean
196 modest_runtime_uninit (void)
197 {
198         if (_singletons) {
199                 g_object_unref (G_OBJECT(_singletons));
200                 _singletons = NULL;
201         }
202         modest_runtime_verify_object_death (_singletons, "_singletons");
203         
204         return TRUE;
205 }
206
207
208 ModestAccountMgr*
209 modest_runtime_get_account_mgr   (void)
210 {
211         g_return_val_if_fail (_singletons, NULL);
212         return modest_singletons_get_account_mgr (_singletons);
213 }
214
215 ModestTnyAccountStore*
216 modest_runtime_get_account_store   (void)
217 {
218         g_return_val_if_fail (_singletons, NULL);
219         return modest_singletons_get_account_store (_singletons);
220
221 }
222
223 ModestConf*
224 modest_runtime_get_conf (void)
225 {
226         g_return_val_if_fail (_singletons, NULL);
227         return modest_singletons_get_conf (_singletons);
228 }
229
230
231 ModestCacheMgr*
232 modest_runtime_get_cache_mgr (void)
233 {
234         g_return_val_if_fail (_singletons, NULL);
235         return modest_singletons_get_cache_mgr (_singletons);
236 }
237
238
239 ModestMailOperationQueue*
240 modest_runtime_get_mail_operation_queue (void)
241 {
242         g_return_val_if_fail (_singletons, NULL);
243         return modest_singletons_get_mail_operation_queue (_singletons);
244 }
245
246
247
248 TnyDevice*
249 modest_runtime_get_device (void)
250 {
251         g_return_val_if_fail (_singletons, NULL);
252         return modest_singletons_get_device (_singletons);
253 }
254
255
256 TnyPlatformFactory*
257 modest_runtime_get_platform_factory  (void)
258 {
259         g_return_val_if_fail (_singletons, NULL);
260         return modest_singletons_get_platform_factory (_singletons);
261 }
262
263 ModestTnySendQueue*
264 modest_runtime_get_send_queue  (TnyTransportAccount *account)
265 {
266         ModestCacheMgr *cache_mgr;
267         GHashTable     *send_queue_cache;
268         gpointer       orig_key, send_queue;
269         
270         g_return_val_if_fail (_singletons, NULL);
271         g_return_val_if_fail (TNY_IS_TRANSPORT_ACCOUNT(account), NULL);
272
273         cache_mgr = modest_singletons_get_cache_mgr (_singletons);
274         send_queue_cache = modest_cache_mgr_get_cache (cache_mgr,
275                                                        MODEST_CACHE_MGR_CACHE_TYPE_SEND_QUEUE);
276
277         if (!g_hash_table_lookup_extended (send_queue_cache, account, &orig_key, &send_queue)) {
278                 send_queue = (gpointer)modest_tny_send_queue_new (TNY_CAMEL_TRANSPORT_ACCOUNT(account));
279                 g_hash_table_insert (send_queue_cache, account, send_queue);
280         }
281
282         return MODEST_TNY_SEND_QUEUE(send_queue);
283 }
284
285 ModestWindowMgr *
286 modest_runtime_get_window_mgr (void)
287 {
288         g_return_val_if_fail (_singletons, NULL);
289         return modest_singletons_get_window_mgr (_singletons);
290 }
291
292 /* http://primates.ximian.com/~federico/news-2006-04.html#memory-debugging-infrastructure*/
293 ModestRuntimeDebugFlags
294 modest_runtime_get_debug_flags ()
295 {
296         static const GDebugKey debug_keys[] = {
297                 { "abort-on-warning",   MODEST_RUNTIME_DEBUG_ABORT_ON_WARNING },
298                 { "log-actions",        MODEST_RUNTIME_DEBUG_LOG_ACTIONS },
299                 { "debug-objects",      MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS },
300                 { "debug-signals",      MODEST_RUNTIME_DEBUG_DEBUG_SIGNALS },
301                 { "factory-settings",   MODEST_RUNTIME_DEBUG_FACTORY_SETTINGS},
302                 { "tiny-test-accounts", MODEST_RUNTIME_DEBUG_TINY_TEST_ACCOUNTS}
303         };
304         const gchar *str;
305         static ModestRuntimeDebugFlags debug_flags = -1;
306
307         if (debug_flags != -1)
308                 return debug_flags;
309         
310         str = g_getenv (MODEST_DEBUG);
311         
312         if (str != NULL)
313                 debug_flags = g_parse_debug_string (str, debug_keys, G_N_ELEMENTS (debug_keys));
314         else
315                 debug_flags = 0;
316         
317         return debug_flags;
318 }
319
320
321
322
323
324 /* NOTE: the exact details of this format are important, as they
325  * are also used in modest-widget-memory. FIXME: make a shared function
326  * for this with widget-memory
327  */
328 static gboolean
329 save_header_settings (ModestConf *conf, TnyFolderType type,
330                       ModestHeaderViewStyle style,  const FolderCols* cols,
331                       guint col_num, gboolean overwrite)
332 {
333         int i;
334         gchar *key;
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         /* if we're not in overwrite mode, only write stuff it
343          * there was nothing before */
344         if (!overwrite &&  modest_conf_key_exists(conf, key, NULL)) {
345                 g_free (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 ",
353                                         cols[i].col, cols[i].width); 
354
355         modest_conf_set_string (conf, key, str->str, NULL);
356         g_free (key);
357         g_string_free (str, TRUE);
358         
359         return TRUE;
360 }
361
362 /**
363  * modest_init_header_columns:
364  * @overwrite: write the setting, even if it already exists
365  * 
366  * will set defaults for the columns to show for folder,
367  * if there are no such settings yet (in ModestWidgetMemory)
368  * 
369  * Returns: TRUE if succeeded, FALSE in case of error
370  */
371 static gboolean
372 init_header_columns (ModestConf *conf, gboolean overwrite)
373 {
374         int folder_type;
375         
376         for (folder_type = TNY_FOLDER_TYPE_UNKNOWN;
377              folder_type <= TNY_FOLDER_TYPE_CALENDAR; ++folder_type) {          
378                 
379                 switch (folder_type) {
380                 case TNY_FOLDER_TYPE_OUTBOX:
381                 case TNY_FOLDER_TYPE_SENT:
382                 case TNY_FOLDER_TYPE_DRAFTS:
383                 save_header_settings (conf, folder_type,
384                                       MODEST_HEADER_VIEW_STYLE_DETAILS,
385                                       OUTBOX_COLUMNS_DETAILS,
386                                       G_N_ELEMENTS(OUTBOX_COLUMNS_DETAILS),
387                                       overwrite);
388                 save_header_settings (conf, folder_type,
389                                       MODEST_HEADER_VIEW_STYLE_TWOLINES,
390                                       OUTBOX_COLUMNS_TWOLINES,
391                                       G_N_ELEMENTS(OUTBOX_COLUMNS_TWOLINES),
392                                       overwrite);
393                 break;
394
395                 default:
396                 save_header_settings (conf, folder_type,
397                                       MODEST_HEADER_VIEW_STYLE_DETAILS,
398                                       INBOX_COLUMNS_DETAILS,
399                                       G_N_ELEMENTS(INBOX_COLUMNS_DETAILS),
400                                       overwrite);
401                 save_header_settings (conf, folder_type,
402                                       MODEST_HEADER_VIEW_STYLE_TWOLINES,
403                                       INBOX_COLUMNS_TWOLINES,
404                                       G_N_ELEMENTS(INBOX_COLUMNS_TWOLINES),
405                                       overwrite);
406                 };
407         }
408         return TRUE;
409 }
410
411 /**
412  * init_local_folders:
413  * 
414  * create the Local Folders folder under cache, if they
415  * do not exist yet.
416  * 
417  * Returns: TRUE if the folder were already there, or
418  * they were created, FALSE otherwise
419  */
420 static gboolean
421 init_local_folders  (void)
422 {
423         int i;
424         gchar *maildir_path;
425         static const gchar* maildirs[] = {
426                 "cur", "new", "tmp"
427         };
428         
429         maildir_path = modest_local_folder_info_get_maildir_path ();
430
431         for (i = 0; i != G_N_ELEMENTS(LOCAL_FOLDERS); ++i) {
432                 int j;
433                 for (j = 0; j != G_N_ELEMENTS(maildirs); ++j) {
434                         gchar *dir;
435                         dir = g_build_filename (maildir_path,
436                                                 modest_local_folder_info_get_type_name(LOCAL_FOLDERS[i]),
437                                                 maildirs[j],
438                                                 NULL);
439                         if (g_mkdir_with_parents (dir, 0755) < 0) {
440                                 g_printerr ("modest: failed to create %s\n", dir);
441                                 g_free (dir);
442                                 g_free (maildir_path);
443                                 return FALSE;
444                         }
445                         g_free(dir);
446                 }
447         }
448         
449         g_free (maildir_path);
450         return TRUE;
451 }
452
453
454
455 static void
456 free_element (gpointer data, gpointer user_data)
457 {
458         g_free (data);
459 }
460
461
462
463 /**
464  * init_default_account_maybe:
465  *
466  * if there are accounts defined, but there is no default account,
467  * it will be defined.
468  * 
469  * Returns: TRUE if there was a default account already,
470  *  or one has been created or there are no accounts yet,
471  *  returns FALSE in case of error
472  */
473 static gboolean
474 init_default_account_maybe  (ModestAccountMgr *acc_mgr)
475 {
476         GSList *all_accounts = NULL;
477         gchar *default_account;
478         gboolean retval = TRUE;
479         
480         all_accounts = modest_account_mgr_account_names (acc_mgr);
481         if (all_accounts) { /* if there are any accounts, there should be a default one */
482                 default_account = 
483                         modest_account_mgr_get_default_account (acc_mgr);
484                 if (!default_account) {
485                         gchar *first_account;
486                         g_printerr ("modest: no default account defined\n");
487                         first_account = (gchar*)all_accounts->data;
488                         if ((retval = modest_account_mgr_set_default_account (acc_mgr, first_account)))
489                                 g_printerr ("modest: set '%s' as the default account\n",
490                                             first_account);
491                         else
492                                 g_printerr ("modest: failed to set '%s' as the default account\n",
493                                             first_account);
494                         g_free (default_account);
495                 }
496                 g_slist_foreach (all_accounts, free_element, NULL);
497                 g_slist_free    (all_accounts);
498         }
499         return retval;
500 }
501
502
503
504 static void
505 init_debug_g_type (void)
506 {
507         GTypeDebugFlags gflags;
508         ModestRuntimeDebugFlags mflags;
509         
510         gflags = 0;
511         mflags = modest_runtime_get_debug_flags ();
512
513         if (mflags & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS)
514                 gflags |= G_TYPE_DEBUG_OBJECTS;
515         if (mflags & MODEST_RUNTIME_DEBUG_DEBUG_SIGNALS)
516                 gflags |= G_TYPE_DEBUG_SIGNALS;
517
518         g_type_init_with_debug_flags (gflags);
519 }
520
521 static void
522 init_debug_logging (void)
523 {
524         ModestRuntimeDebugFlags mflags;
525         mflags = modest_runtime_get_debug_flags ();
526         
527         if (mflags & MODEST_RUNTIME_DEBUG_ABORT_ON_WARNING)
528                 g_log_set_always_fatal (G_LOG_LEVEL_ERROR |
529                                         G_LOG_LEVEL_CRITICAL |
530                                         G_LOG_LEVEL_WARNING);
531 }
532
533
534 static void
535 init_i18n (void)
536 {
537         /* Setup gettext, to use our .po files: */
538         /* GETTEXT_PACKAGE and MODEST_LOCALE_DIR are defined in config.h */
539         bindtextdomain (GETTEXT_PACKAGE, MODEST_LOCALE_DIR);
540         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
541         textdomain (GETTEXT_PACKAGE);
542 }
543
544
545 /* 
546  *  This function registers our custom toolbar icons, so they can be
547  *  themed. The idea of this function was taken from the gtk-demo
548  */
549 static void
550 init_stock_icons (void)
551 {
552         static gboolean registered = FALSE;
553   
554         if (!registered) {
555                 GtkIconTheme *current_theme;
556                 GdkPixbuf *pixbuf;
557                 GtkIconFactory *factory;
558                 gint i;
559
560                 static GtkStockItem items[] = {
561 #ifdef MODEST_PLATFORM_MAEMO
562                         { MODEST_STOCK_SPLIT_VIEW, "split view", 0, 0, NULL },
563                         { MODEST_STOCK_SORT, "sort mail", 0, 0, NULL },
564                         { MODEST_STOCK_REFRESH, "refresh mail", 0, 0, NULL },
565 #endif /*MODEST_PLATFORM_MAEMO*/
566                         { MODEST_STOCK_MAIL_SEND, "send mail", 0, 0, NULL },
567                         { MODEST_STOCK_NEW_MAIL, "new mail", 0, 0, NULL },
568 /*                      { MODEST_STOCK_SEND_RECEIVE, "send receive", 0, 0, NULL },  */
569                         { MODEST_STOCK_REPLY, "reply", 0, 0, NULL },
570                         { MODEST_STOCK_REPLY_ALL, "reply all", 0, 0, NULL },
571                         { MODEST_STOCK_FORWARD, "forward", 0, 0, NULL },
572                         { MODEST_STOCK_DELETE, "delete", 0, 0, NULL }, 
573 /*                      { MODEST_STOCK_NEXT, "next", 0, 0, NULL }, */
574 /*                      { MODEST_STOCK_PREV, "prev", 0, 0, NULL }, */
575 /*                      { MODEST_STOCK_STOP, "stop", 0, 0, NULL } */
576                 };
577       
578                 static gchar *items_names [] = {
579 #ifdef MODEST_PLATFORM_MAEMO
580                         MODEST_TOOLBAR_ICON_SPLIT_VIEW,
581                         MODEST_TOOLBAR_ICON_SORT,
582                         MODEST_TOOLBAR_ICON_REFRESH,
583 #endif /*MODEST_PLATFORM_MAEMO*/
584                         MODEST_TOOLBAR_ICON_MAIL_SEND,
585                         MODEST_TOOLBAR_ICON_NEW_MAIL,
586 /*                      MODEST_TOOLBAR_ICON_SEND_RECEIVE,  */
587                         MODEST_TOOLBAR_ICON_REPLY,      
588                         MODEST_TOOLBAR_ICON_REPLY_ALL,
589                         MODEST_TOOLBAR_ICON_FORWARD,
590                         MODEST_TOOLBAR_ICON_DELETE, 
591 /*                      MODEST_TOOLBAR_ICON_NEXT, */
592 /*                      MODEST_TOOLBAR_ICON_PREV, */
593 /*                      MODEST_TOOLBAR_ICON_STOP */
594                         MODEST_TOOLBAR_ICON_FORMAT_BULLETS,
595                 };
596
597                 registered = TRUE;
598
599                 /* Register our stock items */
600                 gtk_stock_add (items, G_N_ELEMENTS (items));
601       
602                 /* Add our custom icon factory to the list of defaults */
603                 factory = gtk_icon_factory_new ();
604                 gtk_icon_factory_add_default (factory);
605
606                 current_theme = gtk_icon_theme_get_default ();
607
608                 /* Register icons to accompany stock items */
609                 for (i = 0; i < G_N_ELEMENTS (items); i++) {
610
611 #ifdef MODEST_PLATFORM_MAEMO  /* MODES_PLATFORM_ID: 1 ==> gnome, 2==> maemo */ 
612                         pixbuf = gtk_icon_theme_load_icon (current_theme,
613                                                            items_names[i],
614                                                            26,
615                                                            GTK_ICON_LOOKUP_NO_SVG,
616                                                            NULL);
617 #else
618                         pixbuf = gdk_pixbuf_new_from_file (items_names[i], NULL);
619 #endif
620
621                         if (pixbuf != NULL) {
622                                 GtkIconSet *icon_set;
623                                 GdkPixbuf *transparent;
624
625                                 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
626                                 icon_set = gtk_icon_set_new_from_pixbuf (transparent);
627                                 gtk_icon_factory_add (factory, items[i].stock_id, icon_set);
628                                 gtk_icon_set_unref (icon_set);
629                                 g_object_unref (pixbuf);
630                                 g_object_unref (transparent);
631                         }
632                         else
633                                 g_warning ("failed to load %s icon", items_names[i]);
634                 }
635                 /* Drop our reference to the factory, GTK will hold a reference. */
636                 g_object_unref (factory);
637         }
638 }
639
640
641 static void
642 init_test_accounts (ModestAccountMgr *acc_mgr)
643 {
644         const gchar *imap_test = "TnyImapTest";
645         const gchar *pop_test  = "TnyPopTest";
646         const gchar *smtp_test = "TnySmtpTest";
647
648         if (!modest_account_mgr_account_exists (acc_mgr, imap_test, FALSE)) 
649                 modest_account_mgr_add_server_account (acc_mgr,
650                                                        imap_test,
651                                                        "imap2.tinymail.org",
652                                                        "tnytest", "tnytest",
653                                                        MODEST_PROTOCOL_STORE_IMAP,
654                                                        MODEST_PROTOCOL_SECURITY_NONE,
655                                                        MODEST_PROTOCOL_AUTH_NONE);
656         if (!modest_account_mgr_account_exists (acc_mgr, pop_test, FALSE)) 
657                 modest_account_mgr_add_server_account (acc_mgr,
658                                                        pop_test,
659                                                        "imap2.tinymail.org",
660                                                        "tnytest", "tnytest",
661                                                        MODEST_PROTOCOL_STORE_POP,
662                                                        MODEST_PROTOCOL_SECURITY_NONE,
663                                                        MODEST_PROTOCOL_AUTH_NONE);
664         if (!modest_account_mgr_account_exists (acc_mgr, smtp_test, FALSE)) 
665                 modest_account_mgr_add_server_account (acc_mgr,
666                                                        smtp_test,
667                                                        "127.0.0.1",
668                                                        NULL,NULL,
669                                                        MODEST_PROTOCOL_TRANSPORT_SMTP,
670                                                        MODEST_PROTOCOL_SECURITY_NONE,
671                                                        MODEST_PROTOCOL_AUTH_NONE);
672         
673         modest_account_mgr_add_account (acc_mgr, imap_test, imap_test, smtp_test);
674         modest_account_mgr_add_account (acc_mgr,  pop_test, pop_test,  smtp_test);
675 }
676
677
678 static void
679 init_default_settings (ModestConf *conf)
680 {
681         if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_TOOLBAR, NULL))
682                 modest_conf_set_bool (conf, MODEST_CONF_SHOW_TOOLBAR, TRUE, NULL);
683         
684         if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_CC, NULL))
685                 modest_conf_set_bool (conf, MODEST_CONF_SHOW_CC, TRUE, NULL);
686
687         if (!modest_conf_key_exists (conf, MODEST_CONF_SHOW_BCC, NULL))
688                 modest_conf_set_bool (conf, MODEST_CONF_SHOW_BCC, FALSE, NULL);
689
690         if (!modest_conf_key_exists (conf, MODEST_CONF_CONNECT_AT_STARTUP, NULL))
691                 modest_conf_set_bool (conf, MODEST_CONF_CONNECT_AT_STARTUP, TRUE, NULL);
692
693 }