summaryrefslogtreecommitdiff
path: root/plugins/HistoryPlusPlus/hpp_database.pas
blob: 4341fa64e2e3a4c6f01ea3b65344380a0eddefa5 (plain)
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
(*
    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_database (historypp project)

 Version:   1.0
 Created:   31.03.2003
 Author:    Oxygen

 [ Description ]

 Helper routines for database use

 [ History ]
 1.0 (31.03.2003) - Initial version

 [ Modifications ]

 [ Knows Inssues ]
 None

 Contributors: theMIROn, Art Fedorov
-----------------------------------------------------------------------------}


unit hpp_database;

interface

uses m_api, windows;

procedure SetSafetyMode(Safe: Boolean);

function DBGetContactSettingString(hContact: THandle; const szModule: PAnsiChar; const szSetting: PAnsiChar; ErrorValue: PAnsiChar): AnsiString;
function DBGetContactSettingWideString(hContact: THandle; const szModule: PAnsiChar; const szSetting: PAnsiChar; ErrorValue: PWideChar): WideString;
function DBWriteContactSettingWideString(hContact: THandle; const szModule: PAnsiChar; const szSetting: PAnsiChar; const val: PWideChar): Integer;

function DBDelete(const Module, Param: AnsiString): Boolean; overload;
function DBDelete(const hContact: THandle; const Module, Param: AnsiString): Boolean; overload;
function DBExists(const Module, Param: AnsiString): Boolean; overload;
function DBExists(const hContact: THandle; const Module, Param: AnsiString): Boolean; overload;

function GetDBBlob(const Module,Param: AnsiString; var Value: Pointer; var Size: Integer): Boolean; overload;
function GetDBBlob(const hContact: THandle; const Module,Param: AnsiString; var Value: Pointer; var Size: Integer): Boolean; overload;
function GetDBStr(const Module,Param: AnsiString; const Default: AnsiString): AnsiString; overload;
function GetDBStr(const hContact: THandle; const Module,Param: AnsiString; const Default: AnsiString): AnsiString; overload;
function GetDBWideStr(const Module,Param: AnsiString; const Default: WideString): WideString; overload;
function GetDBWideStr(const hContact: THandle; const Module,Param: AnsiString; const Default: WideString): WideString; overload;
function GetDBInt(const Module,Param: AnsiString; Default: Integer): Integer; overload;
function GetDBInt(const hContact: THandle; const Module,Param: AnsiString; Default: Integer): Integer; overload;
function GetDBWord(const Module,Param: AnsiString; Default: Word): Word; overload;
function GetDBWord(const hContact: THandle; const Module,Param: AnsiString; Default: Word): Word; overload;
function GetDBDWord(const Module,Param: AnsiString; Default: DWord): DWord; overload;
function GetDBDWord(const hContact: THandle; const Module,Param: AnsiString; Default: DWord): DWord; overload;
function GetDBByte(const Module,Param: AnsiString; Default: Byte): Byte; overload;
function GetDBByte(const hContact: THandle; const Module,Param: AnsiString; Default: Byte): Byte; overload;
function GetDBBool(const Module,Param: AnsiString; Default: Boolean): Boolean; overload;
function GetDBBool(const hContact: THandle; const Module,Param: AnsiString; Default: Boolean): Boolean; overload;
function GetDBDateTime(const hContact: THandle; const Module,Param: AnsiString; Default: TDateTime): TDateTime; overload;
function GetDBDateTime(const Module,Param: AnsiString; Default: TDateTime): TDateTime; overload;

