Skip to content

Commit

Permalink
Add ability to purge history after timeout.
Browse files Browse the repository at this point in the history
Requested in #47.
  • Loading branch information
facetoe committed Jun 13, 2017
1 parent 540f01a commit ac1fdac
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,9 @@ int main(int argc, char **argv) {
/* Init ClipIt */
clipit_init();

/* Create the history_timeout_timer if applicable */
init_history_timeout_timer();

/* Run GTK+ loop */
gtk_main();

Expand Down
78 changes: 68 additions & 10 deletions src/preferences.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ GtkTreeSelection* actions_selection;
GtkListStore* exclude_list;
GtkTreeSelection* exclude_selection;

/* Reference to the current history_timeout_timer */
guint history_timeout_timer;

/* Apply the new preferences */
static void apply_preferences()
{
Expand Down Expand Up @@ -245,8 +248,8 @@ void read_preferences()
prefs.use_rmb_menu = g_key_file_get_boolean(rc_key, "rc", "use_rmb_menu", NULL);
prefs.save_history = g_key_file_get_boolean(rc_key, "rc", "save_history", NULL);
prefs.history_limit = g_key_file_get_integer(rc_key, "rc", "history_limit", NULL);
prefs.history_timeout_seconds = g_key_file_get_integer(rc_key, "rc", "history_timeout_seconds", NULL);
prefs.history_timeout = g_key_file_get_boolean(rc_key, "rc", "history_timeout", NULL);
prefs.history_timeout_seconds = g_key_file_get_integer(rc_key, "rc", "history_timeout_seconds", NULL);
prefs.items_menu = g_key_file_get_integer(rc_key, "rc", "items_menu", NULL);
prefs.statics_show = g_key_file_get_boolean(rc_key, "rc", "statics_show", NULL);
prefs.statics_items = g_key_file_get_integer(rc_key, "rc", "statics_items", NULL);
Expand All @@ -266,10 +269,10 @@ void read_preferences()
/* Check for errors and set default values if any */
if ((!prefs.history_limit) || (prefs.history_limit > 1000) || (prefs.history_limit < 0))
prefs.history_limit = DEF_HISTORY_LIMIT;
if ((!prefs.history_timeout_seconds) || (prefs.history_timeout_seconds > 60*60) || (prefs.history_timeout_seconds < 0))
prefs.history_limit = DEF_HISTORY_TIMEOUT_SECONDS;
if (!prefs.history_timeout)
prefs.history_timeout = DEF_HISTORY_TIMEOUT;
if (!prefs.history_timeout_seconds)
prefs.history_timeout_seconds = DEF_HISTORY_TIMEOUT_SECONDS;
if ((!prefs.items_menu) || (prefs.items_menu > 1000) || (prefs.items_menu < 0))
prefs.items_menu = DEF_ITEMS_MENU;
if ((!prefs.item_length) || (prefs.item_length > 75) || (prefs.item_length < 0))
Expand Down Expand Up @@ -410,13 +413,71 @@ static void check_toggled(GtkToggleButton *togglebutton, gpointer user_data)
} else {
gtk_widget_set_sensitive((GtkWidget*)statics_items_spin, FALSE);
}
/* If history_timeout is disabled, prevent interaction with the history_timeout_spinner */
if (gtk_toggle_button_get_active((GtkToggleButton*)history_timeout_check)) {
gtk_widget_set_sensitive((GtkWidget*)history_timeout_spin, TRUE);
} else {
gtk_widget_set_sensitive((GtkWidget*)history_timeout_spin, FALSE);
}
}


static void start_purge_timer(gint timeout_seconds);
static void stop_purge_timer();

/* Purge history if history_timeout is enabled. This function is called every prefs.history_timeout_seconds */
static gboolean purge_history() {
if (prefs.history_timeout) {
g_list_free(history);
history = NULL;
save_history();
clear_main_data();
return TRUE;
}
stop_purge_timer();
return FALSE;
}

/* Start the history_purge_timer. If another timer exists, it will be replaced */
void start_purge_timer(gint timeout_seconds) {
if (history_timeout_timer)
g_source_remove(history_timeout_timer);
history_timeout_timer = g_timeout_add_seconds(timeout_seconds, purge_history, NULL);
}

/* Stop the history_purge_timer. If no timer exists, nothing is done */
static void stop_purge_timer() {
if (history_timeout_timer) {
g_source_remove(history_timeout_timer);
history_timeout_timer = 0;
}
}

/* When the history_timeout_spin's value changes, create a new history_timeout_timer with the updated value */
static void handle_history_spinner(GtkSpinButton *spinbutton, gpointer user_data) {
gint spinner_value = gtk_spin_button_get_value_as_int((GtkSpinButton*)history_timeout_spin);
if (gtk_toggle_button_get_active((GtkToggleButton*)history_timeout_check))
start_purge_timer(spinner_value);
}

