From b5c6bcea25b1b840fd6c8e89a4a3c9fbd83ba84b Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Wed, 5 Aug 2015 21:25:55 +0000
Subject: Run updates on collected items activity changes.

git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@567 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a
---
 ChangeLog                    | 15 +++++++++++++++
 src/analysis/db/collection.c | 28 +++++++++++++++++----------
 src/analysis/db/collection.h |  2 +-
 src/analysis/db/item-int.h   |  4 ----
 src/analysis/db/item.c       | 46 +++++++++++++++++++++++++++++++++-----------
 src/analysis/db/item.h       |  2 +-
 src/gui/panels/bookmarks.c   |  1 +
 src/gui/panels/history.c     | 19 ++++++++++++++++--
 8 files changed, 88 insertions(+), 29 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 25a9176..40d1932 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,20 @@
 15-08-05  Cyrille Bagard <nocbos@gmail.com>
 
+	* src/analysis/db/collection.c:
+	* src/analysis/db/collection.h:
+	* src/analysis/db/item.c:
+	* src/analysis/db/item.h:
+	* src/analysis/db/item-int.h:
+	Run updates on collected items activity changes.
+
+	* src/gui/panels/bookmarks.c:
+	Do not load inactive bookmarks anymore.
+
+	* src/gui/panels/history.c:
+	Update buttons access on undo/redo actions.
+
+15-08-05  Cyrille Bagard <nocbos@gmail.com>
+
 	* src/gui/panels/history.c:
 	Properly handle the history selection and update the history on changes.
 
diff --git a/src/analysis/db/collection.c b/src/analysis/db/collection.c
index 1afe96b..117a2d5 100644
--- a/src/analysis/db/collection.c
+++ b/src/analysis/db/collection.c
@@ -306,7 +306,7 @@ bool g_db_collection_recv(GDbCollection *collec, int fd, sqlite3 *db)
 
             if (result)
             {
-                if (collec->binary != NULL)
+                if (collec->binary != NULL && g_db_item_is_active(item))
                     result = g_db_item_apply(item, collec->binary);
                 if (db != NULL)
                     result &= g_db_collection_store_item(collec, item, db);
@@ -337,7 +337,7 @@ bool g_db_collection_recv(GDbCollection *collec, int fd, sqlite3 *db)
                     if (g_db_item_is_active(G_DB_ITEM(found->data)))
                     {
                         inactive = _g_db_collection_compute_inactivity_timestamp(collec, false);
-                        result = _g_db_collection_update_item_activity(collec, item, inactive, false);
+                        result = _g_db_collection_update_item_activity(collec, item, &inactive, false);
                     }
                 }
 
@@ -350,7 +350,15 @@ bool g_db_collection_recv(GDbCollection *collec, int fd, sqlite3 *db)
         case DBA_CHANGE_STATE:
 
             if (db == NULL)
-                result = g_db_collection_update_item_activity(collec, item, g_db_item_get_timestamp(item) + 1);
+            {
+                if (g_db_item_is_active(item))
+                    result = g_db_collection_update_item_activity(collec, item, NULL);
+                else
+                {
+                    inactive = g_db_item_get_timestamp(item) + 1;
+                    result = g_db_collection_update_item_activity(collec, item, &inactive);
+                }
+            }
             else
                 result = false;
 
