Beispiel #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SettingsEntry"/> class.
 /// </summary>
 /// <param name="profile">The profile this <see cref="SettingsEntry"/>belongs to.</param>
 /// <param name="name">The name associated to this <see cref="SettingsEntry"/>.</param>
 protected SettingsEntry(SettingsProfile profile, UFile name)
 {
     if (profile == null) throw new ArgumentNullException("profile");
     if (name == null) throw new ArgumentNullException("name");
     Profile = profile;
     Name = name;
 }
Beispiel #2
0
 public SettingsGroup()
 {
     defaultProfile = new SettingsProfile(this, null);
     profileList.Add(defaultProfile);
     currentProfile = defaultProfile;
     Logger = new LoggerResult();
 }
Beispiel #3
0
 public SettingsContainer()
 {
     rootProfile = new SettingsProfile(this, null);
     profileList.Add(rootProfile);
     currentProfile = rootProfile;
     Logger = new LoggerResult();
 }
Beispiel #4
0
        /// <summary>
        /// Creates a new settings profile.
        /// </summary>
        /// <param name="setAsCurrent">If <c>true</c>, the created profile will also be set as <see cref="CurrentProfile"/>.</param>
        /// <param name="parent">The parent profile of the settings to create. If <c>null</c>, a default profile will be used.</param>
        /// <returns>A new instance of the <see cref="SettingsProfile"/> class.</returns>
        public SettingsProfile CreateSettingsProfile(bool setAsCurrent, SettingsProfile parent = null)
        {
            var profile = new SettingsProfile(this, parent ?? defaultProfile);
            profileList.Add(profile);
            if (setAsCurrent)
                CurrentProfile = profile;

            return profile;
        }
Beispiel #5
0
 /// <summary>
 /// Determines whether the specified profile contains key (without checking parent profiles).
 /// </summary>
 /// <param name="profile">The profile.</param>
 /// <returns></returns>
 public bool Remove(SettingsProfile profile)
 {
     return profile.Remove(Name);
 }
Beispiel #6
0
 internal void EncodeSettings(SettingsProfile profile, SettingsDictionary settingsDictionary)
 {
     foreach (var entry in profile.Settings.Values)
     {
         try
         {
             // Find key
             SettingsKey key;
             settingsKeys.TryGetValue(entry.Name, out key);
             settingsDictionary.Add(entry.Name, entry.GetSerializableValue(key));
         }
         catch (Exception)
         {
         }
     }
 }
Beispiel #7
0
        private void ChangeCurrentProfile(SettingsProfile oldProfile, SettingsProfile newProfile)
        {
            if (oldProfile == null) throw new ArgumentNullException("oldProfile");
            if (newProfile == null) throw new ArgumentNullException("newProfile");
            currentProfile = newProfile;

            foreach (var key in settingsKeys)
            {
                object oldValue;
                oldProfile.GetValue(key.Key, out oldValue, true, false);
                object newValue;
                newProfile.GetValue(key.Key, out newValue, true, false);
                var oldList = oldValue as IList;
                var newList = newValue as IList;

                bool isDifferent;
                if (oldList != null && newList != null)
                {
                    isDifferent = oldList.Count != newList.Count;
                    for (int i = 0; i < oldList.Count && !isDifferent; ++i)
                    {
                        if (!Equals(oldList[i], newList[i]))
                            isDifferent = true;
                    }
                }
                else
                {
                    isDifferent = !Equals(oldValue, newValue);
                }
                if (isDifferent)
                {
                    newProfile.NotifyEntryChanged(key.Key);
                }
            }

            // Changes have been notified, empty the list of modified settings.
            newProfile.ValidateSettingsChanges();
        }
