[nautilus-actions] Define a new NATimeout structure



commit 6f2803590604fcd44d5313e761e9fbf86a75f968
Author: Pierre Wieser <pwieser trychlos org>
Date:   Mon Jan 24 07:04:07 2011 +0100

    Define a new NATimeout structure
    
    + src/core/na-pivot.c (on_item_changed_timeout):
    + src/core/na-settings.c (on_keyfile_changed_timeout):
    + src/io-desktop/nadp-desktop-provider.c (on_monitor_timeout):
    + src/plugin-menu/nautilus-actions.c (on_change_event_timeout): Take adavantage of this new structure.

 ChangeLog                              |   12 ++++++
 src/api/Makefile.am                    |    1 +
 src/core/Makefile.am                   |    5 +-
 src/core/na-pivot.c                    |   68 ++++++++-----------------------
 src/core/na-settings.c                 |   55 +++++++-------------------
 src/io-desktop/nadp-desktop-provider.c |   38 +++--------------
 src/io-desktop/nadp-desktop-provider.h |    8 ++--
 src/plugin-menu/nautilus-actions.c     |   67 +++++++-------------------------
 8 files changed, 74 insertions(+), 180 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 1d9261e..cba5384 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,17 @@
 2011-01-24 Pierre Wieser <pwieser trychlos org>
 
+	* src/api/na-timeout.h:
+	* src/core/na-timeout.c: New files.
+
+	* src/api/Makefile.am:
+	* src/core/Makefile.am: Updated accordingly.
+
+	* src/core/na-pivot.c (on_item_changed_timeout):
+	* src/core/na-settings.c (on_keyfile_changed_timeout):
+	* src/io-desktop/nadp-desktop-provider.c (on_monitor_timeout):
+	* src/plugin-menu/nautilus-actions.c (on_change_event_timeout):
+	Take advantage of new NATimeout convenience structure.
+
 	* src/api/na-iio-provider.h:
 	* src/core/na-io-provider.c:
 	* src/core/na-io-provider.h: Fix comments.
diff --git a/src/api/Makefile.am b/src/api/Makefile.am
index fb8203c..b79622d 100644
--- a/src/api/Makefile.am
+++ b/src/api/Makefile.am
@@ -56,4 +56,5 @@ api_include_HEADERS = \
 	na-object-action.h									\
 	na-object-profile.h									\
 	na-object-menu.h									\
+	na-timeout.h										\
 	$(NULL)
diff --git a/src/core/Makefile.am b/src/core/Makefile.am
index 8c2b5a8..ad942be 100644
--- a/src/core/Makefile.am
+++ b/src/core/Makefile.am
@@ -101,10 +101,11 @@ libna_core_la_SOURCES = \
 	na-selected-info.h									\
 	na-settings.c										\
 	na-settings.h										\
-	na-updater.c										\
-	na-updater.h										\
+	na-timeout.c										\
 	na-tokens.c											\
 	na-tokens.h											\
+	na-updater.c										\
+	na-updater.h										\
 	$(NULL)
 
 libna_core_la_LIBADD = \
diff --git a/src/core/na-pivot.c b/src/core/na-pivot.c
index f02d974..f246db8 100644
--- a/src/core/na-pivot.c
+++ b/src/core/na-pivot.c
@@ -35,6 +35,7 @@
 #include <string.h>
 
 #include <api/na-core-utils.h>
+#include <api/na-timeout.h>
 
 #include "na-io-provider.h"
 #include "na-iprefs.h"
