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

interface

implementation

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

{$include i_cnst_program.inc}
{$resource iac_program.res}

const
  ACF_CURPATH  = $00000001; // Current (not program) path
  ACF_PRTHREAD = $00000002; // parallel Program
  ACF_PRG_PRG  = $00000004; // script for program path
  ACF_PRG_ARG  = $00000008; // script for program args

const // V2
  ACF_OLD_CURPATH  = $00000002;
  ACF_OLD_PRTHREAD = $00000004;
  ACF2_PRG_PRG     = $00000001;
  ACF2_PRG_ARG     = $00000002;

const
  opt_prg      = 'program';
  opt_args     = 'arguments';
  opt_time     = 'time';
  opt_show     = 'show';
const
  ioArgs         = 'args';
  ioProgram      = 'program';
  ioCurrent      = 'current';
  ioParallel     = 'parallel';
  ioWait         = 'wait';
  ioFileVariable = 'modvariables';
  ioArgVariable  = 'argvariables';
  ioWindow       = 'window';
  ioHidden       = 'hidden';
  ioMinimized    = 'minimized';
  ioMaximized    = 'maximized';
type
  tProgramAction = class(tBaseAction)
  private
    prgname:pWideChar;
    args   :pWideChar;
    show   :dword;
    time   :dword;
  public
    constructor Create(uid:dword);
    destructor Destroy; 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 -----

function replany(var str:pWideChar;aparam:LPARAM;alast:pWideChar):boolean;
var
  buf:array [0..31] of WideChar;
  tmp:pWideChar;
begin
  if StrScanW(str,'<')<>nil then
  begin
    result:=true;
    mGetMem(tmp,2048);
    StrCopyW(tmp,str);
    StrReplaceW(tmp,'<param>',IntToStr(buf,aparam));
    StrReplaceW(tmp,'<last>' ,alast);

    str:=tmp;
  end
  else
    result:=false;
end;

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

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

  show   :=0;
  time   :=0;
  prgname:=nil;
  args   :=nil;
end;

destructor tProgramAction.Destroy;
begin
  mFreeMem(prgname);
  mFreeMem(args);

  inherited Destroy;
end;

function tProgramAction.DoAction(var WorkData:tWorkData):LRESULT;
var
  tmp,tmpp,lpath:PWideChar;
  replPrg ,replArg :PWideChar;
  replPrg1,replArg1:PWideChar;
  pd:LPARAM;
  vars1,vars2,prgs,argss:boolean;
  buf:array [0..31] of WideChar;
begin
  result:=0;

  if WorkData.ResultType=rtInt then
  begin
    StrDupW(pWideChar(WorkData.LastResult),IntToStr(buf,WorkData.LastResult));
    WorkData.ResultType:=rtWide;
  end;

  replPrg:=prgname;
  prgs   :=replany(replPrg,WorkData.Parameter,pWideChar(WorkData.LastResult));

  replArg:=args;
  argss  :=replany(replArg,WorkData.Parameter,pWideChar(WorkData.LastResult));

  if ((flags and ACF_PRG_PRG)<>0) or
     ((flags and ACF_PRG_ARG)<>0) then
  begin
    if db_is_contact(WorkData.Parameter)<>0 then
      pd:=WorkData.Parameter
    else
      pd:=WndToContact(WaitFocusedWndChild(GetForegroundwindow){GetFocus});
    if (pd=0) and (WorkData.Parameter<>0) then
      pd:=WorkData.Parameter;
  end;

  if (flags and ACF_PRG_ARG)<>0 then
  begin
    vars2:=true;
    tmp :=ParseVarString(replArg,pd,pWideChar(WorkData.LastResult));
  end
  else
  begin
    vars2:=false;
    tmp :=replArg;
  end;

  if (flags and ACF_PRG_PRG)<>0 then
  begin
    vars1:=true;
    tmpp :=ParseVarString(replPrg,pd,pWideChar(WorkData.LastResult));
  end
  else
  begin
    vars1:=false;
    tmpp:=replPrg;
  end;
  
  if StrScanW(tmpp,'%')<>nil then
  begin
    mGetMem(replPrg1,8192*SizeOf(WideChar));
    ExpandEnvironmentStringsW(tmpp,replPrg1,8191);
    if vars1 then mFreeMem(tmpp);
    if prgs  then mFreeMem(replPrg);
    tmpp :=replPrg1;
    prgs :=false;
    vars1:=true;
  end;
  if StrScanW(tmp,'%')<>nil then
  begin
    mGetMem(replArg1,8192*SizeOf(WideChar));
    ExpandEnvironmentStringsW(tmp,replArg1,8191);
    if vars2 then mFreeMem(tmp);
    if argss then mFreeMem(replArg);
    tmp  :=replArg1;
    argss:=false;
    vars2:=true;
  end;

  if (flags and ACF_CURPATH)=0 then
    lpath:=ExtractW(tmpp,false)
  else
    lpath:=nil;

  if (flags and ACF_PRTHREAD)<>0 then
    time:=0
  else if time=0 then
    time:=INFINITE;
  WorkData.LastResult:=ExecuteWaitW(tmpp,tmp,lpath,show,time,@pd);
  WorkData.ResultType:=rtInt;

  if vars2 then mFreeMem(tmp);
  if vars1 then mFreeMem(tmpp);

  if prgs  then mFreeMem(replPrg);
  if argss then mFreeMem(replArg);

  mFreeMem(lpath);
