~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/mono-addins/Mono.Addins/Mono.Addins/Addin.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

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
using System.Linq;
 
38
 
 
39
namespace Mono.Addins
 
40
{
 
41
        /// <summary>
 
42
        /// An add-in.
 
43
        /// </summary>
 
44
        public class Addin
 
45
        {
 
46
                AddinInfo addin;
 
47
                string configFile;
 
48
                string sourceFile;
 
49
                WeakReference desc;
 
50
                AddinDatabase database;
 
51
                bool? isLatestVersion;
 
52
                bool? isUserAddin;
 
53
                string id;
 
54
                
 
55
                internal Addin (AddinDatabase database, string file)
 
56
                {
 
57
                        this.database = database;
 
58
                        configFile = file;
 
59
                }
 
60
                
 
61
                /// <summary>
 
62
                /// Full identifier of the add-in, including namespace and version.
 
63
                /// </summary>
 
64
                public string Id {
 
65
                        get {
 
66
                                if (configFile != null) {
 
67
                                        if (id == null)
 
68
                                                id = Path.GetFileNameWithoutExtension (configFile);
 
69
                                        return id;
 
70
                                }
 
71
                                return this.AddinInfo.Id; 
 
72
                        }
 
73
                }
 
74
                
 
75
                /// <summary>
 
76
                /// Namespace of the add-in.
 
77
                /// </summary>
 
78
                public string Namespace {
 
79
                        get { return this.AddinInfo.Namespace; }
 
80
                }
 
81
                
 
82
                /// <summary>
 
83
                /// Identifier of the add-in (without namespace)
 
84
                /// </summary>
 
85
                public string LocalId {
 
86
                        get { return this.AddinInfo.LocalId; }
 
87
                }
 
88
                
 
89
                /// <summary>
 
90
                /// Version of the add-in
 
91
                /// </summary>
 
92
                public string Version {
 
93
                        get { return this.AddinInfo.Version; }
 
94
                }
 
95
                
 
96
                /// <summary>
 
97
                /// Display name of the add-in
 
98
                /// </summary>
 
99
                public string Name {
 
100
                        get { return this.AddinInfo.Name; }
 
101
                }
 
102
                
 
103
                /// <summary>
 
104
                /// Custom properties specified in the add-in header
 
105
                /// </summary>
 
106
                public AddinPropertyCollection Properties {
 
107
                        get { return this.AddinInfo.Properties; }
 
108
                }
 
109
                
 
110
                internal string PrivateDataPath {
 
111
                        get { return Path.Combine (database.AddinPrivateDataPath, Path.GetFileNameWithoutExtension (Description.FileName)); }
 
112
                }
 
113
                
 
114
                /// <summary>
 
115
                /// Checks version compatibility.
 
116
                /// </summary>
 
117
                /// <param name="version">
 
118
                /// An add-in version.
 
119
                /// </param>
 
120
                /// <returns>
 
121
                /// True if the provided version is compatible with this add-in.
 
122
                /// </returns>
 
123
                /// <remarks>
 
124
                /// This method checks the CompatVersion property to know if the provided version is compatible with the version of this add-in.
 
125
                /// </remarks>
 
126
                public bool SupportsVersion (string version)
 
127
                {
 
128
                        return AddinInfo.SupportsVersion (version);
 
129
                }
 
130
 
 
131
                /// <summary>
 
132
                /// Returns a <see cref="System.String"/> that represents the current <see cref="Mono.Addins.Addin"/>.
 
133
                /// </summary>
 
134
                /// <returns>
 
135
                /// A <see cref="System.String"/> that represents the current <see cref="Mono.Addins.Addin"/>.
 
136
                /// </returns>
 
137
                public override string ToString ()
 
138
                {
 
139
                        return Id;
 
140
                }
 
141
                
 
142
                internal AddinInfo AddinInfo {
 
143
                        get {
 
144
                                if (addin == null) {
 
145
                                        try {
 
146
                                                addin = AddinInfo.ReadFromDescription (Description);
 
147
                                        } catch (Exception ex) {
 
148
                                                throw new InvalidOperationException ("Could not read add-in file: " + configFile, ex);
 
149
                                        }
 
150
                                }
 
151
                                return addin;
 
152
                        }
 
153
                }
 
154
                
 
155
                /// <summary>
 
156
                /// Gets or sets the enabled status of the add-in.
 
157
                /// </summary>
 
158
                /// <remarks>
 
159
                /// This property can be used to enable or disable an add-in.
 
160
                /// The enabled status of an add-in is stored in the add-in registry,
 
161
                /// so when an add-in is disabled, it will be disabled for all applications
 
162
                /// sharing the same registry.
 
163
                /// When an add-in is enabled or disabled, the extension points currently loaded
 
164
                /// in memory will be properly updated to include or exclude extensions from the add-in.
 
165
                /// </remarks>
 
166
                public bool Enabled {
 
167
                        get {
 
168
                                if (!IsLatestVersion)
 
169
                                        return false;
 
170
                                return AddinInfo.IsRoot ? true : database.IsAddinEnabled (Description.Domain, AddinInfo.Id, true);
 
171
                        }
 
172
                        set {
 
173
                                if (value)
 
174
                                        database.EnableAddin (Description.Domain, AddinInfo.Id, true);
 
175
                                else
 
176
                                        database.DisableAddin (Description.Domain, AddinInfo.Id);
 
177
                        }
 
178
                }
 
179
                
 
180
                internal bool IsLatestVersion {
 
181
                        get {
 
182
                                if (isLatestVersion == null) {
 
183
                                        string id, version;
 
184
                                        Addin.GetIdParts (AddinInfo.Id, out id, out version);
 
185
                                        var addins = database.GetInstalledAddins (null, AddinSearchFlagsInternal.IncludeAll | AddinSearchFlagsInternal.LatestVersionsOnly);
 
186
                                        isLatestVersion = addins.Where (a => Addin.GetIdName (a.Id) == id && a.Version == version).Any ();
 
187
                                }
 
188
                                return isLatestVersion.Value;
 
189
                        }
 
190
                        set {
 
191
                                isLatestVersion = value;
 
192
                        }
 
193
                }
 
194
                
 
195
                /// <summary>
 
196
                /// Returns 'true' if the add-in is installed in the user's personal folder
 
197
                /// </summary>
 
198
                public bool IsUserAddin {
 
199
                        get {
 
200
                                if (isUserAddin == null)
 
201
                                        SetIsUserAddin (Description);
 
202
                                return isUserAddin.Value;
 
203
                        }
 
204
                }
 
205
                
 
206
                void SetIsUserAddin (AddinDescription adesc)
 
207
                {
 
208
                        string installPath = database.Registry.DefaultAddinsFolder;
 
209
                        if (installPath [installPath.Length - 1] != Path.DirectorySeparatorChar)
 
210
                                installPath += Path.DirectorySeparatorChar;
 
211
                        isUserAddin = adesc != null && Path.GetFullPath (adesc.AddinFile).StartsWith (installPath);
 
212
                }
 
213
                
 
214
                /// <summary>
 
215
                /// Path to the add-in file (it can be an assembly or a standalone XML manifest)
 
216
                /// </summary>
 
217
                public string AddinFile {
 
218
                        get {
 
219
                                if (sourceFile == null && addin == null)
 
220
                                        LoadAddinInfo ();
 
221
                                return sourceFile;
 
222
                        }
 
223
                }
 
224
                
 
225
                void LoadAddinInfo ()
 
226
                {
 
227
                        if (addin == null) {
 
228
                                try {
 
229
                                        AddinDescription m = Description;
 
230
                                        sourceFile = m.AddinFile;
 
231
                                        addin = AddinInfo.ReadFromDescription (m);
 
232
                                } catch (Exception ex) {
 
233
                                        throw new InvalidOperationException ("Could not read add-in file: " + configFile, ex);
 
234
                                }
 
235
                        }
 
236
                }
 
237
                
 
238
                /// <summary>
 
239
                /// Description of the add-in
 
240
                /// </summary>
 
241
                public AddinDescription Description {
 
242
                        get {
 
243
                                if (desc != null) {
 
244
                                        AddinDescription d = desc.Target as AddinDescription;
 
245
                                        if (d != null)
 
246
                                                return d;
 
247
                                }
 
248
 
 
249
                                AddinDescription m;
 
250
                                database.ReadAddinDescription (new ConsoleProgressStatus (true), configFile, out m);
 
251
                                
 
252
                                if (m == null) {
 
253
                                        try {
 
254
                                                if (File.Exists (configFile)) {
 
255
                                                        // The file is corrupted. Remove it.
 
256
                                                        File.Delete (configFile);
 
257
                                                }
 
258
                                        } catch {
 
259
                                                // Ignore
 
260
                                        }
 
261
                                        throw new InvalidOperationException ("Could not read add-in description");
 
262
                                }
 
263
                                if (addin == null) {
 
264
                                        addin = AddinInfo.ReadFromDescription (m);
 
265
                                        sourceFile = m.AddinFile;
 
266
                                }
 
267
                                SetIsUserAddin (m);
 
268
                                if (!isUserAddin.Value)
 
269
                                        m.Flags |= AddinFlags.CantUninstall;
 
270
                                desc = new WeakReference (m);
 
271
                                return m;
 
272
                        }
 
273
                }
 
274
 
 
275
                internal void ResetCachedData ()
 
276
                {
 
277
                        desc = null;
 
278
                        addin = null;
 
279
                }
 
280
                        
 
281
                /// <summary>
 
282
                /// Compares two add-in versions
 
283
                /// </summary>
 
284
                /// <returns>
 
285
                /// -1 if v1 is greater than v2, 0 if v1 == v2, 1 if v1 less than v2
 
286
                /// </returns>
 
287
                /// <param name='v1'>
 
288
                /// A version
 
289
                /// </param>
 
290
                /// <param name='v2'>
 
291
                /// A version
 
292
                /// </param>
 
293
                public static int CompareVersions (string v1, string v2)
 
294
                {
 
295
                        string[] a1 = v1.Split ('.');
 
296
                        string[] a2 = v2.Split ('.');
 
297
                        
 
298
                        for (int n=0; n<a1.Length; n++) {
 
299
                                if (n >= a2.Length)
 
300
                                        return -1;
 
301
                                if (a1[n].Length == 0) {
 
302
                                        if (a2[n].Length != 0)
 
303
                                                return 1;
 
304
                                        continue;
 
305
                                }
 
306
                                try {
 
307
                                        int n1 = int.Parse (a1[n]);
 
308
                                        int n2 = int.Parse (a2[n]);
 
309
                                        if (n1 < n2)
 
310
                                                return 1;
 
311
                                        else if (n1 > n2)
 
312
                                                return -1;
 
313
                                } catch {
 
314
                                        return 1;
 
315
                                }
 
316
                        }
 
317
                        if (a2.Length > a1.Length)
 
318
                                return 1;
 
319
                        return 0;
 
320
                }
 
321
 
 
322
                /// <summary>
 
323
                /// Returns the identifier of an add-in
 
324
                /// </summary>
 
325
                /// <returns>
 
326
                /// The full identifier.
 
327
                /// </returns>
 
328
                /// <param name='ns'>
 
329
                /// Namespace of the add-in
 
330
                /// </param>
 
331
                /// <param name='id'>
 
332
                /// Name of the add-in
 
333
                /// </param>
 
334
                /// <param name='version'>
 
335
                /// Version of the add-in
 
336
                /// </param>
 
337
                public static string GetFullId (string ns, string id, string version)
 
338
                {
 
339
                        string res;
 
340
                        if (id.StartsWith ("::"))
 
341
                                res = id.Substring (2);
 
342
                        else if (ns != null && ns.Length > 0)
 
343
                                res = ns + "." + id;
 
344
                        else
 
345
                                res = id;
 
346
                        
 
347
                        if (version != null && version.Length > 0)
 
348
                                return res + "," + version;
 
349
                        else
 
350
                                return res;
 
351
                }
 
352
 
 
353
                /// <summary>
 
354
                /// Given a full add-in identifier, returns the namespace and name of the add-in (it removes the version number)
 
355
                /// </summary>
 
356
                /// <param name='addinId'>
 
357
                /// Add-in identifier.
 
358
                /// </param>
 
359
                public static string GetIdName (string addinId)
 
360
                {
 
361
                        int i = addinId.IndexOf (',');
 
362
                        if (i != -1)
 
363
                                return addinId.Substring (0, i);
 
364
                        else
 
365
                                return addinId;
 
366
                }
 
367
                
 
368
                /// <summary>
 
369
                /// Given a full add-in identifier, returns the version the add-in
 
370
                /// </summary>
 
371
                /// <returns>
 
372
                /// The version.
 
373
                /// </returns>
 
374
                public static string GetIdVersion (string addinId)
 
375
                {
 
376
                        int i = addinId.IndexOf (',');
 
377
                        if (i != -1)
 
378
                                return addinId.Substring (i + 1).Trim ();
 
379
                        else
 
380
                                return string.Empty;
 
381
                }
 
382
                
 
383
                /// <summary>
 
384
                /// Splits a full add-in identifier in name and version
 
385
                /// </summary>
 
386
                /// <param name='addinId'>
 
387
                /// Add-in identifier.
 
388
                /// </param>
 
389
                /// <param name='name'>
 
390
                /// The resulting name
 
391
                /// </param>
 
392
                /// <param name='version'>
 
393
                /// The resulting version
 
394
                /// </param>
 
395
                public static void GetIdParts (string addinId, out string name, out string version)
 
396
                {
 
397
                        int i = addinId.IndexOf (',');
 
398
                        if (i != -1) {
 
399
                                name = addinId.Substring (0, i);
 
400
                                version = addinId.Substring (i+1).Trim ();
 
401
                        } else {
 
402
                                name = addinId;
 
403
                                version = string.Empty;
 
404
                        }
 
405
                }
 
406
        }
 
407
}