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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
|
(*
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
*)
{-----------------------------------------------------------------------------
hpp_bookmarks.pas (historypp project)
Version: 1.5
Created: 02.04.2006
Author: Oxygen
[ Description ]
Hello, this is dummy text
[ History ]
1.5 (02.04.2006)
First version
[ Modifications ]
none
[ Known Issues ]
none
Contributors: theMIROn, Art Fedorov
-----------------------------------------------------------------------------}
unit hpp_bookmarks;
interface
uses windows, m_api, hpp_jclSysUtils, SysUtils;
type
TEventData = record
hDBEvent: THandle;
CRC32: DWord;
Timestamp: Cardinal;
end;
PEventData = ^TEventData;
TBookmarksHash = class;
TContactBookmarks = class(TObject)
private
Bookmarks: TBookmarksHash;
hContact: THandle;
FContactCP: Cardinal;
function GetBookmarked(Index: THandle): Boolean;
procedure SetBookmarked(Index: THandle; const Value: Boolean);
function GetBookmarkName(Index: THandle): String;
procedure SetBookmarkName(Index: THandle; const Value: String);
procedure LoadBookmarks;
procedure SaveBookmarks;
procedure DeleteBookmarks;
function GetCount: Integer;
function GetItems(Index: Integer): THandle;
function GetNames(Index: Integer): String;
public
constructor Create(AContact: THandle);
destructor Destroy; override;
procedure Clear;
property Bookmarked[Index: THandle]: Boolean read GetBookmarked write SetBookmarked;
property BookmarkName[Index: THandle]: String read GetBookmarkName write SetBookmarkName;
property Items[Index: Integer]: THandle read GetItems;
property Names[Index: Integer]: String read GetNames;
property Count: Integer read GetCount;
property Contact: THandle read hContact;
property ContactCP: Cardinal read FContactCP;
end;
PContactBookmarks = ^TContactBookmarks;
TPseudoHashEntry = record
Key: uint_ptr;
Value: pointer;
end;
PPseudoHashEntry = ^TPseudoHashEntry;
TPseudoHash = class(TObject)
private
Table: array of TPseudoHashEntry;
procedure RemoveByIndex(Index: Integer);
// procedure InsertByIndex(Index: Integer; Key:Cardinal;Value: pointer);
protected
function AddKey(Key:uint_ptr; Value: pointer): Boolean;
function GetKey(Key:uint_ptr; var Value: pointer): Boolean;
function RemoveKey(Key: uint_ptr): Boolean;
public
destructor Destroy; override;
end;
TContactsHash = class(TPseudoHash)
private
function GetContactBookmarks(Index: THandle): TContactBookmarks;
public
property Items[Index: THandle]: TContactBookmarks read GetContactBookmarks; default;
function RemoveItem(Index: THandle): Boolean;
destructor Destroy; override;
end;
TBookmarksHash = class(TPseudoHash)
private
Contact: TContactBookmarks;
function GetHasItem(Index: THandle): Boolean;
// function GetBookmark(hDBEvent: THandle; var EventData: TEventData): Boolean;
function AddItem(hDBEvent: THandle): Boolean;
function RemoveItem(hDBEvent: THandle): Boolean;
function AddItemName(hDBEvent: THandle; Value: String): Boolean;
function GetItemName(hDBEvent: THandle): String;
function RemoveItemName(hDBEvent: THandle): Boolean;
function FindEventByTimestampAndCrc(ped: PEventData): Boolean;
public
constructor Create(AContact: TContactBookmarks);
destructor Destroy; override;
function Clear: Integer;
function AddEventData(var EventData: TEventData): Boolean;
property HasItem[Index: THandle]: Boolean read GetHasItem; default;
end;
TBookmarkServer = class(TObject)
private
hookContactDeleted,
hookEventDeleted,
hookEventAdded: THandle;
CachedContacts: TContactsHash;
function GetContacts(Index: THandle): TContactBookmarks;
protected
procedure ContactDeleted(hContact: THandle);
procedure EventDeleted(hContact,hDBEvent: THandle);
procedure EventAdded(hContact,hDBEvent: THandle);
public
constructor Create;
destructor Destroy; override;
property Contacts[Index: THandle]: TContactBookmarks read GetContacts; default;
end;
var
BookmarkServer: TBookmarkServer;
procedure hppInitBookmarkServer;
procedure hppDeinitBookmarkServer;
implementation
uses hpp_events, hpp_contacts, hpp_global, Checksum, hpp_database, hpp_forms;
procedure hppInitBookmarkServer;
begin
BookmarkServer := TBookmarkServer.Create;
end;
procedure hppDeinitBookmarkServer;
begin
BookmarkServer.Free;
end;
function ContactDeletedHelper(wParam: WPARAM; lParam: LPARAM): Integer; cdecl;
// wParam: hContact, lParam: 0
begin
if Assigned(BookmarkServer) then
BookmarkServer.ContactDeleted(wParam);
Result := 0;
end;
function EventDeletedHelper(wParam: WPARAM; lParam: LPARAM): Integer; cdecl;
// wParam: hContact, lParam: hDBEvent
begin
if Assigned(BookmarkServer) then
BookmarkServer.EventDeleted(wParam,lParam);
Result := 0;
end;
function EventAddedHelper(wParam: WPARAM; lParam: LPARAM): Integer; cdecl;
// wParam: hContact, lParam: hDBEvent
begin
if Assigned(BookmarkServer) then
BookmarkServer.EventAdded(wParam,lParam);
Result := 0;
end;
function DynArrayComparePseudoHash(Item1, Item2: Pointer): Integer;
begin
{$OVERFLOWCHECKS ON}
Result := PInteger(@PPseudoHashEntry(Item1)^.Key)^ - PInteger(@PPseudoHashEntry(Item2)^.Key)^;
{$OVERFLOWCHECKS OFF}
end;
{ TBookmarkServer }
function TBookmarkServer.GetContacts(Index: THandle): TContactBookmarks;
begin
Result := CachedContacts[Index];
end;
constructor TBookmarkServer.Create;
begin
inherited;
CachedContacts := TContactsHash.Create;
hookContactDeleted := HookEvent(ME_DB_CONTACT_DELETED,ContactDeletedHelper);
hookEventDeleted := HookEvent(ME_DB_EVENT_DELETED,EventDeletedHelper);
hookEventAdded := HookEvent(ME_DB_EVENT_ADDED,EventAddedHelper);
end;
destructor TBookmarkServer.Destroy;
begin
UnhookEvent(hookContactDeleted);
UnhookEvent(hookEventDeleted);
UnhookEvent(hookEventAdded);
CachedContacts.Free;
BookmarkServer := nil;
inherited;
end;
procedure TBookmarkServer.ContactDeleted(hContact: THandle);
begin
// do we really need to delete bookmarks from contact,
// if he is about to be deleted? I think don't
//Contacts[hContact].DeleteBookmarks;
CachedContacts.RemoveItem(hContact);
end;
procedure TBookmarkServer.EventDeleted(hContact, hDBEvent: THandle);
begin
Contacts[hContact].Bookmarked[hDBEvent] := false;
end;
procedure TBookmarkServer.EventAdded(hContact, hDBEvent: THandle);
begin
;
end;
{ TContactBookmarks }
constructor TContactBookmarks.Create(AContact: THandle);
begin
hContact := AContact;
FContactCP := GetContactCodepage(hContact);
Bookmarks := TBookmarksHash.Create(Self);
// read bookmarks from DB here
LoadBookmarks;
end;
procedure TContactBookmarks.DeleteBookmarks;
begin
DBDelete(hContact,hppDBName,'Bookmarks');
end;
destructor TContactBookmarks.Destroy;
begin
Bookmarks.Free;
inherited;
end;
function TContactBookmarks.GetBookmarked(Index: THandle): Boolean;
begin
Result := Bookmarks[Index];
end;
function TContactBookmarks.GetCount: Integer;
begin
Result := Length(Bookmarks.Table);
end;
function TContactBookmarks.GetItems(Index: Integer): THandle;
begin
Result := PEventData(Bookmarks.Table[Index].Value)^.hDBEvent;
end;
function TContactBookmarks.GetNames(Index: Integer): String;
var
hDBEvent: THandle;
begin
hDBEvent := PEventData(Bookmarks.Table[Index].Value)^.hDBEvent;
Result := Bookmarks.GetItemName(hDBEvent)
end;
procedure TContactBookmarks.LoadBookmarks;
var
i: Integer;
mem: PEventData;
mem_org: Pointer;
mem_len: Integer;
rec_size: Word;
count: Integer;
ed: PEventData;
AllOk: Boolean;
begin
if not GetDBBlob(hContact, hppDBName, 'Bookmarks', mem_org, mem_len) then
exit;
try
AllOk := True;
if mem_len < SizeOf(Word) then
raise EAbort.Create('Too small bookmarks rec');
rec_size := PWord(mem_org)^;
if rec_size < SizeOf(TEventData) then
raise EAbort.Create('Bookmark size is too small');
Count := (mem_len - SizeOf(Word)) div rec_size;
mem := pointer(uint_ptr(mem_org) + SizeOf(Word));
for i := 0 to Count - 1 do
begin
ed := PEventData(int_ptr(mem) + i * rec_size);
if not Bookmarks.AddEventData(ed^) then
AllOk := false;
end;
FreeMem(mem_org, mem_len);
// if we found that some items are missing or different, save
// correct copy:
if not AllOk then
SaveBookmarks;
except
DeleteBookmarks;
end;
end;
procedure TContactBookmarks.SaveBookmarks;
var
mem: Pointer;
mem_len: Integer;
i: Integer;
src,dst: PEventData;
begin
if Length(Bookmarks.Table) > 0 then
begin
mem_len := Length(Bookmarks.Table) * SizeOf(TEventData) + SizeOf(Word);
GetMem(mem, mem_len);
PWord(mem)^ := Word(SizeOf(TEventData));
for i := 0 to High(Bookmarks.Table) do
begin
src := PEventData(Bookmarks.Table[i].Value);
dst := PEventData(int_ptr(mem) + SizeOf(Word) + i * SizeOf(TEventData));
Move(src^, dst^, SizeOf(src^));
end;
WriteDBBlob(hContact, hppDBName, 'Bookmarks', mem, mem_len);
FreeMem(mem, mem_len);
end
else
begin
DeleteBookmarks;
end;
end;
procedure TContactBookmarks.SetBookmarked(Index: THandle; const Value: Boolean);
var
res: Boolean;
begin
if Value then
res := Bookmarks.AddItem(Index)
else
res := Bookmarks.RemoveItem(Index);
if res then
begin
SaveBookmarks;
NotifyAllForms(HM_NOTF_BOOKMARKCHANGED, hContact, Index);
end;
end;
procedure TContactBookmarks.SetBookmarkName(Index: THandle; const Value: String);
begin
Bookmarks.AddItemName(Index,Value);
end;
function TContactBookmarks.GetBookmarkName(Index: THandle): String;
begin
Result := Bookmarks.GetItemName(Index);
end;
procedure TContactBookmarks.Clear;
begin
Bookmarks.Clear;
DeleteBookmarks;
//NotifyAllForms(HM_NOTF_BOOKMARKCHANGED,hContact,0);
end;
{ TPseudoHash }
function TPseudoHash.AddKey(Key:uint_ptr; Value: pointer): Boolean;
var
Nearest: Integer;
ph: TPseudoHashEntry;
i: Integer;
begin
Result := False;
ph.Key := Key;
Nearest := SearchDynArray(Table,SizeOf(TPseudoHashEntry),DynArrayComparePseudoHash,@ph,True);
if Nearest <> -1 then begin // we have nearest or match
if Table[Nearest].Key = Key then
exit;
if Table[Nearest].Key < Key then
Inc(Nearest);
end
else
Nearest := 0; // table is empty
SetLength(Table,Length(Table)+1);
for i := Length(Table)-1 downto Nearest do
Table[i] := Table[i-1];
Table[Nearest].Key := Key;
Table[Nearest].Value := Value;
Result := True;
end;
destructor TPseudoHash.Destroy;
begin
SetLength(Table,0);
inherited;
end;
function TPseudoHash.GetKey(Key: uint_ptr; var Value: pointer): Boolean;
var
ph: TPseudoHashEntry;
res: Integer;
begin
Result := false;
ph.Key := Key;
res := SearchDynArray(Table, SizeOf(TPseudoHashEntry), DynArrayComparePseudoHash, @ph, false);
if res <> -1 then
begin
Result := True;
Value := Table[res].Value;
end;
end;
(*
procedure TPseudoHash.InsertByIndex(Index: Integer; Key:cardinal; Value: pointer);
begin
//
end;
*)
procedure TPseudoHash.RemoveByIndex(Index: Integer);
var
i: Integer;
begin
for i := Index to Length(Table) - 2 do
Table[i] := Table[i+1];
SetLength(Table,Length(Table)-1);
end;
function TPseudoHash.RemoveKey(Key: uint_ptr): Boolean;
var
idx: Integer;
ph: TPseudoHashEntry;
begin
Result := False;
ph.Key := Key;
idx := SearchDynArray(Table,SizeOf(TPseudoHashEntry),DynArrayComparePseudoHash,@ph,False);
if idx = -1 then exit;
RemoveByIndex(idx);
Result := True;
end;
{ TContactsHash }
destructor TContactsHash.Destroy;
var
i: Integer;
begin
for i := 0 to Length(Table) - 1 do
TContactBookmarks(Pointer(Table[i].Value)).Free;
inherited;
end;
function TContactsHash.GetContactBookmarks(Index: THandle): TContactBookmarks;
var
val: Pointer;
begin
// Result := nil;
if GetKey(Cardinal(Index), val) then
Result := TContactBookmarks(val)
else
begin
Result := TContactBookmarks.Create(Index);
AddKey(Cardinal(Index), pointer(Result));
end;
end;
function TContactsHash.RemoveItem(Index: THandle): Boolean;
var
val: Pointer;
begin
Result := false;
if GetKey(Cardinal(Index), val) then
begin
RemoveKey(Cardinal(Index));
TContactBookmarks(val).Free;
Result := True;
end;
end;
{ TBookmarksHash }
function TBookmarksHash.AddEventData(var EventData: TEventData): Boolean;
var
ped: PEventData;
ts: Cardinal;
ItemExists, ItemCorrect, NewItemFound: Boolean;
begin
GetMem(ped, SizeOf(TEventData));
ped^.hDBEvent := EventData.hDBEvent;
ped^.CRC32 := EventData.CRC32;
ped^.Timestamp := EventData.Timestamp;
ItemExists := (CallService(MS_DB_EVENT_GETBLOBSIZE, EventData.hDBEvent, 0) >= 0);
ItemCorrect := false; // added by Awkward, default value
if ItemExists then
begin
ts := GetEventTimestamp(EventData.hDBEvent);
ItemCorrect := (ts = ped^.Timestamp);
// we might check for CRC32 here also?
end;
if (not ItemExists) or (not ItemCorrect) then
begin
Result := false;
NewItemFound := FindEventByTimestampAndCrc(ped); // try to find the item
if not NewItemFound then
begin // can not find
FreeMem(ped, SizeOf(TEventData));
exit;
end
else
AddKey(ped^.hDBEvent, ped);
// exit, but leave Result = False as we want to resave after this load
end
else
Result := AddKey(ped^.hDBEvent, ped); // item exists, add as normal
end;
function TBookmarksHash.AddItem(hDBEvent: THandle): Boolean;
var
ped: PEventData;
hi: THistoryItem;
begin
GetMem(ped,SizeOf(TEventData));
ped^.hDBEvent := hDBEvent;
hi := ReadEvent(hDBEvent,Contact.ContactCP);
ped^.Timestamp := hi.Time;
CalcCRC32(PWideChar(hi.Text),Length(hi.Text)*SizeOf(WideChar),Cardinal(ped^.CRC32));
Result := AddKey(hDBEvent,ped);
end;
function TBookmarksHash.AddItemName(hDBEvent: THandle; Value: String): Boolean;
begin
Result := (WriteDBWideStr(Contact.hContact,hppDBName,AnsiString('bm'+intToStr(hDBEvent)),Value) = 0);
end;
function TBookmarksHash.GetItemName(hDBEvent: THandle): String;
begin
Result := GetDBWideStr(Contact.hContact,hppDBName,AnsiString('bm'+intToStr(hDBEvent)),'');
end;
function TBookmarksHash.RemoveItemName(hDBEvent: THandle): Boolean;
begin
if DBExists(Contact.hContact,hppDBName,AnsiString('bm'+intToStr(hDBEvent))) then
Result := DBDelete(Contact.hContact,hppDBName,AnsiString('bm'+intToStr(hDBEvent)))
else
Result := True;
end;
constructor TBookmarksHash.Create(AContact: TContactBookmarks);
begin
Contact := AContact;
end;
destructor TBookmarksHash.Destroy;
begin
Clear;
inherited;
end;
function TBookmarksHash.Clear: Integer;
var
i: Integer;
begin
for i := 0 to Length(Table) - 1 do
FreeMem(PEventData(Table[i].Value),SizeOf(TEventData));
Result := Length(Table);
SetLength(Table,0);
end;
// currently finds events with similar timestamp ONLY
function TBookmarksHash.FindEventByTimestampAndCrc(ped: PEventData): Boolean;
var
hDBEvent: THandle;
first_ts,last_ts,ts,cur_ts: Integer;
StartFromFirst: Boolean;
begin
Result := false;
hDBEvent := CallService(MS_DB_EVENT_FINDFIRST, Contact.hContact, 0);
if hDBEvent = 0 then
exit;
first_ts := GetEventTimestamp(hDBEvent);
hDBEvent := CallService(MS_DB_EVENT_FINDLAST, Contact.hContact, 0);
if hDBEvent = 0 then
exit;
last_ts := GetEventTimestamp(hDBEvent);
ts := ped^.Timestamp;
if (ts < first_ts) or (ts > last_ts) then
exit;
StartFromFirst := ((ts - first_ts) < (last_ts - ts));
if StartFromFirst then
begin
hDBEvent := CallService(MS_DB_EVENT_FINDFIRST, Contact.hContact, 0);
while hDBEvent <> 0 do
begin
cur_ts := GetEventTimestamp(hDBEvent);
if cur_ts > ts then
break;
if cur_ts = ts then
begin
ped^.hDBEvent := hDBEvent;
Result := True;
break;
end;
hDBEvent := CallService(MS_DB_EVENT_FINDNEXT, hDBEvent, 0);
end;
end
else
begin
hDBEvent := CallService(MS_DB_EVENT_FINDLAST, Contact.hContact, 0);
while hDBEvent <> 0 do
begin
cur_ts := GetEventTimestamp(hDBEvent);
if ts > cur_ts then
break;
if cur_ts = ts then
begin
ped^.hDBEvent := hDBEvent;
Result := True;
break;
end;
hDBEvent := CallService(MS_DB_EVENT_FINDPREV, hDBEvent, 0);
end;
end;
end;
(*
function TBookmarksHash.GetBookmark(hDBEvent: THandle;
var EventData: TEventData): Boolean;
var
val: Pointer;
begin
Result := False;
if GetKey(Cardinal(hDBEvent),val) then begin
EventData := PEventData(val)^;
Result := True;
end;
end;
*)
function TBookmarksHash.GetHasItem(Index: THandle): Boolean;
var
val: Pointer;
begin
Result := False;
if GetKey(uint_ptr(Index),val) then
Result := True;
end;
function TBookmarksHash.RemoveItem(hDBEvent: THandle): Boolean;
var
ped: PEventData;
begin
Result := false;
if GetKey(uint_ptr(hDBEvent), pointer(ped)) then
begin
RemoveKey(uint_ptr(hDBEvent));
FreeMem(ped, SizeOf(ped^));
RemoveItemName(hDBEvent);
Result := True;
end;
end;
end.
|