Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AddinRegistry.cs « Mono.Addins « Mono.Addins - github.com/mono/mono-addins.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 94e81239a5438f040759f2ca452cea9932975856 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
//
// AddinRegistry.cs
//
// Author:
//   Lluis Sanchez Gual
//
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//


using System;
using System.IO;
using System.Xml;
using System.Collections;
using System.Collections.Specialized;
using Mono.Addins.Database;
using Mono.Addins.Description;
using System.Collections.Generic;
using System.Linq;

namespace Mono.Addins
{
	/// <summary>
	/// An add-in registry.
	/// </summary>
	/// <remarks>
	/// An add-in registry is a data structure used by the add-in engine to locate add-ins to load.
	/// 
	/// A registry can be configured to look for add-ins in several directories. However, add-ins
	/// copied to those directories won't be detected until an explicit add-in scan is requested.
	/// The registry can be updated by an application by calling Registry.Update(), or by a user by
	/// running the 'mautil' add-in setup tool.
	/// 
	/// The registry has information about the location of every add-in and a timestamp of the last
	/// check, so the Update method will only scan new or modified add-ins. An application can
	/// add a call to Registry.Update() in the Main method to detect all new add-ins every time the
	/// app is started.
	/// 
	/// Every add-in added to the registry is parsed and validated, and if there is any error it
	/// will be rejected. The registry is also in charge of scanning the add-in assemblies and look
	/// for extensions and other information declared using custom attributes. That information is
	/// merged with the manifest information (if there is one) to create a complete add-in
	/// description ready to be used at run-time.
	/// 
	/// Mono.Addins allows sharing an add-in registry among several applications. In this context,
	/// all applications sharing the registry share the same extension point model, and it is
	/// possible to implement add-ins which extend several hosts.
	/// </remarks>
	public class AddinRegistry: IDisposable
	{
		readonly AddinDatabase database;
		readonly string [] addinDirs;
		readonly string basePath;
		readonly string startupDirectory;
		readonly string addinsDir;
		readonly string databaseDir;
		string currentDomain;
		
		/// <summary>
		/// Initializes a new instance.
		/// </summary>
		/// <param name="registryPath">
		/// Location of the add-in registry.
		/// </param>
		/// <remarks>
		/// Creates a new add-in registry located in the provided path.
		/// The add-in registry will look for add-ins in an 'addins'
		/// subdirectory of the provided registryPath.
		/// 
		/// When specifying a path, it is possible to use a special folder name as root.
		/// For example: [Personal]/.config/MyApp. In this case, [Personal] will be replaced
		/// by the location of the Environment.SpecialFolder.Personal folder. Any value
		/// of the Environment.SpecialFolder enumeration can be used (always between square
		/// brackets)
		/// </remarks>
		public AddinRegistry (string registryPath): this (null, registryPath, null, null, null, null)
		{
		}
		
		/// <summary>
		/// Initializes a new instance.
		/// </summary>
		/// <param name="registryPath">
		/// Location of the add-in registry.
		/// </param>
		/// <param name="startupDirectory">
		/// Location of the application.
		/// </param>
		/// <remarks>
		/// Creates a new add-in registry located in the provided path.
		/// The add-in registry will look for add-ins in an 'addins'
		/// subdirectory of the provided registryPath.
		/// 
		/// When specifying a path, it is possible to use a special folder name as root.
		/// For example: [Personal]/.config/MyApp. In this case, [Personal] will be replaced
		/// by the location of the Environment.SpecialFolder.Personal folder. Any value
		/// of the Environment.SpecialFolder enumeration can be used (always between square
		/// brackets)
		/// </remarks>
		public AddinRegistry (string registryPath, string startupDirectory): this (null, registryPath, startupDirectory, null, null, null)
		{
		}
		
