From: Peter Csaszar Date: Fri, 17 Aug 2007 14:14:19 +0000 (+0000) Subject: There was a bug 60714 which was partially fixed by me in revision X-Git-Tag: git_migration_finished~2570 X-Git-Url: http://git.maemo.org/git/?p=modest;a=commitdiff_plain;h=7c8a19df4e4793891f82d8d2c462b33d89f1c18f There was a bug 60714 which was partially fixed by me in revision 2959. However the real problem was that once there was an update (by folder selection or msg retrieval) in header-view for a folder whose msg was shown in msg-view, the model referenced by msg-view was not the same with the model in header-view. Because of this the dimming for prev/next buttons could had not be synchronous with header-view. There is an observer now. The observed is the meader-view and the observer is the msg-view-window. The target is to detect if the model of header-view is replaced. Another essential part is the callback for row-insertions on the model. This can be found in modest-msg-view-window.c file. pmo-trunk-r3010 --- diff --git a/src/maemo/modest-msg-view-window.c b/src/maemo/modest-msg-view-window.c index c094dc8..59c1651 100644 --- a/src/maemo/modest-msg-view-window.c +++ b/src/maemo/modest-msg-view-window.c @@ -59,6 +59,8 @@ static void modest_msg_view_window_class_init (ModestMsgViewWindowClass *klass); static void modest_msg_view_window_init (ModestMsgViewWindow *obj); +static void modest_header_view_observer_init( + ModestHeaderViewObserverIface *iface_class); static void modest_msg_view_window_finalize (GObject *obj); static void modest_msg_view_window_toggle_find_toolbar (GtkToggleAction *obj, gpointer data); @@ -102,8 +104,8 @@ void modest_msg_view_window_on_row_deleted( void modest_msg_view_window_on_row_inserted( GtkTreeModel *header_model, - GtkTreePath *arg1, - GtkTreeIter *arg2, + GtkTreePath *tree_path, + GtkTreeIter *tree_iter, ModestMsgViewWindow *window); void modest_msg_view_window_on_row_reordered( @@ -113,6 +115,11 @@ void modest_msg_view_window_on_row_reordered( gpointer arg3, ModestMsgViewWindow *window); +void modest_msg_view_window_update_model_replaced( + ModestHeaderViewObserver *window, + GtkTreeModel *model, + const gchar *tny_folder_id); + static void cancel_progressbar (GtkToolButton *toolbutton, ModestMsgViewWindow *self); @@ -187,13 +194,18 @@ struct _ModestMsgViewWindowPrivate { * to allow selecting previous/next messages, * if the message is currently selected in the header view. */ + const gchar *header_folder_id; GtkTreeModel *header_model; GtkTreeRowReference *row_reference; GtkTreeRowReference *next_row_reference; - guint clipboard_change_handler; - guint queue_change_handler; - guint account_removed_handler; + gulong clipboard_change_handler; + gulong queue_change_handler; + gulong account_removed_handler; + gulong row_changed_handler; + gulong row_deleted_handler; + gulong row_inserted_handler; + gulong rows_reordered_handler; guint purge_timeout; GtkWidget *remove_attachment_banner; @@ -232,6 +244,17 @@ modest_msg_view_window_get_type (void) my_type = g_type_register_static (MODEST_TYPE_WINDOW, "ModestMsgViewWindow", &my_info, 0); + + static const GInterfaceInfo modest_header_view_observer_info = + { + (GInterfaceInitFunc) modest_header_view_observer_init, + NULL, /* interface_finalize */ + NULL /* interface_data */ + }; + + g_type_add_interface_static (my_type, + MODEST_TYPE_HEADER_VIEW_OBSERVER, + &modest_header_view_observer_info); } return my_type; } @@ -285,17 +308,30 @@ modest_msg_view_window_class_init (ModestMsgViewWindowClass *klass) G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER); } +static void modest_header_view_observer_init( + ModestHeaderViewObserverIface *iface_class) +{ + iface_class->update_func = modest_msg_view_window_update_model_replaced; +} + static void modest_msg_view_window_init (ModestMsgViewWindow *obj) { ModestMsgViewWindowPrivate *priv; priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(obj); + priv->is_search_result = FALSE; + priv->msg_view = NULL; priv->header_model = NULL; + priv->header_folder_id = NULL; priv->clipboard_change_handler = 0; priv->queue_change_handler = 0; priv->account_removed_handler = 0; + priv->row_changed_handler = 0; + priv->row_deleted_handler = 0; + priv->row_inserted_handler = 0; + priv->rows_reordered_handler = 0; priv->current_toolbar_mode = TOOLBAR_MODE_NORMAL; priv->optimized_view = FALSE; @@ -481,6 +517,9 @@ static void modest_msg_view_window_disconnect_signals (ModestWindow *self) { ModestMsgViewWindowPrivate *priv; + ModestHeaderView *header_view = NULL; + ModestMainWindow *main_window = NULL; + ModestWindowMgr *window_mgr = NULL; priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self); @@ -498,6 +537,45 @@ modest_msg_view_window_disconnect_signals (ModestWindow *self) priv->account_removed_handler)) g_signal_handler_disconnect (G_OBJECT (modest_runtime_get_account_store ()), priv->account_removed_handler); + + if (g_signal_handler_is_connected(G_OBJECT (priv->header_model), + priv->row_changed_handler)) + g_signal_handler_disconnect(G_OBJECT (priv->header_model), + priv->row_changed_handler); + + if (g_signal_handler_is_connected(G_OBJECT (priv->header_model), + priv->row_deleted_handler)) + g_signal_handler_disconnect(G_OBJECT (priv->header_model), + priv->row_deleted_handler); + + if (g_signal_handler_is_connected(G_OBJECT (priv->header_model), + priv->row_inserted_handler)) + g_signal_handler_disconnect(G_OBJECT (priv->header_model), + priv->row_inserted_handler); + + if (g_signal_handler_is_connected(G_OBJECT (priv->header_model), + priv->rows_reordered_handler)) + g_signal_handler_disconnect(G_OBJECT (priv->header_model), + priv->rows_reordered_handler); + + window_mgr = modest_runtime_get_window_mgr(); + g_assert(window_mgr != NULL); + + main_window = MODEST_MAIN_WINDOW( + modest_window_mgr_get_main_window(window_mgr)); + + if(main_window == NULL) + return; + + header_view = MODEST_HEADER_VIEW( + modest_main_window_get_child_widget( + main_window, MODEST_WIDGET_TYPE_HEADER_VIEW)); + + if (header_view == NULL) + return; + + modest_header_view_remove_observer(header_view, + MODEST_HEADER_VIEW_OBSERVER(self)); } static void @@ -738,6 +816,10 @@ modest_msg_view_window_new_with_header_model (TnyMsg *msg, { ModestMsgViewWindow *window = NULL; ModestMsgViewWindowPrivate *priv = NULL; + TnyFolder *header_folder = NULL; + ModestHeaderView *header_view = NULL; + ModestMainWindow *main_window = NULL; + ModestWindowMgr *window_mgr = NULL; window = g_object_new(MODEST_TYPE_MSG_VIEW_WINDOW, NULL); g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window), NULL); @@ -747,24 +829,47 @@ modest_msg_view_window_new_with_header_model (TnyMsg *msg, /* Remember the message list's TreeModel so we can detect changes * and change the list selection when necessary: */ - g_object_ref (model); - priv->header_model = model; + window_mgr = modest_runtime_get_window_mgr(); + g_assert(window_mgr != NULL); + main_window = MODEST_MAIN_WINDOW( + modest_window_mgr_get_main_window(window_mgr)); + g_assert(main_window != NULL); + header_view = MODEST_HEADER_VIEW(modest_main_window_get_child_widget( + main_window, MODEST_WIDGET_TYPE_HEADER_VIEW)); + if (header_view != NULL){ + header_folder = modest_header_view_get_folder(header_view); + g_assert(header_folder != NULL); + priv->header_folder_id = tny_folder_get_id(header_folder); + g_assert(priv->header_folder_id != NULL); + g_object_unref(header_folder); + } + + priv->header_model = g_object_ref(model); priv->row_reference = gtk_tree_row_reference_copy (row_reference); priv->next_row_reference = gtk_tree_row_reference_copy (row_reference); select_next_valid_row (model, &(priv->next_row_reference), TRUE); - g_signal_connect (GTK_TREE_MODEL(model), "row-changed", - G_CALLBACK (modest_msg_view_window_on_row_changed), - window); - g_signal_connect (GTK_TREE_MODEL(model), "row-deleted", - G_CALLBACK (modest_msg_view_window_on_row_deleted), - window); - g_signal_connect (GTK_TREE_MODEL(model), "row-inserted", - G_CALLBACK (modest_msg_view_window_on_row_inserted), - window); - g_signal_connect (GTK_TREE_MODEL(model), "rows-reordered", - G_CALLBACK (modest_msg_view_window_on_row_reordered), - window); + priv->row_changed_handler = g_signal_connect( + GTK_TREE_MODEL(model), "row-changed", + G_CALLBACK(modest_msg_view_window_on_row_changed), + window); + priv->row_deleted_handler = g_signal_connect( + GTK_TREE_MODEL(model), "row-deleted", + G_CALLBACK(modest_msg_view_window_on_row_deleted), + window); + priv->row_inserted_handler = g_signal_connect ( + GTK_TREE_MODEL(model), "row-inserted", + G_CALLBACK(modest_msg_view_window_on_row_inserted), + window); + priv->rows_reordered_handler = g_signal_connect( + GTK_TREE_MODEL(model), "rows-reordered", + G_CALLBACK(modest_msg_view_window_on_row_reordered), + window); + + if (header_view != NULL){ + modest_header_view_add_observer(header_view, + MODEST_HEADER_VIEW_OBSERVER(window)); + } modest_msg_view_window_update_priority (window); @@ -825,11 +930,83 @@ void modest_msg_view_window_on_row_deleted( modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (window)); } +/* On insertions we check if the folder still has the message we are + * showing or do not. If do not, we do nothing. Which means we are still + * not attached to any header folder and thus next/prev buttons are + * still dimmed. Once the message that is shown by msg-view is found, the + * new model of header-view will be attached and the references will be set. + * On each further insertions dimming rules will be checked. However + * this requires extra CPU time at least works. + * (An message might be deleted from TnyFolder and thus will not be + * inserted into the model again for example if it is removed by the + * imap server and the header view is refreshed.) + */ void modest_msg_view_window_on_row_inserted( - GtkTreeModel *header_model, - GtkTreePath *arg1, - GtkTreeIter *arg2, + GtkTreeModel *new_model, + GtkTreePath *tree_path, + GtkTreeIter *tree_iter, ModestMsgViewWindow *window){ + ModestMsgViewWindowPrivate *priv = NULL; + TnyHeader *header = NULL; + gchar *uid = NULL; + + g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window)); + + priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window); + + /* If we already has a model attached then the message shown by + * msg-view is in it, and thus we do not need any actions but + * to check the dimming rules.*/ + if(priv->header_model != NULL){ + gtk_tree_row_reference_free(priv->next_row_reference); + priv->next_row_reference = gtk_tree_row_reference_copy( + priv->row_reference); + select_next_valid_row (priv->header_model, + &(priv->next_row_reference), FALSE); + modest_ui_actions_check_toolbar_dimming_rules ( + MODEST_WINDOW (window)); + return; + } + + /* Check if the newly inserted message is the same we are actually + * showing. IF not, we should remain detached from the header model + * and thus prev and next toolbarbuttons should remain dimmed. */ + gtk_tree_model_get (new_model, tree_iter, + TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN, &header, -1); + uid = modest_tny_folder_get_header_unique_id(header); + if(!g_str_equal(priv->msg_uid, uid)){ + g_free(uid); + return; + } + g_free(uid); + + /* Setup row_reference for the actual msg. */ + priv->row_reference = gtk_tree_row_reference_new( + new_model, tree_path); + if(priv->row_reference == NULL){ + g_warning("No reference for msg header item."); + return; + } + + /* Attach new_model and connect some callback to it to become able + * to detect changes in header-view. */ + priv->header_model = g_object_ref(new_model); + g_signal_connect (new_model, "row-changed", + G_CALLBACK (modest_msg_view_window_on_row_changed), + window); + g_signal_connect (new_model, "row-deleted", + G_CALLBACK (modest_msg_view_window_on_row_deleted), + window); + g_signal_connect (new_model, "rows-reordered", + G_CALLBACK (modest_msg_view_window_on_row_reordered), + window); + + /* Now set up next_row_reference. */ + priv->next_row_reference = gtk_tree_row_reference_copy( + priv->row_reference); + select_next_valid_row (priv->header_model, + &(priv->next_row_reference), FALSE); + modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (window)); } @@ -842,6 +1019,83 @@ void modest_msg_view_window_on_row_reordered( modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (window)); } +/* The modest_msg_view_window_update_model_replaced implements update + * function for ModestHeaderViewObserver. Checks whether the TnyFolder + * actually belongs to the header-view is the same as the TnyFolder of + * the message of msg-view or not. If they are different, there is + * nothing to do. If they are the same, then the model has replaced and + * the reference in msg-view shall be replaced from the old model to + * the new model. In this case the view will be detached from it's + * header folder. From this point the next/prev buttons are dimmed. + */ +void modest_msg_view_window_update_model_replaced( + ModestHeaderViewObserver *observer, + GtkTreeModel *model, + const gchar *tny_folder_id){ + ModestMsgViewWindowPrivate *priv = NULL; + ModestMsgViewWindow *window = NULL; + + g_assert(MODEST_IS_HEADER_VIEW_OBSERVER(observer)); + g_assert(MODEST_IS_MSG_VIEW_WINDOW(observer)); + + window = MODEST_MSG_VIEW_WINDOW(observer); + priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(window); + + /* If there is an other folder in the header-view then we do + * not care about it's model (msg list). Else if the + * header-view shows the folder the msg shown by us is in, we + * shall replace our model reference and make some check. */ + if(tny_folder_id == NULL || + !g_str_equal(tny_folder_id, priv->header_folder_id)) + return; + + /* Model is changed(replaced), so we should forget the old + * one. Because there might be other references and there + * might be some change on the model even if we unreferenced + * it, we need to disconnect our signals here. */ + if (g_signal_handler_is_connected(G_OBJECT (priv->header_model), + priv->row_changed_handler)) + g_signal_handler_disconnect(G_OBJECT (priv->header_model), + priv->row_changed_handler); + priv->row_changed_handler = 0; + if (g_signal_handler_is_connected(G_OBJECT (priv->header_model), + priv->row_deleted_handler)) + g_signal_handler_disconnect(G_OBJECT (priv->header_model), + priv->row_deleted_handler); + priv->row_deleted_handler = 0; + if (g_signal_handler_is_connected(G_OBJECT (priv->header_model), + priv->row_inserted_handler)) + g_signal_handler_disconnect(G_OBJECT (priv->header_model), + priv->row_inserted_handler); + priv->row_inserted_handler = 0; + if (g_signal_handler_is_connected(G_OBJECT (priv->header_model), + priv->rows_reordered_handler)) + g_signal_handler_disconnect(G_OBJECT (priv->header_model), + priv->rows_reordered_handler); + priv->rows_reordered_handler = 0; + g_object_unref(priv->header_model); + priv->header_model = NULL; + g_object_unref(priv->row_reference); + priv->row_reference = NULL; + g_object_unref(priv->next_row_reference); + priv->next_row_reference = NULL; + + modest_ui_actions_check_toolbar_dimming_rules(MODEST_WINDOW(window)); + + if(tny_folder_id == NULL) + return; + + g_assert(model != NULL); + + /* Also we must connect to the new model for row insertions. + * Only for insertions now. We will need other ones only after + * the msg is show by msg-view is added to the new model. */ + priv->row_inserted_handler = g_signal_connect ( + model, "row-inserted", + G_CALLBACK(modest_msg_view_window_on_row_inserted), + window); +} + gboolean modest_msg_view_window_toolbar_on_transfer_mode (ModestMsgViewWindow *self) { diff --git a/src/modest-ui-actions.c b/src/modest-ui-actions.c index db3886c..f7d2a47 100644 --- a/src/modest-ui-actions.c +++ b/src/modest-ui-actions.c @@ -855,11 +855,9 @@ open_msg_cb (ModestMailOperation *mail_op, g_list_foreach (sel_list, (GFunc) gtk_tree_path_free, NULL); g_list_free (sel_list); - win = modest_msg_view_window_new_with_header_model (msg, - account, - (const gchar*) uid, - model, - row_reference); + win = modest_msg_view_window_new_with_header_model ( + msg, account, (const gchar*) uid, + model, row_reference); gtk_tree_row_reference_free (row_reference); } else { win = modest_msg_view_window_new_for_attachment (msg, account, (const gchar*) uid); diff --git a/src/widgets/Makefile.am b/src/widgets/Makefile.am index a8fdc3b..b5a1af3 100644 --- a/src/widgets/Makefile.am +++ b/src/widgets/Makefile.am @@ -33,6 +33,8 @@ libmodest_widgets_la_SOURCES= \ modest-hbox-cell-renderer.h \ modest-vbox-cell-renderer.c \ modest-vbox-cell-renderer.h \ + modest-header-view-observer.c \ + modest-header-view-observer.h \ modest-header-view-render.c \ modest-header-view.c \ modest-header-view.h \ diff --git a/src/widgets/modest-header-view-observer.c b/src/widgets/modest-header-view-observer.c new file mode 100644 index 0000000..f32804d --- /dev/null +++ b/src/widgets/modest-header-view-observer.c @@ -0,0 +1,90 @@ +/* Copyright (c) 2006, Nokia Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the Nokia Corporation nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +/** + * modest_header_view_observer_update: + * @self: A #ModestHeaderViewObserver instance + * @change: Informs the observer what kind of change happened in the + * observed object. + **/ +void +modest_header_view_observer_update ( + ModestHeaderViewObserver *self, + GtkTreeModel *model, + const gchar *tny_folder_id) +{ + g_assert(MODEST_IS_HEADER_VIEW_OBSERVER(self)); + g_assert(MODEST_HEADER_VIEW_OBSERVER_GET_IFACE(self)->update_func + != NULL); + + MODEST_HEADER_VIEW_OBSERVER_GET_IFACE(self)->update_func(self, + model, tny_folder_id); +} + + +static void +modest_header_view_observer_base_init (gpointer g_class) +{ + static gboolean initialized = FALSE; + + if (!initialized) { + /* create interface signals here. */ + initialized = TRUE; + } +} + +GType +modest_header_view_observer_get_type (void) +{ + static GType type = 0; + + if (G_UNLIKELY(type == 0)) + { + static const GTypeInfo info = + { + sizeof (ModestHeaderViewObserverIface), + modest_header_view_observer_base_init, /* base_init */ + NULL, /* base_finalize */ + NULL, /* class_init */ + NULL, /* class_finalize */ + NULL, /* class_data */ + 0, + 0, /* n_preallocs */ + NULL, /* instance_init */ + NULL + }; + type = g_type_register_static (G_TYPE_INTERFACE, + "ModestHeaderViewObserver", &info, 0); + } + + return type; +} + diff --git a/src/widgets/modest-header-view-observer.h b/src/widgets/modest-header-view-observer.h new file mode 100644 index 0000000..c4063d8 --- /dev/null +++ b/src/widgets/modest-header-view-observer.h @@ -0,0 +1,78 @@ +#ifndef __MODEST_HEADER_VIEW_OBSERVER_H__ +#define __MODEST_HEADER_VIEW_OBSERVER_H__ + +/* Copyright (c) 2006, Nokia Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the Nokia Corporation nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include + +G_BEGIN_DECLS + +#define MODEST_TYPE_HEADER_VIEW_OBSERVER \ + (modest_header_view_observer_get_type ()) + +#define MODEST_HEADER_VIEW_OBSERVER(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), \ + MODEST_TYPE_HEADER_VIEW_OBSERVER, \ + ModestHeaderViewObserver)) +#define MODEST_IS_HEADER_VIEW_OBSERVER(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \ + MODEST_TYPE_HEADER_VIEW_OBSERVER)) + +#define MODEST_HEADER_VIEW_OBSERVER_GET_IFACE(inst) \ + (G_TYPE_INSTANCE_GET_INTERFACE((inst), \ + MODEST_TYPE_HEADER_VIEW_OBSERVER, \ + ModestHeaderViewObserverIface)) + +typedef struct _ModestHeaderViewObserver ModestHeaderViewObserver; +typedef struct _ModestHeaderViewObserverIface ModestHeaderViewObserverIface; + +struct _ModestHeaderViewObserverIface +{ + GTypeInterface parent; + + void (*update_func)( + ModestHeaderViewObserver *self, + GtkTreeModel *model, + const gchar *tny_folder_id); +}; + +GType modest_header_view_observer_get_type(); + +void modest_header_view_observer_update( + ModestHeaderViewObserver *self, + GtkTreeModel *model, + const gchar *tny_folder_id); + +G_END_DECLS + +#endif + diff --git a/src/widgets/modest-header-view.c b/src/widgets/modest-header-view.c index a0454e7..4206dc2 100644 --- a/src/widgets/modest-header-view.c +++ b/src/widgets/modest-header-view.c @@ -95,6 +95,10 @@ static void _clipboard_set_selected_data (ModestHeaderView *header_view static void _clear_hidding_filter (ModestHeaderView *header_view); +static void modest_header_view_notify_observers( + ModestHeaderView *header_view, + GtkTreeModel *model, + const gchar *tny_folder_id); typedef struct _ModestHeaderViewPrivate ModestHeaderViewPrivate; struct _ModestHeaderViewPrivate { @@ -104,6 +108,10 @@ struct _ModestHeaderViewPrivate { TnyFolderMonitor *monitor; GMutex *observers_lock; + /*header-view-observer observer*/ + GMutex *observer_list_lock; + GSList *observer_list; + /* not unref this object, its a singlenton */ ModestEmailClipboard *clipboard; @@ -512,6 +520,9 @@ modest_header_view_init (ModestHeaderView *obj) priv->empty = TRUE; + priv->observer_list_lock = g_mutex_new(); + priv->observer_list = NULL; + priv->clipboard = modest_runtime_get_email_clipboard (); priv->hidding_ids = NULL; priv->n_selected = 0; @@ -572,6 +583,11 @@ modest_header_view_finalize (GObject *obj) priv->acc_removed_handler); } + /* There is no need to lock because there should not be any + * reference to self now. */ + g_mutex_free(priv->observer_list_lock); + g_slist_free(priv->observer_list); + g_mutex_lock (priv->observers_lock); if (priv->monitor) { tny_folder_monitor_stop (priv->monitor); @@ -946,6 +962,8 @@ modest_header_view_set_folder_intern (ModestHeaderView *self, TnyFolder *folder) /* Set new model */ modest_header_view_set_model (GTK_TREE_VIEW (self), filter_model); + modest_header_view_notify_observers(self, GTK_TREE_MODEL(filter_model), + tny_folder_get_id(folder)); g_object_unref (G_OBJECT (filter_model)); /* modest_header_view_set_model (GTK_TREE_VIEW (self), sortable); */ /* g_object_unref (G_OBJECT (sortable)); */ @@ -1129,6 +1147,8 @@ modest_header_view_set_folder (ModestHeaderView *self, } modest_header_view_set_model (GTK_TREE_VIEW (self), NULL); + modest_header_view_notify_observers(self, NULL, NULL); + g_mutex_unlock (priv->observers_lock); } } @@ -1734,3 +1754,62 @@ on_account_removed (TnyAccountStore *self, g_object_unref (account); } } + +void modest_header_view_add_observer( + ModestHeaderView *header_view, + ModestHeaderViewObserver *observer) +{ + ModestHeaderViewPrivate *priv = NULL; + + g_assert(MODEST_IS_HEADER_VIEW(header_view)); + g_assert(observer != NULL); + g_assert(MODEST_IS_HEADER_VIEW_OBSERVER(observer)); + + priv = MODEST_HEADER_VIEW_GET_PRIVATE(header_view); + + g_mutex_lock(priv->observer_list_lock); + priv->observer_list = g_slist_prepend(priv->observer_list, observer); + g_mutex_unlock(priv->observer_list_lock); +} + +void modest_header_view_remove_observer( + ModestHeaderView *header_view, + ModestHeaderViewObserver *observer) +{ + ModestHeaderViewPrivate *priv = NULL; + + g_assert(MODEST_IS_HEADER_VIEW(header_view)); + g_assert(observer != NULL); + g_assert(MODEST_IS_HEADER_VIEW_OBSERVER(observer)); + + priv = MODEST_HEADER_VIEW_GET_PRIVATE(header_view); + + g_mutex_lock(priv->observer_list_lock); + priv->observer_list = g_slist_remove(priv->observer_list, observer); + g_mutex_unlock(priv->observer_list_lock); +} + +static void modest_header_view_notify_observers( + ModestHeaderView *header_view, + GtkTreeModel *model, + const gchar *tny_folder_id) +{ + ModestHeaderViewPrivate *priv = NULL; + GSList *iter; + ModestHeaderViewObserver *observer; + + g_assert(MODEST_IS_HEADER_VIEW(header_view)); + + priv = MODEST_HEADER_VIEW_GET_PRIVATE(header_view); + + g_mutex_lock(priv->observer_list_lock); + iter = priv->observer_list; + while(iter != NULL){ + observer = MODEST_HEADER_VIEW_OBSERVER(iter->data); + modest_header_view_observer_update(observer, model, + tny_folder_id); + iter = g_slist_next(iter); + } + g_mutex_unlock(priv->observer_list_lock); +} + diff --git a/src/widgets/modest-header-view.h b/src/widgets/modest-header-view.h index fdf6d2f..a1b746b 100644 --- a/src/widgets/modest-header-view.h +++ b/src/widgets/modest-header-view.h @@ -37,6 +37,7 @@ #include #include #include "modest-mail-operation.h" +#include "modest-header-view-observer.h" G_BEGIN_DECLS @@ -385,6 +386,34 @@ modest_header_view_paste_selection (ModestHeaderView *header_view, void modest_header_view_refilter (ModestHeaderView *header_view); +/** + * modest_header_view_add_observer: + * @header_view: a #ModestHeaderView + * @observer: The observer to notify. + * + * Registers a new observer. Warning! Each added observer object must + * removed using @modest_header_view_remove_observer before destroying + * the observer, or at least when it is under destruction. Also you + * should care about that the observer's #update function might be + * called any time until the observer is removed. + **/ +void modest_header_view_add_observer( + ModestHeaderView *header_view, + ModestHeaderViewObserver *observer); + +/** + * modest_header_view_remove_observer: + * @header_view: a #ModestHeaderView + * @observer: The observer to remove. + * + * Removes exactly one observer from the notification list. If you + * added an observer twice, you should call this remove funtion twice + * as well. + **/ +void modest_header_view_remove_observer( + ModestHeaderView *header_view, + ModestHeaderViewObserver *observer); + G_END_DECLS