Commit f9c59e98e598286c244bbea6a5f29e7af7de475c

Authored by perry.werneck@gmail.com
1 parent 8471bd91

Implementando dialogo de transferência de arquivos

src/include/lib3270/filetransfer.h
... ... @@ -74,11 +74,12 @@
74 74 H3270 * host;
75 75 void * widget; /**< File transfer dialog handle */
76 76 FILE * ft_local_file; /**< File descriptor for local file */
  77 + unsigned long length; /**< File length */
  78 +
77 79 LIB3270_FT_STATE state;
78 80  
79 81 void (*complete)(struct _h3270ft *ft, const char *errmsg,unsigned long length,double kbytes_sec,const char *mode);
80   - void (*setlength)(struct _h3270ft *ft, unsigned long length);
81   - void (*update)(struct _h3270ft *ft, unsigned long length, double kbytes_sec);
  82 + void (*update)(struct _h3270ft *ft, unsigned long current, unsigned long length, double kbytes_sec);
82 83 void (*running)(struct _h3270ft *ft, int is_cut);
83 84 void (*aborting)(struct _h3270ft *ft);
84 85 void (*state_changed)(struct _h3270ft *ft, LIB3270_FT_STATE state);
... ... @@ -102,7 +103,8 @@
102 103 * @return Filetransfer handle if ok, NULL if failed
103 104 *
104 105 */
105   - LIB3270_EXPORT H3270FT * lib3270_ft_start(H3270 *session, LIB3270_FT_OPTION flags, const char *local, const char *remote, int lrecl, int blksize, int primspace, int secspace, int dft, const char **msg);
  106 + LIB3270_EXPORT H3270FT * lib3270_ft_new(H3270 *session, LIB3270_FT_OPTION flags, const char *local, const char *remote, int lrecl, int blksize, int primspace, int secspace, int dft, const char **msg);
  107 + LIB3270_EXPORT void lib3270_ft_destroy(H3270FT *ft);
106 108  
107 109 LIB3270_EXPORT int lib3270_ft_cancel(H3270FT *ft, int force);
108 110  
... ...
src/lib3270/api.h
... ... @@ -226,20 +226,21 @@
226 226 LOCAL_EXTERN int CancelFileTransfer(int force);
227 227 // LOCAL_EXTERN enum ft_state GetFileTransferState(void);
228 228  
  229 +/*
229 230 struct filetransfer_callbacks
230 231 {
231 232 unsigned short sz;
232 233  
233 234 void (*begin)(unsigned short flags, const char *local, const char *remote);
234 235 void (*complete)(const char *errmsg,unsigned long length,double kbytes_sec,const char *mode);
235   - void (*setlength)(unsigned long length);
236   - void (*update)(unsigned long length,double kbytes_sec);
  236 + void (*update)(unsigned long length,unsigned long total,double kbytes_sec);
237 237 void (*running)(int is_cut);
238 238 void (*aborting)(void);
239 239  
240 240 };
241 241  
242 242 LOCAL_EXTERN int RegisterFTCallbacks(const struct filetransfer_callbacks *cbk);
  243 +*/
243 244  
244 245 #define PCONNECTED lib3270_pconnected(NULL)
245 246 #define HALF_CONNECTED lib3270_half_connected(NULL)
... ...
src/lib3270/ft.c
... ... @@ -165,12 +165,7 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state);
165 165  
166 166 }
167 167  
168   - static void def_setlength(H3270FT *ft, unsigned long length)
169   - {
170   -
171   - }
172   -
173   - static void def_update(H3270FT *ft, unsigned long length,double kbytes_sec)
  168 + static void def_update(H3270FT *ft, unsigned long current, unsigned long length, double kbytes_sec)
174 169 {
175 170  
176 171 }
... ... @@ -191,7 +186,7 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state);
191 186 }
192 187  
193 188  
194   - LIB3270_EXPORT H3270FT * lib3270_ft_start(H3270 *session, LIB3270_FT_OPTION flags, const char *local, const char *remote, int lrecl, int blksize, int primspace, int secspace, int dft, const char **msg)
  189 + LIB3270_EXPORT H3270FT * lib3270_ft_new(H3270 *session, LIB3270_FT_OPTION flags, const char *local, const char *remote, int lrecl, int blksize, int primspace, int secspace, int dft, const char **msg)
195 190 {
196 191 H3270FT * ftHandle = NULL;
197 192 static const char * rec = "fvu";
... ... @@ -201,6 +196,7 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state);
201 196 unsigned short units = (flags & FT_ALLOCATION_UNITS_MASK) >> 12;
202 197  
203 198 FILE * ft_local_file = NULL;
  199 + unsigned long ft_length = 0L;
