Remove debug statements from KCal backends.
[beagle.git] / beagled / EvolutionDataServerQueryable / CalContainer.cs
blobaed6325c74436b25302b2ba5ddc383967dc21eb5
1 //
2 // CalContainer.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 using System;
28 using System.Globalization;
29 using System.IO;
31 using Beagle.Util;
33 using Evolution;
35 namespace Beagle.Daemon.EvolutionDataServerQueryable {
37 public class CalContainer : Container {
39 private Cal cal;
40 private CalView cal_view;
41 private Scheduler.Priority priority = Scheduler.Priority.Delayed;
43 public CalContainer (Evolution.Source source, EvolutionDataServerQueryable queryable, string fingerprint) : base (source, queryable, fingerprint) { }
45 public override bool OpenClient ()
47 if (!this.source.IsLocal ()) {
48 Logger.Log.Debug ("Skipping remote calendar {0}", this.source.Uri);
49 return false;
52 try {
53 this.cal = new Cal (this.source, CalSourceType.Event);
54 this.cal.Open (true);
55 } catch (Exception e) {
56 Logger.Log.Warn (e, "Unable to open calendar {0}:", this.source.Uri);
57 return false;
60 return true;
63 public override void OpenView ()
65 this.cal_view = this.cal.GetCalView ("#t");
67 this.cal_view.ObjectsAdded += OnObjectsAdded;
68 this.cal_view.ObjectsModified += OnObjectsModified;
69 this.cal_view.ObjectsRemoved += OnObjectsRemoved;
70 this.cal_view.ViewDone += OnViewDone;
72 this.cal_view.Start ();
75 public override void IndexAll ()
77 CalComponent[] event_list = this.cal.GetItems ("#t");
79 Logger.Log.Debug ("Calendar has {0} items", event_list.Length);
81 foreach (CalComponent cc in event_list)
82 AddCalComponent (cc);
85 public override void IndexChanges ()
87 CalComponent[] added, changed;
88 string[] removed;
90 Logger.Log.Debug ("Getting calendar changes for {0}", this.source.Uri);
92 try {
93 this.cal.GetChanges ("beagle-" + this.fingerprint, out added, out changed, out removed);
94 } catch (Exception e) {
95 Logger.Log.Warn (e, "Unable to get changes for {0}:", this.source.Uri);
96 return;
99 Logger.Log.Debug ("Calendar {0}: {1} added, {2} changed, {3} removed",
100 this.cal.Uri, added.Length, changed.Length, removed.Length);
102 foreach (CalComponent cc in added)
103 AddCalComponent (cc);
105 foreach (CalComponent cc in changed)
106 AddCalComponent (cc);
108 #if false
109 // FIXME: Broken in e-d-s right now
110 foreach (string id in removed) {
111 RemoveCalComponent (id);
113 #endif
116 public override void Remove ()
118 Logger.Log.Debug ("Removing calendar source {0}", this.source.Uid);
120 Property prop = Property.NewUnsearched ("fixme:source_uid", this.source.Uid);
121 this.queryable.RemovePropertyIndexable (prop);
123 this.cal_view.Dispose ();
124 this.cal.Dispose ();
127 private void OnObjectsAdded (object o, Evolution.ObjectsAddedArgs args)
129 foreach (CalComponent cc in CalUtil.CalCompFromICal (args.Objects.Handle, this.cal_view.Client))
130 AddCalComponent (cc);
133 private void OnObjectsModified (object o, Evolution.ObjectsModifiedArgs args)
135 foreach (CalComponent cc in CalUtil.CalCompFromICal (args.Objects.Handle, this.cal_view.Client))
136 AddCalComponent (cc);
139 private void OnObjectsRemoved (object o, Evolution.ObjectsRemovedArgs args)
141 // FIXME: This is a temporary workaround for the
142 // fact that the evolution bindings return a
143 // GLib.List with an object type, but there are
144 // really strings in there.
146 GLib.List id_list = new GLib.List (args.Uids.Handle,
147 typeof (string));
149 foreach (string id in id_list)
150 RemoveCalComponent (id);
153 private void OnViewDone (object o, Evolution.ViewDoneArgs args)
155 // Now that we're done synching with the original
156 // state of the calendar, switch all new changes to
157 // Immediate mode
158 priority = Scheduler.Priority.Immediate;
161 /////////////////////////////////////
163 // URI scheme is:
164 // calendar:///?source-uid=<value>&comp-uid=<value>[&comp-rid=value]
166 // The Uri class sucks SO MUCH ASS. It shits itself
167 // on foo:///?bar so we have to insert something in
168 // before "?bar". This is filed as Ximian bug #76146.
169 // Hopefully it is just a bug in Mono and not a
170 // fundamental problem of the Uri class. Fortunately
171 // Evolution can handle the horribly mangled URIs
172 // that come out of it.
174 private Uri GetCalendarUri (CalComponent cc) {
175 return GetCalendarUri (cc.Uid);
178 private Uri GetCalendarUri (string id) {
179 return new Uri (String.Format ("calendar://uri-class-sucks/?source-uid={0}&comp-uid={1}",
180 this.source.Uid, id));
183 /////////////////////////////////////
185 private void AddCalComponent (CalComponent cc)
187 Indexable indexable = CalComponentToIndexable (cc);
189 this.queryable.AddIndexable (indexable, this.priority);
192 private void RemoveCalComponent (string id)
194 this.queryable.RemoveIndexable (GetCalendarUri (id));
197 /////////////////////////////////////
199 private Indexable CalComponentToIndexable (CalComponent cc)
201 Indexable indexable = new Indexable (GetCalendarUri (cc));
203 indexable.Timestamp = cc.Dtstart;
204 indexable.HitType = "Calendar";
206 indexable.AddProperty (Property.NewUnsearched ("fixme:source_uid", this.source.Uid));
207 indexable.AddProperty (Property.NewUnsearched ("fixme:uid", cc.Uid));
208 indexable.AddProperty (Property.NewDate ("fixme:starttime", cc.Dtstart.ToUniversalTime ()));
209 indexable.AddProperty (Property.NewDate ("fixme:endtime", cc.Dtend.ToUniversalTime ()));
211 foreach (string attendee in cc.Attendees)
212 indexable.AddProperty (Property.New ("fixme:attendee", attendee));
214 foreach (string comment in cc.Comments)
215 indexable.AddProperty (Property.New ("fixme:comment", comment));
217 foreach (string description in cc.Descriptions)
218 indexable.AddProperty (Property.New ("fixme:description", description));
220 foreach (string summary in cc.Summaries)
221 indexable.AddProperty (Property.New ("fixme:summary", summary));
223 foreach (string category in cc.Categories)
224 indexable.AddProperty (Property.NewUnsearched ("fixme:category", category));
226 foreach (string location in cc.Location)
227 indexable.AddProperty (Property.New ("fixme:location", location));
229 return indexable;