actions.c
18.1 KB
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/*
* "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
* (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
* aplicativos mainframe. Registro no INPI sob o nome G3270.
*
* Copyright (C) <2008> <Banco do Brasil S.A.>
*
* Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
* os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
* Free Software Foundation.
*
* Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
* GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
* A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
* obter mais detalhes.
*
* Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
* programa; se não, escreva para a Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA, 02111-1307, USA
*
* Este programa está nomeado como actions.c e possui - linhas de código.
*
* Contatos:
*
* perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
* erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
* licinio@bb.com.br (Licínio Luis Branco)
* kraucer@bb.com.br (Kraucer Fernandes Mazuco)
*
*/
#include "globals.h"
#include "uiparser/parser.h"
#include "v3270/v3270.h"
#include <lib3270/actions.h>
#include <lib3270/selection.h>
#include <stdlib.h>
#define ERROR_DOMAIN g_quark_from_static_string(PACKAGE_NAME)
#define TOGGLE_GDKDEBUG LIB3270_TOGGLE_COUNT+1
/*--[ Implement ]------------------------------------------------------------------------------------*/
static void lib3270_action(GtkAction *action, GtkWidget *widget)
{
int (*call)(H3270 *h) = (int (*)(H3270 *h)) g_object_get_data(G_OBJECT(action),"lib3270_call");
trace("Action %s activated on widget %p",gtk_action_get_name(action),widget);
call(GTK_V3270(widget)->host);
}
static void connect_action(GtkAction *action, GtkWidget *widget)
{
gchar *host = (gchar *) g_object_get_data(G_OBJECT(action),"host");
trace("Action %s activated on widget %p",gtk_action_get_name(action),widget);
if(host)
{
v3270_connect(widget,host);
return;
}
host = get_string_from_config("host","uri","");
if(*host)
{
v3270_connect(widget,host);
g_free(host);
return;
}
g_free(host);
hostname_action(action,widget);
}
static void nop_action(GtkAction *action, GtkWidget *widget)
{
trace("Action %s activated on widget %p",gtk_action_get_name(action),widget);
}
static void disconnect_action(GtkAction *action, GtkWidget *widget)
{
trace("Action %s activated on widget %p",gtk_action_get_name(action),widget);
v3270_disconnect(widget);
}
static void activate_action(GtkAction *action, GtkWidget *widget)
{
trace("Action %s activated on widget %p",gtk_action_get_name(action),widget);
gtk_widget_activate(widget);
}
static void reload_action(GtkAction *action, GtkWidget *widget)
{
trace("Action %s activated on widget %p",gtk_action_get_name(action),widget);
v3270_reload(widget);
}
static void copy_action(GtkAction *action, GtkWidget *widget)
{
trace("Action %s activated on widget %p",gtk_action_get_name(action),widget);
v3270_copy(widget);
}
static void append_action(GtkAction *action, GtkWidget *widget)
{
trace("Action %s activated on widget %p",gtk_action_get_name(action),widget);
v3270_copy_append(widget);
}
static void paste_clipboard_action(GtkAction *action, GtkWidget *widget)
{
trace("Action %s activated on widget %p",gtk_action_get_name(action),widget);
v3270_paste(widget);
}
static void paste_next_action(GtkAction *action, GtkWidget *widget)
{
trace("Action %s activated on widget %p",gtk_action_get_name(action),widget);
lib3270_pastenext(GTK_V3270(widget)->host);
}
static void connect_standard_action(GtkAction *action, GtkWidget *widget, const gchar *name)
{
#undef DECLARE_LIB3270_ACTION
#undef DECLARE_LIB3270_CLEAR_SELECTION_ACTION
#undef DECLARE_LIB3270_KEY_ACTION
#undef DECLARE_LIB3270_CURSOR_ACTION
#undef DECLARE_LIB3270_FKEY_ACTION
#define DECLARE_LIB3270_ACTION( name ) { #name, lib3270_ ## name },
#define DECLARE_LIB3270_CLEAR_SELECTION_ACTION( name ) { #name, lib3270_ ## name },
#define DECLARE_LIB3270_KEY_ACTION( name ) { #name, lib3270_ ## name },
#define DECLARE_LIB3270_CURSOR_ACTION( name ) { #name, lib3270_cursor_ ## name },
#define DECLARE_LIB3270_FKEY_ACTION( name ) // name
static const struct _lib3270_action
{
const gchar * name;
int (*call)(H3270 *h);
} lib3270_entry[] =
{
#include <lib3270/action_table.h>
};
static const struct _gtk_action
{
const gchar * name;
void (*call)(GtkAction *action, GtkWidget *widget);
}
gtk_action[] =
{
{ "activate", activate_action },
{ "reload", reload_action },
{ "connect", connect_action },
{ "disconnect", disconnect_action },
{ "hostname", hostname_action },
{ "editcolors", editcolors_action },
};
int f;
// Search for lib3270 predefined actions
for(f=0;f<G_N_ELEMENTS(lib3270_entry);f++)
{
if(!g_strcasecmp(name,lib3270_entry[f].name))
{
g_object_set_data(G_OBJECT(action),"lib3270_call",lib3270_entry[f].call);
g_signal_connect(action,"activate",G_CALLBACK(lib3270_action),widget);
return;
}
}
// Search for application actions
for(f=0;f<G_N_ELEMENTS(gtk_action);f++)
{
if(!g_strcasecmp(name,gtk_action[f].name))
{
g_signal_connect(action,"activate",G_CALLBACK(gtk_action[f].call),widget);
return;
}
}
if(!g_strcasecmp(name,"screensizes"))
return;
// Not-found, disable action
gtk_action_set_sensitive(action,FALSE);
}
static void lib3270_toggle_action(GtkToggleAction *action,GtkWidget *widget)
{
LIB3270_TOGGLE toggle = (LIB3270_TOGGLE) g_object_get_data(G_OBJECT(action),"toggle_id");
trace("Action %s toggled on widget %p (id=%d)",gtk_action_get_name(GTK_ACTION(action)),widget,(int) toggle);
if(toggle == TOGGLE_GDKDEBUG)
gdk_window_set_debug_updates(gtk_toggle_action_get_active(action));
else if(toggle < LIB3270_TOGGLE_COUNT)
lib3270_set_toggle(GTK_V3270(widget)->host,toggle,gtk_toggle_action_get_active(action));
}
static void selection_move_action(GtkAction *action, GtkWidget *widget)
{
trace("Action %s activated on widget %p",gtk_action_get_name(action),widget);
lib3270_move_selection(GTK_V3270(widget)->host,(LIB3270_DIRECTION) g_object_get_data(G_OBJECT(action),"direction"));
}
static void cursor_move_action(GtkAction *action, GtkWidget *widget)
{
int flags = (int) g_object_get_data(G_OBJECT(action),"move_flags");
trace("Action %s activated on widget %p flags=%04x",gtk_action_get_name(action),widget,(unsigned short) g_object_get_data(G_OBJECT(action),"move_flags"));
lib3270_move_cursor(GTK_V3270(widget)->host,(LIB3270_DIRECTION) (flags & 0x03), (flags & 0x80) );
}
static void connect_move_action(GtkAction *action, GtkWidget *widget, const gchar *target, unsigned short flags, GError **error)
{
if(!target)
{
gtk_action_set_sensitive(action,FALSE);
return;
}
if(!g_strcasecmp(target,"selection"))
{
g_object_set_data(G_OBJECT(action),"direction",(gpointer) (flags & 3));
g_signal_connect(action,"activate",G_CALLBACK(selection_move_action),widget);
}
else if(!g_strcasecmp(target,"cursor"))
{
g_object_set_data(G_OBJECT(action),"move_flags",(gpointer) ((int) flags));
g_signal_connect(action,"activate",G_CALLBACK(cursor_move_action),widget);
}
else
{
*error = g_error_new( ERROR_DOMAIN,
ENOENT,
_( "Unexpected target \"%s\""),
target);
}
}
static void action_pfkey(GtkAction *action, GtkWidget *widget)
{
trace("Action %s activated on widget %p key=%p",gtk_action_get_name(action),widget,g_object_get_data(G_OBJECT(action),"pfkey"));
lib3270_pfkey(GTK_V3270(widget)->host,(int) g_object_get_data(G_OBJECT(action),"pfkey"));
}
static void action_pakey(GtkAction *action, GtkWidget *widget)
{
trace("Action %s activated on widget %p key=%p",gtk_action_get_name(action),widget,g_object_get_data(G_OBJECT(action),"pakey"));
lib3270_pakey(GTK_V3270(widget)->host,(int) g_object_get_data(G_OBJECT(action),"pakey"));
}
static void action_set_toggle(GtkAction *action, GtkWidget *widget)
{
LIB3270_TOGGLE id = (LIB3270_TOGGLE) g_object_get_data(G_OBJECT(action),"toggle_id");
trace("Action %s activated on widget %p toggle=%d",gtk_action_get_name(action),widget,id);
lib3270_set_toggle(GTK_V3270(widget)->host,id,1);
}
static void action_reset_toggle(GtkAction *action, GtkWidget *widget)
{
LIB3270_TOGGLE id = (LIB3270_TOGGLE) g_object_get_data(G_OBJECT(action),"toggle_id");
trace("Action %s activated on widget %p toggle=%d",gtk_action_get_name(action),widget,id);
lib3270_set_toggle(GTK_V3270(widget)->host,id,0);
}
static void action_select_all(GtkAction *action, GtkWidget *widget)
{
lib3270_selectall(GTK_V3270(widget)->host);
}
static void action_select_field(GtkAction *action, GtkWidget *widget)
{
lib3270_selectfield(GTK_V3270(widget)->host);
}
static void action_select_none(GtkAction *action, GtkWidget *widget)
{
lib3270_unselect(GTK_V3270(widget)->host);
}
static void action_select_last(GtkAction *action, GtkWidget *widget)
{
lib3270_reselect(GTK_V3270(widget)->host);
}
static int id_from_array(const gchar *key, const gchar **array, GError **error)
{
int f;
if(!key)
return -1;
for(f = 0;array[f];f++)
{
if(!g_strcasecmp(key,array[f]))
return f;
}
*error = g_error_new(ERROR_DOMAIN,EINVAL, _("Unexpected or invalid attribute value \"%s\""), key );
return -1;
}
static int get_attribute_id(const gchar *name, const gchar *key, gchar **nm, const gchar **src, const gchar **names, const gchar **values, GError **error)
{
const gchar *attr = ui_get_attribute(key,names,values);
int id;
if(!attr)
{
*error = g_error_new(ERROR_DOMAIN,EINVAL,_("Attribute \"%s\" is invalid or undefined" ), key);
return -1;
}
id = id_from_array(attr,src,error);
if(id >= 0)
{
if(*nm)
g_free(*nm);
*nm = g_strconcat(name,attr,NULL);
return id;
}
if(!*error)
*error = g_error_new(ERROR_DOMAIN,EINVAL,_("Unexpected or invalid %s attribute: \"%s\"" ), key, attr);
return -1;
}
static int setup_block_action(const gchar *name, const gchar *attr, GError **error)
{
int id = -1;
if(!attr)
{
*error = g_error_new(ERROR_DOMAIN,EINVAL,_("%s action needs src attribute" ), name);
}
else
{
static const gchar * src[] = { "all", "selected", "copy", NULL };
id = id_from_array(attr,src,error);
if(id < 0)
{
*error = g_error_new(ERROR_DOMAIN,EINVAL,_("Unexpected or invalid src attribute: \"%s\"" ), attr);
}
}
return id;
}
GtkAction * ui_get_action(GtkWidget *widget, const gchar *name, GHashTable *hash, const gchar **names, const gchar **values, GError **error)
{
static const gchar *actionname[ACTION_COUNT] = { "pastenext",
"reselect",
"setfullscreen",
"resetfullscreen"
};
GtkAction * action = NULL;
GtkAction **toggle_action = (GtkAction **) g_object_get_data(G_OBJECT(widget),"toggle_actions");
const gchar * direction = ui_get_attribute("direction",names,values);
unsigned short flags = 0;
const GCallback * callback = NULL;
const gchar * attr;
int id = 0;
gchar * nm = NULL;
int f;
enum _action_type
{
ACTION_TYPE_DEFAULT,
ACTION_TYPE_TOGGLE,
ACTION_TYPE_MOVE,
ACTION_TYPE_PFKEY,
ACTION_TYPE_PAKEY,
ACTION_TYPE_SET,
ACTION_TYPE_RESET,
ACTION_TYPE_TABLE,
} action_type = ACTION_TYPE_DEFAULT;
if(direction)
{
static const gchar *dirname[] = { "up", "down", "left", "right" };
int f;
for(f=0;f<G_N_ELEMENTS(dirname);f++)
{
if(!g_strcasecmp(direction,dirname[f]))
{
flags |= f;
break;
}
}
}
if(ui_get_bool_attribute("selecting",names,values,FALSE))
flags |= 0x80;
// Build action name & type
if(!g_strcasecmp(name,"toggle"))
{
action_type = ACTION_TYPE_TOGGLE;
attr = ui_get_attribute("id",names,values);
if(!attr)
attr = ui_get_attribute("toggle",names,values);
if(g_strcasecmp(attr,"gdkdebug"))
{
id = lib3270_get_toggle_id(attr);
if(id < 0)
{
*error = g_error_new(ERROR_DOMAIN,EINVAL,_("%s action needs a valid toggle name" ), name);
return NULL;
}
}
else
{
id = TOGGLE_GDKDEBUG;
}
nm = g_strconcat(name,attr,NULL);
}
else if(!g_strcasecmp(name,"move"))
{
action_type = ACTION_TYPE_MOVE;
attr = ui_get_attribute("target",names,values);
if(!(attr && direction))
{
*error = g_error_new(ERROR_DOMAIN,EINVAL,"%s",_("Move action needs target & direction attributes" ));
return NULL;
}
nm = g_strconcat((flags & 0x80) ? "select" : "move",attr,direction, NULL);
}
else if(!g_strcasecmp(name,"paste"))
{
static const GCallback cbk[] = { G_CALLBACK(paste_clipboard_action),
G_CALLBACK(paste_next_action),
G_CALLBACK(paste_file_action)
};
static const gchar * src[] = { "clipboard", "next", "file", NULL };
callback = cbk;
action_type = ACTION_TYPE_TABLE;
id = get_attribute_id(name,"src",&nm,src,names,values,error);
if(id < 0)
return NULL;
}
else if(!g_strcasecmp(name,"copy"))
{
static const gchar * src[] = { "begin",
"table",
"image",
"append",
NULL
};
static const GCallback cbk[] = { G_CALLBACK(copy_action),
G_CALLBACK(nop_action),
G_CALLBACK(nop_action),
G_CALLBACK(append_action)
};
callback = cbk;
action_type = ACTION_TYPE_TABLE;
id = get_attribute_id(name,"mode",&nm,src,names,values,error);
if(id < 0)
return NULL;
}
else if(!g_strcasecmp(name,"select"))
{
static const gchar * src[] = { "all",
"field",
"none",
"last",
NULL
};
static const GCallback cbk[] = { G_CALLBACK(action_select_all),
G_CALLBACK(action_select_field),
G_CALLBACK(action_select_none),
G_CALLBACK(action_select_last)
};
callback = cbk;
action_type = ACTION_TYPE_TABLE;
id = get_attribute_id(name,"target",&nm,src,names,values,error);
if(id < 0)
return NULL;
}
else if(!g_strcasecmp(name,"save"))
{
static const GCallback cbk[] = { G_CALLBACK(save_all_action),
G_CALLBACK(save_selected_action),
G_CALLBACK(save_copy_action)
};
callback = cbk;
action_type = ACTION_TYPE_TABLE;
attr = ui_get_attribute("src",names,values);
id = setup_block_action(name,attr,error);
if(*error)
return NULL;
nm = g_strconcat(name,attr,NULL);
}
else if(!g_strcasecmp(name,"print"))
{
static const GCallback cbk[] = { G_CALLBACK(print_all_action),
G_CALLBACK(print_selected_action),
G_CALLBACK(print_copy_action)
};
callback = cbk;
action_type = ACTION_TYPE_TABLE;
attr = ui_get_attribute("src",names,values);
id = setup_block_action(name,attr,error);
if(*error)
return NULL;
nm = g_strconcat(name,attr,NULL);
}
else if(!g_strcasecmp(name,"set"))
{
action_type = ACTION_TYPE_SET;
attr = ui_get_attribute("toggle",names,values);
id = lib3270_get_toggle_id(attr);
if(id < 0)
{
*error = g_error_new(ERROR_DOMAIN,EINVAL,_("%s action needs a valid toggle name" ), name);
return NULL;
}
nm = g_strconcat("set",attr,NULL);
}
else if(!g_strcasecmp(name,"reset"))
{
action_type = ACTION_TYPE_RESET;
attr = ui_get_attribute("toggle",names,values);
id = lib3270_get_toggle_id(attr);
if(id < 0)
{
*error = g_error_new(ERROR_DOMAIN,EINVAL,_("%s action needs a valid toggle name" ), name);
return NULL;
}
nm = g_strconcat("reset",attr,NULL);
}
else if(!g_strcasecmp(name,"pfkey"))
{
action_type = ACTION_TYPE_PFKEY;
attr = ui_get_attribute("id",names,values);
if(!attr)
{
*error = g_error_new(ERROR_DOMAIN,EINVAL,_("%s action needs a valid id attribute" ),name);
return NULL;
}
id = atoi(attr);
nm = g_strdup_printf("pf%02d",id);
}
else if(!g_strcasecmp(name,"pakey"))
{
action_type = ACTION_TYPE_PAKEY;
attr = ui_get_attribute("id",names,values);
if(!attr)
{
*error = g_error_new(ERROR_DOMAIN,EINVAL,_("%s action needs a valid id attribute" ),name);
return NULL;
}
id = atoi(attr);
nm = g_strdup_printf("pa%02d",id);
}
else
{
attr = ui_get_attribute("name",names,values);
nm = g_strdup(attr ? attr : name);
}
// Check if action is available
action = g_hash_table_lookup(hash,nm);
if(action)
{
g_free(nm);
return action;
}
// Not available, create a new one
switch(action_type)
{
case ACTION_TYPE_DEFAULT:
action = gtk_action_new(nm,NULL,NULL,NULL);
connect_standard_action(action,widget,name);
break;
case ACTION_TYPE_TOGGLE:
action = GTK_ACTION(gtk_toggle_action_new(nm,NULL,NULL,NULL));
if(id < LIB3270_TOGGLE_COUNT)
toggle_action[id] = action;
g_object_set_data(G_OBJECT(action),"toggle_id",(gpointer) id);
gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action),(lib3270_get_toggle(GTK_V3270(widget)->host,id) != 0));
g_signal_connect(action,"toggled",G_CALLBACK(lib3270_toggle_action),widget);
break;
case ACTION_TYPE_MOVE:
action = gtk_action_new(nm,NULL,NULL,NULL);
connect_move_action(action,widget,attr,flags,error);
break;
case ACTION_TYPE_PFKEY:
action = gtk_action_new(nm,NULL,NULL,NULL);
g_object_set_data(G_OBJECT(action),"pfkey",(gpointer) id);
g_signal_connect(action,"activate",G_CALLBACK(action_pfkey),widget);
break;
case ACTION_TYPE_PAKEY:
action = gtk_action_new(nm,NULL,NULL,NULL);
g_object_set_data(G_OBJECT(action),"pakey",(gpointer) id);
g_signal_connect(action,"activate",G_CALLBACK(action_pakey),widget);
break;
case ACTION_TYPE_SET:
action = gtk_action_new(nm,NULL,NULL,NULL);
g_object_set_data(G_OBJECT(action),"toggle_id",(gpointer) id);
g_signal_connect(action,"activate",G_CALLBACK(action_set_toggle),widget);
break;
case ACTION_TYPE_RESET:
action = gtk_action_new(nm,NULL,NULL,NULL);
g_object_set_data(G_OBJECT(action),"toggle_id",(gpointer) id);
g_signal_connect(action,"activate",G_CALLBACK(action_reset_toggle),widget);
break;
case ACTION_TYPE_TABLE:
action = gtk_action_new(nm,NULL,NULL,NULL);
g_signal_connect(action,"activate",callback[id],widget);
break;
}
for(f=0;f<ACTION_COUNT;f++)
{
if(!g_strcasecmp(actionname[f],nm))
{
GtkAction **named_action = (GtkAction **) g_object_get_data(G_OBJECT(widget),"named_actions");
named_action[f] = action;
break;
}
}
g_hash_table_insert(hash,nm,action);
return action;
}