[pan2: 2/68] FIXED: Bug 465763 Spawn editor asynchronously.



commit 2aa0cebc441aea173c951eafd0e1d62ad3729d25
Author: K. Haley <haleykd users sf net>
Date:   Sat Jun 20 22:20:33 2009 -0600

    FIXED: Bug 465763 Spawn editor asynchronously.
    
    Prevents the external editor from freezing pan.
    Documentation used when I origianlly write this patch indicated
    that G_SPAWN_DO_NOT_REAP_CHILD was only needed on windows.
    Behavioral difference between g_spawn_sync and g_spawn_async and
    current docs indicate otherwise.

 pan/general/quark.h |    1 +
 pan/gui/post-ui.cc  |   38 +++++++++++++++++++++++++++++++++++---
 pan/gui/post-ui.h   |    1 +
 3 files changed, 37 insertions(+), 3 deletions(-)
---
diff --git a/pan/general/quark.h b/pan/general/quark.h
index fafc967..938611a 100644
--- a/pan/general/quark.h
+++ b/pan/general/quark.h
@@ -78,6 +78,7 @@ namespace pan
         StringView to_view () const { return StringView(str,len); }
         //wtf? bool operator() (const Impl& a, const Impl& b) const { return StringView(str,len) == StringView(b.str,b.len); }
         bool operator== (const Impl& b) const { return StringView(str,len) == StringView(b.str,b.len); }
+        bool operator< (const Impl& b) const { return StringView(str,len) < StringView(b.str,b.len); }
       };
 
       struct StringViewHash
diff --git a/pan/gui/post-ui.cc b/pan/gui/post-ui.cc
index f8f7ce6..bb7615a 100644
--- a/pan/gui/post-ui.cc
+++ b/pan/gui/post-ui.cc
@@ -683,6 +683,24 @@ PostUI :: maybe_post_message (GMimeMessage * message)
 ****
 ***/
 
+namespace
+{
+	typedef struct
+	{
+		GPid pid;
+		char *fname;
+		PostUI *pui;
+	} se_data;
+	
+	void child_watch_cb(GPid pid, gint status, gpointer data)
+	{
+		se_data *d=static_cast<se_data*>(data);
+		static_cast<PostUI*>(d->pui)->spawn_editor_dead(static_cast<char*>(d->fname));
+		g_spawn_close_pid(pid);
+		delete d;
+	}
+}
+
 void
 PostUI :: spawn_editor ()
 {
@@ -765,17 +783,32 @@ PostUI :: spawn_editor ()
   // spawn off the external editor
   if (ok) {
     GError * err (0);
-    g_spawn_sync (0, argv, 0, G_SPAWN_SEARCH_PATH, 0, 0, 0, 0, 0, &err);
+    se_data *data=new se_data;
+    data->fname=fname;
+    data->pui=this;
+    g_spawn_async (0, argv, 0, (GSpawnFlags)(G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD), 0, 0, &data->pid, &err);
     if (err != NULL) {
       Log::add_err_va (_("Error starting external editor: %s"), err->message);
       g_clear_error (&err);
       ok = false;
+      delete data;
+    } else {
+      g_child_watch_add(data->pid,child_watch_cb,static_cast<gpointer>(data));
     }
+  } else {
+	  g_free(fname);
   }
 
+  g_strfreev (argv);
+}
+
+void PostUI::spawn_editor_dead(char *fname)
+{
+	GtkTextBuffer * buf (_body_buf);
+	
   // read the file contents back in
   std::string txt;
-  if (ok && file :: get_text_file_contents (fname, txt)) {
+  if (file :: get_text_file_contents (fname, txt)) {
     GtkTextIter start, end;
     gtk_text_buffer_get_bounds (buf, &start, &end);
     gtk_text_buffer_delete (buf, &start, &end);
@@ -785,7 +818,6 @@ PostUI :: spawn_editor ()
   // cleanup
   ::remove (fname);
   g_free (fname);
-  g_strfreev (argv);
 
   gtk_window_present (GTK_WINDOW(root()));
 }
diff --git a/pan/gui/post-ui.h b/pan/gui/post-ui.h
index 766e438..f879d14 100644
--- a/pan/gui/post-ui.h
+++ b/pan/gui/post-ui.h
@@ -142,6 +142,7 @@ namespace pan
 
     public:
       void set_spellcheck_enabled (bool);
+	  void spawn_editor_dead(char *);
 
   };
 }



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