Logo Search packages:      
Sourcecode: f-spot version File versions

AddinRegistry.cs

//
// 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;

namespace Mono.Addins
{
      public class AddinRegistry: IDisposable
      {
            AddinDatabase database;
            StringCollection addinDirs;
            string basePath;
            
            public AddinRegistry (string registryPath): this (registryPath, null)
            {
            }
            
            internal AddinRegistry (string registryPath, string startupDirectory)
            {
                  basePath = Util.GetFullPath (registryPath);
                  database = new AddinDatabase (this);
                  addinDirs = new StringCollection ();
                  addinDirs.Add (Path.Combine (basePath, "addins"));
            }
            
            public static AddinRegistry GetGlobalRegistry ()
            {
                  return GetGlobalRegistry (null);
            }
            
            internal static AddinRegistry GetGlobalRegistry (string startupDirectory)
            {
                  AddinRegistry reg = new AddinRegistry (GlobalRegistryPath, startupDirectory);
                  string baseDir;
                  if (Util.IsWindows)
                        baseDir = Environment.GetFolderPath (Environment.SpecialFolder.CommonProgramFiles); 
                  else
                        baseDir = "/etc";
                  
                  reg.AddinDirectories.Add (Path.Combine (baseDir, "mono.addins"));
                  return reg;
            }
            
            internal static string GlobalRegistryPath {
                  get {
                        string customDir = Environment.GetEnvironmentVariable ("MONO_ADDINS_GLOBAL_REGISTRY");
                        if (customDir != null && customDir.Length > 0)
                              return Util.GetFullPath (customDir);
                        
                        string path = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData); 
                        path = Path.Combine (path, "mono.addins");
                        return Util.GetFullPath (path);
                  }
            }
            
            public string RegistryPath {
                  get { return basePath; }
            }
            
            public void Dispose ()
            {
                  database.Shutdown ();
            }
            
            public Addin GetAddin (string id)
            {
                  return database.GetInstalledAddin (id);
            }
            
            public Addin GetAddin (string id, bool exactVersionMatch)
            {
                  return database.GetInstalledAddin (id, exactVersionMatch);
            }
            
            public Addin[] GetAddins ()
            {
                  ArrayList list = database.GetInstalledAddins ();
                  return (Addin[]) list.ToArray (typeof(Addin));
            }
            
            public Addin[] GetAddinRoots ()
            {
                  ArrayList list = database.GetAddinRoots ();
                  return (Addin[]) list.ToArray (typeof(Addin));
            }
            
            public AddinDescription GetAddinDescription (IProgressStatus progressStatus, string file)
            {
                  string outFile = Path.GetTempFileName ();
                  try {
                        database.ParseAddin (progressStatus, 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);
                  }
            }
            
            public AddinDescription ReadAddinManifestFile (string file)
            {
                  AddinDescription desc = AddinDescription.Read (file);
                  desc.OwnerDatabase = database;
                  return desc;
            }
            
            public bool IsAddinEnabled (string id)
            {
                  return database.IsAddinEnabled (id);
            }
            
            public void EnableAddin (string id)
            {
                  database.EnableAddin (id, true);
            }
            
            public void DisableAddin (string id)
            {
                  database.DisableAddin (id);
            }
            
            public void DumpFile (string file)
            {
                  Mono.Addins.Serialization.BinaryXmlReader.DumpFile (file);
            }
            
            public void ResetConfiguration ()
            {
                  database.ResetConfiguration ();
            }

            public void Update (IProgressStatus monitor)
            {
                  database.Update (monitor);
            }

            public void Rebuild (IProgressStatus monitor)
            {
                  database.Repair (monitor);
            }
            
            internal Addin GetAddinForHostAssembly (string filePath)
            {
                  return database.GetAddinForHostAssembly (filePath);
            }
            
            internal bool AddinDependsOn (string id1, string id2)
            {
                  return database.AddinDependsOn (id1, id2);
            }
            
            internal void ScanFolders (IProgressStatus monitor, string folderToScan, StringCollection filesToIgnore)
            {
                  database.ScanFolders (monitor, folderToScan, filesToIgnore);
            }
            
            internal void ParseAddin (IProgressStatus progressStatus, string file, string outFile)
            {
                  database.ParseAddin (progressStatus, file, outFile, true);
            }
            
            public string DefaultAddinsFolder {
                  get { return Path.Combine (basePath, "addins"); }
            }
            
            internal StringCollection AddinDirectories {
                  get { return addinDirs; }
            }
            
            internal bool CreateHostAddinsFile (string hostFile)
            {
                  hostFile = Util.GetFullPath (hostFile);
                  string baseName = Path.GetFileNameWithoutExtension (hostFile);
                  if (!Directory.Exists (DefaultAddinsFolder))
                        Directory.CreateDirectory (DefaultAddinsFolder);
                  
                  foreach (string s in Directory.GetFiles (DefaultAddinsFolder, baseName + "*.host.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 (DefaultAddinsFolder, baseName) + ".host.addins";
                  int n=1;
                  while (File.Exists (file)) {
                        file = Path.Combine (DefaultAddinsFolder, baseName) + "_" + n + ".host.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.WriteElementString ("Directory", Path.GetDirectoryName (hostFile));
                        tw.WriteEndElement ();
                        tw.Close ();
                  }
                  return true;
            }
      }
}

Generated by  Doxygen 1.6.0   Back to index