204 200  
205 201 char op[4096];
206 202 char buffer[4096];
... ... @@ -215,16 +211,16 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state);
215 211 return NULL;
216 212 }
217 213  
218   - if(ftsession)
  214 + if(session->ft)
219 215 {
220   - *msg = N_( "File transfer is already active" );
  216 + *msg = N_( "File transfer is already active in this session" );
221 217 errno = EBUSY;
222 218 return NULL;
223 219 }
224 220  
225   - if(session->ft)
  221 + if(ftsession)
226 222 {
227   - *msg = N_( "File transfer is already active in this session" );
  223 + *msg = N_( "File transfer is already active" );
228 224 errno = EBUSY;
229 225 return NULL;
230 226 }
... ... @@ -254,7 +250,25 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state);
254 250 cr_flag = ((flags & LIB3270_FT_OPTION_CRLF) != 0);
255 251 remap_flag = ((flags & LIB3270_FT_OPTION_ASCII) != 0);
256 252  
257   - lib3270_write_log(session, "%s file \"%s\"",(flags & LIB3270_FT_OPTION_RECEIVE) ? "Receiving" : "Sending", local);
  253 + if(flags & LIB3270_FT_OPTION_RECEIVE)
  254 + {
  255 + // Receiving file
  256 + lib3270_write_log(session,"ft","Receiving file %s",local);
  257 + }
  258 + else
  259 + {
  260 + // Sending file
  261 + if(fseek(ft_local_file,0L,SEEK_END) < 0)
  262 + {
  263 + *msg = N_( "Can't get file size" );
  264 + return NULL;
  265 + }
  266 +
  267 + ft_length = ftell(ft_local_file);
  268 +
  269 + lib3270_write_log(session,"ft","Sending file %s (%ld bytes)",local,ft_length);
  270 + rewind(ft_local_file);
  271 + }
258 272  
259 273 /* Build the ind$file command */
260 274 snprintf(op,4095,"%s%s%s",
... ... @@ -331,7 +345,7 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state);
331 345 return NULL;
332 346 }
333 347  
334   - trace("Command: \"%s\"",buffer);
  348 + trace_event("Sending FT request:\n%s\n",buffer);
335 349  
336 350 (void) lib3270_emulate_input(NULL, buffer, strlen(buffer), False);
337 351  
... ... @@ -345,9 +359,9 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state);
345 359 ftHandle->sz = sizeof(H3270FT);
346 360 ftHandle->host = session;
347 361 ftHandle->ft_local_file = ft_local_file;
  362 + ftHandle->length = ft_length;
348 363 ftHandle->state = LIB3270_FT_STATE_AWAIT_ACK;
349 364 ftHandle->complete = def_complete;
350   - ftHandle->setlength = def_setlength;
351 365 ftHandle->update = def_update;
352 366 ftHandle->running = def_running;
353 367 ftHandle->aborting = def_aborting;
... ... @@ -390,6 +404,21 @@ void ft_complete(H3270FT *session, const char *errmsg)
390 404  
391 405 session->complete(session,errmsg,ft_length,kbytes_sec,ft_is_cut ? "CUT" : "DFT");
392 406  
  407 +}
  408 +
  409 +LIB3270_EXPORT void lib3270_ft_destroy(H3270FT *session)
  410 +{
  411 + CHECK_FT_HANDLE(session);
  412 +
  413 + if (session->state != LIB3270_FT_STATE_NONE)
  414 + lib3270_ft_cancel(session,1);
  415 +
  416 + if(session->ft_local_file)
  417 + {
  418 + fclose(session->ft_local_file);
  419 + session->ft_local_file = NULL;
  420 + }
  421 +
393 422 if(session == ftsession)
394 423 ftsession = NULL;
395 424  
... ... @@ -417,7 +446,7 @@ void ft_update_length(H3270FT *session)
417 446 (double)(t1.tv_usec - starting_time.tv_usec) / 1.0e6);
418 447 }
419 448  
420   - session->update(session,ft_length,kbytes_sec);
  449 + session->update(session,ft_length,session->length,kbytes_sec);
