~ubuntu-branches/ubuntu/karmic/mono-addins/karmic

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Mirco Bauer
  • Date: 2007-07-14 12:07:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070714120748-2elczfsjlrdsrpms
Tags: upstream-0.2
ImportĀ upstreamĀ versionĀ 0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Addin.cs
 
3
//
 
4
// Author:
 
5
//   Lluis Sanchez Gual
 
6
//
 
7
// Copyright (C) 2005 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
using System;
 
30
using System.Collections;
 
31
using System.IO;
 
32
using System.Xml;
 
33
using System.Xml.Serialization;
 
34
using System.Collections.Specialized;
 
35
using Mono.Addins.Description;
 
36
using Mono.Addins.Database;
 
37
 
 
38
namespace Mono.Addins
 
39
{
 
40
        public class Addin
 
41
        {
 
42
                AddinInfo addin;
 
43
                string configFile;
 
44
                string sourceFile;
 
45
                string hostId;
 
46
                WeakReference desc;
 
47
                AddinDatabase database;
 
48
                
 
49
                internal Addin (AddinDatabase database, string file)
 
50
                {
 
51
                        this.database = database;
 
52
                        configFile = file;
 
53
                }
 
54
                
 
55
                internal Addin (AddinDatabase database, string hostId, string hostSourceFile)
 
56
                {
 
57
                        this.database = database;
 
58
                        this.hostId = hostId;
 
59
                        this.sourceFile = hostSourceFile;
 
60
                }
 
61
                
 
62
                public string Id {
 
63
                        get { return this.AddinInfo.Id; }
 
64
                }
 
65
                
 
66
                public string Namespace {
 
67
                        get { return this.AddinInfo.Namespace; }
 
68
                }
 
69
                
 
70
                public string LocalId {
 
71
                        get { return this.AddinInfo.LocalId; }
 
72
                }
 
73
                
 
74
                public string Version {
 
75
                        get { return this.AddinInfo.Version; }
 
76
                }
 
77
                
 
78
                public string Name {
 
79
                        get { return this.AddinInfo.Name; }
 
80
                }
 
81
                
 
82
                internal string PrivateDataPath {
 
83
                        get { return Path.Combine (database.AddinPrivateDataPath, Path.GetFileNameWithoutExtension (Description.FileName)); }
 
84
                }
 
85
                
 
86
                public bool SupportsVersion (string version)
 
87
                {
 
88
                        return AddinInfo.SupportsVersion (version);
 
89
                }
 
90
                
 
91
                internal AddinInfo AddinInfo {
 
92
                        get {
 
93
                                if (addin == null) {
 
94
                                        try {
 
95
                                                addin = AddinInfo.ReadFromDescription (Description);
 
96
                                        } catch (Exception ex) {
 
97
                                                throw new InvalidOperationException ("Could not read add-in file: " + configFile, ex);
 
98
                                        }
 
99
                                }
 
100
                                return addin;
 
101
                        }
 
102
                }
 
103
                
 
104
                public bool Enabled {
 
105
                        get { return AddinInfo.IsRoot ? true : database.IsAddinEnabled (AddinInfo.Id, true); }
 
106
                        set {
 
107
                                if (value)
 
108
                                        database.EnableAddin (AddinInfo.Id, true);
 
109
                                else
 
110
                                        database.DisableAddin (AddinInfo.Id);
 
111
                        }
 
112
                }
 
113
                
 
114
                public bool IsUserAddin {
 
115
                        get { return configFile.StartsWith (Environment.GetFolderPath (Environment.SpecialFolder.Personal)); }
 
116
                }
 
117
                
 
118
                public string AddinFile {
 
119
                        get {
 
120
                                if (sourceFile == null && addin == null)
 
121
                                        LoadAddinInfo ();
 
122
                                return sourceFile;
 
123
                        }
 
124
                }
 
125
                
 
126
                void LoadAddinInfo ()
 
127
                {
 
128
                        if (addin == null) {
 
129
                                try {
 
130
                                        AddinDescription m = Description;
 
131
                                        sourceFile = m.AddinFile;
 
132
                                        addin = AddinInfo.ReadFromDescription (m);
 
133
                                } catch (Exception ex) {
 
134
                                        throw new InvalidOperationException ("Could not read add-in file: " + configFile, ex);
 
135
                                }
 
136
                        }
 
137
                }
 
138
                
 
139
                public AddinDescription Description {
 
140
                        get {
 
141
                                if (desc != null) {
 
142
                                        AddinDescription d = desc.Target as AddinDescription;
 
143
                                        if (d != null)
 
144
                                                return d;
 
145
                                }
 
146
                                
 
147
                                AddinDescription m;
 
148
                                
 
149
                                if (hostId != null)
 
150
                                        database.GetHostDescription (null, hostId, sourceFile, out m);
 
151
                                else
 
152
                                        database.ReadAddinDescription (null, configFile, out m);
 
153
                                
 
154
                                if (m == null)
 
155
                                        throw new InvalidOperationException ("Could not read add-in description");
 
156
                                if (addin == null) {
 
157
                                        addin = AddinInfo.ReadFromDescription (m);
 
158
                                        sourceFile = m.AddinFile;
 
159
                                }
 
160
                                desc = new WeakReference (m);
 
161
                                return m;
 
162
                        }
 
163
                }
 
164
                        
 
165
                
 
166
                public static int CompareVersions (string v1, string v2)
 
167
                {
 
168
                        string[] a1 = v1.Split ('.');
 
169
                        string[] a2 = v2.Split ('.');
 
170
                        
 
171
                        for (int n=0; n<a1.Length; n++) {
 
172
                                if (a1[n] == "") {
 
173
                                        if (a2[n] != "")
 
174
                                                return 1;
 
175
                                        continue;
 
176
                                }
 
177
                                try {
 
178
                                        int n1 = int.Parse (a1[n]);
 
179
                                        int n2 = int.Parse (a2[n]);
 
180
                                        if (n1 < n2)
 
181
                                                return 1;
 
182
                                        else if (n1 > n2)
 
183
                                                return -1;
 
184
                                } catch {
 
185
                                        return 1;
 
186
                                }
 
187
                        }
 
188
                        return 0;
 
189
                }
 
190
                
 
191
                public static string GetFullId (string ns, string id, string version)
 
192
                {
 
193
                        string res;
 
194
                        if (id.StartsWith ("::"))
 
195
                                res = id.Substring (2);
 
196
                        else if (ns != null && ns.Length > 0)
 
197
                                res = ns + "." + id;
 
198
                        else
 
199
                                res = id;
 
200
                        
 
201
                        if (version != null && version.Length > 0)
 
202
                                return res + "," + version;
 
203
                        else
 
204
                                return res;
 
205
                }
 
206
                
 
207
                public static string GetIdName (string addinId)
 
208
                {
 
209
                        int i = addinId.IndexOf (',');
 
210
                        if (i != -1)
 
211
                                return addinId.Substring (0, i);
 
212
                        else
 
213
                                return addinId;
 
214
                }
 
215
                
 
216
                public static string GetIdVersion (string addinId)
 
217
                {
 
218
                        int i = addinId.IndexOf (',');
 
219
                        if (i != -1)
 
220
                                return addinId.Substring (i + 1).Trim ();
 
221
                        else
 
222
                                return string.Empty;
 
223
                }
 
224
                
 
225
                public static void GetIdParts (string addinId, out string name, out string version)
 
226
                {
 
227
                        int i = addinId.IndexOf (',');
 
228
                        if (i != -1) {
 
229
                                name = addinId.Substring (0, i);
 
230
                                version = addinId.Substring (i+1).Trim ();
 
231
                        } else {
 
232
                                name = addinId;
 
233
                                version = string.Empty;
 
234
                        }
 
235
                }
 
236
        }
 
237
}