		/// <summary>
		/// Initializes a new instance of the <see cref="Mono.Addins.AddinRegistry"/> class.
		/// </summary>
		/// <param name='registryPath'>
		/// Location of the add-in registry.
		/// </param>
		/// <param name='startupDirectory'>
		/// Location of the application.
		/// </param>
		/// <param name='addinsDir'>
		/// Add-ins directory. If the path is relative, it is considered to be relative
		/// to the configDir directory.
		/// </param>
		/// <remarks>
		/// Creates a new add-in registry located in the provided path.
		/// Configuration information about the add-in registry will be stored in
		/// 'registryPath'. The add-in registry will look for add-ins in the provided
		/// 'addinsDir' directory.
		/// 
		/// When specifying a path, it is possible to use a special folder name as root.
		/// For example: [Personal]/.config/MyApp. In this case, [Personal] will be replaced
		/// by the location of the Environment.SpecialFolder.Personal folder. Any value
		/// of the Environment.SpecialFolder enumeration can be used (always between square
		/// brackets)
		/// </remarks>
		public AddinRegistry (string registryPath, string startupDirectory, string addinsDir): this (null, registryPath, startupDirectory, addinsDir, null, null)
		{
		}
		
		/// <summary>
		/// Initializes a new instance of the <see cref="Mono.Addins.AddinRegistry"/> class.
		/// </summary>
		/// <param name='registryPath'>
		/// Location of the add-in registry.
		/// </param>
		/// <param name='startupDirectory'>
		/// Location of the application.
		/// </param>
		/// <param name='addinsDir'>
		/// Add-ins directory. If the path is relative, it is considered to be relative
		/// to the configDir directory.
		/// </param>
		/// <param name='databaseDir'>
		/// Location of the add-in database. If the path is relative, it is considered to be relative
		/// to the configDir directory.
		/// </param>
		/// <remarks>
		/// Creates a new add-in registry located in the provided path.
		/// Configuration information about the add-in registry will be stored in
		/// 'registryPath'. The add-in registry will look for add-ins in the provided
		/// 'addinsDir' directory. Cached information about add-ins will be stored in
		/// the 'databaseDir' directory.
		/// 
		/// When specifying a path, it is possible to use a special folder name as root.
		/// For example: [Personal]/.config/MyApp. In this case, [Personal] will be replaced
		/// by the location of the Environment.SpecialFolder.Personal folder. Any value
		/// of the Environment.SpecialFolder enumeration can be used (always between square
		/// brackets)
		/// </remarks>
		public AddinRegistry (string registryPath, string startupDirectory, string addinsDir, string databaseDir): this (null, registryPath, startupDirectory, addinsDir, databaseDir, null)
		{
		}
		
		internal AddinRegistry (AddinEngine engine, string registryPath, string startupDirectory, string addinsDir, string databaseDir, string additionalGlobalAddinDirectory)
		{
			basePath = Path.GetFullPath (Util.NormalizePath (registryPath));
			
			if (addinsDir != null) {
				addinsDir = Util.NormalizePath (addinsDir);
				if (Path.IsPathRooted (addinsDir))
					this.addinsDir = Path.GetFullPath (addinsDir);
				else
					this.addinsDir = Path.GetFullPath (Path.Combine (basePath, addinsDir));
			} else
				this.addinsDir = Path.Combine (basePath, "addins");
			
			if (databaseDir != null) {
				databaseDir = Util.NormalizePath (databaseDir);
				if (Path.IsPathRooted (databaseDir))
					this.databaseDir = Path.GetFullPath (databaseDir);
				else
					this.databaseDir = Path.GetFullPath (Path.Combine (basePath, databaseDir));
			}
			else
				this.databaseDir = Path.GetFullPath (basePath);

			// Look for add-ins in the hosts directory and in the default
			// addins directory
			if (additionalGlobalAddinDirectory != null)
				addinDirs = new [] { DefaultAddinsFolder, additionalGlobalAddinDirectory };
			else
				addinDirs = new [] { DefaultAddinsFolder };
			
			// Initialize the database after all paths have been set
			database = new AddinDatabase (engine, this);
			
			// Get the domain corresponding to the startup folder
			if (startupDirectory != null && startupDirectory.Length > 0) {
				this.startupDirectory = Util.NormalizePath (startupDirectory);
				currentDomain = database.GetFolderDomain (null, this.startupDirectory);
			} else
				currentDomain = AddinDatabase.GlobalDomain;
		}

