~ubuntu-branches/ubuntu/jaunty/tomboy/jaunty

« back to all changes in this revision

Viewing changes to Mono.Addins/Mono.Addins/Mono.Addins/ExtensionNode.cs

  • Committer: Bazaar Package Importer
  • Author(s): Pedro Fragoso
  • Date: 2009-02-17 03:08:19 UTC
  • mfrom: (1.1.46 upstream)
  • Revision ID: james.westby@ubuntu.com-20090217030819-87k5mkna0w5tvvqf
Tags: 0.13.5-0ubuntu1
* New upstream release
  - Removed bundled Mono.Addins. Mono.Addins is
    now a hard dependency.
  - Update printing to use Gtk.Print (#512369, Benjamin Podszun)
    Still buggy.
  - Fix multi-page printing of exported note HTML (#548198)
  - Fix crash when clicking link and browser not set (#569639).
  - 64-bit Windows support (#558272, Jay R. Wren).
  - Search window position saved on Windows/Mac (#559663).
  - Fix lingering tray icon in Windows (#569709, Benjamin Podszun).
  - Fix bug with font settings (#559724, Benjamin Podszun).
  - Mac MonoDevelop solution now easier to build (Doug Johnston et al).
  - Other fixes: #562846 (James Westby) #570917, #570918.
  - Additional updates to note printing (#512369, #572024
    , Benjamin Podszun).
  - Windows installer now requires Novell's GTK# >= 2.12.8 (#569324).
  - Increase/Decrease Indent shortcuts now appear in menu
    (#570334, Benjamin Podszun).
  - No longer writes to disk every 40 seconds (#514434).
  - Fixes to note linking (#323845, Florian).
  - Add GConf preference for auto-accepting SSL Certs in 
    WebDAV sync (#531364).
  - After succcessfully configuring sync, offer to perform 
    first sync (#553079).
* debian/control:
  - Use libgconf2.24-cil and libgnome2.24-cil (LP: #314516)
  - Build-dep on libgnomepanel2.24-cil
  - Remove Build-dep on libgnomeprint and libgnomeprintui
  - Add Vcs headers
* .bzr-builddeb/default.conf: added
* debian/patches/02_configurable_compiler.patch:
  - Removed, merged upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// ExtensionNode.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.Collections;
32
 
using System.Xml;
33
 
using System.Reflection;
34
 
using Mono.Addins.Description;
35
 
 
36
 
namespace Mono.Addins
37
 
{
38
 
        public class ExtensionNode
39
 
        {
40
 
                bool childrenLoaded;
41
 
                TreeNode treeNode;
42
 
                ExtensionNodeList childNodes;
43
 
                RuntimeAddin addin;
44
 
                string addinId;
45
 
                ExtensionNodeType nodeType;
46
 
                event ExtensionNodeEventHandler extensionNodeChanged;
47
 
                
48
 
                public string Id {
49
 
                        get { return treeNode != null ? treeNode.Id : string.Empty; }
50
 
                }
51
 
                
52
 
                public string Path {
53
 
                        get { return treeNode != null ? treeNode.GetPath () : string.Empty; }
54
 
                }
55
 
                
56
 
                public ExtensionNode Parent {
57
 
                        get {
58
 
                                if (treeNode != null && treeNode.Parent != null)
59
 
                                        return treeNode.Parent.ExtensionNode;
60
 
                                else
61
 
                                        return null;
62
 
                        }
63
 
                }
64
 
                
65
 
                public ExtensionContext ExtensionContext {
66
 
                        get { return treeNode.Context; }
67
 
                }
68
 
                
69
 
                public bool HasId {
70
 
                        get { return !Id.StartsWith (ExtensionTree.AutoIdPrefix); }
71
 
                }
72
 
                
73
 
                internal void SetTreeNode (TreeNode node)
74
 
                {
75
 
                        treeNode = node;
76
 
                }
77
 
                
78
 
                internal void SetData (string plugid, ExtensionNodeType nodeType)
79
 
                {
80
 
                        this.addinId = plugid;
81
 
                        this.nodeType = nodeType;
82
 
                }
83
 
                
84
 
                internal string AddinId {
85
 
                        get { return addinId; }
86
 
                }
87
 
                
88
 
                internal TreeNode TreeNode {
89
 
                        get { return treeNode; }
90
 
                }
91
 
                
92
 
                public RuntimeAddin Addin {
93
 
                        get {
94
 
                                if (addin == null && addinId != null) {
95
 
                                        if (!AddinManager.SessionService.IsAddinLoaded (addinId))
96
 
                                                AddinManager.SessionService.LoadAddin (null, addinId, true);
97
 
                                        addin = AddinManager.SessionService.GetAddin (addinId);
98
 
                                }
99
 
                                if (addin == null)
100
 
                                        throw new InvalidOperationException ("Add-in '" + addinId + "' could not be loaded.");
101
 
                                return addin; 
102
 
                        }
103
 
                }
104
 
                
105
 
                public event ExtensionNodeEventHandler ExtensionNodeChanged {
106
 
                        add {
107
 
                                extensionNodeChanged += value;
108
 
                                foreach (ExtensionNode node in ChildNodes) {
109
 
                                        try {
110
 
                                                extensionNodeChanged (this, new ExtensionNodeEventArgs (ExtensionChange.Add, node));
111
 
                                        } catch (Exception ex) {
112
 
                                                AddinManager.ReportError (null, null, ex, false);
113
 
                                        }
114
 
                                }
115
 
                        }
116
 
                        remove {
117
 
                                extensionNodeChanged -= value;
118
 
                        }
119
 
                }
120
 
                
121
 
                public ExtensionNodeList ChildNodes {
122
 
                        get {
123
 
                                if (childrenLoaded)
124
 
                                        return childNodes;
125
 
                                
126
 
                                childrenLoaded = true;
127
 
                                
128
 
                                try {
129
 
                                        if (treeNode.Children.Count == 0) {
130
 
                                                childNodes = ExtensionNodeList.Empty;
131
 
                                                return childNodes;
132
 
                                        }
133
 
                                }
134
 
                                catch (Exception ex) {
135
 
                                        AddinManager.ReportError (null, null, ex, false);
136
 
                                        childNodes = ExtensionNodeList.Empty;
137
 
                                        return childNodes;
138
 
                                }
139
 
 
140
 
                                ArrayList list = new ArrayList ();
141
 
                                foreach (TreeNode cn in treeNode.Children) {
142
 
                                        
143
 
                                        // For each node check if it is visible for the current context.
144
 
                                        // If something fails while evaluating the condition, just ignore the node.
145
 
                                        
146
 
                                        try {
147
 
                                                if (cn.ExtensionNode != null && cn.IsEnabled)
148
 
                                                        list.Add (cn.ExtensionNode);
149
 
                                        } catch (Exception ex) {
150
 
                                                AddinManager.ReportError (null, null, ex, false);
151
 
                                        }
152
 
                                }
153
 
                                if (list.Count > 0)
154
 
                                        childNodes = new ExtensionNodeList (list);
155
 
                                else
156
 
                                        childNodes = ExtensionNodeList.Empty;
157
 
                        
158
 
                                return childNodes;
159
 
                        }
160
 
                }
161
 
                
162
 
                public object[] GetChildObjects ()
163
 
                {
164
 
                        return GetChildObjects (typeof(object), true);
165
 
                }
166
 
                
167
 
                public object[] GetChildObjects (bool reuseCachedInstance)
168
 
                {
169
 
                        return GetChildObjects (typeof(object), reuseCachedInstance);
170
 
                }
171
 
                
172
 
                public object[] GetChildObjects (Type arrayElementType)
173
 
                {
174
 
                        return GetChildObjects (arrayElementType, true);
175
 
                }
176
 
                
177
 
                public object[] GetChildObjects (Type arrayElementType, bool reuseCachedInstance)
178
 
                {
179
 
                        ArrayList list = new ArrayList (ChildNodes.Count);
180
 
                        
181
 
                        for (int n=0; n<ChildNodes.Count; n++) {
182
 
                                InstanceExtensionNode node = ChildNodes [n] as InstanceExtensionNode;
183
 
                                if (node == null) {
184
 
                                        AddinManager.ReportError ("Error while getting object for node in path '" + Path + "'. Extension node is not a subclass of InstanceExtensionNode.", null, null, false);
185
 
                                        continue;
186
 
                                }
187
 
                                
188
 
                                try {
189
 
                                        if (reuseCachedInstance)
190
 
                                                list.Add (node.GetInstance (arrayElementType));
191
 
                                        else
192
 
                                                list.Add (node.CreateInstance (arrayElementType));
193
 
                                }
194
 
                                catch (Exception ex) {
195
 
                                        AddinManager.ReportError ("Error while getting object for node in path '" + Path + "'.", null, ex, false);
196
 
                                }
197
 
                        }
198
 
                        return (object[]) list.ToArray (arrayElementType);
199
 
                }
200
 
                
201
 
                internal protected virtual void Read (NodeElement elem)
202
 
                {
203
 
                        if (nodeType == null || nodeType.Fields == null)
204
 
                                return;
205
 
 
206
 
                        NodeAttribute[] attributes = elem.Attributes;
207
 
                        Hashtable fields = (Hashtable) nodeType.Fields.Clone ();
208
 
                        
209
 
                        foreach (NodeAttribute at in attributes) {
210
 
                                
211
 
                                ExtensionNodeType.FieldData f = (ExtensionNodeType.FieldData) fields [at.name];
212
 
                                if (f == null)
213
 
                                        continue;
214
 
                                
215
 
                                fields.Remove (at.name);
216
 
                                        
217
 
                                object val;
218
 
 
219
 
                                if (f.Field.FieldType == typeof(string)) {
220
 
                                        if (f.Localizable)
221
 
                                                val = Addin.Localizer.GetString (at.value);
222
 
                                        else
223
 
                                                val = at.value;
224
 
                                }
225
 
                                else if (f.Field.FieldType == typeof(string[])) {
226
 
                                        string[] ss = at.value.Split (',');
227
 
                                        if (ss.Length == 0 && ss[0].Length == 0)
228
 
                                                val = new string [0];
229
 
                                        else {
230
 
                                                for (int n=0; n<ss.Length; n++)
231
 
                                                        ss [n] = ss[n].Trim ();
232
 
                                                val = ss;
233
 
                                        }
234
 
                                }
235
 
                                else if (f.Field.FieldType.IsEnum) {
236
 
                                        val = Enum.Parse (f.Field.FieldType, at.value);
237
 
                                }
238
 
                                else {
239
 
                                        try {
240
 
                                                val = Convert.ChangeType (at.Value, f.Field.FieldType);
241
 
                                        } catch (InvalidCastException) {
242
 
                                                throw new InvalidOperationException ("Property type not supported by [NodeAttribute]: " + f.Field.DeclaringType + "." + f.Field.Name);
243
 
                                        }
244
 
                                }
245
 
                                        
246
 
                                f.Field.SetValue (this, val);
247
 
                        }
248
 
                        
249
 
                        if (fields.Count > 0) {
250
 
                                // Check if one of the remaining fields is mandatory
251
 
                                foreach (DictionaryEntry e in fields) {
252
 
                                        ExtensionNodeType.FieldData f = (ExtensionNodeType.FieldData) e.Value;
253
 
                                        if (f.Required)
254
 
                                                throw new InvalidOperationException ("Required attribute '" + e.Key + "' not found.");
255
 
                                }
256
 
                        }
257
 
                }
258
 
                
259
 
                internal bool NotifyChildChanged ()
260
 
                {
261
 
                        if (!childrenLoaded)
262
 
                                return false;
263
 
 
264
 
                        ExtensionNodeList oldList = childNodes;
265
 
                        childrenLoaded = false;
266
 
                        
267
 
                        bool changed = false;
268
 
                        
269
 
                        foreach (ExtensionNode nod in oldList) {
270
 
                                if (ChildNodes [nod.Id] == null) {
271
 
                                        changed = true;
272
 
                                        OnChildNodeRemoved (nod);
273
 
                                }
274
 
                        }
275
 
                        foreach (ExtensionNode nod in ChildNodes) {
276
 
                                if (oldList [nod.Id] == null) {
277
 
                                        changed = true;
278
 
                                        OnChildNodeAdded (nod);
279
 
                                }
280
 
                        }
281
 
                        if (changed)
282
 
                                OnChildrenChanged ();
283
 
                        return changed;
284
 
                }
285
 
                
286
 
                // Called when the add-in that defined this extension node is actually
287
 
                // loaded in memory.
288
 
                internal protected virtual void OnAddinLoaded ()
289
 
                {
290
 
                }
291
 
                
292
 
                // Called when the add-in that defined this extension node is being
293
 
                // unloaded from memory.
294
 
                internal protected virtual void OnAddinUnloaded ()
295
 
                {
296
 
                }
297
 
                
298
 
                // Called when the children list of this node has changed. It may be due to add-ins
299
 
                // being loaded/unloaded, or to conditions being changed.
300
 
                protected virtual void OnChildrenChanged ()
301
 
                {
302
 
                }
303
 
                
304
 
                protected virtual void OnChildNodeAdded (ExtensionNode node)
305
 
                {
306
 
                        if (extensionNodeChanged != null)
307
 
                                extensionNodeChanged (this, new ExtensionNodeEventArgs (ExtensionChange.Add, node));
308
 
                }
309
 
                
310
 
                protected virtual void OnChildNodeRemoved (ExtensionNode node)
311
 
                {
312
 
                        if (extensionNodeChanged != null)
313
 
                                extensionNodeChanged (this, new ExtensionNodeEventArgs (ExtensionChange.Remove, node));
314
 
                }
315
 
        }
316
 
}