* re-add the correct COPYING file.
[modest] / src / gtk / modest-ui.c
index 47dd0d7..fadca5b 100644 (file)
 #include <config.h>
 #endif /*HAVE_CONFIG_H*/
 
-/* TODO: put in auto* */
-#include <tny-text-buffer-stream.h>
-#include <tny-msg-folder.h>
-
 #include "../modest-ui.h"
-#include "../modest-window-mgr.h"
-#include "../modest-account-mgr.h"
 #include "../modest-account-mgr.h"
-#include "../modest-identity-mgr.h"
-
-#include "../modest-tny-account-store.h"
-#include "../widgets/modest-folder-view.h"
-#include "../widgets/modest-header-view.h"
-#include "../widgets/modest-msg-view.h"
-#include "../modest-tny-transport-actions.h"
-#include "../modest-tny-store-actions.h"
-
-#include "../modest-text-utils.h"
-#include "../modest-tny-msg-actions.h"
-
-#include "../modest-editor-window.h"
+#include "../modest-widget-factory.h"
+#include "modest-main-window.h"
+#include "modest-tny-platform-factory.h"
 
-#include "modest-ui-glade.h"
-#include "modest-ui-wizard.h"
 
 /* 'private'/'protected' functions */
 static void   modest_ui_class_init     (ModestUIClass *klass);
 static void   modest_ui_init           (ModestUI *obj);
 static void   modest_ui_finalize       (GObject *obj);
-static void   modest_ui_last_window_closed (GObject *obj, gpointer data);
 
 gchar *on_password_requested (TnyAccountIface *, const gchar *, gboolean *);
 
