Yet another. Init the gobject type system.
[beagle.git] / Util / Conf.cs
blob81d2ccd2cf7b12f8f72713d3e6d690bf20360aeb
1 //
2 // Conf.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // 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 FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
27 using System;
28 using System.Collections;
29 using System.IO;
30 using System.Diagnostics;
31 using System.Reflection;
32 using System.Xml.Serialization;
33 using System.Text.RegularExpressions;
35 using Beagle.Util;
37 namespace Beagle.Util {
39 public class Conf {
41 // No instantiation
42 private Conf () { }
44 public static Hashtable Sections;
46 public static IndexingConfig Indexing = null;
47 public static DaemonConfig Daemon = null;
48 public static SearchingConfig Searching = null;
50 //#if ENABLE_WEBSERVICES
51 public static NetworkingConfig Networking = null;
52 public static WebServicesConfig WebServices = null;
53 //#endif
54 private static string configs_dir;
55 private static Hashtable mtimes;
56 private static Hashtable subscriptions;
58 private static bool watching_for_updates;
59 private static bool update_watch_present;
61 private static BindingFlags method_search_flags = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod;
63 public delegate void ConfigUpdateHandler (Section section);
65 static Conf ()
67 Sections = new Hashtable (3);
68 mtimes = new Hashtable (3);
69 subscriptions = new Hashtable (3);
71 configs_dir = Path.Combine (PathFinder.StorageDir, "config");
72 if (!Directory.Exists (configs_dir))
73 Directory.CreateDirectory (configs_dir);
75 Conf.Load ();
78 public static void WatchForUpdates ()
80 // Make sure we don't try and watch for updates more than once
81 if (update_watch_present)
82 return;
84 if (Inotify.Enabled) {
85 Inotify.Subscribe (configs_dir, OnInotifyEvent, Inotify.EventType.Create | Inotify.EventType.CloseWrite);
86 } else {
87 // Poll for updates every 60 secs
88 GLib.Timeout.Add (60000, new GLib.TimeoutHandler (CheckForUpdates));
91 update_watch_present = true;
94 private static void OnInotifyEvent (Inotify.Watch watch, string path, string subitem, string srcpath, Inotify.EventType type)
96 if (subitem == "" || watching_for_updates == false)
97 return;
99 Load ();
102 private static bool CheckForUpdates ()
104 if (watching_for_updates)
105 Load ();
106 return true;
109 public static void Subscribe (Type type, ConfigUpdateHandler callback)
111 if (!subscriptions.ContainsKey (type))
112 subscriptions.Add (type, new ArrayList (1));
114 ArrayList callbacks = (ArrayList) subscriptions [type];
115 callbacks.Add (callback);
118 private static void NotifySubscribers (Section section)
120 Type type = section.GetType ();
121 ArrayList callbacks = (ArrayList) subscriptions [type];
123 if (callbacks == null)
124 return;
126 foreach (ConfigUpdateHandler callback in callbacks)
127 callback (section);
130 public static void Load ()
132 Load (false);
135 public static void Load (bool force)
137 Section temp;
139 // FIXME: Yeah
140 LoadFile (typeof (IndexingConfig), Indexing, out temp, force);
141 Indexing = (IndexingConfig) temp;
142 NotifySubscribers (Indexing);
144 LoadFile (typeof (DaemonConfig), Daemon, out temp, force);
145 Daemon = (DaemonConfig) temp;
146 NotifySubscribers (Daemon);
148 LoadFile (typeof (SearchingConfig), Searching, out temp, force);
149 Searching = (SearchingConfig) temp;
150 NotifySubscribers (Searching);
152 //#if ENABLE_WEBSERVICES
153 LoadFile (typeof (NetworkingConfig), Networking, out temp, force);
154 Networking = (NetworkingConfig) temp;
155 NotifySubscribers (Networking);
157 LoadFile (typeof (WebServicesConfig), WebServices, out temp, force);
158 WebServices = (WebServicesConfig) temp;
159 NotifySubscribers (WebServices);
160 //#endif
162 watching_for_updates = true;
165 public static void Save ()
167 Save (false);
170 public static void Save (bool force)
172 foreach (Section section in Sections.Values)
173 if (force || section.SaveNeeded)
174 SaveFile (section);
177 private static bool LoadFile (Type type, Section current, out Section section, bool force)
179 section = current;
180 object [] attrs = Attribute.GetCustomAttributes (type, typeof (ConfigSection));
181 if (attrs.Length == 0)
182 throw new ConfigException ("Could not find ConfigSection attribute on " + type);
184 string sectionname = ((ConfigSection) attrs [0]).Name;
185 string filename = sectionname + ".xml";
186 string filepath = Path.Combine (configs_dir, filename);
187 if (!File.Exists (filepath)) {
188 if (current == null)
189 ConstructDefaultSection (type, sectionname, out section);
190 return false;
193 if (!force && current != null && mtimes.ContainsKey (sectionname) &&
194 File.GetLastWriteTimeUtc (filepath).CompareTo ((DateTime) mtimes [sectionname]) <= 0)
195 return false;
197 Logger.Log.Debug ("Loading {0} from {1}", type, filename);
198 FileStream fs = null;
200 try {
201 fs = File.Open (filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
202 XmlSerializer serializer = new XmlSerializer (type);
203 section = (Section) serializer.Deserialize (fs);
204 } catch (Exception e) {
205 Logger.Log.Error ("Could not load configuration from {0}: {1}", filename, e.Message);
206 if (fs != null)
207 fs.Close ();
208 if (current == null)
209 ConstructDefaultSection (type, sectionname, out section);
210 return false;
213 fs.Close ();
214 Sections.Remove (sectionname);
215 Sections.Add (sectionname, section);
216 mtimes.Remove (sectionname);
217 mtimes.Add (sectionname, File.GetLastWriteTimeUtc (filepath));
218 return true;
221 private static bool SaveFile (Section section)
223 Type type = section.GetType ();
224 object [] attrs = Attribute.GetCustomAttributes (type, typeof (ConfigSection));
225 if (attrs.Length == 0)
226 throw new ConfigException ("Could not find ConfigSection attribute on " + type);
228 string sectionname = ((ConfigSection) attrs [0]).Name;
229 string filename = sectionname + ".xml";
230 string filepath = Path.Combine (configs_dir, filename);
232 Logger.Log.Debug ("Saving {0} to {1}", type, filename);
233 FileStream fs = null;
235 try {
236 watching_for_updates = false;
237 fs = new FileStream (filepath, FileMode.Create);
238 XmlSerializer serializer = new XmlSerializer (type);
239 XmlFu.SerializeUtf8 (serializer, fs, section);
240 } catch (Exception e) {
241 if (fs != null)
242 fs.Close ();
243 Logger.Log.Error ("Could not save configuration to {0}: {1}", filename, e);
244 watching_for_updates = true;
245 return false;
248 fs.Close ();
249 mtimes.Remove (sectionname);
250 mtimes.Add (sectionname, File.GetLastWriteTimeUtc (filepath));
251 watching_for_updates = true;
252 return true;
255 private static void ConstructDefaultSection (Type type, string sectionname, out Section section)
257 ConstructorInfo ctor = type.GetConstructor (Type.EmptyTypes);
258 section = (Section) ctor.Invoke (null);
259 Sections.Remove (sectionname);
260 Sections.Add (sectionname, section);
263 // Lists all config file options in a hash table where key is option name,
264 // and value is description.
265 public static Hashtable GetOptions (Section section)
267 Hashtable options = new Hashtable ();
268 MemberInfo [] members = section.GetType ().GetMembers (method_search_flags);
270 // Find all of the methods ("options") inside the specified section
271 // object which have the ConfigOption attribute.
272 foreach (MemberInfo member in members) {
273 object [] attrs = member.GetCustomAttributes (typeof (ConfigOption), false);
274 if (attrs.Length > 0)
275 options.Add (member.Name, ((ConfigOption) attrs [0]).Description);
278 return options;
281 public static bool InvokeOption (Section section, string option, string [] args, out string output)
283 MethodInfo method = section.GetType ().GetMethod (option, method_search_flags);
284 if (method == null) {
285 string msg = String.Format ("No such method '{0}' for section '{1}'", option, section);
286 throw new ConfigException(msg);
288 object [] attrs = method.GetCustomAttributes (typeof (ConfigOption), false);
289 if (attrs.Length == 0) {
290 string msg = String.Format ("Method '{0}' is not a configurable option", option);
291 throw new ConfigException (msg);
294 // Check the required number of parameters have been provided
295 ConfigOption attr = (ConfigOption) attrs [0];
296 if (attr.Params > 0 && args.Length < attr.Params) {
297 string msg = String.Format ("Option '{0}' requires {1} parameter(s): {2}", option, attr.Params, attr.ParamsDescription);
298 throw new ConfigException (msg);
301 object [] methodparams = { null, args };
302 bool result = (bool) method.Invoke (section, methodparams);
303 output = (string) methodparams [0];
305 // Mark the section as save-needed if we just changed something
306 if (result && attr.IsMutator)
307 section.SaveNeeded = true;
309 return result;
312 [ConfigSection (Name="searching")]
313 public class SearchingConfig : Section {
315 private bool autostart = true;
316 public bool Autostart {
317 get { return autostart; }
318 set { autostart = value; }
321 private KeyBinding show_search_window_binding = new KeyBinding ("F12");
322 public KeyBinding ShowSearchWindowBinding {
323 get { return show_search_window_binding; }
324 set { show_search_window_binding = value; }
327 private int max_displayed = 5;
328 public int MaxDisplayed {
329 get { return max_displayed; }
330 set {
331 if (value <= 0)
332 max_displayed = 1;
333 else
334 max_displayed = value;
338 // BeagleSearch window position and dimension
339 // stored as percentage of screen co-ordinates
340 // to deal with change of resolution problem - hints from tberman
342 private float beagle_search_pos_x = 0;
343 public float BeaglePosX {
344 get { return beagle_search_pos_x; }
345 set { beagle_search_pos_x = value; }
348 private float beagle_search_pos_y = 0;
349 public float BeaglePosY {
350 get { return beagle_search_pos_y; }
351 set { beagle_search_pos_y = value; }
354 private float beagle_search_width = 0;
355 public float BeagleSearchWidth {
356 get { return beagle_search_width; }
357 set { beagle_search_width = value; }
360 private float beagle_search_height = 0;
361 public float BeagleSearchHeight {
362 get { return beagle_search_height; }
363 set { beagle_search_height = value; }
366 // ah!We want a Queue but Queue doesnt serialize *easily*
367 private ArrayList search_history = new ArrayList ();
368 public ArrayList SearchHistory {
369 get { return search_history; }
370 set { search_history = value; }
375 [ConfigSection (Name="daemon")]
376 public class DaemonConfig : Section {
377 private ArrayList static_queryables = new ArrayList ();
378 public ArrayList StaticQueryables {
379 get { return static_queryables; }
380 set { static_queryables = value; }
383 // By default, every backend is allowed.
384 // Only maintain a list of denied backends.
385 private ArrayList denied_backends = new ArrayList ();
386 public ArrayList DeniedBackends {
387 get { return denied_backends; }
388 set { denied_backends = value; }
391 private bool allow_static_backend = false; // by default, false
392 public bool AllowStaticBackend {
393 get { return allow_static_backend; }
394 // Don't really want to expose this, but serialization requires it
395 set { allow_static_backend = value; }
398 private bool index_synchronization = true;
399 public bool IndexSynchronization {
400 get { return index_synchronization; }
401 // Don't really want to expose this, but serialization requires it
402 set { index_synchronization = value; }
405 [ConfigOption (Description="Enable a backend", Params=1, ParamsDescription="Name of the backend to enable")]
406 internal bool AllowBackend (out string output, string [] args)
408 denied_backends.Remove (args [0]);
409 output = "Backend allowed (need to restart beagled for changes to take effect).";
410 return true;
413 [ConfigOption (Description="Disable a backend", Params=1, ParamsDescription="Name of the backend to disable")]
414 internal bool DenyBackend (out string output, string [] args)
416 denied_backends.Add (args [0]);
417 output = "Backend disabled (need to restart beagled for changes to take effect).";
418 return true;
421 private bool allow_root = false;
422 public bool AllowRoot {
423 get { return allow_root; }
424 set { allow_root = value; }
427 [ConfigOption (Description="Add a static queryable", Params=1, ParamsDescription="Index path")]
428 internal bool AddStaticQueryable (out string output, string [] args)
430 static_queryables.Add (args [0]);
431 output = "Static queryable added.";
432 return true;
435 [ConfigOption (Description="Remove a static queryable", Params=1, ParamsDescription="Index path")]
436 internal bool DelStaticQueryable (out string output, string [] args)
438 static_queryables.Remove (args [0]);
439 output = "Static queryable removed.";
440 return true;
443 [ConfigOption (Description="List user-specified static queryables", IsMutator=false)]
444 internal bool ListStaticQueryables (out string output, string [] args)
446 output = "User-specified static queryables:\n";
447 foreach (string index_path in static_queryables)
448 output += String.Format (" - {0}\n", index_path);
449 return true;
452 [ConfigOption (Description="Toggles whether static indexes will be enabled")]
453 internal bool ToggleAllowStaticBackend (out string output, string [] args)
455 allow_static_backend = !allow_static_backend;
456 output = "Static indexes are " + ((allow_static_backend) ? "enabled" : "disabled") + " (need to restart beagled for changes to take effect).";
457 return true;
460 [ConfigOption (Description="Toggles whether your indexes will be synchronized locally if your home directory is on a network device (eg. NFS/Samba)")]
461 internal bool ToggleIndexSynchronization (out string output, string [] args)
463 index_synchronization = !index_synchronization;
464 output = "Index Synchronization is " + ((index_synchronization) ? "enabled" : "disabled") + ".";
465 return true;
468 [ConfigOption (Description="Toggles whether Beagle can be run as root")]
469 internal bool ToggleAllowRoot (out string output, string [] args)
471 allow_root = ! allow_root;
472 if (allow_root)
473 output = "Beagle is now permitted to run as root";
474 else
475 output = "Beagle is no longer permitted to run as root";
476 return true;
480 [ConfigSection (Name="indexing")]
481 public class IndexingConfig : Section
483 private ArrayList roots = new ArrayList ();
484 [XmlArray]
485 [XmlArrayItem(ElementName="Root", Type=typeof(string))]
486 public ArrayList Roots {
487 get { return roots; }
488 set { roots = value; }
491 private bool index_home_dir = true;
492 public bool IndexHomeDir {
493 get { return index_home_dir; }
494 set { index_home_dir = value; }
497 private bool index_on_battery = true;
498 public bool IndexOnBattery {
499 get { return index_on_battery; }
500 set { index_on_battery = value; }
503 private ArrayList excludes = new ArrayList ();
504 [XmlArray]
505 [XmlArrayItem (ElementName="ExcludeItem", Type=typeof(ExcludeItem))]
506 public ArrayList Excludes {
507 get { return excludes; }
508 set { excludes = value; }
511 [ConfigOption (Description="List the indexing roots", IsMutator=false)]
512 internal bool ListRoots (out string output, string [] args)
514 output = "Current roots:\n";
515 if (this.index_home_dir == true)
516 output += " - Your home directory\n";
517 foreach (string root in roots)
518 output += " - " + root + "\n";
520 return true;
523 [ConfigOption (Description="Toggles whether your home directory is to be indexed as a root")]
524 internal bool IndexHome (out string output, string [] args)
526 if (index_home_dir)
527 output = "Your home directory will not be indexed.";
528 else
529 output = "Your home directory will be indexed.";
530 index_home_dir = !index_home_dir;
531 return true;
534 [ConfigOption (Description="Toggles whether any data should be indexed if the system is on battery")]
535 internal bool IndexWhileOnBattery (out string output, string [] args)
537 if (index_on_battery)
538 output = "Data will not be indexed while on battery.";
539 else
540 output = "Data will be indexed while on battery.";
541 index_on_battery = !index_on_battery;
542 return true;
545 [ConfigOption (Description="Add a root path to be indexed", Params=1, ParamsDescription="A path")]
546 internal bool AddRoot (out string output, string [] args)
548 roots.Add (args [0]);
549 output = "Root added.";
550 return true;
553 [ConfigOption (Description="Remove an indexing root", Params=1, ParamsDescription="A path")]
554 internal bool DelRoot (out string output, string [] args)
556 roots.Remove (args [0]);
557 output = "Root removed.";
558 return true;
561 [ConfigOption (Description="List user-specified resources to be excluded from indexing", IsMutator=false)]
562 internal bool ListExcludes (out string output, string [] args)
564 output = "User-specified resources to be excluded from indexing:\n";
565 foreach (ExcludeItem exclude_item in excludes)
566 output += String.Format (" - [{0}] {1}\n", exclude_item.Type.ToString (), exclude_item.Value);
567 return true;
570 [ConfigOption (Description="Add a resource to exclude from indexing", Params=2, ParamsDescription="A type [path/pattern/mailfolder], a path/pattern/name")]
571 internal bool AddExclude (out string output, string [] args)
573 ExcludeType type;
574 try {
575 type = (ExcludeType) Enum.Parse (typeof (ExcludeType), args [0], true);
576 } catch (Exception e) {
577 output = String.Format("Invalid type '{0}'. Valid types: Path, Pattern, MailFolder", args [0]);
578 return false;
581 excludes.Add (new ExcludeItem (type, args [1]));
582 output = "Exclude added.";
583 return true;
586 [ConfigOption (Description="Remove an excluded resource", Params=2, ParamsDescription="A type [path/pattern/mailfolder], a path/pattern/name")]
587 internal bool DelExclude (out string output, string [] args)
589 ExcludeType type;
590 try {
591 type = (ExcludeType) Enum.Parse (typeof (ExcludeType), args [0], true);
592 } catch (Exception e) {
593 output = String.Format("Invalid type '{0}'. Valid types: Path, Pattern, MailFolder", args [0]);
594 return false;
597 foreach (ExcludeItem item in excludes) {
598 if (item.Type != type || item.Value != args [1])
599 continue;
600 excludes.Remove (item);
601 output = "Exclude removed.";
602 return true;
605 output = "Could not find requested exclude to remove.";
606 return false;
611 //#if ENABLE_WEBSERVICES
612 [ConfigSection (Name="webservices")]
613 public class WebServicesConfig: Section
615 private ArrayList publicFolders = new ArrayList ();
616 [XmlArray]
617 [XmlArrayItem(ElementName="PublicFolders", Type=typeof(string))]
618 public ArrayList PublicFolders {
619 get { return publicFolders; }
620 set { publicFolders = value; }
623 private bool allowGlobalAccess = true;
624 public bool AllowGlobalAccess {
625 get { return allowGlobalAccess; }
626 set { allowGlobalAccess = value; }
629 [ConfigOption (Description="List the public folders", IsMutator=false)]
630 internal bool ListPublicFolders(out string output, string [] args)
632 output = "Current list of public folders:\n";
634 foreach (string pf in publicFolders)
635 output += " - " + pf + "\n";
637 return true;
640 [ConfigOption (Description="Check current configuration of global access to Beagle web-services", IsMutator=false)]
641 internal bool CheckGlobalAccess(out string output, string [] args)
643 if (allowGlobalAccess)
644 output = "Global Access to Beagle WebServices is currently ENABLED.";
645 else
646 output = "Global Access to Beagle WebServices is currently DISABLED.";
648 return true;
651 [ConfigOption (Description="Enable/Disable global access to Beagle web-services")]
652 internal bool SwitchGlobalAccess (out string output, string [] args)
654 allowGlobalAccess = !allowGlobalAccess;
656 if (allowGlobalAccess)
657 output = "Global Access to Beagle WebServices now ENABLED.";
658 else
659 output = "Global Access to Beagle WebServices now DISABLED.";
661 return true;
664 [ConfigOption (Description="Add public web-service access to a folder", Params=1, ParamsDescription="A path")]
665 internal bool AddPublicFolder (out string output, string [] args)
667 publicFolders.Add (args [0]);
668 output = "PublicFolder " + args[0] + " added.";
669 return true;
672 [ConfigOption (Description="Remove public web-service access to a folder", Params=1, ParamsDescription="A path")]
673 internal bool DelPublicFolder (out string output, string [] args)
675 publicFolders.Remove (args [0]);
676 output = "PublicFolder " + args[0] + " removed.";
677 return true;
682 [ConfigSection (Name="networking")]
683 public class NetworkingConfig: Section
685 private ArrayList netBeagleNodes = new ArrayList ();
687 [XmlArray]
688 [XmlArrayItem(ElementName="NetBeagleNodes", Type=typeof(string))]
689 public ArrayList NetBeagleNodes {
690 get { return netBeagleNodes; }
691 set { netBeagleNodes = value; }
694 [ConfigOption (Description="List Networked Beagle Daemons to query", IsMutator=false)]
695 internal bool ListBeagleNodes (out string output, string [] args)
697 output = "Current list of Networked Beagle Daemons to query:\n";
699 foreach (string nb in netBeagleNodes)
700 output += " - " + nb + "\n";
702 return true;
705 [ConfigOption (Description="Add a Networked Beagle Daemon to query", Params=1, ParamsDescription="HostName:PortNo")]
706 internal bool AddBeagleNode (out string output, string [] args)
708 string node = args[0];
710 if (((string[])node.Split(':')).Length < 2)
711 node = args [0].Trim() + ":8888";
713 netBeagleNodes.Add(node);
714 output = "Networked Beagle Daemon \"" + node +"\" added.";
715 return true;
718 [ConfigOption (Description="Remove a configured Networked Beagle Daemon", Params=1, ParamsDescription="HostName:PortNo")]
719 internal bool DelBeagleNode (out string output, string [] args)
721 string node = args[0];
723 if (((string[])node.Split(':')).Length < 2)
724 node = args [0].Trim() + ":8888";
726 netBeagleNodes.Remove(node);
727 output = "Networked Beagle Daemon \"" + node +"\" removed.";
728 return true;
731 //#endif
733 public class Section {
734 [XmlIgnore]
735 public bool SaveNeeded = false;
738 private class ConfigOption : Attribute {
739 public string Description;
740 public int Params;
741 public string ParamsDescription;
742 public bool IsMutator = true;
745 private class ConfigSection : Attribute {
746 public string Name;
749 public class ConfigException : Exception {
750 public ConfigException (string msg) : base (msg) { }
755 //////////////////////////////////////////////////////////////////////
757 public enum ExcludeType {
758 Path,
759 Pattern,
760 MailFolder
763 public class ExcludeItem {
765 private ExcludeType type;
766 private string val;
768 [XmlAttribute]
769 public ExcludeType Type {
770 get { return type; }
771 set { type = value; }
774 private string exactMatch;
775 private string prefix;
776 private string suffix;
777 private Regex regex;
779 [XmlAttribute]
780 public string Value {
781 get { return val; }
782 set {
783 switch (type) {
784 case ExcludeType.Path:
785 case ExcludeType.MailFolder:
786 prefix = value;
787 break;
789 case ExcludeType.Pattern:
790 if (value.StartsWith ("/") && value.EndsWith ("/")) {
791 regex = new Regex (value.Substring (1, value.Length - 2));
792 break;
795 int i = value.IndexOf ('*');
796 if (i == -1) {
797 exactMatch = value;
798 } else {
799 if (i > 0)
800 prefix = value.Substring (0, i);
801 if (i < value.Length-1)
802 suffix = value.Substring (i+1);
804 break;
807 val = value;
811 public ExcludeItem () {}
813 public ExcludeItem (ExcludeType type, string value) {
814 this.Type = type;
815 this.Value = value;
818 public bool IsMatch (string param)
820 switch (Type) {
821 case ExcludeType.Path:
822 case ExcludeType.MailFolder:
823 if (prefix != null && ! param.StartsWith (prefix))
824 return false;
826 return true;
828 case ExcludeType.Pattern:
829 if (exactMatch != null)
830 return param == exactMatch;
831 if (prefix != null && ! param.StartsWith (prefix))
832 return false;
833 if (suffix != null && ! param.EndsWith (suffix))
834 return false;
835 if (regex != null && ! regex.IsMatch (param))
836 return false;
838 return true;
841 return false;
844 public override bool Equals (object obj)
846 ExcludeItem exclude = obj as ExcludeItem;
847 return (exclude != null && exclude.Type == type && exclude.Value == val);
850 public override int GetHashCode ()
852 return (this.Value.GetHashCode () ^ (int) this.Type);
857 //////////////////////////////////////////////////////////////////////
859 public class KeyBinding {
860 public string Key;
862 [XmlAttribute]
863 public bool Ctrl = false;
864 [XmlAttribute]
865 public bool Alt = false;
867 public KeyBinding () {}
868 public KeyBinding (string key) : this (key, false, false) {}
870 public KeyBinding (string key, bool ctrl, bool alt)
872 Key = key;
873 Ctrl = ctrl;
874 Alt = alt;
877 public override string ToString ()
879 string result = "";
881 if (Ctrl)
882 result += "<Ctrl>";
883 if (Alt)
884 result += "<Alt>";
886 result += Key;
888 return result;
891 public string ToReadableString ()
893 return ToString ().Replace (">", "-").Replace ("<", "");