[Xfce4-commits] <xfce4-cpugraph-plugin:master> Access settings through dedicated functions

Florian noreply at xfce.org
Sat Apr 17 16:16:21 CEST 2010


Updating branch refs/heads/master
         to 899594331e4fd40ab62c6d3da6737eec7636a117 (commit)
       from 14a9022f547244ed2a8fef113a53bb673d58bd97 (commit)

commit 899594331e4fd40ab62c6d3da6737eec7636a117
Author: Florian <frivoal at gmail.com>
Date:   Fri Feb 26 18:05:27 2010 +0900

    Access settings through dedicated functions
    
    Improve encapsulation by creating write-accessors for the monitor's
    member variables, and using them for initialization and from the
    properties dialog.

 panel-plugin/actions.c    |  117 ++++++----------------------------------
 panel-plugin/actions.h    |    5 +-
 panel-plugin/cpu.c        |   91 +++++++++++++++++++++++++++++++
 panel-plugin/cpu.h        |   16 +++++-
 panel-plugin/properties.c |    2 +-
 panel-plugin/settings.c   |  130 +++++++++++++++++----------------------------
 6 files changed, 174 insertions(+), 187 deletions(-)

diff --git a/panel-plugin/actions.c b/panel-plugin/actions.c
index ad12748..1a88f0d 100644
--- a/panel-plugin/actions.c
+++ b/panel-plugin/actions.c
@@ -2,11 +2,10 @@
 
 void AssociateCommandChange( GtkEntry *entry, CPUGraph * base )
 {
-	g_free (base->m_AssociateCommand );
-	base->m_AssociateCommand = g_strdup( gtk_entry_get_text( entry ) );
+	set_command( base, gtk_entry_get_text( entry ) );
 }
 