421 450  
422 451 }
423 452  
... ...
src/pw3270/filetransfer.c
... ... @@ -174,7 +174,6 @@ static void add_file_fields(GObject *action, struct ftdialog *dlg)
174 174 gtk_table_attach(GTK_TABLE(table),widget,0,1,f,f+1,GTK_FILL,GTK_FILL,2,2);
175 175  
176 176 dlg->file[f] = GTK_ENTRY(gtk_entry_new());
177   - g_signal_connect(G_OBJECT(dlg->file[f]),"changed",G_CALLBACK(check_entry),dlg);
178 177  
179 178 gtk_widget_set_name(GTK_WIDGET(dlg->file[f]),attr[f]);
180 179  
... ... @@ -196,6 +195,8 @@ static void add_file_fields(GObject *action, struct ftdialog *dlg)
196 195  
197 196 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dlg->dialog))),GTK_WIDGET(table),FALSE,FALSE,2);
198 197  
  198 + for(f=0;f<2;f++)
  199 + g_signal_connect(G_OBJECT(dlg->file[f]),"changed",G_CALLBACK(check_entry),dlg);
199 200 }
200 201  
201 202 static void toggle_option(GtkToggleButton *button, const struct ftoption *option)
... ... @@ -282,13 +283,28 @@ static void setup_dft(GObject *action, struct ftdialog *dlg, GtkWidget **label)
282 283  
283 284 }
284 285  
285   -static gboolean run_ft_dialog(GObject *action, GtkWidget *widget, struct ftdialog *dlg)
  286 +
  287 +static void ft_complete(H3270FT *ft, const char *errmsg,unsigned long length,double kbytes_sec,const char *mode)
  288 +{
  289 + if(!ft->widget)
  290 + return;
  291 +
  292 +
  293 +
  294 + ft->widget = NULL;
  295 +}
  296 +
  297 +
  298 +static void run_ft_dialog(GObject *action, GtkWidget *widget, struct ftdialog *dlg)
