1 /* modest-tny-folder-tree-view.c */
3 /* insert (c)/licensing information) */
4 #include <glib/gi18n.h>
6 #include <tny-account-tree-model.h>
7 #include <tny-account-store-iface.h>
8 #include <tny-account-iface.h>
9 #include <tny-summary-window-iface.h>
11 #include "modest-tny-folder-tree-view.h"
12 /* include other impl specific header files */
14 /* 'private'/'protected' functions */
15 static void modest_tny_folder_tree_view_class_init (ModestTnyFolderTreeViewClass *klass);
16 static void modest_tny_folder_tree_view_init (ModestTnyFolderTreeView *obj);
17 static void modest_tny_folder_tree_view_finalize (GObject *obj);
19 //static void modest_tny_folder_tree_view_iface_init (gpointer iface, gpointer data);
20 static void modest_tny_folder_tree_view_set_account_store (TnySummaryWindowIface *self,
21 TnyAccountStoreIface *account_store);
22 static gboolean update_model (ModestTnyFolderTreeView *self,TnyAccountStoreIface *iface);
23 static gboolean update_model_empty (ModestTnyFolderTreeView *self);
25 static void selection_changed (GtkTreeSelection *sel, gpointer data);
29 FOLDER_SELECTED_SIGNAL,
33 typedef struct _ModestTnyFolderTreeViewPrivate ModestTnyFolderTreeViewPrivate;
34 struct _ModestTnyFolderTreeViewPrivate {
35 TnyAccountStoreIface *tny_account_store;
36 gboolean view_is_empty;
39 #define MODEST_TNY_FOLDER_TREE_VIEW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
40 MODEST_TYPE_TNY_FOLDER_TREE_VIEW, \
41 ModestTnyFolderTreeViewPrivate))
43 static GObjectClass *parent_class = NULL;
45 static guint signals[LAST_SIGNAL] = {0};
48 modest_tny_folder_tree_view_get_type (void)
50 static GType my_type = 0;
52 static const GTypeInfo my_info = {
53 sizeof(ModestTnyFolderTreeViewClass),
55 NULL, /* base finalize */
56 (GClassInitFunc) modest_tny_folder_tree_view_class_init,
57 NULL, /* class finalize */
58 NULL, /* class data */
59 sizeof(ModestTnyFolderTreeView),
61 (GInstanceInitFunc) modest_tny_folder_tree_view_init,
64 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
65 "ModestTnyFolderTreeView",
72 modest_tny_folder_tree_view_class_init (ModestTnyFolderTreeViewClass *klass)
74 GObjectClass *gobject_class;
75 gobject_class = (GObjectClass*) klass;
77 parent_class = g_type_class_peek_parent (klass);
78 gobject_class->finalize = modest_tny_folder_tree_view_finalize;
80 g_type_class_add_private (gobject_class,
81 sizeof(ModestTnyFolderTreeViewPrivate));
83 signals[FOLDER_SELECTED_SIGNAL] =
84 g_signal_new ("folder_selected",
85 G_TYPE_FROM_CLASS (gobject_class),
87 G_STRUCT_OFFSET (ModestTnyFolderTreeViewClass,folder_selected),
89 g_cclosure_marshal_VOID__POINTER,
90 G_TYPE_NONE, 1, G_TYPE_POINTER);
94 modest_tny_folder_tree_view_init (ModestTnyFolderTreeView *obj)
96 ModestTnyFolderTreeViewPrivate *priv;
97 priv = MODEST_TNY_FOLDER_TREE_VIEW_GET_PRIVATE(obj);
99 priv->view_is_empty = TRUE;
100 priv->tny_account_store = NULL;
105 modest_tny_folder_tree_view_finalize (GObject *obj)
107 ModestTnyFolderTreeViewPrivate *priv;
109 g_return_if_fail (obj);
111 priv = MODEST_TNY_FOLDER_TREE_VIEW_GET_PRIVATE(obj);
112 if (priv->tny_account_store) {
113 g_object_unref (G_OBJECT(priv->tny_account_store));
114 priv->tny_account_store = NULL;
117 (*parent_class->finalize)(obj);
122 modest_tny_folder_tree_view_set_account_store (TnySummaryWindowIface *self,
123 TnyAccountStoreIface *account_store)
125 ModestTnyFolderTreeViewPrivate *priv;
127 g_return_if_fail (self);
128 g_return_if_fail (account_store);
130 priv = MODEST_TNY_FOLDER_TREE_VIEW_GET_PRIVATE(self);
131 if (priv->tny_account_store) {
132 g_object_unref (priv->tny_account_store);
133 priv->tny_account_store = NULL;
136 g_object_ref (G_OBJECT(priv->tny_account_store = account_store));
142 modest_tny_folder_tree_view_new (TnyAccountStoreIface *iface)
145 ModestTnyFolderTreeViewPrivate *priv;
146 GtkTreeSelection *sel;
148 self = G_OBJECT(g_object_new(MODEST_TYPE_TNY_FOLDER_TREE_VIEW, NULL));
149 priv = MODEST_TNY_FOLDER_TREE_VIEW_GET_PRIVATE(self);
151 g_return_val_if_fail (iface, NULL);
153 if (!update_model (MODEST_TNY_FOLDER_TREE_VIEW(self), iface))
154 g_warning ("failed or update model");
156 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
157 g_signal_connect (sel, "changed",
158 G_CALLBACK(selection_changed), self);
160 return GTK_WIDGET(self);
167 update_model_empty (ModestTnyFolderTreeView *self)
171 ModestTnyFolderTreeViewPrivate *priv;
173 g_return_val_if_fail (self, FALSE);
175 store = gtk_tree_store_new (1, G_TYPE_STRING);
176 gtk_tree_store_append (store, &iter, NULL);
178 gtk_tree_store_set (store, &iter, 0,
181 gtk_tree_view_set_model (GTK_TREE_VIEW(self),
182 GTK_TREE_MODEL(store));
183 g_object_unref (store);
185 priv = MODEST_TNY_FOLDER_TREE_VIEW_GET_PRIVATE(self);
186 priv->view_is_empty = TRUE;
193 update_model (ModestTnyFolderTreeView *self,TnyAccountStoreIface *iface)
195 const GList *accounts;
196 TnyAccountTreeModel *folder_model;
197 ModestTnyFolderTreeViewPrivate *priv;
199 g_return_val_if_fail (iface, FALSE);
201 priv = MODEST_TNY_FOLDER_TREE_VIEW_GET_PRIVATE(self);
202 priv->view_is_empty = TRUE;
204 accounts = tny_account_store_iface_get_store_accounts (iface);
206 g_warning ("no accounts have been defined yet");
207 return update_model_empty (self);
210 folder_model = tny_account_tree_model_new ();
212 g_warning ("failed to get account tree model");
213 return update_model_empty (self);
217 TnyStoreAccountIface *account =
218 TNY_STORE_ACCOUNT_IFACE(accounts->data);
220 g_warning ("invalid account");
221 g_object_unref (folder_model);
222 return update_model_empty (self);
224 tny_account_tree_model_add (TNY_ACCOUNT_TREE_MODEL (folder_model),
226 accounts = accounts->next;
229 gtk_tree_view_set_model (GTK_TREE_VIEW(self),
230 GTK_TREE_MODEL(folder_model));
231 g_object_unref (G_OBJECT(folder_model));
233 priv->view_is_empty = FALSE; /* were not empty anymore! */
239 selection_changed (GtkTreeSelection *sel, gpointer user_data)
242 TnyMsgFolderIface *folder;
244 ModestTnyFolderTreeView *tree_view;
245 ModestTnyFolderTreeViewPrivate *priv;
247 g_return_if_fail (sel);
248 g_return_if_fail (user_data);
250 priv = MODEST_TNY_FOLDER_TREE_VIEW_GET_PRIVATE(user_data);
252 /* is_empty means that there is only the 'empty' item */
253 if (priv->view_is_empty)
256 if (!gtk_tree_selection_get_selected (sel, &model, &iter))
257 return; /* folder was _un_selected */
259 tree_view = MODEST_TNY_FOLDER_TREE_VIEW (user_data);
261 gtk_tree_model_get (model, &iter,
262 TNY_ACCOUNT_TREE_MODEL_INSTANCE_COLUMN,
265 g_signal_emit (G_OBJECT(tree_view), signals[FOLDER_SELECTED_SIGNAL], 0,