Beispiel #8
0
 /// <summary>
 /// Creates a new settings profile.
 /// </summary>
 /// <param name="setAsCurrent">If <c>true</c>, the created profile will also be set as <see cref="CurrentProfile"/>.</param>
 /// <param name="parent">The parent profile of the settings to create. If <c>null</c>, the default profile will be used.</param>
 /// <returns>A new instance of the <see cref="SettingsProfile"/> class.</returns>
 public SettingsProfile CreateSettingsProfile(bool setAsCurrent, SettingsProfile parent = null)
 {
     var profile = new SettingsProfile(this, parent ?? rootProfile);
     lock (SettingsLock)
     {
         profileList.Add(profile);
         if (setAsCurrent)
             CurrentProfile = profile;
     }
     return profile;
 }
        /// <summary>
        /// Saves the given settings profile to a file at the given path.
        /// </summary>
        /// <param name="profile">The profile to save.</param>
        /// <param name="filePath">The path of the file.</param>
        /// <returns><c>true</c> if the file was correctly saved, <c>false</c> otherwise.</returns>
        public bool SaveSettingsProfile(SettingsProfile profile, UFile filePath)
        {
            if (profile == null) throw new ArgumentNullException(nameof(profile));
            try
            {
                profile.Saving = true;
                Directory.CreateDirectory(filePath.GetFullDirectory());

                var settingsFile = new SettingsFile(profile);
                using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Write))
                {
                    SettingsYamlSerializer.Default.Serialize(stream, settingsFile);
                }

                if (filePath != profile.FilePath)
                {
                    if (File.Exists(profile.FilePath))
                    {
                        File.Delete(profile.FilePath);
                    }

                    profile.FilePath = filePath;
                }               
            }
            catch (Exception e)
            {
                Logger.Error("Error while saving settings file [{0}]: {1}", e, filePath, e.FormatFull());
                return false;
            }
            finally
            {
                profile.Saving = false;
            }
            return true;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="FileModifiedEventArgs"/>
 /// </summary>
 /// <param name="profile">The profile corresponding to the file that has been modified.</param>
 public FileModifiedEventArgs(SettingsProfile profile)
 {
     Profile = profile;
 }
Beispiel #11
0
 /// <summary>
 /// Clears the current settings, by removing registered <see cref="SettingsKey"/> and <see cref="SettingsProfile"/> instances. This method should be used only for tests.
 /// </summary>
 public void ClearSettings()
 {
     lock (SettingsLock)
     {
         CurrentProfile = rootProfile;
         CurrentProfile.ValidateSettingsChanges();
         profileList.Clear();
         rootProfile.Settings.Clear();
         settingsKeys.Clear();
     }
 }
 /// <summary>
 /// Notifes that the changes have been validated by <see cref="SettingsProfile.ValidateSettingsChanges"/>.
 /// </summary>
 /// <param name="profile">The profile in which the change has been validated.</param>
 internal void NotifyChangesValidated(SettingsProfile profile)
 {
     ChangesValidated?.Invoke(this, new ChangesValidatedEventArgs(profile));
 }
Beispiel #13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SettingsProfile"/> class.
 /// </summary>
 /// <param name="group">The <see cref="SettingsGroup"/> containing this profile.</param>
 /// <param name="parentProfile">The parent profile.</param>
 internal SettingsProfile(SettingsGroup group, SettingsProfile parentProfile)
 {
     Group = group;
     this.parentProfile = parentProfile;
 }
Beispiel #14
0
 /// <summary>
 /// Tries to gets the value of this settings key in the given profile, if it exists.
 /// </summary>
 /// <param name="value">The resulting value, if found</param>
 /// <param name="searchInParentProfile">If true, the settings service will look in the parent profile of the given profile if the settings key is not defined into it.</param>
 /// <param name="profile">The profile in which to look for the value. If <c>null</c>, it will look in the <see cref="SettingsGroup.CurrentProfile"/>.</param>
 /// <returns><c>true</c> if the value was found, <c>false</c> otherwise.</returns>
 public bool TryGetValue(out object value, bool searchInParentProfile = true, SettingsProfile profile = null)
 {
     profile = profile ?? Group.CurrentProfile;
     if (profile.GetValue(Name, out value, searchInParentProfile, false))
     {
         return true;
     }
     value = DefaultValueObject;
     return false;
 }
Beispiel #15
0
 /// <summary>
 /// Determines whether the specified profile contains key (without checking parent profiles).
 /// </summary>
 /// <param name="profile">The profile.</param>
 /// <returns></returns>
 public bool ContainsKey(SettingsProfile profile)
 {
     object value;
     return profile.GetValue(Name, out value, false, false);
 }
Beispiel #16
0
 /// <summary>
 /// Sets the value of this settings key in the given profile.
 /// </summary>
 /// <param name="value">The new value to set.</param>
 /// <param name="profile">The profile in which to set the value. If <c>null</c>, it will look in the <see cref="SettingsGroup.CurrentProfile"/>.</param>
 public void SetValue(object value, SettingsProfile profile = null)
 {
     profile = profile ?? Group.CurrentProfile;
     profile.SetValue(Name, value);
 }