		/// <summary>
		/// Gets the global registry.
		/// </summary>
		/// <returns>
		/// The global registry
		/// </returns>
		/// <remarks>
		/// The global add-in registry is created in "~/.config/mono.addins",
		/// and it is the default registry used when none is specified.
		/// </remarks>
		public static AddinRegistry GetGlobalRegistry ()
		{
			return GetGlobalRegistry (null, null);
		}
		
		internal static AddinRegistry GetGlobalRegistry (AddinEngine engine, string startupDirectory)
		{
			string baseDir;
			if (Util.IsWindows)
				baseDir = Environment.GetFolderPath (Environment.SpecialFolder.CommonProgramFiles); 
			else
				baseDir = "/etc";

			var globalDir = Path.Combine (baseDir, "mono.addins");

			return new AddinRegistry (engine, GlobalRegistryPath, startupDirectory, null, null, globalDir);
		}
		
		internal bool UnknownDomain {
			get { return currentDomain == AddinDatabase.UnknownDomain; }
		}
		
		internal static string GlobalRegistryPath {
			get {
				string customDir = Environment.GetEnvironmentVariable ("MONO_ADDINS_GLOBAL_REGISTRY");
				if (customDir != null && customDir.Length > 0)
					return Path.GetFullPath (Util.NormalizePath (customDir));
				
				string path = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData); 
				path = Path.Combine (path, "mono.addins");
				return Path.GetFullPath (path);
			}
		}
		
		internal string CurrentDomain {
			get { return currentDomain; }
		}
		
		/// <summary>
		/// Location of the add-in registry.
		/// </summary>
		public string RegistryPath {
			get { return basePath; }
		}
		
		/// <summary>
		/// Disposes the add-in engine.
		/// </summary>
		public void Dispose ()
		{
			database.Shutdown ();
		}
		
		/// <summary>
		/// Returns an add-in from the registry.
		/// </summary>
		/// <param name="id">
		/// Identifier of the add-in.
		/// </param>
		/// <returns>
		/// The add-in, or 'null' if not found.
		/// </returns>
		/// <remarks>
		/// The add-in identifier may optionally include a version number, for example: "TextEditor.Xml,1.2"
		/// </remarks>
		public Addin GetAddin (string id)
		{
			if (currentDomain == AddinDatabase.UnknownDomain)
				return null;
			Addin ad = database.GetInstalledAddin (currentDomain, id);
			if (ad != null && IsRegisteredForUninstall (ad.Id))
				return null;
			return ad;
		}
		
		/// <summary>
		/// Returns an add-in from the registry.
		/// </summary>
		/// <param name="id">
		/// Identifier of the add-in.
		/// </param>
		/// <param name="exactVersionMatch">
		/// 'true' if the exact add-in version must be found.
		/// </param>
		/// <returns>
		/// The add-in, or 'null' if not found.
		/// </returns>
		/// <remarks>
		/// The add-in identifier may optionally include a version number, for example: "TextEditor.Xml,1.2".
		/// In this case, if the exact version is not found and exactVersionMatch is 'false', it will
		/// return one than is compatible with the required version.
		/// </remarks>
		public Addin GetAddin (string id, bool exactVersionMatch)
		{
			if (currentDomain == AddinDatabase.UnknownDomain)
				return null;
			Addin ad = database.GetInstalledAddin (currentDomain, id, exactVersionMatch);
			if (ad != null && IsRegisteredForUninstall (ad.Id))
				return null;
			return ad;
		}
		
