source: cprs/branches/tmg-cprs/CPRS-Chart/uTMGOptions.pas@ 1156

Last change on this file since 1156 was 793, checked in by Kevin Toppenberg, 15 years ago

update

File size: 6.8 KB
Line 
1unit uTMGOptions;
2//kt Added entire unit. 2/10/10
3
4interface
5
6uses
7 IniFiles, SysUtils, Forms, uCore, ORFn, ORNet, Trpcb;
8
9var
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
20implementation
21
22uses
23 Classes;
24
25const
26 DEFAULT_SECTION = 'DEFAULT';
27
28var
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;
44 CallBroker;
45 if RPCBrokerV.Results.Count >0 then begin
46 USE_SERVER_INI := StrToBool(RPCBrokerV.Results.Strings[0]); {returns 1 if available, 0 if not available}
47 end else begin
48 USE_SERVER_INI := false;
49 end;
50 Initialized := true;
51 end;
52
53 procedure WriteRPCStr(const Section, Key: String; Value: String);
54 begin
55 RPCBrokerV.remoteprocedure := 'TMG INIFILE SET';
56 RPCBrokerV.Param[0].Value := Section;
57 RPCBrokerV.Param[0].ptype := literal;
58 RPCBrokerV.Param[1].Value := Key;
59 RPCBrokerV.Param[1].ptype := literal;
60 RPCBrokerV.Param[2].Value := Value;
61 RPCBrokerV.Param[2].ptype := literal;
62 //RPCBrokerV.Call;
63 CallBroker;
64 end;
65
66 function ReadRPCStr(const Section, Key, Default: String) : string;
67 begin
68 RPCBrokerV.remoteprocedure := 'TMG INIFILE GET';
69 RPCBrokerV.param[0].ptype := literal;
70 RPCBrokerV.param[0].value := Section;
71 RPCBrokerV.Param[1].ptype := literal;
72 RPCBrokerV.param[1].value := Key;
73 RPCBrokerV.Param[2].ptype := literal;
74 RPCBrokerV.param[2].value := Default;
75 //Result := Piece(RPCBrokerV.StrCall,'^',2);
76 CallBroker;
77 if RPCBrokerV.Results.Count > 0 then begin
78 Result := Piece(RPCBrokerV.Results.Strings[0],'^',2);
79 end else begin
80 Result := Default;
81 end;
82 end;
83
84
85 Procedure WriteBool(const Key: String; Value: Boolean; AsDefault : boolean = false);
86 begin
87 if Initialized = False then TMGOptionsInitialize;
88 if AsDefault then begin
89 if USE_SERVER_INI then begin
90 WriteRPCStr(DEFAULT_SECTION,Key,BoolToStr(Value));
91 end else begin
92 OptionsIniFile.WriteBool(DEFAULT_SECTION,Key,Value);
93 end;
94 end else begin
95 if USE_SERVER_INI then begin
96 WriteRPCStr(User.Name,Key,BoolToStr(Value));
97 end else begin
98 OptionsIniFile.ReadSection(DEFAULT_SECTION,StrList);
99 if (StrList.IndexOf(Key)=-1) or (OptionsIniFile.ReadBool(DEFAULT_SECTION,Key,false)<>Value) then begin
100 OptionsIniFile.WriteBool(User.Name,Key,Value); //Only write user value if differs from default value
101 end;
102 end;
103 end;
104 end;
105
106 Procedure WriteInteger(const Key: String; Value: Integer; AsDefault : boolean = false);
107 begin
108 if Initialized = False then TMGOptionsInitialize;
109 if AsDefault then begin
110 if USE_SERVER_INI then begin
111 WriteRPCStr(DEFAULT_SECTION,Key,IntToStr(Value));
112 end else begin
113 OptionsIniFile.WriteInteger(DEFAULT_SECTION,Key,Value);
114 end;
115 end else begin
116 if USE_SERVER_INI then begin
117 WriteRPCStr(User.Name,Key,IntToStr(Value));
118 end else begin
119 OptionsIniFile.ReadSection(DEFAULT_SECTION,StrList);
120 if (StrList.IndexOf(Key)=-1) or (OptionsIniFile.ReadInteger(DEFAULT_SECTION,Key,-999)<>Value) then begin
121 OptionsIniFile.WriteInteger(User.Name,Key,Value); //Only write user value if differs from default value
122 end;
123 end;
124 end;
125 end;
126
127 Procedure WriteString(const Key: String; Value: String; AsDefault : boolean = false);
128 begin
129 if Initialized = False then TMGOptionsInitialize;
130 if AsDefault then begin
131 if USE_SERVER_INI then begin
132 WriteRPCStr(DEFAULT_SECTION,Key,Value);
133 end else begin
134 OptionsIniFile.WriteString(DEFAULT_SECTION,Key,Value);
135 end;
136 end else begin
137 if USE_SERVER_INI then begin
138 WriteRPCStr(User.Name,Key,Value);
139 end else begin
140 OptionsIniFile.ReadSection(DEFAULT_SECTION,StrList);
141 if (StrList.IndexOf(Key)=-1) or (OptionsIniFile.ReadString(DEFAULT_SECTION,Key,'xxx')<>Value) then begin
142 OptionsIniFile.WriteString(User.Name,Key,Value); //Only write user value if differs from default value
143 end;
144 end;
145 end;
146 end;
147
148 Function ReadBool(const Key: String; Default: Boolean): Boolean;
149 begin
150 if Initialized = False then TMGOptionsInitialize;
151 if USE_SERVER_INI then begin
152 Result := StrToBool(ReadRPCStr(User.Name,Key,BoolToStr(Default)));
153 end else begin
154 OptionsIniFile.ReadSection(User.Name,StrList);
155 if StrList.IndexOf(Key) > -1 then begin
156 Result := OptionsIniFile.ReadBool(User.Name,Key,Default);
157 end else begin
158 Result := OptionsIniFile.ReadBool(DEFAULT_SECTION,Key,Default);
159 end;
160 end;
161 end;
162
163 Function ReadInteger(const Key: String; Default: Integer): Integer;
164 begin
165 if Initialized = False then TMGOptionsInitialize;
166 if USE_SERVER_INI then begin
167 Result := StrToInt(ReadRPCStr(User.Name,Key,IntToStr(Default)));
168 end else begin
169 OptionsIniFile.ReadSection(User.Name,StrList);
170 if StrList.IndexOf(Key) > -1 then begin
171 Result := OptionsIniFile.ReadInteger(User.Name,Key,Default);
172 end else begin
173 Result := OptionsIniFile.ReadInteger(DEFAULT_SECTION,Key,Default);
174 end;
175 end;
176 end;
177
178 Function ReadString(const Key: String; Default: String): String;
179 begin
180 if Initialized = False then TMGOptionsInitialize;
181 if USE_SERVER_INI then begin
182 Result := ReadRPCStr(User.Name,Key,Default);
183 end else begin
184 OptionsIniFile.ReadSection(User.Name,StrList);
185 if StrList.IndexOf(Key) > -1 then begin
186 Result := OptionsIniFile.ReadString(User.Name,Key,Default);
187 end else begin
188 Result := OptionsIniFile.ReadString(DEFAULT_SECTION,Key,Default);
189 end;
190 end;
191 end;
192
193
194initialization
195 Initialized := false;
196
197
198finalization
199 OptionsIniFile.Free; //kt 8/09
200 StrList.Free;
201
202end.
203
Note: See TracBrowser for help on using the repository browser.