Beispiel #17
0
 /// <summary>
 /// Gets the value of this settings key in the given profile.
 /// </summary>
 /// <param name="searchInParentProfile">If true, the settings service will look in the parent profile of the given profile if the settings key is not defined into it.</param>
 /// <param name="profile">The profile in which to look for the value. If <c>null</c>, it will look in the <see cref="SettingsGroup.CurrentProfile"/>.</param>
 /// <param name="createInCurrentProfile">If true, the list will be created in the current profile, from the value of its parent profile.</param>
 /// <returns>The value of this settings key.</returns>
 /// <exception cref="KeyNotFoundException">No value can be found in the given profile matching this settings key.</exception>
 public virtual object GetValue(bool searchInParentProfile = true, SettingsProfile profile = null, bool createInCurrentProfile = false)
 {
     object value;
     profile = profile ?? Group.CurrentProfile;
     if (profile.GetValue(Name, out value, searchInParentProfile, createInCurrentProfile))
     {
         return value;
     }
     throw new KeyNotFoundException("Settings key not found");
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SettingsEntryValue"/> class.
 /// </summary>
 /// <param name="profile">The profile this <see cref="SettingsEntryValue"/>belongs to.</param>
 /// <param name="name">The name associated to this <see cref="SettingsEntryValue"/>.</param>
 /// <param name="value">The value to associate to this <see cref="SettingsEntryValue"/>.</param>
 internal SettingsEntryValue(SettingsProfile profile, UFile name, object value)
     : base(profile, name)
 {
     Value = value;
     ShouldNotify = true;
 }
Beispiel #19
0
 /// <summary>
 /// Notifes that the changes have been validated by <see cref="SettingsProfile.ValidateSettingsChanges"/>.
 /// </summary>
 /// <param name="profile">The profile in which the change has been validated.</param>
 internal void NotifyChangesValidated(SettingsProfile profile)
 {
     ChangesValidated?.Invoke(this, new ChangesValidatedEventArgs(profile));
 }
        /// <summary>
        /// Creates a new settings profile.
        /// </summary>
        /// <param name="setAsCurrent">If <c>true</c>, the created profile will also be set as <see cref="CurrentProfile"/>.</param>
        /// <param name="parent">The parent profile of the settings to create. If <c>null</c>, the default profile will be used.</param>
        /// <param name="registerInContainer">If true, the profile will be registered in this container. Otherwise it will be disconnected from the container.</param>
        /// <returns>A new instance of the <see cref="SettingsProfile"/> class.</returns>
        /// <remarks>
        /// If the profile is not registered to the container, it won't be able to receive <see cref="SettingsKey"/> that are registered after its
        /// creation. If the profile is registered to the container, <see cref="UnloadSettingsProfile"/> must be call in order to unregister it.
        /// </remarks>
        public SettingsProfile CreateSettingsProfile(bool setAsCurrent, SettingsProfile parent = null, bool registerInContainer = true)
        {
            if (setAsCurrent && !registerInContainer) throw new ArgumentException(@"Cannot set the profile as current if it's not registered to the container", nameof(setAsCurrent));

            var profile = new SettingsProfile(this, parent ?? RootProfile);

            if (registerInContainer)
            {
                lock (SettingsLock)
                {
                    profileList.Add(profile);
                    if (setAsCurrent)
                        CurrentProfile = profile;
                }
            }
            return profile;
        }
Beispiel #21
0
 /// <summary>
 /// Resolves the profile to use, returning the current profile if the given profile is null and checking the consistency of related <see cref="SettingsContainer"/>.
 /// </summary>
 /// <param name="profile">The profile to resolve.</param>
 /// <returns>The resolved profile.</returns>
 protected SettingsProfile ResolveProfile(SettingsProfile profile = null)
 {
     profile = profile ?? Container.CurrentProfile;
     if (profile.Container != Container)
         throw new ArgumentException("This settings key has a different container that the given settings profile.");
     return profile;
 }
Beispiel #22
0
        public SettingsProfile LoadSettingsProfile([NotNull] UFile filePath, bool setAsCurrent, SettingsProfile parent = null, bool registerInContainer = true)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }
            if (setAsCurrent && !registerInContainer)
            {
                throw new ArgumentException(@"Cannot set the profile as current if it's not registered to the container", nameof(setAsCurrent));
            }


            if (!File.Exists(filePath))
            {
                Logger.Error($"Settings file [{filePath}] was not found");
                return(null);
            }

            var profile = new SettingsProfile(this, parent ?? RootProfile)
            {
                FilePath = filePath
            };

            try
            {
                var settingsFile = new SettingsFile(profile);
                using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    SettingsYamlSerializer.Default.Deserialize(stream, settingsFile);
                }
            }
            catch (Exception e)
            {
                return(null);
            }

            if (registerInContainer)
            {
                lock (SettingsLock)
                {
                    profileList.Add(profile);
                    if (setAsCurrent)
                    {
                        CurrentProfile = profile;
                    }
                }
            }

            var handler = SettingsFileLoaded;

            handler?.Invoke(null, new SettingsFileLoadedEventArgs(filePath));
            return(profile);
        }