-void ChangeColor( int color, CPUGraph * base )
+GdkColor ChangeColor( CPUGraph *base, GdkColor color, GtkWidget *button )
 {
 	GtkWidget *dialog;
 	GtkColorSelection *colorsel;
@@ -17,81 +16,45 @@ void ChangeColor( int color, CPUGraph * base )
 
 	colorsel = GTK_COLOR_SELECTION( GTK_COLOR_SELECTION_DIALOG( dialog )->colorsel );
 
-	if( color == 0 )
-	{
-		gtk_color_selection_set_previous_color( colorsel, &base->m_ForeGround1 );
-		gtk_color_selection_set_current_color( colorsel, &base->m_ForeGround1 );
-	}
-	else if( color == 1 )
-	{
-		gtk_color_selection_set_previous_color( colorsel, &base->m_ForeGround2 );
-		gtk_color_selection_set_current_color( colorsel, &base->m_ForeGround2 );
-	}
-	else if( color == 2 )
-	{
-		gtk_color_selection_set_previous_color( colorsel, &base->m_BackGround );
-		gtk_color_selection_set_current_color( colorsel, &base->m_BackGround );
-	}
-	else if( color == 3 )
-	{
-		gtk_color_selection_set_previous_color( colorsel, &base->m_ForeGround3 );
-		gtk_color_selection_set_current_color( colorsel, &base->m_ForeGround3 );
-	}
+	gtk_color_selection_set_previous_color( colorsel, &color );
+	gtk_color_selection_set_current_color( colorsel, &color );
 
 	gtk_color_selection_set_has_palette( colorsel, TRUE );
 
 	response = gtk_dialog_run( GTK_DIALOG( dialog ) );
 	if( response == GTK_RESPONSE_OK )
 	{
-		if( color == 0 )
-		{
-			gtk_color_selection_get_current_color( colorsel, &base->m_ForeGround1 );
-			gtk_widget_modify_bg( base->m_Options.m_ColorDA, GTK_STATE_NORMAL, &base->m_ForeGround1 );
-		}
-		else if( color == 1 )
-		{
-			gtk_color_selection_get_current_color( colorsel, &base->m_ForeGround2 );
-			gtk_widget_modify_bg( base->m_Options.m_ColorDA2, GTK_STATE_NORMAL, &base->m_ForeGround2 );
-		}
-		else if( color == 2 )
-		{
-			gtk_color_selection_get_current_color( colorsel, &base->m_BackGround );
-			gtk_widget_modify_bg( base->m_Options.m_ColorDA3, GTK_STATE_NORMAL, &base->m_BackGround );
-		}
-		else if( color == 3 )
-		{
-			gtk_color_selection_get_current_color( colorsel, &base->m_ForeGround3 );
-			gtk_widget_modify_bg( base->m_Options.m_ColorDA5, GTK_STATE_NORMAL, &base->m_ForeGround3 );
-		}
+		gtk_color_selection_get_current_color( colorsel, &color );
+		gtk_widget_modify_bg( button, GTK_STATE_NORMAL, &color );
 	}
 
 	gtk_widget_destroy( dialog );
-
+	return color;
 }
 
 void ChangeColor1( GtkButton * button, CPUGraph * base )
 {
-	ChangeColor( 0, base );
+	set_foreground_color1( base, ChangeColor( base, base->m_ForeGround1, base->m_Options.m_ColorDA ) );
 }
 
 void ChangeColor2( GtkButton * button, CPUGraph * base )
 {
-	ChangeColor( 1, base );
+	set_foreground_color2( base, ChangeColor( base, base->m_ForeGround2, base->m_Options.m_ColorDA2 ) );
 }
 
 void ChangeColor3( GtkButton * button, CPUGraph * base )
 {
-	ChangeColor( 2, base );
+	set_background_color( base, ChangeColor( base, base->m_BackGround, base->m_Options.m_ColorDA3 ) );
 }
 
 void ChangeColor4( GtkButton * button, CPUGraph * base )
 {
-	ChangeColor( 3, base );
+	set_foreground_color3( base, ChangeColor( base, base->m_ForeGround3, base->m_Options.m_ColorDA5 ) );
 }
 
 void ColorModeChange( GtkOptionMenu * om, CPUGraph * base )
 {
-	base->m_ColorMode = gtk_option_menu_get_history( om );
+	set_color_mode( base, gtk_option_menu_get_history( om ) );
 	if( base->m_ColorMode == 0 )
 	{
 		if( base->m_Mode == 0 || base->m_Mode == 2 )
@@ -128,47 +91,19 @@ void ColorModeChange( GtkOptionMenu * om, CPUGraph * base )
 
 void DialogResponse( GtkWidget *dlg, int response, CPUGraph *base )
 {
-	ApplyChanges( base );
 	gtk_widget_destroy( dlg );
 	xfce_panel_plugin_unblock_menu( base->plugin );
 	WriteSettings( base->plugin, base );
 }
 
-void ApplyChanges( CPUGraph * base )
-{
-	int update;
-
-	if( base->m_TimeoutID )
-		g_source_remove( base->m_TimeoutID );
-	switch( base->m_UpdateInterval )
-	{
-		case 0:
-			update = 250;
-			break;
-		case 1:
-			update = 500;
-			break;
-		case 2:
-			update = 750;
-			break;
-		default:
-			update = 1000;
-	}
-	base->m_TimeoutID = g_timeout_add( update, (GtkFunction) UpdateCPU, base );
-
-	UserSetSize( base );
-	SetHistorySize( base, base->m_Width );
-}
-
 void FrameChange( GtkToggleButton * button, CPUGraph * base )
 {
-	base->m_Frame = gtk_toggle_button_get_active( button );
-	gtk_frame_set_shadow_type( GTK_FRAME( base->m_FrameWidget ), base->m_Frame ? GTK_SHADOW_IN : GTK_SHADOW_NONE );
+	set_frame( base, gtk_toggle_button_get_active( button ) );
 }
 
 void ModeChange( GtkOptionMenu * om, CPUGraph * base )
 {
-	base->m_Mode = gtk_option_menu_get_history( om );
+	set_mode( base, gtk_option_menu_get_history( om ) );
 	if( base->m_Mode == 0 )
 	{
 		if( base->m_ColorMode > 0 )
@@ -195,34 +130,18 @@ void ModeChange( GtkOptionMenu * om, CPUGraph * base )
 	}
 }
 
-void SpinChange( GtkSpinButton * sb, int *value )
+void WidthChange( GtkSpinButton * sb, CPUGraph *base)
 {
-	(*value) = gtk_spin_button_get_value_as_int( sb );
+	set_width( base, gtk_spin_button_get_value_as_int( sb ) );
 }
 
 void TimeScaleChange( GtkToggleButton * button, CPUGraph * base )
 {
-	base->m_TimeScale = gtk_toggle_button_get_active( button );
+	set_nonlinear_time( base, gtk_toggle_button_get_active( button ) );
 }
 
 void UpdateChange( GtkOptionMenu * om, CPUGraph * base )
 {
-	base->m_UpdateInterval = gtk_option_menu_get_history( om );
+	set_update_rate( base, gtk_option_menu_get_history( om ) );
 }
 
-void SetHistorySize( CPUGraph * base, int size )
-{
-	int i;
-	base->m_History = (long *) g_realloc( base->m_History, 2 * size * sizeof( long ) );
-
-	base->m_CpuData = cpuData_read();
-	base->m_CpuData[0].pUsed = 0;
-	base->m_CpuData[0].pTotal = 0;
-	long usage = base->m_CpuData[0].load;
-	for( i = size - 1; i >= base->m_Values; i-- )
-	{
-		base->m_History[i] = usage;
-		base->m_History[i+size] = base->m_CpuData[0].scalCurFreq;
-	}
-	base->m_Values = size;
-}
diff --git a/panel-plugin/actions.h b/panel-plugin/actions.h
index 20c4204..7a64d98 100644
--- a/panel-plugin/actions.h
+++ b/panel-plugin/actions.h
@@ -1,7 +1,7 @@
 #include <cpu.h>
 
 void AssociateCommandChange( GtkEntry *entry, CPUGraph *base );
-void ChangeColor( int color, CPUGraph *base );
+GdkColor ChangeColor( CPUGraph *base, GdkColor color, GtkWidget *button );
 void ChangeColor1( GtkButton *button, CPUGraph *base );
 void ChangeColor2( GtkButton *button, CPUGraph *base );
 void ChangeColor3( GtkButton *button, CPUGraph *base );
@@ -11,7 +11,6 @@ void DialogResponse( GtkWidget *dlg, int response, CPUGraph *base );
 void ApplyChanges( CPUGraph *base );
 void FrameChange( GtkToggleButton *button, CPUGraph *base );
 void ModeChange( GtkOptionMenu *om, CPUGraph *base );
-void SpinChange( GtkSpinButton *sb, int *value );
+void WidthChange( GtkSpinButton *sb, CPUGraph *base );
 void TimeScaleChange( GtkToggleButton *button, CPUGraph *base );
 void UpdateChange( GtkOptionMenu *om, CPUGraph *base );
-void SetHistorySize( CPUGraph * base, int size );
diff --git a/panel-plugin/cpu.c b/panel-plugin/cpu.c
index 256b1f3..ac92f04 100644
--- a/panel-plugin/cpu.c
+++ b/panel-plugin/cpu.c
@@ -247,3 +247,94 @@ gboolean LaunchCommand( GtkWidget*w,GdkEventButton *event, CPUGraph *base )
 	}
 	return FALSE;
 }
