// // // // // $Revision: 3681 $ // using System; using System.Collections.Generic; using System.IO; namespace ICSharpCode.Core { /// /// Class that helps starting up ICSharpCode.Core. /// /// /// Initializing ICSharpCode.Core requires initializing several static classes /// and the . does this work /// for you, provided you use it like this: /// 1. Create a new CoreStartup instance /// 2. (Optional) Set the values of the properties. /// 3. Call . /// 4. Add "preinstalled" AddIns using /// and . /// 5. (Optional) Call to support /// disabling AddIns and installing external AddIns /// 6. (Optional) Call to support installing /// user AddIns. /// 7. Call . /// public sealed class CoreStartup { List addInFiles = new List(); List disabledAddIns = new List(); bool externalAddInsConfigured; string propertiesName; string configDirectory; string dataDirectory; string applicationName; /// /// Sets the name used for the properties (only name, without path or extension). /// Must be set before StartCoreServices() is called. /// public string PropertiesName { get { return propertiesName; } set { if (value == null || value.Length == 0) throw new ArgumentNullException("value"); propertiesName = value; } } /// /// Sets the directory name used for the property service. /// Must be set before StartCoreServices() is called. /// Use null to use the default path "%ApplicationData%\%ApplicationName%", /// where %ApplicationData% is the system setting for /// "c:\documents and settings\username\application data" /// and %ApplicationName% is the application name you used in the /// CoreStartup constructor call. /// public string ConfigDirectory { get { return configDirectory; } set { configDirectory = value; } } /// /// Sets the data directory used to load resources. /// Must be set before StartCoreServices() is called. /// Use null to use the default path "ApplicationRootPath\data". /// public string DataDirectory { get { return dataDirectory; } set { dataDirectory = value; } } /// /// Creates a new CoreStartup instance. /// /// /// The name of your application. /// This is used as default title for message boxes, /// default name for the configuration directory etc. /// public CoreStartup(string applicationName) { if (applicationName == null) throw new ArgumentNullException("applicationName"); this.applicationName = applicationName; propertiesName = applicationName + "Properties"; MessageService.DefaultMessageBoxTitle = applicationName; MessageService.ProductName = applicationName; } /// /// Find AddIns by searching all .addin files recursively in . /// The found AddIns are added to the list of AddIn files to load. /// public void AddAddInsFromDirectory(string addInDir) { if (addInDir == null) throw new ArgumentNullException("addInDir"); addInFiles.AddRange(FileUtility.SearchDirectory(addInDir, "*.addin")); } /// /// Add the specified .addin file to the list of AddIn files to load. /// public void AddAddInFile(string addInFile) { if (addInFile == null) throw new ArgumentNullException("addInFile"); addInFiles.Add(addInFile); } /// /// Use the specified configuration file to store information about /// disabled AddIns and external AddIns. /// You have to call this method to support the . /// /// /// The name of the file used to store the list of disabled AddIns /// and the list of installed external AddIns. /// A good value for this parameter would be /// Path.Combine(, "AddIns.xml"). /// public void ConfigureExternalAddIns(string addInConfigurationFile) { externalAddInsConfigured = true; AddInManager.ConfigurationFileName = addInConfigurationFile; AddInManager.LoadAddInConfiguration(addInFiles, disabledAddIns); } /// /// Configures user AddIn support. /// /// /// The AddIn installation temporary directory. /// ConfigureUserAddIns will install the AddIns from this directory and /// store the parameter value in . /// /// /// The path where user AddIns are installed to. /// AddIns from this directory will be loaded. /// public void ConfigureUserAddIns(string addInInstallTemp, string userAddInPath) { if (!externalAddInsConfigured) { throw new InvalidOperationException("ConfigureExternalAddIns must be called before ConfigureUserAddIns"); } AddInManager.AddInInstallTemp = addInInstallTemp; AddInManager.UserAddInPath = userAddInPath; if (Directory.Exists(addInInstallTemp)) { AddInManager.InstallAddIns(disabledAddIns); } if (Directory.Exists(userAddInPath)) { AddAddInsFromDirectory(userAddInPath); } } /// /// Initializes the AddIn system. /// This loads the AddIns that were added to the list, /// then it executes the commands /// in /Workspace/Autostart. /// public void RunInitialization() { AddInTree.Load(addInFiles, disabledAddIns); // run workspace autostart commands LoggingService.Info("Running autostart commands..."); foreach (ICommand command in AddInTree.BuildItems("/Workspace/Autostart", null, false)) { try { command.Run(); } catch (Exception ex) { // allow startup to continue if some commands fail MessageService.ShowError(ex); } } } /// /// Starts the core services. /// This initializes the PropertyService and ResourceService. /// public void StartCoreServices() { if (configDirectory == null) configDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), applicationName); PropertyService.InitializeService(configDirectory, dataDirectory ?? Path.Combine(FileUtility.ApplicationRootPath, "data"), propertiesName); PropertyService.Load(); ResourceService.InitializeService(FileUtility.Combine(PropertyService.DataDirectory, "resources")); StringParser.Properties["AppName"] = applicationName; } } }