Beispiel #23
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SettingsFile"/> class.
 /// </summary>
 public SettingsFile(SettingsProfile profile)
 {
     Settings = profile;
 }
Beispiel #24
0
 internal void DecodeSettings([NotNull] SettingsDictionary settingsDictionary, SettingsProfile profile)
 {
     lock (SettingsLock)
     {
         foreach (var settings in settingsDictionary)
         {
             SettingsKey key;
             var         value      = settings.Value;
             object      finalValue = value;
             if (settingsKeys.TryGetValue(settings.Key, out key))
             {
                 finalValue = key.ConvertValue(value);
             }
             profile.SetValue(settings.Key, finalValue);
         }
     }
 }
Beispiel #25
0
        /// <summary>
        /// Loads a settings profile from the given file.
        /// </summary>
        /// <param name="filePath">The path of the file from which to load settings.</param>
        /// <param name="setAsCurrent">If <c>true</c>, the loaded profile will also be set as <see cref="CurrentProfile"/>.</param>
        /// <param name="parent">The profile to use as parent for the loaded profile. If <c>null</c>, a default profile will be used.</param>
        /// <returns><c>true</c> if settings were correctly loaded, <c>false</c> otherwise.</returns>
        public SettingsProfile LoadSettingsProfile(UFile filePath, bool setAsCurrent, SettingsProfile parent = null)
        {
            if (filePath == null) throw new ArgumentNullException(nameof(filePath));

            if (!File.Exists(filePath))
            {
                Logger.Error("Settings file [{0}] was not found", filePath);
                return null;
            }

            var profile = new SettingsProfile(this, parent ?? rootProfile) { FilePath = filePath };
            try
            {
                var settingsFile = new SettingsFile(profile);
                using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    YamlSerializer.Deserialize(stream, settingsFile);
                }
            }
            catch (Exception e)
            {
                Logger.Error("Error while loading settings file [{0}]: {1}", e, filePath, e.FormatForReport());
                return null;
            }

            lock (SettingsLock)
            {
                profileList.Add(profile);
                if (setAsCurrent)
                {
                    CurrentProfile = profile;
                }
            }
            
            var handler = SettingsFileLoaded;
            handler?.Invoke(null, new SettingsFileLoadedEventArgs(filePath));
            return profile;
        }
Beispiel #26
0
 /// <summary>
 /// Sets the value of this settings key in the given profile.
 /// </summary>
 /// <param name="value">The new value to set.</param>
 /// <param name="profile">The profile in which to set the value. If <c>null</c>, it will look in the <see cref="SettingsGroup.CurrentProfile"/>.</param>
 public void SetValue(object value, SettingsProfile profile = null)
 {
     profile = profile ?? Group.CurrentProfile;
     profile.SetValue(Name, value);
 }
Beispiel #27
0
 /// <summary>
 /// Creates a new instance of a class derived from <see cref="SettingsEntry"/> that matches the type of the given value. 
 /// </summary>
 /// <param name="profile">The profile the <see cref="SettingsEntry"/> to create belongs to.</param>
 /// <param name="name">The name associated to the <see cref="SettingsEntry"/> to create.</param>
 /// <param name="value">The value to associate to the <see cref="SettingsEntry"/> to create.</param>
 /// <returns>A new instance of a <see cref="SettingsEntry"/> class.</returns>
 internal static SettingsEntry CreateFromValue(SettingsProfile profile, UFile name, object value)
 {
     if (profile == null) throw new ArgumentNullException("profile");
     if (name == null) throw new ArgumentNullException("name");
     return new SettingsEntryValue(profile, name, value);
 }