286 299 {
287 300 H3270FT * ft = NULL;
288 301 const char * msg = NULL;
289 302 int f;
290 303 int parm[G_N_ELEMENTS(dlg->parm)];
291 304  
  305 + for(f=0;f<2;f++)
  306 + gtk_widget_set_sensitive(dlg->ready,is_dialog_ok(GTK_EDITABLE(dlg->file[f]),dlg));
  307 +
292 308 gtk_widget_show_all(dlg->dialog);
293 309  
294 310 for(f=0;f<G_N_ELEMENTS(dlg->parm);f++)
... ... @@ -303,7 +319,11 @@ static gboolean run_ft_dialog(GObject *action, GtkWidget *widget, struct ftdialo
303 319 }
304 320  
305 321 if(gtk_dialog_run(GTK_DIALOG(dlg->dialog)) != GTK_RESPONSE_ACCEPT)
306   - return FALSE;
  322 + {
  323 + gtk_widget_destroy(dlg->dialog);
  324 + dlg->dialog = NULL;
  325 + return;
  326 + }
307 327  
308 328 for(f=0;f<G_N_ELEMENTS(dlg->parm);f++)
309 329 {
... ... @@ -321,7 +341,7 @@ static gboolean run_ft_dialog(GObject *action, GtkWidget *widget, struct ftdialo
321 341 set_string_to_config(dlg->name,"local","%s",gtk_entry_get_text(dlg->file[0]));
322 342 set_string_to_config(dlg->name,"remote","%s",gtk_entry_get_text(dlg->file[1]));
323 343  
324   - ft = lib3270_ft_start( v3270_get_session(widget),
  344 + ft = lib3270_ft_new( v3270_get_session(widget),
325 345 dlg->option,
326 346 gtk_entry_get_text(dlg->file[0]),
327 347 gtk_entry_get_text(dlg->file[1]),
... ... @@ -332,6 +352,9 @@ static gboolean run_ft_dialog(GObject *action, GtkWidget *widget, struct ftdialo
332 352 parm[4], // dft
333 353 &msg );
334 354  
  355 +
  356 + gtk_widget_set_visible(dlg->dialog,FALSE);
  357 +
335 358 if(msg)
336 359 {
337 360 GtkWidget *popup = gtk_message_dialog_new_with_markup(
... ... @@ -351,10 +374,90 @@ static gboolean run_ft_dialog(GObject *action, GtkWidget *widget, struct ftdialo
351 374  
352 375 }
353 376  
354   - if(!ft)
355   - return FALSE;
  377 + if(ft)
  378 + {
  379 + // Setup FT callbacks, create popup window
  380 + /*
  381 + http://www.suggestsoft.com/images/medieval-software/medieval-bluetooth-obex-file-transfer.gif
  382 +
  383 + --Informations----------------------------------------
  384 + |
  385 + | From: xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx
  386 + |
  387 + | To: xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx
  388 + |
  389 + | Status: xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx
  390 + ------------------------------------------------------
  391 +
  392 + --Progress----------------------------------------------
  393 + |
  394 + | Total: xxx.xxx.xxx Current: xxx.xxx.xxx
  395 + |
  396 + | Started: xx:xx:xx ETA: xx:xx:xx
  397 + |
  398 + | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  399 + --------------------------------------------------------
  400 +
  401 + [Cancel]
  402 + */
  403 + GtkWidget *container;
  404 +
  405 + ft->widget = gtk_dialog_new_with_buttons( _( "File transfer" ),
  406 + GTK_WINDOW(gtk_widget_get_toplevel(widget)),
  407 + GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
  408 + GTK_STOCK_CLOSE,GTK_RESPONSE_CLOSE );
  409 +
  410 +
  411 + container = gtk_vbox_new(FALSE,2);
  412 + gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(ft->widget))),container,TRUE,TRUE,2);
  413 +
  414 + // Information frame
  415 + {
  416 + static const gchar *text[] = { N_( "_From:" ), N_( "_To:" ), N_( "_Status:" ) };
  417 +
  418 + GtkWidget * frame = gtk_frame_new( _( "Informations" ) );
  419 + GtkWidget * table = gtk_table_new(G_N_ELEMENTS(text),2,FALSE);
  420 + int f;
  421 + GtkWidget **entry = g_new0(GtkWidget *, G_N_ELEMENTS(text));
  422 +
  423 + g_object_set_data_full(G_OBJECT(ft->widget),"info",entry,g_free);
  424 + gtk_container_set_border_width(GTK_CONTAINER(frame),3);
  425 +
  426 + for(f=0;f<G_N_ELEMENTS(text);f++)
  427 + {
  428 + GtkWidget *label = gtk_label_new_with_mnemonic(gettext(text[f]));
  429 + gtk_misc_set_alignment(GTK_MISC(label),0,.5);
  430 + gtk_table_attach(GTK_TABLE(table),label,0,1,f,f+1,GTK_FILL,GTK_FILL,2,2);
  431 +
  432 + entry[f] = gtk_entry_new();
  433 + gtk_entry_set_width_chars(GTK_ENTRY(entry[f]),70);
  434 + gtk_editable_set_editable(GTK_EDITABLE(entry[f]),FALSE);
  435 + gtk_table_attach(GTK_TABLE(table),entry[f],1,2,f,f+1,GTK_FILL|GTK_EXPAND,GTK_FILL|GTK_EXPAND,2,2);
  436 +
  437 + gtk_label_set_mnemonic_widget(GTK_LABEL(label),entry[f]);
  438 + }
  439 +
  440 + for(f=0;f<2;f++)
  441 + gtk_entry_set_text(GTK_ENTRY(entry[f]),gtk_entry_get_text(dlg->file[f]));
  442 +
  443 + gtk_container_add(GTK_CONTAINER(frame),table);
  444 + gtk_box_pack_start(GTK_BOX(container),frame,TRUE,TRUE,2);
  445 +
  446 + }
  447 +
  448 +
  449 +
  450 + gtk_widget_show_all(ft->widget);
  451 + gtk_dialog_run(GTK_DIALOG(ft->widget));
  452 + gtk_widget_destroy(ft->widget);
  453 +
  454 + lib3270_ft_destroy(ft);
  455 +
  456 + }
  457 +
  458 + gtk_widget_destroy(dlg->dialog);
  459 + dlg->dialog = NULL;
356 460  
357   - return TRUE;
358 461 }
359 462  
360 463 static void add_buttons(struct ftdialog *dlg)
... ... @@ -409,8 +512,6 @@ void download_action(GtkAction *action, GtkWidget *widget)
409 512  
410 513 run_ft_dialog(G_OBJECT(action),widget,&dlg);
411 514  
412   - gtk_widget_destroy(dlg.dialog);
413   -
414 515 }
415 516  
416 517 static void toggle_format(GtkToggleButton *button, const struct ftmask *option)
... ... @@ -565,9 +666,9 @@ void upload_action(GtkAction *action, GtkWidget *widget)
565 666  
566 667 }
567 668  
568   - run_ft_dialog(G_OBJECT(action),widget,&dlg);
  669 + trace("Running ft fialog %p",&dlg);
569 670  
570   - gtk_widget_destroy(dlg.dialog);
  671 + run_ft_dialog(G_OBJECT(action),widget,&dlg);
571 672  
572 673  
573 674 }
... ...