[gparted/psusi/refactor: 18/19] Refactor linux_swap to use execute_command_async



commit 1b166c2dda3e6dabeaed2ebd9a61ea8c63bbb5ea
Author: Phillip Susi <psusi ubuntu com>
Date:   Tue Feb 21 21:53:12 2012 -0500

    Refactor linux_swap to use execute_command_async
    
    Instead of Filesystem::execute_command or Utils::execute_command, the
    linux_swap methods now use Filesystem::execute_command_async, and block the
    calling thread on a mutex until the job completes.  Methods that invoked
    multiple external utilities have been split into multiple functions that
    are invoked as the callback when the previous command has completed.

 include/linux_swap.h |   17 ++++
 src/linux_swap.cc    |  202 +++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 167 insertions(+), 52 deletions(-)
---
diff --git a/include/linux_swap.h b/include/linux_swap.h
index 6be8c23..d99ac6b 100644
--- a/include/linux_swap.h
+++ b/include/linux_swap.h
@@ -32,20 +32,37 @@ public:
 
 	FS get_filesystem_support() ;
 	void set_used_sectors( Partition & partition ) ;
+	void set_used_sectors( Partition & partition, sigc::slot<bool, double> slot );
 	void read_label( Partition & partition ) ;
+	void read_label( Partition & partition, sigc::slot<bool, double> slot );
 	bool write_label( const Partition & partition, OperationDetail & operationdetail ) ;
+	void write_label( const Partition & partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot );
 	void read_uuid( Partition & partition ) ;
+	void read_uuid( Partition & partition, sigc::slot<bool, double> slot );
 	bool write_uuid( const Partition & partition, OperationDetail & operationdetail ) ;
+	void write_uuid( const Partition & partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot );
 	bool create( const Partition & new_partition, OperationDetail & operationdetail ) ;
+	void create( const Partition & new_partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot );
 	bool resize( const Partition & partition_new, OperationDetail & operationdetail, bool fill_partition = false ) ;
+	void resize( const Partition & partition_new, OperationDetail & operationdetail,
+		     bool fill_partition, sigc::slot<bool, double> slot );
 	bool move( const Partition & partition_new
 	         , const Partition & partition_old
 	         , OperationDetail & operationdetail
 	         ) ;
+	void move( const Partition & partition_new, const Partition & partition_old,
+		   OperationDetail & operationdetail, sigc::slot<bool, double> slot );
 	bool copy( const Glib::ustring & src_part_path,
 		   const Glib::ustring & dest_part_path,
 		   OperationDetail & operationdetail ) ;
+	void copy( const Glib::ustring &src_part_path, const Glib::ustring &dest_part_path,
+		   OperationDetail &operationdetail, sigc::slot<bool, double> slot );
 	bool check_repair( const Partition & partition, OperationDetail & operationdetail ) ;
+	void check_repair( const Partition & partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot );
+private:
+	bool read_label2( double progress, Partition *partition );
+	bool read_uuid2( double progress, Partition *partition );
+	bool resize2( double progress, OperationDetail *operationdetail );
 };
 
 } //GParted
diff --git a/src/linux_swap.cc b/src/linux_swap.cc
index 64d4404..751ddd6 100644
--- a/src/linux_swap.cc
+++ b/src/linux_swap.cc
@@ -70,69 +70,150 @@ void linux_swap::set_used_sectors( Partition & partition )
 {
 }
 