end;

procedure tProgramAction.Load(node:pointer;fmt:integer);
var
  section: array [0..127] of AnsiChar;
  pc:pAnsiChar;
  tmp:pWideChar;
  flags2,lflags:dword;
begin
  inherited Load(node,fmt);
  case fmt of
    0: begin
      pc:=StrCopyE(section,pAnsiChar(node));
      StrCopy(pc,opt_prg ); prgname:=DBReadUnicode(0,DBBranch,section,nil);
      StrCopy(pc,opt_args); args   :=DBReadUnicode(0,DBBranch,section,nil);
      StrCopy(pc,opt_time); time   :=DBReadDWord  (0,DBBranch,section,0);
      StrCopy(pc,opt_show); show   :=DBReadDWord  (0,DBBranch,section,SW_SHOW);
    end;

    100: begin
      pc:=StrCopyE(section,pAnsiChar(node));
      StrCopy(pc,opt_prg ); prgname:=DBReadUnicode(0,DBBranch,section,nil);
      StrCopy(pc,opt_args); args   :=DBReadUnicode(0,DBBranch,section,nil);
      StrCopy(pc,opt_time); time   :=DBReadDWord  (0,DBBranch,section,0);
      StrCopy(pc,opt_show); show   :=DBReadDWord  (0,DBBranch,section,SW_SHOW);

      StrCopy(pc,'flags2'); flags2:=DBReadDWord(0,DBBranch,section,0);
      lflags:=flags;
      flags:=flags and not ACF_MASK;

      if (lflags and ACF_OLD_CURPATH )<>0 then flags:=flags or ACF_CURPATH;
      if (lflags and ACF_OLD_PRTHREAD)<>0 then flags:=flags or ACF_PRTHREAD;

      if (flags2 and ACF2_PRG_PRG)<>0 then flags:=flags or ACF_PRG_PRG;
      if (flags2 and ACF2_PRG_ARG)<>0 then flags:=flags or ACF_PRG_ARG;
    end;
  end;
end;

procedure tProgramAction.Save(node:pointer;fmt:integer);
var
  section: array [0..127] of AnsiChar;
  pc:pAnsiChar;
begin
  inherited Save(node,fmt);
  case fmt of
    0: begin
      pc:=StrCopyE(section,pAnsiChar(node));
      StrCopy(pc,opt_prg ); DBWriteUnicode(0,DBBranch,section,prgname);
      StrCopy(pc,opt_args); DBWriteUnicode(0,DBBranch,section,args);
      StrCopy(pc,opt_time); DBWriteDWord  (0,DBBranch,section,time);
      StrCopy(pc,opt_show); DBWriteDWord  (0,DBBranch,section,show);
    end;
{
    1: begin
    end;
}
    13: begin
    end;
  end;
end;

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