@@ -77,8 +78,7 @@ struct _NAPivotPrivate {
 
 	/* timeout to manage i/o providers 'item-changed' burst
 	 */
-	GTimeVal    last_event;
-	guint       event_source_id;
+	NATimeout   change_timeout;
 
 	/* the preferences management
 	 */
@@ -117,8 +117,7 @@ static NAObjectItem *get_item_from_tree( const NAPivot *pivot, GList *tree, cons
 static void          free_consumers( GList *list );
 
 /* NAIIOProvider management */
-static gboolean      on_item_changed_timeout( NAPivot *pivot );
-static gulong        time_val_diff( const GTimeVal *recent, const GTimeVal *old );
+static void          on_item_changed_timeout( NAPivot *pivot );
 
 GType
 na_pivot_get_type( void )
@@ -225,7 +224,13 @@ instance_init( GTypeInstance *instance, gpointer klass )
 	self->private->consumers = NULL;
 	self->private->tree = NULL;
 	self->private->automatic_reload = FALSE;
-	self->private->event_source_id = 0;
+
+	/* initialize timeout parameters for 'item-changed' handler
+	 */
+	self->private->change_timeout.timeout = st_burst_timeout;
+	self->private->change_timeout.handler = ( NATimeoutFunc ) on_item_changed_timeout;
+	self->private->change_timeout.user_data = self;
+	self->private->change_timeout.source_id = 0;
 }
 
 static void
@@ -627,52 +632,30 @@ na_pivot_on_item_changed_handler( NAIIOProvider *provider, NAPivot *pivot  )
 	g_return_if_fail( NA_IS_PIVOT( pivot ));
 
 	if( !pivot->private->dispose_has_run ){
-
 		g_debug( "%s: provider=%p, pivot=%p", thisfn, ( void * ) provider, ( void * ) pivot );
 
-		/* set a timeout to notify clients at the end of the serie */
-		g_get_current_time( &pivot->private->last_event );
-
-		if( !pivot->private->event_source_id ){
-			pivot->private->event_source_id =
-				g_timeout_add( st_burst_timeout, ( GSourceFunc ) on_item_changed_timeout, pivot );
-		}
+		na_timeout_event( &pivot->private->change_timeout );
 	}
 }
 
 /*
- * this timer is set when we receive the first event of a serie
- * we continue to loop until last event is older that our burst timeout
- *
- * there is no race condition here as we are not multithreaded
- * or .. is there ?
+ * this callback is triggered after having received a first 'item-changed' event,
+ * and having received no more event during a 'st_burst_timeout' period; we can
+ * so suppose that the burst if modification events is terminated
+ * this is up to NAPivot to send now its summarized signal
  */