		/// <summary>
		/// Gets all add-ins or add-in roots registered in the registry.
		/// </summary>
		/// <returns>
		/// The addins.
		/// </returns>
		/// <param name='flags'>
		/// Flags.
		/// </param>
		public Addin[] GetModules (AddinSearchFlags flags)
		{
			if (currentDomain == AddinDatabase.UnknownDomain)
				return new Addin [0];
			AddinSearchFlagsInternal f = (AddinSearchFlagsInternal)(int)flags;
			return database.GetInstalledAddins (currentDomain, f | AddinSearchFlagsInternal.ExcludePendingUninstall).ToArray ();
		}
		
		/// <summary>
		/// Gets all add-ins registered in the registry.
		/// </summary>
		/// <returns>
		/// Add-ins registered in the registry.
		/// </returns>
		public Addin[] GetAddins ()
		{
			return GetModules (AddinSearchFlags.IncludeAddins);
		}
		
		/// <summary>
		/// Gets all add-in roots registered in the registry.
		/// </summary>
		/// <returns>
		/// Descriptions of all add-in roots.
		/// </returns>
		public Addin[] GetAddinRoots ()
		{
			return GetModules (AddinSearchFlags.IncludeRoots);
		}
		
		/// <summary>
		/// Loads an add-in description
		/// </summary>
		/// <param name="progressStatus">
		/// Progress tracker.
		/// </param>
		/// <param name="file">
		/// Name of the file to load
		/// </param>
		/// <returns>
		/// An add-in description
		/// </returns>
		/// <remarks>
		/// This method loads an add-in description from a file. The file can be an XML manifest or an
		/// assembly that implements an add-in.
		/// </remarks>
		public AddinDescription GetAddinDescription (IProgressStatus progressStatus, string file)
		{
			if (currentDomain == AddinDatabase.UnknownDomain)
				return null;
			string outFile = Path.GetTempFileName ();
			try {
				database.ParseAddin (progressStatus, currentDomain, file, outFile, false);
			}
			catch {
				File.Delete (outFile);
				throw;
			}
			
			try {
				AddinDescription desc = AddinDescription.Read (outFile);
				if (desc != null) {
					desc.AddinFile = file;
					desc.OwnerDatabase = database;
				}
				return desc;
			}
			catch {
				// Errors are already reported using the progress status object
				return null;
			}
			finally {
				File.Delete (outFile);
			}
		}
		
		/// <summary>
		/// Reads an XML add-in manifest
		/// </summary>
		/// <param name="file">
		/// Path to the XML file
		/// </param>
		/// <returns>
		/// An add-in description
		/// </returns>
		public AddinDescription ReadAddinManifestFile (string file)
		{
			AddinDescription desc = AddinDescription.Read (file);
			if (currentDomain != AddinDatabase.UnknownDomain) {
				desc.OwnerDatabase = database;
				desc.Domain = currentDomain;
			}
			return desc;
		}

		/// <summary>
		/// Reads an XML add-in manifest
		/// </summary>
		/// <param name="reader">
		/// Reader that contains the XML
		/// </param>
		/// <param name="baseFile">
		/// Base path to use to discover add-in files
		/// </param>
		/// <returns>
		/// An add-in description
		/// </returns>
		public AddinDescription ReadAddinManifestFile (TextReader reader, string baseFile)
		{
			if (currentDomain == AddinDatabase.UnknownDomain)
				return null;
			AddinDescription desc = AddinDescription.Read (reader, baseFile);
			desc.OwnerDatabase = database;
			desc.Domain = currentDomain;
			return desc;
		}
		
		/// <summary>
		/// Checks whether an add-in is enabled.
		/// </summary>
		/// <param name="id">
		/// Identifier of the add-in.
		/// </param>
		/// <returns>
		/// 'true' if the add-in is enabled.
		/// </returns>
		public bool IsAddinEnabled (string id)
		{
			if (currentDomain == AddinDatabase.UnknownDomain)
				return false;
			return database.IsAddinEnabled (currentDomain, id);
		}
		
