application.c
13.8 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
/*
* "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., 51 Franklin
* St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Este programa está nomeado como - e possui - linhas de código.
*
* Contatos:
*
* perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
* erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
*
* References:
*
* https://fossies.org/linux/gtk+/examples/plugman.c
*
*/
#include "private.h"
#include <pw3270.h>
#include <pw3270/application.h>
#include <pw3270/actions.h>
#include <pw3270/keypad.h>
#include <stdlib.h>
enum {
PROP_ZERO,
PROP_UI_STYLE,
NUM_PROPERTIES
};
static GParamSpec * props[NUM_PROPERTIES];
struct _pw3270ApplicationClass {
GtkApplicationClass parent_class;
};
struct _pw3270Application {
GtkApplication parent;
GSettings * settings;
GList * keypads;
GSList * plugins; ///< @brief Handlers of the loaded plugins.
PW3270_UI_STYLE ui_style;
};
static void startup(GApplication * application);
static void activate(GApplication * application);
static void finalize(GObject *object);
G_DEFINE_TYPE(pw3270Application, pw3270Application, GTK_TYPE_APPLICATION);
static void get_property(GObject *object, guint prop_id, GValue *value, GParamSpec G_GNUC_UNUSED(*pspec)) {
switch (prop_id) {
case PROP_UI_STYLE:
g_value_set_uint(value,pw3270_application_get_ui_style(G_APPLICATION(object)));
break;
default:
g_assert_not_reached ();
}
}
static void set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec G_GNUC_UNUSED(*pspec)) {
switch (prop_id) {
case PROP_UI_STYLE:
pw3270_application_set_ui_style(G_APPLICATION(object),g_value_get_uint(value));
break;
default:
g_assert_not_reached ();
}
}
static void window_added(GtkApplication *application, GtkWindow *window) {
GTK_APPLICATION_CLASS(pw3270Application_parent_class)->window_added(application,window);
void (*call)(GtkWindow *window);
GSList * item;
for(item = PW3270_APPLICATION(application)->plugins; item; item = g_slist_next(item)) {
if(g_module_symbol((GModule *) item->data, "pw3270_plugin_window_added", (gpointer *) &call)) {
call(window);
}
}
}
static void window_removed(GtkApplication *application, GtkWindow *window) {
debug("%s(%p)",__FUNCTION__,window);
void (*call)(GtkWindow *window);
GSList * item;
for(item = PW3270_APPLICATION(application)->plugins; item; item = g_slist_next(item)) {
if(g_module_symbol((GModule *) item->data, "pw3270_plugin_window_removed", (gpointer *) &call)) {
call(window);
}
}
GTK_APPLICATION_CLASS(pw3270Application_parent_class)->window_removed(application,window);
}
static void pw3270Application_class_init(pw3270ApplicationClass *klass) {
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->get_property = get_property;
object_class->set_property = set_property;
object_class->finalize = finalize;
{
GtkApplicationClass *application_class = GTK_APPLICATION_CLASS(klass);
application_class->window_added = window_added;
application_class->window_removed = window_removed;
}
{
GApplicationClass * application_class = G_APPLICATION_CLASS(klass);
application_class->startup = startup;
application_class->activate = activate;
application_class->open = pw3270_application_open;
}
props[PROP_UI_STYLE] =
g_param_spec_uint(
"ui-style",
_("UI Type"),
_("The code of the User interface type"),
PW3270_UI_STYLE_CLASSICAL,
PW3270_UI_STYLE_AUTOMATIC,
#ifdef _WIN32
PW3270_UI_STYLE_CLASSICAL,
#else
PW3270_UI_STYLE_AUTOMATIC,
#endif // _WIN32
G_PARAM_READABLE|G_PARAM_WRITABLE
);
g_object_class_install_properties(object_class, NUM_PROPERTIES, props);
}
static gboolean on_user_interface(const gchar G_GNUC_UNUSED(*option), const gchar *value, gpointer G_GNUC_UNUSED(dunno), GError **error) {
g_autoptr(GSettings) app_settings = pw3270_application_settings_new();
if(!app_settings) {
g_warning("Can't get application settings");
return FALSE;
}
g_autoptr(GSettings) win_settings = pw3270_application_window_settings_new();
if(!win_settings) {
g_warning("Can't get window settings");
return FALSE;
}
if(!g_ascii_strcasecmp(value,"gnome")) {
g_settings_set_uint(app_settings,"ui-style",PW3270_UI_STYLE_GNOME);
g_settings_set_boolean(win_settings,"toolbar-visible",TRUE);
g_settings_set_boolean(win_settings,"menubar-visible",FALSE);
} else if(!g_ascii_strcasecmp(value,"classic")) {
g_settings_set_uint(app_settings,"ui-style",PW3270_UI_STYLE_CLASSICAL);
g_settings_set_boolean(win_settings,"toolbar-visible",TRUE);
g_settings_set_boolean(win_settings,"menubar-visible",TRUE);
} else if(!g_ascii_strcasecmp(value,"default")) {
g_settings_set_uint(app_settings,"ui-style",PW3270_UI_STYLE_AUTOMATIC);
} else {
g_set_error(
error,
g_quark_from_static_string(G_STRINGIFY(PRODUCT_NAME)),
EINVAL,
_( "\"%s\" is not a valid user interface name" ), value
);
}
return TRUE;
}
static void pw3270Application_init(pw3270Application *app) {
static GOptionEntry cmd_options[] = {
{ "user-interface", 'U', 0, G_OPTION_ARG_CALLBACK, &on_user_interface, N_( "Set the user-interface type" ), NULL },
{ NULL }
};
g_application_add_main_option_entries(G_APPLICATION(app), cmd_options);
#ifdef _WIN32
app->ui_style = PW3270_UI_STYLE_CLASSICAL;
#else
app->ui_style = PW3270_UI_STYLE_AUTOMATIC;
#endif // _WIN32
// Get settings
app->settings = pw3270_application_settings_new();
// Bind properties
if(app->settings) {
g_object_ref_sink(G_OBJECT(app->settings));
#ifdef _WIN32
{
// https://stackoverflow.com/questions/37035936/how-to-get-native-windows-decorations-on-gtk3-on-windows-7-and-msys2
int gtk_csd = g_settings_get_int(app->settings,"gtk-csd");
if(gtk_csd != -1) {
g_autofree gchar * env = g_strdup_printf("GTK_CSD=%d",gtk_csd);
putenv(env);
}
}
#endif // _WIN32
g_settings_bind(app->settings, "ui-style", app, "ui-style", G_SETTINGS_BIND_DEFAULT);
}
// Get plugins.
{
#ifdef _WIN32
lib3270_autoptr(char) path = lib3270_build_data_filename("plugins",NULL);
#else
const gchar * path = G_STRINGIFY(LIBDIR) G_DIR_SEPARATOR_S G_STRINGIFY(PRODUCT_NAME) "-plugins";
#endif // _WIN32
if(g_file_test(path,G_FILE_TEST_IS_DIR)) {
g_message("Loading plugins from %s",path);
GError * err = NULL;
GDir * dir = g_dir_open(path,0,&err);
if(dir) {
const gchar *name;
while((name = g_dir_read_name(dir)) != NULL) {
g_autofree gchar *filename = g_build_filename(path,name,NULL);
if(g_str_has_suffix(filename,G_MODULE_SUFFIX)) {
g_message("Loading %s",filename);
GModule *handle = g_module_open(filename,G_MODULE_BIND_LOCAL);
if(handle) {
app->plugins = g_slist_append(app->plugins,handle);
} else {
g_warning("Can't load %s: %s",filename,g_module_error());
}
}
}
g_dir_close(dir);
}
if(err) {
g_warning("Can't load plugins from %s: %s",path,err->message);
g_error_free(err);
}
}
}
}
static void finalize(GObject *object) {
pw3270Application * application = PW3270_APPLICATION(object);
if(application->plugins) {
#pragma GCC diagnostic push
#ifdef _WIN32
#pragma GCC diagnostic ignored "-Wcast-function-type"
#endif // _WIN32
g_slist_free_full(application->plugins,(GDestroyNotify) g_module_close);
#pragma GCC diagnostic pop
application->plugins = NULL;
}
if(application->settings) {
g_object_unref(application->settings);
application->settings = NULL;
}
g_list_free_full(application->keypads,g_object_unref);
G_OBJECT_CLASS(pw3270Application_parent_class)->finalize(object);
}
GtkApplication * pw3270_application_new(const gchar *application_id, GApplicationFlags flags) {
return g_object_new(
PW3270_TYPE_APPLICATION,
"application-id", application_id,
"flags", flags,
NULL);
}
void startup(GApplication *application) {
size_t ix;
pw3270Application * app = PW3270_APPLICATION(application);
G_APPLICATION_CLASS(pw3270Application_parent_class)->startup(application);
GSettings *settings = pw3270_application_get_settings(application);
//
// Common actions
//
GAction * actions[] = {
pw3270_about_action_new(),
pw3270_preferences_action_new(),
pw3270_quit_action_new()
};
for(ix = 0; ix < G_N_ELEMENTS(actions); ix++) {
g_action_map_add_action(G_ACTION_MAP(application),actions[ix]);
}
//
// Open session actions.
//
if(g_settings_get_boolean(settings,"allow-open-session-actions")) {
g_action_map_add_action(G_ACTION_MAP(application),pw3270_open_session_action_new());
}
//
// New tab actions
//
if(g_settings_get_boolean(settings,"allow-new-tab-actions")) {
GAction * new_tab_actions[] = {
pw3270_open_tab_action_new(),
pw3270_new_tab_action_new()
};
for(ix = 0; ix < G_N_ELEMENTS(new_tab_actions); ix++) {
g_action_map_add_action(G_ACTION_MAP(application),new_tab_actions[ix]);
}
}
//
// New window actions
//
if(g_settings_get_boolean(settings,"allow-new-window-actions")) {
GAction * new_window_actions[] = {
pw3270_open_window_action_new(),
pw3270_new_window_action_new()
};
for(ix = 0; ix < G_N_ELEMENTS(new_window_actions); ix++) {
g_action_map_add_action(G_ACTION_MAP(application),new_window_actions[ix]);
}
}
//
// Setup application menus
//
GtkBuilder * builder;
#ifdef DEBUG
builder = gtk_builder_new_from_file("ui/application.xml");
#else
{
lib3270_autoptr(char) build_file = lib3270_build_data_filename("ui","application.xml",NULL);
builder = gtk_builder_new_from_file(build_file);
}
#endif // DEBUG
//
// Load keypad models
//
{
lib3270_autoptr(char) keypad_path = lib3270_build_data_filename("keypad",NULL);
g_autoptr(GError) error = NULL;
g_autoptr(GDir) dir = g_dir_open(keypad_path,0,&error);
if(dir) {
const gchar *name = g_dir_read_name(dir);
while(!error && name) {
g_autofree gchar * path = g_build_filename(keypad_path,name,NULL);
app->keypads = pw3270_keypad_model_new_from_xml(app->keypads,path);
name = g_dir_read_name(dir);
}
}
if(error) {
g_message("Can't read %s: %s",keypad_path,error->message);
}
}
if(gtk_application_prefers_app_menu(GTK_APPLICATION(application)))
gtk_application_set_app_menu(GTK_APPLICATION (application), G_MENU_MODEL(gtk_builder_get_object (builder, "app-menu")));
gtk_application_set_menubar(GTK_APPLICATION (application), G_MENU_MODEL(gtk_builder_get_object (builder, "menubar")));
pw3270_load_placeholders(application, builder);
g_object_unref(builder);
}
void activate(GApplication *application) {
GtkWidget * window = pw3270_application_window_new(GTK_APPLICATION(application),NULL);
// Present the new window
pw3270_window_set_current_page(window,0);
gtk_window_present(GTK_WINDOW(window));
}
void pw3270_application_set_ui_style(GApplication *app, PW3270_UI_STYLE type) {
g_return_if_fail(PW3270_IS_APPLICATION(app));
pw3270Application * application = PW3270_APPLICATION(app);
if(application->ui_style == type)
return;
application->ui_style = type;
g_object_notify_by_pspec(G_OBJECT(app), props[PROP_UI_STYLE]);
}
PW3270_UI_STYLE pw3270_application_get_ui_style(GApplication *app) {
g_return_val_if_fail(PW3270_IS_APPLICATION(app),PW3270_UI_STYLE_CLASSICAL);
return PW3270_APPLICATION(app)->ui_style;
}
GSettings * pw3270_application_get_settings(GApplication *app) {
g_return_val_if_fail(PW3270_IS_APPLICATION(app),NULL);
return PW3270_APPLICATION(app)->settings;
}
GSList * pw3270_application_get_plugins(GApplication *app) {
g_return_val_if_fail(PW3270_IS_APPLICATION(app),NULL);
return PW3270_APPLICATION(app)->plugins;
}
void pw3270_application_plugin_foreach(GApplication *app, GFunc func, gpointer user_data) {
g_return_if_fail(PW3270_IS_APPLICATION(app));
GSList * item;
for(item = PW3270_APPLICATION(app)->plugins; item; item = g_slist_next(item)) {
func(item->data,user_data);
}
}
void pw3270_application_plugin_call(GApplication *app, const gchar *method, gpointer user_data) {
g_return_if_fail(PW3270_IS_APPLICATION(app));
int (*call)(GtkWidget *);
GSList * item;
for(item = PW3270_APPLICATION(app)->plugins; item; item = g_slist_next(item)) {
if(g_module_symbol((GModule *) item->data, method, (gpointer *) &call)) {
call(user_data);
}
}
}
GSettings * pw3270_application_settings_new() {
GSettings *settings = NULL;
#ifdef DEBUG
GError * error = NULL;
GSettingsSchemaSource * source =
g_settings_schema_source_new_from_directory(
".",
NULL,
TRUE,
&error
);
g_assert_no_error(error);
GSettingsSchema * schema =
g_settings_schema_source_lookup(
source,
"br.com.bb." G_STRINGIFY(PRODUCT_NAME),
TRUE);
debug("schema %s=%p","br.com.bb." PACKAGE_NAME,schema);
settings = g_settings_new_full(schema, NULL, NULL);
g_settings_schema_source_unref(source);
#else
settings = g_settings_new("br.com.bb." G_STRINGIFY(PRODUCT_NAME));
#endif // DEBUG
return settings;
}
GList * pw3270_application_get_keypad_models(GApplication *app) {
g_return_val_if_fail(PW3270_IS_APPLICATION(app),NULL);
return PW3270_APPLICATION(app)->keypads;
}