+
+typedef struct _ModestUIPrivate ModestUIPrivate;
+struct _ModestUIPrivate {
+       ModestWidgetFactory   *widget_factory;  
+
+       GtkWidget              *main_window;
+};
+
+#define MODEST_UI_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
+                                       MODEST_TYPE_UI, \
+                                       ModestUIPrivate))
+
+
 /* list my signals */
 enum {
        /* MY_SIGNAL_1, */
@@ -95,6 +89,7 @@ modest_ui_get_type (void)
                        sizeof(ModestUI),
                        1,              /* n_preallocs */
                        (GInstanceInitFunc) modest_ui_init,
+                       NULL
                };
                my_type = g_type_register_static (G_TYPE_OBJECT,
                                                  "ModestUI",
@@ -121,235 +116,94 @@ modest_ui_class_init (ModestUIClass *klass)
 static void
 modest_ui_init (ModestUI *obj)
 {
-       ModestUIPrivate *priv = MODEST_UI_GET_PRIVATE(obj);
-
-       priv->modest_acc_mgr    = NULL;
-       priv->modest_id_mgr     = NULL;
-       priv->modest_conf       = NULL;
-       priv->modest_window_mgr = NULL;
-       priv->glade_xml         = NULL;
-       priv->folder_view       = NULL;
-       priv->header_view       = NULL;
-       priv->message_view      = NULL;
-       priv->current_folder    = NULL;
-}
-
+       ModestUIPrivate *priv;
 
-static void
-modest_ui_finalize (GObject *obj)
-{
-       ModestUIPrivate *priv = MODEST_UI_GET_PRIVATE(obj);
-
-       if (priv->modest_acc_mgr)
-               g_object_unref (priv->modest_acc_mgr);
-       priv->modest_acc_mgr = NULL;
-
-       if (priv->modest_id_mgr)
-               g_object_unref (priv->modest_id_mgr);
-       priv->modest_id_mgr = NULL;
-
-       if (priv->modest_conf)
-               g_object_unref (priv->modest_conf);
-       priv->modest_conf = NULL;
+       priv = MODEST_UI_GET_PRIVATE(obj);
 
-       if (priv->modest_window_mgr)
-               g_object_unref (priv->modest_window_mgr);
-       priv->modest_window_mgr = NULL;
+       priv->widget_factory = NULL;
+       priv->main_window    = NULL;
 }
 
 
 static void
-on_accounts_reloaded (ModestTnyAccountStore *account_store, gpointer user_data)
+modest_ui_finalize (GObject *obj)
 {
-       ModestUIPrivate *priv = user_data;
-
-       g_return_if_fail (MODEST_IS_FOLDER_VIEW (priv->folder_view));
-       g_return_if_fail (MODEST_IS_HEADER_VIEW (priv->header_view));
-
-       modest_header_view_set_folder (MODEST_HEADER_VIEW(priv->header_view),
-                                               NULL);
-
-       //      modest_folder_view_update_model(MODEST_FOLDER_VIEW(priv->folder_view),
-       //                      TNY_ACCOUNT_STORE_IFACE(account_store));
+       
+       ModestUIPrivate *priv = MODEST_UI_GET_PRIVATE(obj);
+       
+       if (priv->widget_factory) {
+               g_object_unref (G_OBJECT(priv->widget_factory));
+               priv->widget_factory = NULL;
+       }
+       
+       G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
 
 
 ModestUI*
-modest_ui_new (ModestConf *modest_conf)
+modest_ui_new (void)
 {
        GObject *obj;
        ModestUIPrivate *priv;
-       ModestAccountMgr *modest_acc_mgr;
-       ModestIdentityMgr *modest_id_mgr;
-       TnyAccountStoreIface *account_store_iface;
-       GSList *account_names_list, *cursor;
-       GSList *identities_list;
-
-       g_return_val_if_fail (modest_conf, NULL);
+       TnyPlatformFactory *fact;
+       ModestAccountMgr *account_mgr;
+       TnyAccountStore *account_store;
 
-       obj = g_object_new(MODEST_TYPE_UI, NULL);
+       obj  = g_object_new(MODEST_TYPE_UI, NULL);
        priv = MODEST_UI_GET_PRIVATE(obj);
 
-       modest_acc_mgr = MODEST_ACCOUNT_MGR(modest_account_mgr_new (modest_conf));
-       if (!modest_acc_mgr) {
-               g_warning ("could not create ModestAccountMgr instance");
+       /* Get the platform-dependent instances */
+       fact = modest_tny_platform_factory_get_instance ();
+       
+       account_mgr = modest_tny_platform_factory_get_modest_account_mgr_instance (fact);
+       if (!account_mgr) {
+               g_printerr ("modest: could not create ModestAccountMgr instance\n");
                g_object_unref (obj);
                return NULL;
         }
 
-       modest_id_mgr = MODEST_IDENTITY_MGR(modest_identity_mgr_new (modest_conf));
-       if (!modest_id_mgr) {
-               g_warning ("could not create ModestIdentityMgr instance");
-               g_object_unref (obj);
-               return NULL;
-       }
-
-       account_store_iface =
-               TNY_ACCOUNT_STORE_IFACE(modest_tny_account_store_new (modest_acc_mgr));
-       if (!account_store_iface) {
-               g_warning ("could not initialze ModestTnyAccountStore");
+       account_store = tny_platform_factory_new_account_store (fact);
+       if (!account_store) {
+               g_printerr ("modest: could not initialze ModestTnyAccountStore instance\n");
                return NULL;
         }
 
-        //modest_tny_account_store_set_get_pass_func(MODEST_TNY_ACCOUNT_STORE(account_store_iface),
-        //                                           on_password_requested);
-
-       g_signal_connect (account_store_iface, "accounts_reloaded",
-                         G_CALLBACK(on_accounts_reloaded), priv);
-
-       glade_init ();
-       priv->glade_xml = glade_xml_new (MODEST_GLADE, NULL, NULL);
-       if (!priv->glade_xml) {
-               g_warning ("failed to do glade stuff");
-               g_object_unref (obj);
+       priv->widget_factory = modest_widget_factory_new ();
+       if (!priv->widget_factory) {
+               g_printerr ("modest: could not initialize widget factory\n");
                return NULL;
        }
-
-       /* FIXME: could be used, but doesn't work atm.
-        * glade_xml_signal_autoconnect(priv->glade_xml);
-        */
-
-       priv->modest_acc_mgr = modest_acc_mgr;
-       priv->modest_id_mgr  = modest_id_mgr;
-       g_object_ref (priv->modest_conf = modest_conf);
-
-       priv->account_store = account_store_iface;
-
-       priv->modest_window_mgr = MODEST_WINDOW_MGR(modest_window_mgr_new());
-       g_signal_connect (priv->modest_window_mgr, "last_window_closed",
-                         G_CALLBACK(modest_ui_last_window_closed),
-                         NULL);
-
-       account_names_list = modest_account_mgr_search_server_accounts(modest_acc_mgr,
-                                                                      NULL, MODEST_PROTO_TYPE_ANY,
-                                                                      NULL);
-       identities_list = modest_identity_mgr_identity_names(modest_id_mgr, NULL);
-       if (!(account_names_list != NULL || identities_list != NULL))
-               wizard_account_dialog(MODEST_UI(obj));
-
-       cursor = account_names_list;
-       while (cursor) {
-               g_free (cursor->data);
-               cursor = cursor->next;
-       }
-       g_slist_free(account_names_list);
-       
-
-       cursor = identities_list;
-       while (cursor) {
-               g_free (cursor->data);
-               cursor = cursor->next;
-       }
-       g_slist_free(identities_list);
-
+               
        return MODEST_UI(obj);
 }
 
-
-static void
-modest_ui_last_window_closed (GObject *obj, gpointer data)
+static gboolean
+on_main_window_destroy (GtkObject *widget, ModestUI *self)
 {
-       /* FIXME: Other cleanups todo? Finalize Tinymail? */
+       /* FIXME: check if there any viewer/editing windows opened */
        gtk_main_quit ();
+       return FALSE;
 }
 
 
-gchar *
-on_password_requested (TnyAccountIface *account,
-                      const gchar *prompt,
-                      gboolean *cancel) {
-
-       GtkWidget *passdialog;
-       GtkWidget *vbox;
-       GtkWidget *infoscroll;
-       GtkWidget *infolabel;
-       GtkWidget *passentry;
-       GtkTextBuffer *infobuffer;
-       gchar *retval;
-       gint result;
-
-       passdialog = gtk_dialog_new_with_buttons(_("Password"),
-                                                NULL,
-                                                GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
-                                                GTK_STOCK_OK,
-                                                GTK_RESPONSE_ACCEPT,
-                                                GTK_STOCK_CANCEL,
-                                                GTK_RESPONSE_REJECT,
-                                                NULL);
-
-       vbox = gtk_vbox_new(FALSE, 0);
-
-       infobuffer = gtk_text_buffer_new (NULL);
-       gtk_text_buffer_set_text(infobuffer, prompt, -1);
-       infoscroll = gtk_scrolled_window_new(NULL, NULL);
-       infolabel = gtk_text_view_new_with_buffer(infobuffer);
-       gtk_container_add(GTK_CONTAINER(infoscroll), infolabel);
-       passentry = gtk_entry_new();
-       gtk_entry_set_visibility(GTK_ENTRY(passentry), FALSE);
-
-       gtk_box_pack_start(GTK_BOX(GTK_DIALOG(passdialog)->vbox), infoscroll, FALSE, FALSE, 0);
-       gtk_box_pack_start(GTK_BOX(GTK_DIALOG(passdialog)->vbox), passentry, FALSE, FALSE, 0);
-       gtk_widget_show_all(passdialog);
-
-       result = gtk_dialog_run (GTK_DIALOG(passdialog));
-
-       switch (result) {
-       case GTK_RESPONSE_ACCEPT:
-               retval = g_strdup(gtk_entry_get_text(GTK_ENTRY(passentry)));
-               *cancel=FALSE;
-               break;
-       default:
-               retval = g_strdup("");;
-               *cancel=TRUE;
-               break;
-       }
-
-        gtk_widget_hide(passdialog);
-       gtk_widget_destroy(passdialog);
-       while (gtk_events_pending()){
-               gtk_main_iteration();
-       }
-
-       return retval;
-}
-
-
-void
-on_account_selector_selection_changed (GtkWidget *widget, gpointer user_data)
+GtkWidget*
+modest_ui_main_window (ModestUI *modest_ui)
 {
-       GtkTreeModel *model = gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
-       GtkTreeIter iter;
+       ModestUIPrivate *priv;
 
-       gchar *account_name;
+       g_return_val_if_fail (modest_ui, NULL);
+       priv = MODEST_UI_GET_PRIVATE(modest_ui);
 
-       if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(widget), &iter)) {
-               gtk_tree_model_get(GTK_TREE_MODEL(model), &iter,
-                                  0, &account_name, -1);
-       } else {
-               account_name="empty";
-       }
+       if (!priv->main_window) {
+               priv->main_window =
+                       modest_main_window_new (priv->widget_factory);
+               g_signal_connect (G_OBJECT(priv->main_window), "destroy",
+                                 G_CALLBACK(on_main_window_destroy), modest_ui);
 
-       free(account_name);
+       }
+               
+       if (!priv->main_window)
+               g_printerr ("modest: could not create main window\n");
+       
+       return priv->main_window;
 }
-
-