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.
8 // Copyright (C) 2006 Debajyoti Bera <dbera.web@gmail.com>
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.
33 using System
.Collections
;
36 using System
.Globalization
;
41 namespace Beagle
.Filters
{
43 public abstract class FilterKCal
: Beagle
.Daemon
.Filter
{
44 protected Hashtable KCalNamePropertyMapping
;
46 protected enum KCalType
{
47 Special
, // Special handling
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
)
61 is_comma_sep
= comma_sep
;
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)
82 virtual protected Hashtable KCalPropertyMapping
{
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 (
108 } else if (token
.Type
== ICalParser
.TokenType
.PropertyValue
) {
109 ProcessProperty (current_property
,
116 override protected void DoPull ()
121 private void ProcessProperty (string prop_name
,
125 if (prop_name
== null || paramlist
== null)
128 if (prop_value
== null)
131 if (! KCalPropertyMapping
.Contains (prop_name
))
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 (',')) {
151 AddProperty (Beagle
.Property
.NewKeyword (
155 AddProperty (Beagle
.Property
.New (
161 AddProperty (Beagle
.Property
.NewKeyword (beagle_prop_name
, prop_value
));
163 AddProperty (Beagle
.Property
.New (beagle_prop_name
, prop_value
));
171 protected DateTime
ProcessKCalDate (string date_string
)
173 DateTime dt
= DateTime
.MinValue
;
176 date_string
= date_string
.Replace ("Z", "+00:00");
180 "yyyy-MM-ddTHH:mm:sszzz",
184 dt
= DateTime
.ParseExact (
187 DateTimeFormatInfo
.InvariantInfo
,
188 DateTimeStyles
.AdjustToUniversal
);
189 } catch (Exception e
) {
190 Log
.Debug (e
, "Error while parsing date string [{0}]", date_string
);
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
,