function WriteDBBlob(const Module,Param: AnsiString; Value: Pointer; Size: Integer): Integer; overload;
function WriteDBBlob(const hContact: THandle; const Module,Param: AnsiString; Value: Pointer; Size: Integer): Integer; overload;
function WriteDBByte(const Module,Param: AnsiString; Value: Byte): Integer; overload;
function WriteDBByte(const hContact: THandle; const Module,Param: AnsiString; Value: Byte): Integer; overload;
function WriteDBWord(const Module,Param: AnsiString; Value: Word): Integer; overload;
function WriteDBWord(const hContact: THandle; const Module,Param: AnsiString; Value: Word): Integer; overload;
function WriteDBDWord(const Module,Param: AnsiString; Value: DWord): Integer; overload;
function WriteDBDWord(const hContact: THandle; const Module,Param: AnsiString; Value: DWord): Integer; overload;
function WriteDBInt(const Module,Param: AnsiString; Value: Integer): Integer; overload;
function WriteDBInt(const hContact: THandle; const Module,Param: AnsiString; Value: Integer): Integer; overload;
function WriteDBStr(const Module,Param: AnsiString; const Value: AnsiString): Integer; overload;
function WriteDBStr(const hContact: THandle; const Module,Param: AnsiString; const Value: AnsiString): Integer; overload;
function WriteDBWideStr(const Module,Param: AnsiString; const Value: WideString): Integer; overload;
function WriteDBWideStr(const hContact: THandle; const Module,Param: AnsiString; const Value: WideString): Integer; overload;
function WriteDBBool(const Module,Param: AnsiString; Value: Boolean): Integer; overload;
function WriteDBBool(const hContact: THandle; const Module,Param: AnsiString; Value: Boolean): Integer; overload;
function WriteDBDateTime(const hContact: THandle; const Module,Param: AnsiString; Value: TDateTime): Integer; overload;
function WriteDBDateTime(const Module,Param: AnsiString; Value: TDateTime): Integer; overload;

implementation

uses
  hpp_global;

procedure SetSafetyMode(Safe: Boolean);
begin
  CallService(MS_DB_SETSAFETYMODE,WPARAM(Safe),0);
end;

function DBExists(const Module, Param: AnsiString): Boolean;
begin
  Result := DBExists(0,Module,Param);
end;

function DBExists(const hContact: THandle; const Module, Param: AnsiString): Boolean;
var
  dbv: TDBVARIANT;
begin
  Result := (db_get(hContact, PAnsiChar(Module), PAnsiChar(Param), @dbv) = 0);
  if Result then
    DBFreeVariant(@dbv);
end;

function DBDelete(const Module, Param: AnsiString): Boolean;
begin
  Result := DBDelete(0,Module,Param);
end;

function DBDelete(const hContact: THandle; const Module, Param: AnsiString): Boolean;
begin
  Result := (DBDeleteContactSetting(hContact,PAnsiChar(Module),PAnsiChar(Param)) = 0);
end;

function WriteDBBool(const Module,Param: AnsiString; Value: Boolean): Integer;
begin
  Result := WriteDBBool(0,Module,Param,Value);
end;

function WriteDBBool(const hContact: THandle; const Module,Param: AnsiString; Value: Boolean): Integer;
begin
  Result := WriteDBByte(hContact,Module,Param,Byte(Value));
end;

function WriteDBByte(const Module,Param: AnsiString; Value: Byte): Integer;
begin
  Result := WriteDBByte(0,Module,Param,Value);
end;

function WriteDBByte(const hContact: THandle; const Module,Param: AnsiString; Value: Byte): Integer;
begin
  Result := DBWriteContactSettingByte(hContact,PAnsiChar(Module), PAnsiChar(Param), Value);
end;

function WriteDBWord(const Module,Param: AnsiString; Value: Word): Integer;
begin
  Result := WriteDBWord(0,Module,Param,Value);
end;

function WriteDBWord(const hContact: THandle; const Module,Param: AnsiString; Value: Word): Integer;
begin
  Result := DBWriteContactSettingWord(hContact,PAnsiChar(Module),PAnsiChar(Param),Value);
end;

function WriteDBDWord(const Module,Param: AnsiString; Value: DWord): Integer;
begin
  Result := WriteDBWord(0,Module,Param,Value);
end;

function WriteDBDWord(const hContact: THandle; const Module,Param: AnsiString; Value: DWord): Integer;
begin
  Result := DBWriteContactSettingDWord(hContact,PAnsiChar(Module),PAnsiChar(Param),Value);
end;

function WriteDBInt(const Module,Param: AnsiString; Value: Integer): Integer;
begin
  Result := WriteDBInt(0,Module,Param,Value);
end;

function WriteDBInt(const hContact: THandle; const Module,Param: AnsiString; Value: Integer): Integer;
var
  cws: TDBCONTACTWRITESETTING;
begin
  cws.szModule := PAnsiChar(Module);
  cws.szSetting := PAnsiChar(Param);
  cws.value._type := DBVT_DWORD;
  cws.value.dVal := Value;
  Result := CallService(MS_DB_CONTACT_WRITESETTING, hContact, lParam(@cws));
end;

