callbacks.c 12.2 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
/*
 * "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)
 *
 */

 #include <config.h>

 #ifdef _WIN32
	#include <winsock2.h>
 #endif // _WIN32

 #include <terminal.h>
 #include <internals.h>
 #include <v3270/dialogs.h>

 #include <gtk/gtk.h>
 #include <libintl.h>
 #include <glib/gi18n.h>

 #include <lib3270.h>
 #include <lib3270/session.h>
 #include <lib3270/log.h>
 #include <errno.h>

/**
 * SECTION:	v3270
 * @title:	Virtual 3270 widget
 * @short_description:	The virtual 3270 terminal widget.
 *
 * Manages lib3270 callbacks.
 *
 */

/*--[ Widget definition ]----------------------------------------------------------------------------*/

/*--[ Implement ]------------------------------------------------------------------------------------*/

static void set_timer(H3270 *session, unsigned char on)
{
	GtkWidget *widget = GTK_WIDGET(lib3270_get_user_data(session));

	if(on)
		v3270_start_timer(widget);
	else
		v3270_stop_timer(widget);

}

static void update_toggle(H3270 *session, LIB3270_TOGGLE_ID id, unsigned char value, G_GNUC_UNUSED LIB3270_TOGGLE_TYPE reason, const char *name)
{
	v3270_update_toggle((GtkWidget *) lib3270_get_user_data(session), id, value, name);
}

static gboolean bg_update_message(H3270 *session)
{
	void *widget = lib3270_get_user_data(session);
 	// trace("-----A %s %p",__FUNCTION__, lib3270_get_user_data(session));

	g_signal_emit(
		GTK_WIDGET(widget),
		v3270_widget_signal[V3270_SIGNAL_MESSAGE_CHANGED],
		0,
		(gint) lib3270_get_program_message(session)
	);

 	//trace("-----B %s %p",__FUNCTION__, lib3270_get_user_data(session));

 	return FALSE;
}

static void update_message(H3270 *session, G_GNUC_UNUSED LIB3270_MESSAGE id)
{
	g_idle_add((GSourceFunc) bg_update_message, session);
}

static void update_luname(H3270 *session, const char G_GNUC_UNUSED(*name))
{
	g_idle_add((GSourceFunc) v3270_update_luname, lib3270_get_user_data(session));
}

static gboolean	v3270_update_url(v3270 *terminal)
{
	debug("url=%s",v3270_get_url(GTK_WIDGET(terminal)));
	g_object_notify_by_pspec(G_OBJECT(terminal), GTK_V3270_GET_CLASS(terminal)->properties.url);
	return FALSE;
}

static void update_url(H3270 *session, const char G_GNUC_UNUSED(*name))
{
	g_idle_add((GSourceFunc) v3270_update_url, lib3270_get_user_data(session));
}

struct select_cursor_data
{
	H3270			* hSession;
	LIB3270_POINTER   id;
};

static gboolean bg_select_cursor(struct select_cursor_data *data)
{
	GtkWidget *widget = GTK_WIDGET(lib3270_get_user_data(data->hSession));

#if GTK_CHECK_VERSION(2,20,0)
	if(gtk_widget_get_realized(widget) && gtk_widget_get_has_window(widget))
#else
	if(GTK_WIDGET_REALIZED(widget) && widget->window)
#endif // GTK(2,20)
	{
		GTK_V3270(widget)->pointer_id = data->id;
		v3270_update_mouse_pointer(widget);
	}

	return FALSE;
}

static void select_cursor(H3270 *session, LIB3270_POINTER id)
{
	struct select_cursor_data *data = g_new0(struct select_cursor_data,1);
	data->hSession = session;
	data->id = id;
	g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,(GSourceFunc) bg_select_cursor, data, g_free);
}

static void ctlr_done(H3270 *session)
{
	GtkWidget *widget = GTK_WIDGET(lib3270_get_user_data(session));

	if(gtk_widget_get_realized(widget) && gtk_widget_get_has_window(widget))
	{
		v3270_update_mouse_pointer(widget);
	}

}

static void update_connect(H3270 *session, unsigned char connected)
{
	v3270 *widget = GTK_V3270(lib3270_get_user_data(session));

	if(connected)
	{
		widget->cursor.show |= 2;
		g_signal_emit(GTK_WIDGET(widget), v3270_widget_signal[V3270_SIGNAL_CONNECTED], 0, lib3270_get_url(session));
	}
	else
	{
		widget->cursor.show &= ~2;
		g_signal_emit(GTK_WIDGET(widget), v3270_widget_signal[V3270_SIGNAL_DISCONNECTED], 0);
	}

	g_object_notify_by_pspec(G_OBJECT(widget), GTK_V3270_GET_CLASS(widget)->properties.online);

	widget->activity.timestamp = time(0);

	gtk_widget_queue_draw(GTK_WIDGET(widget));
}

