[pan: 10/15] Clean up the way the edit score menu works a bit




commit 85f14460a5b36cc86fa355171f5b477e7936f530
Author: Thomas Tanner <thosrtanner googlemail com>
Date:   Fri Jun 3 22:15:52 2022 +0100

    Clean up the way the edit score menu works a bit

 pan/general/editor-spawner.cc | 11 +++++------
 pan/general/editor-spawner.h  |  4 +---
 pan/gui/gui.cc                | 42 +++++++++++++++++++++---------------------
 pan/gui/gui.h                 |  7 ++++++-
 4 files changed, 33 insertions(+), 31 deletions(-)
---
diff --git a/pan/general/editor-spawner.cc b/pan/general/editor-spawner.cc
index 166ac7b..aa48739 100644
--- a/pan/general/editor-spawner.cc
+++ b/pan/general/editor-spawner.cc
@@ -24,7 +24,6 @@
 #include <pan/gui/url.h> //For get_default_editors
 
 #include <glib/gi18n.h> // for _
-#include <gtk/gtk.h>
 
 #include <set>
 
@@ -32,13 +31,13 @@ namespace pan {
 
 namespace
 {
-       typedef struct
+       struct se_data
        {
                char *fname;
                EditorSpawner::Callback callback;
-       } se_data;
+       };
 
-       void child_watch_cb(GPid pid, gint status, gpointer data)
+       void child_watch_callback(GPid pid, int status, void * data)
        {
                se_data * d{static_cast<se_data *>(data)};
                g_spawn_close_pid(pid);
@@ -61,7 +60,7 @@ EditorSpawner::EditorSpawner(
   int argc{0};
   char ** argv{nullptr};
   GError * err{nullptr};
-  g_shell_parse_argv (editor.c_str(), &argc, &argv, &err);
+  g_shell_parse_argv(editor.c_str(), &argc, &argv, &err);
   if (err != nullptr) {
     Log::add_err_va(
       _("Error parsing \"external editor\" command line: %s (Command was: %s)"),
@@ -117,7 +116,7 @@ EditorSpawner::EditorSpawner(
   data->fname = fname;
   data->callback = callback;
 
-  _child_id = g_child_watch_add(pid, child_watch_cb, static_cast<gpointer>(data));
+  _child_id = g_child_watch_add(pid, child_watch_callback, data);
 }
 
 EditorSpawner::~EditorSpawner()
diff --git a/pan/general/editor-spawner.h b/pan/general/editor-spawner.h
index efa5b6c..2a80c58 100644
--- a/pan/general/editor-spawner.h
+++ b/pan/general/editor-spawner.h
@@ -23,8 +23,6 @@
 #include <functional>
 #include <stdexcept>
 
-#include <glib.h>
-
 namespace pan {
 
   class Prefs;
@@ -50,7 +48,7 @@ namespace pan {
       ~EditorSpawner();
 
     private:
-      guint _child_id;
+      unsigned int _child_id;
   };
 
   class EditorSpawnerError : public std::runtime_error
diff --git a/pan/gui/gui.cc b/pan/gui/gui.cc
index 03a2bd9..85c77ee 100644
--- a/pan/gui/gui.cc
+++ b/pan/gui/gui.cc
@@ -2372,25 +2372,15 @@ GUI :: on_prefs_string_changed (const StringView& key, const StringView& value)
   }
 }
 
-namespace {
-
-EditorSpawner *spawner;
-GtkAction *spawner_action;
-
-}
-
 void
-GUI :: do_edit_scores (GtkAction *a)
+GUI :: do_edit_scores (GtkAction *act)
 {
   //Protect against bouncy keypresses
-  if (not gtk_action_get_sensitive(a)) {
+  if (not gtk_action_get_sensitive(act)) {
     return;
   }
 
-  gtk_action_set_sensitive(a, false);
-  spawner_action = a;
-
-  //This is wrong because this might not be the filename
+  //FIXME This is wrong because this might not be the filename
   char *filename = g_build_filename(file::get_pan_home().c_str(), "Score", NULL);
   if (not file::file_exists(filename)) {
     FILE *f = fopen(filename, "a+");
@@ -2402,19 +2392,29 @@ GUI :: do_edit_scores (GtkAction *a)
     fclose(f);
   }
 
-  using namespace std::placeholders;
-  spawner = new EditorSpawner(filename,
-                                std::bind(&GUI::edit_scores_cleanup, this, _1, _2),
-                              _prefs);
+  try
+  {
+    using namespace std::placeholders;
+    _spawner.reset(
+      new EditorSpawner(filename,
+                        std::bind(&GUI::edit_scores_cleanup, this, _1, _2, act),
+                        _prefs));
+    gtk_action_set_sensitive(act, false);
+  }
+  catch (EditorSpawnerError const &)
+  {
+    //There should be a big red exclamation on the status line
+    g_free(filename);
+  }
 }
 
 void
-GUI :: edit_scores_cleanup(int status, char *filename)
+GUI :: edit_scores_cleanup(int status, char *filename, GtkAction *act)
 {
-  //rescore articles
+  //FIXME rescore articles
   g_free(filename);
-  gtk_action_set_sensitive(spawner_action, true);
-  delete spawner;
+  gtk_action_set_sensitive(act, true);
+  _spawner.reset();
   gtk_window_present(get_window(_root));
 }
 
diff --git a/pan/gui/gui.h b/pan/gui/gui.h
index 1388568..e4a7aec 100644
--- a/pan/gui/gui.h
+++ b/pan/gui/gui.h
@@ -33,12 +33,14 @@
 #include <pan/gui/group-prefs.h>
 #include <pan/gui/wait.h>
 
+#include <memory>
 #include <stdint.h>
 
 #include "gtk-compat.h"
 
 namespace pan
 {
+  class EditorSpawner;
   class GroupPane;
   class HeaderPane;
   class BodyPane;
@@ -306,7 +308,10 @@ namespace pan
       static void prefs_dialog_destroyed_cb (GtkWidget * w, gpointer self);
       void prefs_dialog_destroyed (GtkWidget* w);
       int score_int_from_string(std::string val, const char* rules[]);
-      void edit_scores_cleanup(int, char *);
+
+      void edit_scores_cleanup(int, char *, GtkAction *);
+      std::unique_ptr<EditorSpawner> _spawner;
+
 #ifdef HAVE_GNUTLS
       static gboolean show_cert_failed_cb(gpointer gp);
 #endif


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]