function WriteDBStr(const Module,Param: AnsiString; const Value: AnsiString): Integer;
begin
  Result := WriteDBStr(0,Module,Param,Value);
end;

function WriteDBStr(const hContact: THandle; const Module,Param: AnsiString; const Value: AnsiString): Integer;
begin
  Result := DBWriteContactSettingString(hContact,PAnsiChar(Module),PAnsiChar(Param),PAnsiChar(Value));
end;

function WriteDBWideStr(const Module,Param: AnsiString; const Value: WideString): Integer;
begin
  Result := WriteDBWideStr(0,Module,Param,Value);
end;

function WriteDBWideStr(const hContact: THandle; const Module,Param: AnsiString; const Value: WideString): Integer;
begin
  Result := DBWriteContactSettingWideString(hContact,PAnsiChar(Module),PAnsiChar(Param),PWideChar(Value));
end;

function DBWriteContactSettingWideString(hContact: THandle; const szModule: PAnsiChar; const szSetting: PAnsiChar; const val: PWideChar): Integer;
var
  cws: TDBCONTACTWRITESETTING;
begin
  cws.szModule := szModule;
  cws.szSetting := szSetting;
  cws.value._type := DBVT_WCHAR;
  cws.value.szVal.w := val;
  Result := CallService(MS_DB_CONTACT_WRITESETTING, hContact, lParam(@cws));
end;

function WriteDBBlob(const Module,Param: AnsiString; Value: Pointer; Size: Integer): Integer;
begin
  Result := WriteDBBlob(0,Module,Param,Value,Size);
end;

function WriteDBBlob(const hContact: THandle; const Module,Param: AnsiString; Value: Pointer; Size: Integer): Integer;
var
  cws: TDBContactWriteSetting;
begin
  ZeroMemory(@cws,SizeOf(cws));
  cws.szModule := PAnsiChar(Module);
  cws.szSetting := PAnsiChar(Param);
  cws.value._type := DBVT_BLOB;
  cws.value.pbVal := Value;
  cws.value.cpbVal := Word(Size);
  Result := CallService(MS_DB_CONTACT_WRITESETTING,hContact,lParam(@cws));
end;

function WriteDBDateTime(const hContact: THandle; const Module,Param: AnsiString; Value: TDateTime): Integer; overload;
var
  p: PDateTime;
begin
  GetMem(p,SizeOf(TDateTime));
  p^ := Value;
  Result := WriteDBBlob(hContact,Module,Param,p,SizeOf(TDateTime));
  FreeMem(p,SizeOf(TDateTime));
end;

function WriteDBDateTime(const Module,Param: AnsiString; Value: TDateTime): Integer; overload;
begin
  Result := WriteDBDateTime(0,Module,Param,Value);
end;

function GetDBBlob(const Module,Param: AnsiString; var Value: Pointer; var Size: Integer): Boolean;
begin
  Result := GetDBBlob(0,Module,Param,Value,Size);
end;

function GetDBBlob(const hContact: THandle; const Module,Param: AnsiString; var Value: Pointer; var Size: Integer): Boolean;
var
  dbv: TDBVARIANT;
begin
  Result := False;
  if db_get(hContact, PAnsiChar(Module), PAnsiChar(Param), @dbv) <> 0 then exit;
  Size := dbv.cpbVal;
  Value := nil;
  if dbv.cpbVal = 0 then exit;
  GetMem(Value,dbv.cpbVal);
  Move(dbv.pbVal^,PByte(Value)^,dbv.cpbVal);
  DBFreeVariant(@dbv);
  Result := True;
end;

function GetDBBool(const Module,Param: AnsiString; Default: Boolean): Boolean;
begin
  Result := GetDBBool(0,Module,Param,Default);
end;

function GetDBBool(const hContact: THandle; const Module,Param: AnsiString; Default: Boolean): Boolean;
begin
  Result := Boolean(GetDBByte(hContact,Module,Param,Byte(Default)));
end;

function GetDBByte(const Module,Param: AnsiString; Default: Byte): Byte;
begin
  Result := GetDBByte(0,Module,Param,Default);
end;

function GetDBByte(const hContact: THandle; const Module,Param: AnsiString; Default: Byte): Byte;
begin
  Result := DBGetContactSettingByte(hContact,PAnsiChar(Module),PAnsiChar(Param),Default);
end;

function GetDBWord(const Module,Param: AnsiString; Default: Word): Word;
begin
  Result := GetDBWord(0,Module,Param,Default);
