#include <glib/gi18n.h>
#include <string.h>
-
-#include <tny-gtk-account-tree-model.h>
+#include <gdk/gdkkeysyms.h>
+#include <tny-account-store-view.h>
+#include <tny-gtk-account-list-model.h>
+#include <tny-gtk-folder-store-tree-model.h>
+#include <tny-gtk-header-list-model.h>
+#include <tny-folder.h>
#include <tny-account-store.h>
#include <tny-account.h>
#include <tny-folder.h>
+#include <tny-camel-folder.h>
+#include <tny-simple-list.h>
+#include <modest-tny-folder.h>
+#include <modest-marshal.h>
#include <modest-icon-names.h>
-#include <modest-icon-factory.h>
#include <modest-tny-account-store.h>
-
+#include <modest-text-utils.h>
+#include <modest-runtime.h>
#include "modest-folder-view.h"
-
+#include <modest-dnd.h>
+#include <modest-platform.h>
/* 'private'/'protected' functions */
static void modest_folder_view_class_init (ModestFolderViewClass *klass);
static void modest_folder_view_init (ModestFolderView *obj);
static void modest_folder_view_finalize (GObject *obj);
-static gboolean update_model (ModestFolderView *self,
- ModestTnyAccountStore *account_store);
-static gboolean update_model_empty (ModestFolderView *self);
+static void tny_account_store_view_init (gpointer g,
+ gpointer iface_data);
+
+static void modest_folder_view_set_account_store (TnyAccountStoreView *self,
+ TnyAccountStore *account_store);
+
+static gboolean update_model (ModestFolderView *self,
+ ModestTnyAccountStore *account_store);
+
+static void on_selection_changed (GtkTreeSelection *sel, gpointer data);
+
+static void on_account_update (TnyAccountStore *account_store,
+ const gchar *account,
+ gpointer user_data);
+
+static void on_accounts_reloaded (TnyAccountStore *store,
+ gpointer user_data);
+
+static gint cmp_rows (GtkTreeModel *tree_model,
+ GtkTreeIter *iter1,
+ GtkTreeIter *iter2,
+ gpointer user_data);
+
+static gboolean filter_row (GtkTreeModel *model,
+ GtkTreeIter *iter,
+ gpointer data);
-static void on_selection_changed (GtkTreeSelection *sel, gpointer data);
-static void on_subscription_changed (TnyStoreAccount *store_account, TnyFolder *folder,
- ModestFolderView *self);
+static gboolean on_key_pressed (GtkWidget *self,
+ GdkEventKey *event,
+ gpointer user_data);
-static gboolean modest_folder_view_update_model (ModestFolderView *self,
- TnyAccountStore *account_store);
-static const gchar *get_account_name_from_folder (GtkTreeModel *model, GtkTreeIter iter);
+static void on_configuration_key_changed (ModestConf* conf,
+ const gchar *key,
+ ModestConfEvent event,
+ ModestFolderView *self);
-static void modest_folder_view_disconnect_store_account_handlers (GtkTreeView *self);
+/* DnD functions */
+static void on_drag_data_get (GtkWidget *widget,
+ GdkDragContext *context,
+ GtkSelectionData *selection_data,
+ guint info,
+ guint time,
+ gpointer data);
+
+static void on_drag_data_received (GtkWidget *widget,
+ GdkDragContext *context,
+ gint x,
+ gint y,
+ GtkSelectionData *selection_data,
+ guint info,
+ guint time,
+ gpointer data);
+
+static gboolean on_drag_motion (GtkWidget *widget,
+ GdkDragContext *context,
+ gint x,
+ gint y,
+ guint time,
+ gpointer user_data);
+
+static gint expand_row_timeout (gpointer data);
+
+static void setup_drag_and_drop (GtkTreeView *self);
enum {
- FOLDER_SELECTED_SIGNAL,
+ FOLDER_SELECTION_CHANGED_SIGNAL,
+ FOLDER_DISPLAY_NAME_CHANGED_SIGNAL,
LAST_SIGNAL
};
typedef struct _ModestFolderViewPrivate ModestFolderViewPrivate;
struct _ModestFolderViewPrivate {
+ TnyAccountStore *account_store;
+ TnyFolderStore *cur_folder_store;
- TnyAccountStore *account_store;
- TnyFolder *cur_folder;
- gboolean view_is_empty;
-
- gulong sig1, sig2;
- gulong *store_accounts_handlers;
- GMutex *lock;
- GtkTreeSelection *cur_selection;
- TnyFolderStoreQuery *query;
+ gulong account_update_signal;
+ gulong changed_signal;
+ gulong accounts_reloaded_signal;
+
+ GtkTreeSelection *cur_selection;
+ TnyFolderStoreQuery *query;
+ guint timer_expander;
+ gchar *local_account_name;
+ ModestFolderViewStyle style;
};
-#define MODEST_FOLDER_VIEW_GET_PRIVATE(o) \
- (G_TYPE_INSTANCE_GET_PRIVATE((o), \
- MODEST_TYPE_FOLDER_VIEW, \
+#define MODEST_FOLDER_VIEW_GET_PRIVATE(o) \
+ (G_TYPE_INSTANCE_GET_PRIVATE((o), \
+ MODEST_TYPE_FOLDER_VIEW, \
ModestFolderViewPrivate))
/* globals */
static GObjectClass *parent_class = NULL;
(GInstanceInitFunc) modest_folder_view_init,
NULL
};
+
+ static const GInterfaceInfo tny_account_store_view_info = {
+ (GInterfaceInitFunc) tny_account_store_view_init, /* interface_init */
+ NULL, /* interface_finalize */
+ NULL /* interface_data */
+ };
+
my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
"ModestFolderView",
- &my_info, 0);
+ &my_info, 0);
+
+ g_type_add_interface_static (my_type,
+ TNY_TYPE_ACCOUNT_STORE_VIEW,
+ &tny_account_store_view_info);
}
return my_type;
}
parent_class = g_type_class_peek_parent (klass);
gobject_class->finalize = modest_folder_view_finalize;
-
- klass->update_model = modest_folder_view_update_model;
g_type_class_add_private (gobject_class,
sizeof(ModestFolderViewPrivate));
- signals[FOLDER_SELECTED_SIGNAL] =
- g_signal_new ("folder_selected",
+ signals[FOLDER_SELECTION_CHANGED_SIGNAL] =
+ g_signal_new ("folder_selection_changed",
+ G_TYPE_FROM_CLASS (gobject_class),
+ G_SIGNAL_RUN_FIRST,
+ G_STRUCT_OFFSET (ModestFolderViewClass,
+ folder_selection_changed),
+ NULL, NULL,
+ modest_marshal_VOID__POINTER_BOOLEAN,
+ G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_BOOLEAN);
+
+ /*
+ * This signal is emitted whenever the currently selected
+ * folder display name is computed. Note that the name could
+ * be different to the folder name, because we could append
+ * the unread messages count to the folder name to build the
+ * folder display name
+ */
+ signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL] =
+ g_signal_new ("folder-display-name-changed",
G_TYPE_FROM_CLASS (gobject_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (ModestFolderViewClass,
- folder_selected),
+ folder_display_name_changed),
NULL, NULL,
- g_cclosure_marshal_VOID__POINTER,
- G_TYPE_NONE, 1, G_TYPE_POINTER);
+ g_cclosure_marshal_VOID__STRING,
+ G_TYPE_NONE, 1, G_TYPE_STRING);
}
text_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
{
+ ModestFolderViewPrivate *priv;
GObject *rendobj;
- gchar *fname;
- gint unread;
+ gchar *fname = NULL;
+ gint unread, all;
TnyFolderType type;
+ GObject *instance = NULL;
+ g_return_if_fail (column);
+ g_return_if_fail (tree_model);
+
gtk_tree_model_get (tree_model, iter,
- TNY_GTK_ACCOUNT_TREE_MODEL_NAME_COLUMN, &fname,
- TNY_GTK_ACCOUNT_TREE_MODEL_TYPE_COLUMN, &type,
- TNY_GTK_ACCOUNT_TREE_MODEL_UNREAD_COLUMN, &unread, -1);
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_ALL_COLUMN, &all,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
+ -1);
rendobj = G_OBJECT(renderer);
+
+ if (!fname)
+ return;
- if (unread > 0) {
- gchar *folder_title = g_strdup_printf ("%s (%d)", fname, unread);
- g_object_set (rendobj,"text", folder_title, "weight", 800, NULL);
- g_free (folder_title);
- } else
- g_object_set (rendobj,"text", fname, "weight", 400, NULL);
-
- g_free (fname);
-}
+ if (!instance) {
+ g_free (fname);
+ return;
+ }
-/* FIXME: move these to TnyMail */
-enum {
+
+ priv = MODEST_FOLDER_VIEW_GET_PRIVATE (data);
+
+ if (type != TNY_FOLDER_TYPE_ROOT) {
+ gint number;
+
+ if (modest_tny_folder_is_local_folder (TNY_FOLDER (instance))) {
+ TnyFolderType type;
+ type = modest_tny_folder_get_local_folder_type (TNY_FOLDER (instance));
+ if (type != TNY_FOLDER_TYPE_UNKNOWN) {
+ g_free (fname);
+ fname = g_strdup(modest_local_folder_info_get_type_display_name (type));
+ }
+ }
+
+ /* Select the number to show */
+ if ((type == TNY_FOLDER_TYPE_DRAFTS) || (type == TNY_FOLDER_TYPE_OUTBOX))
+ number = all;
+ else
+ number = unread;
+
+ /* Use bold font style if there are unread messages */
+ if (unread > 0) {
+ gchar *folder_title = g_strdup_printf ("%s (%d)", fname, unread);
+ g_object_set (rendobj,"text", folder_title, "weight", 800, NULL);
+ if (G_OBJECT (priv->cur_folder_store) == instance)
+ g_signal_emit (G_OBJECT(data),
+ signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
+ folder_title);
+ g_free (folder_title);
+ } else {
+ g_object_set (rendobj,"text", fname, "weight", 400, NULL);
+ if (G_OBJECT (priv->cur_folder_store) == instance)
+ g_signal_emit (G_OBJECT(data),
+ signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
+ fname);
+ }
- TNY_FOLDER_TYPE_NOTES = TNY_FOLDER_TYPE_SENT + 1, /* urgh */
- TNY_FOLDER_TYPE_DRAFTS,
- TNY_FOLDER_TYPE_CONTACTS,
- TNY_FOLDER_TYPE_CALENDAR
-};
+ } else {
+ const gchar *account_name = NULL;
+ const gchar *account_id = NULL;
+
+ /* If it's a server account */
+ account_id = tny_account_get_id (TNY_ACCOUNT (instance));
+ if (!strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID)) {
+ account_name = priv->local_account_name;
+ } else {
+ if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) {
+ /* TODO: get MMC card name */
+ } else {
+ account_name = fname;
+ }
+ }
+
+ /* Notify display name observers */
+ if (G_OBJECT (priv->cur_folder_store) == instance)
+ g_signal_emit (G_OBJECT(data),
+ signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
+ account_name);
+
+ /* Use bold font style */
+ g_object_set (rendobj,"text", account_name, "weight", 800, NULL);
+ }
-static TnyFolderType
-guess_folder_type (const gchar* name)
-{
- TnyFolderType type;
- gchar *folder;
-
- g_return_val_if_fail (name, TNY_FOLDER_TYPE_NORMAL);
-
- type = TNY_FOLDER_TYPE_NORMAL;
- folder = g_utf8_strdown (name, strlen(name));
-
- if (strcmp (folder, "inbox") == 0 ||
- strcmp (folder, _("inbox")) == 0)
- type = TNY_FOLDER_TYPE_INBOX;
- else if (strcmp (folder, "outbox") == 0 ||
- strcmp (folder, _("outbox")) == 0)
- type = TNY_FOLDER_TYPE_OUTBOX;
- else if (g_str_has_prefix(folder, "junk") ||
- g_str_has_prefix(folder, _("junk")))
- type = TNY_FOLDER_TYPE_JUNK;
- else if (g_str_has_prefix(folder, "trash") ||
- g_str_has_prefix(folder, _("trash")))
- type = TNY_FOLDER_TYPE_JUNK;
- else if (g_str_has_prefix(folder, "sent") ||
- g_str_has_prefix(folder, _("sent")))
- type = TNY_FOLDER_TYPE_SENT;
-
- /* these are not *really* TNY_ types */
- else if (g_str_has_prefix(folder, "draft") ||
- g_str_has_prefix(folder, _("draft")))
- type = TNY_FOLDER_TYPE_DRAFTS;
- else if (g_str_has_prefix(folder, "notes") ||
- g_str_has_prefix(folder, _("notes")))
- type = TNY_FOLDER_TYPE_NOTES;
- else if (g_str_has_prefix(folder, "contacts") ||
- g_str_has_prefix(folder, _("contacts")))
- type = TNY_FOLDER_TYPE_CONTACTS;
- else if (g_str_has_prefix(folder, "calendar") ||
- g_str_has_prefix(folder, _("calendar")))
- type = TNY_FOLDER_TYPE_CALENDAR;
-
- g_free (folder);
- return type;
+ g_object_unref (G_OBJECT (instance));
+ g_free (fname);
}
+
static void
icon_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
{
- GObject *rendobj;
+ GObject *rendobj, *instance;
GdkPixbuf *pixbuf;
TnyFolderType type;
gchar *fname = NULL;
+ const gchar *account_id = NULL;
gint unread;
rendobj = G_OBJECT(renderer);
gtk_tree_model_get (tree_model, iter,
- TNY_GTK_ACCOUNT_TREE_MODEL_TYPE_COLUMN, &type,
- TNY_GTK_ACCOUNT_TREE_MODEL_NAME_COLUMN, &fname,
- TNY_GTK_ACCOUNT_TREE_MODEL_UNREAD_COLUMN, &unread, -1);
- rendobj = G_OBJECT(renderer);
-
- if (type == TNY_FOLDER_TYPE_NORMAL)
- type = guess_folder_type (fname);
-
- if (fname)
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
+ -1);
+
+ if (!fname)
+ return;
+
+ if (!instance) {
g_free (fname);
+ return;
+ }
+
+ if (type == TNY_FOLDER_TYPE_NORMAL || type == TNY_FOLDER_TYPE_UNKNOWN) {
+ type = modest_tny_folder_guess_folder_type_from_name (fname);
+ }
switch (type) {
- case TNY_FOLDER_TYPE_INBOX:
- pixbuf = modest_icon_factory_get_small_icon (MODEST_FOLDER_ICON_INBOX);
+ case TNY_FOLDER_TYPE_ROOT:
+ account_id = tny_account_get_id (TNY_ACCOUNT (instance));
+ if (!strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID)) {
+ pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_LOCAL_FOLDERS);
+ } else {
+ if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID))
+ pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_MMC);
+ else
+ pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_ACCOUNT);
+ }
+ break;
+ case TNY_FOLDER_TYPE_INBOX:
+ pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_INBOX);
break;
case TNY_FOLDER_TYPE_OUTBOX:
- pixbuf = modest_icon_factory_get_small_icon (MODEST_FOLDER_ICON_OUTBOX);
+ pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_OUTBOX);
break;
case TNY_FOLDER_TYPE_JUNK:
- pixbuf = modest_icon_factory_get_small_icon (MODEST_FOLDER_ICON_JUNK);
+ pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_JUNK);
break;
case TNY_FOLDER_TYPE_SENT:
- pixbuf = modest_icon_factory_get_small_icon (MODEST_FOLDER_ICON_SENT);
+ pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_SENT);
break;
- case TNY_FOLDER_TYPE_DRAFTS:
- pixbuf = modest_icon_factory_get_small_icon (MODEST_FOLDER_ICON_DRAFTS);
- break;
- case TNY_FOLDER_TYPE_NOTES:
- pixbuf = modest_icon_factory_get_small_icon (MODEST_FOLDER_ICON_NOTES);
- break;
- case TNY_FOLDER_TYPE_CALENDAR:
- pixbuf = modest_icon_factory_get_small_icon (MODEST_FOLDER_ICON_CALENDAR);
+ case TNY_FOLDER_TYPE_TRASH:
+ pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_TRASH);
break;
- case TNY_FOLDER_TYPE_CONTACTS:
- pixbuf = modest_icon_factory_get_small_icon (MODEST_FOLDER_ICON_CONTACTS);
+ case TNY_FOLDER_TYPE_DRAFTS:
+ pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_DRAFTS);
break;
case TNY_FOLDER_TYPE_NORMAL:
default:
- pixbuf = modest_icon_factory_get_small_icon (MODEST_FOLDER_ICON_NORMAL);
- break;
+ pixbuf = modest_platform_get_icon (MODEST_FOLDER_ICON_NORMAL);
+ break;
}
+ g_object_unref (G_OBJECT (instance));
+ g_free (fname);
- g_object_set (rendobj,
- "pixbuf-expander-open",
- modest_icon_factory_get_icon (MODEST_FOLDER_ICON_OPEN),
- "pixbuf-expander-closed",
- modest_icon_factory_get_icon (MODEST_FOLDER_ICON_CLOSED),
- "pixbuf", pixbuf,
- NULL);
+ /* Set pixbuf */
+ g_object_set (rendobj, "pixbuf", pixbuf, NULL);
}
static void
GtkTreeViewColumn *column;
GtkCellRenderer *renderer;
GtkTreeSelection *sel;
+ ModestConf *conf;
priv = MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
- priv->view_is_empty = TRUE;
+ priv->timer_expander = 0;
priv->account_store = NULL;
- priv->cur_folder = NULL;
+ priv->cur_folder_store = NULL;
priv->query = NULL;
- priv->lock = g_mutex_new ();
-
- column = gtk_tree_view_column_new ();
- gtk_tree_view_column_set_title (column,
- _("All Mail Folders"));
-
- gtk_tree_view_append_column (GTK_TREE_VIEW(obj),
- column);
+ priv->style = MODEST_FOLDER_VIEW_STYLE_SHOW_ALL;
+
+ /* Initialize the local account name */
+ conf = modest_runtime_get_conf();
+ priv->local_account_name = modest_conf_get_string (conf, MODEST_CONF_DEVICE_NAME, NULL);
+
+ /* Build treeview */
+ column = gtk_tree_view_column_new ();
renderer = gtk_cell_renderer_pixbuf_new();
gtk_tree_view_column_pack_start (column, renderer, FALSE);
gtk_tree_view_column_set_cell_data_func(column, renderer,
- icon_cell_data, NULL, NULL);
+ icon_cell_data, obj, NULL);
renderer = gtk_cell_renderer_text_new();
gtk_tree_view_column_pack_start (column, renderer, FALSE);
gtk_tree_view_column_set_cell_data_func(column, renderer,
- text_cell_data, NULL, NULL);
+ text_cell_data, obj, NULL);
sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj));
gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
gtk_tree_view_column_set_spacing (column, 2);
gtk_tree_view_column_set_resizable (column, TRUE);
gtk_tree_view_column_set_fixed_width (column, TRUE);
- gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(obj), TRUE);
gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(obj), FALSE);
gtk_tree_view_set_enable_search (GTK_TREE_VIEW(obj), FALSE);
+ gtk_tree_view_append_column (GTK_TREE_VIEW(obj),column);
+
+ /* Setup drag and drop */
+ setup_drag_and_drop (GTK_TREE_VIEW(obj));
+
+ /* Connect signals */
+ g_signal_connect (G_OBJECT (obj),
+ "key-press-event",
+ G_CALLBACK (on_key_pressed), NULL);
+
+ /*
+ * Track changes in the local account name (in the device it
+ * will be the device name)
+ */
+ g_signal_connect (G_OBJECT(conf),
+ "key_changed",
+ G_CALLBACK(on_configuration_key_changed), obj);
+
}
static void
-modest_folder_view_disconnect_store_account_handlers (GtkTreeView *self)
+tny_account_store_view_init (gpointer g, gpointer iface_data)
{
- TnyIterator *iter;
- ModestFolderViewPrivate *priv;
- GtkTreeModel *model;
- GtkTreeModelSort *sortable;
- gint i = 0;
-
- sortable = GTK_TREE_MODEL_SORT (gtk_tree_view_get_model (self));
- if (!sortable)
- return;
+ TnyAccountStoreViewIface *klass = (TnyAccountStoreViewIface *)g;
- model = gtk_tree_model_sort_get_model (sortable);
- if (!model)
- return;
+ klass->set_account_store_func = modest_folder_view_set_account_store;
- priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
- iter = tny_list_create_iterator (TNY_LIST (model));
- do {
- g_signal_handler_disconnect (G_OBJECT (tny_iterator_get_current (iter)),
- priv->store_accounts_handlers [i++]);
- tny_iterator_next (iter);
- } while (!tny_iterator_is_done (iter));
+ return;
}
-
static void
modest_folder_view_finalize (GObject *obj)
{
g_return_if_fail (obj);
priv = MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
+
+ if (priv->timer_expander != 0) {
+ g_source_remove (priv->timer_expander);
+ priv->timer_expander = 0;
+ }
+
if (priv->account_store) {
g_signal_handler_disconnect (G_OBJECT(priv->account_store),
- priv->sig1);
+ priv->account_update_signal);
+ g_signal_handler_disconnect (G_OBJECT(priv->account_store),
+ priv->accounts_reloaded_signal);
g_object_unref (G_OBJECT(priv->account_store));
priv->account_store = NULL;
}
- if (priv->lock) {
- g_mutex_free (priv->lock);
- priv->lock = NULL;
- }
-
- if (priv->store_accounts_handlers) {
- modest_folder_view_disconnect_store_account_handlers (GTK_TREE_VIEW (obj));
- g_free (priv->store_accounts_handlers);
- priv->store_accounts_handlers = NULL;
- }
-
if (priv->query) {
g_object_unref (G_OBJECT (priv->query));
priv->query = NULL;
sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj));
if (sel)
- g_signal_handler_disconnect (G_OBJECT(sel), priv->sig2);
+ g_signal_handler_disconnect (G_OBJECT(sel), priv->changed_signal);
+
+ g_free (priv->local_account_name);
G_OBJECT_CLASS(parent_class)->finalize (obj);
}
static void
+modest_folder_view_set_account_store (TnyAccountStoreView *self, TnyAccountStore *account_store)
+{
+ ModestFolderViewPrivate *priv;
+ TnyDevice *device;
+
+ g_return_if_fail (MODEST_IS_FOLDER_VIEW (self));
+ g_return_if_fail (TNY_IS_ACCOUNT_STORE (account_store));
+
+ priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
+ device = tny_account_store_get_device (account_store);
+
+ if (G_UNLIKELY (priv->account_store)) {
+
+ if (g_signal_handler_is_connected (G_OBJECT (priv->account_store),
+ priv->account_update_signal))
+ g_signal_handler_disconnect (G_OBJECT (priv->account_store),
+ priv->account_update_signal);
+ if (g_signal_handler_is_connected (G_OBJECT (priv->account_store),
+ priv->accounts_reloaded_signal))
+ g_signal_handler_disconnect (G_OBJECT (priv->account_store),
+ priv->accounts_reloaded_signal);
+
+ g_object_unref (G_OBJECT (priv->account_store));
+ }
+
+ priv->account_store = g_object_ref (G_OBJECT (account_store));
+
+ priv->account_update_signal =
+ g_signal_connect (G_OBJECT(account_store), "account_update",
+ G_CALLBACK (on_account_update), self);
+
+ priv->accounts_reloaded_signal =
+ g_signal_connect (G_OBJECT(account_store), "accounts_reloaded",
+ G_CALLBACK (on_accounts_reloaded), self);
+
+ if (!update_model (MODEST_FOLDER_VIEW (self),
+ MODEST_TNY_ACCOUNT_STORE (priv->account_store)))
+ g_printerr ("modest: failed to update model\n");
+
+ g_object_unref (G_OBJECT (device));
+}
+
+static void
on_account_update (TnyAccountStore *account_store, const gchar *account,
gpointer user_data)
{
- update_model_empty (MODEST_FOLDER_VIEW(user_data));
-
if (!update_model (MODEST_FOLDER_VIEW(user_data),
MODEST_TNY_ACCOUNT_STORE(account_store)))
g_printerr ("modest: failed to update model for changes in '%s'",
account);
}
-
-GtkWidget*
-modest_folder_view_new (ModestTnyAccountStore *account_store,
- TnyFolderStoreQuery *query)
+static void
+on_accounts_reloaded (TnyAccountStore *account_store,
+ gpointer user_data)
{
- GObject *self;
- ModestFolderViewPrivate *priv;
- GtkTreeSelection *sel;
-
- g_return_val_if_fail (account_store, NULL);
-
- self = G_OBJECT(g_object_new(MODEST_TYPE_FOLDER_VIEW, NULL));
- priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
-
- priv->account_store = g_object_ref (G_OBJECT (account_store));
- if (query)
- priv->query = g_object_ref (G_OBJECT (query));
-
- if (!update_model (MODEST_FOLDER_VIEW(self),
- MODEST_TNY_ACCOUNT_STORE(account_store)))
- g_printerr ("modest: failed to update model");
-
- priv->sig1 = g_signal_connect (G_OBJECT(account_store), "account_update",
- G_CALLBACK (on_account_update), self);
-
- sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
- priv->sig2 = g_signal_connect (sel, "changed",
- G_CALLBACK(on_selection_changed), self);
-
- return GTK_WIDGET(self);
+ update_model (MODEST_FOLDER_VIEW (user_data),
+ MODEST_TNY_ACCOUNT_STORE(account_store));
}
-
-const gchar *
-modest_folder_view_get_selected_account (ModestFolderView *self)
+void
+modest_folder_view_set_title (ModestFolderView *self, const gchar *title)
{
- GtkTreeModel *model;
- GtkTreeIter iter;
- ModestFolderViewPrivate *priv;
+ GtkTreeViewColumn *col;
- g_return_val_if_fail (self, NULL);
- priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
+ g_return_if_fail (self);
- gtk_tree_selection_get_selected (priv->cur_selection, &model, &iter);
+ col = gtk_tree_view_get_column (GTK_TREE_VIEW(self), 0);
+ if (!col) {
+ g_printerr ("modest: failed get column for title\n");
+ return;
+ }
- return get_account_name_from_folder (model, iter);
+ gtk_tree_view_column_set_title (col, title);
+ gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self),
+ title != NULL);
}
-static gboolean
-update_model_empty (ModestFolderView *self)
+GtkWidget*
+modest_folder_view_new (TnyFolderStoreQuery *query)
{
- GtkTreeIter iter;
- GtkTreeStore *store;
+ GObject *self;
ModestFolderViewPrivate *priv;
+ GtkTreeSelection *sel;
- g_return_val_if_fail (self, FALSE);
- priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
-
- /* Disconnect old handlers */
- if (priv->store_accounts_handlers) {
- modest_folder_view_disconnect_store_account_handlers (GTK_TREE_VIEW (self));
- g_free (priv->store_accounts_handlers);
- priv->store_accounts_handlers = NULL;
- }
-
- /* Create the new model */
- store = gtk_tree_store_new (1, G_TYPE_STRING);
- gtk_tree_store_append (store, &iter, NULL);
-
- gtk_tree_store_set (store, &iter, 0,
- _("(empty)"), -1);
+ self = G_OBJECT (g_object_new (MODEST_TYPE_FOLDER_VIEW, NULL));
+ priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
- gtk_tree_view_set_model (GTK_TREE_VIEW(self),
- GTK_TREE_MODEL(store));
- g_object_unref (store);
-
- priv->view_is_empty = TRUE;
+ priv->query = g_object_ref (query);
+
+ sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
+ priv->changed_signal = g_signal_connect (sel, "changed",
+ G_CALLBACK (on_selection_changed), self);
- g_signal_emit (G_OBJECT(self), signals[FOLDER_SELECTED_SIGNAL], 0,
- NULL);
- return TRUE;
+ return GTK_WIDGET(self);
}
-
+/* this feels dirty; any other way to expand all the root items? */
static void
-update_store_account_handlers (ModestFolderView *self, TnyList *account_list)
+expand_root_items (ModestFolderView *self)
{
- gint size;
- ModestFolderViewPrivate *priv;
- TnyIterator *iter;
+ GtkTreePath *path;
+ path = gtk_tree_path_new_first ();
+
+ /* all folders should have child items, so.. */
+ while (gtk_tree_view_expand_row (GTK_TREE_VIEW(self), path, FALSE))
+ gtk_tree_path_next (path);
- priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
+ gtk_tree_path_free (path);
+}
- /* Listen to subscription changes */
- size = tny_list_get_length (TNY_LIST (account_list)) * sizeof (gulong);
+/*
+ * HACK: we use this function to implement the
+ * MODEST_FOLDER_VIEW_STYLE_SHOW_ONE style. This implementation
+ * assumes that the model has the following order (which is currently
+ * true) Remote folders->Local folders->MMC folders, so the rows of
+ * the first level (that represent the accounts) are received in the
+ * same order. So basically it uses a static variable to register that
+ * a remote account has already being shown to hide the others
+ * (returning NULL). When the function evaluates the local or the MMC
+ * accounts is time to reset the static variable in order to get it
+ * ready for the next time the tree model needs to be shown.
+ */
+static gboolean
+filter_row (GtkTreeModel *model,
+ GtkTreeIter *iter,
+ gpointer data)
+{
+ static gboolean found = FALSE;
+ gboolean retval;
+ gint type;
+ GObject *instance;
- g_assert (priv->store_accounts_handlers == NULL); /* don't leak */
- priv->store_accounts_handlers = g_malloc0 (size);
- iter = tny_list_create_iterator (account_list);
-
- if (!tny_iterator_is_done (iter))
- priv->view_is_empty = FALSE;
- else {
- gint i = 0;
- while (!tny_iterator_is_done (iter)) {
-
- priv->store_accounts_handlers [i++] =
- g_signal_connect (G_OBJECT (tny_iterator_get_current (iter)),
- "subscription_changed",
- G_CALLBACK (on_subscription_changed),
- self);
- tny_iterator_next (iter);
- }
- }
- g_object_unref (iter);
+ gtk_tree_model_get (model, iter,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
+ -1);
+
+ if (type == TNY_FOLDER_TYPE_ROOT) {
+ const gchar *account_id = tny_account_get_id (TNY_ACCOUNT (instance));
+
+ if (strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) &&
+ strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) {
+
+ if (!found) {
+ found = TRUE;
+ retval = TRUE;
+ } else
+ retval = FALSE;
+ } else {
+ found = FALSE;
+ retval = TRUE;
+ }
+ } else
+ retval = TRUE;
+
+ return retval;
}
static gboolean
update_model (ModestFolderView *self, ModestTnyAccountStore *account_store)
{
ModestFolderViewPrivate *priv;
-
TnyList *account_list;
- GtkTreeModel *model, *sortable;
+ GtkTreeModel *model;
g_return_val_if_fail (account_store, FALSE);
- priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
- update_model_empty (self); /* cleanup */
+ priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
- //model = GTK_TREE_MODEL(tny_gtk_account_tree_model_new (TRUE, priv->query)); /* async */
- model = GTK_TREE_MODEL(tny_gtk_account_tree_model_new (TRUE, NULL)); /* async */
-
+ /* Notify that there is no folder selected */
+ g_signal_emit (G_OBJECT(self),
+ signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
+ NULL, TRUE);
+
+ /* FIXME: the local accounts are not shown when the query
+ selects only the subscribed folders. */
+/* model = tny_gtk_folder_store_tree_model_new (TRUE, priv->query); */
+ model = tny_gtk_folder_store_tree_model_new (TRUE, NULL);
account_list = TNY_LIST(model);
tny_account_store_get_accounts (TNY_ACCOUNT_STORE(account_store),
account_list,
- TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
-
+ TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
if (account_list) {
+ GtkTreeModel *filter_model = NULL, *sortable = NULL;
+
sortable = gtk_tree_model_sort_new_with_model (model);
- gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (sortable),
- TNY_GTK_ACCOUNT_TREE_MODEL_NAME_COLUMN,
+ gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(sortable),
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
GTK_SORT_ASCENDING);
- gtk_tree_view_set_model (GTK_TREE_VIEW(self), sortable);
-
- update_store_account_handlers (self, account_list);
+ gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (sortable),
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
+ cmp_rows, NULL, NULL);
+
+ /* Create filter model */
+ if (priv->style == MODEST_FOLDER_VIEW_STYLE_SHOW_ONE) {
+ filter_model = gtk_tree_model_filter_new (sortable, NULL);
+ gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (filter_model),
+ filter_row,
+ NULL,
+ NULL);
+ }
+
+ /* Set new model */
+ gtk_tree_view_set_model (GTK_TREE_VIEW(self),
+ (filter_model) ? filter_model : sortable);
+ expand_root_items (self); /* expand all account folders */
+ g_object_unref (account_list);
}
-
- g_object_unref (model);
+
return TRUE;
}
on_selection_changed (GtkTreeSelection *sel, gpointer user_data)
{
GtkTreeModel *model;
- TnyFolder *folder = NULL;
+ TnyFolderStore *folder = NULL;
GtkTreeIter iter;
ModestFolderView *tree_view;
ModestFolderViewPrivate *priv;
- gint type;
- const gchar *account_name;
+ gint type;
g_return_if_fail (sel);
g_return_if_fail (user_data);
-
+
priv = MODEST_FOLDER_VIEW_GET_PRIVATE(user_data);
priv->cur_selection = sel;
-
- /* is_empty means that there is only the 'empty' item */
- if (priv->view_is_empty)
- return;
/* folder was _un_selected if true */
if (!gtk_tree_selection_get_selected (sel, &model, &iter)) {
- priv->cur_folder = NULL; /* FIXME: need this? */
+ if (priv->cur_folder_store)
+ g_object_unref (priv->cur_folder_store);
+ priv->cur_folder_store = NULL;
+
+ /* Notify the display name observers */
+ g_signal_emit (G_OBJECT(user_data),
+ signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
+ NULL);
return;
}
tree_view = MODEST_FOLDER_VIEW (user_data);
- gtk_tree_model_get (model, &iter,
- TNY_GTK_ACCOUNT_TREE_MODEL_TYPE_COLUMN, &type,
- TNY_GTK_ACCOUNT_TREE_MODEL_INSTANCE_COLUMN, &folder,
+ gtk_tree_model_get (model, &iter,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder,
-1);
+ /* If the folder is the same do not notify */
+ if (priv->cur_folder_store == folder) {
+ g_object_unref (folder);
+ return;
+ }
+
+ /* Current folder was unselected */
+ if (priv->cur_folder_store) {
+ g_signal_emit (G_OBJECT(tree_view), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
+ priv->cur_folder_store, FALSE);
+ g_object_unref (priv->cur_folder_store);
+ }
+
+ /* New current references */
+ priv->cur_folder_store = folder;
+
+ /* New folder has been selected */
+ g_signal_emit (G_OBJECT(tree_view),
+ signals[FOLDER_SELECTION_CHANGED_SIGNAL],
+ 0, folder, TRUE);
+}
+
+TnyFolderStore *
+modest_folder_view_get_selected (ModestFolderView *self)
+{
+ ModestFolderViewPrivate *priv;
+
+ g_return_val_if_fail (self, NULL);
+
+ priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
+ if (priv->cur_folder_store)
+ g_object_ref (priv->cur_folder_store);
+
+ return priv->cur_folder_store;
+}
+
+/*
+ * This function orders the mail accounts following the next rules
+ * 1st - remote accounts
+ * 2nd - local account
+ * 3rd - MMC account
+ */
+static gint
+cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2,
+ gpointer user_data)
+{
+ gint cmp;
+ gchar *name1, *name2;
+ TnyFolderType type;
+ TnyFolder *folder1, *folder2;
+
+ gtk_tree_model_get (tree_model, iter1,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name1,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder1,
+ -1);
+ gtk_tree_model_get (tree_model, iter2,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name2,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder2,
+ -1);
+
+ /* Order must be: Remote accounts -> Local account -> MMC account */
if (type == TNY_FOLDER_TYPE_ROOT) {
- account_name = tny_account_get_name (TNY_ACCOUNT (folder));
+ const gchar *account_id = tny_account_get_id (TNY_ACCOUNT (folder1));
+ const gchar *account_id2 = tny_account_get_id (TNY_ACCOUNT (folder2));
+
+ if (!strcmp (account_id, MODEST_MMC_ACCOUNT_ID))
+ cmp = +1;
+ else {
+ if (!strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID)) {
+ if (!strcmp (account_id2, MODEST_MMC_ACCOUNT_ID))
+ cmp = -1;
+ else
+ cmp = +1;
+ } else {
+ if (!strcmp (account_id2, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) ||
+ !strcmp (account_id2, MODEST_MMC_ACCOUNT_ID))
+ cmp = -1;
+ else
+ cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
+ }
+ }
+ } else
+ cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
+
+ if (folder1)
+ g_object_unref(G_OBJECT(folder1));
+ if (folder2)
+ g_object_unref(G_OBJECT(folder2));
+
+ g_free (name1);
+ g_free (name2);
+
+ return cmp;
+}
+
+/*****************************************************************************/
+/* DRAG and DROP stuff */
+/*****************************************************************************/
+
+/*
+ * This function fills the #GtkSelectionData with the row and the
+ * model that has been dragged. It's called when this widget is a
+ * source for dnd after the event drop happened
+ */
+static void
+on_drag_data_get (GtkWidget *widget,
+ GdkDragContext *context,
+ GtkSelectionData *selection_data,
+ guint info,
+ guint time,
+ gpointer data)
+{
+ GtkTreeSelection *selection;
+ GtkTreeModel *model_sort, *model;
+ GtkTreeIter iter;
+ GtkTreePath *source_row_sort;
+ GtkTreePath *source_row;
+
+ selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
+ gtk_tree_selection_get_selected (selection, &model_sort, &iter);
+ source_row_sort = gtk_tree_model_get_path (model_sort, &iter);
+
+ /* Get the unsorted path and model */
+ model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
+ source_row = gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
+ source_row_sort);
+
+ gtk_tree_set_row_drag_data (selection_data,
+ model,
+ source_row);
+
+ gtk_tree_path_free (source_row_sort);
+ gtk_tree_path_free (source_row);
+}
+
+typedef struct _DndHelper {
+ gboolean delete_source;
+ GtkTreePath *source_row;
+ GdkDragContext *context;
+ guint time;
+} DndHelper;
+
+
+/*
+ * This function is the callback of the
+ * modest_mail_operation_xfer_msg() and
+ * modest_mail_operation_xfer_folder() calls. We check here if the
+ * message/folder was correctly asynchronously transferred. The reason
+ * to use the same callback is that the code is the same, it only has
+ * to check that the operation went fine and then finalize the drag
+ * and drop action
+ */
+static void
+on_progress_changed (ModestMailOperation *mail_op, gpointer user_data)
+{
+ gboolean success;
+ DndHelper *helper;
+
+ helper = (DndHelper *) user_data;
+
+ if (!modest_mail_operation_is_finished (mail_op))
+ return;
+
+ if (modest_mail_operation_get_status (mail_op) ==
+ MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
+ success = TRUE;
} else {
- if (priv->cur_folder)
- tny_folder_expunge (priv->cur_folder);
- priv->cur_folder = folder;
-
- /* FIXME: this is ugly */
- account_name = get_account_name_from_folder (model, iter);
-
- g_signal_emit (G_OBJECT(tree_view), signals[FOLDER_SELECTED_SIGNAL], 0,
- folder);
+ success = FALSE;
}
+
+ /* Notify the drag source. Never call delete, the monitor will
+ do the job if needed */
+ gtk_drag_finish (helper->context, success, FALSE, helper->time);
+
+ /* Free the helper */
+ gtk_tree_path_free (helper->source_row);
+ g_slice_free (DndHelper, helper);
+}
+
+/*
+ * This function is used by drag_data_received_cb to manage drag and
+ * drop of a header, i.e, and drag from the header view to the folder
+ * view.
+ */
+static void
+drag_and_drop_from_header_view (GtkTreeModel *source_model,
+ GtkTreeModel *dest_model,
+ GtkTreePath *dest_row,
+ DndHelper *helper)
+{
+ TnyHeader *header;
+ TnyFolder *folder;
+ ModestMailOperation *mail_op;
+ GtkTreeIter source_iter, dest_iter;
+
+ /* Get header */
+ gtk_tree_model_get_iter (source_model, &source_iter, helper->source_row);
+ gtk_tree_model_get (source_model, &source_iter,
+ TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN,
+ &header, -1);
+
+ /* Get Folder */
+ gtk_tree_model_get_iter (dest_model, &dest_iter, dest_row);
+ gtk_tree_model_get (dest_model, &dest_iter,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
+ &folder, -1);
+
+ /* Transfer message */
+ mail_op = modest_mail_operation_new ();
+ modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
+ mail_op);
+ g_signal_connect (G_OBJECT (mail_op), "progress-changed",
+ G_CALLBACK (on_progress_changed), helper);
+
+ modest_mail_operation_xfer_msg (mail_op, header, folder, helper->delete_source);
+
+ /* Frees */
+ g_object_unref (G_OBJECT (mail_op));
+ g_object_unref (G_OBJECT (header));
+ g_object_unref (G_OBJECT (folder));
}
+/*
+ * This function is used by drag_data_received_cb to manage drag and
+ * drop of a folder, i.e, and drag from the folder view to the same
+ * folder view.
+ */
+static void
+drag_and_drop_from_folder_view (GtkTreeModel *source_model,
+ GtkTreeModel *dest_model,
+ GtkTreePath *dest_row,
+ GtkSelectionData *selection_data,
+ DndHelper *helper)
+{
+ ModestMailOperation *mail_op;
+ GtkTreeIter parent_iter, iter;
+ TnyFolderStore *parent_folder;
+ TnyFolder *folder;
+
+ /* Check if the drag is possible */
+ if (!gtk_tree_path_compare (helper->source_row, dest_row) ||
+ !gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (dest_model),
+ dest_row,
+ selection_data)) {
+
+ gtk_drag_finish (helper->context, FALSE, FALSE, helper->time);
+ gtk_tree_path_free (helper->source_row);
+ g_slice_free (DndHelper, helper);
+ return;
+ }
+
+ /* Get data */
+ gtk_tree_model_get_iter (source_model, &parent_iter, dest_row);
+ gtk_tree_model_get_iter (source_model, &iter, helper->source_row);
+ gtk_tree_model_get (source_model, &parent_iter,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
+ &parent_folder, -1);
+ gtk_tree_model_get (source_model, &iter,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
+ &folder, -1);
+
+ /* Do the mail operation */
+ mail_op = modest_mail_operation_new ();
+ modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
+ mail_op);
+ g_signal_connect (G_OBJECT (mail_op), "progress-changed",
+ G_CALLBACK (on_progress_changed), helper);
+
+ modest_mail_operation_xfer_folder (mail_op,
+ folder,
+ parent_folder,
+ helper->delete_source);
+
+ /* Frees */
+ g_object_unref (G_OBJECT (parent_folder));
+ g_object_unref (G_OBJECT (folder));
+ g_object_unref (G_OBJECT (mail_op));
+}
+
+/*
+ * This function receives the data set by the "drag-data-get" signal
+ * handler. This information comes within the #GtkSelectionData. This
+ * function will manage both the drags of folders of the treeview and
+ * drags of headers of the header view widget.
+ */
static void
-on_subscription_changed (TnyStoreAccount *store_account,
- TnyFolder *folder,
- ModestFolderView *self)
+on_drag_data_received (GtkWidget *widget,
+ GdkDragContext *context,
+ gint x,
+ gint y,
+ GtkSelectionData *selection_data,
+ guint target_type,
+ guint time,
+ gpointer data)
{
- /* TODO: probably we won't need a full reload, just the store
- account or even the parent of the folder */
+ GtkWidget *source_widget;
+ GtkTreeModel *model_sort, *dest_model, *source_model;
+ GtkTreePath *source_row, *dest_row, *child_dest_row;
+ GtkTreeViewDropPosition pos;
+ gboolean success = FALSE, delete_source = FALSE;
+ DndHelper *helper;
+
+ /* Do not allow further process */
+ g_signal_stop_emission_by_name (widget, "drag-data-received");
+
+ /* Get the action */
+ if (context->action == GDK_ACTION_MOVE)
+ delete_source = TRUE;
+
+ /* Check if the get_data failed */
+ if (selection_data == NULL || selection_data->length < 0)
+ gtk_drag_finish (context, success, FALSE, time);
+
+ /* Get the models */
+ source_widget = gtk_drag_get_source_widget (context);
+ gtk_tree_get_row_drag_data (selection_data,
+ &source_model,
+ &source_row);
+
+ model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
+ /* Select the destination model */
+ if (source_widget == widget) {
+ dest_model = source_model;
+ } else {
+ dest_model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
+ }
- ModestFolderViewPrivate *priv;
+ /* Get the path to the destination row. Can not call
+ gtk_tree_view_get_drag_dest_row() because the source row
+ is not selected anymore */
+ gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget), x, y,
+ &dest_row, &pos);
+
+ /* Only allow drops IN other rows */
+ if (!dest_row || pos == GTK_TREE_VIEW_DROP_BEFORE || pos == GTK_TREE_VIEW_DROP_AFTER)
+ gtk_drag_finish (context, success, FALSE, time);
+
+ /* Create the helper */
+ helper = g_slice_new0 (DndHelper);
+ helper->delete_source = delete_source;
+ helper->source_row = gtk_tree_path_copy (source_row);
+ helper->context = context;
+ helper->time = time;
+
+ /* Get path from the unsorted model */
+ child_dest_row =
+ gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
+ dest_row);
+ gtk_tree_path_free (dest_row);
+
+ /* Drags from the header view */
+ if (source_widget != widget) {
+
+ drag_and_drop_from_header_view (source_model,
+ dest_model,
+ child_dest_row,
+ helper);
+ } else {
- priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
- update_model (self, MODEST_TNY_ACCOUNT_STORE (priv->account_store));
+
+ drag_and_drop_from_folder_view (source_model,
+ dest_model,
+ child_dest_row,
+ selection_data,
+ helper);
+ }
+
+ /* Frees */
+ gtk_tree_path_free (source_row);
+ gtk_tree_path_free (child_dest_row);
+}
+
+/*
+ * We define a "drag-drop" signal handler because we do not want to
+ * use the default one, because the default one always calls
+ * gtk_drag_finish and we prefer to do it in the "drag-data-received"
+ * signal handler, because there we have all the information available
+ * to know if the dnd was a success or not.
+ */
+static gboolean
+drag_drop_cb (GtkWidget *widget,
+ GdkDragContext *context,
+ gint x,
+ gint y,
+ guint time,
+ gpointer user_data)
+{
+ gpointer target;
+
+ if (!context->targets)
+ return FALSE;
+
+ /* Check if we're dragging a folder row */
+ target = gtk_drag_dest_find_target (widget, context, NULL);
+
+ /* Request the data from the source. */
+ gtk_drag_get_data(widget, context, target, time);
+
+ return TRUE;
}
+/*
+ * This function expands a node of a tree view if it's not expanded
+ * yet. Not sure why it needs the threads stuff, but gtk+`example code
+ * does that, so that's why they're here.
+ */
+static gint
+expand_row_timeout (gpointer data)
+{
+ GtkTreeView *tree_view = data;
+ GtkTreePath *dest_path = NULL;
+ GtkTreeViewDropPosition pos;
+ gboolean result = FALSE;
+
+ GDK_THREADS_ENTER ();
+
+ gtk_tree_view_get_drag_dest_row (tree_view,
+ &dest_path,
+ &pos);
+
+ if (dest_path &&
+ (pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER ||
+ pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)) {
+ gtk_tree_view_expand_row (tree_view, dest_path, FALSE);
+ gtk_tree_path_free (dest_path);
+ }
+ else {
+ if (dest_path)
+ gtk_tree_path_free (dest_path);
+
+ result = TRUE;
+ }
+
+ GDK_THREADS_LEAVE ();
+
+ return result;
+}
+/*
+ * This function is called whenever the pointer is moved over a widget
+ * while dragging some data. It installs a timeout that will expand a
+ * node of the treeview if not expanded yet. This function also calls
+ * gdk_drag_status in order to set the suggested action that will be
+ * used by the "drag-data-received" signal handler to know if we
+ * should do a move or just a copy of the data.
+ */
static gboolean
-modest_folder_view_update_model (ModestFolderView *self, TnyAccountStore *account_store)
+on_drag_motion (GtkWidget *widget,
+ GdkDragContext *context,
+ gint x,
+ gint y,
+ guint time,
+ gpointer user_data)
{
- gboolean retval;
+ GtkTreeViewDropPosition pos;
+ GtkTreePath *dest_row;
+ ModestFolderViewPrivate *priv;
+ GdkDragAction suggested_action;
+ gboolean valid_location = FALSE;
+
+ priv = MODEST_FOLDER_VIEW_GET_PRIVATE (widget);
+
+ if (priv->timer_expander != 0) {
+ g_source_remove (priv->timer_expander);
+ priv->timer_expander = 0;
+ }
+
+ gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
+ x, y,
+ &dest_row,
+ &pos);
+
+ /* Do not allow drops between folders */
+ if (!dest_row ||
+ pos == GTK_TREE_VIEW_DROP_BEFORE ||
+ pos == GTK_TREE_VIEW_DROP_AFTER) {
+ gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW (widget), NULL, 0);
+ gdk_drag_status(context, 0, time);
+ valid_location = FALSE;
+ goto out;
+ } else {
+ valid_location = TRUE;
+ }
+
+ /* Expand the selected row after 1/2 second */
+ if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), dest_row)) {
+ gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), dest_row, pos);
+ priv->timer_expander = g_timeout_add (500, expand_row_timeout, widget);
+ }
+ gtk_tree_path_free (dest_row);
+
+ /* Select the desired action. By default we pick MOVE */
+ suggested_action = GDK_ACTION_MOVE;
+
+ if (context->actions == GDK_ACTION_COPY)
+ gdk_drag_status(context, GDK_ACTION_COPY, time);
+ else if (context->actions == GDK_ACTION_MOVE)
+ gdk_drag_status(context, GDK_ACTION_MOVE, time);
+ else if (context->actions & suggested_action)
+ gdk_drag_status(context, suggested_action, time);
+ else
+ gdk_drag_status(context, GDK_ACTION_DEFAULT, time);
+
+ out:
+ g_signal_stop_emission_by_name (widget, "drag-motion");
+ return valid_location;
+}
+
+
+/* Folder view drag types */
+const GtkTargetEntry folder_view_drag_types[] =
+{
+ { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, MODEST_FOLDER_ROW },
+ { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_APP, MODEST_HEADER_ROW }
+};
+
+/*
+ * This function sets the treeview as a source and a target for dnd
+ * events. It also connects all the requirede signals.
+ */
+static void
+setup_drag_and_drop (GtkTreeView *self)
+{
+ /* Set up the folder view as a dnd destination. Set only the
+ highlight flag, otherwise gtk will have a different
+ behaviour */
+ gtk_drag_dest_set (GTK_WIDGET (self),
+ GTK_DEST_DEFAULT_HIGHLIGHT,
+ folder_view_drag_types,
+ G_N_ELEMENTS (folder_view_drag_types),
+ GDK_ACTION_MOVE | GDK_ACTION_COPY);
+
+ g_signal_connect (G_OBJECT (self),
+ "drag_data_received",
+ G_CALLBACK (on_drag_data_received),
+ NULL);
+
+
+ /* Set up the treeview as a dnd source */
+ gtk_drag_source_set (GTK_WIDGET (self),
+ GDK_BUTTON1_MASK,
+ folder_view_drag_types,
+ G_N_ELEMENTS (folder_view_drag_types),
+ GDK_ACTION_MOVE | GDK_ACTION_COPY);
+
+ g_signal_connect (G_OBJECT (self),
+ "drag_motion",
+ G_CALLBACK (on_drag_motion),
+ NULL);
- g_return_val_if_fail (MODEST_IS_FOLDER_VIEW (self), FALSE);
- retval = update_model (self, MODEST_TNY_ACCOUNT_STORE(account_store)); /* ugly */
+ g_signal_connect (G_OBJECT (self),
+ "drag_data_get",
+ G_CALLBACK (on_drag_data_get),
+ NULL);
+
+ g_signal_connect (G_OBJECT (self),
+ "drag_drop",
+ G_CALLBACK (drag_drop_cb),
+ NULL);
+}
- g_signal_emit (G_OBJECT(self), signals[FOLDER_SELECTED_SIGNAL],
- 0, NULL);
+/*
+ * This function manages the navigation through the folders using the
+ * keyboard or the hardware keys in the device
+ */
+static gboolean
+on_key_pressed (GtkWidget *self,
+ GdkEventKey *event,
+ gpointer user_data)
+{
+ GtkTreeSelection *selection;
+ GtkTreeIter iter;
+ GtkTreeModel *model;
+ gboolean retval = FALSE;
+
+ /* Up and Down are automatically managed by the treeview */
+ if (event->keyval == GDK_Return) {
+ /* Expand/Collapse the selected row */
+ selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
+ if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
+ GtkTreePath *path;
+
+ path = gtk_tree_model_get_path (model, &iter);
+
+ if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (self), path))
+ gtk_tree_view_collapse_row (GTK_TREE_VIEW (self), path);
+ else
+ gtk_tree_view_expand_row (GTK_TREE_VIEW (self), path, FALSE);
+ gtk_tree_path_free (path);
+ }
+ /* No further processing */
+ retval = TRUE;
+ }
return retval;
}
-static const gchar *
-get_account_name_from_folder (GtkTreeModel *model, GtkTreeIter iter)
+/*
+ * We listen to the changes in the local folder account name key,
+ * because we want to show the right name in the view. The local
+ * folder account name corresponds to the device name in the Maemo
+ * version. We do this because we do not want to query gconf on each
+ * tree view refresh. It's better to cache it and change whenever
+ * necessary.
+ */
+static void
+on_configuration_key_changed (ModestConf* conf,
+ const gchar *key,
+ ModestConfEvent event,
+ ModestFolderView *self)
{
- GtkTreePath *path;
- GtkTreeIter new_iter;
- TnyFolder *account_folder;
- gint depth, i;
+ ModestFolderViewPrivate *priv;
- path = gtk_tree_model_get_path (model, &iter);
- depth = gtk_tree_path_get_depth (path);
+ if (!key || strcmp (key, MODEST_CONF_DEVICE_NAME))
+ return;
- for (i = 1; i < depth; ++i)
- gtk_tree_path_up (path);
+ priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
- gtk_tree_model_get_iter (model, &new_iter, path);
- gtk_tree_model_get (model, &new_iter,
- TNY_GTK_ACCOUNT_TREE_MODEL_INSTANCE_COLUMN, &account_folder,
- -1);
- return tny_account_get_name (TNY_ACCOUNT (account_folder));
+ g_free (priv->local_account_name);
+
+ if (event == MODEST_CONF_EVENT_KEY_UNSET)
+ priv->local_account_name = g_strdup (MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME);
+ else
+ priv->local_account_name = modest_conf_get_string (modest_runtime_get_conf(),
+ MODEST_CONF_DEVICE_NAME, NULL);
+
+ /* Force a redraw */
+#if GTK_CHECK_VERSION(2, 8, 0) /* gtk_tree_view_column_queue_resize is only available in GTK+ 2.8 */
+ GtkTreeViewColumn * tree_column = gtk_tree_view_get_column (GTK_TREE_VIEW (self),
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN);
+ gtk_tree_view_column_queue_resize (tree_column);
+#endif
+}
+
+void
+modest_folder_view_set_style (ModestFolderView *self,
+ ModestFolderViewStyle style)
+{
+ ModestFolderViewPrivate *priv;
+
+ g_return_if_fail (self);
+
+ priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
+
+ priv->style = style;
}