[gparted] Display pop up dialog on libparted exceptions



commit a1f843e74ae2be98a146c5b77f3a4e0c2f4ea80a
Author: Phillip Susi <psusi cfl rr com>
Date:   Wed Jan 4 19:30:28 2012 -0500

    Display pop up dialog on libparted exceptions
    
    We used to just log libparted exceptions without handling them.  This patch
    changes the exception handler to display a modal dialog box and return the
    chosen action to libparted.

 include/GParted_Core.h |    1 +
 src/Dialog_Progress.cc |    3 +-
 src/GParted_Core.cc    |   63 ++++++++++++++++++++++++++++++++++++++++++++++-
 src/Win_GParted.cc     |    4 +-
 src/main.cc            |    4 +++
 5 files changed, 70 insertions(+), 5 deletions(-)
---
diff --git a/include/GParted_Core.h b/include/GParted_Core.h
index 03ad852..670ae5d 100644
--- a/include/GParted_Core.h
+++ b/include/GParted_Core.h
@@ -32,6 +32,7 @@ namespace GParted
 class GParted_Core
 {
 public:
+	static Glib::Thread *mainthread;
 	GParted_Core() ;
 	~GParted_Core() ;
 
diff --git a/src/Dialog_Progress.cc b/src/Dialog_Progress.cc
index c6f02b7..ecb7146 100644
--- a/src/Dialog_Progress.cc
+++ b/src/Dialog_Progress.cc
@@ -221,8 +221,9 @@ void Dialog_Progress::on_signal_show()
 			
 			while ( Gtk::Main::events_pending() )
 				Gtk::Main::iteration();
-
+			gdk_threads_leave();
 			usleep( 10000 ) ;
+			gdk_threads_enter();
 		}
 
 		//set status (succes/error) for this operation
diff --git a/src/GParted_Core.cc b/src/GParted_Core.cc
index 9c6b116..346a27d 100644
--- a/src/GParted_Core.cc
+++ b/src/GParted_Core.cc
@@ -55,6 +55,7 @@
 #include <unistd.h>
 #include <dirent.h>
 #include <mntent.h>
+#include <gtkmm/messagedialog.h>
 
 std::vector<Glib::ustring> libparted_messages ; //see ped_exception_handler()
 
@@ -3180,18 +3181,76 @@ void GParted_Core::settle_device( std::time_t timeout )
 		sleep( timeout ) ;
 }
 
+class PedExceptionMsg : public Gtk::MessageDialog
+{
+public:
+	PedExceptionMsg( PedException &e ) : MessageDialog( Glib::ustring(e.message), false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_NONE, true )
+		{
+			switch( e.type )
+			{
+			case PED_EXCEPTION_WARNING:
+				set_title( _("Libparted Warning") );
+				property_message_type() = Gtk::MESSAGE_WARNING;
+				break;
+			case PED_EXCEPTION_INFORMATION:
+				set_title( _("Libparted Information") );
+				property_message_type() = Gtk::MESSAGE_INFO;
+				break;
+			case PED_EXCEPTION_ERROR:
+				set_title( _("Libparted Error") );
+			default:
+				set_title( _("Libparted Bug Found!") );
+			}
+			if (e.options & PED_EXCEPTION_FIX)
+				add_button( _("Fix"), PED_EXCEPTION_FIX );
+			if (e.options & PED_EXCEPTION_YES)
+				add_button( _("Yes"), PED_EXCEPTION_YES );
+			if (e.options & PED_EXCEPTION_OK)
+				add_button( _("Ok"), PED_EXCEPTION_OK );
+			if (e.options & PED_EXCEPTION_RETRY)
+				add_button( _("Retry"), PED_EXCEPTION_RETRY );
+			if (e.options & PED_EXCEPTION_NO)
+				add_button( _("No"), PED_EXCEPTION_NO );
+			if (e.options & PED_EXCEPTION_CANCEL)
+				add_button( _("Cancel"), PED_EXCEPTION_CANCEL );
+			if (e.options & PED_EXCEPTION_IGNORE)
+				add_button( _("Ignore"), PED_EXCEPTION_IGNORE );
+		}
+};
+
 PedExceptionOption GParted_Core::ped_exception_handler( PedException * e ) 
 {
+	PedExceptionOption ret = PED_EXCEPTION_UNHANDLED;
         std::cout << e ->message << std::endl ;
 
         libparted_messages .push_back( e->message ) ;
-
-	return PED_EXCEPTION_UNHANDLED ;
+	char optcount = 0;
+	int opt = 0;
+	for( char c = 0; c < 10; c++ )
+		if( e->options & (1 << c) ) {
+			opt = (1 << c);
+			optcount++;
+		}
+	// if only one option was given, choose it without popup
+	if( optcount == 1 && e->type != PED_EXCEPTION_BUG && e->type != PED_EXCEPTION_FATAL )
+		return (PedExceptionOption)opt;
+	if (Glib::Thread::self() != GParted_Core::mainthread)
+		gdk_threads_enter();
+	PedExceptionMsg msg( *e );
+	msg.show_all();
+	ret = (PedExceptionOption)msg.run();
+	if (Glib::Thread::self() != GParted_Core::mainthread)
+		gdk_threads_leave();
+	if (ret < 0)
+		ret = PED_EXCEPTION_UNHANDLED;
+	return ret;
 }
 
 GParted_Core::~GParted_Core() 
 {
 	delete p_filesystem;
 }
+
+Glib::Thread *GParted_Core::mainthread;
 	
 } //GParted
diff --git a/src/Win_GParted.cc b/src/Win_GParted.cc
index 0a92c62..5da4e6c 100644
--- a/src/Win_GParted.cc
+++ b/src/Win_GParted.cc
@@ -617,9 +617,9 @@ void Win_GParted::show_pulsebar( const Glib::ustring & status_message )
 		pulsebar .pulse();
 		while ( Gtk::Main::events_pending() )
 			Gtk::Main::iteration();
-		
+		gdk_threads_leave();
 		usleep( 10000 );
-
+		gdk_threads_enter();
 		Glib::ustring tmp_msg = gparted_core .get_thread_status_message() ;
 		if ( tmp_msg != "" )
 			statusbar .push( tmp_msg ) ;
diff --git a/src/main.cc b/src/main.cc
index fc0aeee..36c2b33 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -19,11 +19,15 @@
 
 #include <gtkmm/messagedialog.h>
 #include <gtkmm/main.h>
+#include "../include/GParted_Core.h"
 
 int main( int argc, char *argv[] )
 {
 	//initialize thread system
 	Glib::thread_init() ;
+	gdk_threads_init();
+	gdk_threads_enter();
+	GParted::GParted_Core::mainthread = Glib::Thread::self();
 	
 	Gtk::Main kit( argc, argv ) ;
 		 



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