Fix a fd leak in knotes backend (stupid me) and some cleanup in kaddrbook backend.
[beagle.git] / beagled / Flavor.cs
blob92f3670ca37baa2ba262aba6368ff7774a41ef48
1 //
2 // Flavor.cs
3 //
4 // Copyright (C) 2004 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.IO;
29 using System.Collections;
31 using Beagle.Util;
33 namespace Beagle.Daemon {
35 public class FilterFlavor {
37 private string uri = null;
38 private string mime_type = null;
39 private string extension = null;
41 private int priority = 0;
43 public string Uri {
44 get { return uri; }
45 set { uri = IsWild (value) ? null : value; }
48 public string Extension {
49 get { return extension; }
50 set { extension = IsWild (value) ? null : value; }
53 public string MimeType {
54 get { return mime_type; }
55 set { mime_type = IsWild (value) ? null : value; }
58 public int Priority {
59 get { return priority; }
60 set { priority = value; }
63 public FilterFlavor (string uri, string extension, string mime_type, int priority)
65 this.uri = uri;
66 this.extension = extension;
67 this.mime_type = mime_type;
68 this.priority = priority;
71 private bool IsWild (string str)
73 if (str == null)
74 return true;
75 if (str == "")
76 return false;
77 foreach (char c in str)
78 if (c != '*')
79 return false;
80 return true;
83 public bool IsMatch (Uri uri, string extension, string mime_type)
85 if (Uri != null && (uri == null || !StringFu.GlobMatch (Uri, uri.ToString ())))
86 return false;
88 if (Extension != null && (extension == null || !StringFu.GlobMatch (Extension, extension)))
89 return false;
91 if (MimeType != null && (mime_type == null || !StringFu.GlobMatch (MimeType, mime_type)))
92 return false;
94 return true;
97 public int Weight
99 get {
100 int weight = priority;
102 if (Uri != null)
103 weight += 1;
104 if (Extension != null)
105 weight += 1;
106 if (MimeType != null)
107 weight += 1;
109 return weight;
113 ////////////////////////////////////////////
115 public override string ToString ()
117 string ret = "";
119 if (Uri != null)
120 ret += String.Format ("Uri: {0}", Uri);
122 if (Extension != null)
123 ret += String.Format ("Extension: {0}", Extension);
125 if (MimeType != null)
126 ret += String.Format ("MimeType: {0}", MimeType);
128 return ret;
131 public class WeightComparer : IComparer
133 public int Compare (object obj1, object obj2)
135 FilterFlavor flav1 = (FilterFlavor) obj1;
136 FilterFlavor flav2 = (FilterFlavor) obj2;
138 return flav1.Weight.CompareTo (flav2.Weight);
142 public class Hasher : IHashCodeProvider
144 public int GetHashCode (object o)
146 return o.ToString ().GetHashCode ();
150 static WeightComparer the_comparer = new WeightComparer ();
151 static Hasher the_hasher = new Hasher ();
153 public static Hashtable NewHashtable ()
155 return new Hashtable (the_hasher, the_comparer);
158 ////////////////////////////////////////////
160 static private ArrayList flavors = new ArrayList ();
162 static public ArrayList Flavors {
163 get { return flavors; }
166 public static FilterFlavor NewFromMimeType (string mime_type) {
167 return new FilterFlavor (null, null, mime_type, 0);
170 public static FilterFlavor NewFromExtension (string extension) {
171 return new FilterFlavor (null, extension, null, 0);