1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
(*
History++ plugin for Miranda IM: the free IM client for Microsoft* Windows*
Copyright (C) 2006-2009 theMIROn, 2003-2006 Art Fedorov.
History+ parts (C) 2001 Christian Kastner
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)
unit hpp_eventfilters;
interface
uses Types, SysUtils, Classes, m_api, hpp_global;
const
// filter modes
FM_INCLUDE = 0; // show all events from filEvents (default)
FM_EXCLUDE = 1; // show all events except from filEvents
const
MAX_FILTER_NAME_LENGTH = 33; // make it uneven, so our db record would align in 4 bytes
type
ThppEventFilter = record
Name : String;
Events : TMessageTypes; // resulting events mask generated from filMode and filEvents, filled in runtime
filMode : Byte; // FM_* consts
filEvents: TMessageTypes; // filter events which are combined with filMode
filCustom: Word; // filter events which are combined with filMode
end;
ThppEventFilterArray = array of ThppEventFilter;
var
hppEventFilters: ThppEventFilterArray;
hppDefEventFilters: ThppEventFilterArray;
procedure InitEventFilters;
procedure ReadEventFilters;
procedure WriteEventFilters;
procedure ResetEventFiltersToDefault;
procedure CopyEventFilters(var Src,Dest: ThppEventFilterArray);
function GetShowAllEventsIndex(Arr: ThppEventFilterArray = nil): Integer;
function MessageTypesToDWord(mt: TMessageTypes): DWord;
// compile filMode & filEvents into Events:
function GenerateEvents(filMode: Byte; filEvents: TMessageTypes): TMessageTypes;
// compile filMode & filEvents into Events for all filters
procedure GenerateEventFilters(var Filters: array of ThppEventFilter);
const
EventsInclude: TMessageTypes = [];
EventsDirection: TMessageTypes = [mtIncoming,mtOutgoing];
EventsExclude: TMessageTypes = [mtUnknown,mtCustom];
EventsCustom: TMessageTypes = [mtCustom];
implementation
uses
hpp_database, hpp_forms;
var
filterAll: TMessageTypes;
const
hppIntDefEventFilters: array[0..12] of ThppEventFilter = (
(Name: 'Show all events'; Events: []; filMode: FM_EXCLUDE; filEvents: []),
(Name: 'Messages'; Events: []; filMode: FM_INCLUDE; filEvents: [mtMessage,mtIncoming,mtOutgoing]),
(Name: 'Link URLs'; Events: []; filMode: FM_INCLUDE; filEvents: [mtUrl,mtIncoming,mtOutgoing]),
(Name: 'Files'; Events: []; filMode: FM_INCLUDE; filEvents: [mtFile,mtIncoming,mtOutgoing]),
(Name: 'Contacts'; Events: []; filMode: FM_INCLUDE; filEvents: [mtContacts,mtIncoming,mtOutgoing]),
(Name: 'Status changes'; Events: []; filMode: FM_INCLUDE; filEvents: [mtStatus,mtIncoming,mtOutgoing]),
(Name: 'Status message changes'; Events: []; filMode: FM_INCLUDE; filEvents: [mtStatusMessage,mtIncoming,mtOutgoing]),
(Name: 'Nick changes'; Events: []; filMode: FM_INCLUDE; filEvents: [mtNickChange,mtIncoming,mtOutgoing]),
(Name: 'Avatar changes'; Events: []; filMode: FM_INCLUDE; filEvents: [mtAvatarChange,mtIncoming,mtOutgoing]),
(Name: 'WATrack notify'; Events: []; filMode: FM_INCLUDE; filEvents: [mtWATrack,mtIncoming,mtOutgoing]),
(Name: 'Voice calls'; Events: []; filMode: FM_INCLUDE; filEvents: [mtVoiceCall,mtIncoming,mtOutgoing]),
(Name: 'All except changes'; Events: []; filMode: FM_EXCLUDE; filEvents: [mtStatus,mtStatusMessage,mtNickChange,mtAvatarChange]),
(Name: 'All except system'; Events: []; filMode: FM_EXCLUDE; filEvents: [mtSystem])
);
function IsSameAsDefault: Boolean;
var
i: Integer;
begin
Result := False;
if Length(hppDefEventFilters) <> Length(hppEventFilters) then
exit;
for i := 0 to Length(hppEventFilters) - 1 do
begin
if hppEventFilters[i].Name <> hppDefEventFilters[i].Name then
exit;
if hppEventFilters[i].Events <> hppDefEventFilters[i].Events then
exit;
end;
Result := True;
end;
function DWordToMessageTypes(dwmt: DWord): TMessageTypes;
begin
Result := [];
Move(dwmt,Result,SizeOf(Result));
end;
function MessageTypesToDWord(mt: TMessageTypes): DWord;
begin
Result := 0;
Move(mt,Result,SizeOf(mt));
end;
procedure UpdateEventFiltersOnForms;
begin
NotifyAllForms(HM_NOTF_FILTERSCHANGED,0,0);
end;
function GenerateEvents(filMode: Byte; filEvents: TMessageTypes): TMessageTypes;
begin
if filMode = FM_INCLUDE then
Result := filEvents
else
Result := filterAll - filEvents;
Result := Result - EventsExclude + EventsInclude;
end;
procedure GenerateEventFilters(var Filters: array of ThppEventFilter);
var
i: Integer;
begin
for i := 0 to Length(Filters) - 1 do
begin
Filters[i].Events := GenerateEvents(Filters[i].filMode, Filters[i].filEvents);
end;
end;
procedure CopyEventFilters(var Src,Dest: ThppEventFilterArray);
var
i: Integer;
begin
SetLength(Dest,Length(Src));
for i := 0 to Length(Src) - 1 do
begin
Dest[i].Name := Src[i].Name;
Dest[i].Events := Src[i].Events;
Dest[i].filMode := Src[i].filMode;
Dest[i].filEvents := Src[i].filEvents;
Dest[i].filCustom := Src[i].filCustom;
end;
end;
function GetShowAllEventsIndex(Arr: ThppEventFilterArray = nil): Integer;
var
i: Integer;
begin
if Arr = nil then
Arr := hppEventFilters;
Result := 0;
for i := 0 to Length(Arr) - 1 do
if (Arr[i].filMode = FM_EXCLUDE) and (Arr[i].filEvents = []) then
begin
Result := i;
break;
end;
end;
procedure DeleteEventFilterSettings;
var
i: Integer;
begin
i := 1;
while True do
begin
if not DBDelete(hppDBName, AnsiString('EventFilter' + IntToStr(i))) then
break;
Inc(i);
end;
end;
procedure ResetEventFiltersToDefault;
begin
CopyEventFilters(hppDefEventFilters,hppEventFilters);
DeleteEventFilterSettings;
UpdateEventFiltersOnForms;
end;
//----- Stealed fro Tnt -----
function ExtractStringFromStringArray(var P: PChar; Separator: Char = #0): String;
var
Start: PChar;
begin
Start := P;
P := StrScan(Start, Separator);
if P = nil then
begin
Result := Start;
P := StrEnd(Start);
end
else
begin
SetString(Result, Start, P - Start);
Inc(P);
end;
end;
function ExtractStringsFromStringArray(P: PChar; Separator: Char = #0): TWideStringDynArray;
const
GROW_COUNT = 256;
var
Count: Integer;
Item: WideString;
begin
Count := 0;
SetLength(Result, GROW_COUNT);
Item := ExtractStringFromStringArray(P, Separator);
While Item <> '' do
begin
if Count > High(Result) then
SetLength(Result, Length(Result) + GROW_COUNT);
Result[Count] := Item;
Inc(Count);
Item := ExtractStringFromStringArray(P, Separator);
end;
SetLength(Result, Count);
end;
//----- end of Tnt -----
procedure ReadEventFilters;
var
i: Integer;
FilterStr: String;
hexs: TWideStringDynArray;
filEvents: DWord;
filMode: Byte;
filCustom: Word;
begin
SetLength(hppEventFilters, 0);
try
i := 1;
while True do
begin
if not DBExists(hppDBName, AnsiString('EventFilter' + IntToStr(i))) then
begin
if Length(hppEventFilters) = 0 then
raise EAbort.Create('No filters');
break;
end;
FilterStr := GetDBWideStr(hppDBName, AnsiString('EventFilter' + IntToStr(i)), '');
if FilterStr = '' then
break;
SetLength(hppEventFilters, Length(hppEventFilters) + 1);
// parse String
hexs := ExtractStringsFromStringArray(PWideChar(FilterStr),',');
if Length(hexs) < 4 then
raise EAbort.Create('Wrong filter (' + IntToStr(i) + ') format');
filMode := 0;
filEvents := 0;
filCustom := 0;
hppEventFilters[i - 1].Name := hexs[0];
// read filMode
HexToBin(PWideChar(hexs[1]), @filMode, SizeOf(filMode));
hppEventFilters[i - 1].filMode := filMode;
// read filEvents
HexToBin(PWideChar(hexs[2]), @filEvents, SizeOf(filEvents));
hppEventFilters[i - 1].filEvents := DWordToMessageTypes(filEvents);
// read filCustom
HexToBin(PWideChar(hexs[3]), @filEvents, SizeOf(filCustom));
hppEventFilters[i - 1].filCustom := filCustom;
Inc(i);
end;
GenerateEventFilters(hppEventFilters);
except
ResetEventFiltersToDefault;
end;
end;
procedure WriteEventFilters;
var
i: Integer;
FilterStr: String;
hex: String;
begin
if Length(hppEventFilters) = 0 then
begin
ResetEventFiltersToDefault;
exit;
end;
if IsSameAsDefault then
begin
// revert to default state
DeleteEventFilterSettings;
UpdateEventFiltersOnForms;
exit;
end;
for i := 0 to Length(hppEventFilters) - 1 do
begin
FilterStr := Copy(hppEventFilters[i].Name, 1, MAX_FILTER_NAME_LENGTH);
// add filMode
SetLength(hex, SizeOf(hppEventFilters[i].filMode) * 2);
BinToHex(@hppEventFilters[i].filMode, PChar(hex),SizeOf(hppEventFilters[i].filMode));
FilterStr := FilterStr + ',' + hex;
// add filEvents
SetLength(hex, SizeOf(hppEventFilters[i].filEvents) * 2);
BinToHex(@hppEventFilters[i].filEvents, PChar(hex),SizeOf(hppEventFilters[i].filEvents));
FilterStr := FilterStr + ',' + hex;
// add filCustom
SetLength(hex, SizeOf(hppEventFilters[i].filCustom) * 2);
BinToHex(@hppEventFilters[i].filCustom, PChar(hex),SizeOf(hppEventFilters[i].filCustom));
FilterStr := FilterStr + ',' + hex;
WriteDBWideStr(hppDBName, AnsiString('EventFilter' + IntToStr(i + 1)), FilterStr);
end;
// delete left filters if we have more than Length(hppEventFilters)
i := Length(hppEventFilters) + 1;
while True do
begin
if not DBDelete(hppDBName, AnsiString('EventFilter' + IntToStr(i))) then
break;
Inc(i);
end;
UpdateEventFiltersOnForms;
end;
procedure InitEventFilters;
var
i: Integer;
mt: TMessageType;
begin
// translate and copy internal default static array to dynamic array
SetLength(hppDefEventFilters, Length(hppIntDefEventFilters));
for i := 0 to High(hppIntDefEventFilters) do
begin
hppDefEventFilters[i].Name := Copy(TranslateUnicodeString(hppIntDefEventFilters[i].Name), 1,
MAX_FILTER_NAME_LENGTH { TRANSLATE-IGNORE } );
hppDefEventFilters[i].filMode := hppIntDefEventFilters[i].filMode;
hppDefEventFilters[i].filEvents := hppIntDefEventFilters[i].filEvents;
hppDefEventFilters[i].filCustom := hppIntDefEventFilters[i].filCustom;
end;
filterAll := [];
for mt := Low(TMessageType) to High(TMessageType) do
Include(filterAll, mt);
GenerateEventFilters(hppDefEventFilters);
end;
end.
|