summaryrefslogtreecommitdiff
path: root/plugins/Actman30/iac_ini.pas
blob: 03c8d7a9d6f9f51546a1ed6778438c7d694e09a0 (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
unit iac_ini;

interface

implementation

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

{$include i_cnst_ini.inc}
{$resource iac_ini.res}

const
  opt_file   :PAnsiChar = 'inifile';
  opt_section:PAnsiChar = 'section';
  opt_param  :PAnsiChar = 'param';
  opt_value  :PAnsiChar = 'value';

const
  ACF_INI_WRITE   = $00000001;
  ACF_INI_DELETE  = $00000002;
  ACF_INI_LR      = $00000004;
  ACF_INI_UTF     = $00000008;
  ACF_INI_FILE    = $00000010;
  ACF_INI_SECTION = $00000020;
  ACF_INI_PARAM   = $00000040;
  ACF_INI_VALUE   = $00000080;
type
  tINIAction = class(tBaseAction)
  private
    inifile  :pWideChar;
    section  :pWideChar;
    parameter:pWideChar;
    value    :pWideChar;
  public
    constructor Create(uid:dword);
    destructor Destroy; override;
//    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 -----

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

constructor tINIAction.Create(uid:dword);
begin
  inherited Create(uid);
end;

destructor tINIAction.Destroy;
begin
  mFreeMem(inifile);
  mFreeMem(section);
  mFreeMem(parameter);
  mFreeMem(value);

  inherited Destroy;
end;
{
function tINIAction.Clone:tBaseAction;
begin
  result:=tServiceAction.Create(0);
  Duplicate(result);

end;
}
function tINIAction.DoAction(var WorkData:tWorkData):LRESULT;
var
  linifile,
  lsection,
  lparam,
  lvalue:pWideChar;
  ainifile,
  asection,
  aparam,
  avalue:pAnsiChar;
  buf:pAnsiChar;
  cond:bool;
begin
  result:=0;
  cond:=true;

  if (flags and ACF_INI_FILE)<>0 then
    linifile:=ParseVarString(inifile,WorkData.Parameter,pWideChar(WorkData.LastResult))
  else
    linifile:=inifile;
  if (linifile=nil) or (linifile^=#0) then
    cond:=false;

  if cond then
  begin
    if (flags and ACF_INI_SECTION)<>0 then
      lsection:=ParseVarString(section,WorkData.Parameter,pWideChar(WorkData.LastResult))
    else
      lsection:=section;
    if (lsection=nil) or (lsection^=#0) then
      cond:=false;
  end
  else
    lsection:=nil;

  if cond then
  begin
    if (flags and ACF_INI_PARAM)<>0 then
      lparam:=ParseVarString(parameter,WorkData.Parameter,pWideChar(WorkData.LastResult))
    else
      lparam:=parameter;
  end
  else
    lparam:=nil;

  if cond then
  begin
    if (flags and ACF_INI_DELETE)<>0 then
    begin
      WritePrivateProfileStringW(lsection,lparam,nil,linifile);
    end
    else
    begin
      if (lparam<>nil) and (lparam^<>#0) then
      begin
        if (flags and ACF_INI_LR)<>0 then
          lvalue:=pWideChar(WorkData.LastResult)
        else if (flags and ACF_INI_VALUE)<>0 then
          lvalue:=ParseVarString(value,WorkData.Parameter,pWideChar(WorkData.LastResult))
        else
          lvalue:=value;

        WideToAnsi(linifile,ainifile);
        WideToAnsi(lsection,asection);
        WideToAnsi(lparam  ,aparam);

        if (flags and ACF_INI_WRITE)<>0 then
        begin
          if (flags and ACF_INI_UTF)=0 then
            WideToAnsi(lvalue,avalue,MirandaCP)
          else
            WideToUTF8(lvalue,avalue);

          WritePrivateProfileStringA(asection,aparam,avalue,ainifile);

          mFreeMem(avalue);
        end

        else // single line only (Windows-way)
        begin
          mGetMem(buf,4096); buf^:=#0;
          GetPrivateProfileStringA(asection,aparam,avalue,buf,4096,ainifile);
          ClearResult(WorkData);

          if GetTextFormat(pByte(buf),StrLen(buf))=CP_UTF8 then
            UTF8ToWide(buf,pWideChar(WorkData.LastResult))
          else
            AnsiToWide(buf,pWideChar(WorkData.LastResult),MirandaCP);
          WorkData.ResultType:=rtWide;
          mFreeMem(buf);
        end;

        mFreeMem(ainifile);
        mFreeMem(asection);
        mFreeMem(aparam);

        if ((flags and ACF_INI_VALUE)<>0) and
           ((flags and ACF_INI_LR   ) =0) then mFreeMem(lvalue);
      end;
    end;
  end;

  if (flags and ACF_INI_FILE   )<>0 then mFreeMem(linifile);
  if (flags and ACF_INI_SECTION)<>0 then mFreeMem(lsection);
  if (flags and ACF_INI_PARAM  )<>0 then mFreeMem(lparam);
end;

procedure tINIAction.Load(node:pointer;fmt:integer);
var
  lsection: array [0..127] of AnsiChar;
  pc:pAnsiChar;
begin
  inherited Load(node,fmt);
  case fmt of
    0: begin
      pc:=StrCopyE(lsection,pAnsiChar(node));
      StrCopy(pc,opt_file   ); inifile  :=DBReadUnicode(0,DBBranch,lsection,nil);
      StrCopy(pc,opt_section); section  :=DBReadUnicode(0,DBBranch,lsection,nil);
      StrCopy(pc,opt_param  ); parameter:=DBReadUnicode(0,DBBranch,lsection,nil);
      StrCopy(pc,opt_value  ); value    :=DBReadUnicode(0,DBBranch,lsection,nil);
    end;
{
    1: begin
    end;
}
  end;
end;

procedure tINIAction.Save(node:pointer;fmt:integer);
var
  lsection: array [0..127] of AnsiChar;
  pc:pAnsiChar;
begin
  inherited Save(node,fmt);
  case fmt of
    0: begin
      pc:=StrCopyE(lsection,pAnsiChar(node));
      StrCopy(pc,opt_file   ); DBWriteUnicode(0,DBBranch,lsection,inifile);
      StrCopy(pc,opt_section); DBWriteUnicode(0,DBBranch,lsection,section);
      StrCopy(pc,opt_param  ); DBWriteUnicode(0,DBBranch,lsection,parameter);
      StrCopy(pc,opt_value  ); DBWriteUnicode(0,DBBranch,lsection,value);
    end;
{
    1: begin
    end;
}
    13: begin
      tTextExport(node).AddFlag('write'     ,(flags and ACF_INI_WRITE )<>0);
      tTextExport(node).AddFlag('delete'    ,(flags and ACF_INI_DELETE)<>0);
      tTextExport(node).AddFlag('lastresult',(flags and ACF_INI_LR    )<>0);
      tTextExport(node).AddFlag('utf'       ,(flags and ACF_INI_UTF   )<>0);
      if flags and (ACF_INI_WRITE or ACF_INI_DELETE or ACF_INI_LR or ACF_INI_UTF)<>0 then
        tTextExport(node).AddNewLine();
      tTextExport(node).AddTextW('inifile'  ,inifile  ); tTextExport(node).AddNewLine();
      tTextExport(node).AddTextW('section'  ,section  ); tTextExport(node).AddNewLine();
      tTextExport(node).AddTextW('parameter',parameter); tTextExport(node).AddNewLine();
      if flags and (ACF_INI_DELETE or ACF_INI_LR)=0 then
      begin
        tTextExport(node).AddTextW('value',value); tTextExport(node).AddNewLine();
      end;
    end;
  end;
end;

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

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;

procedure ClearFields(Dialog:HWND);
begin
  CheckDlgButton(Dialog,IDC_INI_READ  ,BST_UNCHECKED);
  CheckDlgButton(Dialog,IDC_INI_WRITE ,BST_UNCHECKED);
  CheckDlgButton(Dialog,IDC_INI_DELETE,BST_UNCHECKED);

  CheckDlgButton(Dialog,IDC_INI_LR ,BST_UNCHECKED);
  CheckDlgButton(Dialog,IDC_INI_UTF,BST_UNCHECKED);

  EnableEditField(Dialog,IDC_INI_VALUE,true);
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_INI_PATH);
      MakeEditField(Dialog,IDC_INI_SECTION);
      MakeEditField(Dialog,IDC_INI_PARAM);
      MakeEditField(Dialog,IDC_INI_VALUE);
    end;

    WM_ACT_SETVALUE: begin
      NoProcess:=true;
      ClearFields(Dialog);
      with tINIAction(lParam) do
      begin
        SetDlgItemTextW(Dialog,IDC_INI_PATH   ,inifile);
        SetDlgItemTextW(Dialog,IDC_INI_SECTION,section);
        SetDlgItemTextW(Dialog,IDC_INI_PARAM  ,parameter);
        SetDlgItemTextW(Dialog,IDC_INI_VALUE  ,value);

        SetEditFlags(Dialog,IDC_INI_PATH   ,EF_SCRIPT,ord((flags and ACF_INI_FILE   )<>0));
        SetEditFlags(Dialog,IDC_INI_SECTION,EF_SCRIPT,ord((flags and ACF_INI_SECTION)<>0));
        SetEditFlags(Dialog,IDC_INI_PARAM  ,EF_SCRIPT,ord((flags and ACF_INI_PARAM  )<>0));

        SetEditFlags(Dialog,IDC_INI_VALUE,EF_SCRIPT,ord((flags and ACF_INI_VALUE)<>0));
        if ((flags and ACF_INI_DELETE)<>0) or
           ((flags and ACF_INI_LR    )<>0) then
          EnableEditField(Dialog,IDC_INI_VALUE,false);

        if (flags and ACF_INI_WRITE)<>0 then
          CheckDlgButton(Dialog,IDC_INI_WRITE,BST_CHECKED)
        else if (flags and ACF_INI_DELETE)<>0 then
          CheckDlgButton(Dialog,IDC_INI_DELETE,BST_CHECKED)
        else
          CheckDlgButton(Dialog,IDC_INI_READ,BST_CHECKED);

        EnableWindow(GetDlgItem(Dialog,IDC_INI_LR),(flags and ACF_INI_DELETE)=0);
        if (flags and ACF_INI_LR)<>0 then
          CheckDlgButton(Dialog,IDC_INI_LR,BST_CHECKED);

        EnableWindow(GetDlgItem(Dialog,IDC_INI_UTF),(flags and ACF_INI_WRITE)<>0);
        if (flags and ACF_INI_UTF)<>0 then
          CheckDlgButton(Dialog,IDC_INI_UTF,BST_CHECKED);
      end;
      NoProcess:=false;
    end;

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

      SetDlgItemTextW(Dialog,IDC_INI_PATH   ,nil);
      SetDlgItemTextW(Dialog,IDC_INI_SECTION,nil);
      SetDlgItemTextW(Dialog,IDC_INI_PARAM  ,nil);
      SetDlgItemTextW(Dialog,IDC_INI_VALUE  ,nil);
      SetEditFlags(Dialog,IDC_INI_PATH   ,EF_ALL,0);
      SetEditFlags(Dialog,IDC_INI_SECTION,EF_ALL,0);
      SetEditFlags(Dialog,IDC_INI_PARAM  ,EF_ALL,0);
      SetEditFlags(Dialog,IDC_INI_VALUE  ,EF_ALL,0);

      EnableWindow(GetDlgItem(Dialog,IDC_INI_LR ),true);
      EnableWindow(GetDlgItem(Dialog,IDC_INI_UTF),false);

      CheckDlgButton(Dialog,IDC_INI_READ,BST_CHECKED);
      NoProcess:=false;
    end;

    WM_ACT_SAVE: begin
      with tINIAction(lParam) do
      begin
        if IsDlgButtonChecked(Dialog,IDC_INI_WRITE)<>BST_UNCHECKED then
          flags:=flags or ACF_INI_WRITE
        else if IsDlgButtonChecked(Dialog,IDC_INI_DELETE)<>BST_UNCHECKED then
          flags:=flags or ACF_INI_DELETE;

        if IsDlgButtonChecked(Dialog,IDC_INI_LR )<>BST_UNCHECKED then flags:=flags or ACF_INI_LR;
        if IsDlgButtonChecked(Dialog,IDC_INI_UTF)<>BST_UNCHECKED then flags:=flags or ACF_INI_UTF;

        inifile  :=GetDlgText(Dialog,IDC_INI_PATH);
        section  :=GetDlgText(Dialog,IDC_INI_SECTION);
        parameter:=GetDlgText(Dialog,IDC_INI_PARAM);
        value    :=GetDlgText(Dialog,IDC_INI_VALUE);
        if (GetEditFlags(Dialog,IDC_INI_PATH   ) and EF_SCRIPT)<>0 then flags:=flags or ACF_INI_FILE;
        if (GetEditFlags(Dialog,IDC_INI_SECTION) and EF_SCRIPT)<>0 then flags:=flags or ACF_INI_SECTION;
        if (GetEditFlags(Dialog,IDC_INI_PARAM  ) and EF_SCRIPT)<>0 then flags:=flags or ACF_INI_PARAM;
        if (GetEditFlags(Dialog,IDC_INI_VALUE  ) and EF_SCRIPT)<>0 then flags:=flags or ACF_INI_VALUE;
      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_INI_INIBTN: begin
              if not FillFileName(Dialog,IDC_INI_PATH) then
                exit;
            end;

            IDC_INI_READ,
            IDC_INI_WRITE,
            IDC_INI_DELETE: begin
              EnableEditField(Dialog,IDC_INI_VALUE,loword(wParam)<>IDC_INI_DELETE);

              EnableWindow(GetDlgItem(Dialog,IDC_INI_LR),
                IsDlgButtonChecked(Dialog,IDC_INI_DELETE)=BST_UNCHECKED);
              EnableWindow(GetDlgItem(Dialog,IDC_INI_UTF),
                IsDlgButtonChecked(Dialog,IDC_INI_WRITE)<>BST_UNCHECKED);
            end;

            IDC_INI_LR: begin
              EnableEditField(Dialog,IDC_INI_VALUE,IsDlgButtonChecked(Dialog,IDC_INI_LR)=BST_UNCHECKED);
            end;
          end;

          if not NoProcess then
            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:=tINIAction.Create(vc.Hash);
end;

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

procedure Init;
begin
  vc.Next    :=ModuleLink;

  vc.Name    :='INI';
  vc.Dialog  :=@CreateDialog;
  vc.Create  :=@CreateAction;
  vc.Icon    :='IDI_INI';

  ModuleLink :=@vc;
end;

begin
  Init;
end.