Exemple #1
0
        public static void ReadConfig()
        {
            const string RegPath = RegConst.Root + RegConst.Config;

            var categories =
                from categoryProperty in typeof(Config).GetProperties()
                where categoryProperty.CanWrite
                let categoryType = categoryProperty.PropertyType
                                   let categoryObject = categoryProperty.GetValue(LoadedConfig, null)
                                                        select new {
                keyPath = RegPath + categoryType.Name.Substring(1),
                categoryObject,
                settings = (
                    from settingProperty in categoryType.GetProperties()
                    select new {
                    name = settingProperty.Name,
                    type = settingProperty.PropertyType,
                    value = settingProperty.GetValue(categoryObject, null),
                    property = settingProperty
                }
                    )
            };

            foreach (var category in categories)
            {
                using (var key = Registry.CurrentUser.CreateSubKey(category.keyPath)) {
                    foreach (var setting in category.settings)
                    {
                        object value = key.GetValue(setting.name);
                        if (value == null)
                        {
                            continue;
                        }

                        Type t = setting.type;
                        try {
                            if (t == typeof(bool))
                            {
                                value = (int)value != 0;
                            }
                            else if (t.IsEnum)
                            {
                                value = Enum.Parse(t, value.ToString());
                            }
                            else if (t != typeof(int) && t != typeof(string))
                            {
                                using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(value.ToString()))) {
                                    if (t == typeof(Font))
                                    {
                                        var ser = new DataContractJsonSerializer(typeof(XmlSerializableFont));
                                        var xsf = ser.ReadObject(stream) as XmlSerializableFont;
                                        value = xsf == null ? null : xsf.ToFont();
                                    }
                                    else
                                    {
                                        var ser = new DataContractJsonSerializer(t);
                                        value = ser.ReadObject(stream);
                                    }
                                }
                            }

                            setting.property.SetValue(category.categoryObject, value, null);
                        } catch {}
                    }
                }
            }

            using (IDLWrapper wrapper = new IDLWrapper(Config.Window.DefaultLocation)) {
                if (!wrapper.Available)
                {
                    Config.Window.DefaultLocation = new Config._Window().DefaultLocation;
                }
            }
            Config.Tips.PreviewFont      = Config.Tips.PreviewFont ?? Control.DefaultFont;
            Config.Tips.PreviewMaxWidth  = QTUtility.ValidateMinMax(Config.Tips.PreviewMaxWidth, 128, 1920);
            Config.Tips.PreviewMaxHeight = QTUtility.ValidateMinMax(Config.Tips.PreviewMaxHeight, 96, 1200);
            Config.Misc.TabHistoryCount  = QTUtility.ValidateMinMax(Config.Misc.TabHistoryCount, 1, 30);
            Config.Misc.FileHistoryCount = QTUtility.ValidateMinMax(Config.Misc.FileHistoryCount, 1, 30);
            Config.Misc.NetworkTimeout   = QTUtility.ValidateMinMax(Config.Misc.NetworkTimeout, 0, 120);
            Config.Skin.TabHeight        = QTUtility.ValidateMinMax(Config.Skin.TabHeight, 10, 50);
            Config.Skin.TabMinWidth      = QTUtility.ValidateMinMax(Config.Skin.TabMinWidth, 10, 50);
            Config.Skin.TabMaxWidth      = QTUtility.ValidateMinMax(Config.Skin.TabMaxWidth, 50, 999);
            Config.Skin.OverlapPixels    = QTUtility.ValidateMinMax(Config.Skin.TabHeight, 0, 20);
            Config.Skin.TabTextFont      = Config.Skin.TabTextFont ?? Control.DefaultFont;
            Func <Padding, Padding> validatePadding = p => {
                p.Left   = QTUtility.ValidateMinMax(p.Left, 0, 99);
                p.Top    = QTUtility.ValidateMinMax(p.Top, 0, 99);
                p.Right  = QTUtility.ValidateMinMax(p.Right, 0, 99);
                p.Bottom = QTUtility.ValidateMinMax(p.Bottom, 0, 99);
                return(p);
            };

            Config.Skin.RebarSizeMargin  = validatePadding(Config.Skin.RebarSizeMargin);
            Config.Skin.TabContentMargin = validatePadding(Config.Skin.TabContentMargin);
            Config.Skin.TabSizeMargin    = validatePadding(Config.Skin.TabSizeMargin);
            using (IDLWrapper wrapper = new IDLWrapper(Config.Skin.TabImageFile)) {
                if (!wrapper.Available)
                {
                    Config.Skin.TabImageFile = "";
                }
            }
            using (IDLWrapper wrapper = new IDLWrapper(Config.Skin.RebarImageFile)) {
                if (!wrapper.Available)
                {
                    Config.Skin.RebarImageFile = "";
                }
            }
            using (IDLWrapper wrapper = new IDLWrapper(Config.BBar.ImageStripPath)) {
                // todo: check dimensions
                if (!wrapper.Available)
                {
                    Config.BBar.ImageStripPath = "";
                }
            }
            List <int> blist = Config.BBar.ButtonIndexes.ToList();

            blist.RemoveAll(i => (i.HiWord() - 1) >= Config.BBar.ActivePluginIDs.Length);
            Config.BBar.ButtonIndexes = blist.ToArray();
            var keys = Config.Keys.Shortcuts;

            Array.Resize(ref keys, (int)BindAction.KEYBOARD_ACTION_COUNT);
            Config.Keys.Shortcuts = keys;
            foreach (var pair in Config.Keys.PluginShortcuts.Where(p => p.Value == null).ToList())
            {
                Config.Keys.PluginShortcuts.Remove(pair.Key);
            }
            if (QTUtility.IsXP)
            {
                Config.Tweaks.AlwaysShowHeaders = false;
            }
            if (!QTUtility.IsWin7)
            {
                Config.Tweaks.RedirectLibraryFolders = false;
            }
            if (!QTUtility.IsXP)
            {
                Config.Tweaks.KillExtWhileRenaming = true;
            }
            if (QTUtility.IsXP)
            {
                Config.Tweaks.BackspaceUpLevel = true;
            }
            if (!QTUtility.IsWin7)
            {
                Config.Tweaks.ForceSysListView = true;
            }
        }