+void linux_swap::set_used_sectors( Partition & partition, sigc::slot<bool, double> slot )
+{
+}
+
 void linux_swap::read_label( Partition & partition )
 {
-	if ( ! Utils::execute_command( "vol_id " + partition .get_path(), output, error, true ) )
+	read_label( partition, unlock_mutex );
+	mutex.lock();
+}
+
+void linux_swap::read_label( Partition & partition, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "vol_id " + partition .get_path(),
+			 sigc::bind( sigc::mem_fun( *this, &linux_swap::read_label2 ),
+				     &partition ) );
+}
+
+bool linux_swap::read_label2( double progress, Partition *partition )
+{
+	if ( progress != 1.0 )
+		return false;
+	if ( !exit_status )
 	{
-		partition .label = Utils::regexp_label( output, "ID_FS_LABEL=([^\n]*)" ) ;
+		partition->label = Utils::regexp_label( output, "ID_FS_LABEL=([^\n]*)" );
 	}
 	else
 	{
-		if ( ! output .empty() )
-			partition .messages .push_back( output ) ;
-		
-		if ( ! error .empty() )
-			partition .messages .push_back( error ) ;
+		if ( !output.empty() )
+			partition->messages.push_back( output );
+		if ( !error.empty() )
+			partition->messages.push_back( error );
 	}
+	return slot( 1.0 );
 }
 
 bool linux_swap::write_label( const Partition & partition, OperationDetail & operationdetail )
 {
-	return ! execute_command( "swaplabel -L \"" + partition .label + "\" " + partition .get_path(), operationdetail ) ;
+	write_label( partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void linux_swap::write_label( const Partition & partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "swaplabel -L \"" + partition .label + "\" " + partition.get_path(),
+			 operationdetail,
+			 set_success );
 }
 
 void linux_swap::read_uuid( Partition & partition )
 {
-	if ( ! Utils::execute_command( "swaplabel " + partition .get_path(), output, error, true ) )
+	read_uuid( partition, unlock_mutex );
+	mutex.lock();
+}
+
+void linux_swap::read_uuid( Partition & partition, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "swaplabel " + partition.get_path(),
+			 sigc::bind( sigc::mem_fun( *this, &linux_swap::read_uuid2 ),
+				     &partition ) );
+}
+
+bool linux_swap::read_uuid2( double progress, Partition *partition )
+{
+	if ( progress != 1.0 )
+		return false;
+	if ( !exit_status )
 	{
-		partition .uuid = Utils::regexp_label( output, "^UUID:[[:blank:]]*([^[:space:]]*)" ) ;
+		partition->uuid = Utils::regexp_label( output, "^UUID:[[:blank:]]*([^[:space:]]*)" );
 	}
 	else
 	{
-		if ( ! output .empty() )
-			partition .messages .push_back( output ) ;
-
-		if ( ! error .empty() )
-			partition .messages .push_back( error ) ;
+		if ( !output.empty() )
+			partition->messages.push_back( output );
+		if ( !error.empty() )
+			partition->messages.push_back( error );
 	}
+	return slot( 1.0 );
 }
 
