Example #1
0
        /// <summary>
        /// Attempts to increment the RunCount option for the specified version of the specified Type
        /// </summary>
        /// <param name="configuration">The configuration in which the option will be found</param>
        /// <param name="type">The Type for which the RunCount value will be incremented</param>
        /// <returns></returns>
        public static bool IncrementRunCountForVersionOfType(XmlConfiguration configuration, Type type, out DateTime installDate, out int runCount)
        {
            installDate = DateTime.Now;
            runCount    = 0;
            try
            {
                XmlConfigurationCategory category = InstallationEngine.GetExistingCategoryForTypeVersion(configuration, type, CategoryNames.Installed);
                if (category != null)
                {
                    XmlConfigurationOption option = null;

                    option = category.Options[@"InstallDate"];
                    if (option != null)
                    {
                        installDate = (DateTime)option.Value;
                    }

                    option = category.Options[@"RunCount"];
                    if (option != null)
                    {
                        // retrieve, increment, and set the run count
                        runCount = (int)option.Value;
                        runCount++;
                        option.Value = runCount;
                    }
                }
            }
            catch (System.Exception systemException)
            {
                System.Diagnostics.Trace.WriteLine(systemException);
            }
            return(false);
        }
        /// <summary>
        /// Reads properties for the window from the configuration. Properties include Size, Location, and WindowState.
        /// </summary>
        /// <param name="window">The window whose properties were previously persisted</param>
        /// <param name="configuration">The configuration to read from</param>
        public static void Read(Form window, XmlConfiguration configuration)
        {
            if (window != null)
            {
                if (configuration != null)
                {
                    string categoryName = @"Persisted Objects\Windows\" + window.GetType().FullName;
                    XmlConfigurationCategory category = null;

                    // load the category
                    if ((category = configuration.Categories[categoryName]) != null)
                    {
                        XmlConfigurationOption option = null;

                        // load the location
                        if ((option = category.Options["Location"]) != null)
                        {
                            window.Location = (Point)option.Value;
                        }

                        if ((option = category.Options["Size"]) != null)
                        {
                            window.Size = (Size)option.Value;
                        }

                        if ((option = category.Options["WindowState"]) != null)
                        {
                            window.WindowState = (FormWindowState)option.Value;
                        }
                    }
                }
            }
        }
