[california/wip/725786-edit-recurring] Move RRULE into Instance, fix Event.compare_to(), Instance.equal_to()



commit 4c250156fc8df495116f16fc88d5d267318bc048
Author: Jim Nelson <jim yorba org>
Date:   Tue Jul 15 13:15:41 2014 -0700

    Move RRULE into Instance, fix Event.compare_to(), Instance.equal_to()

 src/component/component-event.vala    |   84 +++------------------------------
 src/component/component-instance.vala |   74 +++++++++++++++++++++++++++--
 2 files changed, 77 insertions(+), 81 deletions(-)
---
diff --git a/src/component/component-event.vala b/src/component/component-event.vala
index ebae855..e5a492f 100644
--- a/src/component/component-event.vala
+++ b/src/component/component-event.vala
@@ -20,7 +20,6 @@ public class Event : Instance, Gee.Comparable<Event> {
     public const string PROP_IS_ALL_DAY = "is-all-day";
     public const string PROP_LOCATION = "location";
     public const string PROP_STATUS = "status";
-    public const string PROP_RRULE = "rrule";
     
     public enum Status {
         TENTATIVE,
@@ -87,15 +86,6 @@ public class Event : Instance, Gee.Comparable<Event> {
     public Status status { get; set; default = Status.CONFIRMED; }
     
     /**
-     * { link RecurrenceRule} (RRULE) for { link Event}.
-     *
-     * If the RecurrenceRule is itself altered, that signal is reflected to { link Instance.altered}.
-     *
-     * @see make_recurring
-     */
-    public RecurrenceRule? rrule { get; private set; default = null; }
-    
-    /**
      * Create an { link Event} { link Component} from an EDS CalComponent object.
      *
      * Throws a BackingError if the E.CalComponent's VTYPE is not VEVENT.
@@ -161,12 +151,6 @@ public class Event : Instance, Gee.Comparable<Event> {
                 status = Status.CONFIRMED;
             break;
         }
-        
-        try {
-            make_recurring(new RecurrenceRule.from_ical(ical_component, false));
-        } catch (ComponentError comperr) {
-            // ignored; generally means no RRULE in component
-        }
     }
     
     private void on_notify(ParamSpec pspec) {
@@ -235,15 +219,6 @@ public class Event : Instance, Gee.Comparable<Event> {
                 }
             break;
             
-            case PROP_RRULE:
-                // always remove existing RRULE
-                remove_all_properties(iCal.icalproperty_kind.RRULE_PROPERTY);
-                
-                // add new one, if added
-                if (rrule != null)
-                    rrule.add_to_ical(ical_component);
-            break;
-            
             default:
                 altered = false;
             break;
@@ -350,34 +325,6 @@ public class Event : Instance, Gee.Comparable<Event> {
     }
     
     /**
-     * Add a { link RecurrenceRule} to the { link Event}.
-     *
-     * Pass null to make Event non-recurring.
-     */
-    public void make_recurring(RecurrenceRule? rrule) {
-        if (this.rrule != null) {
-            this.rrule.notify.disconnect(on_rrule_updated);
-            this.rrule.by_rule_updated.disconnect(on_rrule_updated);
-        }
-        
-        if (rrule != null) {
-            rrule.notify.connect(on_rrule_updated);
-            rrule.by_rule_updated.connect(on_rrule_updated);
-        }
-        
-        this.rrule = rrule;
-    }
-    
-    private void on_rrule_updated() {
-        // remove old property, replace with new one
-        remove_all_properties(iCal.icalproperty_kind.RRULE_PROPERTY);
-        rrule.add_to_ical(ical_component);
-        
-        // count this as an alteration
-        notify_altered(false);
-    }
-    
-    /**
      * @inheritDoc
      */
     public override bool is_valid(bool and_useful) {
@@ -428,6 +375,13 @@ public class Event : Instance, Gee.Comparable<Event> {
         if (compare != 0)
             return compare;
         
+        // rid
+        if (rid != null && other.rid != null) {
+            compare = rid.compare_to(other.rid);
+            if (compare != 0)
+                return compare;
+        }
+        
         // summary
         compare = strcmp(summary, other.summary);
         if (compare != 0)
@@ -447,30 +401,6 @@ public class Event : Instance, Gee.Comparable<Event> {
         return uid.compare_to(other.uid);
     }
     
-    public override bool equal_to(Component.Instance other) {
-        Component.Event? other_event = other as Component.Event;
-        if (other_event == null)
-            return false;
-        
-        if (this == other_event)
-            return true;
-        
-        if (is_recurring_instance != other_event.is_recurring_instance)
-            return false;
-        
-        if (is_recurring_instance && !rid.equal_to(other_event.rid))
-            return false;
-        
-        if (sequence != other_event.sequence)
-            return false;
-        
-        return base.equal_to(other);
-    }
-    
-    public override uint hash() {
-        return uid.hash() ^ ((rid != null) ? rid.hash() : 0) ^ sequence;
-    }
-    
     public override string to_string() {
         return "Event %s/rid=%s/%d \"%s\" (%s)".printf(
             uid.to_string(),
diff --git a/src/component/component-instance.vala b/src/component/component-instance.vala
index a515ade..35e6581 100644
--- a/src/component/component-instance.vala
+++ b/src/component/component-instance.vala
@@ -29,6 +29,7 @@ public abstract class Instance : BaseObject, Gee.Hashable<Instance> {
     public const string PROP_DTSTAMP = "dtstamp";
     public const string PROP_UID = "uid";
     public const string PROP_ICAL_COMPONENT = "ical-component";
+    public const string PROP_RRULE = "rrule";
     public const string PROP_RID = "rid";
     public const string PROP_SEQUENCE = "sequence";
     
@@ -61,6 +62,15 @@ public abstract class Instance : BaseObject, Gee.Hashable<Instance> {
     public UID uid { get; private set; }
     
     /**
+     * { link RecurrenceRule} (RRULE) for { link Instance}.
+     *
+     * If the RecurrenceRule is itself altered, that signal is reflected to { link altered}.
+     *
+     * @see make_recurring
+     */
+    public RecurrenceRule? rrule { get; private set; default = null; }
+    
+    /**
      * The RECURRENCE-ID of a recurring component.
      *
      * See [[https://tools.ietf.org/html/rfc5545#section-3.8.4.4]]
@@ -252,6 +262,12 @@ public abstract class Instance : BaseObject, Gee.Hashable<Instance> {
         
         sequence = ical_component.get_sequence();
         
+        try {
+            make_recurring(new RecurrenceRule.from_ical(ical_component, false));
+        } catch (ComponentError comperr) {
+            // ignored; generally means no RRULE in component
+        }
+        
         // save own copy of component; no ownership transferrance w/ current bindings
         if (_ical_component != ical_component)
             _ical_component = ical_component.clone();
@@ -275,6 +291,15 @@ public abstract class Instance : BaseObject, Gee.Hashable<Instance> {
                 ical_component.set_sequence(sequence);
             break;
             
+            case PROP_RRULE:
+                // always remove existing RRULE
+                remove_all_properties(iCal.icalproperty_kind.RRULE_PROPERTY);
+                
+                // add new one, if added
+                if (rrule != null)
+                    rrule.add_to_ical(ical_component);
+            break;
+            
             default:
                 altered = false;
             break;
@@ -285,6 +310,34 @@ public abstract class Instance : BaseObject, Gee.Hashable<Instance> {
     }
     
     /**
+     * Add a { link RecurrenceRule} to the { link Instance}.
+     *
+     * Pass null to make the Instance non-recurring.
+     */
+    public void make_recurring(RecurrenceRule? rrule) {
+        if (this.rrule != null) {
+            this.rrule.notify.disconnect(on_rrule_updated);
+            this.rrule.by_rule_updated.disconnect(on_rrule_updated);
+        }
+        
+        if (rrule != null) {
+            rrule.notify.connect(on_rrule_updated);
+            rrule.by_rule_updated.connect(on_rrule_updated);
+        }
+        
+        this.rrule = rrule;
+    }
+    
+    private void on_rrule_updated() {
+        // remove old property, replace with new one
+        remove_all_properties(iCal.icalproperty_kind.RRULE_PROPERTY);
+        rrule.add_to_ical(ical_component);
+        
+        // count this as an alteration
+        notify_altered(false);
+    }
+    
+    /**
      * Returns an appropriate { link Component} instance for the iCalendar component.
      *
      * VCALENDARs should use { link Component.iCalendar}.
@@ -359,21 +412,34 @@ public abstract class Instance : BaseObject, Gee.Hashable<Instance> {
     }
     
     /**
-     * Equality is defined as { link Component.Instance}s having the same UID.
+     * Equality is defined as { link Component.Instance}s having the same { link uid}, { link rid},
+     * and { link sequence}.
      *
      * Subclasses should override this and { link hash} if more definite equality is necessary.
      */
     public virtual bool equal_to(Instance other) {
-        return (this != other) ? uid.equal_to(other.uid) : true;
+        if (this == other)
+            return true;
+        
+        if (is_recurring_instance != other.is_recurring_instance)
+            return false;
+        
+        if (is_recurring_instance && !rid.equal_to(other.rid))
+            return false;
+        
+        if (sequence != other.sequence)
+            return false;
+        
+        return uid.equal_to(other.uid);
     }
     
     /**
-     * Hash is calculated using the { link Instance} { link UID}.
+     * Hash is calculated using the { link Instance} { link UID}, { link rid}, and { link sequence}.
      *
      * Subclasses should override if they override { link equal_to}.
      */
     public virtual uint hash() {
-        return uid.hash();
+        return uid.hash() ^ ((rid != null) ? rid.hash() : 0) ^ sequence;
     }
 }
 


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