]> Pileus Git - grits/commitdiff
Add rate limit to auto-saving prefs
authorAndy Spencer <andy753421@gmail.com>
Sat, 4 Feb 2012 07:54:35 +0000 (07:54 +0000)
committerAndy Spencer <andy753421@gmail.com>
Sun, 5 Feb 2012 03:13:11 +0000 (03:13 +0000)
src/grits-prefs.c

index 0ea39088317f2860ddf97ebc287d2032aaad1a74..51e19cd64eb9f5bb011cb6ebb980cc1f71d2d053 100644 (file)
@@ -33,6 +33,7 @@
 #include <config.h>
 
 #include <glib.h>
+#include <time.h>
 #include "grits-marshal.h"
 #include "grits-prefs.h"
 
@@ -45,6 +46,7 @@ static guint signals[NUM_SIGNALS];
 /* Helper functions */
 static void grits_prefs_save(GritsPrefs *prefs)
 {
+       g_debug("GritsPrefs: save");
        gsize length;
        gchar *dir = g_path_get_dirname(prefs->key_path);
        if (!g_file_test(dir, G_FILE_TEST_EXISTS))
@@ -54,6 +56,21 @@ static void grits_prefs_save(GritsPrefs *prefs)
        g_free(dir);
        g_free(data);
 }
+static gboolean grits_prefs_try_save(GritsPrefs *prefs)
+{
+       const  time_t interval = 1;
+       static time_t lastsave = 0;
+       static guint  source   = 0;
+       if (time(NULL) - lastsave > interval) {
+               grits_prefs_save(prefs);
+               lastsave = time(NULL);
+               source   = 0;
+       } else if (source == 0) {
+               source = g_timeout_add_seconds(interval,
+                       (GSourceFunc)grits_prefs_try_save, prefs);
+       }
+       return FALSE;
+}
 
 /***********
  * Methods *
@@ -131,7 +148,7 @@ void grits_prefs_set_##name##_v(GritsPrefs *prefs,
        gchar *all = g_strconcat(group, "/", key, NULL);                             \
        g_signal_emit(prefs, signals[SIG_PREF_CHANGED], 0,                           \
                        all, g_type, &value);                                        \
-       grits_prefs_save(prefs);                                                     \
+       grits_prefs_try_save(prefs);                                                 \
        g_free(all);                                                                 \
 }                                                                                    \
 void grits_prefs_set_##name(GritsPrefs *prefs, const gchar *key, const c_type value) \