[pan2: 4/23] Add a newsrc autosave timer.



commit 25ab4ab9a2c80cef072c4e986f4b308152e51f95
Author: K. Haley <haleykd users sf net>
Date:   Thu May 5 17:26:33 2011 -0600

    Add a newsrc autosave timer.
    
    If newsrc is dirty utomatically save newsrc files after x minutes
    have elapsed. Set to 0 in preferences dialog to disable.

 pan/data-impl/data-impl.cc |    4 +++-
 pan/data-impl/data-impl.h  |   13 +++++++++++++
 pan/data-impl/groups.cc    |    3 +++
 pan/data-impl/headers.cc   |   14 ++++++++++++++
 pan/gui/pan.cc             |    1 +
 pan/gui/prefs-ui.cc        |   23 ++++++++++++++++++++++-
 6 files changed, 56 insertions(+), 2 deletions(-)
---
diff --git a/pan/data-impl/data-impl.cc b/pan/data-impl/data-impl.cc
index 7300dd5..9f6bc53 100644
--- a/pan/data-impl/data-impl.cc
+++ b/pan/data-impl/data-impl.cc
@@ -56,7 +56,9 @@ DataImpl :: DataImpl (bool unit_test, int cache_megs, DataIO * io):
   _cache (get_cache_path(), cache_megs),
   _unit_test (unit_test),
   _data_io (io),
-  _descriptions_loaded (false)
+  _descriptions_loaded (false),
+  newsrc_autosave_id (0),
+  newsrc_autosave_timeout (0)
 {
   rebuild_backend ();
 }
diff --git a/pan/data-impl/data-impl.h b/pan/data-impl/data-impl.h
index cc0855b..6e577b2 100644
--- a/pan/data-impl/data-impl.h
+++ b/pan/data-impl/data-impl.h
@@ -614,6 +614,19 @@ namespace pan
     public:
 
       const ArticleFilter _article_filter;
+
+    private:
+      guint newsrc_autosave_id;
+      guint newsrc_autosave_timeout;
+    public:
+      bool in_newsrc_cb;
+      void set_newsrc_autosave_timeout(guint seconds)
+        {newsrc_autosave_timeout = seconds;}
+      void save_newsrc_files()
+      {
+        save_newsrc_files(*_data_io);
+        newsrc_autosave_id = 0;
+      }
   };
 }
 
diff --git a/pan/data-impl/groups.cc b/pan/data-impl/groups.cc
index f1901d3..37892a8 100644
--- a/pan/data-impl/groups.cc
+++ b/pan/data-impl/groups.cc
@@ -208,6 +208,9 @@ DataImpl :: load_newsrc_files (const DataIO& data_io)
 void
 DataImpl :: save_newsrc_files (DataIO& data_io) const
 {
+  if (!in_newsrc_cb && newsrc_autosave_id)
+    g_source_remove( newsrc_autosave_id );
+
   if (_unit_test)
     return;
 
diff --git a/pan/data-impl/headers.cc b/pan/data-impl/headers.cc
index b8d5127..1e62c2b 100644
--- a/pan/data-impl/headers.cc
+++ b/pan/data-impl/headers.cc
@@ -838,6 +838,17 @@ DataImpl :: save_headers (DataIO& data_io, const Quark& group) const
 /*******
 ********
 *******/
+namespace
+{
+  gboolean nrc_as_cb(gpointer ptr)
+  {
+    DataImpl *data = static_cast<DataImpl*>(ptr);
+    data->in_newsrc_cb = true;
+    data->save_newsrc_files();
+    data->in_newsrc_cb = false;
+    return FALSE;
+  }
+}
 
 void
 DataImpl :: mark_read (const Article & a, bool read)
@@ -876,6 +887,9 @@ DataImpl :: mark_read (const Article  ** articles,
     fire_group_counts (group, g._unread_count, g._article_count);
     on_articles_changed (group, it->second, false);
   }
+  
+  if( !newsrc_autosave_id && newsrc_autosave_timeout )
+    newsrc_autosave_id = g_timeout_add_seconds( newsrc_autosave_timeout * 60, nrc_as_cb, this);
 }
 
 
