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

« back to all changes in this revision

Viewing changes to Mono.Addins/Mono.Addins/AddinRegistry.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
// AddinRegistry.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.IO;
 
32
using System.Xml;
 
33
using System.Collections;
 
34
using System.Collections.Specialized;
 
35
using Mono.Addins.Database;
 
36
using Mono.Addins.Description;
 
37
 
 
38
namespace Mono.Addins
 
39
{
 
40
        public class AddinRegistry: IDisposable
 
41
        {
 
42
                AddinDatabase database;
 
43
                StringCollection addinDirs;
 
44
                string basePath;
 
45
                
 
46
                public AddinRegistry (string registryPath): this (registryPath, null)
 
47
                {
 
48
                }
 
49
                
 
50
                internal AddinRegistry (string registryPath, string startupDirectory)
 
51
                {
 
52
                        basePath = Util.GetFullPath (registryPath);
 
53
                        database = new AddinDatabase (this);
 
54
                        addinDirs = new StringCollection ();
 
55
                        addinDirs.Add (Path.Combine (basePath, "addins"));
 
56
                }
 
57
                
 
58
                public static AddinRegistry GetGlobalRegistry ()
 
59
                {
 
60
                        return GetGlobalRegistry (null);
 
61
                }
 
62
                
 
63
                internal static AddinRegistry GetGlobalRegistry (string startupDirectory)
 
64
                {
 
65
                        AddinRegistry reg = new AddinRegistry (GlobalRegistryPath, startupDirectory);
 
66
                        string baseDir;
 
67
                        if (Util.IsWindows)
 
68
                                baseDir = Environment.GetFolderPath (Environment.SpecialFolder.CommonProgramFiles); 
 
69
                        else
 
70
                                baseDir = "/etc";
 
71
                        
 
72
                        reg.AddinDirectories.Add (Path.Combine (baseDir, "mono.addins"));
 
73
                        return reg;
 
74
                }
 
75
                
 
76
                internal static string GlobalRegistryPath {
 
77
                        get {
 
78
                                string customDir = Environment.GetEnvironmentVariable ("MONO_ADDINS_GLOBAL_REGISTRY");
 
79
                                if (customDir != null && customDir.Length > 0)
 
80
                                        return Util.GetFullPath (customDir);
 
81
                                
 
82
                                string path = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData); 
 
83
                                path = Path.Combine (path, "mono.addins");
 
84
                                return Util.GetFullPath (path);
 
85
                        }
 
86
                }
 
87
                
 
88
                public string RegistryPath {
 
89
                        get { return basePath; }
 
90
                }
 
91
                
 
92
                public void Dispose ()
 
93
                {
 
94
                        database.Shutdown ();
 
95
                }
 
96
                
 
97
                public Addin GetAddin (string id)
 
98
                {
 
99
                        return database.GetInstalledAddin (id);
 
100
                }
 
101
                
 
102
                public Addin GetAddin (string id, bool exactVersionMatch)
 
103
                {
 
104
                        return database.GetInstalledAddin (id, exactVersionMatch);
 
105
                }
 
106
                
 
107
                public Addin[] GetAddins ()
 
108
                {
 
109
                        ArrayList list = database.GetInstalledAddins ();
 
110
                        return (Addin[]) list.ToArray (typeof(Addin));
 
111
                }
 
112
                
 
113
                public Addin[] GetAddinRoots ()
 
114
                {
 
115
                        ArrayList list = database.GetAddinRoots ();
 
116
                        return (Addin[]) list.ToArray (typeof(Addin));
 
117
                }
 
118
                
 
119
                public AddinDescription GetAddinDescription (IProgressStatus progressStatus, string file)
 
120
                {
 
121
                        string outFile = Path.GetTempFileName ();
 
122
                        try {
 
123
                                database.ParseAddin (progressStatus, file, outFile, false);
 
124
                        }
 
125
                        catch {
 
126
                                File.Delete (outFile);
 
127
                                throw;
 
128
                        }
 
129
                        
 
130
                        try {
 
131
                                AddinDescription desc = AddinDescription.Read (outFile);
 
132
                                if (desc != null) {
 
133
                                        desc.AddinFile = file;
 
134
                                        desc.OwnerDatabase = database;
 
135
                                }
 
136
                                return desc;
 
137
                        }
 
138
                        catch {
 
139
                                // Errors are already reported using the progress status object
 
140
                                return null;
 
141
                        }
 
142
                        finally {
 
143
                                File.Delete (outFile);
 
144
                        }
 
145
                }
 
146
                
 
147
                public AddinDescription ReadAddinManifestFile (string file)
 
148
                {
 
149
                        AddinDescription desc = AddinDescription.Read (file);
 
150
                        desc.OwnerDatabase = database;
 
151
                        return desc;
 
152
                }
 
153
                
 
154
                public bool IsAddinEnabled (string id)
 
155
                {
 
156
                        return database.IsAddinEnabled (id);
 
157
                }
 
158
                
 
159
                public void EnableAddin (string id)
 
160
                {
 
161
                        database.EnableAddin (id, true);
 
162
                }
 
163
                
 
164
                public void DisableAddin (string id)
 
165
                {
 
166
                        database.DisableAddin (id);
 
167
                }
 
168
                
 
169
                public void DumpFile (string file)
 
170
                {
 
171
                        Mono.Addins.Serialization.BinaryXmlReader.DumpFile (file);
 
172
                }
 
173
                
 
174
                public void ResetConfiguration ()
 
175
                {
 
176
                        database.ResetConfiguration ();
 
177
                }
 
178
 
 
179
                public void Update (IProgressStatus monitor)
 
180
                {
 
181
                        database.Update (monitor);
 
182
                }
 
183
 
 
184
                public void Rebuild (IProgressStatus monitor)
 
185
                {
 
186
                        database.Repair (monitor);
 
187
                }
 
188
                
 
189
                internal Addin GetAddinForHostAssembly (string filePath)
 
190
                {
 
191
                        return database.GetAddinForHostAssembly (filePath);
 
192
                }
 
193
                
 
194
                internal bool AddinDependsOn (string id1, string id2)
 
195
                {
 
196
                        return database.AddinDependsOn (id1, id2);
 
197
                }
 
198
                
 
199
                internal void ScanFolders (IProgressStatus monitor, string folderToScan, StringCollection filesToIgnore)
 
200
                {
 
201
                        database.ScanFolders (monitor, folderToScan, filesToIgnore);
 
202
                }
 
203
                
 
204
                internal void ParseAddin (IProgressStatus progressStatus, string file, string outFile)
 
205
                {
 
206
                        database.ParseAddin (progressStatus, file, outFile, true);
 
207
                }
 
208
                
 
209
                public string DefaultAddinsFolder {
 
210
                        get { return Path.Combine (basePath, "addins"); }
 
211
                }
 
212
                
 
213
                internal StringCollection AddinDirectories {
 
214
                        get { return addinDirs; }
 
215
                }
 
216
                
 
217
                internal bool CreateHostAddinsFile (string hostFile)
 
218
                {
 
219
                        hostFile = Util.GetFullPath (hostFile);
 
220
                        string baseName = Path.GetFileNameWithoutExtension (hostFile);
 
221
                        if (!Directory.Exists (DefaultAddinsFolder))
 
222
                                Directory.CreateDirectory (DefaultAddinsFolder);
 
223
                        
 
224
                        foreach (string s in Directory.GetFiles (DefaultAddinsFolder, baseName + "*.host.addins")) {
 
225
                                try {
 
226
                                        using (StreamReader sr = new StreamReader (s)) {
 
227
                                                XmlTextReader tr = new XmlTextReader (sr);
 
228
                                                tr.MoveToContent ();
 
229
                                                string host = tr.GetAttribute ("host-reference");
 
230
                                                if (host == hostFile)
 
231
                                                        return false;
 
232
                                        }
 
233
                                }
 
234
                                catch {
 
235
                                        // Ignore this file
 
236
                                }
 
237
                        }
 
238
                        
 
239
                        string file = Path.Combine (DefaultAddinsFolder, baseName) + ".host.addins";
 
240
                        int n=1;
 
241
                        while (File.Exists (file)) {
 
242
                                file = Path.Combine (DefaultAddinsFolder, baseName) + "_" + n + ".host.addins";
 
243
                                n++;
 
244
                        }
 
245
                        
 
246
                        using (StreamWriter sw = new StreamWriter (file)) {
 
247
                                XmlTextWriter tw = new XmlTextWriter (sw);
 
248
                                tw.Formatting = Formatting.Indented;
 
249
                                tw.WriteStartElement ("Addins");
 
250
                                tw.WriteAttributeString ("host-reference", hostFile);
 
251
                                tw.WriteElementString ("Directory", Path.GetDirectoryName (hostFile));
 
252
                                tw.WriteEndElement ();
 
253
                                tw.Close ();
 
254
                        }
 
255
                        return true;
 
256
                }
 
257
        }
 
258
}