summaryrefslogtreecommitdiff
path: root/plugins/HistoryPlusPlus/EmptyHistoryForm.pas
blob: e6235fd9f95cbc39d107a9092c51f91f5d8b17f1 (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
(*
    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
*)

{-----------------------------------------------------------------------------
 EmptyHistoryForm (historypp project)

 Version:   1.0
 Created:   15.03.2008
 Author:    theMIROn

 [ Description ]

  Empty history dialog

 [ History ]

 1.0 (15.04.08) First version.

 [ Modifications ]

 [ Known Issues ]

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

unit EmptyHistoryForm;

interface

uses Windows, Classes, Controls, Graphics,
  Forms, Buttons, StdCtrls, ExtCtrls,
//  HistoryControls,
  m_api;

type
  TEmptyHistoryFrm = class(TForm)
    btYes: TButton;
    btNo: TButton;
    paContacts: TPanel;
    paButtons: TPanel;
    Image: TImage;
    Text: TLabel;
    cbInclude: TCheckBox;
    btCancel: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormShow(Sender: TObject);
    procedure btYesClick(Sender: TObject);
  private
    FContact: THandle;
    FContacts: Array of THandle;
    FPasswordMode: Boolean;
    procedure TranslateForm;
    procedure PrepareForm;
    procedure SetContact(const Value: THandle);
    procedure SetPasswordMode(const Value: Boolean);
    procedure EmptyHistory(hContact: THandle);
  protected
    function GetFormText: String;
  public
    property Contact: THandle read FContact write SetContact;
    property PasswordMode: Boolean read FPasswordMode write SetPasswordMode;
  end;

implementation

uses
  Math, SysUtils, HistoryForm,
  PassForm, {PassCheckForm,}
  hpp_global, hpp_forms, hpp_contacts, hpp_database, hpp_bookmarks;

{$R *.dfm}

function GetAveCharSize(Canvas: TCanvas): TPoint;
var
  I: Integer;
  Buffer: array[0..51] of WideChar;
  tm: TTextMetric;
begin
  for I := 0 to 25 do Buffer[I] := WideChar(I + Ord('A'));
  for I := 0 to 25 do Buffer[I + 26] := WideChar(I + Ord('a'));
  GetTextMetrics(Canvas.Handle, tm);
  GetTextExtentPointW(Canvas.Handle, Buffer, 52, TSize(Result));
  Result.X := (Result.X div 26 + 1) div 2;
  Result.Y := tm.tmHeight;
end;

function TEmptyHistoryFrm.GetFormText: String;
var
  DividerLine, ButtonCaptions, IncludeContacts: String;
  I: integer;
begin
  DividerLine := StringOfChar('-', 27) + sLineBreak;
  for I := 0 to ComponentCount - 1 do
    if Components[I] is TButton then
      ButtonCaptions := ButtonCaptions +
                        TButton(Components[I]).Caption + StringOfChar(' ', 3);
  ButtonCaptions := StringReplace(ButtonCaptions,'&','', [rfReplaceAll]);
  if paContacts.Visible then
  begin
    if cbInclude.Checked then
      IncludeContacts := '[x]'
    else
      IncludeContacts := '[ ]';
    IncludeContacts := sLineBreak + IncludeContacts + ' ' + cbInclude.Caption + sLineBreak;
  end
  else
    IncludeContacts := '';
  Result := DividerLine + Caption + sLineBreak +
            DividerLine + Text.Caption + sLineBreak +
            IncludeContacts +
            DividerLine + ButtonCaptions + sLineBreak +
            DividerLine;
end;

procedure TEmptyHistoryFrm.TranslateForm;
begin
  Caption           := TranslateUnicodeString(Caption);
  cbInclude.Caption := TranslateUnicodeString(cbInclude.Caption);
  btYes.Caption     := TranslateUnicodeString(btYes.Caption);
  btNo.Caption      := TranslateUnicodeString(btNo.Caption);
  btCancel.Caption  := TranslateUnicodeString(btCancel.Caption);
end;

procedure TEmptyHistoryFrm.PrepareForm;
const
  mcSpacing = 8;
  mcButtonWidth = 50;
  mcButtonHeight = 14;
  mcButtonSpacing = 4;
var
  DialogUnits: TPoint;
  HorzSpacing, VertSpacing,
  ButtonWidth, ButtonHeight, ButtonSpacing, ButtonGroupWidth,
  IconTextWidth, IconTextHeight: Integer;
  TextRect,ContRect: TRect;
begin
  DialogUnits := GetAveCharSize(Canvas);
  HorzSpacing   := MulDiv(mcSpacing, DialogUnits.X, 8);
  VertSpacing   := MulDiv(mcSpacing, DialogUnits.X, 4);
  ButtonWidth   := MulDiv(mcButtonWidth, DialogUnits.X, 4);
  ButtonHeight  := MulDiv(mcButtonHeight, DialogUnits.Y, 8);
  ButtonSpacing := MulDiv(mcButtonSpacing, DialogUnits.X, 4);

  SetRect(TextRect, 0, 0, Screen.Width div 2, 0);
    DrawTextW(Canvas.Handle, PChar(Text.Caption),
      Length(Text.Caption)+1, TextRect,
      DT_EXPANDTABS or DT_CALCRECT or DT_WORDBREAK or
      DrawTextBiDiModeFlagsReadingOnly);

  IconTextWidth := Image.Width + HorzSpacing + TextRect.Right;
  IconTextHeight := Max(Image.Height,TextRect.Bottom);

  if PasswordMode then
    ButtonGroupWidth := ButtonWidth
  else
    ButtonGroupWidth := ButtonWidth*2 + ButtonSpacing;

  BorderWidth := VertSpacing;
  ClientWidth := Max(IconTextWidth, ButtonGroupWidth);
  if paContacts.Visible then
  begin
    ContRect := Rect(0,0,0,0);
    DrawTextW(Canvas.Handle,
      PChar(cbInclude.Caption), -1,
      ContRect, DT_CALCRECT or DT_LEFT or DT_SINGLELINE or
      DrawTextBiDiModeFlagsReadingOnly);
    Inc(ContRect.Right, HorzSpacing*4);
    cbInclude.SetBounds((ClientWidth - ContRect.Right) div 2,0,
      ContRect.Right, ContRect.Bottom);
    paContacts.Height := cbInclude.Height + VertSpacing;
    ClientHeight := IconTextHeight + VertSpacing + paContacts.Height + paButtons.Height;
  end
  else
    ClientHeight := IconTextHeight + VertSpacing + paButtons.Height;
  Text.SetBounds(Image.Width + HorzSpacing, 0,
    TextRect.Right, TextRect.Bottom);

  if PasswordMode then
  begin
    btCancel.SetBounds((ClientWidth - ButtonGroupWidth) div 2,0, ButtonWidth, ButtonHeight);
  end
  else
  begin
    btYes.SetBounds((ClientWidth - ButtonGroupWidth) div 2,0, ButtonWidth, ButtonHeight);
    btNo.SetBounds(btYes.Left + btYes.Width + ButtonSpacing,0, ButtonWidth, ButtonHeight);
  end;
end;

procedure TEmptyHistoryFrm.FormShow(Sender: TObject);
begin
  TranslateForm;
  PrepareForm;
end;

procedure TEmptyHistoryFrm.FormCreate(Sender: TObject);
var
  NonClientMetrics: TNonClientMetrics;
begin
  NonClientMetrics.cbSize := sizeof(NonClientMetrics);
  if SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, @NonClientMetrics, 0) then
    Font.Handle := CreateFontIndirect(NonClientMetrics.lfMessageFont);
  MakeFontsParent(Self);
  Canvas.Font := Font;
  DoubleBuffered := True;
  MakeDoubleBufferedParent(Self);
end;

procedure TEmptyHistoryFrm.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if (Shift = [ssCtrl]) and (Key = Word('C')) then
  begin
    CopyToClip(GetFormText,CP_ACP);
    Key := 0;
  end;
end;

procedure TEmptyHistoryFrm.SetContact(const Value: THandle);
var
  hContact: THandle;
  Proto: AnsiString;
  i,num: Integer;
begin
  FContact := Value;
  SetLength(FContacts,0);
  GetContactProto(FContact,hContact,Proto);
  if Value <> hContact then
  begin
    num := CallService(MS_MC_GETNUMCONTACTS,FContact,0);
    for i := 0 to num-1 do
    begin
      hContact := CallService(MS_MC_GETSUBCONTACT,FContact,i);
      if hContact <> THandle(-1) then
      begin
        SetLength(FContacts,Length(FContacts)+1);
        FContacts[High(FContacts)] := hContact;
      end;
    end;
  end;
  if Assigned(Owner) and (Owner is THistoryFrm) then
    PasswordMode := THistoryFrm(Owner).PasswordMode
  else
    PasswordMode := (not IsPasswordBlank(GetPassword)) and IsUserProtected(FContact);
  paContacts.Visible := not PasswordMode and (Length(FContacts) > 0);
end;

procedure TEmptyHistoryFrm.SetPasswordMode(const Value: Boolean);
begin
  FPasswordMode := Value;
  if PasswordMode then
  begin
    Image.Picture.Icon.Handle := LoadIcon(0, IDI_EXCLAMATION);
    Text.Caption := TranslateW('History of this contact is password protected');
  end
  else
  begin
    Image.Picture.Icon.Handle := LoadIcon(0, IDI_QUESTION);
    Text.Caption :=
      TranslateW('Do you really want to delete ALL items for this contact?')+#10#13+
      #10#13+
      TranslateW('Note: It can take several minutes for large histories');
  end;
  btYes.Visible := not FPasswordMode;
  btYes.Default := not FPasswordMode;
  btNo.Visible := not FPasswordMode;
  btCancel.Visible := FPasswordMode;
  btCancel.Default := FPasswordMode;
end;

procedure TEmptyHistoryFrm.EmptyHistory(hContact: THandle);
var
  hDBEvent,prevhDbEvent: THandle;
begin
  BookmarkServer.Contacts[hContact].Clear;
  hDBEvent := db_event_last(hContact);
  SetSafetyMode(False);
  while hDBEvent <> 0 do
  begin
    prevhDbEvent := db_event_prev(hDBEvent);
    if db_event_delete(hContact,hDBEvent) = 0 then
      hDBEvent := prevhDbEvent
    else
      hDBEvent := 0;
  end;
  SetSafetyMode(True);
end;

procedure TEmptyHistoryFrm.btYesClick(Sender: TObject);
var
  i: Integer;
begin
  if Assigned(Owner) and (Owner is THistoryFrm) then
    THistoryFrm(Owner).EmptyHistory
  else
    EmptyHistory(FContact);
  if paContacts.Visible and cbInclude.Checked then
    for i := 0 to High(FContacts) do
      EmptyHistory(FContacts[i]);
end;

end.