procedure MakeFileEncList(wnd:HWND);
begin
  SendMessage(wnd,CB_RESETCONTENT,0,0);
{
  InsertString(wnd,0,'Ansi');
  InsertString(wnd,1,'UTF8');
  InsertString(wnd,2,'UTF8+sign');
  InsertString(wnd,3,'UTF16');
  InsertString(wnd,4,'UTF16+sign');
}
  SendMessage(wnd,CB_SETCURSEL,0,0);
end;

procedure ClearFields(Dialog:HWND);
begin
  CheckDlgButton(Dialog,IDC_FLAG_NORMAL,BST_UNCHECKED);
  CheckDlgButton(Dialog,IDC_FLAG_HIDDEN,BST_UNCHECKED);
  CheckDlgButton(Dialog,IDC_FLAG_MINIMIZE,BST_UNCHECKED);
  CheckDlgButton(Dialog,IDC_FLAG_MAXIMIZE,BST_UNCHECKED);

  CheckDlgButton(Dialog,IDC_FLAG_CURPATH,BST_UNCHECKED);
  CheckDlgButton(Dialog,IDC_FLAG_CONTINUE,BST_UNCHECKED);
  CheckDlgButton(Dialog,IDC_FLAG_PARALLEL,BST_UNCHECKED);
end;

function FillFileName(Dialog:HWND;idc:integer):boolean;
var
  pw,ppw:pWideChar;
begin
  mGetMem(pw,1024*SizeOf(WideChar));
  ppw:=GetDlgText(Dialog,idc);
  result:=ShowDlgW(pw,ppw);
  if result then
  begin
    SetDlgItemTextW(Dialog,idc,pw);
    SetEditFlags(Dialog,idc,EF_SCRIPT,0);
  end;
  mFreeMem(ppw);
  mFreeMem(pw);
end;

function DlgProc(Dialog:HWND;hMessage:uint;wParam:WPARAM;lParam:LPARAM):LRESULT; stdcall;
const
  NoProcess:boolean=true;