static void update_screen_size(H3270 *session, G_GNUC_UNUSED unsigned short rows, G_GNUC_UNUSED unsigned short cols)
{
	debug("%s",__FUNCTION__);
	GtkWidget * widget = GTK_WIDGET(lib3270_get_user_data(session));

	v3270_reconfigure(GTK_V3270(widget));
	gtk_widget_queue_draw(widget);

}

static void update_model(H3270 *session, const char *name, int model, G_GNUC_UNUSED int rows, G_GNUC_UNUSED int cols)
{
	GtkWidget * widget = GTK_WIDGET(lib3270_get_user_data(session));
	g_object_notify_by_pspec(G_OBJECT(lib3270_get_user_data(session)), GTK_V3270_GET_CLASS(widget)->properties.model);
	g_signal_emit(widget,v3270_widget_signal[V3270_SIGNAL_MODEL_CHANGED], 0, (guint) model, name);
}

static void changed(H3270 *session, int offset, int len)
{
	GtkWidget 		* widget	= lib3270_get_user_data(session);
	GtkAccessible	* obj		= GTK_V3270(widget)->accessible;

	if(obj)
	{
		// Get new text, notify atk
		gsize	  bytes_written	= 0;
		char	* text 			= lib3270_get_string_at_address(session,offset,len,'\n');

		if(text)
		{
			GError	* error		= NULL;
			gchar	* utfchar	= g_convert_with_fallback(	text,
																-1,
																"UTF-8",
																lib3270_get_display_charset(session),
																" ",
																NULL,
																&bytes_written,
																&error );

			lib3270_free(text);

			if(error)
			{
				g_warning("%s failed: %s",__FUNCTION__,error->message);
				g_error_free(error);
			}

			if(utfchar)
			{
				g_signal_emit_by_name(obj, "text-insert", offset, bytes_written, utfchar);
				g_free(utfchar);
			}

		}
	}

#ifdef WIN32
	gtk_widget_queue_draw(widget);
#endif // WIN32

	g_signal_emit(GTK_WIDGET(widget),v3270_widget_signal[V3270_SIGNAL_CHANGED], 0, (guint) offset, (guint) len);
}

static void set_selection(H3270 *session, unsigned char status)
{
	GtkWidget * widget = GTK_WIDGET(lib3270_get_user_data(session));

	g_object_notify_by_pspec(G_OBJECT(widget), GTK_V3270_GET_CLASS(widget)->properties.selection);
	g_signal_emit(widget,v3270_widget_signal[V3270_SIGNAL_SELECTING], 0, status ? TRUE : FALSE);

}

static void update_selection(H3270 *session, G_GNUC_UNUSED int start, G_GNUC_UNUSED int end)
{
	// Selected region changed
	GtkWidget		* widget	= GTK_WIDGET(lib3270_get_user_data(session));
	GtkAccessible	* atk_obj	= GTK_V3270(widget)->accessible;

	if(atk_obj)
		g_signal_emit_by_name(atk_obj,"text-selection-changed");

}

static void message(H3270 *session, LIB3270_NOTIFY id , const char *title, const char *message, const char *text)
{
	g_signal_emit(	GTK_WIDGET(lib3270_get_user_data(session)), v3270_widget_signal[V3270_SIGNAL_MESSAGE], 0,
							(int) id,
							(gchar *) title,
							(gchar *) message,
							(gchar *) text );

}


static int print(H3270 *session, LIB3270_CONTENT_OPTION mode)
{
	return v3270_print_dialog(GTK_WIDGET(lib3270_get_user_data(session)), mode, NULL);
}

static int save(H3270 *session, LIB3270_CONTENT_OPTION mode, const char *filename)
{
	GtkWidget * widget = GTK_WIDGET(lib3270_get_user_data(session));

	if(!GTK_IS_V3270(widget))
		return errno = EINVAL;

	GtkWidget *dialog = v3270_save_dialog_new(widget,mode,filename);
	gtk_widget_show_all(dialog);
	v3270_save_dialog_run(dialog);
	gtk_widget_destroy(dialog);

	return 0;
}

static int load(H3270 *session, const char *filename)
{
	GtkWidget * widget = GTK_WIDGET(lib3270_get_user_data(session));

	if(!GTK_IS_V3270(widget))
		return errno = EINVAL;

	GtkWidget *dialog = v3270_load_dialog_new(widget,filename);
	gtk_widget_show_all(dialog);
	v3270_load_dialog_run(dialog);
	gtk_widget_destroy(dialog);

	return 0;
}