		/// <summary>
		/// Enables an add-in.
		/// </summary>
		/// <param name="id">
		/// Identifier of the add-in
		/// </param>
		/// <remarks>
		/// If the enabled add-in depends on other add-ins which are disabled,
		/// those will automatically be enabled too.
		/// </remarks>
		public void EnableAddin (string id)
		{
			if (currentDomain == AddinDatabase.UnknownDomain)
				return;
			database.EnableAddin (currentDomain, id, true);
		}
		
		/// <summary>
		/// Disables an add-in.
		/// </summary>
		/// <param name="id">
		/// Identifier of the add-in.
		/// </param>
		/// <remarks>
		/// When an add-in is disabled, all extension points it defines will be ignored
		/// by the add-in engine. Other add-ins which depend on the disabled add-in will
		/// also automatically be disabled.
		/// </remarks>
		public void DisableAddin (string id)
		{
			if (currentDomain == AddinDatabase.UnknownDomain)
				return;
			database.DisableAddin (currentDomain, id);
		}

		/// <summary>
		/// Disables an add-in.
		/// </summary>
		/// <param name="id">
		/// Identifier of the add-in.
		/// </param>
		/// <param name="exactVersionMatch">
		/// If true, it disables the add-in that exactly matches the provided version. If false, it disables
		/// all versions of add-ins with the same Id
		/// </param>
		/// <remarks>
		/// When an add-in is disabled, all extension points it defines will be ignored
		/// by the add-in engine. Other add-ins which depend on the disabled add-in will
		/// also automatically be disabled.
		/// </remarks>
		public void DisableAddin (string id, bool exactVersionMatch)
		{
			if (currentDomain == AddinDatabase.UnknownDomain)
				return;
			database.DisableAddin (currentDomain, id, exactVersionMatch);
		}

		/// <summary>
		/// Registers a set of add-ins for uninstallation.
		/// </summary>
		/// <param name='id'>
		/// Identifier of the add-in
		/// </param>
		/// <param name='files'>
		/// Files to be uninstalled
		/// </param>
		/// <remarks>
		/// This method can be used to instruct the add-in manager to uninstall
		/// an add-in the next time the registry is updated. This is useful
		/// when an add-in manager can't delete an add-in because if it is
		/// loaded.
		/// </remarks>
		public void RegisterForUninstall (string id, IEnumerable<string> files)
		{
			database.RegisterForUninstall (currentDomain, id, files);
		}
		
		/// <summary>
		/// Determines whether an add-in is registered for uninstallation
		/// </summary>
		/// <returns>
		/// <c>true</c> if the add-in is registered for uninstallation
		/// </returns>
		/// <param name='addinId'>
		/// Identifier of the add-in
		/// </param>
		public bool IsRegisteredForUninstall (string addinId)
		{
			return database.IsRegisteredForUninstall (currentDomain, addinId);
		}
		
		/// <summary>
		/// Gets a value indicating whether there are pending add-ins to be uninstalled installed
		/// </summary>
		public bool HasPendingUninstalls {
			get { return database.HasPendingUninstalls (currentDomain); }
		}
		
		/// <summary>
		/// Internal use only
		/// </summary>
		public void DumpFile (string file)
		{
			Mono.Addins.Serialization.BinaryXmlReader.DumpFile (file);
		}
		
		/// <summary>
		/// Resets the configuration files of the registry
		/// </summary>
		public void ResetConfiguration ()
		{
			database.ResetConfiguration ();
		}
		
		internal void NotifyDatabaseUpdated ()
		{
			if (startupDirectory != null)
				currentDomain = database.GetFolderDomain (null, startupDirectory);
		}

		/// <summary>
		/// Updates the add-in registry.
		/// </summary>
		/// <remarks>
		/// This method must be called after modifying, installing or uninstalling add-ins.
		/// 
		/// When calling Update, every add-in added to the registry is parsed and validated,
		/// and if there is any error it will be rejected. It will also cache add-in information
		/// needed at run-time.
		/// 
		/// If during the update operation the registry finds new add-ins or detects that some
		/// add-ins have been deleted, the loaded extension points will be updated to include
		/// or exclude extension nodes from those add-ins.
		/// </remarks>
		public void Update ()
		{
			Update (new ConsoleProgressStatus (false));
		}