Example #3
0
        /// <summary>
        /// Writes the data for the window we are listening to, to the appropriate category in the specified configuration
        /// </summary>
        private void WriteWindowData()
        {
            try
            {
                XmlConfigurationEventArgs e = new XmlConfigurationEventArgs(null, XmlConfigurationElementActions.None);
                this.OnNeedsConfiguration(this, e);
                if (e.Element == null)
                {
                    return;
                }

                // retrive the configuration where this listener will read and write
                XmlConfiguration configuration = e.Element;
                if (configuration != null)
                {
                    XmlConfigurationCategory category = configuration.Categories[_path, true];
                    if (category != null)
                    {
                        XmlConfigurationOption option = null;

                        if ((option = category.Options["Size"]) == null)
                        {
                            option                      = category.Options["Size", true, _size];
                            option.Description          = "The size of the window (Width & Height)";
                            option.Category             = "Layout";
                            option.ShouldSerializeValue = true;
                        }
                        option.Value = _size;

                        if ((option = category.Options["Location"]) == null)
                        {
                            option                      = category.Options["Location", true, _location];
                            option.Description          = "The location of the top left corner of the window (Left & Top)";
                            option.Category             = "Layout";
                            option.ShouldSerializeValue = true;
                        }
                        option.Value = _location;

                        if ((option = category.Options["WindowState"]) == null)
                        {
                            option             = category.Options["WindowState", true, _state];
                            option.Description = "The state of the window (Normal, Maximized, or Minimized)";
                            option.Category    = "Layout";
//							option.ShouldSerializeValue = true;
                        }
                        option.Value = _state;
                    }
                }
            }
            catch (System.Exception systemException)
            {
                System.Diagnostics.Trace.WriteLine(systemException);
            }
        }
        /// <summary>
        /// Writes properties of the window to the configuration. Properties include Size, Location, and WindowState.
        /// </summary>
        /// <param name="window">The window whose properties will be written with the following pameters</param>
        /// <param name="size">The size of the window</param>
        /// <param name="location">The location of the window</param>
        /// <param name="windowState">The state of the window</param>
        /// <param name="configuration">The configuration to write to</param>
        public static void Write(Form window, Size size, Point location, FormWindowState windowState, XmlConfiguration configuration)
        {
            if (window != null)
            {
                if (configuration != null)
                {
                    string categoryName = @"Persisted Objects\Windows\" + window.GetType().FullName;
                    XmlConfigurationCategory category = null;

                    // load the category
                    if ((category = configuration.Categories[categoryName, false]) == null)
                    {
                        category = configuration.Categories[categoryName, true];
//						category.Hidden = true;
                    }

                    XmlConfigurationOption option = null;

                    // load the location
                    if ((option = category.Options["Location", false]) == null)
                    {
                        option                      = category.Options["Location", true, window.Location];
                        option.Category             = "Layout";
                        option.Description          = "The coordinates of the upper-left corner of the window relative to it's container.";
                        option.ShouldSerializeValue = true;
                    }
                    option.Value = location;

                    if ((option = category.Options["Size", false]) == null)
                    {
                        option                      = category.Options["Size", true, window.Size];
                        option.Category             = "Layout";
                        option.Description          = "The size of the window.";
                        option.ShouldSerializeValue = true;
                    }
                    option.Value = size;

                    if ((option = category.Options["WindowState", false]) == null)
                    {
                        option             = category.Options["WindowState", true, window.WindowState];
                        option.Category    = "Layout";
                        option.Description = "The state of the window (ie. Maximized, Minimized, Normal).";
//						option.ShouldSerializeValue = true;
                    }
                    option.Value = windowState;
                }
            }
        }
Example #5
0
        /// <summary>
        /// Reads the window data from the configuration
        /// </summary>
        private void ReadWindowData()
        {
            try
            {
                XmlConfigurationEventArgs e = new XmlConfigurationEventArgs(null, XmlConfigurationElementActions.None);
                this.OnNeedsConfiguration(this, e);
                if (e.Element == null)
                {
                    return;
                }

                // retrive the configuration where this listener will read and write
                XmlConfiguration configuration = e.Element;
                if (configuration != null)
                {
                    XmlConfigurationCategory category = configuration.Categories[_path, false];
                    if (category != null)
                    {
                        XmlConfigurationOption option = null;

                        if ((option = category.Options["Size"]) != null)
                        {
                            _size = (Size)option.Value;
                        }

                        if ((option = category.Options["Location"]) != null)
                        {
                            _location = (Point)option.Value;
                        }

                        if ((option = category.Options["WindowState"]) != null)
                        {
                            _state = (FormWindowState)option.Value;
                        }
                    }
                }
            }
            catch (System.Exception systemException)
            {
                System.Diagnostics.Trace.WriteLine(systemException);
            }
        }
Example #6
0
        /// <summary>
        /// Creates an installation record for the specified Type
        /// </summary>
        /// <param name="configuration">The configuration in which the category will be created</param>
        /// <param name="type">The type for which the category will be created</param>
        /// <returns></returns>
        public static XmlConfigurationCategory InstallVersionOfType(XmlConfiguration configuration, Type type, out DateTime installDate)
        {
            installDate = DateTime.Now;

            // remove the uninstall entry for the type
            InstallationEngine.RemoveUninstalledEntryForType(configuration, type);

            // create the install entry for the type
            XmlConfigurationCategory category = InstallationEngine.CreateCategoryForTypeVersion(configuration, type, CategoryNames.Installed);

            if (category != null)
            {
                XmlConfigurationOption option = null;

                // create the install date
                option = category.Options[@"InstallDate"];
                if (option == null)
                {
                    option                      = category.Options[@"InstallDate", true, installDate];
                    option.Category             = @"Installation Notes";
                    option.Description          = @"This date marks the date and time on which the SnapIn was installed.";
                    option.ShouldSerializeValue = true;
                }
                option = null;

                // create the run count
                option = category.Options[@"RunCount"];
                if (option == null)
                {
                    option             = category.Options[@"RunCount", true, 0];
                    option.Category    = @"Installation Notes";
                    option.Description = @"This number indicates the number of times the SnapIn has executed.";
                }
                option = null;
            }
            return(category);
        }