+
+void set_command( CPUGraph *base, const gchar *command )
+{
+	g_free (base->m_AssociateCommand );
+	base->m_AssociateCommand = g_strdup( command );
+}
+
+void set_frame( CPUGraph *base, gboolean frame )
+{
+	base->m_Frame = frame;
+	gtk_frame_set_shadow_type( GTK_FRAME( base->m_FrameWidget ), base->m_Frame ? GTK_SHADOW_IN : GTK_SHADOW_NONE );
+}
+
+void set_nonlinear_time( CPUGraph *base, gboolean nonlinear )
+{
+	base->m_TimeScale = nonlinear;
+}
+
+void set_update_rate( CPUGraph *base, int rate )
+{
+	int update;
+
+	base->m_UpdateInterval = rate;
+
+	if( base->m_TimeoutID )
+		g_source_remove( base->m_TimeoutID );
+	switch( base->m_UpdateInterval )
+	{
+		case 0:
+			update = 250;
+			break;
+		case 1:
+			update = 500;
+			break;
+		case 2:
+			update = 750;
+			break;
+		default:
+			update = 1000;
+	}
+	base->m_TimeoutID = g_timeout_add( update, (GtkFunction) UpdateCPU, base );
+}
+
+void set_width( CPUGraph *base, int width )
+{
+	base->m_Width = width;
+	int i;
+	base->m_History = (long *) g_realloc( base->m_History, 2 * width * sizeof( long ) );
+
+	base->m_CpuData = cpuData_read();
+	base->m_CpuData[0].pUsed = 0;
+	base->m_CpuData[0].pTotal = 0;
+	long usage = base->m_CpuData[0].load;
+	for( i = width - 1; i >= base->m_Values; i-- )
+	{
+		base->m_History[i] = usage;
+		base->m_History[i+width] = base->m_CpuData[0].scalCurFreq;
+	}
+	base->m_Values = width;
+	UserSetSize( base );
+}
+
+void set_color_mode( CPUGraph *base, int color_mode )
+{
+	base->m_ColorMode = color_mode;
+}
+
+void set_mode( CPUGraph *base, int mode )
+{
+	base->m_Mode = mode;
+}
+
+void set_foreground_color1( CPUGraph *base, GdkColor color )
+{
+	base->m_ForeGround1 = color;
+}
+
+void set_foreground_color2( CPUGraph *base, GdkColor color )
+{
+	base->m_ForeGround2 = color;
+}
+
+void set_foreground_color3( CPUGraph *base, GdkColor color )
+{
+	base->m_ForeGround3 = color;
+}
+
+void set_background_color( CPUGraph *base, GdkColor color )
+{
+	base->m_BackGround = color;
+}
diff --git a/panel-plugin/cpu.h b/panel-plugin/cpu.h
index a0ca879..919030b 100644
--- a/panel-plugin/cpu.h
+++ b/panel-plugin/cpu.h
@@ -50,11 +50,11 @@ typedef struct
 	SOptions m_Options;
 
 	int m_UpdateInterval; // Number of ms between updates.