/* When the history_timeout_check button is toggled, either start or stop the history_timeout_timer */
static void handle_history_purge_toggle(GtkWidget *togglebutton, gpointer user_data) {
if (gtk_toggle_button_get_active((GtkToggleButton*)history_timeout_check)) {
gint spinner_value = gtk_spin_button_get_value_as_int((GtkSpinButton*)history_timeout_spin);
save_preferences();
start_purge_timer(spinner_value);
} else {
stop_purge_timer();
}
}

/* Initialize the history_timeout_timer on startup */
void init_history_timeout_timer() {
if (prefs.history_timeout) {
start_purge_timer(prefs.history_timeout_seconds);
}
}

/* Called when Add... button is clicked */
static void add_action(GtkButton *button, gpointer user_data)
{
Expand Down Expand Up @@ -766,7 +827,6 @@ void show_preferences(gint tab) {
gtk_box_pack_start((GtkBox*)hbox, history_spin, FALSE, FALSE, 0);
hbox = gtk_hbox_new(FALSE, 4);
gtk_box_pack_start((GtkBox*)vbox, hbox, FALSE, FALSE, 0);

label = gtk_label_new(_("Items in menu:"));
gtk_misc_set_alignment((GtkMisc*)label, 0.0, 0.50);
gtk_box_pack_start((GtkBox*)hbox, label, FALSE, FALSE, 0);
Expand All @@ -779,7 +839,6 @@ void show_preferences(gint tab) {
gtk_box_pack_start((GtkBox*)vbox, statics_show_check, FALSE, FALSE, 0);
hbox = gtk_hbox_new(FALSE, 4);
gtk_box_pack_start((GtkBox*)vbox, hbox, FALSE, FALSE, 0);

label = gtk_label_new(_("Static items in menu:"));
gtk_misc_set_alignment((GtkMisc*)label, 0.0, 0.50);
gtk_box_pack_start((GtkBox*)hbox, label, FALSE, FALSE, 0);
Expand All @@ -788,19 +847,18 @@ void show_preferences(gint tab) {
gtk_spin_button_set_update_policy((GtkSpinButton*)statics_items_spin, GTK_UPDATE_IF_VALID);
gtk_box_pack_start((GtkBox*)hbox, statics_items_spin, FALSE, FALSE, 0);
gtk_box_pack_start((GtkBox*)vbox_history, frame, FALSE, FALSE, 0);


history_timeout_check = gtk_check_button_new_with_mnemonic(_("Purge history after timeout"));
history_timeout_check = gtk_check_button_new_with_mnemonic(_("Purge history after _timeout"));
g_signal_connect((GObject*)history_timeout_check, "toggled", (GCallback)check_toggled, NULL);
g_signal_connect((GObject*)history_timeout_check, "toggled", (GCallback)handle_history_purge_toggle, NULL);
gtk_box_pack_start((GtkBox*)vbox, history_timeout_check, FALSE, FALSE, 0);
hbox = gtk_hbox_new(FALSE, 4);
gtk_box_pack_start((GtkBox*)vbox, hbox, FALSE, FALSE, 0);

label = gtk_label_new(_("Timeout seconds"));
gtk_misc_set_alignment((GtkMisc*)label, 0.0, 0.50);
gtk_box_pack_start((GtkBox*)hbox, label, FALSE, FALSE, 0);
adjustment_small = gtk_adjustment_new(25, 5, 100, 1, 10, 0);
adjustment_small = gtk_adjustment_new(0, 1, 100, 1, 10, 0);
history_timeout_spin = gtk_spin_button_new((GtkAdjustment*)adjustment_small, 0.0, 0);
g_signal_connect((GObject*)history_timeout_spin, "value-changed", (GCallback)handle_history_spinner, NULL);
gtk_spin_button_set_update_policy((GtkSpinButton*)history_timeout_spin, GTK_UPDATE_IF_VALID);
gtk_box_pack_start((GtkBox*)hbox, history_timeout_spin, FALSE, FALSE, 0);

Expand Down
4 changes: 3 additions & 1 deletion src/preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ G_BEGIN_DECLS
#define DEF_USE_RMB_MENU FALSE
#define DEF_HISTORY_LIMIT 50
#define DEF_HISTORY_TIMEOUT FALSE
#define DEF_HISTORY_TIMEOUT_SECONDS 10
#define DEF_HISTORY_TIMEOUT_SECONDS 30
#define DEF_ITEMS_MENU 20
#define DEF_STATICS_SHOW TRUE
#define DEF_STATICS_ITEMS 10
Expand Down Expand Up @@ -77,6 +77,8 @@ void save_preferences();

void show_preferences(gint tab);

void init_purge_timer();

G_END_DECLS

#endif

0 comments on commit ac1fdac

Please sign in to comment.