From 181e3a9a8819ba50c74f4864c0fca111e375aa5e Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Tue, 26 Dec 2017 12:08:23 +0100
Subject: Registered the current project as a real global variable.

---
 ChangeLog                | 17 +++++++++++
 src/analysis/project.c   | 38 +------------------------
 src/analysis/project.h   |  9 ++----
 src/core/global.c        | 74 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/core/global.h        | 13 +++++++++
 src/gui/editor.c         | 57 ++++++++++++++++++++++++++++++++++++-
 src/gui/menus/file.c     |  1 +
 src/gui/menus/project.c  |  1 +
 src/gui/panels/welcome.c |  2 +-
 src/main.c               |  1 -
 10 files changed, 167 insertions(+), 46 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index ba0b7e2..e79d296 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+17-12-26  Cyrille Bagard <nocbos@gmail.com>
+
+	* src/analysis/project.c:
+	* src/analysis/project.h:
+	Clean the code for projects.
+
+	* src/core/global.c:
+	* src/core/global.h:
+	Register the current project as a real global variable.
+
+	* src/gui/editor.c:
+	* src/gui/menus/file.c:
+	* src/gui/menus/project.c:
+	* src/gui/panels/welcome.c:
+	* src/main.c:
+	Update code.
+
 17-12-15  Cyrille Bagard <nocbos@gmail.com>
 
 	* plugins/elf/python/dynamic.c:
diff --git a/src/analysis/project.c b/src/analysis/project.c
index 5fe7ab3..66f6b0f 100644
--- a/src/analysis/project.c
+++ b/src/analysis/project.c
@@ -103,9 +103,6 @@ static void g_study_project_class_init(GStudyProjectClass *);
 /*Initialise une instance de projet d'étude. */
 static void g_study_project_init(GStudyProject *);
 
-/* Supprime de l'écran un projet en place. */
-static void g_study_project_hide(const GStudyProject *);
-
 /* Assure un positionnement initial idéal. */
 static gboolean scroll_for_the_first_time(GtkWidget *, GdkEvent *, GLoadedBinary *);
 
@@ -722,7 +719,7 @@ void g_study_project_display(const GStudyProject *project)
 *                                                                             *
 ******************************************************************************/
 
