summaryrefslogtreecommitdiff
path: root/plugins/HistoryPlusPlus/hpp_external.pas
blob: 95ea03a1bfb6d4fb8a3be6a46f99f3701f4f4c59 (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
(*
    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_external;

interface

uses
  Classes, Windows,
  m_api,
  hpp_externalgrid;

type
  TExternalGrids = class(TObject)
  private
    FGrids: array[TExGridMode] of TList;
    procedure SetGroupLinked(Value: Boolean);
  public
    constructor Create;
    destructor Destroy; override;
    procedure Add(const ExtGrid: TExternalGrid; GridMode: TExGridMode);
    function Find(Handle: HWND; GridMode: TExGridMode): TExternalGrid;
    function Delete(Handle: HWND; GridMode: TExGridMode): Boolean;
    function Clear(GridMode: TExGridMode): Boolean;
    procedure Perform(Msg: Cardinal; wParam: WPARAM; lParam: LPARAM);
    property GroupLinked: Boolean write SetGroupLinked;
  end;


const
  MS_HPP_EG_WINDOW         = 'History++/ExtGrid/NewWindow';
  MS_HPP_EG_EVENT	         = 'History++/ExtGrid/Event';
  MS_HPP_EG_NAVIGATE       = 'History++/ExtGrid/Navigate';
  ME_HPP_EG_OPTIONSCHANGED = 'History++/ExtGrid/OptionsChanged';

var
  ImitateIEView: boolean;
  ExternalGrids: TExternalGrids;

procedure RegisterExtGridServices;
procedure UnregisterExtGridServices;

implementation

uses
  hpp_global, hpp_database;

{$include m_ieview.inc}

var
  hExtWindowIE, hExtEventIE, hExtNavigateIE, hExtOptChangedIE: THandle;
  hExtWindow, hExtEvent, hExtNavigate, hExtOptChanged: THandle;

function _ExtWindow(wParam:WPARAM; lParam: LPARAM; GridMode: TExGridMode): uint_ptr;
var
  par: PIEVIEWWINDOW;
  ExtGrid: TExternalGrid;
  ControlID: Cardinal;
begin
  Result := 0;
  //try
    par := PIEVIEWWINDOW(lParam);
    Assert(par <> nil, 'Empty IEVIEWWINDOW structure');
    case par.iType of
      IEW_CREATE: begin
        {$IFDEF DEBUG}
        OutputDebugString('IEW_CREATE');
        {$ENDIF}
        case par.dwMode of
          IEWM_TABSRMM: ControlID := 1006;  // IDC_LOG from tabSRMM
          IEWM_SCRIVER: ControlID := 1001;  // IDC_LOG from Scriver
          IEWM_MUCC:    ControlID := 0;
          IEWM_CHAT:    ControlID := 0;
          IEWM_HISTORY: ControlID := 0;
        else            ControlID := 0;
        end;
        ExtGrid := TExternalGrid.Create(par.Parent,ControlID);
        case par.dwMode of
          IEWM_MUCC,IEWM_CHAT: begin
            ExtGrid.ShowHeaders   := False;
            ExtGrid.GroupLinked   := False;
            ExtGrid.ShowBookmarks := False;
          end;
          IEWM_HISTORY:
            ExtGrid.GroupLinked := False;
        end;
        ExtGrid.SetPosition(par.x,par.y,par.cx,par.cy);
        ExternalGrids.Add(ExtGrid,GridMode);
        par.Hwnd := ExtGrid.GridHandle;
      end;
      IEW_DESTROY: begin
        {$IFDEF DEBUG}
        OutputDebugString('IEW_DESTROY');
        {$ENDIF}
        ExternalGrids.Delete(par.Hwnd,GridMode);
      end;
      IEW_SETPOS: begin
        {$IFDEF DEBUG}
        OutputDebugString('IEW_SETPOS');
        {$ENDIF}
        ExtGrid := ExternalGrids.Find(par.Hwnd,GridMode);
        if ExtGrid <> nil then
          ExtGrid.SetPosition(par.x,par.y,par.cx,par.cy);
      end;
      IEW_SCROLLBOTTOM: begin
        {$IFDEF DEBUG}
        OutputDebugString('IEW_SCROLLBOTTOM');
        {$ENDIF}
        ExtGrid := ExternalGrids.Find(par.Hwnd,GridMode);
        if ExtGrid <> nil then
          ExtGrid.ScrollToBottom;
      end;
    end;
  //except
  //end;
end;

function ExtWindowNative(wParam:WPARAM; lParam: LPARAM): uint_ptr; cdecl;
begin
  Result := _ExtWindow(wParam,lParam,gmNative);
end;

function ExtWindowIEView(wParam:WPARAM; lParam: LPARAM): uint_ptr; cdecl;
begin
  Result := _ExtWindow(wParam,lParam,gmIEView);
end;

function _ExtEvent(wParam:WPARAM; lParam: LPARAM; GridMode: TExGridMode): uint_ptr; cdecl;
var
  event: PIEVIEWEVENT;
  customEvent: PIEVIEWEVENTDATA;
  UsedCodepage: Cardinal;
  hDBNext: THandle;
  eventCount: Integer;
  ExtGrid: TExternalGrid;
  CustomItem: TExtCustomItem;
begin
  Result := 0;
  //try
    {$IFDEF DEBUG}
    OutputDebugString('MS_IEVIEW_EVENT');
    {$ENDIF}
    event := PIEVIEWEVENT(lParam);
    Assert(event <> nil, 'Empty IEVIEWEVENT structure');
    ExtGrid := ExternalGrids.Find(event.Hwnd,GridMode);
    if ExtGrid = nil then exit;
    case event.iType of
      IEE_LOG_DB_EVENTS: begin
        if event.cbSize >= IEVIEWEVENT_SIZE_V2 then
          UsedCodepage := event.Codepage
        else
          UsedCodepage := CP_ACP;
        eventCount := event.Count;
        hDBNext := event.Event.hDBEventFirst;
        ExtGrid.BeginUpdate;
        while (eventCount <> 0) and (hDBNext <> 0) do
        begin
          ExtGrid.AddEvent(event.hContact, hDBNext, UsedCodepage,
                           boolean(event.dwFlags and IEEF_RTL),
                           not boolean(event.dwFlags and IEEF_NO_SCROLLING));
          if eventCount > 0 then Dec(eventCount);
          if eventCount <> 0 then
            hDBNext := db_event_next(hDBNext);
        end;
        ExtGrid.EndUpdate;
      end;
      IEE_LOG_MEM_EVENTS: begin
        if event.cbSize >= IEVIEWEVENT_SIZE_V2 then
          UsedCodepage := event.Codepage
        else
          UsedCodepage := CP_ACP;
        eventCount := event.Count;
        customEvent := event.Event.eventData;
        ExtGrid.BeginUpdate;
        while (eventCount <> 0) and (customEvent <> nil) do
        begin
          if boolean(customEvent.dwFlags and IEEDF_UNICODE_TEXT) then
            SetString(CustomItem.Text,customEvent.Text.w,lstrlenW(customEvent.Text.w))
          else
            CustomItem.Text := AnsiToWideString(AnsiString(customEvent.Text.a),UsedCodepage);
          if boolean(customEvent.dwFlags and IEEDF_UNICODE_NICK) then
            SetString(CustomItem.Nick,customEvent.Nick.w,lstrlenW(customEvent.Nick.w))
          else
            CustomItem.Nick := AnsiToWideString(AnsiString(customEvent.Nick.a),UsedCodepage);
          CustomItem.Sent := boolean(customEvent.bIsMe);
          CustomItem.Time := customEvent.time;
          ExtGrid.AddCustomEvent(event.hContact, CustomItem, UsedCodepage,
                             boolean(event.dwFlags and IEEF_RTL),
                             not boolean(event.dwFlags and IEEF_NO_SCROLLING));
          if eventCount > 0 then Dec(eventCount);
          customEvent := customEvent.next;
        end;
        ExtGrid.EndUpdate;
      end;
      IEE_CLEAR_LOG: begin
        ExtGrid.BeginUpdate;
        ExtGrid.Clear;
        ExtGrid.EndUpdate;
      end;
      IEE_GET_SELECTION: begin
        Result := uint_ptr(ExtGrid.GetSelection(boolean(event.dwFlags and IEEF_NO_UNICODE)));
      end;
      IEE_SAVE_DOCUMENT: begin
        ExtGrid.SaveSelected;
      end;
    end;
  //except
  //end;
end;

function ExtEventNative(wParam:WPARAM; lParam: LPARAM): uint_ptr; cdecl;
begin
  Result := _ExtEvent(wParam,lParam,gmNative);
end;

function ExtEventIEView(wParam:WPARAM; lParam: LPARAM): uint_ptr; cdecl;
begin
  Result := _ExtEvent(wParam,lParam,gmIEView);
end;

function ExtNavigate(wParam:WPARAM; lParam: LPARAM): uint_ptr; cdecl;
begin
  Result := 0;
  //try
    {$IFDEF DEBUG}
    OutputDebugString('MS_IEVIEW_NAVIGATE');
    {$ENDIF}
  //except
  //end;
end;

procedure RegisterExtGridServices;
begin
  ExternalGrids := TExternalGrids.Create;
  ImitateIEView := GetDBBool(hppDBName,'IEViewAPI',false);
  if ImitateIEView then
  begin
    hExtWindowIE     := CreateServiceFunction(MS_IEVIEW_WINDOW,ExtWindowIEView);
    hExtEventIE      := CreateServiceFunction(MS_IEVIEW_EVENT,ExtEventIEView);
    hExtNavigateIE   := CreateServiceFunction(MS_IEVIEW_NAVIGATE,ExtNavigate);
    hExtOptChangedIE := CreateHookableEvent(ME_IEVIEW_OPTIONSCHANGED);
  end;
  hExtWindow     := CreateServiceFunction(MS_HPP_EG_WINDOW,ExtWindowNative);
  hExtEvent      := CreateServiceFunction(MS_HPP_EG_EVENT,ExtEventNative);
  hExtNavigate   := CreateServiceFunction(MS_HPP_EG_NAVIGATE,ExtNavigate);
  hExtOptChanged := CreateHookableEvent(ME_HPP_EG_OPTIONSCHANGED);
end;

procedure UnregisterExtGridServices;
begin
  if ImitateIEView then
  begin
    DestroyServiceFunction(hExtWindowIE);
    DestroyServiceFunction(hExtEventIE);
    DestroyServiceFunction(hExtNavigateIE);
    DestroyHookableEvent(hExtOptChangedIE);
  end;
  DestroyServiceFunction(hExtWindow);
  DestroyServiceFunction(hExtEvent);
  DestroyServiceFunction(hExtNavigate);
  DestroyHookableEvent(hExtOptChanged);
  ExternalGrids.Destroy;
end;

constructor TExternalGrids.Create;
var
  GridMode: TExGridMode;
begin
  for GridMode := Low(TExGridMode) to High(TExGridMode) do
    FGrids[GridMode] := TList.Create;
end;

destructor TExternalGrids.Destroy;
var
  GridMode: TExGridMode;
begin
  for GridMode := Low(TExGridMode) to High(TExGridMode) do begin
    Clear(GridMode);
    FGrids[GridMode].Free;
  end;
  inherited;
end;

procedure TExternalGrids.Add(const ExtGrid: TExternalGrid; GridMode: TExGridMode);
begin
  FGrids[GridMode].Add(ExtGrid);
end;

function TExternalGrids.Find(Handle: HWND; GridMode: TExGridMode): TExternalGrid;
var
  i: Integer;
  ExtGrid: TExternalGrid;
begin
  Result := nil;
  for i := 0 to FGrids[GridMode].Count-1 do
  begin
    ExtGrid := TExternalGrid(FGrids[GridMode].Items[i]);
    if ExtGrid.GridHandle = Handle then
    begin
      Result := ExtGrid;
      break;
    end;
  end;
end;

function TExternalGrids.Delete(Handle: HWND; GridMode: TExGridMode): Boolean;
var
  i: Integer;
  ExtGrid: TExternalGrid;
begin
  Result := True;
  for i := 0 to FGrids[GridMode].Count-1 do
  begin
    ExtGrid := TExternalGrid(FGrids[GridMode].Items[i]);
    if ExtGrid.GridHandle = Handle then
    begin
      try
        ExtGrid.Free;
      except
        Result := False;
      end;
      FGrids[GridMode].Delete(i);
      break;
    end;
  end;
end;

function TExternalGrids.Clear(GridMode: TExGridMode): Boolean;
var
  i: Integer;
  ExtGrid: TExternalGrid;
begin
  Result := True;
  for i := 0 to FGrids[GridMode].Count-1 do
  begin
    ExtGrid := TExternalGrid(FGrids[GridMode].Items[i]);
    try
      ExtGrid.Free;
    except
      Result := False;
    end;
  end;
  FGrids[GridMode].Clear;
end;

procedure TExternalGrids.Perform(Msg: Cardinal; wParam: WPARAM; lParam: LPARAM);
var
  i: Integer;
  GridMode: TExGridMode;
begin
  for GridMode := Low(TExGridMode) to High(TExGridMode) do
    for i := FGrids[GridMode].Count-1 downto 0 do
      TExternalGrid(FGrids[GridMode].Items[i]).Perform(Msg,wParam,lParam);
end;

procedure TExternalGrids.SetGroupLinked(Value: Boolean);
var
  i: Integer;
  GridMode: TExGridMode;
begin
  for GridMode := Low(TExGridMode) to High(TExGridMode) do
    for i := FGrids[GridMode].Count-1 downto 0 do
      TExternalGrid(FGrids[GridMode].Items[i]).GroupLinked := Value;
end;

end.