* use the slice allocator
[modest] / src / modest-ui.c
index 163e7de..d236c4c 100644 (file)
@@ -33,6 +33,7 @@
 
 #include <glib/gi18n.h>
 #include <string.h>
+#include <modest-runtime.h>
 #include "modest-ui-priv.h"
 #include "modest-ui.h"
 #include "modest-ui-actions.h"
@@ -98,6 +99,14 @@ static void     reply_forward          (GtkWidget *widget,
 static gchar*   ask_for_folder_name    (GtkWindow *parent_window,
                                        const gchar *title);
 
+static void     _modest_ui_actions_on_connection_changed    (TnyDevice *device,
+                                                            gboolean online,
+                                                            ModestUI *modest_ui);
+
+
+static void     _modest_ui_actions_on_accounts_reloaded     (TnyAccountStore *store, 
+                                                            gpointer user_data);
+
 GType
 modest_ui_get_type (void)
 {
@@ -144,9 +153,7 @@ modest_ui_init (ModestUI *obj)
 
        priv = MODEST_UI_GET_PRIVATE(obj);
 
-       priv->widget_factory = NULL;
        priv->main_window    = NULL;
-       priv->account_store  = NULL;
 }
 
 
@@ -155,50 +162,26 @@ modest_ui_finalize (GObject *obj)
 {
        ModestUIPrivate *priv = MODEST_UI_GET_PRIVATE(obj);
        
-       if (priv->widget_factory) {
-               g_object_unref (G_OBJECT(priv->widget_factory));
-               priv->widget_factory = NULL;
-       }
-
        if (priv->ui_manager) {
                g_object_unref (G_OBJECT(priv->ui_manager));
                priv->ui_manager = NULL;
        }
 
+       priv->main_window = NULL;
+
        G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
 
 
 ModestUI*
-modest_ui_new (TnyAccountStore *account_store)
+modest_ui_new (void)
 {
        GObject *obj;
        ModestUIPrivate *priv;
-       TnyPlatformFactory *fact;
-       ModestAccountMgr *account_mgr;
 
        obj  = g_object_new(MODEST_TYPE_UI, NULL);
        priv = MODEST_UI_GET_PRIVATE(obj);
-
-       /* Get the platform-dependent instances */
-       fact = modest_tny_platform_factory_get_instance ();
        
-       priv->account_store = account_store;
-
-       account_mgr = modest_tny_platform_factory_get_account_mgr_instance
-               (MODEST_TNY_PLATFORM_FACTORY(fact));
-       if (!account_mgr) {
-               g_printerr ("modest: could not create ModestAccountMgr instance\n");
-               g_object_unref (obj);
-               return NULL;
-        }
-
-       priv->widget_factory = modest_widget_factory_new ();
-       if (!priv->widget_factory) {
-               g_printerr ("modest: could not initialize widget factory\n");
-               return NULL;
-       }
-
        /* Register our own icons as stock icons in order to
           use them with the UI manager */
        register_stock_icons ();
@@ -224,11 +207,7 @@ modest_ui_main_window (ModestUI *self)
        priv = MODEST_UI_GET_PRIVATE(self);
 
        if (!priv->main_window) {
-
-               /* Create main window */
-               priv->main_window = modest_main_window_new (priv->widget_factory,
-                                                           priv->account_store);
-
+               priv->main_window = modest_main_window_new ();
                connect_signals (self);
        }
                
@@ -248,9 +227,7 @@ modest_ui_edit_window (ModestUI *self, ModestEditType edit_type)
        priv = MODEST_UI_GET_PRIVATE(self);
 
        /* Create window */
-       edit_window = modest_edit_msg_window_new (priv->widget_factory, 
-                                                 priv->account_store,
-                                                 edit_type);
+       edit_window = modest_edit_msg_window_new (edit_type);
        
        /* Connect Edit Window signals */
 /*     connect_edit_window_signals (self); */
@@ -337,19 +314,24 @@ static void
 connect_signals (ModestUI *self)
 {
        TnyDevice *device;
+       TnyAccountStore *account_store;
        ModestUIPrivate *priv;
        ModestFolderView *folder_view;
        ModestHeaderView *header_view;
        ModestMsgView *msg_view;
        GtkWidget *toggle;
+       ModestWidgetFactory *widget_factory;
        
        priv = MODEST_UI_GET_PRIVATE(self);
 
-       folder_view = modest_widget_factory_get_folder_view (priv->widget_factory);
-       header_view = modest_widget_factory_get_header_view (priv->widget_factory);
-       msg_view    = modest_widget_factory_get_msg_preview (priv->widget_factory);
-       toggle      = modest_widget_factory_get_online_toggle (priv->widget_factory);
-       device      = tny_account_store_get_device (priv->account_store);
+       widget_factory = modest_runtime_get_widget_factory (); 
+       
+       folder_view   = modest_widget_factory_get_folder_view (widget_factory);
+       header_view   = modest_widget_factory_get_header_view (widget_factory);
+       msg_view      = modest_widget_factory_get_msg_preview (widget_factory);
+       toggle        = modest_widget_factory_get_online_toggle (widget_factory);
+       account_store = TNY_ACCOUNT_STORE(modest_runtime_get_account_store());
+       device        = tny_account_store_get_device (account_store);
 
        /* folder view */
        g_signal_connect (G_OBJECT(folder_view), "folder_selection_changed",
@@ -376,13 +358,20 @@ connect_signals (ModestUI *self)
                          G_CALLBACK(_modest_ui_actions_on_msg_attachment_clicked), 
                          priv->main_window);
 
+       /* Account store */
+       g_signal_connect (G_OBJECT (account_store), "accounts_reloaded",
+                         G_CALLBACK (_modest_ui_actions_on_accounts_reloaded),
+                         priv->main_window);
+
        /* Device */
        g_signal_connect (G_OBJECT(device), "connection_changed",
                          G_CALLBACK(_modest_ui_actions_on_connection_changed), 
-                         priv->main_window);
-       g_signal_connect (G_OBJECT(toggle), "toggled",
-                         G_CALLBACK(_modest_ui_actions_on_online_toggle_toggled),
-                         priv->main_window);
+                         self);
+
+       priv->toggle_button_signal=
+               g_signal_connect (G_OBJECT(toggle), "toggled",
+                                 G_CALLBACK(_modest_ui_actions_on_online_toggle_toggled),
+                                 priv->main_window);
                
        /* Destroy window */
        g_signal_connect (G_OBJECT(priv->main_window), 
@@ -390,11 +379,10 @@ connect_signals (ModestUI *self)
                          G_CALLBACK(on_main_window_destroy), 
                          NULL);
 
-
        /* Init toggle in correct state */
        _modest_ui_actions_on_connection_changed (device,
                                                 tny_device_is_online (device),
-                                                MODEST_MAIN_WINDOW (priv->main_window));
+                                                self);
 }
 
 
@@ -438,10 +426,9 @@ _modest_ui_actions_on_delete (GtkWidget *widget,
        TnyIterator *iter;
        GtkTreeModel *model;
 
-       widget_factory = modest_window_get_widget_factory (MODEST_WINDOW (main_window));
+       widget_factory = modest_runtime_get_widget_factory ();
        header_view = modest_widget_factory_get_header_view (widget_factory);
        header_list = modest_header_view_get_selected_headers (header_view);
-       g_object_unref (G_OBJECT(widget_factory));
        
        if (header_list) {
                iter = tny_list_create_iterator (header_list);
@@ -492,14 +479,10 @@ _modest_ui_actions_on_quit (GtkWidget *widget,
 
 void
 _modest_ui_actions_on_accounts (GtkWidget *widget, 
-                              ModestMainWindow *main_window)
+                               ModestMainWindow *main_window)
 {
        GtkWidget *account_win;
-       ModestWidgetFactory *widget_factory;
-
-       widget_factory = modest_window_get_widget_factory (MODEST_WINDOW (main_window));
-       account_win = modest_account_view_window_new (widget_factory);
-       g_object_unref (G_OBJECT(widget_factory));
+       account_win = modest_account_view_window_new (modest_runtime_get_widget_factory());
 
        gtk_window_set_transient_for (GTK_WINDOW (account_win),
                                      GTK_WINDOW (main_window));
@@ -512,27 +495,14 @@ _modest_ui_actions_on_new_msg (GtkWidget *widget,
                               ModestMainWindow *main_window)
 {
        ModestWindow *msg_win;
-       ModestWidgetFactory *widget_factory;
-       TnyAccountStore *account_store;
-
-       widget_factory = modest_window_get_widget_factory (MODEST_WINDOW (main_window));
-       account_store = modest_window_get_account_store (MODEST_WINDOW (main_window));
-       msg_win = modest_edit_msg_window_new (widget_factory, 
-                                             account_store,
-                                             MODEST_EDIT_TYPE_NEW);
-       g_object_unref (G_OBJECT (widget_factory));
-       g_object_unref (G_OBJECT (account_store));
-
+       msg_win = modest_edit_msg_window_new (MODEST_EDIT_TYPE_NEW);
        gtk_widget_show_all (GTK_WIDGET (msg_win));
 }
 
 static void
 reply_forward_func (gpointer data, gpointer user_data)
 {
-       ModestWidgetFactory *widget_factory;
-       //TnyHeader *new_header;
        TnyMsg *msg, *new_msg;
-       TnyAccountStore *account_store;
        GetMsgAsyncHelper *helper;
        ReplyForwardHelper *rf_helper;
        ModestWindow *msg_win;
@@ -570,21 +540,9 @@ reply_forward_func (gpointer data, gpointer user_data)
                g_warning ("Unable to create a message");
                goto cleanup;
        }
-
-/*     /\* Set from *\/ */
-/*     new_header = tny_msg_get_header (new_msg); */
-/*     tny_header_set_from (new_header, rf_helper->from); */
-/*     g_object_unref (G_OBJECT (new_header)); */
                
        /* Show edit window */
-       widget_factory = modest_window_get_widget_factory (MODEST_WINDOW (helper->main_window));
-       account_store = modest_window_get_account_store (MODEST_WINDOW (helper->main_window));
-       msg_win = modest_edit_msg_window_new (widget_factory, 
-                                             account_store,
-                                             MODEST_EDIT_TYPE_NEW);
-       g_object_unref (G_OBJECT (widget_factory));
-       g_object_unref (G_OBJECT (account_store));
-
+       msg_win = modest_edit_msg_window_new (MODEST_EDIT_TYPE_NEW);
        modest_edit_msg_window_set_msg (MODEST_EDIT_MSG_WINDOW (msg_win),
                                        new_msg);
        gtk_widget_show_all (GTK_WIDGET (msg_win));
@@ -607,23 +565,18 @@ reply_forward (GtkWidget *widget,
 {
        ModestHeaderView *header_view;
        ModestAccountMgr *account_mgr;
-       ModestWidgetFactory *widget_factory;
        TnyList *header_list;
        guint reply_forward_type;
        ModestConf *conf;       
        ModestAccountData *default_account_data;
-       TnyPlatformFactory *plat_factory;
        TnyHeader *header;
        TnyFolder *folder;
        gchar *from, *key, *default_account_name;
        GetMsgAsyncHelper *helper;
        ReplyForwardHelper *rf_helper;
 
-       /* Get ModestConf */
-       plat_factory = modest_tny_platform_factory_get_instance ();
-       conf = modest_tny_platform_factory_get_conf_instance
-               (MODEST_TNY_PLATFORM_FACTORY(plat_factory));
-
+       conf = modest_runtime_get_conf ();
+       
        /* Get reply or forward type */
        key = g_strdup_printf ("%s/%s", MODEST_CONF_NAMESPACE, 
                               (action == ACTION_FORWARD) ? MODEST_CONF_FORWARD_TYPE : MODEST_CONF_REPLY_TYPE);
@@ -631,11 +584,8 @@ reply_forward (GtkWidget *widget,
        g_free (key);
 
        /* Get the list of headers */
-       widget_factory = modest_window_get_widget_factory (MODEST_WINDOW (main_window));
-       header_view = modest_widget_factory_get_header_view (widget_factory);
-       header_list = modest_header_view_get_selected_headers (header_view);
-       g_object_unref (G_OBJECT(widget_factory));
-
+       header_view = modest_widget_factory_get_header_view (modest_runtime_get_widget_factory());
+       header_list = modest_header_view_get_selected_headers (header_view);    
        if (!header_list)
                return;
 
@@ -643,8 +593,7 @@ reply_forward (GtkWidget *widget,
           same folder and that we reply all of them from the
           same account. In fact the interface currently only
           allows single selection */
-       account_mgr = modest_tny_platform_factory_get_account_mgr_instance
-               (MODEST_TNY_PLATFORM_FACTORY(plat_factory));
+       account_mgr = modest_runtime_get_account_mgr();
        default_account_name = modest_account_mgr_get_default_account (account_mgr);
        default_account_data = 
                modest_account_mgr_get_account_data (account_mgr,
@@ -672,6 +621,7 @@ reply_forward (GtkWidget *widget,
        tny_folder_get_msg_async (folder, header, get_msg_cb, helper);
        
        /* Clean */
+       g_object_unref (G_OBJECT (header));
        g_object_unref (G_OBJECT (folder));
 }
 
@@ -701,11 +651,9 @@ _modest_ui_actions_on_next (GtkWidget *widget,
                           ModestMainWindow *main_window)
 {
        ModestHeaderView *header_view;
-       ModestWidgetFactory *widget_factory;
 
-       widget_factory = modest_window_get_widget_factory (MODEST_WINDOW (main_window));
-       header_view = modest_widget_factory_get_header_view (widget_factory);
-       g_object_unref (G_OBJECT(widget_factory));
+       header_view = modest_widget_factory_get_header_view
+               (modest_runtime_get_widget_factory());
 
        modest_header_view_select_next (header_view);
 }
@@ -714,19 +662,14 @@ void
 _modest_ui_actions_toggle_view (GtkWidget *widget,
                                ModestMainWindow *main_window)
 {
-       ModestHeaderView *header_view;
-       ModestWidgetFactory *widget_factory;
        ModestConf *conf;
-       TnyPlatformFactory *plat_factory;
+       ModestHeaderView *header_view;
 
-       /* Get ModestConf */
-       plat_factory = modest_tny_platform_factory_get_instance ();
-       conf = modest_tny_platform_factory_get_conf_instance
-               (MODEST_TNY_PLATFORM_FACTORY(plat_factory));
-       widget_factory = modest_window_get_widget_factory (MODEST_WINDOW (main_window));
-       header_view = modest_widget_factory_get_header_view (widget_factory);
-       g_object_unref (G_OBJECT(widget_factory));
+       header_view = modest_widget_factory_get_header_view
+               (modest_runtime_get_widget_factory());
 
+       conf = modest_runtime_get_conf ();
+       
        /* what is saved/restored is depending on the style; thus; we save with
         * old style, then update the style, and restore for this new style*/
        modest_widget_memory_save (conf, G_OBJECT(header_view), "header-view");
@@ -748,7 +691,6 @@ static void
 read_msg_func (gpointer data, gpointer user_data)
 {
        ModestMsgView *msg_view;
-       ModestWidgetFactory *widget_factory;
        TnyMsg *msg;
        TnyHeader *header;
        GetMsgAsyncHelper *helper;
@@ -764,9 +706,8 @@ read_msg_func (gpointer data, gpointer user_data)
        g_object_unref (G_OBJECT (header));
 
        /* Set message on msg view */
-       widget_factory = modest_window_get_widget_factory (MODEST_WINDOW (helper->main_window));
-       msg_view = modest_widget_factory_get_msg_preview (widget_factory);
-       g_object_unref (G_OBJECT(widget_factory));
+       msg_view = modest_widget_factory_get_msg_preview
+               (modest_runtime_get_widget_factory());
        modest_msg_view_set_message (msg_view, msg);
 }
 
@@ -786,22 +727,16 @@ get_msg_cb (TnyFolder *folder, TnyMsg *msg, GError **err, gpointer user_data)
 
        helper = (GetMsgAsyncHelper *) user_data;
 
-       if (*err && ((*err)->code == TNY_FOLDER_ERROR_GET_MSG)) {
+       if ((*err && ((*err)->code == TNY_FOLDER_ERROR_GET_MSG)) || !msg) {
                ModestHeaderView *header_view;
-               ModestWidgetFactory *widget_factory;
-
-               widget_factory = modest_window_get_widget_factory (MODEST_WINDOW (helper->main_window));
-               header_view = modest_widget_factory_get_header_view (widget_factory);
-               g_object_unref (G_OBJECT (widget_factory));
+               header_view = modest_widget_factory_get_header_view
+                       (modest_runtime_get_widget_factory());
                _modest_ui_actions_on_item_not_found (header_view,
                                                      MODEST_ITEM_TYPE_MESSAGE,
                                                      helper->main_window);
                return;
        }
 
-       if (!msg)
-               return;
-
        /* Call user function */
        helper->func (msg, user_data);
 
@@ -814,10 +749,13 @@ get_msg_cb (TnyFolder *folder, TnyMsg *msg, GError **err, gpointer user_data)
                g_object_unref (G_OBJECT (headers));
                g_object_unref (G_OBJECT (helper->iter));
                g_slice_free (GetMsgAsyncHelper, helper);
-       } else
-               tny_folder_get_msg_async (folder, 
-                                         TNY_HEADER (tny_iterator_get_current (helper->iter)), 
+       } else {
+               TnyHeader *header;
+               header = TNY_HEADER (tny_iterator_get_current (helper->iter)); 
+               tny_folder_get_msg_async (folder, header,                         
                                          get_msg_cb, helper);
+               g_object_unref (G_OBJECT(header));
+       }
 }
 
 void 
@@ -832,9 +770,8 @@ _modest_ui_actions_on_header_selected (ModestHeaderView *folder_view,
        /* when there's no header, clear the msgview */
        if (!header) {
                ModestMsgView *msg_view;
-               ModestWidgetFactory *widget_factory;
-               widget_factory = modest_window_get_widget_factory (MODEST_WINDOW (main_window));
-               msg_view       = modest_widget_factory_get_msg_preview (widget_factory);
+               msg_view       = modest_widget_factory_get_msg_preview
+                       (modest_runtime_get_widget_factory());
                modest_msg_view_set_message (msg_view, NULL);
                return;
        }
@@ -866,26 +803,21 @@ _modest_ui_actions_on_folder_selection_changed (ModestFolderView *folder_view,
                                               ModestMainWindow *main_window)
 {
        GtkLabel *folder_info_label;
-       TnyPlatformFactory *factory;
        gchar *txt;     
        ModestConf *conf;
        ModestHeaderView *header_view;
-       ModestWidgetFactory *widget_factory;
 
-       widget_factory = modest_window_get_widget_factory (MODEST_WINDOW (main_window));
        folder_info_label = 
-               GTK_LABEL (modest_widget_factory_get_folder_info_label (widget_factory));
+               GTK_LABEL (modest_widget_factory_get_folder_info_label
+                          (modest_runtime_get_widget_factory()));
 
        if (!folder) {
                gtk_label_set_label (GTK_LABEL(folder_info_label), "");
                return;
        }
-
-       factory = modest_tny_platform_factory_get_instance ();
-       header_view = modest_widget_factory_get_header_view (widget_factory);
-       conf = modest_tny_platform_factory_get_conf_instance
-               (MODEST_TNY_PLATFORM_FACTORY(factory));
-       g_object_unref (G_OBJECT (widget_factory));
+       
+       header_view = modest_widget_factory_get_header_view (modest_runtime_get_widget_factory());
+       conf = modest_runtime_get_conf ();
 
        if (!selected) { /* the folder was unselected; save it's settings  */
                modest_widget_memory_save (conf, G_OBJECT (header_view),
@@ -969,38 +901,50 @@ statusbar_push (ModestWidgetFactory *factory, guint context_id, const gchar *msg
 }
 /****************************************************************************/
 
-void
+static void
 _modest_ui_actions_on_connection_changed (TnyDevice *device, 
-                                        gboolean online,
-                                        ModestMainWindow *main_window)
+                                         gboolean online,
+                                         ModestUI *self)
 {
        GtkWidget *online_toggle;
        ModestHeaderView *header_view;
        ModestWidgetFactory *widget_factory;
+       ModestUIPrivate *priv;
+       GtkWidget *icon;
+       const gchar *icon_name;
 
-       g_return_if_fail (main_window);
-       
-       widget_factory = modest_window_get_widget_factory (MODEST_WINDOW (main_window));
+       g_return_if_fail (device);
+       g_return_if_fail (self);
+
+       priv = MODEST_UI_GET_PRIVATE (self);
+
+       icon_name = online ? GTK_STOCK_CONNECT : GTK_STOCK_DISCONNECT;
+       icon      = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
+
+       widget_factory = modest_runtime_get_widget_factory ();
        header_view   = modest_widget_factory_get_header_view (widget_factory);
        online_toggle = modest_widget_factory_get_online_toggle (widget_factory);
 
-       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(online_toggle),
-                                     online);
-       gtk_button_set_label (GTK_BUTTON(online_toggle),
-                             online ? _("Online") : _("Offline"));
+       /* Block handlers in order to avoid unnecessary calls */
+       g_signal_handler_block (G_OBJECT (online_toggle), priv->toggle_button_signal);
+       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(online_toggle), online);
+       g_signal_handler_unblock (G_OBJECT (online_toggle), priv->toggle_button_signal);
 
+       gtk_button_set_image (GTK_BUTTON(online_toggle), icon);
        statusbar_push (widget_factory, 0, 
                        online ? _("Modest went online") : _("Modest went offline"));
-       g_object_unref (G_OBJECT (widget_factory));
-
+       
        /* If Modest has became online and the header view has a
           header selected then show it */
-       if (online) {
-               GtkTreeSelection *selected;
+       /* FIXME: there is a race condition if some account needs to
+          ask the user for a password */
 
-               selected = gtk_tree_view_get_selection (GTK_TREE_VIEW (header_view));
-               _modest_header_view_change_selection (selected, header_view);
-       }
+/*     if (online) { */
+/*             GtkTreeSelection *selected; */
+
+/*             selected = gtk_tree_view_get_selection (GTK_TREE_VIEW (header_view)); */
+/*             _modest_header_view_change_selection (selected, header_view); */
+/*     } */
 }
 
 void
@@ -1009,14 +953,9 @@ _modest_ui_actions_on_online_toggle_toggled (GtkToggleButton *toggle,
 {
        gboolean online;
        TnyDevice *device;
-       TnyPlatformFactory *factory;
-       TnyAccountStore *account_store;
 
-       /* Get device. Do not ask the platform factory for it, because
-          it returns always a new one */
-       factory = modest_tny_platform_factory_get_instance ();
-       account_store = tny_platform_factory_new_account_store (factory);
-       device = tny_account_store_get_device (account_store);
+       device = tny_account_store_get_device
+               (TNY_ACCOUNT_STORE(modest_runtime_get_account_store()));
 
        online  = gtk_toggle_button_get_active (toggle);
 
@@ -1035,15 +974,13 @@ _modest_ui_actions_on_item_not_found (ModestHeaderView *header_view,
        gchar *txt, *item;
        gboolean online;
        TnyDevice *device;
-       TnyPlatformFactory *factory;
        TnyAccountStore *account_store;
 
        item = (type == MODEST_ITEM_TYPE_FOLDER) ? "folder" : "message";
 
        /* Get device. Do not ask the platform factory for it, because
           it returns always a new one */
-       factory = modest_tny_platform_factory_get_instance ();
-       account_store = tny_platform_factory_new_account_store (factory);
+       account_store = TNY_ACCOUNT_STORE (modest_runtime_get_account_store ());
        device = tny_account_store_get_device (account_store);
 
        if (g_main_depth > 0)   
@@ -1097,12 +1034,10 @@ _modest_ui_actions_on_header_status_update (ModestHeaderView *header_view,
                                            ModestMainWindow *main_window)
 {
        GtkWidget *progress_bar;
-       ModestWidgetFactory *widget_factory;
        char* txt;
        
-       widget_factory = modest_window_get_widget_factory (MODEST_WINDOW (main_window));
-       progress_bar = modest_widget_factory_get_progress_bar (widget_factory);
-
+       progress_bar = modest_widget_factory_get_progress_bar
+               (modest_runtime_get_widget_factory());
        if (total != 0)
                gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR(progress_bar),
                                               (gdouble)num/(gdouble)total);
@@ -1113,10 +1048,7 @@ _modest_ui_actions_on_header_status_update (ModestHeaderView *header_view,
        gtk_progress_bar_set_text (GTK_PROGRESS_BAR(progress_bar), txt);
        g_free (txt);
        
-       statusbar_push (widget_factory, 0, msg);
-       
-       /* Free */
-       g_object_unref (G_OBJECT (widget_factory));
+       statusbar_push (modest_runtime_get_widget_factory(), 0, msg);
 }
 
 
@@ -1126,11 +1058,7 @@ _modest_ui_actions_on_msg_link_hover (ModestMsgView *msgview,
                                      const gchar* link,
                                      ModestMainWindow *main_window)
 {
-       ModestWidgetFactory *widget_factory;
-
-       widget_factory = modest_window_get_widget_factory (MODEST_WINDOW (main_window));
-       statusbar_push (widget_factory, 0, link);
-       g_object_unref (G_OBJECT (widget_factory));     
+       statusbar_push (modest_runtime_get_widget_factory(), 0, link);
 
        /* TODO: do something */
 }      
@@ -1142,14 +1070,9 @@ _modest_ui_actions_on_msg_link_clicked (ModestMsgView *msgview,
                                        ModestMainWindow *main_window)
 {
        gchar *msg;
-       ModestWidgetFactory *widget_factory;
 
        msg = g_strdup_printf (_("Opening %s..."), link);
-       widget_factory = modest_window_get_widget_factory (MODEST_WINDOW (main_window));
-       statusbar_push (widget_factory, 0, msg);
-
-       g_object_unref (G_OBJECT (widget_factory));     
-
+       statusbar_push (modest_runtime_get_widget_factory(), 0, msg);
        g_free (msg);
 
        /* TODO: do something */
@@ -1161,14 +1084,11 @@ _modest_ui_actions_on_msg_attachment_clicked (ModestMsgView *msgview,
                                              ModestMainWindow *main_window)
 {
        gchar *msg;
-       ModestWidgetFactory *widget_factory;
        
        msg = g_strdup_printf (_("Opening attachment %d..."), index);
-       widget_factory = modest_window_get_widget_factory (MODEST_WINDOW (main_window));
-       statusbar_push (widget_factory, 0, msg);
+       statusbar_push (modest_runtime_get_widget_factory(), 0, msg);
        
        g_free (msg);
-       g_object_unref (G_OBJECT (widget_factory));
        /* TODO: do something */
 }
 
@@ -1179,44 +1099,38 @@ _modest_ui_actions_on_send (GtkWidget *widget,
        TnyTransportAccount *transport_account;
        ModestMailOperation *mail_operation;
        MsgData *data;
-
+       gchar *account_name, *from;
+       ModestAccountMgr *account_mgr;
+       
+       
        data = modest_edit_msg_window_get_msg_data (edit_window);
 
        /* FIXME: Code added just for testing. The final version will
           use the send queue provided by tinymail and some
           classifier */
-       {
-               TnyList *accounts;
-               TnyIterator *iter;
-               TnyAccountStore *account_store;
-
-               accounts = TNY_LIST(tny_simple_list_new ());
-               account_store = modest_window_get_account_store (MODEST_WINDOW (edit_window));
-               tny_account_store_get_accounts (account_store, accounts,
-                                               TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
-               g_object_unref (G_OBJECT (account_store));
-
-               iter = tny_list_create_iterator(accounts);
-               tny_iterator_first (iter);
-               if (tny_iterator_is_done (iter)) {
-                       /* FIXME: Add error handling through mail operation */
-                       g_printerr("modest: no transport accounts defined\n");
-                       modest_edit_msg_window_free_msg_data (edit_window, data);
-                       return;
-               }
-               transport_account = TNY_TRANSPORT_ACCOUNT (tny_iterator_get_current(iter));
-               g_object_ref (transport_account);
-
-               tny_list_foreach (accounts, (GFunc) g_object_unref, NULL);
-               g_object_unref (G_OBJECT (accounts));
-               g_object_unref (G_OBJECT (iter));
+       account_mgr = modest_runtime_get_account_mgr();
+       account_name = modest_account_mgr_get_default_account (account_mgr);
+       if (!account_name) {
+               g_printerr ("modest: no default account found\n");
+               modest_edit_msg_window_free_msg_data (edit_window, data);
+               return;
        }
-
+       transport_account =
+               TNY_TRANSPORT_ACCOUNT(modest_account_mgr_get_tny_account (account_mgr,
+                                                                         account_name,
+                                                                         TNY_ACCOUNT_TYPE_TRANSPORT));
+       if (!transport_account) {
+               g_printerr ("modest: no transport account found\n");
+               g_free (account_name);
+               modest_edit_msg_window_free_msg_data (edit_window, data);
+               return;
+       }
+       from = modest_account_mgr_get_from_string (account_mgr, account_name);
+               
        mail_operation = modest_mail_operation_new ();
-
        modest_mail_operation_send_new_mail (mail_operation,
                                             transport_account,
-                                            data->from, 
+                                            from,
                                             data->to, 
                                             data->cc, 
                                             data->bcc,
@@ -1224,6 +1138,8 @@ _modest_ui_actions_on_send (GtkWidget *widget,
                                             data->body, 
                                             NULL);
        /* Frees */
+       g_free (from);
+       g_free (account_name);
        g_object_unref (G_OBJECT (mail_operation));
        g_object_unref (G_OBJECT (transport_account));
        modest_edit_msg_window_free_msg_data (edit_window, data);
@@ -1279,12 +1195,11 @@ _modest_ui_actions_on_new_folder (GtkWidget *widget,
 {
        TnyFolder *parent_folder;
        ModestFolderView *folder_view;
-       ModestWidgetFactory *widget_factory;
 
-       widget_factory = modest_window_get_widget_factory (MODEST_WINDOW (main_window));
-       folder_view = modest_widget_factory_get_folder_view (widget_factory);
+       folder_view = modest_widget_factory_get_folder_view
+               (modest_runtime_get_widget_factory());
        parent_folder = modest_folder_view_get_selected (folder_view);
-
+       
        if (parent_folder) {
                gchar *folder_name;
 
@@ -1308,7 +1223,6 @@ _modest_ui_actions_on_new_folder (GtkWidget *widget,
                }
                g_object_unref (parent_folder);
        }
-       g_object_unref (G_OBJECT (widget_factory));
 }
 
 void 
@@ -1317,10 +1231,8 @@ _modest_ui_actions_on_rename_folder (GtkWidget *widget,
 {
        TnyFolder *folder;
        ModestFolderView *folder_view;
-       ModestWidgetFactory *widget_factory;
-
-       widget_factory = modest_window_get_widget_factory (MODEST_WINDOW (main_window));
-       folder_view = modest_widget_factory_get_folder_view (widget_factory);
+       
+       folder_view = modest_widget_factory_get_folder_view (modest_runtime_get_widget_factory());
        folder = modest_folder_view_get_selected (folder_view);
 
        if (folder) {
@@ -1340,7 +1252,6 @@ _modest_ui_actions_on_rename_folder (GtkWidget *widget,
                }
                g_object_unref (folder);
        }
-       g_object_unref (G_OBJECT (widget_factory));
 }
 
 static void
@@ -1349,11 +1260,9 @@ delete_folder (ModestMainWindow *main_window,
 {
        TnyFolder *folder;
        ModestFolderView *folder_view;
-       ModestWidgetFactory *widget_factory;
        ModestMailOperation *mail_op;
-                       
-       widget_factory = modest_window_get_widget_factory (MODEST_WINDOW (main_window));
-       folder_view = modest_widget_factory_get_folder_view (widget_factory);
+       
+       folder_view = modest_widget_factory_get_folder_view (modest_runtime_get_widget_factory());
        folder = modest_folder_view_get_selected (folder_view);
 
        mail_op = modest_mail_operation_new ();
@@ -1374,3 +1283,12 @@ _modest_ui_actions_on_move_to_trash_folder (GtkWidget *widget,
 {
        delete_folder (main_window, TRUE);
 }
+
+static void
+_modest_ui_actions_on_accounts_reloaded (TnyAccountStore *store, gpointer user_data)
+{
+       ModestFolderView *folder_view;
+       
+       folder_view = modest_widget_factory_get_folder_view (modest_runtime_get_widget_factory());
+       modest_folder_view_update_model (folder_view, store);
+}