end;

function GetDBWord(const hContact: THandle; const Module,Param: AnsiString; Default: Word): Word;
begin
  Result := DBGetContactSettingWord(hContact,PAnsiChar(Module),PAnsiChar(Param),Default);
end;

function GetDBDWord(const Module,Param: AnsiString; Default: DWord): DWord;
begin
  Result := GetDBDWord(0,Module,Param,Default);
end;

function GetDBDWord(const hContact: THandle; const Module,Param: AnsiString; Default: DWord): DWord;
begin
  Result := DBGetContactSettingDWord(hContact,PAnsiChar(Module),PAnsiChar(Param),Default);
end;

function GetDBInt(const Module,Param: AnsiString; Default: Integer): Integer;
begin
  Result := GetDBInt(0,Module,Param,Default);
end;

function GetDBInt(const hContact: THandle; const Module,Param: AnsiString; Default: Integer): Integer;
var
  dbv:TDBVariant;
begin
  dbv._type := DBVT_DWORD;
  dbv.dVal:=Default;
  if db_get(hContact,PAnsiChar(Module),PAnsiChar(Param),@dbv)<>0 then
    Result:=default
  else
    Result:=dbv.dval;
end;

function GetDBStr(const Module,Param: AnsiString; const Default: AnsiString): AnsiString;
begin
  Result := GetDBStr(0,Module,Param,Default);
end;

function GetDBStr(const hContact: THandle; const Module,Param: AnsiString; const Default: AnsiString): AnsiString;
begin
  Result := DBGetContactSettingString(hContact,PAnsiChar(Module),PAnsiChar(Param),PAnsiChar(Default));
end;

function DBGetContactSettingString(hContact: THandle; const szModule: PAnsiChar; const szSetting: PAnsiChar; ErrorValue: PAnsiChar): AnsiString;
var
  dbv: TDBVARIANT;
  tmp: WideString;
begin
  if db_get(hContact, szModule, szSetting, @dbv) <> 0 then
    Result := ErrorValue
  else begin
    case dbv._type of
      DBVT_ASCIIZ:
        Result := AnsiString(dbv.szVal.a);
      DBVT_UTF8: begin
        tmp := AnsiToWideString(dbv.szVal.a,CP_UTF8);
	      Result := WideToAnsiString(tmp,hppCodepage);
        end;
      DBVT_WCHAR:
        Result := WideToAnsiString(dbv.szVal.w,hppCodepage);
    end;
    // free variant
    DBFreeVariant(@dbv);
  end;
end;

function GetDBWideStr(const Module,Param: AnsiString; const Default: WideString): WideString;
begin
  Result := GetDBWideStr(0,Module,Param,Default);
end;

function GetDBWideStr(const hContact: THandle; const Module,Param: AnsiString; const Default: WideString): WideString;
begin
  Result := DBGetContactSettingWideString(hContact,PAnsiChar(Module),PAnsiChar(Param),PWideChar(Default));
end;

function DBGetContactSettingWideString(hContact: THandle; const szModule: PAnsiChar; const szSetting: PAnsiChar; ErrorValue: PWideChar): WideString;
var
  dbv: TDBVARIANT;
begin
  if db_get(hContact, szModule, szSetting, @dbv) <> 0 then
    Result := ErrorValue
  else begin
    case dbv._type of
      DBVT_ASCIIZ:
        Result := AnsiToWideString(dbv.szVal.a,hppCodepage);
      DBVT_UTF8:
        Result := AnsiToWideString(dbv.szVal.a,CP_UTF8);
      DBVT_WCHAR:
        Result := WideString(dbv.szVal.w);
    end;
    // free variant
    DBFreeVariant(@dbv);
  end;
end;

function GetDBDateTime(const hContact: THandle; const Module,Param: AnsiString; Default: TDateTime): TDateTime; overload;
var
  p: Pointer;
  s: Integer;
begin
  Result := Default;
  if not GetDBBlob(hContact,Module,Param,p,s) then exit;
  if s <> SizeOf(TDateTime) then begin
    FreeMem(p,s);
    exit;
  end;
  Result := PDateTime(p)^;
  FreeMem(p,s);
end;

function GetDBDateTime(const Module,Param: AnsiString; Default: TDateTime): TDateTime; overload;
begin
  Result := GetDBDateTime(0,Module,Param,Default);
end;

end.