-static gboolean
+static void
 on_item_changed_timeout( NAPivot *pivot )
 {
 	static const gchar *thisfn = "na_pivot_on_item_changed_timeout";
-	GTimeVal now;
-	gulong diff;
 	GList *ic;
-	gulong timeout_usec = 1000*st_burst_timeout;
 
-	g_return_val_if_fail( NA_IS_PIVOT( pivot ), FALSE );
-
-	g_get_current_time( &now );
-	diff = time_val_diff( &now, &pivot->private->last_event );
-	if( diff < timeout_usec ){
-		return( TRUE );
-	}
+	g_return_if_fail( NA_IS_PIVOT( pivot ));
 
-	/* last individual notification is older that the st_burst_timeout
-	 * we may so suppose that the burst is terminated
-	 * this is up to NAPivot to send now its summarized signal
-	 * last, destroy this timeout
-	 */
 	g_debug( "%s: emitting %s signal", thisfn, PIVOT_SIGNAL_ITEMS_CHANGED );
 	g_signal_emit_by_name(( gpointer ) pivot, PIVOT_SIGNAL_ITEMS_CHANGED );
 
-	/* this has to be deprecated */
+	/* this has to be deprecated.. or not ?? */
 	g_debug( "%s: triggering NAIPivotConsumer interfaces", thisfn );
 	if( pivot->private->automatic_reload ){
 		na_pivot_load_items( pivot );
@@ -680,21 +663,6 @@ on_item_changed_timeout( NAPivot *pivot )
 	for( ic = pivot->private->consumers ; ic ; ic = ic->next ){
 		na_ipivot_consumer_notify_of_items_changed( NA_IPIVOT_CONSUMER( ic->data ));
 	}
-
-	pivot->private->event_source_id = 0;
-
-	return( FALSE );
-}
-
-/*
- * returns the difference in microseconds.
- */
-static gulong
-time_val_diff( const GTimeVal *recent, const GTimeVal *old )
-{
-	gulong microsec = 1000000 * ( recent->tv_sec - old->tv_sec );
-	microsec += recent->tv_usec  - old->tv_usec;
-	return( microsec );
 }
 
 /*
diff --git a/src/core/na-settings.c b/src/core/na-settings.c
index 706ec6f..0574481 100644
--- a/src/core/na-settings.c
+++ b/src/core/na-settings.c
@@ -39,6 +39,7 @@
 
 #include <api/na-boxed.h>
 #include <api/na-core-utils.h>
+#include <api/na-timeout.h>
 
 #include "na-settings.h"
 
@@ -92,11 +93,12 @@ typedef struct {
 /* private instance data
  */
 struct _NASettingsPrivate {
-	gboolean dispose_has_run;
-	KeyFile *mandatory;
-	KeyFile *user;
-	GList   *content;
-	GList   *consumers;
+	gboolean  dispose_has_run;
+	KeyFile  *mandatory;
+	KeyFile  *user;
+	GList    *content;
+	GList    *consumers;
+	NATimeout timeout;
 };
 
 #define GROUP_NACT						"nact"
@@ -167,8 +169,6 @@ static const KeyDef st_def_keys[] = {
 };
 
 static GObjectClass *st_parent_class    = NULL;
-static GTimeVal      st_last_event;
-static guint         st_event_source_id = 0;
 static gint          st_burst_timeout   = 100;		/* burst timeout in msec */
 
 static GType     register_type( void );
@@ -183,7 +183,7 @@ static GList    *content_load_keys( NASettings *settings, GList *content, KeyFil
 static KeyDef   *get_key_def( const gchar *key );
 static KeyFile  *key_file_new( NASettings *settings, const gchar *dir );
 static void      on_keyfile_changed( GFileMonitor *monitor, GFile *file, GFile *other_file, GFileMonitorEvent event_type, NASettings *settings );
-static gboolean  on_keyfile_changed_timeout( NASettings *settings );
+static void      on_keyfile_changed_timeout( NASettings *settings );
 static KeyValue *peek_key_value_from_content( GList *content, const gchar *group, const gchar *key );
 static KeyValue *read_key_value( NASettings *settings, const gchar *group, const gchar *key, gboolean *found, gboolean *mandatory );
 static KeyValue *read_key_value_from_key_file( GKeyFile *key_file, const gchar *group, const gchar *key, const KeyDef *key_def );
@@ -191,7 +191,6 @@ static void      release_consumer( Consumer *consumer );
 static void      release_key_file( KeyFile *key_file );
 static void      release_key_value( KeyValue *value );
 static gboolean  set_key_value( NASettings *settings, const gchar *group, const gchar *key, const gchar *string );
-static gulong    time_val_diff( const GTimeVal *recent, const GTimeVal *old );
 static gboolean  write_user_key_file( NASettings *settings );
 
 GType
@@ -268,6 +267,11 @@ instance_init( GTypeInstance *instance, gpointer klass )
 	self->private->user = NULL;
 	self->private->content = NULL;
 	self->private->consumers = NULL;
+
+	self->private->timeout.timeout = st_burst_timeout;
+	self->private->timeout.handler = ( NATimeoutFunc ) on_keyfile_changed_timeout;
+	self->private->timeout.user_data = self;
+	self->private->timeout.source_id = 0;
 }
 
 static void
@@ -1070,17 +1074,11 @@ on_keyfile_changed( GFileMonitor *monitor,
 
 	if( !settings->private->dispose_has_run ){
 
-		/* set a timeout to notify consumers at the end of the serie */
-		g_get_current_time( &st_last_event );
-
-		if( !st_event_source_id ){
-			st_event_source_id =
-				g_timeout_add( st_burst_timeout, ( GSourceFunc ) on_keyfile_changed_timeout, settings );
-		}
+		na_timeout_event( &settings->private->timeout );
 	}
 }
 
-static gboolean
+static void
 on_keyfile_changed_timeout( NASettings *settings )
 {
 	static const gchar *thisfn = "na_settings_on_keyfile_changed_timeout";
@@ -1091,15 +1089,6 @@ on_keyfile_changed_timeout( NASettings *settings )
 	Consumer *consumer;
 	KeyValue *changed;
 	gchar *group_prefix, *key;
-	GTimeVal now;
-	gulong diff;
-	gulong timeout_usec = 1000*st_burst_timeout;
-
-	g_get_current_time( &now );
-	diff = time_val_diff( &now, &st_last_event );
-	if( diff < timeout_usec ){
-		return( TRUE );
-	}
 
 	/* last individual notification is older that the st_burst_timeout
 	 * we may so suppose that the burst is terminated
@@ -1141,9 +1130,6 @@ on_keyfile_changed_timeout( NASettings *settings )
 
 	g_list_foreach( modifs, ( GFunc ) release_key_value, NULL );
 	g_list_free( modifs );
-
-	st_event_source_id = 0;
-	return( FALSE );
 }
 
 static KeyValue *
@@ -1337,17 +1323,6 @@ set_key_value( NASettings *settings, const gchar *group, const gchar *key, const
 	return( ok );
 }
 
-/*
- * returns the difference in microseconds.
- */
-static gulong
-time_val_diff( const GTimeVal *recent, const GTimeVal *old )
-{
-	gulong microsec = 1000000 * ( recent->tv_sec - old->tv_sec );
-	microsec += recent->tv_usec  - old->tv_usec;
-	return( microsec );
-}
-
 static gboolean
 write_user_key_file( NASettings *settings )
 {
diff --git a/src/io-desktop/nadp-desktop-provider.c b/src/io-desktop/nadp-desktop-provider.c
index 9209d18..9fbb38a 100644
--- a/src/io-desktop/nadp-desktop-provider.c
+++ b/src/io-desktop/nadp-desktop-provider.c
@@ -77,8 +77,7 @@ static guint                    iexporter_get_version( const NAIExporter *export
 static gchar                   *iexporter_get_name( const NAIExporter *exporter );
 static const NAIExporterFormat *iexporter_get_formats( const NAIExporter *exporter );
 
-static gboolean                 on_monitor_timeout( NadpDesktopProvider *provider );
-static gulong                   time_val_diff( const GTimeVal *recent, const GTimeVal *old );
+static void                     on_monitor_timeout( NadpDesktopProvider *provider );
 
 GType
 nadp_desktop_provider_get_type( void )
@@ -174,6 +173,10 @@ instance_init( GTypeInstance *instance, gpointer klass )
 
 	self->private->dispose_has_run = FALSE;
 	self->private->monitors = NULL;
+	self->private->timeout.timeout = st_burst_timeout;
+	self->private->timeout.handler = ( NATimeoutFunc ) on_monitor_timeout;
+	self->private->timeout.user_data = self;
+	self->private->timeout.source_id = 0;
 }
 
 static void
@@ -362,12 +365,7 @@ nadp_desktop_provider_on_monitor_event( NadpDesktopProvider *provider )
 
 	if( !provider->private->dispose_has_run ){
 
-		g_get_current_time( &provider->private->last_event );
-
-		if( !provider->private->event_source_id ){
-			provider->private->event_source_id =
-				g_timeout_add( st_burst_timeout, ( GSourceFunc ) on_monitor_timeout, provider );
-		}
+		na_timeout_event( &provider->private->timeout );
 	}
 }
 
@@ -390,19 +388,10 @@ nadp_desktop_provider_release_monitors( NadpDesktopProvider *provider )
 	}
 }
 