@@ -716,7 +724,7 @@ timestamp_t _g_db_collection_compute_inactivity_timestamp(GDbCollection *collec,
 *                                                                             *
 ******************************************************************************/
 
-bool _g_db_collection_update_item_activity(GDbCollection *collec, GDbItem *item, timestamp_t timestamp, bool lock)
+bool _g_db_collection_update_item_activity(GDbCollection *collec, GDbItem *item, timestamp_t *timestamp, bool lock)
 {
     bool result;                            /* Bilan à faire remonter      */
     GList *found;                           /* Test de présence existante  */
@@ -733,7 +741,7 @@ bool _g_db_collection_update_item_activity(GDbCollection *collec, GDbItem *item,
     {
         internal = G_DB_ITEM(found->data);
 
-        g_db_item_set_activity(internal, (timestamp_t []) { timestamp });
+        result = g_db_item_set_activity(internal, collec->binary, timestamp);
 
         g_signal_emit_by_name(collec, "content-changed", DBA_CHANGE_STATE, internal);
 
@@ -780,7 +788,7 @@ GList *g_db_collection_set_last_active(GDbCollection *collec, timestamp_t timest
         {
             if (!g_db_item_is_active(item))
             {
-                g_db_item_set_activity(item, NULL);
+                /* ... */g_db_item_set_activity(item, collec->binary, NULL);
                 /* ... */g_db_collection_store_updated_item(collec, item, db);
                 g_signal_emit_by_name(collec, "content-changed", DBA_CHANGE_STATE, item);
             }
@@ -823,16 +831,16 @@ GList *g_db_collection_set_last_active(GDbCollection *collec, timestamp_t timest
 
 bool g_db_collection_set_inactive(GDbCollection *collec, GDbItem *item, timestamp_t *timestamp)
 {
+    bool result;                            /* Bilan à retourner           */
+
     /* Si cette collection n'est pas concernée, on ne bouge pas ! */
     if (G_OBJECT_TYPE(G_OBJECT(item)) != collec->type) return false;
 
-    assert(g_db_item_is_active(item));
-
-    g_db_item_set_activity(item, timestamp);
+    result = g_db_item_set_activity(item, collec->binary, timestamp);
 
     g_signal_emit_by_name(collec, "content-changed", DBA_CHANGE_STATE, item);
 
-    return true;
+    return result;
 
 }
 
diff --git a/src/analysis/db/collection.h b/src/analysis/db/collection.h
index e9f93d2..0f7e1f0 100644
--- a/src/analysis/db/collection.h
+++ b/src/analysis/db/collection.h
@@ -109,7 +109,7 @@ bool _g_db_collection_remove_item(GDbCollection *, GDbItem *, bool);
 timestamp_t _g_db_collection_compute_inactivity_timestamp(GDbCollection *, bool);
 
 /* Met à jour le statut d'activité d'un élément de collection. */
-bool _g_db_collection_update_item_activity(GDbCollection *, GDbItem *, timestamp_t, bool);
+bool _g_db_collection_update_item_activity(GDbCollection *, GDbItem *, timestamp_t *, bool);
 
 #define g_db_collection_add_item(c, i) _g_db_collection_add_item(c, i, true)
 #define g_db_collection_remove_item(c, i) _g_db_collection_remove_item(c, i, true)
diff --git a/src/analysis/db/item-int.h b/src/analysis/db/item-int.h
index fb33c72..c2c8b31 100644
--- a/src/analysis/db/item-int.h
+++ b/src/analysis/db/item-int.h
@@ -73,10 +73,6 @@ struct _GDbItem
 
     bool is_volatile;                       /* Pas besoin de sauvegarde ?  */
 
-#ifdef DEBUG
-    bool applied;                           /* L'élément a été appliqué    */
-#endif
-
 };
 
 /* Base d'un élément pour collection générique (classe) */
diff --git a/src/analysis/db/item.c b/src/analysis/db/item.c
index 3fc6f6f..013703d 100644
--- a/src/analysis/db/item.c
+++ b/src/analysis/db/item.c
@@ -403,11 +403,9 @@ bool g_db_item_apply(GDbItem *item, GLoadedBinary *binary)
 {
     bool result;                            /* Bilan à faire remonter      */
 
-    result = G_DB_ITEM_GET_CLASS(item)->apply(item, binary);
+    assert(g_db_item_is_active(item));
 
-#ifdef DEBUG
-    item->applied = true;
-#endif
+    result = G_DB_ITEM_GET_CLASS(item)->apply(item, binary);
 
     return result;
 
@@ -431,9 +429,7 @@ bool g_db_item_cancel(GDbItem *item, GLoadedBinary *binary)
 {
     bool result;                            /* Bilan à faire remonter      */
 
-#ifdef DEBUG
-    assert(item->applied);
-#endif
+    assert(!g_db_item_is_active(item));
 
     result = G_DB_ITEM_GET_CLASS(item)->cancel(item, binary);
 
@@ -482,23 +478,51 @@ timestamp_t g_db_item_get_timestamp(const GDbItem *item)
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : item  = élément de collection à mettre à jour.               *
-*                first = horodatage du premier élément désactivé ou NULL.     *
+*  Paramètres  : item   = élément de collection à mettre à jour.              *
+*                binary = binaire chargé en mémoire à modifier.               *
+*                first  = horodatage du premier élément désactivé ou NULL.    *
 *                                                                             *
 *  Description : Active ou désactive un élément de collection en place.       *
 *                                                                             *
-*  Retour      : -                                                            *
+*  Retour      : Bilan de la mise à jour de l'élément.                        *
 *                                                                             *
 *  Remarques   : -                                                            *
 *                                                                             *
 ******************************************************************************/
 
-void g_db_item_set_activity(GDbItem *item, timestamp_t *first)
+bool g_db_item_set_activity(GDbItem *item, GLoadedBinary *binary, timestamp_t *first)
 {
+    bool result;                            /* Bilan à faire remonter      */
+    bool active;                            /* Etat avant changement       */
+
+    active = g_db_item_is_active(item);
+
+    /* Archivage de l'état */
+
     if (first == NULL)
+    {
+        assert(!active);
         item->timestamp = item->created;
+    }
     else
+    {
+        assert(active);
         item->timestamp = --(*first);
+    }
+
+    /* Application de l'état */
+
+    if (binary != NULL)
+    {
+        if (active)
+            result = g_db_item_cancel(item, binary);
+        else
+            result = g_db_item_apply(item, binary);
+    }
+    else
+        result = true;
+
+    return result;
 
 }
 
diff --git a/src/analysis/db/item.h b/src/analysis/db/item.h
index 7d3392c..2edf594 100644
--- a/src/analysis/db/item.h
+++ b/src/analysis/db/item.h
@@ -87,7 +87,7 @@ const char *g_db_item_get_label(const GDbItem *);
 timestamp_t g_db_item_get_timestamp(const GDbItem *);
 
 /*  Active ou désactive un élément de collection en place. */
-void g_db_item_set_activity(GDbItem *, timestamp_t *);
+bool g_db_item_set_activity(GDbItem *, GLoadedBinary *, timestamp_t *);
 
 /* Indique si l'élément est activé ou désactivé. */
 bool g_db_item_is_active(const GDbItem *);
diff --git a/src/gui/panels/bookmarks.c b/src/gui/panels/bookmarks.c
index 5b0649c..977cbc9 100644
--- a/src/gui/panels/bookmarks.c
+++ b/src/gui/panels/bookmarks.c
@@ -530,6 +530,7 @@ static void reload_bookmarks_into_treeview(GBookmarksPanel *panel, GLoadedBinary
     for (b = g_list_first(items); b != NULL; b = g_list_next(b))
     {
         bookmark = G_DB_BOOKMARK(b->data);
+        if (!g_db_item_is_active(G_DB_ITEM(bookmark))) continue;
 
         addr = g_db_bookmark_get_address(bookmark);
 
diff --git a/src/gui/panels/history.c b/src/gui/panels/history.c
index 7d0cdd5..74945c7 100644
--- a/src/gui/panels/history.c
+++ b/src/gui/panels/history.c
@@ -450,11 +450,13 @@ static void change_history_panel_current_binary(GHistoryPanel *panel, GLoadedBin
 static void on_history_changed(GDbCollection *collec, DBAction action, GDbItem *item, GHistoryPanel *panel)
 {
     GtkTreeModel *model;                    /* Modèle de gestion courant   */
-    GtkTreeIter iter;                       /* Boucle de parcours          */
     GtkTreeSelection *selection;            /* Nouvelle sélection à établir*/
+    GtkTreeIter iter;                       /* Boucle de parcours          */
 
     model = GTK_TREE_MODEL(panel->store);
 
+    selection = gtk_tree_view_get_selection(panel->treeview);
+
     /* Mise à jour de la liste affichée */
 
     bool find_changed_item(GtkTreeModel *_model, GDbItem *target, GtkTreeIter *_found)
@@ -544,11 +546,14 @@ static void on_history_changed(GDbCollection *collec, DBAction action, GDbItem *
 
         gtk_tree_model_foreach(model, (GtkTreeModelForeachFunc)find_last_active, &iter);
 
-        selection = gtk_tree_view_get_selection(panel->treeview);
         gtk_tree_selection_select_iter(selection, &iter);
 
     }
 
+    /* Actualisation des accès */
+
+    on_history_selection_change(selection, panel);
+
 }
 
 
@@ -620,6 +625,16 @@ static void on_history_selection_change(GtkTreeSelection *selection, GHistoryPan
 
     }
 
+    else
+    {
+        button = GTK_WIDGET(g_object_get_data(G_OBJECT(panel), "undo"));
+        gtk_widget_set_sensitive(button, FALSE);
+
+        button = GTK_WIDGET(g_object_get_data(G_OBJECT(panel), "redo"));
+        gtk_widget_set_sensitive(button, FALSE);
+
+    }
+
 }
 
 
-- 
cgit v0.11.2-87-g4458