[california/wip/725763-manager] Write changes to the CalendarSource back to EDS



commit d6f71cc91db093dab3e1b64094726f8d052c1a76
Author: Jim Nelson <jim yorba org>
Date:   Fri Mar 14 16:41:23 2014 -0700

    Write changes to the CalendarSource back to EDS

 src/backing/eds/backing-eds-calendar-source.vala |   61 +++++++++++++++++++++-
 1 files changed, 59 insertions(+), 2 deletions(-)
---
diff --git a/src/backing/eds/backing-eds-calendar-source.vala 
b/src/backing/eds/backing-eds-calendar-source.vala
index 197f686..8b31f90 100644
--- a/src/backing/eds/backing-eds-calendar-source.vala
+++ b/src/backing/eds/backing-eds-calendar-source.vala
@@ -11,9 +11,13 @@ namespace California.Backing {
  */
 
 internal class EdsCalendarSource : CalendarSource {
+    private const int UPDATE_DELAY_MSEC = 500;
+    
     private E.Source eds_source;
     private E.SourceCalendar eds_calendar;
     private E.CalClient? client = null;
+    private uint source_write_id = 0;
+    private Cancellable? source_write_cancellable = null;
     
     public EdsCalendarSource(E.Source eds_source, E.SourceCalendar eds_calendar) {
         base (eds_source.display_name);
@@ -21,13 +25,66 @@ internal class EdsCalendarSource : CalendarSource {
         this.eds_source = eds_source;
         this.eds_calendar = eds_calendar;
         
-        visible = eds_calendar.selected;
+        // use unidirectional bindings so source updates (writing) only occurs when changed from
+        // within the app
+        eds_calendar.bind_property("selected", this, PROP_VISIBLE, BindingFlags.SYNC_CREATE);
         
+        // when changed within the app, need to write it back out
         notify[PROP_VISIBLE].connect(on_visible_changed);
     }
     
+    ~EdsCalendarSource() {
+        cancel_source_write();
+    }
+    
     private void on_visible_changed() {
-        debug("%s visible=%s", to_string(), visible.to_string());
+        // only schedule source writes if something actually changed
+        if (eds_calendar.selected == visible)
+            return;
+        
+        eds_calendar.selected = visible;
+        schedule_source_write("visible=%s".printf(visible.to_string()));
+    }
+    
+    private void schedule_source_write(string reason) {
+        cancel_source_write();
+        
+        debug("Scheduling update of %s due to %s...", to_string(), reason);
+        source_write_cancellable = new Cancellable();
+        source_write_id = Timeout.add(UPDATE_DELAY_MSEC, on_background_write_source, Priority.LOW);
+    }
+    
+    private void cancel_source_write() {
+        if (source_write_id != 0) {
+            GLib.Source.remove(source_write_id);
+            source_write_id = 0;
+        }
+        
+        if (source_write_cancellable != null) {
+            source_write_cancellable.cancel();
+            source_write_cancellable = null;
+        }
+    }
+    
+    private bool on_background_write_source() {
+        // in essence, say this is no longer scheduled ... for now, allow another write to be
+        // scheduled while this one is occurring
+        source_write_id = 0;
+        Cancellable? cancellable = source_write_cancellable;
+        source_write_cancellable = null;
+        
+        if (cancellable == null || cancellable.is_cancelled())
+            return false;
+        
+        try {
+            debug("Updating EDS source %s...", to_string());
+            // TODO: Fix bindings to use async variant
+            eds_source.write_sync(cancellable);
+        } catch (Error err) {
+            debug("Error updating EDS source %s: %s", to_string(), err.message);
+        }
+        
+        return false;
     }
     
     // Invoked by EdsStore prior to making it available outside of unit


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