-static gboolean
+static void
 on_monitor_timeout( NadpDesktopProvider *provider )
 {
 	static const gchar *thisfn = "nadp_desktop_provider_on_monitor_timeout";
-	GTimeVal now;
-	gulong diff;
-	gulong timeout_usec = 1000*st_burst_timeout;
-
-	g_get_current_time( &now );
-	diff = time_val_diff( &now, &provider->private->last_event );
-	if( diff < timeout_usec ){
-		return( TRUE );
-	}
 
 	/* last individual notification is older that the st_burst_timeout
 	 * so triggers the NAIIOProvider interface and destroys this timeout
@@ -411,17 +400,4 @@ on_monitor_timeout( NadpDesktopProvider *provider )
 			thisfn, ( void * ) provider, G_OBJECT_TYPE_NAME( provider ));
 
 	na_iio_provider_item_changed( NA_IIO_PROVIDER( provider ));
-	provider->private->event_source_id = 0;
-	return( FALSE );
-}
-
-/*
- * returns the difference in microseconds.
- */
-static gulong
-time_val_diff( const GTimeVal *recent, const GTimeVal *old )
-{
-	gulong microsec = 1000000 * ( recent->tv_sec - old->tv_sec );
-	microsec += recent->tv_usec  - old->tv_usec;
-	return( microsec );
 }
diff --git a/src/io-desktop/nadp-desktop-provider.h b/src/io-desktop/nadp-desktop-provider.h
index 2ac5e31..bb658e1 100644
--- a/src/io-desktop/nadp-desktop-provider.h
+++ b/src/io-desktop/nadp-desktop-provider.h
@@ -42,6 +42,7 @@
  */
 
 #include <api/na-object-item.h>
+#include <api/na-timeout.h>
 
 #include "nadp-desktop-file.h"
 
@@ -58,10 +59,9 @@ G_BEGIN_DECLS
  */
 typedef struct _NadpDesktopProviderPrivate {
 	/*< private >*/
-	gboolean dispose_has_run;
-	GList   *monitors;
-	GTimeVal last_event;
-	guint    event_source_id;
+	gboolean  dispose_has_run;
+	GList    *monitors;
+	NATimeout timeout;
 }
 	NadpDesktopProviderPrivate;
 
diff --git a/src/plugin-menu/nautilus-actions.c b/src/plugin-menu/nautilus-actions.c
index 54e6e71..53697db 100644
--- a/src/plugin-menu/nautilus-actions.c
+++ b/src/plugin-menu/nautilus-actions.c
@@ -42,6 +42,7 @@
 
 #include <api/na-core-utils.h>
 #include <api/na-object-api.h>
+#include <api/na-timeout.h>
 
 #include <core/na-pivot.h>
 #include <core/na-iabout.h>
@@ -59,15 +60,14 @@ struct _NautilusActionsClassPrivate {
 /* private instance data
  */
 struct _NautilusActionsPrivate {
-	gboolean dispose_has_run;
-	NAPivot *pivot;
-	gulong   items_changed_handler;
+	gboolean  dispose_has_run;
+	NAPivot  *pivot;
+	gulong    items_changed_handler;
+	NATimeout timeout;
 };
 
 static GObjectClass *st_parent_class    = NULL;
 static GType         st_actions_type    = 0;
-static GTimeVal      st_last_event;
-static guint         st_event_source_id = 0;
 static gint          st_burst_timeout   = 100;		/* burst timeout in msec */
 
 static void              class_init( NautilusActionsClass *klass );
@@ -104,9 +104,7 @@ static void              execute_about( NautilusMenuItem *item, NautilusActions
 
 static void              on_pivot_items_changed_handler( NAPivot *pivot, NautilusActions *plugin );
 static void              on_runtime_preference_changed( const gchar *group, const gchar *key, gconstpointer newvalue, gboolean mandatory, NautilusActions *plugin );
-static void              record_change_event( NautilusActions *plugin );
-static gboolean          on_change_event_timeout( NautilusActions *plugin );
-static gulong            time_val_diff( const GTimeVal *recent, const GTimeVal *old );
+static void              on_change_event_timeout( NautilusActions *plugin );
 
 GType
 nautilus_actions_get_type( void )
@@ -188,6 +186,10 @@ instance_init( GTypeInstance *instance, gpointer klass )
 	self->private = g_new0( NautilusActionsPrivate, 1 );
 
 	self->private->dispose_has_run = FALSE;
+	self->private->timeout.timeout = st_burst_timeout;
+	self->private->timeout.handler = ( NATimeoutFunc ) on_change_event_timeout;
+	self->private->timeout.user_data = self;
+	self->private->timeout.source_id = 0;
 }
 
 /*
@@ -1034,7 +1036,8 @@ on_pivot_items_changed_handler( NAPivot *pivot, NautilusActions *plugin )
 	g_return_if_fail( NAUTILUS_IS_ACTIONS( plugin ));
 
 	if( !plugin->private->dispose_has_run ){
-		record_change_event( plugin );
+
+		na_timeout_event( &plugin->private->timeout );
 	}
 }
 
@@ -1044,24 +1047,8 @@ on_runtime_preference_changed( const gchar *group, const gchar *key, gconstpoint
 	g_return_if_fail( NAUTILUS_IS_ACTIONS( plugin ));
 
 	if( !plugin->private->dispose_has_run ){
-		record_change_event( plugin );
-	}
-}
 
-/* each signal handler or settings callback calls this function
- * so that we are sure that all change events are taken into account
- * in our timeout management
- *
- * create a new event source if it does not exists yet
- */
-static void
-record_change_event( NautilusActions *plugin )
-{
-	g_get_current_time( &st_last_event );
-
-	if( !st_event_source_id ){
-		st_event_source_id =
-			g_timeout_add( st_burst_timeout, ( GSourceFunc ) on_change_event_timeout, plugin );
+		na_timeout_event( &plugin->private->timeout );
 	}
 }
 
@@ -1073,41 +1060,15 @@ record_change_event( NautilusActions *plugin )
  *
  * we so reload the items, signal the file manager, and reset the event source.
  */
-static gboolean
+static void
 on_change_event_timeout( NautilusActions *plugin )
 {
 	static const gchar *thisfn = "nautilus_actions_on_change_event_timeout";
-	GTimeVal now;
-	gulong diff;
-	gulong timeout_usec = 1000*st_burst_timeout;
-
-	g_get_current_time( &now );
-	diff = time_val_diff( &now, &st_last_event );
-	if( diff < timeout_usec ){
-		/* continue periodic calls */
-		return( TRUE );
-	}
 
 	/* do what we have to and close the event source
 	 */
 	g_debug( "%s: timeout expired", thisfn );
 
 	na_pivot_load_items( plugin->private->pivot );
-
 	nautilus_menu_provider_emit_items_updated_signal( NAUTILUS_MENU_PROVIDER( plugin ));
-
-	st_event_source_id = 0;
-
-	return( FALSE );
-}
-
-/*
- * returns the difference in microseconds.
- */
-static gulong
-time_val_diff( const GTimeVal *recent, const GTimeVal *old )
-{
-	gulong microsec = 1000000 * ( recent->tv_sec - old->tv_sec );
-	microsec += recent->tv_usec  - old->tv_usec;
-	return( microsec );
 }



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