begin
  result:=0;

  case hMessage of
    WM_INITDIALOG: begin
      TranslateDialogDefault(Dialog);

      MakeEditField(Dialog,IDC_EDIT_PRGPATH);
      MakeEditField(Dialog,IDC_EDIT_PRGARGS);
    end;

    WM_ACT_SETVALUE: begin
      NoProcess:=true;
      ClearFields(Dialog);

      with tProgramAction(lParam) do
      begin
        SetDlgItemTextW(Dialog,IDC_EDIT_PRGPATH ,prgname);
        SetDlgItemTextW(Dialog,IDC_EDIT_PRGARGS ,args);

        SetEditFlags(Dialog,IDC_EDIT_PRGPATH,EF_SCRIPT,ord((flags and ACF_PRG_PRG)<>0));
        SetEditFlags(Dialog,IDC_EDIT_PRGARGS,EF_SCRIPT,ord((flags and ACF_PRG_ARG)<>0));

        SetDlgItemInt(Dialog,IDC_EDIT_PROCTIME,time,false);
        case show of
          SW_HIDE         : CheckDlgButton(Dialog,IDC_FLAG_HIDDEN,BST_CHECKED);
          SW_SHOWMINIMIZED: CheckDlgButton(Dialog,IDC_FLAG_MINIMIZE,BST_CHECKED);
          SW_SHOWMAXIMIZED: CheckDlgButton(Dialog,IDC_FLAG_MAXIMIZE,BST_CHECKED);
        else
          {SW_SHOWNORMAL   :} CheckDlgButton(Dialog,IDC_FLAG_NORMAL,BST_CHECKED);
        end;
        if (flags and ACF_CURPATH)<>0 then
          CheckDlgButton(Dialog,IDC_FLAG_CURPATH,BST_CHECKED);
        if (flags and ACF_PRTHREAD)<>0 then
          CheckDlgButton(Dialog,IDC_FLAG_PARALLEL,BST_CHECKED)
        else
          CheckDlgButton(Dialog,IDC_FLAG_CONTINUE,BST_CHECKED);
      end;
      NoProcess:=false;
    end;

    WM_ACT_RESET: begin
      NoProcess:=true;
      ClearFields(Dialog);

      SetDlgItemTextW(Dialog,IDC_EDIT_PRGPATH,nil);
      SetDlgItemTextW(Dialog,IDC_EDIT_PRGARGS,nil);
      SetEditFlags(Dialog,IDC_EDIT_PRGPATH,EF_ALL,0);
      SetEditFlags(Dialog,IDC_EDIT_PRGARGS,EF_ALL,0);

      CheckDlgButton(Dialog,IDC_FLAG_PARALLEL,BST_CHECKED);
      CheckDlgButton(Dialog,IDC_FLAG_NORMAL  ,BST_CHECKED);
      SetDlgItemInt(Dialog,IDC_EDIT_PROCTIME,0,false);
      NoProcess:=false;
    end;

    WM_ACT_SAVE: begin
      with tProgramAction(lParam) do
      begin
        {mFreeMem(prgname); }prgname:=GetDlgText(Dialog,IDC_EDIT_PRGPATH);
        {mFreeMem(args   ); }args   :=GetDlgText(Dialog,IDC_EDIT_PRGARGS);
{
        p:=GetDlgText(IDC_EDIT_PRGPATH);
        if p<>nil then
        begin
          CallService(MS_UTILS_PATHTORELATIVE,dword(p),dword(@buf));
          StrDupW(prgname,@buf);
          mFreeMem(p);
        end;
}
        if IsDlgButtonChecked(Dialog,IDC_FLAG_PARALLEL)=BST_CHECKED then
          flags:=flags or ACF_PRTHREAD;
        if IsDlgButtonChecked(Dialog,IDC_FLAG_CURPATH)=BST_CHECKED then
          flags:=flags or ACF_CURPATH;

        time:=GetDlgItemInt(Dialog,IDC_EDIT_PROCTIME,pbool(nil)^,false);

        if IsDlgButtonChecked(Dialog,IDC_FLAG_MINIMIZE)=BST_CHECKED then
          show:=SW_SHOWMINIMIZED
        else if IsDlgButtonChecked(Dialog,IDC_FLAG_MAXIMIZE)=BST_CHECKED then
          show:=SW_SHOWMAXIMIZED
        else if IsDlgButtonChecked(Dialog,IDC_FLAG_HIDDEN)=BST_CHECKED then
          show:=SW_HIDE
        else //if IsDlgButtonChecked(Dialog,IDC_FLAG_NORMAL)=BST_CHECKED then
          show:=SW_SHOWNORMAL;

        if (GetEditFlags(Dialog,IDC_EDIT_PRGPATH) and EF_SCRIPT)<>0 then flags:=flags or ACF_PRG_PRG;
        if (GetEditFlags(Dialog,IDC_EDIT_PRGARGS) and EF_SCRIPT)<>0 then flags:=flags or ACF_PRG_ARG;
      end;
    end;

    WM_COMMAND: begin
      case wParam shr 16 of
        EN_CHANGE: if not NoProcess then
            SendMessage(GetParent(GetParent(Dialog)),PSM_CHANGED,0,0);

        BN_CLICKED: begin
          case loword(wParam) of
            IDC_PROGRAM: begin
              if not FillFileName(Dialog,IDC_EDIT_PRGPATH) then
                exit;
            end;
          end;
          SendMessage(GetParent(GetParent(Dialog)),PSM_CHANGED,0,0);
        end;
      end;
    end;

    WM_HELP: begin
      MessageBoxW(0,
        TranslateW('Text <last> replacing'#13#10+
          'by last result'#13#10#13#10+
          'Text <param> replacing'#13#10+
          'by parameter'),
        TranslateW('Text'),0);
      result:=1;
    end;

  end;
end;

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

var
  vc:tActModule;

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

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

procedure Init;
begin
  vc.Next    :=ModuleLink;

  vc.Name    :='Program';
  vc.Dialog  :=@CreateDialog;
  vc.Create  :=@CreateAction;
  vc.Icon    :='IDI_PROGRAM';
  vc.Hash    :=0;

  ModuleLink :=@vc;
end;

begin
  Init;
end.