예제 #1
0
        public void Save(string profile)
        {
            var fileName = Profiles.GetFileName(profile);

            this.Debouncer.Exec(() =>
            {
                Logger.Write(this, LogLevel.Debug, "Saving configuration to file \"{0}\".", fileName);
                try
                {
                    //Use a temp file so the settings aren't lost if something goes wrong.
                    var temp = Path.GetTempFileName();
                    using (var stream = File.Create(temp))
                    {
                        Serializer.Save(stream, this.Sections);
                    }
                    if (!MoveFileEx(temp, fileName, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH))
                    {
                        throw new Exception("MoveFileEx: Failed.");
                    }
                    Profiles.Profile = profile;
                }
                catch (Exception e)
                {
                    Logger.Write(this, LogLevel.Warn, "Failed to save configuration: {0}", e.Message);
                }
            });
            //Configuration not technically saved *yet* but it doesn't matter.
            this.OnSaved();
        }
예제 #2
0
        public void Delete(string profile)
        {
            var fileName = Profiles.GetFileName(profile);

            Profiles.Delete(profile);
            File.Delete(fileName);
            this.Load();
        }
예제 #3
0
        public void Delete(string profile)
        {
            var fileName = Profiles.GetFileName(profile);

            try
            {
                Profiles.Delete(profile);
                File.Delete(fileName);
                this.Load();
            }
            catch (Exception e)
            {
                Logger.Write(this, LogLevel.Warn, "Failed to delete configuration: {0}", e.Message);
            }
            this.OnSaved();
        }
예제 #4
0
        public void Save(string profile)
        {
            var fileName = Profiles.GetFileName(profile);

            this.Debouncer.Exec(() =>
            {
                Logger.Write(this, LogLevel.Debug, "Saving configuration to file \"{0}\".", fileName);
                try
                {
                    using (var stream = File.Create(fileName))
                    {
                        Serializer.Save(stream, this.Sections);
                    }
                }
                catch (Exception e)
                {
                    Logger.Write(this, LogLevel.Warn, "Failed to save configuration: {0}", e.Message);
                }
            });
            Profiles.Profile = profile;
        }
예제 #5
0
        public void Load(string profile)
        {
            foreach (var section in this.Sections)
            {
                if (section.IsInitialized)
                {
                    continue;
                }
                section.InitializeComponent();
            }
            var fileName = Profiles.GetFileName(profile);

            if (!File.Exists(fileName))
            {
                Logger.Write(this, LogLevel.Debug, "Configuration file \"{0}\" does not exist.", fileName);
                return;
            }
            Logger.Write(this, LogLevel.Debug, "Loading configuration from file \"{0}\".", fileName);
            try
            {
                var modifiedElements = this.GetModifiedElements();
                var restoredElements = new List <ConfigurationElement>();
                using (var stream = File.OpenRead(fileName))
                {
                    var sections = Serializer.Load(stream);
                    foreach (var section in sections)
                    {
                        if (!this.Contains(section.Key))
                        {
                            //If config was created by a component that is no longer loaded then it will be lost here.
                            //TODO: Add the config but hide it so it's preserved but not displayed.
                            Logger.Write(this, LogLevel.Warn, "Configuration section \"{0}\" no longer exists.", section.Key);
                            continue;
                        }
                        var existing = this.GetSection(section.Key);
                        try
                        {
                            Logger.Write(this, LogLevel.Debug, "Loading configuration section \"{0}\".", section.Key);
                            restoredElements.AddRange(this.Load(existing, section.Value));
                        }
                        catch (Exception e)
                        {
                            Logger.Write(this, LogLevel.Warn, "Failed to load configuration section \"{0}\": {1}", existing.Id, e.Message);
                        }
                    }
                }
                foreach (var modifiedElement in modifiedElements)
                {
                    if (restoredElements.Contains(modifiedElement))
                    {
                        continue;
                    }
                    Logger.Write(this, LogLevel.Debug, "Resetting configuration element: \"{0}\".", modifiedElement.Id);
                    modifiedElement.Reset();
                }
                Profiles.Profile = profile;
            }
            catch (Exception e)
            {
                Logger.Write(this, LogLevel.Warn, "Failed to load configuration: {0}", e.Message);
            }
        }