1. Summary
  2. Files
  3. Support
  4. Report Spam
  5. Create account
  6. Log in

root/branches/desktoptool/QTTabBar/StaticReg.cs @ 431

Revision 431, 5.3 KB (checked in by masamunexgp, 14 months ago)

Fixed QTTabBar.csproj, which somehow got corrupted. Added a few methods to RegBackedList? that I forgot about.

Line 
1using System;
2using System.Collections.Generic;
3using System.Collections.ObjectModel;
4using System.Linq;
5using System.Text;
6using Microsoft.Win32;
7
8namespace QTTabBarLib {
9    internal static class StaticReg {
10        internal static string CreateWindowGroup {
11            get { return (string)ReadProp("CreateWindowGroup"); }
12            set { WriteProp("CreateWindowGroup", value); }
13        }
14
15        internal static bool SkipNextCapture {
16            get { return (int)ReadProp("SkipNextCapture") != 0; }
17            set { WriteProp("SkipNextCapture", value ? 1 : 0); }
18        }
19
20        internal static UniqueList<string> ExecutedPathsList = new UniqueList<string>(16); // todo
21        internal static UniqueList<string> ClosedTabHistoryList = new UniqueList<string>(16); // todo
22
23        private static RegBackedList<string> _LockedTabsToRestoreList = new RegBackedList<string>("LockedTabs");
24        internal static RegBackedList<string> LockedTabsToRestoreList {
25            get {
26                _LockedTabsToRestoreList.Update();
27                return _LockedTabsToRestoreList;
28            }
29        }
30
31        private static RegBackedList<byte[]> _CreateWindowIDLs = new RegBackedList<byte[]>("CreateWindowIDLs");
32        internal static RegBackedList<byte[]> CreateWindowIDLs {
33            get {
34                _CreateWindowIDLs.Update();
35                return _CreateWindowIDLs;
36            }
37        }
38
39        private static RegBackedList<string> _CreateWindowPaths = new RegBackedList<string>("CreateWindowPaths");
40        internal static RegBackedList<string> CreateWindowPaths {
41            get {
42                _CreateWindowPaths.Update();
43                return _CreateWindowPaths;
44            }
45        }
46
47        private static object ReadProp(string key) {
48            using(RegistryKey reg = Registry.CurrentUser.CreateSubKey(RegConst.StaticReg)) {
49                return reg.GetValue(key);
50            }
51        }
52
53        private static void WriteProp(string key, object value) {
54            using(RegistryKey reg = Registry.CurrentUser.CreateSubKey(RegConst.StaticReg)) {
55                reg.SetValue(key, value);
56            }
57        }
58    }
59
60    internal sealed class RegBackedList<T> : Collection<T> {
61        private string key;
62        private int lastUpdate = 0;
63        private bool updating = false;
64        public RegBackedList(IList<T> list, string key) : base(list) {
65            this.key = key;
66        }
67
68        public RegBackedList(string key) {
69            this.key = key;
70        }
71
72        protected override void SetItem(int index, T item) {
73            base.SetItem(index, item);
74            if(updating) return;
75            Update();
76            using(RegistryKey reg = Registry.CurrentUser.CreateSubKey(RegConst.StaticReg + key)) {
77                reg.SetValue("" + index, item);
78                reg.SetValue("", ++lastUpdate);
79            }
80        }
81
82        protected override void RemoveItem(int index) {
83            base.RemoveItem(index);
84            if(updating) return;
85            Update();
86            using(RegistryKey reg = Registry.CurrentUser.CreateSubKey(RegConst.StaticReg + key)) {
87                for(int i = index; i < Count; i++) {
88                    reg.SetValue("" + i, this[i]);
89                }
90                reg.DeleteValue("" + Count);
91                reg.SetValue("", ++lastUpdate);
92            }
93        }
94
95        protected override void InsertItem(int index, T item) {
96            base.InsertItem(index, item);
97            if(updating) return;
98            Update();
99            using(RegistryKey reg = Registry.CurrentUser.CreateSubKey(RegConst.StaticReg + key)) {
100                for(int i = index; i < Count; i++) {
101                    reg.SetValue("" + i, this[i]);
102                }
103                reg.SetValue("", ++lastUpdate);
104            }
105        }
106
107        protected override void ClearItems() {
108            base.ClearItems();
109            if(updating) return;
110            Update();
111            using(RegistryKey reg = Registry.CurrentUser.CreateSubKey(RegConst.StaticReg + key)) {
112                foreach(string name in reg.GetValueNames()) {
113                    reg.DeleteValue(name);
114                }
115                reg.SetValue("", ++lastUpdate);
116            }           
117        }
118
119        public void Update() {
120            using(RegistryKey reg = Registry.CurrentUser.OpenSubKey(RegConst.StaticReg + key)) {
121                if(reg == null) return;
122                int update = (int)reg.GetValue("", 0);
123                if(update == lastUpdate) return;
124                lastUpdate = update;
125                updating = true;
126                Clear();
127                for(int i = 0;; i++) {
128                    object obj = reg.GetValue("" + i);
129                    if(obj == null) break;
130                    Add((T)obj);
131                }
132                updating = false;
133            }
134        }
135
136        public void AddRange(IEnumerable<T> range) {
137            foreach(T t in range) {
138                Add(t); // todo: make more efficient
139            }
140        }
141
142        public void Assign(IEnumerable<T> collection) {
143            Clear(); // todo: make more efficient
144            AddRange(collection);
145        }
146    }
147}
Note: See TracBrowser for help on using the browser.