-	int m_TimeScale; // Wether to use non-linear time scale.
+	gboolean m_TimeScale; // Wether to use non-linear time scale.
 	int m_Width; // The width of the plugin.
 	int m_Mode; // Eventual mode of the plugin.
 	int m_ColorMode;
-	int m_Frame;
+	gboolean m_Frame;
 	gchar  *m_AssociateCommand;
 	guint nrCores; // Number of cores (not including total cpu)
 
@@ -85,4 +85,16 @@ void CreateOptions( XfcePanelPlugin *plugin, CPUGraph *base );
 void SetOrientation( XfcePanelPlugin *plugin, GtkOrientation orientation, CPUGraph *base );
 
 gboolean LaunchCommand( GtkWidget *w, GdkEventButton *event, CPUGraph *base );
+
+void set_command( CPUGraph *base, const gchar *command );
+void set_frame( CPUGraph *base, gboolean frame );
+void set_nonlinear_time( CPUGraph *base, gboolean nonlinear );
+void set_update_rate( CPUGraph *base, int rate );
+void set_width( CPUGraph *base, int width );
+void set_color_mode( CPUGraph *base, int color_mode );
+void set_mode( CPUGraph *base, int mode );
+void set_foreground_color1( CPUGraph *base, GdkColor color );
+void set_foreground_color2( CPUGraph *base, GdkColor color );
+void set_foreground_color3( CPUGraph *base, GdkColor color );
+void set_background_color( CPUGraph *base, GdkColor color );
 #endif
