Commit 434eab8a authored by Pierre Wieser's avatar Pierre Wieser

Implement autosave feature

parent 31e921fc
2010-08-07 Pierre Wieser <pwieser@trychlos.org>
Implement auto-save feature.
* src/core/na-ipivot-consumer.c:
* src/core/na-ipivot-consumer.h (on_autosave_changed): New interface.
* src/core/na-iprefs.c:
* src/core/na-iprefs.h (na_iprefs_read_uint): New function.
* src/nact/nact-iprefs.c:
* src/nact/nact-iprefs.h (nact_iprefs_read_uint): Removed function.
* src/core/na-pivot.c (on_preferences_change):
Trigger NAIPivotConsumer interface when autosave is changed.
* src/nact/nact-main-menubar-file.c:
* src/nact/nact-main-menubar-file.h
(nact_main_menubar_file_set_autosave): New function.
* src/nact/nact-main-window.c (ipivot_consumer_on_autosave_changed):
Implement the new on_autosave_changed() NAIPivotConsumer interface.
* src/nact/nact-preferences-editor.c (on_base_runtime_init_dialog):
Replace nact_iprefs_read_uint() with na_iprefs_read_uint().
Setup auto-save in preferences.
* data/nautilus-actions-prefs.schemas.in:
......
......@@ -160,53 +160,23 @@ na_ipivot_consumer_delay_notify( NAIPivotConsumer *instance )
}
/**
* na_ipivot_consumer_notify_of_items_changed:
* na_ipivot_consumer_notify_of_autosave_changed:
* @instance: the #NAIPivotConsumer instance to be notified of the end
* of the modifications.
* @enabled: whether auto-save feature is now enabled.
* @period: new auto-save period in minutes.
*
* Notifies the consumers that the actions have been modified on one of
* the underlying storage subsystems.
*/
void na_ipivot_consumer_notify_of_items_changed( NAIPivotConsumer *instance )
{
static const gchar *thisfn = "na_ipivot_consumer_notify_of_items_changed";
g_return_if_fail( NA_IS_IPIVOT_CONSUMER( instance ));
if( st_initialized && !st_finalized ){
g_debug( "%s: instance=%p", thisfn, ( void * ) instance );
if( is_notify_allowed( instance )){
if( NA_IPIVOT_CONSUMER_GET_INTERFACE( instance )->on_items_changed ){
NA_IPIVOT_CONSUMER_GET_INTERFACE( instance )->on_items_changed( instance, NULL );
}
}
}
}
/**
* na_ipivot_consumer_notify_of_mandatory_prefs_changed:
* @instance: the #NAIPivotConsumer instance to be notified of the modifications.
*
* Notifies the consumers that a mandatory 'locked' preference has been changed.
* Notifies the consumers that the ato-save preferences have been changed.
*/
void na_ipivot_consumer_notify_of_mandatory_prefs_changed( NAIPivotConsumer *instance )
void
na_ipivot_consumer_notify_of_autosave_changed( NAIPivotConsumer *instance, gboolean enabled, guint period )
{
static const gchar *thisfn = "na_ipivot_consumer_notify_of_mandatory_prefs_changed";
g_return_if_fail( NA_IS_IPIVOT_CONSUMER( instance ));
if( st_initialized && !st_finalized ){
g_debug( "%s: instance=%p", thisfn, ( void * ) instance );
if( is_notify_allowed( instance )){
if( NA_IPIVOT_CONSUMER_GET_INTERFACE( instance )->on_mandatory_prefs_changed ){
NA_IPIVOT_CONSUMER_GET_INTERFACE( instance )->on_mandatory_prefs_changed( instance );
}
if( NA_IPIVOT_CONSUMER_GET_INTERFACE( instance )->on_autosave_changed ){
NA_IPIVOT_CONSUMER_GET_INTERFACE( instance )->on_autosave_changed( instance, enabled, period );
}
}
}
......@@ -284,6 +254,58 @@ na_ipivot_consumer_notify_of_display_order_changed( NAIPivotConsumer *instance,
}
}
/**
* na_ipivot_consumer_notify_of_items_changed:
* @instance: the #NAIPivotConsumer instance to be notified of the end
* of the modifications.
*
* Notifies the consumers that the actions have been modified on one of
* the underlying storage subsystems.
*/
void na_ipivot_consumer_notify_of_items_changed( NAIPivotConsumer *instance )
{
static const gchar *thisfn = "na_ipivot_consumer_notify_of_items_changed";
g_return_if_fail( NA_IS_IPIVOT_CONSUMER( instance ));
if( st_initialized && !st_finalized ){
g_debug( "%s: instance=%p", thisfn, ( void * ) instance );
if( is_notify_allowed( instance )){
if( NA_IPIVOT_CONSUMER_GET_INTERFACE( instance )->on_items_changed ){
NA_IPIVOT_CONSUMER_GET_INTERFACE( instance )->on_items_changed( instance, NULL );
}
}
}
}
/**
* na_ipivot_consumer_notify_of_mandatory_prefs_changed:
* @instance: the #NAIPivotConsumer instance to be notified of the modifications.
*
* Notifies the consumers that a mandatory 'locked' preference has been changed.
*/
void na_ipivot_consumer_notify_of_mandatory_prefs_changed( NAIPivotConsumer *instance )
{
static const gchar *thisfn = "na_ipivot_consumer_notify_of_mandatory_prefs_changed";
g_return_if_fail( NA_IS_IPIVOT_CONSUMER( instance ));
if( st_initialized && !st_finalized ){
g_debug( "%s: instance=%p", thisfn, ( void * ) instance );
if( is_notify_allowed( instance )){
if( NA_IPIVOT_CONSUMER_GET_INTERFACE( instance )->on_mandatory_prefs_changed ){
NA_IPIVOT_CONSUMER_GET_INTERFACE( instance )->on_mandatory_prefs_changed( instance );
}
}
}
}
static gboolean
is_notify_allowed( const NAIPivotConsumer *instance )
{
......
......@@ -59,17 +59,17 @@ typedef struct {
NAIPivotConsumerInterfacePrivate *private;
/**
* on_items_changed:
* on_autosave_changed:
* @instance: the #NAIPivotConsumer instance which implements this
* interface.
* user_data: user data set when emitting the message. Currently,
* not used.
* @enabled: whether auto-save feature is now enabled.
* @period: new auto-save period in minutes.
*
* This function is triggered once when #NAPivot detects the end of
* a bunch of modifications. At this time, the embedded list of
* #NAAction has been updated to be up to date.
* This function is triggered once when #NAPivot detects that the
* auto-save preferences have been changed. Parameters are the new
* parameters of the configuration.
*/
void ( *on_items_changed ) ( NAIPivotConsumer *instance, gpointer user_data );
void ( *on_autosave_changed ) ( NAIPivotConsumer *instance, gboolean enabled, guint period );
/**
* on_create_root_menu_changed:
......@@ -104,6 +104,19 @@ typedef struct {
*/
void ( *on_display_order_changed ) ( NAIPivotConsumer *instance, gint order_mode );
/**
* on_items_changed:
* @instance: the #NAIPivotConsumer instance which implements this
* interface.
* user_data: user data set when emitting the message. Currently,
* not used.
*
* This function is triggered once when #NAPivot detects the end of
* a bunch of modifications. At this time, the embedded list of
* #NAAction has been updated to be up to date.
*/
void ( *on_items_changed ) ( NAIPivotConsumer *instance, gpointer user_data );
/**
* on_mandatory_prefs_changed:
* @instance: the #NAIPivotConsumer instance which implements this interface.
......@@ -118,11 +131,12 @@ GType na_ipivot_consumer_get_type( void );
void na_ipivot_consumer_delay_notify( NAIPivotConsumer *instance );
void na_ipivot_consumer_notify_of_items_changed ( NAIPivotConsumer *instance );
void na_ipivot_consumer_notify_of_mandatory_prefs_changed ( NAIPivotConsumer *instance );
void na_ipivot_consumer_notify_of_autosave_changed ( NAIPivotConsumer *instance, gboolean enabled, guint period );
void na_ipivot_consumer_notify_of_create_root_menu_changed( NAIPivotConsumer *instance, gboolean enabled );
void na_ipivot_consumer_notify_of_display_about_changed ( NAIPivotConsumer *instance, gboolean enabled );
void na_ipivot_consumer_notify_of_display_order_changed ( NAIPivotConsumer *instance, gint order_mode );
void na_ipivot_consumer_notify_of_items_changed ( NAIPivotConsumer *instance );
void na_ipivot_consumer_notify_of_mandatory_prefs_changed ( NAIPivotConsumer *instance );
G_END_DECLS
......
......@@ -394,6 +394,34 @@ na_iprefs_read_string_list( const NAIPrefs *instance, const gchar *name, const g
return( list );
}
/**
* na_iprefs_read_uint:
* @instance: this #NAIPrefs interface instance.
* @name: the preference entry.
* @default_value: the value to be returned if the key is not found.
*
* Returns: the uint value associated with the given key.
*/
guint
na_iprefs_read_uint( const NAIPrefs *instance, const gchar *name, guint default_value )
{
guint value;
gchar *path;
g_return_val_if_fail( NA_IS_IPREFS( instance ), 0 );
value = 0;
if( st_initialized && !st_finalized ){
path = gconf_concat_dir_and_key( IPREFS_GCONF_PREFS_PATH, name );
value = na_gconf_utils_read_int( na_iprefs_get_gconf_client( instance ), path, TRUE, default_value );
g_free( path );
}
return( value );
}
/*
* na_iprefs_write_string:
* @instance: this #NAIPrefs interface instance.
......
......@@ -106,6 +106,9 @@ typedef struct {
#define IPREFS_IMPORT_KEEP_CHOICE "import-keep-choice"
#define IPREFS_IMPORT_ASK_LAST_MODE "import-ask-user-last-mode"
#define IPREFS_AUTOSAVE_ON "auto-save-on"
#define IPREFS_AUTOSAVE_PERIOD "auto-save-period"
/* alphabetical order values
*/
enum {
......@@ -127,6 +130,7 @@ GConfClient *na_iprefs_get_gconf_client ( const NAIPrefs *instance );
gboolean na_iprefs_read_bool ( const NAIPrefs *instance, const gchar *key, gboolean default_value );
gchar *na_iprefs_read_string ( const NAIPrefs *instance, const gchar *key, const gchar *default_value );
GSList *na_iprefs_read_string_list ( const NAIPrefs *instance, const gchar *key, const gchar *default_value );
guint na_iprefs_read_uint ( const NAIPrefs *instance, const gchar *key, guint defaut_value );
void na_iprefs_write_string_list( const NAIPrefs *instance, const gchar *key, GSList *value );
......
......@@ -121,6 +121,7 @@ static void on_preferences_change( GConfClient *client, guint cnxn_id,
static void display_order_changed( NAPivot *pivot );
static void create_root_menu_changed( NAPivot *pivot );
static void display_about_changed( NAPivot *pivot );
static void autosave_changed( NAPivot *pivot );
GType
na_pivot_get_type( void )
......@@ -985,6 +986,10 @@ on_preferences_change( GConfClient *client, guint cnxn_id, GConfEntry *entry, NA
display_order_changed( pivot );
}
if( !g_ascii_strcasecmp( key_entry, IPREFS_AUTOSAVE_ON ) || !g_ascii_strcasecmp( key_entry, IPREFS_AUTOSAVE_PERIOD )){
autosave_changed( pivot );
}
g_free( key_entry );
}
......@@ -1049,3 +1054,26 @@ display_about_changed( NAPivot *pivot )
}
}
}
static void
autosave_changed( NAPivot *pivot )
{
static const gchar *thisfn = "na_pivot_autosave_changed";
GList *ic;
gboolean autosave_on;
guint autosave_period;
g_return_if_fail( NA_IS_PIVOT( pivot ));
if( !pivot->private->dispose_has_run ){
g_debug( "%s: pivot=%p", thisfn, ( void * ) pivot );
autosave_on = na_iprefs_read_bool( NA_IPREFS( pivot ), IPREFS_AUTOSAVE_ON, FALSE );
autosave_period = na_iprefs_read_uint( NA_IPREFS( pivot ), IPREFS_AUTOSAVE_PERIOD, 5 );
for( ic = pivot->private->consumers ; ic ; ic = ic->next ){
na_ipivot_consumer_notify_of_autosave_changed( NA_IPIVOT_CONSUMER( ic->data ), autosave_on, autosave_period );
}
}
}
......@@ -231,35 +231,6 @@ nact_iprefs_migrate_key( const BaseWindow *window, const gchar *old_key, const g
}
}
/**
* nact_iprefs_read_uint:
* @window: this #BaseWindow-derived window.
* @name: the preference entry.
* @default_value: the value to be returned if the key is not found.
*
* Returns: the uint value associated with the given key.
*/
guint
nact_iprefs_read_uint( const BaseWindow *window, const gchar *name, guint default_value )
{
guint value;
gchar *path;
g_return_val_if_fail( BASE_IS_WINDOW( window ), 0 );
g_return_val_if_fail( NACT_IS_IPREFS( window ), 0 );
value = 0;
if( st_initialized && !st_finalized ){
path = gconf_concat_dir_and_key( IPREFS_GCONF_PREFS_PATH, name );
value = na_gconf_utils_read_int( NACT_IPREFS_GET_INTERFACE( window )->private->client, path, TRUE, default_value );
g_free( path );
}
return( value );
}
/**
* nact_iprefs_write_bool:
* @window: this #BaseWindow-derived window.
......
......@@ -65,8 +65,6 @@ typedef struct {
#define IPREFS_IMPORT_ITEMS_FOLDER_URI "import-folder-uri"
#define IPREFS_ASSIST_ESC_QUIT "assistant-esc-quit"
#define IPREFS_ASSIST_ESC_CONFIRM "assistant-esc-confirm"
#define IPREFS_AUTOSAVE_ON "auto-save-on"
#define IPREFS_AUTOSAVE_PERIOD "auto-save-period"
/* these are special export formats
*/
......@@ -82,7 +80,6 @@ void nact_iprefs_set_export_format( const BaseWindow *window, const gchar *pre
void nact_iprefs_migrate_key ( const BaseWindow *window, const gchar *old_key, const gchar *new_key );
guint nact_iprefs_read_uint ( const BaseWindow *window, const gchar *key, guint defaut_value );
void nact_iprefs_write_bool ( const BaseWindow *window, const gchar *key, gboolean value );
void nact_iprefs_write_uint ( const BaseWindow *window, const gchar *key, guint value );
void nact_iprefs_write_string ( const BaseWindow *window, const gchar *name, const gchar *value );
......
......@@ -38,10 +38,15 @@
#include "nact-application.h"
#include "nact-iactions-list.h"
#include "nact-main-statusbar.h"
#include "nact-main-tab.h"
#include "nact-main-menubar-file.h"
static void save_item( NactMainWindow *window, NAUpdater *updater, NAObjectItem *item );
static guint st_event_autosave = 0;
static void save_item( NactMainWindow *window, NAUpdater *updater, NAObjectItem *item );
static gboolean autosave_callback( NactMainWindow *window );
static void autosave_destroyed( NactMainWindow *window );
/**
* nact_main_menubar_file_on_update_sensitivities:
......@@ -397,3 +402,52 @@ nact_main_menubar_file_on_quit( GtkAction *gtk_action, NactMainWindow *window )
g_object_unref( window );
}
}
/**
* nact_main_menubar_file_set_autosave:
* @window: the #NactMainWindow main window.
* @enabled: whether the autosave feature is enabled.
* @period: autosave periodicity in minutes.
*
* Setup or disabled the autosave feature.
*/
void
nact_main_menubar_file_set_autosave( NactMainWindow *window, gboolean enabled, guint period )
{
static const gchar *thisfn = "nact_main_menubar_file_set_autosave";
if( st_event_autosave ){
if( !g_source_remove( st_event_autosave )){
g_warning( "%s: unable to remove autosave event source", thisfn );
}
st_event_autosave = 0;
}
if( enabled ){
st_event_autosave = g_timeout_add_seconds_full(
G_PRIORITY_DEFAULT,
period * 60,
( GSourceFunc ) autosave_callback,
window,
( GDestroyNotify ) autosave_destroyed );
}
}
static gboolean
autosave_callback( NactMainWindow *window )
{
const gchar *context = "autosave-context";
g_debug( "nact_main_menubar_file_autosave_callback" );
nact_main_statusbar_display_status( window, context, _( "Automatically saving pending modifications..." ));
nact_main_menubar_file_save_items( window );
nact_main_statusbar_hide_status( window, context );
return( TRUE );
}
static void
autosave_destroyed( NactMainWindow *window )
{
g_debug( "nact_main_menubar_file_autosave_destroyed" );
}
......@@ -51,7 +51,9 @@ void nact_main_menubar_file_on_new_profile( GtkAction *action, NactMainWindow *w
void nact_main_menubar_file_on_save ( GtkAction *action, NactMainWindow *window );
void nact_main_menubar_file_on_quit ( GtkAction *action, NactMainWindow *window );
void nact_main_menubar_file_save_items( NactMainWindow *window );
void nact_main_menubar_file_save_items ( NactMainWindow *window );
void nact_main_menubar_file_set_autosave( NactMainWindow *window, gboolean enabled, guint period );
G_END_DECLS
......
......@@ -59,6 +59,7 @@
#include "nact-iproperties-tab.h"
#include "nact-main-tab.h"
#include "nact-main-menubar.h"
#include "nact-main-menubar-file.h"
#include "nact-main-statusbar.h"
#include "nact-marshal.h"
#include "nact-main-window.h"
......@@ -182,6 +183,8 @@ static void on_tab_updatable_item_updated( NactMainWindow *window, gpointer
static gboolean confirm_for_giveup_from_menu( NactMainWindow *window );
static gboolean confirm_for_giveup_from_pivot( NactMainWindow *window );
static void install_autosave( NactMainWindow *window );
static void ipivot_consumer_on_autosave_changed( NAIPivotConsumer *instance, gboolean enabled, guint period );
static void ipivot_consumer_on_items_changed( NAIPivotConsumer *instance, gpointer user_data );
static void ipivot_consumer_on_display_order_changed( NAIPivotConsumer *instance, gint order_mode );
static void ipivot_consumer_on_mandatory_prefs_changed( NAIPivotConsumer *instance );
......@@ -605,10 +608,11 @@ ipivot_consumer_iface_init( NAIPivotConsumerInterface *iface )
g_debug( "%s: iface=%p", thisfn, ( void * ) iface );
iface->on_items_changed = ipivot_consumer_on_items_changed;
iface->on_autosave_changed = ipivot_consumer_on_autosave_changed;
iface->on_create_root_menu_changed = NULL;
iface->on_display_about_changed = NULL;
iface->on_display_order_changed = ipivot_consumer_on_display_order_changed;
iface->on_items_changed = ipivot_consumer_on_items_changed;
iface->on_mandatory_prefs_changed = ipivot_consumer_on_mandatory_prefs_changed;
}
......@@ -1201,6 +1205,8 @@ on_base_all_widgets_showed( NactMainWindow *window, gpointer user_data )
nact_iactions_list_all_widgets_showed( NACT_IACTIONS_LIST( window ));
nact_sort_buttons_all_widgets_showed( window );
install_autosave( window );
}
}
......@@ -1468,6 +1474,22 @@ confirm_for_giveup_from_pivot( NactMainWindow *window )
return( reload_ok );
}
static void
install_autosave( NactMainWindow *window )
{
gboolean autosave_on;
guint autosave_period;
NactApplication *application;
NAUpdater *updater;
application = NACT_APPLICATION( base_window_get_application( BASE_WINDOW( window )));
updater = nact_application_get_updater( application );
autosave_on = na_iprefs_read_bool( NA_IPREFS( updater ), IPREFS_AUTOSAVE_ON, FALSE );
autosave_period = na_iprefs_read_uint( NA_IPREFS( updater ), IPREFS_AUTOSAVE_PERIOD, 5 );
nact_main_menubar_file_set_autosave( window, autosave_on, autosave_period );
}
/*
* called by NAPivot because this window implements the IIOConsumer
* interface, i.e. it wish to be advertised when the list of actions
......@@ -1498,30 +1520,20 @@ ipivot_consumer_on_items_changed( NAIPivotConsumer *instance, gpointer user_data
}
}
/*
* called by NAPivot via NAIPivotConsumer whenever the
* autosave preferences have been modified.
*/
static void
reload( NactMainWindow *window )
ipivot_consumer_on_autosave_changed( NAIPivotConsumer *instance, gboolean enabled, guint period )
{
static const gchar *thisfn = "nact_main_window_reload";
NactApplication *application;
NAUpdater *updater;
g_debug( "%s: window=%p", thisfn, ( void * ) window );
g_return_if_fail( NACT_IS_MAIN_WINDOW( window ));
if( !window->private->dispose_has_run ){
window->private->selected_item = NULL;
window->private->selected_profile = NULL;
static const gchar *thisfn = "nact_main_window_ipivot_consumer_on_autosave_changed";
na_object_unref_items( window->private->deleted );
window->private->deleted = NULL;
g_return_if_fail( NACT_IS_MAIN_WINDOW( instance ));
g_debug( "%s: instance=%p, enabled=%s, period=%d",
thisfn, ( void * ) instance, enabled ? "True":"False", period );
application = NACT_APPLICATION( base_window_get_application( BASE_WINDOW( window )));
updater = nact_application_get_updater( application );
na_pivot_load_items( NA_PIVOT( updater ));
nact_iactions_list_fill( NACT_IACTIONS_LIST( window ), na_pivot_get_items( NA_PIVOT( updater )));
nact_iactions_list_bis_select_first_row( NACT_IACTIONS_LIST( window ));
}
nact_main_menubar_file_set_autosave( NACT_MAIN_WINDOW( instance ), enabled, period );
}
/*
......@@ -1560,6 +1572,32 @@ ipivot_consumer_on_mandatory_prefs_changed( NAIPivotConsumer *instance )
nact_sort_buttons_level_zero_writability_change( NACT_MAIN_WINDOW( instance ));
}
static void
reload( NactMainWindow *window )
{
static const gchar *thisfn = "nact_main_window_reload";
NactApplication *application;
NAUpdater *updater;
g_debug( "%s: window=%p", thisfn, ( void * ) window );
g_return_if_fail( NACT_IS_MAIN_WINDOW( window ));
if( !window->private->dispose_has_run ){
window->private->selected_item = NULL;
window->private->selected_profile = NULL;
na_object_unref_items( window->private->deleted );
window->private->deleted = NULL;
application = NACT_APPLICATION( base_window_get_application( BASE_WINDOW( window )));
updater = nact_application_get_updater( application );
na_pivot_load_items( NA_PIVOT( updater ));
nact_iactions_list_fill( NACT_IACTIONS_LIST( window ), na_pivot_get_items( NA_PIVOT( updater )));
nact_iactions_list_bis_select_first_row( NACT_IACTIONS_LIST( window ));
}
}
static gchar *
iabout_get_application_name( NAIAbout *instance )
{
......
......@@ -392,7 +392,7 @@ on_base_runtime_init_dialog( NactPreferencesEditor *editor, gpointer user_data )
"toggled",
G_CALLBACK( on_auto_save_toggled ));
auto_save_period = nact_iprefs_read_uint( BASE_WINDOW( editor ), IPREFS_AUTOSAVE_PERIOD, 5 );
auto_save_period = na_iprefs_read_uint( NA_IPREFS( updater ), IPREFS_AUTOSAVE_PERIOD, 5 );
button = base_window_get_widget( BASE_WINDOW( editor ), "AutoSavePeriodicitySpinButton" );
adjustment = gtk_spin_button_get_adjustment( GTK_SPIN_BUTTON( button ));
gtk_adjustment_configure( adjustment, auto_save_period, 1, 999, 1, 10, 0 );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment