From 8b3aa75c5407d4917c6c54484f07c5472894def5 Mon Sep 17 00:00:00 2001 From: Jan Lipovský Date: Thu, 30 Dec 2010 10:59:31 +0100 Subject: ncpmount wrapper --- dialogs.c | 34 +++++++++------ dialogs.h | 1 + globals.c | 137 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- globals.h | 5 +-- gncpmount.c | 124 +++++++++++++++++++++++++++++------------------------ ncpwrapper.c | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ncpwrapper.h | 8 ++++ 7 files changed, 374 insertions(+), 73 deletions(-) create mode 100644 ncpwrapper.c create mode 100644 ncpwrapper.h diff --git a/dialogs.c b/dialogs.c index 2f75057..cfc403e 100644 --- a/dialogs.c +++ b/dialogs.c @@ -280,22 +280,18 @@ void options_dialog () row++; options.check_C = gtk_check_button_new_with_label ("Don't convet password to uppercase [-C]"); - /*g_signal_connect (G_OBJECT (chbox), "toggled", G_CALLBACK (passwd_uppercase), (gpointer) chbox);*/ gtk_table_attach_defaults (GTK_TABLE (table), options.check_C, 0, 2, row, row+1); row++; options.check_m = gtk_check_button_new_with_label ("Allow multiple logins to server [-m]"); - /*g_signal_connect (G_OBJECT (chbox), "toggled", G_CALLBACK (passwd_uppercase), (gpointer) chbox);*/ gtk_table_attach_defaults (GTK_TABLE (table), options.check_m, 0, 2, row, row+1); row++; options.check_s = gtk_check_button_new_with_label ("Enable renaming/deletion of read-only files [-s]"); - /*g_signal_connect (G_OBJECT (chbox), "toggled", G_CALLBACK (passwd_uppercase), (gpointer) chbox);*/ gtk_table_attach_defaults (GTK_TABLE (table), options.check_s, 0, 2, row, row+1); row++; options.check_b = gtk_check_button_new_with_label ("Force bindery login to NDS servers [-b]"); - /*g_signal_connect (G_OBJECT (chbox), "toggled", G_CALLBACK (passwd_uppercase), (gpointer) chbox);*/ gtk_table_attach_defaults (GTK_TABLE (table), options.check_b, 0, 2, row, row+1); row++; @@ -320,14 +316,26 @@ void options_dialog () -#if 0 - -static void passwd_uppercase (GtkWidget *wid, GtkWidget *win) +/** +* Show info, error or warning message +*/ +void +show_message (GtkMessageType type, const gchar *format, gchar *msgtxt, const gchar *format_sec, gchar *msgtxt_sec) { - /* Nastavit -C parametr */ + GtkWidget *dialog; + + dialog = gtk_message_dialog_new ( GTK_WINDOW (gui.win), + GTK_DIALOG_DESTROY_WITH_PARENT, + type, + GTK_BUTTONS_OK, + format, + msgtxt); + + gtk_window_set_title (GTK_WINDOW (dialog), GUI_TITLE ); + + gtk_message_dialog_format_secondary_text ( GTK_MESSAGE_DIALOG (dialog), + format_sec, + msgtxt_sec); + gtk_dialog_run ( GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); } - - - - -#endif diff --git a/dialogs.h b/dialogs.h index e7543cf..5167b8c 100644 --- a/dialogs.h +++ b/dialogs.h @@ -6,6 +6,7 @@ /** Shows options dialog */ void options_dialog (); +void show_message (GtkMessageType type, const gchar *format, gchar *msgtxt, const gchar *format_sec, gchar *msgtxt_sec); #endif diff --git a/globals.c b/globals.c index cbe5897..d8044c2 100644 --- a/globals.c +++ b/globals.c @@ -6,10 +6,14 @@ void set_parameter_text (GtkWidget *entry, gchar **param) { gchar *tmp; - tmp = g_strdup(gtk_entry_get_text (GTK_ENTRY(entry))); g_strstrip(tmp); + if(!g_strcmp0(tmp,"")) + { + g_free(tmp); + tmp = NULL; + } g_free(*param); *param = g_strdup(tmp); @@ -25,3 +29,134 @@ void set_parameter_bool (GtkWidget *checkbox, gboolean *param) *param = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (checkbox)); } + + + +gchar *get_ncpmount_command () +{ + GString *command; + gchar *ret; + + command = g_string_new("ncpmount"); + + if(cmd_params.username != NULL) + { + command = g_string_append (command, " -U "); + command = g_string_append (command, cmd_params.username); + } + + if(cmd_params.password != NULL && !cmd_params.n) + { + command = g_string_append (command, " -P "); + command = g_string_append (command, cmd_params.password); + } + + if(cmd_params.server != NULL) + { + command = g_string_append (command, " -S "); + command = g_string_append (command, cmd_params.server); + } + + if(cmd_params.dns_name != NULL) + { + command = g_string_append (command, " -A "); + command = g_string_append (command, cmd_params.dns_name); + } + + if(cmd_params.volume != NULL) + { + command = g_string_append (command, " -V "); + command = g_string_append (command, cmd_params.volume); + } + + if(cmd_params.uid != NULL) + { + command = g_string_append (command, " -u "); + command = g_string_append (command, cmd_params.uid); + } + + if(cmd_params.gid != NULL) + { + command = g_string_append (command, " -g "); + command = g_string_append (command, cmd_params.gid); + } + + if(cmd_params.fmode != NULL) + { + command = g_string_append (command, " -f "); + command = g_string_append (command, cmd_params.fmode); + } + + if(cmd_params.dmode != NULL) + { + command = g_string_append (command, " -d "); + command = g_string_append (command, cmd_params.dmode); + } + + if(cmd_params.time_out != NULL) + { + command = g_string_append (command, " -t "); + command = g_string_append (command, cmd_params.time_out); + } + + if(cmd_params.retry_count != NULL) + { + command = g_string_append (command, " -r "); + command = g_string_append (command, cmd_params.retry_count); + } + + if(cmd_params.level != NULL) + { + command = g_string_append (command, " -i "); + command = g_string_append (command, cmd_params.level); + } + + if(cmd_params.charset != NULL) + { + command = g_string_append (command, " -y "); + command = g_string_append (command, cmd_params.charset); + } + + if(cmd_params.codepage != NULL) + { + command = g_string_append (command, " -p "); + command = g_string_append (command, cmd_params.codepage); + } + + if(cmd_params.C) + { + command = g_string_append (command, " -C "); + } + + if(cmd_params.n) + { + command = g_string_append (command, " -n "); + } + + if(cmd_params.s) + { + command = g_string_append (command, " -s "); + } + + if(cmd_params.b) + { + command = g_string_append (command, " -b "); + } + + if(cmd_params.m) + { + command = g_string_append (command, " -m "); + } + + + command = g_string_append (command, " "); + + /* Mount point on the end */ + if(cmd_params.mount_point != NULL) + command = g_string_append (command, cmd_params.mount_point); + + ret = command->str; + g_string_free(command, FALSE); + + return ret; +} diff --git a/globals.h b/globals.h index 95e48d8..ef8ed67 100644 --- a/globals.h +++ b/globals.h @@ -107,9 +107,6 @@ typedef struct params TParameters cmd_params; -/** Sets given parameter whith given value */ -/*void init_parameters (const gchar *value, gchar **param);*/ - /** Sets given parameter whith given value */ void set_parameter_text (GtkWidget *entry, gchar **param); @@ -118,6 +115,8 @@ void set_parameter_text (GtkWidget *entry, gchar **param); void set_parameter_bool (GtkWidget *checkbox, gboolean *param); +/** Return ncpmount command*/ +gchar * get_ncpmount_command (); #endif diff --git a/gncpmount.c b/gncpmount.c index 2a61dae..bd859ba 100644 --- a/gncpmount.c +++ b/gncpmount.c @@ -1,38 +1,56 @@ #include #include +#include "ncpwrapper.h" #include "dialogs.h" #include "globals.h" #define WIDTH 400 #define HEIGHT -1 +/** +* Load savet options to GUI +*/ +static void +set_gui_from_cmdparams () +{ + if(cmd_params.username != NULL) + gtk_entry_set_text(GTK_ENTRY(gui.entry_username), cmd_params.username); + if(cmd_params.password != NULL) + gtk_entry_set_text(GTK_ENTRY(gui.entry_password), cmd_params.password); + + if(cmd_params.server != NULL) + gtk_entry_set_text(GTK_ENTRY(gui.entry_server), cmd_params.server); + + if(cmd_params.dns_name != NULL) + gtk_entry_set_text(GTK_ENTRY(gui.entry_dns_server), cmd_params.dns_name); + + if(cmd_params.mount_point != NULL) + gtk_entry_set_text(GTK_ENTRY(gui.entry_mount_point), cmd_params.mount_point); + + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (gui.check_n), cmd_params.n); +} /** -* Show info, error or warning message +* Save options set in GUI by user to cmd_params */ -void -show_message (GtkMessageType type, const gchar *format, gchar *msgtxt, const gchar *format_sec, gchar *msgtxt_sec) +static void +save_gui_to_cmdparams() { - GtkWidget *dialog; - - dialog = gtk_message_dialog_new ( GTK_WINDOW (gui.win), - GTK_DIALOG_DESTROY_WITH_PARENT, - type, - GTK_BUTTONS_OK, - format, - msgtxt); - - gtk_message_dialog_format_secondary_text ( GTK_MESSAGE_DIALOG (dialog), - format_sec, - msgtxt_sec); - gtk_dialog_run ( GTK_DIALOG (dialog)); - gtk_widget_destroy (dialog); + set_parameter_text ((gui.entry_dns_server), &cmd_params.dns_name); + set_parameter_text ((gui.entry_mount_point), &cmd_params.mount_point); + set_parameter_text ((gui.entry_password), &cmd_params.password); + set_parameter_text ((gui.entry_server), &cmd_params.server); + set_parameter_text ((gui.entry_username), &cmd_params.username); + + /* Checkboxes */ + set_parameter_bool ((gui.check_n), &cmd_params.n); } + /** * Create Open folder dialog and let user select folder * for mount point @@ -59,7 +77,21 @@ open_folder (GtkWidget *wid, GtkWidget *win) } +/** +* Call ncpmount command +*/ +static void +call_ncpmount (GtkWidget *wid, GtkWidget *win) +{ + /* TODO - kontrola vyplneni udaju */ + save_gui_to_cmdparams(); + run_ncpmount(); +} + +/** +* Set if -P (password) is used or not +*/ static void passwd_usage (GtkWidget *wid, GtkWidget *win) { @@ -82,40 +114,18 @@ passwd_usage (GtkWidget *wid, GtkWidget *win) } - +/** +* Open Options dialog +*/ static void run_options_dialog ( GtkWidget *w, gpointer data ) { - options_dialog(&gui); + options_dialog(); } -static void -set_gui_from_cmdparams () -{ - if(cmd_params.username != NULL) - gtk_entry_set_text(GTK_ENTRY(gui.entry_username), cmd_params.username); - - if(cmd_params.password != NULL) - gtk_entry_set_text(GTK_ENTRY(gui.entry_password), cmd_params.password); - - if(cmd_params.server != NULL) - gtk_entry_set_text(GTK_ENTRY(gui.entry_server), cmd_params.server); - - if(cmd_params.dns_name != NULL) - gtk_entry_set_text(GTK_ENTRY(gui.entry_dns_server), cmd_params.dns_name); - - if(cmd_params.mount_point != NULL) - gtk_entry_set_text(GTK_ENTRY(gui.entry_mount_point), cmd_params.mount_point); - - gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (gui.check_n), cmd_params.n); -} - - - - -/* Menu, an array of GtkItemFactoryEntry structures that defines each menu item */ +/** Menu, an array of GtkItemFactoryEntry structures that defines each menu item */ static GtkItemFactoryEntry menu_items[] = { { "/_File", NULL, NULL, 0, "" }, { "/File/_New", "N", NULL, 0, "", GTK_STOCK_NEW }, @@ -129,11 +139,13 @@ static GtkItemFactoryEntry menu_items[] = { { "/_Help/About", NULL, NULL, 0, "" }, }; - +/** Menu items count */ static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]); -/* Returns a menubar widget made from the above menu */ +/** +* Returns a menubar widget made from the above menu +*/ static GtkWidget * get_menubar_menu( GtkWidget *window ) { @@ -223,25 +235,25 @@ int main (int argc, char *argv[]) gtk_container_add(GTK_CONTAINER(halign), label); gtk_table_attach(GTK_TABLE(gui.table), halign, 0, 1, 0, 1, GTK_FILL , GTK_FILL | GTK_EXPAND, 4, 0); + + gui.entry_username = gtk_entry_new (); + gtk_table_attach_defaults (GTK_TABLE (gui.table), gui.entry_username, 1, 2, 0, 1); + halign = gtk_alignment_new(0, 0, 0, 1); label = gtk_label_new ("Password [-P]:"); gtk_container_add(GTK_CONTAINER(halign), label); gtk_table_attach(GTK_TABLE(gui.table), halign, 0, 1, 1, 2, GTK_FILL, GTK_FILL | GTK_EXPAND, 4, 0); - /* Chcekbox */ - gui.check_n = gtk_check_button_new_with_label ("Do not use any password [-n]"); - g_signal_connect (G_OBJECT (gui.check_n), "toggled", G_CALLBACK (passwd_usage), NULL); - gtk_table_attach_defaults (GTK_TABLE (gui.table), gui.check_n, 1, 2, 2, 3); - - - /* Entry */ - gui.entry_username = gtk_entry_new (); - gtk_table_attach_defaults (GTK_TABLE (gui.table), gui.entry_username, 1, 2, 0, 1); gui.entry_password = gtk_entry_new (); gtk_entry_set_visibility (GTK_ENTRY(gui.entry_password), FALSE); gtk_table_attach_defaults (GTK_TABLE (gui.table), gui.entry_password, 1, 2, 1, 2); + /* Chcekbox */ + gui.check_n = gtk_check_button_new_with_label ("Do not use any password [-n]"); + g_signal_connect (G_OBJECT (gui.check_n), "toggled", G_CALLBACK (passwd_usage), NULL); + gtk_table_attach_defaults (GTK_TABLE (gui.table), gui.check_n, 1, 2, 2, 3); + /* Page SERVER */ @@ -294,7 +306,7 @@ int main (int argc, char *argv[]) /* Buttons */ gui.btn_mount = gtk_button_new_with_label ("Mount"); - g_signal_connect (gui.btn_mount, "clicked", gtk_main_quit, NULL); + g_signal_connect (gui.btn_mount, "clicked", G_CALLBACK(call_ncpmount), NULL); gtk_box_pack_start (GTK_BOX (gui.btnbox), gui.btn_mount, TRUE, TRUE, 0); gui.btn_close = gtk_button_new_from_stock (GTK_STOCK_CLOSE); diff --git a/ncpwrapper.c b/ncpwrapper.c new file mode 100644 index 0000000..cdec0c5 --- /dev/null +++ b/ncpwrapper.c @@ -0,0 +1,138 @@ +#include + +#include "globals.h" +#include "ncpwrapper.h" +#include "dialogs.h" + + +static void +cb_child_watch( GPid pid, + gint status) +{ + /* Close pid */ + g_spawn_close_pid( pid ); +} + + +static gboolean +cb_out_watch( GIOChannel *channel, + GIOCondition cond) +{ + GString *string; + gchar *tmp; + gsize size; + + if( cond == G_IO_HUP ) + { + 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) + string = g_string_append (string, tmp); + + g_free( tmp ); + } + + + show_message( GTK_MESSAGE_INFO,"%s", "ncpmount output","%s",string->str); + g_string_free(string, TRUE); + + return( TRUE ); +} + + +static gboolean +cb_err_watch( GIOChannel *channel, + GIOCondition cond) +{ + GString *string; + gchar *tmp; + gsize size; + + if( cond == G_IO_HUP ) + { + g_io_channel_unref( channel ); + 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) + string = g_string_append (string, tmp); + + g_free( tmp ); + } + + + show_message( GTK_MESSAGE_ERROR,"%s", "ncpmount error","%s",string->str); + g_string_free(string, TRUE); + + return( TRUE ); +} + +/* + TODO - jak odchytit uspesne mountnuti??? +*/ + +void run_ncpmount() +{ + + GIOChannel *out_ch, *err_ch; + GError *gerror = NULL; + GPid pid; + gint out, err; + gboolean ret; + const gchar *command = get_ncpmount_command(); + + gchar **argv = g_strsplit(command, " ", 0); + + puts (command); + + g_free(argv[0]); + argv[0] = g_strdup("/usr/bin/ncpmount"); + + /* gchar *argv[] = {"/usr/bin/ncpmount", "-v", NULL};*/ + + /* 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" ); + 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); + + /* Create channels that will be used to read data from pipes. */ +#ifdef G_OS_WIN32 + out_ch = g_io_channel_win32_new_fd( out ); + err_ch = g_io_channel_win32_new_fd( err ); +#else + out_ch = g_io_channel_unix_new( out ); + err_ch = g_io_channel_unix_new( err ); +#endif + + /* Add watches to channels */ + 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, NULL ); +} diff --git a/ncpwrapper.h b/ncpwrapper.h new file mode 100644 index 0000000..822d8fe --- /dev/null +++ b/ncpwrapper.h @@ -0,0 +1,8 @@ +#ifndef GNCPMOUNT_NCPWRAPPER_H +#define GNCPMOUNT_NCPWRAPPER_H + +#include "globals.h" + +void run_ncpmount(); + +#endif -- cgit