(Back)port some changes from beagle-lucene-1-9-lockfile-branch: allow ext: queries...
[beagle.git] / Filters / FilterKCal.cs
blob7b0d43cf908da722aa21d26b075a46a78ce69efa
1 //
2 // FilterKCal.cs
3 // WARNING: This is not a general purpose ical/vcard filter!
4 // It could be modified to serve that purpose, but for the time
5 // being, it is just a base class to filter kcal (knotes and
6 // kaddressbook) files.
7 //
8 // Copyright (C) 2006 Debajyoti Bera <dbera.web@gmail.com>
9 //
12 // Permission is hereby granted, free of charge, to any person obtaining a
13 // copy of this software and associated documentation files (the "Software"),
14 // to deal in the Software without restriction, including without limitation
15 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 // and/or sell copies of the Software, and to permit persons to whom the
17 // Software is furnished to do so, subject to the following conditions:
19 // The above copyright notice and this permission notice shall be included in
20 // all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 // DEALINGS IN THE SOFTWARE.
32 using System;
33 using System.Collections;
34 using System.IO;
35 using System.Text;
36 using System.Globalization;
38 using Beagle.Daemon;
39 using Beagle.Util;
41 namespace Beagle.Filters {
43 public abstract class FilterKCal : Beagle.Daemon.Filter {
44 protected Hashtable KCalNamePropertyMapping;
46 protected enum KCalType {
47 Special, // Special handling
48 Text,
49 Date
52 protected struct KCalProperty {
53 public string property_name;
54 public bool is_comma_sep;
55 public bool is_keyword;
56 public KCalType property_type;
58 public KCalProperty (string name, bool comma_sep, bool keyword, KCalType type)
60 property_name = name;
61 is_comma_sep = comma_sep;
62 is_keyword = keyword;
63 property_type = type;
66 public KCalProperty (KCalType type) : this (null, false, false, type)
71 protected struct KCalPropertyParameter {
72 public string param_name;
73 public string param_value;
75 public KCalPropertyParameter (string name, string value)
77 param_name = name;
78 param_value = value;
82 virtual protected Hashtable KCalPropertyMapping {
83 get { return null;}
86 override protected void DoPullProperties ()
88 ICalParser parser = new ICalParser (TextReader);
89 string current_property = null;
90 string current_parameter = null;
91 ArrayList paramlist = new ArrayList ();
93 while (parser.MoveNext()) {
94 ICalParser.Token token = (ICalParser.Token) parser.Current;
95 //Console.WriteLine (token);
97 if (token.Type == ICalParser.TokenType.Property)
98 current_property = token.Value;
100 else if (token.Type == ICalParser.TokenType.PropertyParameter)
101 current_parameter = token.Value;
103 else if (token.Type == ICalParser.TokenType.PropertyParameterValue) {
104 paramlist.Add (new KCalPropertyParameter (
105 current_parameter,
106 token.Value));
108 } else if (token.Type == ICalParser.TokenType.PropertyValue) {
109 ProcessProperty (current_property,
110 paramlist,
111 token.Value);
116 override protected void DoPull ()
118 Finished ();
121 private void ProcessProperty (string prop_name,
122 ArrayList paramlist,
123 string prop_value)
125 if (prop_name == null || paramlist == null)
126 return;
128 if (prop_value == null)
129 prop_value = "";
131 if (! KCalPropertyMapping.Contains (prop_name))
132 return;
134 KCalProperty vcp = (KCalProperty) KCalPropertyMapping [prop_name];
135 if (vcp.property_type == KCalType.Special)
136 ProcessPropertySpecial (prop_name, paramlist, prop_value);
138 string beagle_prop_name = GetPropertyName (prop_name, paramlist);
140 if (vcp.property_type == KCalType.Date) {
141 DateTime dt = ProcessKCalDate (prop_value);
142 AddProperty (Beagle.Property.NewDate (beagle_prop_name, dt));
143 } else if (vcp.property_type == KCalType.Text) {
144 prop_value = prop_value.Replace ("\\,", ",");
145 prop_value = prop_value.Replace ("\\\\", "\\");
146 prop_value = prop_value.Replace ("\\n", "\n");
148 if (vcp.is_comma_sep) {
149 foreach (string sub_value in prop_value.Split (',')) {
150 if (vcp.is_keyword)
151 AddProperty (Beagle.Property.NewKeyword (
152 beagle_prop_name,
153 sub_value));
154 else
155 AddProperty (Beagle.Property.New (
156 beagle_prop_name,
157 sub_value));
159 } else {
160 if (vcp.is_keyword)
161 AddProperty (Beagle.Property.NewKeyword (beagle_prop_name, prop_value));
162 else
163 AddProperty (Beagle.Property.New (beagle_prop_name, prop_value));
167 paramlist.Clear ();
171 protected DateTime ProcessKCalDate (string date_string)
173 DateTime dt = DateTime.MinValue;
175 try {
176 date_string = date_string.Replace ("Z", "+00:00");
177 string[] fmts =
179 // ISO 8601 formats
180 "yyyy-MM-ddTHH:mm:sszzz",
181 "yyyyMMddTHHmmsszzz"
184 dt = DateTime.ParseExact (
185 date_string,
186 fmts,
187 DateTimeFormatInfo.InvariantInfo,
188 DateTimeStyles.AdjustToUniversal);
189 } catch (Exception e) {
190 Log.Debug (e, "Error while parsing date string [{0}]", date_string);
192 return dt;
194 virtual protected string GetPropertyName (string prop_name, ArrayList paramlist)
196 string mapped_prop_name =
197 ((KCalProperty)KCalPropertyMapping [prop_name]).property_name;
198 return mapped_prop_name;
201 virtual protected void ProcessPropertySpecial (string prop_name,
202 ArrayList paramlist,
203 string prop_value)