Beispiel #28
0
 /// <summary>
 /// Unloads a profile that was previously loaded.
 /// </summary>
 /// <param name="profile">The profile to unload.</param>
 public void UnloadSettingsProfile(SettingsProfile profile)
 {
     if (profile == defaultProfile)
         throw new ArgumentException("The default profile cannot be unloaded");
     if (profile == CurrentProfile)
         throw new InvalidOperationException("Unable to unload the current profile.");
     profileList.Remove(profile);
 }
Beispiel #29
0
        /// <summary>
        /// Determines whether the specified profile contains key (without checking parent profiles).
        /// </summary>
        /// <param name="profile">The profile.</param>
        /// <returns></returns>
        public bool ContainsKey(SettingsProfile profile)
        {
            object value;

            return(profile.GetValue(Name, out value, false, false));
        }
        /// <summary>
        /// Reloads a profile from its file, updating the value that have changed.
        /// </summary>
        /// <param name="profile">The profile to reload.</param>
        public void ReloadSettingsProfile(SettingsProfile profile)
        {
            var filePath = profile.FilePath;
            if (filePath == null) throw new ArgumentException("profile");
            if (!File.Exists(filePath))
            {
                Logger.Error("Settings file [{0}] was not found", filePath);
                throw new ArgumentException("profile");
            }

            try
            {
                var settingsFile = new SettingsFile(profile);
                using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    SettingsYamlSerializer.Default.Deserialize(stream, settingsFile);
                }
            }
            catch (Exception e)
            {
                Logger.Error("Error while loading settings file [{0}]: {1}", e, filePath, e.FormatFull());
            }

            var handler = SettingsFileLoaded;
            handler?.Invoke(null, new SettingsFileLoadedEventArgs(filePath));
        }
Beispiel #31
0
        /// <summary>
        /// Reloads a profile from its file, updating the value that have changed.
        /// </summary>
        /// <param name="profile">The profile to reload.</param>
        public void ReloadSettingsProfile(SettingsProfile profile)
        {
            var filePath = profile.FilePath;
            if (filePath == null) throw new ArgumentException("profile");
            if (!File.Exists(filePath))
            {
                Logger.Error("Settings file [{0}] was not found", filePath);
                throw new ArgumentException("profile");
            }

            try
            {
                SettingsFile settingsFile;
                using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    settingsFile = (SettingsFile)YamlSerializer.Deserialize(stream);
                }

                DecodeSettings(settingsFile.Settings, profile);
            }
            catch (Exception e)
            {
                Logger.Error("Error while loading settings file [{0}]: {1}", e, filePath, e.FormatForReport());
            }

            var handler = SettingsFileLoaded;
            if (handler != null)
            {
                SettingsFileLoaded(null, new SettingsFileLoadedEventArgs(filePath));
            }
        }
Beispiel #32
0
 /// <summary>
 /// Determines whether the specified profile contains key (without checking parent profiles).
 /// </summary>
 /// <param name="profile">The profile.</param>
 /// <returns></returns>
 public bool Remove(SettingsProfile profile)
 {
     return(profile.Remove(Name));
 }
Beispiel #33
0
        /// <summary>
        /// Saves the given settings profile to a file at the given path.
        /// </summary>
        /// <param name="profile">The profile to save.</param>
        /// <param name="filePath">The path of the file.</param>
        /// <returns><c>true</c> if the file was correctly saved, <c>false</c> otherwise.</returns>
        public bool SaveSettingsProfile(SettingsProfile profile, UFile filePath)
        {
            if (profile == null) throw new ArgumentNullException("profile");
            try
            {
                profile.Saving = true;
                Directory.CreateDirectory(filePath.GetFullDirectory());

                var settingsFile = new SettingsFile();
                EncodeSettings(profile, settingsFile.Settings);

                using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Write))
                {
                    YamlSerializer.Serialize(stream, settingsFile);
                }
            }
            catch (Exception e)
            {
                Logger.Error("Error while saving settings file [{0}]: {1}", e, filePath, e.FormatForReport());
                return false;
            }
            finally
            {
                profile.Saving = false;
            }
            return true;
        }
Beispiel #34
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ChangesValidatedEventArgs"/> class.
 /// </summary>
 /// <param name="profile">The profile in which changes have been validated.</param>
 public ChangesValidatedEventArgs(SettingsProfile profile)
 {
     Profile = profile;
 }
