Example #1
0
        /// <summary>
        /// This private constructor doesn't allow to instanciate this method.
        /// </summary>
        private Config()
        {
            #region Load or create default config storage.

            ConfigObjectV1 co = null;
            
            String configFile = Path.Combine(_configFolderPath, "config.xml");

            if (!File.Exists(configFile))
            {
                co = new ConfigObjectV1();
                StoreChanges();
            }

            if (File.Exists(configFile))
            {
                using (var stream = new FileStream(configFile, FileMode.Open, FileAccess.Read))
                using (var reader = new System.Xml.XmlTextReader(stream))
                {
                    // Find the version of config file.
                    String configVersion = null;
                    while (reader.Read())
                    {
                        if (reader.Name.Equals("Version", StringComparison.Ordinal))
                        {
                            reader.Read(); // Force reader to go to node's value.

                            configVersion = reader.Value;

                            break;
                        }
                    }

                    if (configVersion != null)                    
                    {
                        Int32 version;
                        
                        if (Int32.TryParse(configVersion, out version))
                        {
                            if (version > 1)
                            {
                                MessageBox.Show(
                                    LocalizedData.ConfigIsNewerThanSupported,
                                    LocalizedData.PluginName,
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Exclamation);
                            }
                            // Correct version, read config's content.
                            else if (version == 1)
                            {
                                stream.Position = 0;

                                var serializer = new XmlSerializer(typeof(ConfigObjectV1));
                               
                                try
                                {
                                    co = (ConfigObjectV1)serializer.Deserialize(stream);
                                }
                                catch (InvalidOperationException ex)
                                {
                                    MessageBox.Show(
                                        LocalizedData.FailedToDeserializePlugin + ex.Message,
                                        LocalizedData.PluginName,
                                        MessageBoxButton.OK,
                                        MessageBoxImage.Exclamation);
                                }
                            }
                            else // This is really odd situation, negative or zero version...
                            {
                                throw new ApplicationException("Unknown version of configuration file: " + version);
                            }
                        }                        
                    }
                }
            }

            co = co ?? new ConfigObjectV1();

            #endregion

            #region Transfer data from the storage into this object.

            IsEnabled = co.IsEnabled;
            Height = co.Height;
            Width = co.Width;
            Left = co.Left;
            Top = co.Top;
            ShowInTaskbar = co.ShowInTaskbar;
            EnableHotKeys = co.EnableHotKeys;

            foreach (var rule in _rules)
            {
                rule.Enabled = co.Rules.Contains(rule.Rule);
            }

            #endregion
        }
Example #2
0
        /// <summary>
        /// Saves changes made to this object to configuration file
        /// and raises the <see cref="Saved"/> event;
        /// </summary>
        public void StoreChanges()
        {
            #region Create a storage object with a copy of this object's data.

            var configObject = new ConfigObjectV1
            {
                IsEnabled = IsEnabled,
                Height = Height,
                Width = Width,
                Left = Left,
                Top = Top,
                ShowInTaskbar = ShowInTaskbar,
                EnableHotKeys = EnableHotKeys,
                Rules = _rules
                    .Where(r => r.Enabled)
                    .Select(r => r.Rule)
                    .ToArray()
            };

            #endregion

            #region Serialize storage object to an XML file.

            if (!Directory.Exists(_configFolderPath))
            {
                try
                {
                    Directory.CreateDirectory(_configFolderPath);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(
                        LocalizedData.FailedToCreateConfigFolder + ex.Message,
                        LocalizedData.PluginName,
                        MessageBoxButton.OK,
                        MessageBoxImage.Error);

                    throw;
                }
            }

            String configFile = Path.Combine(_configFolderPath, "config.xml");

            var serializer = new XmlSerializer(typeof(ConfigObjectV1));
            using (var ms = new MemoryStream())
            {
                serializer.Serialize(ms, configObject);
                ms.Flush();
                byte[] buf = ms.GetBuffer();
                if (ms.Length > 0)
                {
                    using (var fs = new FileStream(configFile, FileMode.Create))
                    {
                        fs.Write(buf, 0, (int) ms.Length);
                    }
                }
            }

            #endregion

            OnSaved(this, EventArgs.Empty);
        }