static void popup_handler(H3270 *session, LIB3270_NOTIFY type, const char *title, const char *msg, const char *fmt, va_list args)
{
 	GtkWidget *terminal = (GtkWidget *) lib3270_get_user_data(session);

 	if(terminal && GTK_IS_V3270(terminal))
	{

		if(fmt)
		{
			gchar *text = g_strdup_vprintf(fmt,args);
			v3270_popup_message(GTK_WIDGET(terminal),type,title,msg,text);
			g_free(text);
		}
		else
		{
			v3270_popup_message(GTK_WIDGET(terminal),type,title,msg,NULL);
		}

 	}

 }

 static gboolean bg_update_ssl(H3270 *session)
 {
 	v3270_blink_ssl(GTK_V3270(lib3270_get_user_data(session)));

	if(lib3270_get_ssl_state(session) == LIB3270_SSL_NEGOTIATING)
		v3270_start_blinking(GTK_WIDGET(lib3270_get_user_data(session)));

	return FALSE;
 }

 static void update_ssl(H3270 *session, G_GNUC_UNUSED LIB3270_SSL_STATE state)
 {
	g_idle_add((GSourceFunc) bg_update_ssl, session);
 }

 struct update_oia_data
 {
 	H3270 *session;
	LIB3270_FLAG id;
	unsigned char on;
 };

 static gboolean bg_update_oia(struct update_oia_data *data)
 {
  	v3270_update_oia(GTK_V3270(lib3270_get_user_data(data->session)), data->id, data->on);
 	return FALSE;
 }

 static void update_oia(H3270 *session, LIB3270_FLAG id, unsigned char on)
 {
	struct update_oia_data *data = g_new0(struct update_oia_data,1);
	data->session = session;
	data->id = id;
	data->on = on;
	g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,(GSourceFunc) bg_update_oia, data, g_free);
 }

 static int popup_ssl_error(H3270 *session, int rc, const char *title, const char *summary, const char *body)
 {
 	GtkWidget *terminal = (GtkWidget *) lib3270_get_user_data(session);

 	debug("%s.summary=\"%s\"",__FUNCTION__,summary);
 	debug("%s.body=\"%s\"",__FUNCTION__,body ? body : "undefined");

 	if(terminal && GTK_IS_V3270(terminal))
	{
		GtkWidget * dialog = gtk_message_dialog_new_with_markup(
								GTK_WINDOW(gtk_widget_get_toplevel(terminal)),
								GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
								GTK_MESSAGE_WARNING,
								GTK_BUTTONS_NONE,
								"%s",
								summary
						);

		gtk_window_set_title(GTK_WINDOW(dialog), title);

		if(body && *body)
		{
			gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),"%s",body);
		}
		else if(rc)
		{
			gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),_( "The error code was %d"),rc);
		}

		gtk_dialog_add_buttons(
			GTK_DIALOG(dialog),
			_("Cancel"), GTK_RESPONSE_CANCEL,
			_("Continue"), GTK_RESPONSE_APPLY,
			NULL
		);

		gtk_dialog_set_default_response(GTK_DIALOG (dialog),GTK_RESPONSE_CANCEL);

		gtk_widget_show_all(dialog);
		int rc = gtk_dialog_run(GTK_DIALOG(dialog));
		gtk_widget_destroy(dialog);

		if(rc == GTK_RESPONSE_APPLY)
			return 0;

 	}

	return -1;
 }

 void v3270_install_callbacks(v3270 *widget)
 {
	struct lib3270_session_callbacks *cbk;

	lib3270_set_popup_handler(widget->host, popup_handler);

	cbk = lib3270_get_session_callbacks(widget->host,sizeof(struct lib3270_session_callbacks));
	if(!cbk)
	{
		g_error( _( "Invalid callback table, possible version mismatch in lib3270") );
		return;
	}

	cbk->update				= v3270_update_char;
	cbk->changed			= changed;
	cbk->set_timer 			= set_timer;

	cbk->set_selection		= set_selection;
	cbk->update_selection	= update_selection;

	cbk->update_luname		= update_luname;
	cbk->update_url			= update_url;

	cbk->configure			= update_screen_size;
	cbk->update_status 		= update_message;
	cbk->update_cursor 		= v3270_update_cursor;
	cbk->update_toggle 		= update_toggle;
	cbk->update_oia			= update_oia;
	cbk->cursor				= select_cursor;
	cbk->update_connect		= update_connect;
	cbk->update_model		= update_model;
	cbk->changed			= changed;
	cbk->ctlr_done			= ctlr_done;
	cbk->message			= message;
	cbk->update_ssl			= update_ssl;
	cbk->print				= print;
	cbk->save				= save;
	cbk->load				= load;
	cbk->popup_ssl_error	= popup_ssl_error;

}