Example #7
0
        /// <summary>
        /// Creates an uninstallation record for the specified Type
        /// </summary>
        /// <param name="configuration">The configuration in which the category will be created</param>
        /// <param name="type">The type for which the category will be created</param>
        /// <returns></returns>
        public static XmlConfigurationCategory UninstallVersionOfType(XmlConfiguration configuration, Type type)
        {
            // remove the install entry for the type
            InstallationEngine.RemoveInstalledEntryForType(configuration, type);

            // create the uninstall entry for the type
            XmlConfigurationCategory category = InstallationEngine.CreateCategoryForTypeVersion(configuration, type, CategoryNames.Uninstalled);

            if (category != null)
            {
                XmlConfigurationOption option = null;

                // create the install date
                option = category.Options[@"UninstallDate"];
                if (option == null)
                {
                    option             = category.Options[@"UninstallDate", true, DateTime.Now.ToString()];
                    option.Category    = @"Installation Notes";
                    option.Description = @"This date marks the date and time on which the SnapIn was uninstalled.";
                }
                option = null;
            }
            return(category);
        }
        /// <summary>
        /// Saves the AutoUpdate options specified.
        /// </summary>
        /// <param name="options">The options to retrieve the value of the option to save.</param>
        /// <param name="optionToSave">The name of the option to save.</param>
        public static void Save(AutoUpdateOptions options, AutoUpdateOptions defaultOptions, AutoUpdateOptionNames optionName)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            if (defaultOptions == null)
            {
                throw new ArgumentNullException("defaultOptions");
            }

            try
            {
                XmlConfiguration         configuration = AllUsersConfigurationProvider.Current.Configuration;
                XmlConfigurationCategory category      = configuration.Categories[ConfigurationCategoryName, true];
                XmlConfigurationOption   option        = null;

                // this could be refactored again
                // into another method, but i'm in a massive hurry today

                switch (optionName)
                {
                case AutoUpdateOptionNames.AutomaticallyCheckForUpdates:
                    if ((option = category.Options[optionName.ToString()]) == null)
                    {
                        Load(defaultOptions, optionName);
                        option = category.Options[optionName.ToString()];
                    }
                    option.Value = options.AutomaticallyCheckForUpdates;
                    break;

                case AutoUpdateOptionNames.AutomaticallyDownloadUpdates:
                    if ((option = category.Options[optionName.ToString()]) == null)
                    {
                        Load(defaultOptions, optionName);
                        option = category.Options[optionName.ToString()];
                    }
                    option.Value = options.AutomaticallyDownloadUpdates;
                    break;

                case AutoUpdateOptionNames.AutomaticallyInstallUpdates:
                    if ((option = category.Options[optionName.ToString()]) == null)
                    {
                        Load(defaultOptions, optionName);
                        option = category.Options[optionName.ToString()];
                    }
                    option.Value = options.AutomaticallyInstallUpdates;
                    break;

                case AutoUpdateOptionNames.AutomaticallySwitchToNewVersion:
                    if ((option = category.Options[optionName.ToString()]) == null)
                    {
                        Load(defaultOptions, optionName);
                        option = category.Options[optionName.ToString()];
                    }
                    option.Value = options.AutomaticallySwitchToNewVersion;
                    break;

                case AutoUpdateOptionNames.AutomaticallyUpdateAlternatePath:
                    if ((option = category.Options[optionName.ToString()]) == null)
                    {
                        Load(defaultOptions, optionName);
                        option = category.Options[optionName.ToString()];
                    }
                    option.Value = options.AutomaticallyUpdateAlternatePath;
                    break;

                case AutoUpdateOptionNames.AlternatePath:
                    if ((option = category.Options[optionName.ToString()]) == null)
                    {
                        Load(defaultOptions, optionName);
                        option = category.Options[optionName.ToString()];
                    }
                    option.Value = options.AlternatePath;
                    break;

                case AutoUpdateOptionNames.WebServiceUrl:
                    if ((option = category.Options[optionName.ToString()]) == null)
                    {
                        Load(defaultOptions, optionName);
                        option = category.Options[optionName.ToString()];
                    }
                    option.Value = options.WebServiceUrl;
                    break;
                }
                ;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }
        /// <summary>
        /// Loads the value of the option specified. Creates the option if it does not exist, and writes the default value to the option.
        /// </summary>
        /// <param name="defaultOptions">The default options to use to create the option if the option does not exist.</param>
        /// <param name="optionName">The name of the option to load.</param>
        /// <returns></returns>
        public static object Load(AutoUpdateOptions defaultOptions, AutoUpdateOptionNames optionName)
        {
            if (defaultOptions == null)
            {
                throw new ArgumentNullException("defaultOptions");
            }

            try
            {
                XmlConfiguration         configuration = AllUsersConfigurationProvider.Current.Configuration;
                XmlConfigurationCategory category      = configuration.Categories[ConfigurationCategoryName, true];
                XmlConfigurationOption   option        = null;

                switch (optionName)
                {
                case AutoUpdateOptionNames.AutomaticallyCheckForUpdates:
                    if ((option = category.Options[optionName.ToString()]) == null)
                    {
                        option             = category.Options[optionName.ToString(), true, defaultOptions.AutomaticallyCheckForUpdates];
                        option.DisplayName = @"Automatically Check for Updates";
                        option.Category    = @"General";
                        option.Description = @"Determines whether the AutoUpdateManager automatically checks for updates on startup.";
                    }
                    break;

                case AutoUpdateOptionNames.AutomaticallyDownloadUpdates:
                    if ((option = category.Options[optionName.ToString()]) == null)
                    {
                        option             = category.Options[optionName.ToString(), true, defaultOptions.AutomaticallyDownloadUpdates];
                        option.DisplayName = @"Automatically Download Updates";
                        option.Category    = @"General";
                        option.Description = @"Determines whether the AutoUpdateManager automatically downloads available updates without prompting.";
                    }
                    break;

                case AutoUpdateOptionNames.AutomaticallyInstallUpdates:
                    if ((option = category.Options[optionName.ToString()]) == null)
                    {
                        option             = category.Options[optionName.ToString(), true, defaultOptions.AutomaticallyInstallUpdates];
                        option.DisplayName = @"Automatically Install Updates";
                        option.Category    = @"General";
                        option.Description = @"Determines whether the AutoUpdateManager automatically installs the updates after downloading.";
                    }
                    break;

                case AutoUpdateOptionNames.AutomaticallySwitchToNewVersion:
                    if ((option = category.Options[optionName.ToString()]) == null)
                    {
                        option             = category.Options[optionName.ToString(), true, defaultOptions.AutomaticallySwitchToNewVersion];
                        option.DisplayName = @"Automatically Switch to Newest Version";
                        option.Category    = @"General";
                        option.Description = @"Determines whether the AutoUpdateManager automatically switches to the newest version after installation.";
                    }
                    break;

                case AutoUpdateOptionNames.AutomaticallyUpdateAlternatePath:
                    if ((option = category.Options[optionName.ToString()]) == null)
                    {
                        option             = category.Options[optionName.ToString(), true, defaultOptions.AutomaticallyUpdateAlternatePath];
                        option.DisplayName = @"Automatically Update Alternate Path";
                        option.Category    = @"General";
                        option.Description = @"Determines whether the AutoUpdateManager automatically creates backup copies of the .Manifest and .Update files after installation.";
                    }
                    break;

                case AutoUpdateOptionNames.AlternatePath:
                    if ((option = category.Options[optionName.ToString()]) == null)
                    {
                        option             = category.Options[optionName.ToString(), true, defaultOptions.AlternatePath];
                        option.DisplayName = @"Alternate Download Path";
                        option.Category    = @"General";
                        option.Description = @"This alternate path (url or unc path) will be checked first before attempting to use the web service url to locate updates.";
                        option.EditorAssemblyQualifiedName = typeof(FolderBrowserUITypeEditor).AssemblyQualifiedName;
                    }
                    break;

                case AutoUpdateOptionNames.WebServiceUrl:
                    if ((option = category.Options[optionName.ToString()]) == null)
                    {
                        option             = category.Options[optionName.ToString(), true, defaultOptions.WebServiceUrl];
                        option.DisplayName = @"Web Service Url";
                        option.Category    = @"General";
                        option.Description = @"The url specifying where the AutoUpdate Web Service can be located.";
                    }
                    break;
                }
                ;

                return(option.Value);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }

            return(null);
        }
        /// <summary>
        /// Monitors the all users configuration for changes to the AutoUpdate options.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void HandleConfigurationChanged(object sender, XmlConfigurationElementEventArgs e)
        {
            // bail if the element is being edited
            if (e.Element.IsBeingEdited)
            {
                return;
            }

            // we only care about options
            if (e.Element.GetElementType() == XmlConfigurationElementTypes.XmlConfigurationOption)
            {
                string[] optionNames = Enum.GetNames(typeof(AutoUpdateOptionNames));

                foreach (string name in optionNames)
                {
                    string fullPath = string.Format("{0}\\{1}\\{2}", AllUsersConfigurationProvider.Current.ConfigurationName, ConfigurationCategoryName, name);

                    if (string.Compare(e.Element.Fullpath, fullPath, true) == 0)
                    {
                        AutoUpdateOptionNames optionName = (AutoUpdateOptionNames)Enum.Parse(typeof(AutoUpdateOptionNames), name);

                        try
                        {
                            this.BeginInit();

                            XmlConfigurationOption option = (XmlConfigurationOption)e.Element;

                            // read the value back out into our copy
                            switch (optionName)
                            {
                            case AutoUpdateOptionNames.AutomaticallyCheckForUpdates:
                                this.AutomaticallyCheckForUpdates = Convert.ToBoolean(option.Value);
                                break;

                            case AutoUpdateOptionNames.AutomaticallyDownloadUpdates:
                                this.AutomaticallyDownloadUpdates = Convert.ToBoolean(option.Value);
                                break;

                            case AutoUpdateOptionNames.AutomaticallyInstallUpdates:
                                this.AutomaticallyInstallUpdates = Convert.ToBoolean(option.Value);
                                break;

                            case AutoUpdateOptionNames.AutomaticallySwitchToNewVersion:
                                this.AutomaticallySwitchToNewVersion = Convert.ToBoolean(option.Value);
                                break;

                            case AutoUpdateOptionNames.AutomaticallyUpdateAlternatePath:
                                this.AutomaticallyUpdateAlternatePath = Convert.ToBoolean(option.Value);
                                break;

                            case AutoUpdateOptionNames.AlternatePath:
                                this.AlternatePath = Convert.ToString(option.Value);
                                break;

                            case AutoUpdateOptionNames.WebServiceUrl:
                                this.WebServiceUrl = Convert.ToString(option.Value);
                                break;
                            }
                            ;
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(ex);
                        }
                        finally
                        {
                            this.EndInit();
                        }

                        // raise the changed event to signal that this option was changed
                        EventManager.Raise <AutoUpdateOptionsEventArgs>(this.Changed, this, new AutoUpdateOptionsEventArgs(this, optionName));
                        break;
                    }
                }
            }
        }