From 1579a7769f997fcf9e3f9dc51e113f4d3604e84d Mon Sep 17 00:00:00 2001 From: Jan Lipovský Date: Sat, 1 Jan 2011 16:49:19 +0100 Subject: GTK+ 2.14; Load and save settings; Gui dialogs... --- ncpwrapper.c | 178 ++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 139 insertions(+), 39 deletions(-) (limited to 'ncpwrapper.c') diff --git a/ncpwrapper.c b/ncpwrapper.c index ac24f91..9d8d889 100644 --- a/ncpwrapper.c +++ b/ncpwrapper.c @@ -1,34 +1,126 @@ -#include - #include "ncpwrapper.h" -gboolean mounted = TRUE; +/** TData structure */ +typedef struct struc_data +{ + gchar *m1format; + gchar *m1; + gchar *m2format; + gchar *m2; + + gint count; + + GtkMessageType msgtype; + gboolean show; + gboolean stdout_end; + gboolean stderr_end; + gint timeout; + +} TData; + +/** data structure to comunicate between callbacks */ +TData data; + + + +/** +* Fill data structure +*/ +void fill_data (TData *data, + const gchar *m1format, + const gchar *m1, + const gchar *m2format, + const gchar *m2, + GtkMessageType type, + const gboolean show) +{ + if(data->m1 != NULL) + g_free(data->m1); + + if(data->m1format != NULL) + g_free(data->m1format); + + if(data->m2 != NULL) + g_free(data->m2); + + if(data->m2format != NULL) + g_free(data->m2format); + + data->m1 = g_strdup(m1); + data->m1format = g_strdup(m1format); + data->m2 = g_strdup(m2); + data->m2format= g_strdup(m2format); + + data->msgtype = type; + data->count++; + + data->show = show; +} + + + + +/** +* Catch termination of the process +*/ static void cb_child_watch( GPid pid, - gint status) + gint status, + TData *data) { /* Close pid */ g_spawn_close_pid( pid ); } + +/** +* Timeout loop, show messages +*/ +static gboolean +cb_timeout( TData *data ) +{ + if(data->show) + { + data->show = FALSE; + show_message( data->msgtype, data->m1format, data->m1, data->m2format, data->m2); + } + + if(data->stderr_end && data->stdout_end) + { + if(data->count == 0) + show_message(GTK_MESSAGE_INFO, "Mounting succesfull", "","Mounted to \"%s\" directory.",cmd_params.mount_point); + + g_source_remove(data->timeout); + } + + return( TRUE ); +} + + + +/** +* Catch messages on stdout +*/ static gboolean cb_out_watch( GIOChannel *channel, - GIOCondition cond) + GIOCondition cond, + TData *data) { GString *string; gchar *tmp; gsize size; + if( cond == G_IO_HUP ) { + data->stdout_end = TRUE; g_io_channel_unref( channel ); return( FALSE ); } string = g_string_new(""); - while (g_io_channel_read_line( channel, &tmp, &size, NULL, NULL) == G_IO_STATUS_NORMAL) { if(tmp != NULL) @@ -37,19 +129,21 @@ cb_out_watch( GIOChannel *channel, g_free( tmp ); } - /* Receved some message - not mounted */ - mounted = FALSE; - - show_message( GTK_MESSAGE_INFO,"%s", "ncpmount output","%s",string->str); + fill_data (data,"%s", "ncpmount output","%s",string->str, GTK_MESSAGE_INFO, TRUE); g_string_free(string, TRUE); return( TRUE ); } + +/** +* Catch messages on stdout +*/ static gboolean cb_err_watch( GIOChannel *channel, - GIOCondition cond) + GIOCondition cond, + TData *data) { GString *string; gchar *tmp; @@ -57,21 +151,12 @@ cb_err_watch( GIOChannel *channel, if( cond == G_IO_HUP ) { + data->stderr_end = TRUE; g_io_channel_unref( channel ); - /* hangup signal - if mounted then show message */ - if(mounted) - show_message( GTK_MESSAGE_INFO,"Mounting succesfull", "","Mounted to \"%s\" directory.",cmd_params.mount_point); return( FALSE ); } -/* - g_io_channel_read_line( channel, &string, &size, NULL, NULL ); - show_message( GTK_MESSAGE_ERROR,"ERROR: %s", string,"%s",string); - puts ("err"); - puts (string); - g_free( string );*/ string = g_string_new(""); - while (g_io_channel_read_line( channel, &tmp, &size, NULL, NULL) == G_IO_STATUS_NORMAL) { if(tmp != NULL) @@ -80,50 +165,57 @@ cb_err_watch( GIOChannel *channel, g_free( tmp ); } - - /* Receved some message - not mounted */ - mounted = FALSE; - - show_message( GTK_MESSAGE_ERROR,"%s", "ncpmount error","%s",string->str); + fill_data (data, "%s", "ncpmount error","%s",string->str, GTK_MESSAGE_ERROR, TRUE); g_string_free(string, TRUE); return( TRUE ); } + + +/** +* Run ncpmount and adds wotches to stdout and stderr +*/ void run_ncpmount() { - GIOChannel *out_ch, *err_ch; GError *gerror = NULL; GPid pid; - gint out, err; + gint out, err, argc; gboolean ret; - gchar *command = get_ncpmount_command(NULL); - gchar **argv = g_strsplit(command, " ", 0); + gchar *command = get_ncpmount_command(TRUE, NULL); + gchar **argv; + /*gchar **argv = g_strsplit(command, " ", 0);*/ - puts (command); + if(!g_shell_parse_argv (command,&argc,&argv,&gerror)) + { + g_warning(gerror->message); + show_message( GTK_MESSAGE_ERROR,"ERROR: %s", "Shell parse argv fail","%s",gerror->message); + g_error_free(gerror); + return; + } + + /*puts (command);*/ g_free(argv[0]); argv[0] = g_strdup("/usr/bin/ncpmount"); - /* gchar *argv[] = {"/usr/bin/ncpmount", "-v", NULL};*/ + /* gchar *argv[] = {"/usr/bin/ncpmount", "-v", NULL};*/ - mounted = TRUE; /* Spawn child process */ ret = g_spawn_async_with_pipes( NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL, &out, &err, &gerror); if( !ret ) { - g_warning( "SPAWN FAILED" ); + g_warning(gerror->message); show_message( GTK_MESSAGE_ERROR,"ERROR: %s", "Spawn failed!","%s",gerror->message); g_error_free(gerror); return; } - /* Add watch function to catch termination of the process. This function - * will clean any remnants of process. */ - g_child_watch_add( pid, (GChildWatchFunc)cb_child_watch, NULL); + /* function to catch termination of the process. */ + g_child_watch_add( pid, (GChildWatchFunc)cb_child_watch, &data); /* Create channels that will be used to read data from pipes. */ #ifdef G_OS_WIN32 @@ -135,8 +227,16 @@ void run_ncpmount() #endif /* Add watches to channels */ - g_io_add_watch( err_ch, G_IO_IN | G_IO_HUP, (GIOFunc)cb_err_watch, NULL ); - g_io_add_watch( out_ch, G_IO_IN | G_IO_HUP, (GIOFunc)cb_out_watch, NULL ); + g_io_add_watch( err_ch, G_IO_IN | G_IO_HUP, (GIOFunc)cb_err_watch, &data ); + g_io_add_watch( out_ch, G_IO_IN | G_IO_HUP, (GIOFunc)cb_out_watch, &data ); + + /* Init data structure */ + data.count = 0; + data.stderr_end = FALSE; + data.stdout_end = FALSE; + + /* Add timeout collback */ + data.timeout = g_timeout_add( 100, (GSourceFunc)cb_timeout, &data ); g_strfreev(argv); g_free(command); -- cgit