Beispiel #35
0
 internal void DecodeSettings(SettingsDictionary settingsDictionary, SettingsProfile profile)
 {
     foreach (var settings in settingsDictionary)
     {
         SettingsKey key;
         var value = settings.Value;
         object finalValue = value;
         if (settingsKeys.TryGetValue(settings.Key, out key))
         {
             finalValue = key.ConvertValue(value);
         }
         profile.SetValue(settings.Key, finalValue);
     }
 }
Beispiel #36
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SettingsFile"/> class.
 /// </summary>
 public SettingsFile(SettingsProfile profile)
 {
     Settings = profile;
 }
Beispiel #37
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SettingsEntryValue"/> class.
 /// </summary>
 /// <param name="profile">The profile this <see cref="SettingsEntryValue"/>belongs to.</param>
 /// <param name="name">The name associated to this <see cref="SettingsEntryValue"/>.</param>
 /// <param name="value">The value to associate to this <see cref="SettingsEntryValue"/>.</param>
 internal SettingsEntryValue(SettingsProfile profile, UFile name, object value)
     : base(profile, name)
 {
     Value        = value;
     ShouldNotify = true;
 }
Beispiel #38
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SettingsProfile"/> class.
 /// </summary>
 /// <param name="container">The <see cref="SettingsContainer"/> containing this profile.</param>
 /// <param name="parentProfile">The parent profile.</param>
 internal SettingsProfile(SettingsContainer container, SettingsProfile parentProfile)
 {
     Container          = container;
     this.parentProfile = parentProfile;
 }
Beispiel #39
0
        /// <summary>
        /// Loads a settings profile from the given file.
        /// </summary>
        /// <param name="filePath">The path of the file from which to load settings.</param>
        /// <param name="setAsCurrent">If <c>true</c>, the loaded profile will also be set as <see cref="CurrentProfile"/>.</param>
        /// <param name="parent">The profile to use as parent for the loaded profile. If <c>null</c>, a default profile will be used.</param>
        /// <returns><c>true</c> if settings were correctly loaded, <c>false</c> otherwise.</returns>
        public SettingsProfile LoadSettingsProfile(UFile filePath, bool setAsCurrent, SettingsProfile parent = null)
        {
            if (filePath == null) throw new ArgumentNullException("filePath");

            if (!File.Exists(filePath))
            {
                Logger.Error("Settings file [{0}] was not found", filePath);
                return null;
            }

            SettingsProfile profile;
            try
            {
                SettingsFile settingsFile;
                using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    settingsFile = (SettingsFile)YamlSerializer.Deserialize(stream);
                }
                profile = new SettingsProfile(this, parent ?? defaultProfile) { FilePath = filePath };

                DecodeSettings(settingsFile.Settings, profile);
            }
            catch (Exception e)
            {
                Logger.Error("Error while loading settings file [{0}]: {1}", e, filePath, e.FormatForReport());
                return null;
            }

            profileList.Add(profile);
            if (setAsCurrent)
            {
                CurrentProfile = profile;
            }
            
            var handler = SettingsFileLoaded;
            if (handler != null)
            {
                SettingsFileLoaded(null, new SettingsFileLoadedEventArgs(filePath));
            }
            return profile;
        }
Beispiel #40
0
        /// <summary>
        /// Loads a settings profile from the given file.
        /// </summary>
        /// <param name="filePath">The path of the file from which to load settings.</param>
        /// <param name="setAsCurrent">If <c>true</c>, the loaded profile will also be set as <see cref="CurrentProfile"/>.</param>
        /// <param name="parent">The profile to use as parent for the loaded profile. If <c>null</c>, a default profile will be used.</param>
        /// <returns><c>true</c> if settings were correctly loaded, <c>false</c> otherwise.</returns>
        public SettingsProfile LoadSettingsProfile(UFile filePath, bool setAsCurrent, SettingsProfile parent = null)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }

            if (!File.Exists(filePath))
            {
                Logger.Error("Settings file [{0}] was not found", filePath);
                return(null);
            }

            var profile = new SettingsProfile(this, parent ?? rootProfile)
            {
                FilePath = filePath
            };

            try
            {
                var settingsFile = new SettingsFile(profile);
                using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    YamlSerializer.Deserialize(stream, settingsFile);
                }
            }
            catch (Exception e)
            {
                Logger.Error("Error while loading settings file [{0}]: {1}", e, filePath, e.FormatForReport());
                return(null);
            }

            lock (SettingsLock)
            {
                profileList.Add(profile);
                if (setAsCurrent)
                {
                    CurrentProfile = profile;
                }
            }

            var handler = SettingsFileLoaded;

            handler?.Invoke(null, new SettingsFileLoadedEventArgs(filePath));
            return(profile);
        }
