popup.c
5.72 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
/*
* "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. 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)
*
*/
/**
* @file popup.c
*
* @brief A callback based popup dialog engine.
*
*/
#include <config.h>
#include <internals.h>
#include <lib3270.h>
#include <lib3270/log.h>
#include <networking.h>
/*--[ Implement ]------------------------------------------------------------------------------------*/
LIB3270_EXPORT int lib3270_popup(H3270 *hSession, const LIB3270_POPUP *popup, unsigned char wait) {
return hSession->cbk.popup(hSession,popup,wait);
}
int lib3270_popup_translated(H3270 *hSession, const LIB3270_POPUP *popup, unsigned char wait) {
LIB3270_POPUP translated = *popup;
if(popup->title) {
translated.title = dgettext(GETTEXT_PACKAGE,popup->title);
}
if(popup->label) {
translated.label = dgettext(GETTEXT_PACKAGE,popup->label);
}
if(popup->summary) {
translated.summary = dgettext(GETTEXT_PACKAGE,popup->summary);
}
if(popup->body) {
translated.body = dgettext(GETTEXT_PACKAGE,popup->body);
}
int rc = hSession->cbk.popup(hSession,&translated,wait);
debug("%s - User response was '%s' (rc=%d)",__FUNCTION__,strerror(rc),rc);
if(rc) {
lib3270_write_trace(hSession,"User response was '%s' (rc=%d)",strerror(rc),rc);
}
return rc;
}
/// @brief Pop up an error dialog.
void popup_an_error(H3270 *hSession, const char *fmt, ...) {
lib3270_autoptr(char) summary = NULL;
if(fmt) {
va_list args;
va_start(args, fmt);
summary = lib3270_vsprintf(fmt,args);
va_end(args);
}
LIB3270_POPUP popup = {
.type = LIB3270_NOTIFY_ERROR,
.summary = summary
};
hSession->cbk.popup(hSession,&popup,0);
}
void popup_system_error(H3270 *hSession, const char *title, const char *summary, const char *fmt, ...) {
lib3270_autoptr(char) body = NULL;
if(fmt) {
va_list args;
va_start(args, fmt);
body = lib3270_vsprintf(fmt,args);
va_end(args);
}
LIB3270_POPUP popup = {
.type = LIB3270_NOTIFY_ERROR,
.title = title,
.summary = summary,
.body = body
};
hSession->cbk.popup(hSession,&popup,0);
}
LIB3270_EXPORT void lib3270_popup_dialog(H3270 *session, LIB3270_NOTIFY id, const char *title, const char *message, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
lib3270_popup_va(session, id, title, message, fmt, args);
va_end(args);
}
LIB3270_EXPORT void lib3270_popup_va(H3270 *hSession, LIB3270_NOTIFY id, const char *title, const char *message, const char *fmt, va_list args) {
CHECK_SESSION_HANDLE(hSession);
lib3270_autoptr(char) body = NULL;
if(fmt) {
body = lib3270_vsprintf(fmt,args);
}
LIB3270_POPUP popup = {
.type = id,
.title = title,
.summary = message,
.body = body
};
hSession->cbk.popup(hSession,&popup,0);
}
LIB3270_POPUP * lib3270_popup_clone_printf(const LIB3270_POPUP *origin, const char *fmt, ...) {
va_list args;
// Create body
lib3270_autoptr(char) body = NULL;
va_start(args, fmt);
body = lib3270_vsprintf(fmt, args);
va_end(args);
// Alocate new struct
LIB3270_POPUP * popup = lib3270_malloc(sizeof(LIB3270_POPUP)+strlen(body)+1);
if(origin) {
*popup = *origin;
} else {
memset(popup,0,sizeof(LIB3270_POPUP));
}
strcpy((char *)(popup+1),body);
popup->body = (char *)(popup+1);
return popup;
}
static int def_popup(H3270 *hSession, const LIB3270_POPUP *popup, unsigned char GNUC_UNUSED wait) {
const char * text[] = {
popup->title,
popup->summary,
popup->body
};
size_t ix;
for(ix = 0; ix < (sizeof(text)/sizeof(text[0])); ix++) {
if(text[ix])
lib3270_write_log(hSession,"popup","%s",text[ix]);
}
return ENOTSUP;
}
LIB3270_EXPORT void lib3270_set_popup_handler(H3270 *hSession, int (*handler)(H3270 *, const LIB3270_POPUP *, unsigned char wait)) {
if(handler)
hSession->cbk.popup = handler;
else
hSession->cbk.popup = def_popup;
}
void lib3270_set_network_error(H3270 *hSession, const char *summary, const char *fmt, ...) {
// Release last error.
if(hSession->connection.error) {
lib3270_free(hSession->connection.error);
hSession->connection.error = NULL;
}
// Format body.
va_list args;
va_start(args, fmt);
lib3270_autoptr(char) body = lib3270_vsprintf(fmt, args);
va_end(args);
hSession->connection.error = lib3270_malloc(sizeof(LIB3270_POPUP) + strlen(summary) + strlen(body) + 3);
// Set type.
hSession->connection.error->type = LIB3270_NOTIFY_ERROR;
// Copy summary
hSession->connection.error->summary = (char *) (hSession->connection.error + 1);
strcpy((char *) hSession->connection.error->summary,summary);
// Copy body.
hSession->connection.error->body = hSession->connection.error->summary + strlen(hSession->connection.error->summary) + 1;
strcpy((char *) hSession->connection.error->body,body);
}