1 | unit uTMGOptions;
|
---|
2 | //kt Added entire unit. 2/10/10
|
---|
3 |
|
---|
4 | interface
|
---|
5 |
|
---|
6 | uses
|
---|
7 | IniFiles, SysUtils, Forms, uCore, ORFn, ORNet, Trpcb;
|
---|
8 |
|
---|
9 | var
|
---|
10 | USE_SERVER_INI : boolean;
|
---|
11 | Initialized : boolean;
|
---|
12 |
|
---|
13 | Procedure WriteBool(const Key: String; Value: Boolean; AsDefault : boolean = false);
|
---|
14 | Procedure WriteInteger(const Key: String; Value: Integer; AsDefault : boolean = false);
|
---|
15 | Procedure WriteString(const Key: String; Value: String; AsDefault : boolean = false);
|
---|
16 | Function ReadBool(const Key: String; Default: Boolean): Boolean;
|
---|
17 | Function ReadInteger(const Key: String; Default: Integer): Integer;
|
---|
18 | Function ReadString(const Key: String; Default: String): String;
|
---|
19 |
|
---|
20 | implementation
|
---|
21 |
|
---|
22 | uses
|
---|
23 | Classes;
|
---|
24 |
|
---|
25 | const
|
---|
26 | DEFAULT_SECTION = 'DEFAULT';
|
---|
27 |
|
---|
28 | var
|
---|
29 | StrList : TStringList;
|
---|
30 | OptionsIniFile : TIniFile; //kt 8/09
|
---|
31 |
|
---|
32 | procedure TMGOptionsInitialize;
|
---|
33 | begin
|
---|
34 | OptionsIniFile := TIniFile.Create(ChangeFileExt(Application.ExeName, '.INI')); //kt
|
---|
35 | StrList := TStringList.Create;
|
---|
36 |
|
---|
37 | //if RPC exists, use in lieu of INI file
|
---|
38 | RPCBrokerV.remoteprocedure := 'XWB IS RPC AVAILABLE';
|
---|
39 | RPCBrokerV.Param[0].Value := 'TMG INIFILE GET';
|
---|
40 | RPCBrokerV.Param[0].ptype := literal;
|
---|
41 | RPCBrokerV.Param[1].Value := 'R';
|
---|
42 | RPCBrokerV.Param[1].ptype := literal;
|
---|
43 | //RPCResult := RPCBrokerV.StrCall; {returns 1 if available, 0 if not available}
|
---|
44 | USE_SERVER_INI := StrToBool(RPCBrokerV.StrCall);
|
---|
45 | end;
|
---|
46 |
|
---|
47 | procedure WriteRPCStr(const Section, Key: String; Value: String);
|
---|
48 | begin
|
---|
49 | RPCBrokerV.remoteprocedure := 'TMG INIFILE SET';
|
---|
50 | RPCBrokerV.Param[0].Value := Section;
|
---|
51 | RPCBrokerV.Param[0].ptype := literal;
|
---|
52 | RPCBrokerV.Param[1].Value := Key;
|
---|
53 | RPCBrokerV.Param[1].ptype := literal;
|
---|
54 | RPCBrokerV.Param[2].Value := Value;
|
---|
55 | RPCBrokerV.Param[2].ptype := literal;
|
---|
56 | RPCBrokerV.Call;
|
---|
57 | end;
|
---|
58 |
|
---|
59 | function ReadRPCStr(const Section, Key, Default: String) : string;
|
---|
60 | begin
|
---|
61 | RPCBrokerV.remoteprocedure := 'TMG INIFILE GET';
|
---|
62 | RPCBrokerV.param[0].ptype := literal;
|
---|
63 | RPCBrokerV.param[0].value := Section;
|
---|
64 | RPCBrokerV.Param[1].ptype := literal;
|
---|
65 | RPCBrokerV.param[1].value := Key;
|
---|
66 | RPCBrokerV.Param[2].ptype := literal;
|
---|
67 | RPCBrokerV.param[2].value := Default;
|
---|
68 | Result := Piece(RPCBrokerV.StrCall,'^',2);
|
---|
69 | end;
|
---|
70 |
|
---|
71 |
|
---|
72 | Procedure WriteBool(const Key: String; Value: Boolean; AsDefault : boolean = false);
|
---|
73 | begin
|
---|
74 | if Initialized = False then TMGOptionsInitialize;
|
---|
75 | if AsDefault then begin
|
---|
76 | if USE_SERVER_INI then begin
|
---|
77 | WriteRPCStr(DEFAULT_SECTION,Key,BoolToStr(Value));
|
---|
78 | end else begin
|
---|
79 | OptionsIniFile.WriteBool(DEFAULT_SECTION,Key,Value);
|
---|
80 | end;
|
---|
81 | end else begin
|
---|
82 | if USE_SERVER_INI then begin
|
---|
83 | WriteRPCStr(User.Name,Key,BoolToStr(Value));
|
---|
84 | end else begin
|
---|
85 | OptionsIniFile.ReadSection(DEFAULT_SECTION,StrList);
|
---|
86 | if (StrList.IndexOf(Key)=-1) or (OptionsIniFile.ReadBool(DEFAULT_SECTION,Key,false)<>Value) then begin
|
---|
87 | OptionsIniFile.WriteBool(User.Name,Key,Value); //Only write user value if differs from default value
|
---|
88 | end;
|
---|
89 | end;
|
---|
90 | end;
|
---|
91 | end;
|
---|
92 |
|
---|
93 | Procedure WriteInteger(const Key: String; Value: Integer; AsDefault : boolean = false);
|
---|
94 | begin
|
---|
95 | if Initialized = False then TMGOptionsInitialize;
|
---|
96 | if AsDefault then begin
|
---|
97 | if USE_SERVER_INI then begin
|
---|
98 | WriteRPCStr(DEFAULT_SECTION,Key,IntToStr(Value));
|
---|
99 | end else begin
|
---|
100 | OptionsIniFile.WriteInteger(DEFAULT_SECTION,Key,Value);
|
---|
101 | end;
|
---|
102 | end else begin
|
---|
103 | if USE_SERVER_INI then begin
|
---|
104 | WriteRPCStr(User.Name,Key,IntToStr(Value));
|
---|
105 | end else begin
|
---|
106 | OptionsIniFile.ReadSection(DEFAULT_SECTION,StrList);
|
---|
107 | if (StrList.IndexOf(Key)=-1) or (OptionsIniFile.ReadInteger(DEFAULT_SECTION,Key,-999)<>Value) then begin
|
---|
108 | OptionsIniFile.WriteInteger(User.Name,Key,Value); //Only write user value if differs from default value
|
---|
109 | end;
|
---|
110 | end;
|
---|
111 | end;
|
---|
112 | end;
|
---|
113 |
|
---|
114 | Procedure WriteString(const Key: String; Value: String; AsDefault : boolean = false);
|
---|
115 | begin
|
---|
116 | if Initialized = False then TMGOptionsInitialize;
|
---|
117 | if AsDefault then begin
|
---|
118 | if USE_SERVER_INI then begin
|
---|
119 | WriteRPCStr(DEFAULT_SECTION,Key,Value);
|
---|
120 | end else begin
|
---|
121 | OptionsIniFile.WriteString(DEFAULT_SECTION,Key,Value);
|
---|
122 | end;
|
---|
123 | end else begin
|
---|
124 | if USE_SERVER_INI then begin
|
---|
125 | WriteRPCStr(User.Name,Key,Value);
|
---|
126 | end else begin
|
---|
127 | OptionsIniFile.ReadSection(DEFAULT_SECTION,StrList);
|
---|
128 | if (StrList.IndexOf(Key)=-1) or (OptionsIniFile.ReadString(DEFAULT_SECTION,Key,'xxx')<>Value) then begin
|
---|
129 | OptionsIniFile.WriteString(User.Name,Key,Value); //Only write user value if differs from default value
|
---|
130 | end;
|
---|
131 | end;
|
---|
132 | end;
|
---|
133 | end;
|
---|
134 |
|
---|
135 | Function ReadBool(const Key: String; Default: Boolean): Boolean;
|
---|
136 | begin
|
---|
137 | if Initialized = False then TMGOptionsInitialize;
|
---|
138 | if USE_SERVER_INI then begin
|
---|
139 | Result := StrToBool(ReadRPCStr(User.Name,Key,BoolToStr(Default)));
|
---|
140 | end else begin
|
---|
141 | OptionsIniFile.ReadSection(User.Name,StrList);
|
---|
142 | if StrList.IndexOf(Key) > -1 then begin
|
---|
143 | Result := OptionsIniFile.ReadBool(User.Name,Key,Default);
|
---|
144 | end else begin
|
---|
145 | Result := OptionsIniFile.ReadBool(DEFAULT_SECTION,Key,Default);
|
---|
146 | end;
|
---|
147 | end;
|
---|
148 | end;
|
---|
149 |
|
---|
150 | Function ReadInteger(const Key: String; Default: Integer): Integer;
|
---|
151 | begin
|
---|
152 | if Initialized = False then TMGOptionsInitialize;
|
---|
153 | if USE_SERVER_INI then begin
|
---|
154 | Result := StrToInt(ReadRPCStr(User.Name,Key,IntToStr(Default)));
|
---|
155 | end else begin
|
---|
156 | OptionsIniFile.ReadSection(User.Name,StrList);
|
---|
157 | if StrList.IndexOf(Key) > -1 then begin
|
---|
158 | Result := OptionsIniFile.ReadInteger(User.Name,Key,Default);
|
---|
159 | end else begin
|
---|
160 | Result := OptionsIniFile.ReadInteger(DEFAULT_SECTION,Key,Default);
|
---|
161 | end;
|
---|
162 | end;
|
---|
163 | end;
|
---|
164 |
|
---|
165 | Function ReadString(const Key: String; Default: String): String;
|
---|
166 | begin
|
---|
167 | if Initialized = False then TMGOptionsInitialize;
|
---|
168 | if USE_SERVER_INI then begin
|
---|
169 | Result := ReadRPCStr(User.Name,Key,Default);
|
---|
170 | end else begin
|
---|
171 | OptionsIniFile.ReadSection(User.Name,StrList);
|
---|
172 | if StrList.IndexOf(Key) > -1 then begin
|
---|
173 | Result := OptionsIniFile.ReadString(User.Name,Key,Default);
|
---|
174 | end else begin
|
---|
175 | Result := OptionsIniFile.ReadString(DEFAULT_SECTION,Key,Default);
|
---|
176 | end;
|
---|
177 | end;
|
---|
178 | end;
|
---|
179 |
|
---|
180 |
|
---|
181 | initialization
|
---|
182 | Initialized := false;
|
---|
183 |
|
---|
184 |
|
---|
185 | finalization
|
---|
186 | OptionsIniFile.Free; //kt 8/09
|
---|
187 | StrList.Free;
|
---|
188 |
|
---|
189 | end.
|
---|
190 |
|
---|