summaryrefslogtreecommitdiff
path: root/plugins/Actman/ua/i_inoutxm.inc
blob: 4a539a827564a4f0100bdf70001651e82fedd366 (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
{}

const
  ioAction         :PWideChar = 'Action';
  ioUA             :PWideChar = 'UA';

  ioName           :PWideChar = 'name';

  ioTwoState       :PWideChar = 'twostate';
  ioSaveState      :PWideChar = 'savestate';

  ioHotkey         :PWideChar = 'Hotkey';
  ioToolbar        :PWideChar = 'Toolbar';
  ioTabSRMM        :PWideChar = 'TabSRMM';
  ioMenuItem       :PWideChar = 'Menu';

  ioTooltip        :PWideChar = 'tooltip';
  ioTooltipPressed :PWideChar = 'tt_pressed';

  ioType           :PWideChar = 'type';
  ioMenuPopup      :PWideChar = 'Popup';
  ioMenuName       :PWideChar = 'Name';
  ioMenuShow       :PWideChar = 'Show';
  ioMenuUsed       :PWideChar = 'Used';
  ioMenuSeparated  :PWideChar = 'Separated';
  ioNoTranslate    :PWideChar = 'NoTranslate';


function ImportMenuItems(node:HXML;var MenuItem:tUAMenuItem):integer;
begin
  result:=0;

  with MenuItem do
  begin
    menu_opt:=0;
  // popup
    StrDupW(szMenuPopup,xmlGetAttrValue(node,ioMenuPopup));
  // name
    StrDupW(szMenuNameVars,xmlGetAttrValue(node,ioMenuName));
  // show
    StrDupW(szMenuShowWhenVars,xmlGetAttrValue(node,ioMenuShow));
  // used
    if StrToInt(xmlGetAttrValue(node,ioMenuUsed))<>0 then
      menu_opt:=menu_opt or UAF_MENUUSE;
  // separated
    if StrToInt(xmlGetAttrValue(node,ioMenuSeparated))<>0 then
      menu_opt:=menu_opt or UAF_MENUSEP;
  // no translate
    if StrToInt(xmlGetAttrValue(node,ioNoTranslate))<>0 then
      menu_opt:=menu_opt or UAF_NOTRANS;
  end;
end;

function ImportUAction(actnode:HXML;var UA:tMyActionItem):integer;
var
  num,i:integer;
  sub:HXML;
begin
  result:=0;
  if actnode=0 then exit;

  UA.flags:=0;
  // ----- Common -----
  if StrToInt(xmlGetAttrValue(actnode,ioTwoState))<>0 then
    UA.flags:=UA.flags or UAF_2STATE;

  if StrToInt(xmlGetAttrValue(actnode,ioSaveState))<>0 then
    UA.flags:=UA.flags or UAF_SAVESTATE;

  if StrToInt(xmlGetAttrValue(actnode,ioHotkey))<>0 then
    UA.flags:=UA.flags or UAF_REGHOTKEY;
  if StrToInt(xmlGetAttrValue(actnode,ioToolbar))<>0 then
    UA.flags:=UA.flags or UAF_REGTTBB;
  if StrToInt(xmlGetAttrValue(actnode,ioTabSRMM))<>0 then
    UA.flags:=UA.flags or UAF_REGTABB;

  // ----- Hotkey -----
  // nothing

  // ----- Modern CList toolbar -----
  // source - ANSI text
  sub:=xmlGetNthChild(actnode,ioToolbar,0);
  WideToAnsi(xmlGetAttrValue(sub,ioTooltip       ),UA.szTTBTooltip       ,MirandaCP);
  WideToAnsi(xmlGetAttrValue(sub,ioTooltipPressed),UA.szTTBTooltipPressed,MirandaCP);

  // ----- TabSRMM toolbar -----
  sub:=xmlGetNthChild(actnode,ioTabSRMM,0);
  StrDupW(UA.szTabBTooltip       ,xmlGetAttrValue(sub,ioTooltip));
  StrDupW(UA.szTabBTooltipPressed,xmlGetAttrValue(sub,ioTooltipPressed));

  // ----- Menus -----
  num:=0;
  repeat
    sub:=xmlGetNextChild(actnode,ioMenuItem,@num);
    if sub=0 then break;

    i:=StrToInt(xmlGetAttrValue(sub,ioType));
    ImportMenuItems(sub,UA.UAMenuItem[tMenuType(i)]);
  until false;
end;

function Import(fname:PWideChar;aflags:dword):integer;
var
  i,j,act:integer;
  root,actnode:HXML;
  pcw,res:pWideChar;
  f:THANDLE;
  num,num1:integer;
  ptr,ptr1:pChain;
begin
  result:=0;

  if (fname=nil) or (fname^=#0) then
    exit;
  i:=GetFSize(fname);
  if i=0 then
    exit;

  num:=CallService(MS_ACT_GETLIST,0,LPARAM(@ptr));
  if num=0 then exit;
  ptr1:=ptr;

  mGetMem (res ,i+SizeOf(WideChar));
  FillChar(res^,i+SizeOf(WideChar),0);
  f:=Reset(fname);
  BlockRead(f,res^,i);
  CloseHandle(f);

  root:=xmlParseString(ChangeUnicode(res),@i,nil);
  j:=0;
  repeat
    actnode:=xmlGetNthChild(root,ioAction,j);
    if actnode=0 then break;
    // search id by name?
    pcw:=xmlGetAttrValue(actnode,ioName);
    ptr:=ptr1;
    inc(pbyte(ptr),4);
    for i:=0 to num-1 do
    begin
      if (ptr.flags and ACF_SELECTED)<>0 then
      begin
        if StrCmpw(pcw,ptr.descr)=0 then
        begin
          // delete old UA for overwrited actions
          if (ptr.flags and ACF_OVERLOAD)<>0 then
          begin
            for act:=0 to HIGH(UActionList) do
            begin
              if ptr.id=UActionList[act].dwActID then
              begin
                DeleteUAction(act,true);
                break;
              end;
            end;
          end;
          num1:=AddUAction(-1,ptr);
          ImportUAction(xmlGetNthChild(actnode,ioUA,0),UActionList[num1]);
          break;
        end;
      end;
      inc(ptr);
    end;
    
    inc(j);
  until false;
  xmlDestroyNode(root);

  CallService(MS_ACT_FREELIST,0,LPARAM(ptr1));
  mFreeMem(res);
  result:=1;
  if settings<>0 then
  begin
    FillActionList(settings);
    ShowAction(settings,-1);
  end;
end;

//--------------------------

function ExportMenuItems(node:HXML;const MenuItem:tUAMenuItem):HXML;
begin
  result:=xmlAddChild(node,ioMenuItem,nil);
  with MenuItem do
  begin
  // popup
    if (szMenuPopup<>nil) and (szMenuPopup^<>#0) then
      xmlAddAttr(result,ioMenuPopup,szMenuPopup);
  // name
    if (szMenuNameVars<>nil) and (szMenuNameVars^<>#0) then
      xmlAddAttr(result,ioMenuName,szMenuNameVars);
  // show
    if (szMenuShowWhenVars<>nil) and (szMenuShowWhenVars^<>#0) then
      xmlAddAttr(result,ioMenuShow,szMenuShowWhenVars);
  // used
    xmlAddAttrInt(result,ioMenuUsed,ord((menu_opt AND UAF_MENUUSE)<>0));
  // separated
    xmlAddAttrInt(result,ioMenuSeparated,ord((menu_opt AND UAF_MENUSEP)<>0));
  // no translate
    xmlAddAttrInt(result,ioNoTranslate,ord((menu_opt AND UAF_NOTRANS)<>0));
  end;
end;

procedure WriteUAction(root:HXML;id:dword;name:pWideChar);
var
  i:integer;
  lmenu:tMenuType;
  pc:pWideChar;
  actnode,sub:HXML;
  UA:pMyActionItem;
begin
  for i:=0 to HIGH(UActionList) do
  begin
    if UActionList[i].dwActID=id then
    begin
      UA:=@UActionList[i];
      actnode:=xmlGetChildByAttrValue(root,ioAction,ioName,name);
      if actnode=0 then break;
      // we don't need that node as is, just it's child for UA
      actnode:=xmlAddChild(actnode,ioUA,nil);

      // ----- Common -----
      xmlAddAttrInt(actnode,ioTwoState ,ORD((UA.flags and UAF_2STATE   )<>0));
      xmlAddAttrInt(actnode,ioSaveState,ORD((UA.flags and UAF_SAVESTATE)<>0));

      xmlAddAttrInt(actnode,ioHotkey ,ORD((UA.flags and UAF_REGHOTKEY)<>0));
      xmlAddAttrInt(actnode,ioToolbar,ORD((UA.flags and UAF_REGTTBB )<>0));
      xmlAddAttrInt(actnode,ioTabSRMM,ORD((UA.flags and UAF_REGTABB )<>0));

      // ----- Hotkey -----
      // nothing

      // ----- Modern CList toolbar -----
      // source - ANSI text
      if ((UA.szTTBTooltip       <>nil) and (UA.szTTBTooltip^       <>#0)) or
         ((UA.szTTBTooltipPressed<>nil) and (UA.szTTBTooltipPressed^<>#0)) then
      begin
        sub:=xmlAddChild(actnode,ioToolbar,nil);
        if (UA.szTTBTooltip<>nil) and (UA.szTTBTooltip^<>#0) then
        begin
          AnsiToWide(UA.szTTBTooltip,pc,MirandaCP);
          xmlAddAttr(sub,ioTooltip,pc);
          mFreeMem(pc);
        end;
        if (UA.szTTBTooltipPressed<>nil) and (UA.szTTBTooltipPressed^<>#0) then
        begin
          AnsiToWide(UA.szTTBTooltipPressed,pc,MirandaCP);
          xmlAddAttr(sub,ioTooltipPressed,pc);
          mFreeMem(pc);
        end;
      end;

      // ----- TabSRMM toolbar -----
      if ((UA.szTabBTooltip       <>nil) and (UA.szTabBTooltip^       <>#0)) or
         ((UA.szTabBTooltipPressed<>nil) and (UA.szTabBTooltipPressed^<>#0)) then
      begin
        sub:=xmlAddChild(actnode,ioTabSRMM,nil);
        if (UA.szTabBTooltip<>nil) and (UA.szTabBTooltip^<>#0) then
          xmlAddAttr(sub,ioTooltip,UA.szTabBTooltip);
        if (UA.szTabBTooltipPressed<>nil) and (UA.szTabBTooltipPressed^<>#0) then
          xmlAddAttr(sub,ioTooltipPressed,UA.szTabBTooltipPressed);
      end;

      // ----- Menus -----
      for lmenu:=main_menu to HIGH(tMenuType) do
      begin
        sub:=ExportMenuItems(actnode,UA.UAMenuItem[lmenu]);
        xmlAddAttrInt(sub,ioType,ORD(lmenu));
      end;

      break;
    end;
  end;
end;

function Export(fname:pWideChar;aflags:dword):integer;
var
  i,num:integer;
  f:THANDLE;
  root:HXML;
  res:pWideChar;
  ptr,ptr1:pChain;
begin
  result:=0;

  // we need append file, not rewrite
  i:=GetFSize(fname);
  if i=0 then exit;

  mGetMem (res ,i+SizeOf(WideChar));
  FillChar(res^,i+SizeOf(WideChar),0);
  f:=Reset(fname);
  BlockRead(f,res^,i);
  CloseHandle(f);
  root:=xmlParseString(res,@i,nil);
  mFreeMem(res);

  num:=CallService(MS_ACT_GETLIST,0,LPARAM(@ptr));
  if num>0 then
  begin
    ptr1:=ptr;
    inc(pbyte(ptr),4);
    for i:=0 to num-1 do
    begin
      if ((aflags and ACIO_SELECTED)=0) or
         ((ptr.flags and ACCF_EXPORT)<>0) then
      begin
        WriteUAction(root,ptr.id,ptr.descr);
      end;
      inc(ptr);
    end;
    CallService(MS_ACT_FREELIST,0,LPARAM(ptr1));
  end;

  res:=xmlToString(root,@i);

  f:=Rewrite(fname);
  BlockWrite(f,res^,i*SizeOf(WideChar));
  CloseHandle(f);
  xmlFree(res);
  xmlDestroyNode(root);
  result:=1;
end;

function ActInOut(wParam:WPARAM;lParam:LPARAM):int_ptr; cdecl;
begin
  if (wParam and ACIO_EXPORT)=0 then
  begin
    result:=Import(pWideChar(lParam),wParam);
  end
  else
  begin
    result:=Export(pWideChar(lParam),wParam);
  end;
end;