-
 bool linux_swap::write_uuid( const Partition & partition, OperationDetail & operationdetail )
 {
-	return ! execute_command( "swaplabel -U \"" + Utils::generate_uuid() + "\" " + partition .get_path(), operationdetail ) ;
+	write_uuid( partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void linux_swap::write_uuid( const Partition & partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "swaplabel -U \"" + Utils::generate_uuid() + "\" " + partition.get_path(),
+			 operationdetail,
+			 set_success );
 }
 
 bool linux_swap::create( const Partition & new_partition, OperationDetail & operationdetail )
 {
-	return ! execute_command( "mkswap -L \"" + new_partition .label + "\" " + new_partition .get_path(), operationdetail ) ;
+	create( new_partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void linux_swap::create( const Partition & new_partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "mkswap -L \"" + new_partition.label + "\" " + new_partition.get_path(),
+			 operationdetail,
+			 set_success );
 }
 
 bool linux_swap::resize( const Partition & partition_new, OperationDetail & operationdetail, bool fill_partition )
 {
-	/*TO TRANSLATORS: looks like create new linux-swap file system */ 
-	operationdetail .add_child( OperationDetail( 
-		String::ucompose( _("create new %1 file system"), Utils::get_filesystem_string( FS_LINUX_SWAP ) ) ) ) ;
+	resize( partition_new, operationdetail, fill_partition, unlock_mutex );
+	mutex.lock();
+	return success;
+}
 
+void linux_swap::resize( const Partition & partition_new, OperationDetail & operationdetail,
+			 bool fill_partition, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	/*TO TRANSLATORS: looks like create new linux-swap file system */ 
+	operationdetail.add_child( OperationDetail( 
+		String::ucompose( _("create new %1 file system"), Utils::get_filesystem_string( FS_LINUX_SWAP ) ) ) );
 	//Maintain label and uuid when recreating swap
-	Glib::ustring command = "mkswap -L \"" + partition_new .label + "\" " ;
-	if ( ! partition_new .uuid .empty() )
-		command +=  " -U \"" + partition_new .uuid + "\" " ;
-	command += partition_new .get_path() ;
-	bool exit_status = ! execute_command( command , operationdetail .get_last_child() ) ;
+	Glib::ustring command = "mkswap -L \"" + partition_new.label + "\" " ;
+	if ( !partition_new.uuid.empty() )
+		command +=  " -U \"" + partition_new.uuid + "\" " ;
+	command += partition_new.get_path();
+	execute_command( command , operationdetail.get_last_child(),
+			 sigc::bind( sigc::mem_fun( *this, &linux_swap::resize2 ),
+				     &operationdetail.get_last_child() ) );
+}
 
-	operationdetail .get_last_child() .set_status( exit_status ? STATUS_SUCCES : STATUS_ERROR ) ;
-	return exit_status ;
+bool linux_swap::resize2( double progress, OperationDetail *operationdetail )
+{
+	if ( progress != 1.0 )
+		return false;
+	operationdetail->get_last_child().set_status( exit_status ? STATUS_ERROR : STATUS_SUCCES );
+	success = !exit_status;
+	return slot( 1.0 );
 }
 
 bool linux_swap::move( const Partition & partition_new
@@ -140,38 +221,51 @@ bool linux_swap::move( const Partition & partition_new
                      , OperationDetail & operationdetail
                      )
 {
-	//Since linux-swap does not contain data, do not actually move the partition
-	operationdetail .add_child(
-	    OperationDetail(
-	                     /* TO TRANSLATORS: looks like   Partition move action skipped because linux-swap file system does not contain data */
-	                     String::ucompose( _("Partition move action skipped because %1 file system does not contain data")
-	                                     , Utils::get_filesystem_string( FS_LINUX_SWAP )
-	                                     )
-	                   , STATUS_NONE
-	                   , FONT_ITALIC
-	                   )
-	                          ) ;
+	move( partition_new, partition_old, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
 
-	return true ;
+void linux_swap::move( const Partition & partition_new, const Partition & partition_old,
+		       OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	//Since linux-swap does not contain data, do not actually move the partition
+	operationdetail.add_child(
+		OperationDetail(
+			/* TO TRANSLATORS: looks like   Partition move action skipped because linux-swap file system does not contain data */
+			String::ucompose( _("Partition move action skipped because %1 file system does not contain data")
+					  , Utils::get_filesystem_string( FS_LINUX_SWAP ) ),
+			STATUS_NONE,
+			FONT_ITALIC ) );
+	success = true;
+	slot( 1.0 );
 }
 
 bool linux_swap::copy( const Glib::ustring & src_part_path,
 		       const Glib::ustring & dest_part_path,
 		       OperationDetail & operationdetail )
 {
-	//Since linux-swap does not contain data, do not actually copy the partition
-	operationdetail .add_child(
-	    OperationDetail(
-	                     /* TO TRANSLATORS: looks like   Partition copy action skipped because linux-swap file system does not contain data */
-	                     String::ucompose( _("Partition copy action skipped because %1 file system does not contain data")
-	                                     , Utils::get_filesystem_string( FS_LINUX_SWAP )
-	                                     )
-	                   , STATUS_NONE
-	                   , FONT_ITALIC
-	                   )
-	                          ) ;
+	copy( src_part_path, dest_part_path, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
 
-	return true ;
+void linux_swap::copy( const Glib::ustring &src_part_path, const Glib::ustring &dest_part_path,
+		       OperationDetail &operationdetail, sigc::slot<bool, double> slot )
+
+{
+	this->slot = slot;
+	//Since linux-swap does not contain data, do not actually copy the partition
+	operationdetail.add_child(
+		OperationDetail(
+			/* TO TRANSLATORS: looks like   Partition copy action skipped because linux-swap file system does not contain data */
+			String::ucompose( _("Partition copy action skipped because %1 file system does not contain data"),
+					  Utils::get_filesystem_string( FS_LINUX_SWAP ) ),
+			STATUS_NONE,
+	                FONT_ITALIC ) );
+	success = true;
+	slot( 1.0 );
 }
 
 bool linux_swap::check_repair( const Partition & partition, OperationDetail & operationdetail )
@@ -179,4 +273,8 @@ bool linux_swap::check_repair( const Partition & partition, OperationDetail & op
 	return true ;
 }
 
+void linux_swap::check_repair( const Partition & partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+}
+
 } //GParted



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