Beispiel #41
0
 /// <summary>
 /// Notifes that the changes have been validated by <see cref="SettingsProfile.ValidateSettingsChanges"/>.
 /// </summary>
 /// <param name="profile">The profile in which the change has been validated.</param>
 internal void NotifyChangesValidated(SettingsProfile profile)
 {
     var handler = ChangesValidated;
     if (handler != null)
         handler(this, new ChangesValidatedEventArgs(profile));
 }
Beispiel #42
0
        private void ChangeCurrentProfile(SettingsProfile oldProfile, SettingsProfile newProfile)
        {
            if (oldProfile == null)
            {
                throw new ArgumentNullException(nameof(oldProfile));
            }
            if (newProfile == null)
            {
                throw new ArgumentNullException(nameof(newProfile));
            }
            currentProfile = newProfile;

            lock (SettingsLock)
            {
                foreach (var key in settingsKeys)
                {
                    object oldValue;
                    oldProfile.GetValue(key.Key, out oldValue, true, false);
                    object newValue;
                    newProfile.GetValue(key.Key, out newValue, true, false);
                    var  oldList       = oldValue as IList;
                    var  newList       = newValue as IList;
                    var  oldDictionary = oldValue as IDictionary;
                    var  newDictionary = newValue as IDictionary;
                    bool isDifferent;
                    if (oldList != null && newList != null)
                    {
                        isDifferent = oldList.Count != newList.Count;
                        for (int i = 0; i < oldList.Count && !isDifferent; ++i)
                        {
                            if (!Equals(oldList[i], newList[i]))
                            {
                                isDifferent = true;
                            }
                        }
                    }
                    else if (oldDictionary != null && newDictionary != null)
                    {
                        isDifferent = oldDictionary.Count != newDictionary.Count;
                        foreach (var k in oldDictionary.Keys)
                        {
                            if (!newDictionary.Contains(k) || !Equals(oldDictionary[k], newDictionary[k]))
                            {
                                isDifferent = true;
                            }
                        }
                    }
                    else
                    {
                        isDifferent = !Equals(oldValue, newValue);
                    }
                    if (isDifferent)
                    {
                        newProfile.NotifyEntryChanged(key.Key);
                    }
                }
            }

            // Changes have been notified, empty the list of modified settings.
            newProfile.ValidateSettingsChanges();
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ChangesValidatedEventArgs"/> class.
 /// </summary>
 /// <param name="profile">The profile in which changes have been validated.</param>
 public ChangesValidatedEventArgs(SettingsProfile profile)
 {
     Profile = profile;
 }
Beispiel #44
0
        /// <summary>
        /// Loads a settings profile from the given file.
        /// </summary>
        /// <param name="filePath">The path of the file from which to load settings.</param>
        /// <param name="setAsCurrent">If <c>true</c>, the loaded profile will also be set as <see cref="CurrentProfile"/>.</param>
        /// <param name="parent">The profile to use as parent for the loaded profile. If <c>null</c>, a default profile will be used.</param>
        /// <returns><c>true</c> if settings were correctly loaded, <c>false</c> otherwise.</returns>
        public SettingsProfile LoadSettingsProfile(UFile filePath, bool setAsCurrent, SettingsProfile parent = null)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException("filePath");
            }

            if (!File.Exists(filePath))
            {
                Logger.Error("Settings file [{0}] was not found", filePath);
                return(null);
            }

            SettingsProfile profile;

            try
            {
                SettingsFile settingsFile;
                using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    settingsFile = (SettingsFile)YamlSerializer.Deserialize(stream);
                }
                profile = new SettingsProfile(this, parent ?? defaultProfile)
                {
                    FilePath = filePath
                };

                DecodeSettings(settingsFile.Settings, profile);
            }
            catch (Exception e)
            {
                Logger.Error("Error while loading settings file [{0}]: {1}", e, filePath, e.FormatForReport());
                return(null);
            }

            profileList.Add(profile);
            if (setAsCurrent)
            {
                CurrentProfile = profile;
            }

            var handler = SettingsFileLoaded;

            if (handler != null)
            {
                SettingsFileLoaded(null, new SettingsFileLoadedEventArgs(filePath));
            }
            return(profile);
        }