* all:
[modest] / src / modest-widget-factory.c
index 5708a7a..3b61c6b 100644 (file)
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <glib/gi18n.h>
+#include <gdk/gdkkeysyms.h>
+#include <tny-gtk-account-list-model.h>
+#include <tny-gtk-folder-store-tree-model.h>
+#include <tny-account-store.h>
+#include <tny-simple-list.h>
+#include <tny-device.h>
+#include <tny-folder-store-query.h>
 #include "modest-widget-factory.h"
-/* include other impl specific header files */
+#include "modest-widget-memory.h"
+#include <modest-protocol-info.h>
+#include "modest-tny-platform-factory.h"
+
+#include <modest-account-mgr.h>
+#include <modest-account-mgr-helpers.h>
+#include <modest-runtime.h>
+
+#include "modest-mail-operation.h"
+#include "widgets/modest-header-view-priv.h"
 
 /* 'private'/'protected' functions */
 static void modest_widget_factory_class_init    (ModestWidgetFactoryClass *klass);
 static void modest_widget_factory_init          (ModestWidgetFactory *obj);
 static void modest_widget_factory_finalize      (GObject *obj);
 
-static void on_folder_clicked (ModestTnyFolderTreeView *folder_view, TnyMsgFolderIface *folder,
-                              ModestWidgetFactory *self);
-static void on_message_selected (ModestTnyFolderTreeView *folder_view, TnyMsgIface *msg,
-                                ModestWidgetFactory *self);
 
 /* list my signals */
 enum {
@@ -48,18 +61,19 @@ enum {
 };
 
 typedef struct _ModestWidgetFactoryPrivate ModestWidgetFactoryPrivate;
-struct _ModestWidgetFactoryPrivate {
-       
-       ModestTnyAccountStore *account_store;
-       ModestAccountMgr      *account_mgr;
-       ModestConf            *conf;
+struct _ModestWidgetFactoryPrivate {   
+
+       ModestTnyAccountStore       *account_store;
        
-       ModestTnyHeaderTreeView *header_view;
-       ModestTnyFolderTreeView *folder_view;
-       ModestTnyMsgView        *msg_preview;
-       ModestAccountView       *account_view;
+       ModestHeaderView            *header_view;
+       ModestFolderView            *folder_view;
+       ModestMsgView               *msg_preview;
+
+       GtkWidget                   *progress_bar;
+       GtkWidget                   *status_bar;
+       GtkWidget                   *folder_info_label;
 
-       gboolean                auto_connect;
+       GtkWidget                   *online_toggle;
 };
 #define MODEST_WIDGET_FACTORY_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
                                                    MODEST_TYPE_WIDGET_FACTORY, \
@@ -85,6 +99,7 @@ modest_widget_factory_get_type (void)
                        sizeof(ModestWidgetFactory),
                        1,              /* n_preallocs */
                        (GInstanceInitFunc) modest_widget_factory_init,
+                       NULL
                };
                my_type = g_type_register_static (G_TYPE_OBJECT,
                                                  "ModestWidgetFactory",
@@ -103,13 +118,6 @@ modest_widget_factory_class_init (ModestWidgetFactoryClass *klass)
        gobject_class->finalize = modest_widget_factory_finalize;
 
        g_type_class_add_private (gobject_class, sizeof(ModestWidgetFactoryPrivate));
-
-       /* signal definitions go here, e.g.: */
-/*     signals[MY_SIGNAL_1] = */
-/*             g_signal_new ("my_signal_1",....); */
-/*     signals[MY_SIGNAL_2] = */
-/*             g_signal_new ("my_signal_2",....); */
-/*     etc. */
 }
 
 static void
@@ -117,175 +125,278 @@ modest_widget_factory_init (ModestWidgetFactory *obj)
 {
        ModestWidgetFactoryPrivate *priv;
        priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(obj);
-
-       priv->conf          = NULL;
-       priv->account_mgr   = NULL;
+       
        priv->account_store = NULL;
+       
+       priv->progress_bar = gtk_progress_bar_new ();
+       gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR(priv->progress_bar),
+                                      1.0);
+       priv->status_bar   = gtk_statusbar_new ();
+       gtk_statusbar_set_has_resize_grip (GTK_STATUSBAR(priv->status_bar),
+                                          FALSE);
 }
 