-static void g_study_project_hide(const GStudyProject *project)
+void g_study_project_hide(const GStudyProject *project)
 {
     size_t i;                               /* Boucle de parcours #1       */
     loaded_binary *handled;                 /* Binaire prise en compte     */
@@ -1039,39 +1036,6 @@ GtkDisplayPanel *get_alt_view_for_view_panel(GtkDisplayPanel *panel, BinaryView
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : project = éventuel adresse à renvoyer désormais.             *
-*                                                                             *
-*  Description : Fournit l'adresse du projet courant.                         *
-*                                                                             *
-*  Retour      : Adresse du projet ouvert ou NULL si aucun (!).               *
-*                                                                             *
-*  Remarques   : -                                                            *
-*                                                                             *
-******************************************************************************/
-
-GStudyProject *_get_current_study_project(GStudyProject *project)
-{
-    static GStudyProject *result = NULL;    /* Adresse à retourner         */
-
-    if (project != NULL)
-    {
-        if (result != NULL)
-        {
-            g_study_project_hide(result);
-            g_object_unref(G_OBJECT(result));
-        }
-
-        result = project;
-
-    }
-
-    return result;
-
-}
-
-
-/******************************************************************************
-*                                                                             *
 *  Paramètres  : -                                                            *
 *                                                                             *
 *  Description : Fournit le gestionnaire des projets connus.                  *
diff --git a/src/analysis/project.h b/src/analysis/project.h
index 34dd976..b331b1f 100644
--- a/src/analysis/project.h
+++ b/src/analysis/project.h
@@ -111,6 +111,9 @@ void g_study_project_detach_binary(GStudyProject *, GLoadedBinary *);
 /* Met en place un projet à l'écran. */
 void g_study_project_display(const GStudyProject *);
 
+/* Supprime de l'écran un projet en place. */
+void g_study_project_hide(const GStudyProject *);
+
 /* Fournit l'ensemble des binaires associés à un projet. */
 GLoadedBinary **g_study_project_get_binaries(const GStudyProject *, size_t *);
 
@@ -136,12 +139,6 @@ GtkDisplayPanel *get_alt_view_for_view_panel(GtkDisplayPanel *, BinaryView);
 /* ------------------------- GESTION GLOBALISEE DES PROJETS ------------------------- */
 
 
-/* Fournit l'adresse du projet courant. */
-GStudyProject *_get_current_study_project(GStudyProject *);
-
-#define set_current_project(prj) _get_current_study_project(prj)
-#define get_current_project() _get_current_study_project(NULL)
-
 /* Fournit le gestionnaire des projets connus. */
 GtkRecentManager *get_project_manager(void);
 
diff --git a/src/core/global.c b/src/core/global.c
index 86c7e5f..c5f25c1 100644
--- a/src/core/global.c
+++ b/src/core/global.c
@@ -31,6 +31,12 @@
 /* Gestionnaire de tâches parallèles */
 static GWorkQueue *_queue = NULL;
 
+/* Projet global actif */
+static GStudyProject *_project = NULL;
+
+/* Avertisseur de changement de projet principal */
+static current_project_change_cb _project_notify = NULL;
+
 
 
 /******************************************************************************
@@ -71,3 +77,71 @@ GWorkQueue *get_work_queue(void)
     return _queue;
 
 }
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : project = éventuelle adresse du nouveau projet principal.    *
+*                                                                             *
+*  Description : Définit l'adresse du projet courant.                         *
+*                                                                             *
+*  Retour      : -                                                            *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+void set_current_project(GStudyProject *project)
+{
+    if (_project != NULL)
+    {
+        _project_notify(_project, false);
+        g_object_unref(G_OBJECT(_project));
+    }
+
+    _project = project;
+
+    if (_project != NULL)
+        _project_notify(_project, true);
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : project = éventuel adresse à renvoyer désormais.             *
+*                                                                             *
+*  Description : Fournit l'adresse du projet courant.                         *
+*                                                                             *
+*  Retour      : Adresse du projet ouvert ou NULL si aucun (!).               *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+GStudyProject *get_current_project(void)
+{
+    return _project;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : notify = procédure à appeler à chaque changement de project. *
+*                                                                             *
+*  Description : Enregistre une partie de code à avertir en cas de changement.*
+*                                                                             *
+*  Retour      : -                                                            *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+void register_project_change_notification(current_project_change_cb notify)
+{
+    assert(_project_notify == NULL);
+
+    _project_notify = notify;
+
+}
diff --git a/src/core/global.h b/src/core/global.h
index d29cb76..9e2b8cd 100644
--- a/src/core/global.h
+++ b/src/core/global.h
@@ -25,6 +25,7 @@
 #define _CORE_GLOBAL_H
 
 
+#include "../analysis/project.h"
 #include "../glibext/delayed.h"
 
 
@@ -35,6 +36,18 @@ void set_work_queue(GWorkQueue *);
 /* Fournit le gestionnaire de traitements parallèles courant. */
 GWorkQueue *get_work_queue(void);
 
+/* Définit l'adresse du projet courant. */
+void set_current_project(GStudyProject *);
+
+/* Fournit l'adresse du projet courant. */
+GStudyProject *get_current_project(void);
+
+/* Réagit à un changement du projet principal. */
+typedef void (* current_project_change_cb) (GStudyProject *, bool);
+
+/* Enregistre une partie de code à avertir en cas de changement. */
+void register_project_change_notification(current_project_change_cb);
+
 
 
 #endif  /* _CORE_GLOBAL_H */
diff --git a/src/gui/editor.c b/src/gui/editor.c
index 2488a27..6785753 100644
--- a/src/gui/editor.c
+++ b/src/gui/editor.c
@@ -43,8 +43,8 @@
 #include "panels/panel.h"
 #include "panels/welcome.h"
 #include "tb/portions.h"
-#include "../analysis/project.h"
 #include "../common/extstr.h"
+#include "../core/global.h"
 #include "../core/params.h"
 #include "../gtkext/easygtk.h"
 #include "../gtkext/gtkdockable.h"
@@ -208,6 +208,15 @@ static void notify_paned_handle_position_change(GObject *, GParamSpec *, gpointe
 
 
 
+/* ------------------------- INTEGRATION ET SUIVI DE PROJET ------------------------- */
+
+
+/* Réagit à un changement du projet principal. */
+static void notify_editor_project_change(GStudyProject *, bool);
+
+
+
+
 
 
 
@@ -372,6 +381,8 @@ GtkWidget *create_editor(void)
 
     /* Actualisation des contenus */
 
+    register_project_change_notification(notify_editor_project_change);
+
     change_editor_items_current_binary(NULL);
 
 
@@ -1574,3 +1585,47 @@ static void notify_paned_handle_position_change(GObject *obj, GParamSpec *pspec,
     free(key);
 
 }
+
+
+
+/* ---------------------------------------------------------------------------------- */
+/*                           INTEGRATION ET SUIVI DE PROJET                           */
+/* ---------------------------------------------------------------------------------- */
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : project = projet concerné par la procédure.                  *
+*                new     = indique si le projet est le nouvel actif ou non.   *
+*                                                                             *
+*  Description : Réagit à un changement du projet principal.                  *
+*                                                                             *
+*  Retour      : -                                                            *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static void notify_editor_project_change(GStudyProject *project, bool new)
+{
+
+    if (new)
+    {
+
+        /* ... */
+
+
+    }
+
+    else
+    {
+
+        g_study_project_hide(project);
+
+
+
+    }
+
+
+
+}
diff --git a/src/gui/menus/file.c b/src/gui/menus/file.c
index 6e64357..c0c031c 100644
--- a/src/gui/menus/file.c
+++ b/src/gui/menus/file.c
@@ -30,6 +30,7 @@
 
 #include "../core/global.h"
 #include "../../analysis/project.h"
+#include "../../core/global.h"
 #include "../../gtkext/easygtk.h"
 
 
diff --git a/src/gui/menus/project.c b/src/gui/menus/project.c
index 8c60f72..2930a95 100644
--- a/src/gui/menus/project.c
+++ b/src/gui/menus/project.c
@@ -35,6 +35,7 @@
 #include "../dialogs/shellcode.h"
 #include "../../analysis/loading.h"
 #include "../../analysis/contents/file.h"
+#include "../../core/global.h"
 #include "../../gtkext/easygtk.h"
 
 
diff --git a/src/gui/panels/welcome.c b/src/gui/panels/welcome.c
index f69b81d..0edeb41 100644
--- a/src/gui/panels/welcome.c
+++ b/src/gui/panels/welcome.c
@@ -38,11 +38,11 @@
 
 #include "panel-int.h"
 #include "../core/global.h"
-#include "../../analysis/project.h"
 #include "../../common/cpp.h"
 #include "../../common/io.h"
 #include "../../common/net.h"
 #include "../../common/shuffle.h"
+#include "../../core/global.h"
 #include "../../core/params.h"
 #include "../../gtkext/support.h"
 
diff --git a/src/main.c b/src/main.c
index e05ccb6..1834352 100644
--- a/src/main.c
+++ b/src/main.c
@@ -31,7 +31,6 @@
 #include <i18n.h>
 
 #include "analysis/loading.h"
-#include "analysis/project.h"
 #include "analysis/contents/file.h"
 #include "analysis/db/server.h"
 #include "common/xdg.h"
-- 
cgit v0.11.2-87-g4458