Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ConditionType.cs « Mono.Addins « Mono.Addins - github.com/mono/mono-addins.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4f7b0db81574a92e7e4420df2c1d9eea1d6c7b2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//
// ConditionType.cs
//
// Author:
//   Lluis Sanchez Gual
//
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//


using System;
using System.Xml;
using Mono.Addins.Description;
using System.Collections;
using System.Collections.Generic;

namespace Mono.Addins
{
	/// <summary>
	/// A condition evaluator.
	/// </summary>
	/// <remarks>
	/// Add-ins may use conditions to register nodes in an extension point which
	/// are only visible under some contexts. For example, an add-in registering
	/// a custom menu option to the main menu of a sample text editor might want
	/// to make that option visible only for some kind of files. To allow add-ins
	/// to do this kind of check, the host application needs to define a new condition.
	/// </remarks>
	public abstract class ConditionType
	{
		internal event EventHandler Changed;
		string id;
		
		/// <summary>
		/// Evaluates the condition.
		/// </summary>
		/// <param name="conditionNode">
		/// Condition node information.
		/// </param>
		/// <returns>
		/// 'true' if the condition is satisfied.
		/// </returns>
		public abstract bool Evaluate (NodeElement conditionNode);
		
		/// <summary>
		/// Notifies that the condition has changed, and that it has to be re-evaluated.
		/// </summary>
		/// This method must be called when there is a change in the state that determines
		/// the result of the evaluation. When this method is called, all node conditions
		/// depending on it are reevaluated and the corresponding events for adding or
		/// removing extension nodes are fired.
		/// <remarks>
		/// </remarks>
		public void NotifyChanged ()
		{
			if (Changed != null)
				Changed (this, EventArgs.Empty);
		}
		
		internal string Id {
			get { return id; }
			set { id = value; }
		}
	}
	
	internal class BaseCondition
	{
		BaseCondition parent;
		
		internal BaseCondition (BaseCondition parent)
		{
			this.parent = parent;
		}
		
		public virtual bool Evaluate (ExtensionContext ctx)
		{
			return parent == null || parent.Evaluate (ctx);
		}
		
		internal virtual void GetConditionTypes (List<string> listToFill)
		{
		}
	}
	
	internal class NullCondition: BaseCondition
	{
		public NullCondition (): base (null)
		{
		}
		
		public override bool Evaluate (ExtensionContext ctx)
		{
			return false;
		}
	}
	
	class OrCondition: BaseCondition
	{
		BaseCondition[] conditions;
		
		public OrCondition (BaseCondition[] conditions, BaseCondition parent): base (parent)
		{
			this.conditions = conditions;
		}
		
		public override bool Evaluate (ExtensionContext ctx)
		{
			if (!base.Evaluate (ctx))
				return false;
			foreach (BaseCondition cond in conditions)
				if (cond.Evaluate (ctx))
					return true;
			return false;
		}
		
		internal override void GetConditionTypes (List<string> listToFill)
		{
			foreach (BaseCondition cond in conditions)
				cond.GetConditionTypes (listToFill);
		}
	}
	
	class AndCondition: BaseCondition
	{
		BaseCondition[] conditions;
		
		public AndCondition (BaseCondition[] conditions, BaseCondition parent): base (parent)
		{
			this.conditions = conditions;
		}
		
		public override bool Evaluate (ExtensionContext ctx)
		{
			if (!base.Evaluate (ctx))
				return false;
			foreach (BaseCondition cond in conditions)
				if (!cond.Evaluate (ctx))
					return false;
			return true;
		}
		
		internal override void GetConditionTypes (List<string> listToFill)
		{
			foreach (BaseCondition cond in conditions)
				cond.GetConditionTypes (listToFill);
		}
	}
	
	class NotCondition: BaseCondition
	{
		BaseCondition baseCond;
		
		public NotCondition (BaseCondition baseCond, BaseCondition parent): base (parent)
		{
			this.baseCond = baseCond;
		}
		
		public override bool Evaluate (ExtensionContext ctx)
		{
			return !baseCond.Evaluate (ctx);
		}
		
		internal override void GetConditionTypes (List<string> listToFill)
		{
			baseCond.GetConditionTypes (listToFill);
		}
	}

	
	internal sealed class Condition: BaseCondition
	{
		ExtensionNodeDescription node;
		string typeId;
		AddinEngine addinEngine;
		string addin;

		internal const string SourceAddinAttribute = "__sourceAddin"; 
		
		internal Condition (AddinEngine addinEngine, ExtensionNodeDescription element, BaseCondition parent): base (parent)
		{
			this.addinEngine = addinEngine;
			typeId = element.GetAttribute ("id");
			addin = element.GetAttribute (SourceAddinAttribute);
			node = element;
		}
		
		public override bool Evaluate (ExtensionContext ctx)
		{
			if (!base.Evaluate (ctx))
				return false;

			if (!string.IsNullOrEmpty (addin)) {
				// Make sure the add-in that implements the condition is loaded
				addinEngine.LoadAddin (null, addin, true);
				addin = null; // Don't try again
			}
			
			ConditionType type = ctx.GetCondition (typeId);

			if (type == null) {
				var parts = string.Join(", ", Array.ConvertAll(node.Attributes, attr => attr.Name + "=" + attr.Value));
				addinEngine.ReportError ("Condition '" + typeId + "' not found in current extension context. [" + parts + "]", node.ParentAddinDescription.AddinId, null, false);
				return false;
			}
			
			try {
				return type.Evaluate (node);
			}
			catch (Exception ex) {
				addinEngine.ReportError ("Error while evaluating condition '" + typeId + "'", node.ParentAddinDescription.AddinId, ex, false);
				return false;
			}
		}
		
		internal override void GetConditionTypes (List<string> listToFill)
		{
			listToFill.Add (typeId);
		}
	}
}