commit e98b100c7658bf62901e0658339f3344cf0bf9fc
Author: Javier Kohen <jko...@users.sourceforge.net>
Date:   Wed Feb 16 05:58:00 2011 +0100

    Changed auto-save gating to use time based intervals.

 libgtkpod/file.c                                   |   41 ++++++++++++++-----
 libgtkpod/file.h                                   |    2 +
 libgtkpod/prefs.c                                  |    4 +-
 plugins/core_preferences/core_prefs.c              |    4 +-
 plugins/core_preferences/core_prefs.xml            |    7 +--
 .../playlist_display/playlist_display_actions.c    |    9 +---
 6 files changed, 41 insertions(+), 26 deletions(-)
---
diff --git a/libgtkpod/file.c b/libgtkpod/file.c
index 9716a5f..a6e82e3 100644
--- a/libgtkpod/file.c
+++ b/libgtkpod/file.c
@@ -87,12 +87,30 @@ FileType *determine_filetype(const gchar *path) {
     return type;
 }
 
-static void save_if_needed(gint count, iTunesDB *itdb) {
-    /* save every ${file_threshold} files but do at least ${file_theshold} 
first*/
-    int threshold = prefs_get_int("file_saving_threshold");
-    if (count >= threshold && count % threshold == 0) {
+/*
+ * Save the database every ${file_saving_time_threshold} seconds. This
+ * is useful for checkpointing when importing large batches of files.
+ *
+ * The caller is responsible for calling gp_save_itdb one last time
+ * when done.
+ *
+ * Args:
+ *   last_save_time: the time of the last save. Initialize to time(NULL) on
+ *     the first call.
+ */
+void gp_save_if_needed(GTime *last_save_time, iTunesDB *itdb) {
+    g_assert(NULL != last_save_time);
+    int threshold = prefs_get_int("file_saving_time_threshold");
+    GTime next_save_time = *last_save_time + threshold;
+    GTime now = (GTime) time(NULL);
+    printf("last_save: %u; next_save: %u; now: %u\n",
+          *last_save_time, next_save_time, now);
+    if (now >= next_save_time) {
         gp_save_itdb(itdb);
         gtkpod_tracks_statusbar_update();
+       /* Use the finishing time as the last save time, so if saving
+          is slow, we don't save on every call */
+       *last_save_time = time(NULL);
     }
 }
 
@@ -147,13 +165,14 @@ add_playlist_by_filename(iTunesDB *itdb, gchar *plfile, 
Playlist *plitem, gint p
     gchar *dirname = NULL, *plname = NULL;
     gchar buf[PATH_MAX];
     FileType *type = NULL; /* type of playlist file */
-    gint line, tracks;
+    gint line;
     FILE *fp;
     gboolean error;
 
     g_return_val_if_fail (plfile, FALSE);
     g_return_val_if_fail (itdb, FALSE);
 
+    GTime last_save_time = (GTime) time(NULL);
     if (g_file_test(plfile, G_FILE_TEST_IS_DIR)) {
         gtkpod_warning(_("'%s' is a directory, not a playlist file.\n\n"), 
plfile);
         return FALSE; /* definitely not! */
@@ -194,7 +213,6 @@ add_playlist_by_filename(iTunesDB *itdb, gchar *plfile, 
Playlist *plitem, gint p
      all of these are line based -- add different code for different
      playlist files */
     line = -1; /* nr of line being read */
-    tracks = 0; /* nr of tracks added */
     error = FALSE;
     while (!error && fgets(buf, PATH_MAX, fp)) {
         gchar *bufp = buf;
@@ -264,7 +282,7 @@ add_playlist_by_filename(iTunesDB *itdb, gchar *plfile, 
Playlist *plitem, gint p
                 gtkpod_warning(_("Skipping '%s' to avoid adding playlist file 
recursively\n"), filename);
             }
             else if (add_track_by_filename(itdb, filename, plitem, 
prefs_get_int("add_recursively"), addtrackfunc, data)) {
-                save_if_needed(tracks, itdb);
+                gp_save_if_needed(&last_save_time, itdb);
             }
             g_free(filename);
         }
@@ -288,7 +306,7 @@ add_playlist_by_filename(iTunesDB *itdb, gchar *plfile, 
Playlist *plitem, gint p
  \*------------------------------------------------------------------*/
 
 
-static gint add_directory_by_name_internal(iTunesDB *itdb, gchar *name, 
Playlist *plitem, gboolean descend, gint *filecount, AddTrackFunc addtrackfunc, 
gpointer data) {
+static gint add_directory_by_name_internal(GTime *last_save_time, iTunesDB 
*itdb, gchar *name, Playlist *plitem, gboolean descend, gint *filecount, 
AddTrackFunc addtrackfunc, gpointer data) {
     gint result = 0;
 
     g_return_val_if_fail (itdb, 0);
@@ -304,7 +322,7 @@ static gint add_directory_by_name_internal(iTunesDB *itdb, 
gchar *name, Playlist
                 if (next != NULL) {
                     gchar *nextfull = g_build_filename(name, next, NULL);
                     if (descend || !g_file_test(nextfull, G_FILE_TEST_IS_DIR)) 
{
-                        result += add_directory_by_name_internal(itdb, 
nextfull, plitem, descend, filecount, addtrackfunc, data);
+                       result += 
add_directory_by_name_internal(last_save_time, itdb, nextfull, plitem, descend, 
filecount, addtrackfunc, data);
                     }
                     g_free(nextfull);
                 }
@@ -318,7 +336,7 @@ static gint add_directory_by_name_internal(iTunesDB *itdb, 
gchar *name, Playlist
     else {
         if (add_track_by_filename(itdb, name, plitem, descend, addtrackfunc, 
data)) {
             *filecount = *filecount + 1;
-            save_if_needed(*filecount, itdb);
+            gp_save_if_needed(last_save_time, itdb);
         }
         result += *filecount;
     }
@@ -344,7 +362,8 @@ static gint add_directory_by_name_internal(iTunesDB *itdb, 
gchar *name, Playlist
 gint add_directory_by_name(iTunesDB *itdb, gchar *name, Playlist *plitem, 
gboolean descend, AddTrackFunc addtrackfunc, gpointer data) {
     /* Uses internal method so that a count parameter can be added for saving 
purposes. */
     gint filecount = 0;
-    return add_directory_by_name_internal(itdb, name, plitem, descend, 
&filecount, addtrackfunc, data);
+    GTime last_save_time = (GTime) time(NULL);
+    return add_directory_by_name_internal(&last_save_time, itdb, name, plitem, 
descend, &filecount, addtrackfunc, data);
 }
 
 /*------------------------------------------------------------------*\
diff --git a/libgtkpod/file.h b/libgtkpod/file.h
index a11ddcf..36c53be 100644
--- a/libgtkpod/file.h
+++ b/libgtkpod/file.h
@@ -34,6 +34,7 @@
 #  include <config.h>
 #endif
 
+#include <glib.h>
 #include <gtk/gtk.h>
 #include <stdio.h>
 #include "itdb.h"
@@ -76,6 +77,7 @@ void gp_load_ipods (void);
 iTunesDB *gp_load_ipod (iTunesDB *itdb);
 gboolean gp_eject_ipod(iTunesDB *itdb);
 gboolean gp_save_itdb (iTunesDB *itdb);
+void gp_save_if_needed(GTime* last_save_time, iTunesDB *itdb);
 void handle_export (void);
 void data_changed (iTunesDB *itdb);
 void data_unchanged (iTunesDB *itdb);
diff --git a/libgtkpod/prefs.c b/libgtkpod/prefs.c
index 43aa3df..a68fcb0 100644
--- a/libgtkpod/prefs.c
+++ b/libgtkpod/prefs.c
@@ -174,10 +174,10 @@ static void set_default_preferences() {
     prefs_set_string("initial_mountpoint", "/media/ipod");
 
     /*
-     * When adding files, determines after how many a
+     * When adding files, determines how often a
      * save should be performed.
      */
-    prefs_set_int("file_saving_threshold", 10);
+    prefs_set_int("file_saving_time_threshold", 60);  /* in seconds */
 
     str = g_build_filename(get_script_dir(), CONVERT_TO_MP3_SCRIPT, NULL);
     prefs_set_string("path_conv_mp3", str);
diff --git a/plugins/core_preferences/core_prefs.c 
b/plugins/core_preferences/core_prefs.c
index f7ec7a9..533772c 100644
--- a/plugins/core_preferences/core_prefs.c
+++ b/plugins/core_preferences/core_prefs.c
@@ -361,7 +361,7 @@ G_MODULE_EXPORT void 
on_video_thumbnailer_changed(GtkEditable *sender, gpointer
  glade callback
  */
 G_MODULE_EXPORT void on_save_threshold_spin_button_value_changed(GtkSpinButton 
*spinbutton, gpointer user_data) {
-    prefs_set_int("file_saving_threshold", gtk_spin_button_get_value_as_int 
(spinbutton));
+    prefs_set_int("file_saving_time_threshold", 
gtk_spin_button_get_value_as_int (spinbutton));
 }
 
 /*
@@ -667,7 +667,7 @@ static void setup_values() {
     GtkWidget *skip_track_update_radio = GTK_WIDGET(gtk_builder_get_object 
(builder, "skip_track_update"));
 
     gtk_spin_button_set_value(GTK_SPIN_BUTTON (gtk_builder_get_object 
(builder, "agp_track_count")), prefs_get_int("misc_track_nr"));
-    gtk_spin_button_set_value(GTK_SPIN_BUTTON (gtk_builder_get_object 
(builder, "save_threshold_spin_button")), 
prefs_get_int("file_saving_threshold"));
+    gtk_spin_button_set_value(GTK_SPIN_BUTTON (gtk_builder_get_object 
(builder, "save_threshold_spin_button")), 
prefs_get_int("file_saving_time_threshold"));
 
     /* Check boxes */
     for (i = 0; i < COUNTOF(checkbox_map); i++) {
diff --git a/plugins/core_preferences/core_prefs.xml 
b/plugins/core_preferences/core_prefs.xml
index bdf22dd..93ff96a 100644
--- a/plugins/core_preferences/core_prefs.xml
+++ b/plugins/core_preferences/core_prefs.xml
@@ -1604,12 +1604,11 @@ Examples:
                                 <property name="tooltip_text" 
translatable="yes">When multiple tracks are added to a repository, should an
 error occur then it is likely, without saving all added tracks
 will be lost since they are not saved. This preference allows for
-a save operation to be conducted after the number of tracks
-specified.
+a save operation to be conducted periodically.
 
-The default is 10 so after 10 tracks have been added a save
+The default is 60 seconds, so every minute a save
 will be imposed on the repository.</property>
-                                <property name="label" 
translatable="yes">Threshold for import of tracks before a save 
triggered:</property>
+                                <property name="label" 
translatable="yes">Auto-save every seconds:</property>
                               </object>
                               <packing>
                                 <property name="expand">False</property>
diff --git a/plugins/playlist_display/playlist_display_actions.c 
b/plugins/playlist_display/playlist_display_actions.c
index 6234d6f..c3fbd4b 100644
--- a/plugins/playlist_display/playlist_display_actions.c
+++ b/plugins/playlist_display/playlist_display_actions.c
@@ -234,26 +234,21 @@ static void create_add_playlists_dialog(iTunesDB *itdb) {
 }
 
 static void fileselection_add_files(GSList* names, Playlist *playlist) {
-    gint count = 0;
     GSList* gsl; /* Current node in list */
     gboolean result = TRUE; /* Result of file adding */
-    int threshold = prefs_get_int("file_saving_threshold");
 
     /* If we don't have a playlist to add to, don't add anything */
     g_return_if_fail (playlist);
 
     block_widgets();
 
+    GTime last_save_time = (GTime) time(NULL);
     gtkpod_statusbar_busy_push();
     /* Get the filenames and add them */
     for (gsl = names; gsl; gsl = gsl->next) {
         result
                 &= add_track_by_filename(playlist->itdb, gsl->data, playlist, 
prefs_get_int("add_recursively"), NULL, NULL);
-        count++;
-        if (count % threshold == 0) { /* update and save every ten tracks 
added */
-            gp_save_itdb(playlist->itdb);
-            gtkpod_tracks_statusbar_update();
-        }
+       gp_save_if_needed(&last_save_time, playlist->itdb);
     }
 
     /* Final save of remaining added tracks */

------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
gtkpod-cvs2 mailing list
gtkpod-cvs2@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gtkpod-cvs2

Reply via email to