~ted/ubuntu/lucid/tomboy/with-patch

« back to all changes in this revision

Viewing changes to Mono.Addins/Mono.Addins/Mono.Addins.Database/DatabaseConfiguration.cs

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2007-07-16 10:26:35 UTC
  • mfrom: (1.1.21 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20070716102635-0wzk26jo50csob7b
Tags: 0.7.2-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// DatabaseConfiguration.cs
 
3
//
 
4
// Author:
 
5
//   Lluis Sanchez Gual
 
6
//
 
7
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
// 
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
 
 
30
using System;
 
31
using System.IO;
 
32
using System.Collections;
 
33
using System.Xml;
 
34
 
 
35
namespace Mono.Addins.Database
 
36
{
 
37
        internal class DatabaseConfiguration
 
38
        {
 
39
                Hashtable addinStatus = new Hashtable ();
 
40
                
 
41
                public bool IsEnabled (string addinId, bool defaultValue)
 
42
                {
 
43
                        if (addinStatus.Contains (addinId))
 
44
                                return addinStatus [addinId] != null;
 
45
                        else
 
46
                                return defaultValue;
 
47
                }
 
48
                
 
49
                public void SetStatus (string addinId, bool enabled, bool defaultValue)
 
50
                {
 
51
                        if (enabled == defaultValue)
 
52
                                addinStatus.Remove (addinId);
 
53
                        else if (enabled)
 
54
                                addinStatus [addinId] = this;
 
55
                        else
 
56
                                addinStatus [addinId] = null;
 
57
                }
 
58
                
 
59
                public static DatabaseConfiguration Read (string file)
 
60
                {
 
61
                        DatabaseConfiguration config = new DatabaseConfiguration ();
 
62
                        
 
63
                        StreamReader s = new StreamReader (file);
 
64
                        using (s) {
 
65
                                XmlTextReader tr = new XmlTextReader (s);
 
66
                                tr.MoveToContent ();
 
67
                                if (tr.IsEmptyElement)
 
68
                                        return config;
 
69
                                
 
70
                                tr.ReadStartElement ("Configuration");
 
71
                                tr.MoveToContent ();
 
72
                                
 
73
                                while (tr.NodeType != XmlNodeType.EndElement) {
 
74
                                        
 
75
                                        if (tr.NodeType != XmlNodeType.Element || tr.IsEmptyElement) {
 
76
                                                tr.Skip ();
 
77
                                        }
 
78
                                        else if (tr.LocalName == "DisabledAddins") {
 
79
                                                // For back compatibility
 
80
                                                tr.ReadStartElement ();
 
81
                                                tr.MoveToContent ();
 
82
                                                while (tr.NodeType != XmlNodeType.EndElement) {
 
83
                                                        if (tr.NodeType == XmlNodeType.Element && tr.LocalName == "Addin")
 
84
                                                                config.addinStatus [tr.ReadElementString ()] = null;
 
85
                                                        else
 
86
                                                                tr.Skip ();
 
87
                                                        tr.MoveToContent ();
 
88
                                                }
 
89
                                                tr.ReadEndElement ();
 
90
                                        }
 
91
                                        else if (tr.LocalName == "AddinStatus") {
 
92
                                                tr.ReadStartElement ();
 
93
                                                tr.MoveToContent ();
 
94
                                                while (tr.NodeType != XmlNodeType.EndElement) {
 
95
                                                        if (tr.NodeType == XmlNodeType.Element && tr.LocalName == "Addin") {
 
96
                                                                string aid = tr.GetAttribute ("id");
 
97
                                                                string senabled = tr.GetAttribute ("enabled");
 
98
                                                                if (senabled.Length == 0 || senabled == "True")
 
99
                                                                        config.addinStatus [aid] = config;
 
100
                                                                else
 
101
                                                                        config.addinStatus [aid] = null;
 
102
                                                        }
 
103
                                                        tr.Skip ();
 
104
                                                        tr.MoveToContent ();
 
105
                                                }
 
106
                                                tr.ReadEndElement ();
 
107
                                        }
 
108
                                        tr.MoveToContent ();
 
109
                                }
 
110
                        }
 
111
                        return config;
 
112
                }
 
113
                
 
114
                public void Write (string file)
 
115
                {
 
116
                        StreamWriter s = new StreamWriter (file);
 
117
                        using (s) {
 
118
                                XmlTextWriter tw = new XmlTextWriter (s);
 
119
                                tw.Formatting = Formatting.Indented;
 
120
                                tw.WriteStartElement ("Configuration");
 
121
                                tw.WriteStartElement ("AddinStatus");
 
122
                                foreach (DictionaryEntry e in addinStatus) {
 
123
                                        tw.WriteStartElement ("Addin");
 
124
                                        tw.WriteAttributeString ("id", (string)e.Key);
 
125
                                        tw.WriteAttributeString ("enabled", (e.Value != null).ToString ());
 
126
                                        tw.WriteEndElement ();
 
127
                                }
 
128
                                tw.WriteEndElement ();
 
129
                                tw.WriteEndElement ();
 
130
                        }
 
131
                }
 
132
        }
 
133
}