		/// <summary>
		/// Updates the add-in registry.
		/// </summary>
		/// <param name="monitor">
		/// Progress monitor to keep track of the update operation.
		/// </param>
		/// <remarks>
		/// This method must be called after modifying, installing or uninstalling add-ins.
		/// 
		/// When calling Update, every add-in added to the registry is parsed and validated,
		/// and if there is any error it will be rejected. It will also cache add-in information
		/// needed at run-time.
		/// 
		/// If during the update operation the registry finds new add-ins or detects that some
		/// add-ins have been deleted, the loaded extension points will be updated to include
		/// or exclude extension nodes from those add-ins.
		/// </remarks>
		public void Update (IProgressStatus monitor)
		{
			database.Update (monitor, currentDomain);
		}

		/// <summary>
		/// Regenerates the cached data of the add-in registry.
		/// </summary>
		/// <param name="monitor">
		/// Progress monitor to keep track of the rebuild operation.
		/// </param>
		public void Rebuild (IProgressStatus monitor)
		{
			var context = new ScanOptions ();
			context.CleanGeneratedAddinScanDataFiles = true;
			database.Repair (monitor, currentDomain, context);

			// A full rebuild may cause the domain to change
			if (!string.IsNullOrEmpty (startupDirectory))
				currentDomain = database.GetFolderDomain (null, startupDirectory);
		}

		/// <summary>
		/// Generates add-in data cache files for add-ins in the provided folder
		/// and any other directory included through a .addins file.
		/// If folder is not provided, it scans the startup directory.
		/// </summary>
		/// <param name="monitor">
		/// Progress monitor to keep track of the rebuild operation.
		/// </param>
		/// <param name="folder">
		/// Folder that contains the add-ins to be scanned.
		/// </param>
		/// <param name="recursive">
		/// If true, sub-directories are scanned recursively
		/// </param>
		public void GenerateAddinScanDataFiles (IProgressStatus monitor, string folder = null, bool recursive = false)
		{
			database.GenerateScanDataFiles (monitor, folder ?? StartupDirectory, recursive);
		}

		/// <summary>
		/// Registers an extension. Only AddinFileSystemExtension extensions are supported right now.
		/// </summary>
		/// <param name='extension'>
		/// The extension to register
		/// </param>
		public void RegisterExtension (object extension)
		{
			database.RegisterExtension (extension);
		}
		
		/// <summary>
		/// Unregisters an extension.
		/// </summary>
		/// <param name='extension'>
		/// The extension to unregister
		/// </param>
		public void UnregisterExtension (object extension)
		{
			database.UnregisterExtension (extension);
		}
		
		internal void CopyExtensionsFrom (AddinRegistry other)
		{
			database.CopyExtensions (other.database);
		}
		
		internal Addin GetAddinForHostAssembly (string filePath)
		{
			if (currentDomain == AddinDatabase.UnknownDomain)
				return null;
			return database.GetAddinForHostAssembly (currentDomain, filePath);
		}
		
		internal bool AddinDependsOn (string id1, string id2)
		{
			return database.AddinDependsOn (currentDomain, id1, id2);
		}
		
		internal void ScanFolders (IProgressStatus monitor, string folderToScan, ScanOptions context)
		{
			database.ScanFolders (monitor, currentDomain, folderToScan, context);
		}
		
		internal void GenerateScanDataFilesInProcess (IProgressStatus monitor, string folderToScan, bool recursive)
		{
			database.GenerateScanDataFilesInProcess (monitor, folderToScan, recursive);
		}
		
		internal void ParseAddin (IProgressStatus progressStatus, string file, string outFile)
		{
			database.ParseAddin (progressStatus, currentDomain, file, outFile, true);
		}
		