+
 static void
 modest_widget_factory_finalize (GObject *obj)
 {
+       /* no need to unref account_store; we don't own the reference */
+       
+       G_OBJECT_CLASS(parent_class)->finalize (obj);
+}
+
+
+static gboolean
+init_widgets (ModestWidgetFactory *self)
+{
        ModestWidgetFactoryPrivate *priv;
-       priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(obj);
+       TnyFolderStoreQuery *query;
 
-       if (priv->account_mgr) {
-               g_object_unref (G_OBJECT(priv->account_mgr));
-               priv->account_mgr = NULL;
-       }
+       priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
 
-       if (priv->conf) {
-               g_object_unref (G_OBJECT(priv->conf));
-               priv->conf = NULL;
-       }
+       /* folder view */
+       query = tny_folder_store_query_new ();
+       tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
 
-       if (priv->account_store) {
-               g_object_unref (G_OBJECT(priv->account_store));
-               priv->account_store = NULL;
+       priv->folder_view =  MODEST_FOLDER_VIEW(modest_folder_view_new (priv->account_store,query));
+       if (!priv->folder_view) {
+               g_printerr ("modest: cannot instantiate folder view\n");
+               return FALSE;
+       }       
+       g_object_unref (G_OBJECT (query));
+       
+       /* header view */
+       priv->header_view =
+               MODEST_HEADER_VIEW(modest_header_view_new (NULL, MODEST_HEADER_VIEW_STYLE_DETAILS));
+       if (!priv->header_view) {
+               g_printerr ("modest: cannot instantiate header view\n");
+               return FALSE;
        }
+               
+       /* msg preview */
+       priv->msg_preview = MODEST_MSG_VIEW(modest_msg_view_new (NULL));
+       if (!priv->msg_preview) {
+               g_printerr ("modest: cannot instantiate header view\n");
+               return FALSE;
+       }
+
+       /* online/offline combo */
+       priv->online_toggle = gtk_toggle_button_new ();
+
+       /* label with number of items, unread items for 
+          the current folder */
+       priv->folder_info_label = gtk_label_new (NULL);
+       
+       return TRUE;
 }
 
+
 ModestWidgetFactory*
-modest_widget_factory_new (ModestConf *conf,
-                          ModestTnyAccountStore *account_store,
-                          ModestAccountMgr *account_mgr,
-                          gboolean auto_connect)
+modest_widget_factory_new (ModestTnyAccountStore *account_store)
 {
        GObject *obj;
        ModestWidgetFactoryPrivate *priv;
 
        g_return_val_if_fail (account_store, NULL);
-       g_return_val_if_fail (account_mgr, NULL);
-       g_return_val_if_fail (conf, NULL);
        
-       obj  = g_object_new(MODEST_TYPE_WIDGET_FACTORY, NULL);  
+       obj  = g_object_new (MODEST_TYPE_WIDGET_FACTORY, NULL); 
        priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(obj);
-
-       priv->auto_connect = auto_connect;
-
-       g_object_ref (G_OBJECT(conf));
-       priv->conf = conf;
        
-       g_object_ref (G_OBJECT(account_mgr));
-       priv->account_mgr = account_mgr;
-
-       g_object_ref (G_OBJECT(account_store));
        priv->account_store = account_store;
        
+       if (!init_widgets (MODEST_WIDGET_FACTORY(obj))) {
+               g_printerr ("modest: widget factory failed to init widgets\n");
+               g_object_unref (obj);
+               return NULL;
+       }
+       
        return MODEST_WIDGET_FACTORY(obj);
 }
 
 
 
 
-ModestTnyFolderTreeView*
-modest_widget_factory_get_folder_tree_widget (ModestWidgetFactory *self)
+ModestFolderView*
+modest_widget_factory_get_folder_view (ModestWidgetFactory *self)
 {
-       ModestWidgetFactoryPrivate *priv;
-       
        g_return_val_if_fail (self, NULL);
+       return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->folder_view;
+}
 
-       priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
 
-       if (!priv->folder_view) {
-               priv->folder_view =
-                       MODEST_TNY_FOLDER_TREE_VIEW(modest_tny_folder_tree_view_new
-                                                   (TNY_ACCOUNT_STORE_IFACE(priv->account_store)));
-               if (priv->folder_view && priv->auto_connect)
-                       g_signal_connect (G_OBJECT(priv->folder_view), "folder_selected",
-                                         G_CALLBACK(on_folder_clicked), self);
-       }
-               
-       if (!priv->folder_view)
-               g_printerr ("modest: cannot instantiate folder view\n");
-       
-       return priv->folder_view;
+ModestHeaderView*
+modest_widget_factory_get_header_view (ModestWidgetFactory *self)
+{
+       g_return_val_if_fail (self, NULL);
+       return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->header_view;
 }
 
 
-ModestTnyHeaderTreeView*
-modest_widget_factory_get_header_tree_widget (ModestWidgetFactory *self)
+ModestMsgView*
+modest_widget_factory_get_msg_preview (ModestWidgetFactory *self)
 {
-       ModestWidgetFactoryPrivate *priv;
-       
        g_return_val_if_fail (self, NULL);
+       return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->msg_preview;
+}
 
-       priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
 
-       if (!priv->header_view) {
-               priv->header_view =
-                       MODEST_TNY_HEADER_TREE_VIEW(modest_tny_header_tree_view_new
-                                                   (NULL, NULL,
-                                                    MODEST_TNY_HEADER_TREE_VIEW_STYLE_NORMAL));
-               if (priv->header_view && priv->auto_connect)
-                       g_signal_connect (G_OBJECT(priv->header_view), "message_selected",
-                                         G_CALLBACK(on_message_selected), self);
-       }
-       
-       if (!priv->header_view)
-               g_printerr ("modest: cannot instantiate header view\n");
-       
-       return priv->header_view;
+ModestAccountView*
+modest_widget_factory_get_account_view (ModestWidgetFactory *self)
+{
+       return modest_account_view_new (modest_runtime_get_account_mgr());
 }
 
 
-ModestTnyMsgView*
-modest_widget_factory_get_msg_preview_widget (ModestWidgetFactory *self)
+
+GtkWidget*
+modest_widget_factory_get_progress_bar (ModestWidgetFactory *self)
 {
-       ModestWidgetFactoryPrivate *priv;
-       
        g_return_val_if_fail (self, NULL);
+       return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->progress_bar;
+}
 
-       priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
 
-       if (!priv->msg_preview)
-               priv->msg_preview =
-                       MODEST_TNY_MSG_VIEW(modest_tny_msg_view_new (NULL));
-       
-       if (!priv->msg_preview)
-               g_printerr ("modest: cannot instantiate header view\n");
-       
-       return priv->msg_preview;
+GtkWidget*
+modest_widget_factory_get_status_bar (ModestWidgetFactory *self)
+{
+       g_return_val_if_fail (self, NULL);
+       return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->status_bar;
 }
 
 
-static void
-on_folder_clicked (ModestTnyFolderTreeView *folder_view, TnyMsgFolderIface *folder,
-                  ModestWidgetFactory *self)
+
+static const GSList*
+get_transports (ModestWidgetFactory *self)
 {
        ModestWidgetFactoryPrivate *priv;
+       ModestAccountMgr *account_mgr;
+       GSList *transports = NULL;
+       GSList *cursor, *accounts;
+       
        priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
+
+       account_mgr = modest_runtime_get_account_mgr();
+       cursor = accounts = modest_account_mgr_account_names (account_mgr, NULL);
+       while (cursor) {
+               ModestAccountData *data;
+               gchar *account_name = (gchar*)cursor->data;
+
+               data = modest_account_mgr_get_account_data (account_mgr, account_name);
+               if (data && data->transport_account) {
+                       ModestPair *pair;
+                       gchar *display_name = g_strdup_printf ("%s (%s)", data->email, account_name);
+                       pair = modest_pair_new ((gpointer) data,
+                                               (gpointer) display_name , TRUE);
+                       transports = g_slist_prepend (transports, pair);
+               }
+               /* don't free account name; it's freed when the transports list is freed */
+               cursor = cursor->next;
+       }
+       g_slist_free (accounts);
        
-       modest_tny_header_tree_view_set_folder (priv->header_view, folder);
+       return transports;
 }
 
 
-static void
-on_message_selected (ModestTnyFolderTreeView *folder_view, TnyMsgIface *msg,
-                    ModestWidgetFactory *self)
-{      
+#if 0
+static const GSList*
+get_stores (ModestWidgetFactory *self, gboolean only_remote)
+{
        ModestWidgetFactoryPrivate *priv;
+       TnyAccountStore *account_store;
+       TnyList *stores;
+       TnyIterator *iter;
+       
        priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
 
-       modest_tny_msg_view_set_message (priv->msg_preview, msg);
+       account_store =
+               tny_platform_factory_new_account_store (priv->fact);                    
+
+       stores = tny_simple_list_new ();
+       tny_account_store_get_accounts (account_store, stores,
+                                        TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
+
+       /* simply return all the stores */
+       if (!only_remote)
+               return stores;
+
+       /*  remove the non-remote stores from the list */
+       if (only_remote) {
+               iter = tny_list_create_iterator (stores);
+               while (!tny_iterator_is_done (iter)) {
+                       TnyAccount *acc = (TnyAccount*)tny_iterator_get_current(iter);
+                       /* is it a local account? if so, remove */
+                       ModestProtocol proto = modest_protocol_info_get_protocol (tny_account_get_proto(acc));
+                       if (modest_protocol_info_protocol_is_local_store(proto))
+                               tny_list_remove (stores, acc); /* FIXME: iter still valid? */
+                       tny_iterator_next (iter);
+               }
+               g_object_unref (G_OBJECT(iter));
+       }
+       return stores;          
 }
+#endif
 
-
-ModestAccountView*
-modest_widget_factory_get_account_view_widget (ModestWidgetFactory *self)
+GtkWidget*
+modest_widget_factory_get_combo_box (ModestWidgetFactory *self, ModestComboBoxType type)
 {
        ModestWidgetFactoryPrivate *priv;
+       ModestPairList *protos = NULL;
+       GtkWidget* combo_box;
        
        g_return_val_if_fail (self, NULL);
 
        priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
+       
+       switch (type) {
+       case MODEST_COMBO_BOX_TYPE_STORE_PROTOS:
+               protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_STORE);
+               break;
+       case MODEST_COMBO_BOX_TYPE_TRANSPORT_PROTOS:
+               protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_TRANSPORT);
+               break;
+       case MODEST_COMBO_BOX_TYPE_SECURITY_PROTOS:
+               protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_SECURITY);
+               break;
+       case MODEST_COMBO_BOX_TYPE_AUTH_PROTOS:
+               protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_AUTH);
+               break;
+       case MODEST_COMBO_BOX_TYPE_TRANSPORTS:
+               protos = (ModestPairList *) get_transports (self);
+               break;
+/*     case MODEST_COMBO_BOX_TYPE_REMOTE_STORES: */
+/*             // FIXME */
+/*             list = get_stores (self, TRUE); /\* get all *remote* stores *\/ */
+/*             combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL(list)); */
+/*             g_object_unref (G_OBJECT(list)); */
+/*             //return combo_box; */
+       default:
+               g_warning ("invalid combo box type: %d", type);
+               return NULL;
+       }
 
-       if (!priv->account_view)
-               priv->account_view =
-                       MODEST_ACCOUNT_VIEW(modest_account_view_new (priv->account_mgr));
-
-       if (!priv->account_view)
-               g_printerr ("modest: cannot create account view widget\n");
+       combo_box = modest_combo_box_new (protos);
+       g_slist_free (protos);
+       
+       gtk_combo_box_set_active (GTK_COMBO_BOX(combo_box), 0);
        
-       return priv->account_view;      
+       return combo_box;
+}
+
+
+
+GtkWidget*
+modest_widget_factory_get_online_toggle (ModestWidgetFactory *self)
+{
+       g_return_val_if_fail (self, NULL);
+       return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->online_toggle;
+}
+
+
+
+GtkWidget*
+modest_widget_factory_get_folder_info_label (ModestWidgetFactory *self)
+{
+       g_return_val_if_fail (self, NULL);
+       return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->folder_info_label;
 }