[gnome-dvb-daemon] Update timer in SQlite database when changing start time or duration



commit 733dacc87ab922da77f29a27b67399471ff2e33b
Author: Sebastian PÃlsterl <sebp k-d-w org>
Date:   Sun Mar 25 12:34:31 2012 +0200

    Update timer in SQlite database when changing start time or duration

 src/Recorder.vala                                |   43 ++++++++++++++--------
 src/database/TimersStore.vala                    |    3 +-
 src/database/sqlite/SqliteConfigTimersStore.vala |   36 ++++++++++++++++++-
 3 files changed, 65 insertions(+), 17 deletions(-)
---
diff --git a/src/Recorder.vala b/src/Recorder.vala
index ee0f6a3..522fbf4 100644
--- a/src/Recorder.vala
+++ b/src/Recorder.vala
@@ -163,17 +163,16 @@ namespace DVB {
                         }
                     }
                 }
-                
+
                 if (!has_conflict) {
                     this.timers.set (new_timer.Id, new_timer);
                     try {
-                        new Factory().get_timers_store ().add_timer_to_device_group (new_timer,
+                        ret = new Factory().get_timers_store ().add_timer_to_device_group (new_timer,
                             this.DeviceGroup);
                     } catch (SqlError e) {
                         log.error ("%s", e.message);
                     }
-                    this.changed (new_timer.Id, ChangeType.ADDED);
-                                   
+
                     if (this.timers.size == 1 && !this.have_check_timers_timeout) {
                         log.debug ("Creating new check timers");
                         this. check_timers_event_id = Timeout.add_seconds (
@@ -181,12 +180,14 @@ namespace DVB {
                         );
                         this.have_check_timers_timeout = true;
                     }
-                    
+
                     timer_id = new_timer.Id;
-                    ret = true;
                 }
             }
-            
+
+            if (ret)
+                this.changed (new_timer.Id, ChangeType.ADDED);
+
             return ret;
         }
         
@@ -231,7 +232,7 @@ namespace DVB {
         }
 
         protected bool delete_timer (uint32 timer_id) {
-            bool val;
+            bool ret = false;
             lock (this.timers) {
                 if (this.timers.has_key (timer_id)) {
                     if (this.is_timer_active (timer_id)) {
@@ -241,18 +242,18 @@ namespace DVB {
                     }
                     this.timers.unset (timer_id);
                     try {
-                        new Factory().get_timers_store ().remove_timer_from_device_group (
+                        ret = new Factory().get_timers_store ().remove_timer_from_device_group (
                             timer_id, this.DeviceGroup);
                     } catch (SqlError e) {
                         log.error ("%s", e.message);
                     }
-                    this.changed (timer_id, ChangeType.DELETED);
-                    val = true;
-                } else {
-                    val = false;
                 }
             }
-            return val;
+
+            if (ret)
+                this.changed (timer_id, ChangeType.DELETED);
+
+            return ret;
         }
         
         /**
@@ -320,7 +321,12 @@ namespace DVB {
                         timer.set_start_time (start_year, start_month,
                             start_day, start_hour, start_minute);
 
-                        ret = true;
+                        try {
+                            ret = new Factory().get_timers_store ().update_timer (
+                                timer, this.DeviceGroup);
+                        } catch (SqlError e) {
+                            log.error ("%s", e.message);
+                        }
                     }
                 }
             }
@@ -387,6 +393,13 @@ namespace DVB {
                 if (ret) {
                     Timer timer = this.timers.get (timer_id);
                     timer.Duration = duration;
+
+                    try {
+                        ret = new Factory().get_timers_store ().update_timer (
+                            timer, this.DeviceGroup);
+                    } catch (SqlError e) {
+                        log.error ("%s", e.message);
+                    }
                 }
             }
 
diff --git a/src/database/TimersStore.vala b/src/database/TimersStore.vala
index 783d7c0..ab4b9c4 100644
--- a/src/database/TimersStore.vala
+++ b/src/database/TimersStore.vala
@@ -27,7 +27,8 @@ namespace DVB.database {
         public abstract bool add_timer_to_device_group (Timer timer, DeviceGroup dev) throws SqlError;
         public abstract bool remove_timer_from_device_group (uint timer_id, DeviceGroup dev) throws SqlError;
         public abstract bool remove_all_timers_from_device_group (uint group_id) throws SqlError;
-        
+        public abstract bool update_timer (Timer timer, DeviceGroup dev) throws SqlError;
+
     }
 
 }
diff --git a/src/database/sqlite/SqliteConfigTimersStore.vala b/src/database/sqlite/SqliteConfigTimersStore.vala
index 1b1d280..ec7dd71 100644
--- a/src/database/sqlite/SqliteConfigTimersStore.vala
+++ b/src/database/sqlite/SqliteConfigTimersStore.vala
@@ -60,6 +60,11 @@ namespace DVB.database.sqlite {
         event_id INTEGER,
         PRIMARY KEY(timer_id))""";
 
+        private static const string UPDATE_TIMER  =
+        """UPDATE timers SET
+        year=?, month=?, day=?, hour=?, minute=?, duration=?
+        WHERE timer_id=? AND group_id=?""";
+
         private static const string CREATE_GROUPS =
         """CREATE TABLE channel_groups (
         channel_group_id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -159,6 +164,7 @@ namespace DVB.database.sqlite {
         private Statement add_channel_group_statement;
         private Statement remove_channel_group_statement;
         private Statement select_channels_statement;
+        private Statement update_timer_statement;
 
         public SqliteConfigTimersStore () {
             File config_dir = File.new_for_path (
@@ -226,6 +232,8 @@ namespace DVB.database.sqlite {
                 out this.remove_channel_group_statement);
             this.db.prepare (SELECT_CHANNELS, -1,
                 out this.select_channels_statement);
+            this.db.prepare (UPDATE_TIMER, -1,
+                out this.update_timer_statement);
         }
 
         public Gee.List<DeviceGroup> get_all_device_groups () throws SqlError {
@@ -528,7 +536,33 @@ namespace DVB.database.sqlite {
             return true;
         }
 
-        public bool contains_timer (uint timer_id) throws SqlError {
+        public bool update_timer (Timer timer, DeviceGroup dev)
+                throws SqlError
+        {
+            uint[] start = timer.get_start_time ();
+            if (this.update_timer_statement.bind_int (1, (int)start[0]) != Sqlite.OK
+                || this.update_timer_statement.bind_int (2, (int)start[1]) != Sqlite.OK
+                || this.update_timer_statement.bind_int (3, (int)start[2]) != Sqlite.OK
+                || this.update_timer_statement.bind_int (4, (int)start[3]) != Sqlite.OK
+                || this.update_timer_statement.bind_int (5, (int)start[4]) != Sqlite.OK
+                || this.update_timer_statement.bind_int (6, (int)timer.Duration) != Sqlite.OK
+                || this.update_timer_statement.bind_int (7, (int)timer.Id) != Sqlite.OK
+                || this.update_timer_statement.bind_int (8, (int)dev.Id) != Sqlite.OK)
+            {
+                this.throw_last_error ();
+                return false;
+            }
+
+            if (this.update_timer_statement.step () != Sqlite.DONE) {
+                this.throw_last_error_reset (this.update_timer_statement);
+                return false;
+            }
+            this.update_timer_statement.reset ();
+
+            return true;
+        }
+
+        private bool contains_timer (uint timer_id) throws SqlError {
             if (this.contains_timer_statement.bind_int (1, (int)timer_id) != Sqlite.OK)
             {
                 this.throw_last_error ();



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