		/// <summary>
		/// Gets the default add-ins folder of the registry.
		/// </summary>
		/// <remarks>
		/// For every add-in registry there is an add-in folder where the registry will look for add-ins by default.
		/// This folder is an "addins" subdirectory of the directory where the repository is located. In most cases,
		/// this folder will only contain .addins files referencing other more convenient locations for add-ins.
		/// </remarks>
		public string DefaultAddinsFolder {
			get { return addinsDir; }
		}
		
		internal string AddinCachePath {
			get { return databaseDir; }
		}
	
		internal IEnumerable<string> GlobalAddinDirectories {
			get { return addinDirs; }
		}

		internal void RegisterGlobalAddinDirectory (string dir)
		{

		}

		internal string StartupDirectory {
			get {
				return startupDirectory;
			}
		}
		
		internal bool CreateHostAddinsFile (string hostFile)
		{
			hostFile = Path.GetFullPath (hostFile);
			string baseName = Path.GetFileNameWithoutExtension (hostFile);
			if (!Directory.Exists (database.HostsPath))
				Directory.CreateDirectory (database.HostsPath);
			
			foreach (string s in Directory.EnumerateFiles (database.HostsPath, baseName + "*.addins")) {
				try {
					using (StreamReader sr = new StreamReader (s)) {
						XmlTextReader tr = new XmlTextReader (sr);
						tr.MoveToContent ();
						string host = tr.GetAttribute ("host-reference");
						if (host == hostFile)
							return false;
					}
				}
				catch {
					// Ignore this file
				}
			}
			
			string file = Path.Combine (database.HostsPath, baseName) + ".addins";
			int n=1;
			while (File.Exists (file)) {
				file = Path.Combine (database.HostsPath, baseName) + "_" + n + ".addins";
				n++;
			}
			
			using (StreamWriter sw = new StreamWriter (file)) {
				XmlTextWriter tw = new XmlTextWriter (sw);
				tw.Formatting = Formatting.Indented;
				tw.WriteStartElement ("Addins");
				tw.WriteAttributeString ("host-reference", hostFile);
				tw.WriteStartElement ("Directory");
				tw.WriteAttributeString ("shared", "false");
				tw.WriteString (Path.GetDirectoryName (hostFile));
				tw.WriteEndElement ();
				tw.Close ();
			}
			return true;
		}
		
#pragma warning disable 1591
		[Obsolete]
		public static string[] GetRegisteredStartupFolders (string registryPath)
		{
			string dbDir = Path.Combine (registryPath, "addin-db-" + AddinDatabase.VersionTag);
			dbDir = Path.Combine (dbDir, "hosts");
			
			if (!Directory.Exists (dbDir))
				return new string [0];
			
			ArrayList dirs = new ArrayList ();
			
			foreach (string s in Directory.GetFiles (dbDir, "*.addins")) {
				try {
					using (StreamReader sr = new StreamReader (s)) {
						XmlTextReader tr = new XmlTextReader (sr);
						tr.MoveToContent ();
						string host = tr.GetAttribute ("host-reference");
						host = Path.GetDirectoryName (host);
						if (!dirs.Contains (host))
							dirs.Add (host);
					}
				}
				catch {
					// Ignore this file
				}
			}
			return (string[]) dirs.ToArray (typeof(string));
		}
#pragma warning restore 1591
	}
	
	/// <summary>
	/// Addin search flags.
	/// </summary>
	[Flags]
	public enum AddinSearchFlags
	{
		/// <summary>
		/// Add-ins are included in the search
		/// </summary>
		IncludeAddins = 1,
		/// <summary>
		/// Add-in roots are included in the search
		/// </summary>
		IncludeRoots = 1 << 1,
		/// <summary>
		/// Both add-in and add-in roots are included in the search
		/// </summary>
		IncludeAll = IncludeAddins | IncludeRoots,
		/// <summary>
		/// Only the latest version of every add-in or add-in root is included in the search
		/// </summary>
		LatestVersionsOnly = 1 << 3
	}
}