summaryrefslogtreecommitdiff
path: root/plugins/Actman/iac_contact.pas
blob: ffcfd47bd5ed285a3c8e6c91c26cc601f1404e8e (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
unit iac_contact;

interface

implementation

uses
  windows, messages, commctrl,
  m_api, global, iac_global, common,
  mircontacts, dlgshare, syswin, inouttext,
  wrapper, mirutils, dbsettings;

{$include i_cnst_contact.inc}
{$resource iac_contact.res}

const
  ACF_KEEPONLY  = $00000001; // keep contact handle in Last, don't show window
  ACF_GETACTIVE = $00000002; // try to get contact from active window

const
  ioKeepOnly = 'keeponly';
  ioWindow   = 'window';

type
  tContactAction = class(tBaseAction)
  private
    contact:TMCONTACT;
  public
    constructor Create(uid:dword);
//    function  Clone:tBaseAction; override;
    function  DoAction(var WorkData:tWorkData):LRESULT; override;
    procedure Save(node:pointer;fmt:integer); override;
    procedure Load(node:pointer;fmt:integer); override;
  end;

//----- Support functions -----

constructor tContactAction.Create(uid:dword);
begin
  inherited Create(uid);

  contact:=0;
end;
{
function tContactAction.Clone:tBaseAction;
begin
  result:=tContactAction.Create(0);
  Duplicate(result);

  tContactAction(result).contact:=contact;
end;
}

//----- Object realization -----

function tContactAction.DoAction(var WorkData:tWorkData):LRESULT;
var
  wnd:HWND;
begin
  ClearResult(WorkData);

  if (flags and ACF_GETACTIVE)<>0 then
  begin
    wnd:=WaitFocusedWndChild(GetForegroundWindow){GetFocus};
    if wnd<>0 then
      contact:=WndToContact(wnd)
    else
      contact:=0;
  end
  else if (flags and ACF_KEEPONLY)=0 then
    ShowContactDialog(contact);

  WorkData.LastResult:=contact;
  WorkData.ResultType:=rtInt;

  result:=0;
end;

procedure tContactAction.Load(node:pointer;fmt:integer);
begin
  inherited Load(node,fmt);
  case fmt of
    0: if (flags and ACF_GETACTIVE)=0 then
      contact:=LoadContact(DBBranch,node);

    1: begin
      contact:=ImportContact(HXML(node));
      if StrToInt(xmlGetAttrValue(HXML(node),ioKeepOnly))=1 then
        flags:=flags or ACF_KEEPONLY;
      if StrToInt(xmlGetAttrValue(HXML(node),ioWindow))=1 then
        flags:=flags or ACF_GETACTIVE;
    end;
  end;
end;

function ExportContactText(node:tTextExport;hContact:TMCONTACT):integer;
var
  proto,uid:pAnsiChar;
  cws:TDBVARIANT;
  p1:pAnsiChar;
  p:pWideChar;
  is_chat:boolean;
begin
  result:=0;
  proto:=Proto_GetBaseAccountName(hContact);
  if proto<>nil then
  begin
    is_chat:=IsChat(hContact);
    if is_chat then
    begin
      p:=DBReadUnicode(hContact,proto,'ChatRoomID');
      node.AddTextW('id',p);
      mFreeMem(p);
      result:=1;
    end
    else
    begin
      uid:=Proto_GetUniqueId(proto);
      if DBReadSetting(hContact,proto,uid,@cws)=0 then
      begin
        result:=1;
        node.AddDWord('ctype',cws._type);
        case cws._type of
          DBVT_BYTE  : node.AddDWord('id',cws.bVal);
          DBVT_WORD  : node.AddDWord('id',cws.wVal);
          DBVT_DWORD : node.AddDWord('id',cws.dVal);
          DBVT_ASCIIZ: begin
            node.AddText('id',cws.szVal.A); // ansi to utf
          end;
          DBVT_UTF8  : begin
            node.AddText('id',cws.szVal.A);
          end;
          DBVT_WCHAR : node.AddTextW('id',cws.szVal.W);
          DBVT_BLOB  : begin
            p1:=mir_base64_encode(cws.pbVal,cws.cpbVal);
            node.AddText('id',p1);
            mir_free(p1);
          end;
        end;
      end;
      db_free(@cws);
    end;
    if result<>0 then
    begin
      node.AddText('protocol',proto);
      node.AddFlag('ischat'  ,is_chat);
    end;
  end;
end;

procedure tContactAction.Save(node:pointer;fmt:integer);
begin
  inherited Save(node,fmt);
  case fmt of
    0: if (flags and ACF_GETACTIVE)=0 then
      SaveContact(contact,DBBranch,node);

    13: begin
      tTextExport(node).AddFlag('keeponly' ,(flags or ACF_KEEPONLY )<>0);
      tTextExport(node).AddFlag('getactive',(flags or ACF_GETACTIVE)<>0);
      ExportContactText(tTextExport(node),contact);
    end;
  end;
end;

//----- Dialog realization -----

procedure ClearFields(Dialog:HWND);
begin
  CheckDlgButton(Dialog,IDC_CNT_KEEP,BST_UNCHECKED);
  CheckDlgButton(Dialog,IDC_CNT_GET ,BST_UNCHECKED);
end;

function DlgProc(Dialog:HWND;hMessage:uint;wParam:WPARAM;lParam:LPARAM):LRESULT; stdcall;
var
  wnd:HWND;
  bb:boolean;
begin
  result:=0;

  case hMessage of
    WM_INITDIALOG: begin
      TranslateDialogDefault(Dialog);

      wnd:=GetDlgItem(Dialog,IDC_CNT_REFRESH);
      OptSetButtonIcon(wnd,ACI_REFRESH);
      SendMessage(wnd,BUTTONADDTOOLTIP,TWPARAM(TranslateW('Refresh')),BATF_UNICODE);
      OptFillContactList(GetDlgItem(Dialog,IDC_CONTACTLIST));
    end;

    WM_ACT_SETVALUE: begin
      ClearFields(Dialog);

      with tContactAction(lParam) do
      begin
        if (flags and ACF_GETACTIVE)<>0 then
        begin
          bb:=false;
          CheckDlgButton(Dialog,IDC_CNT_GET,BST_CHECKED);
        end
        else
        begin
          bb:=true;
          if (flags and ACF_KEEPONLY)<>0 then
            CheckDlgButton(Dialog,IDC_CNT_KEEP,BST_CHECKED);

          SendDlgItemMessage(Dialog,IDC_CONTACTLIST,CB_SETCURSEL,
            FindContact(GetDlgItem(Dialog,IDC_CONTACTLIST),contact),0);
        end;
        EnableWindow(GetDlgItem(Dialog,IDC_CNT_REFRESH),bb);
        EnableWindow(GetDlgItem(Dialog,IDC_CONTACTLIST),bb);
        EnableWindow(GetDlgItem(Dialog,IDC_CNT_KEEP   ),bb);
      end;
    end;

    WM_ACT_RESET: begin
      ClearFields(Dialog);
      EnableWindow(GetDlgItem(Dialog,IDC_CNT_REFRESH),true);
      EnableWindow(GetDlgItem(Dialog,IDC_CONTACTLIST),true);
      EnableWindow(GetDlgItem(Dialog,IDC_CNT_KEEP   ),true);
    end;

    WM_ACT_SAVE: begin
      with tContactAction(lParam) do
      begin
        contact:=SendDlgItemMessage(Dialog,IDC_CONTACTLIST,CB_GETITEMDATA,
            SendDlgItemMessage(Dialog,IDC_CONTACTLIST,CB_GETCURSEL,0,0),0);
        if IsDlgButtonChecked(Dialog,IDC_CNT_KEEP)=BST_CHECKED then
          flags:=flags or ACF_KEEPONLY;
      end;
    end;

    WM_COMMAND: begin
      case wParam shr 16 of
        CBN_SELCHANGE: SendMessage(GetParent(GetParent(Dialog)),PSM_CHANGED,0,0);
        BN_CLICKED: begin
          case loword(wParam) of
            IDC_CNT_GET: begin
              bb:=IsDlgButtonChecked(Dialog,IDC_CNT_GET)=BST_UNCHECKED;
              EnableWindow(GetDlgItem(Dialog,IDC_CNT_REFRESH),bb);
              EnableWindow(GetDlgItem(Dialog,IDC_CONTACTLIST),bb);
              EnableWindow(GetDlgItem(Dialog,IDC_CNT_KEEP   ),bb);
            end;

            IDC_CNT_REFRESH: begin
              OptFillContactList(GetDlgItem(Dialog,IDC_CONTACTLIST));
              exit;
            end;
          end;
          SendMessage(GetParent(GetParent(Dialog)),PSM_CHANGED,0,0);
         end;

      end;
    end;

    WM_HELP: begin
      result:=1;
    end;
  end;
end;

//----- Export/interface functions -----

var
  vc:tActModule;

function CreateAction:tBaseAction;
begin
  result:=tContactAction.Create(vc.Hash);
end;

function CreateDialog(parent:HWND):HWND;
begin
  result:=CreateDialogW(hInstance,'IDD_ACTCONTACT',parent,@DlgProc);
end;

procedure Init;
begin
  vc.Next    :=ModuleLink;

  vc.Name    :='Contact';
  vc.Dialog  :=@CreateDialog;
  vc.Create  :=@CreateAction;
  vc.Icon    :='IDI_CONTACT';
  vc.Hash    :=0;

  ModuleLink :=@vc;
end;

begin
  Init;
end.