diff --git a/pan/gui/pan.cc b/pan/gui/pan.cc
index 5268ef7..3e50861 100644
--- a/pan/gui/pan.cc
+++ b/pan/gui/pan.cc
@@ -317,6 +317,7 @@ main (int argc, char *argv[])
       std::cerr << _("Please configure Pan's news servers before using it as an nzb client.") << std::endl;
        return 0;
     }
+    data.set_newsrc_autosave_timeout( prefs.get_int("newsrc-autosave-timeout-min", 10 ));
 
     // instantiate the queue...
     WorkerPool worker_pool (4, true);
diff --git a/pan/gui/prefs-ui.cc b/pan/gui/prefs-ui.cc
index 1adf6ff..7dac161 100644
--- a/pan/gui/prefs-ui.cc
+++ b/pan/gui/prefs-ui.cc
@@ -89,6 +89,22 @@ namespace
     return r;
   }
 
+  void spin_value_changed_cb( GtkSpinButton *spin, gpointer data)
+  {
+    const char * key = (const char*) g_object_get_data (G_OBJECT(spin), PREFS_KEY);
+    Prefs *prefs = static_cast<Prefs*>(data);
+    prefs->set_int(key, gtk_spin_button_get_value_as_int(spin));
+  }
+  GtkWidget* new_spin_button (const char *key, int low, int high, Prefs &prefs)
+  {
+    guint tm = prefs.get_int(key, 5 );
+    GtkAdjustment *adj = (GtkAdjustment*) gtk_adjustment_new(tm, low, high, 1.0, 1.0, 0.0);
+    GtkWidget *w = gtk_spin_button_new( adj, 1.0, 0);
+    g_object_set_data_full(G_OBJECT(w), PREFS_KEY, g_strdup(key), g_free);
+    g_signal_connect (w, "value_changed", G_CALLBACK(spin_value_changed_cb), &prefs);
+    return w;
+  }
+
   GtkWidget* new_orient_radio (GtkWidget* prev, const char* label, const char* value, std::string& cur, Prefs& prefs)
   {
     GtkWidget * r = prev==0
@@ -449,13 +465,18 @@ PrefsDialog :: PrefsDialog (Prefs& prefs, GtkWindow* parent):
     HIG :: workarea_add_wide_control (t, &row, w);
   HIG::workarea_add_section_divider (t, &row);
   HIG :: workarea_add_section_title (t, &row, _("Articles"));
-    HIG :: workarea_add_section_spacer (t, row, 3);
+    HIG :: workarea_add_section_spacer (t, row, 4);
     w = new_check_button (_("Space selects next article rather than next unread"), "space-selects-next-article", true, prefs);
     HIG :: workarea_add_wide_control (t, &row, w);
     w = new_check_button (_("Smooth scrolling"), "smooth-scrolling", true, prefs);
     HIG :: workarea_add_wide_control (t, &row, w);
     w = new_check_button (_("Clear article cache on shutdown"), "clear-article-cache-on-shutdown", false, prefs);
     HIG :: workarea_add_wide_control (t, &row, w);
+    w = new_spin_button ("newsrc-autosave-timeout-min", 0, 60, prefs);
+    l = gtk_label_new(_("Minutes to autosave newsrc files."));
+    gtk_misc_set_alignment (GTK_MISC(l), 0.0, 0.5);
+    gtk_label_set_mnemonic_widget(GTK_LABEL(l), w);
+    HIG::workarea_add_row (t, &row, w, l);
   HIG :: workarea_finish (t, &row);
   gtk_notebook_append_page (GTK_NOTEBOOK(notebook), t, gtk_label_new_with_mnemonic(_("_Behavior")));
 



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