diff --git a/panel-plugin/properties.c b/panel-plugin/properties.c
index b8d4844..40efb22 100644
--- a/panel-plugin/properties.c
+++ b/panel-plugin/properties.c
@@ -188,7 +188,7 @@ static void SetupWidthOption( GtkBox *vbox, GtkSizeGroup *sg, XfcePanelPlugin *p
 	gtk_spin_button_set_value( GTK_SPIN_BUTTON( Size ), base->m_Width );
 	gtk_widget_show( Size );
 	gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( Size ), FALSE, FALSE, 0 );
-	g_signal_connect( Size, "value-changed", G_CALLBACK( SpinChange ), &base->m_Width );
+	g_signal_connect( Size, "value-changed", G_CALLBACK( WidthChange ), base );
 }
 
 static void SetupAssociateCommandOption( GtkBox *vbox, GtkSizeGroup *sg, CPUGraph *base )
diff --git a/panel-plugin/settings.c b/panel-plugin/settings.c
index e344660..546313d 100644
--- a/panel-plugin/settings.c
+++ b/panel-plugin/settings.c
@@ -1,35 +1,39 @@
-#include <actions.h>
+#include <cpu.h>
 
 void ReadSettings( XfcePanelPlugin * plugin, CPUGraph * base )
 {
 	const char *value;
 	char *file;
 	XfceRc *rc;
-	int update;
 
-	base->m_Width = 70;
+	int rate = 0;
+	gboolean nonlinear = FALSE;
+	int width = 70;
+	int mode = 0;
+	int color_mode = 0;
+	gboolean frame = FALSE;
+	const gchar  *associated_command = DEFAULT_COMMAND;
 
-	base->m_ForeGround1.red = 0;
-	base->m_ForeGround1.green = 65535;
-	base->m_ForeGround1.blue = 0;
+	GdkColor foreground1;
+	GdkColor foreground2;
+	GdkColor foreground3;
+	GdkColor background;
 
-	base->m_ForeGround2.red = 65535;
-	base->m_ForeGround2.green = 0;
-	base->m_ForeGround2.blue = 0;
+	foreground1.red = 0;
+	foreground1.green = 65535;
+	foreground1.blue = 0;
 
-	base->m_ForeGround3.red = 0;
-	base->m_ForeGround3.green = 0;
-	base->m_ForeGround3.blue = 65535;
+	foreground2.red = 65535;
+	foreground2.green = 0;
+	foreground2.blue = 0;
 
-	base->m_BackGround.red = 65535;
-	base->m_BackGround.green = 65535;
-	base->m_BackGround.blue = 65535;
+	foreground3.red = 0;
+	foreground3.green = 0;
+	foreground3.blue = 65535;
 
-	base->m_TimeScale = 0;
-	base->m_Frame = 0;
-	base->m_AssociateCommand = NULL;
-	base->m_ColorMode = 0;
-	base->m_Mode = 0;
+	background.red = 65535;
+	background.green = 65535;
+	background.blue = 65535;
 
 	if( (file = xfce_panel_plugin_lookup_rc_file( plugin )) != NULL )
 	{
@@ -38,76 +42,38 @@ void ReadSettings( XfcePanelPlugin * plugin, CPUGraph * base )
 
 		if( rc )
 		{
-			base->m_UpdateInterval =
-				xfce_rc_read_int_entry (rc, "UpdateInterval",
-						base->m_UpdateInterval);
-
-			base->m_TimeScale =
-				xfce_rc_read_int_entry (rc, "TimeScale",
-						base->m_TimeScale);
-
-			base->m_Width =
-				xfce_rc_read_int_entry( rc, "Width", base->m_Width );
-
-			base->m_Mode = xfce_rc_read_int_entry( rc, "Mode", base->m_Mode );
-
-			base->m_Frame =
-				xfce_rc_read_int_entry( rc, "Frame", base->m_Frame );
-
-			if( value = xfce_rc_read_entry( rc, "AssociateCommand", DEFAULT_COMMAND ) ) {
-				base->m_AssociateCommand = g_strdup(value);
-			}
-			else
-			{
-				base->m_AssociateCommand = g_strdup( DEFAULT_COMMAND );
-			}
-
-			base->m_ColorMode =
-				xfce_rc_read_int_entry( rc, "ColorMode", base->m_ColorMode );
+			rate =  xfce_rc_read_int_entry (rc, "UpdateInterval", rate );
+			nonlinear = xfce_rc_read_int_entry (rc, "TimeScale", nonlinear );
+			width = xfce_rc_read_int_entry( rc, "Width", width );
+			mode = xfce_rc_read_int_entry( rc, "Mode", mode );
+			color_mode = xfce_rc_read_int_entry( rc, "ColorMode", color_mode );
+			frame = xfce_rc_read_int_entry( rc, "Frame", frame );
+			associated_command = xfce_rc_read_entry( rc, "AssociateCommand", associated_command );
 
 			if( (value = xfce_rc_read_entry( rc, "Foreground1", NULL )) )
-			{
-				gdk_color_parse( value, &base->m_ForeGround1 );
-			}
+				gdk_color_parse( value, &foreground1 );
 			if( (value = xfce_rc_read_entry( rc, "Foreground2", NULL )) )
-			{
-				gdk_color_parse( value, &base->m_ForeGround2 );
-			}
-			if( (value = xfce_rc_read_entry( rc, "Background", NULL )) )
-			{
-				gdk_color_parse( value, &base->m_BackGround );
-			}
+				gdk_color_parse( value, &foreground2 );
 			if( (value = xfce_rc_read_entry( rc, "Foreground3", NULL )) )
-			{
-				gdk_color_parse( value, &base->m_ForeGround3 );
-			}
+				gdk_color_parse( value, &foreground3 );
+			if( (value = xfce_rc_read_entry( rc, "Background", NULL )) )
+				gdk_color_parse( value, &background );
 
 			xfce_rc_close( rc );
 		}
 	}
-	else
-	{
-		base->m_AssociateCommand = g_strdup( DEFAULT_COMMAND );
-	}
-	SetHistorySize( base, base->m_Width );
-	if( base->m_TimeoutID )
-		g_source_remove( base->m_TimeoutID );
-	switch( base->m_UpdateInterval )
-	{
-		case 0:
-			update = 250;
-			break;
-		case 1:
-			update = 500;
-			break;
-		case 2:
-			update = 750;
-			break;
-		default:
-			update = 1000;
-	}
-	base->m_TimeoutID = g_timeout_add( update, (GtkFunction) UpdateCPU, base );
-	gtk_frame_set_shadow_type( GTK_FRAME(base->m_FrameWidget ), base->m_Frame ? GTK_SHADOW_IN : GTK_SHADOW_NONE );
+
+	set_update_rate( base, rate );
+	set_nonlinear_time( base, nonlinear );
+	set_width( base, width );
+	set_mode( base,  mode );
+	set_color_mode( base, color_mode );
+	set_frame( base, frame );
+	set_command( base, associated_command );
+	set_foreground_color1( base, foreground1 );
+	set_foreground_color2( base, foreground2 );
+	set_foreground_color3( base, foreground3 );
+	set_background_color( base, background );
 }
 
 void WriteSettings( XfcePanelPlugin *plugin, CPUGraph *base )



More information about the Xfce4-commits mailing list