7
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
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.
30
using System.Collections;
33
using System.Xml.Serialization;
34
using System.Collections.Specialized;
35
using Mono.Addins.Description;
36
using Mono.Addins.Database;
50
AddinDatabase database;
51
bool? isLatestVersion;
55
internal Addin (AddinDatabase database, string file)
57
this.database = database;
62
/// Full identifier of the add-in, including namespace and version.
66
if (configFile != null) {
68
id = Path.GetFileNameWithoutExtension (configFile);
71
return this.AddinInfo.Id;
76
/// Namespace of the add-in.
78
public string Namespace {
79
get { return this.AddinInfo.Namespace; }
83
/// Identifier of the add-in (without namespace)
85
public string LocalId {
86
get { return this.AddinInfo.LocalId; }
90
/// Version of the add-in
92
public string Version {
93
get { return this.AddinInfo.Version; }
97
/// Display name of the add-in
100
get { return this.AddinInfo.Name; }
104
/// Custom properties specified in the add-in header
106
public AddinPropertyCollection Properties {
107
get { return this.AddinInfo.Properties; }
110
internal string PrivateDataPath {
111
get { return Path.Combine (database.AddinPrivateDataPath, Path.GetFileNameWithoutExtension (Description.FileName)); }
115
/// Checks version compatibility.
117
/// <param name="version">
118
/// An add-in version.
121
/// True if the provided version is compatible with this add-in.
124
/// This method checks the CompatVersion property to know if the provided version is compatible with the version of this add-in.
126
public bool SupportsVersion (string version)
128
return AddinInfo.SupportsVersion (version);
132
/// Returns a <see cref="System.String"/> that represents the current <see cref="Mono.Addins.Addin"/>.
135
/// A <see cref="System.String"/> that represents the current <see cref="Mono.Addins.Addin"/>.
137
public override string ToString ()
142
internal AddinInfo AddinInfo {
146
addin = AddinInfo.ReadFromDescription (Description);
147
} catch (Exception ex) {
148
throw new InvalidOperationException ("Could not read add-in file: " + configFile, ex);
156
/// Gets or sets the enabled status of the add-in.
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.
166
public bool Enabled {
168
if (!IsLatestVersion)
170
return AddinInfo.IsRoot ? true : database.IsAddinEnabled (Description.Domain, AddinInfo.Id, true);
174
database.EnableAddin (Description.Domain, AddinInfo.Id, true);
176
database.DisableAddin (Description.Domain, AddinInfo.Id);
180
internal bool IsLatestVersion {
182
if (isLatestVersion == null) {
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 ();
188
return isLatestVersion.Value;
191
isLatestVersion = value;
196
/// Returns 'true' if the add-in is installed in the user's personal folder
198
public bool IsUserAddin {
200
if (isUserAddin == null)
201
SetIsUserAddin (Description);
202
return isUserAddin.Value;
206
void SetIsUserAddin (AddinDescription adesc)
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);
215
/// Path to the add-in file (it can be an assembly or a standalone XML manifest)
217
public string AddinFile {
219
if (sourceFile == null && addin == null)
225
void LoadAddinInfo ()
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);
239
/// Description of the add-in
241
public AddinDescription Description {
244
AddinDescription d = desc.Target as AddinDescription;
250
database.ReadAddinDescription (new ConsoleProgressStatus (true), configFile, out m);
254
if (File.Exists (configFile)) {
255
// The file is corrupted. Remove it.
256
File.Delete (configFile);
261
throw new InvalidOperationException ("Could not read add-in description");
264
addin = AddinInfo.ReadFromDescription (m);
265
sourceFile = m.AddinFile;
268
if (!isUserAddin.Value)
269
m.Flags |= AddinFlags.CantUninstall;
270
desc = new WeakReference (m);
275
internal void ResetCachedData ()
282
/// Compares two add-in versions
285
/// -1 if v1 is greater than v2, 0 if v1 == v2, 1 if v1 less than v2
287
/// <param name='v1'>
290
/// <param name='v2'>
293
public static int CompareVersions (string v1, string v2)
295
string[] a1 = v1.Split ('.');
296
string[] a2 = v2.Split ('.');
298
for (int n=0; n<a1.Length; n++) {
301
if (a1[n].Length == 0) {
302
if (a2[n].Length != 0)
307
int n1 = int.Parse (a1[n]);
308
int n2 = int.Parse (a2[n]);
317
if (a2.Length > a1.Length)
323
/// Returns the identifier of an add-in
326
/// The full identifier.
328
/// <param name='ns'>
329
/// Namespace of the add-in
331
/// <param name='id'>
332
/// Name of the add-in
334
/// <param name='version'>
335
/// Version of the add-in
337
public static string GetFullId (string ns, string id, string version)
340
if (id.StartsWith ("::"))
341
res = id.Substring (2);
342
else if (ns != null && ns.Length > 0)
347
if (version != null && version.Length > 0)
348
return res + "," + version;
354
/// Given a full add-in identifier, returns the namespace and name of the add-in (it removes the version number)
356
/// <param name='addinId'>
357
/// Add-in identifier.
359
public static string GetIdName (string addinId)
361
int i = addinId.IndexOf (',');
363
return addinId.Substring (0, i);
369
/// Given a full add-in identifier, returns the version the add-in
374
public static string GetIdVersion (string addinId)
376
int i = addinId.IndexOf (',');
378
return addinId.Substring (i + 1).Trim ();
384
/// Splits a full add-in identifier in name and version
386
/// <param name='addinId'>
387
/// Add-in identifier.
389
/// <param name='name'>
390
/// The resulting name
392
/// <param name='version'>
393
/// The resulting version
395
public static void GetIdParts (string addinId, out string name, out string version)
397
int i = addinId.IndexOf (',');
399
name = addinId.Substring (0, i);
400
version = addinId.Substring (i+1).Trim ();
403
version = string.Empty;