342
|
1 /*****************************************************************************/
|
|
2 /* gtkui.c - GTK+ UI related functions for gFTP */
|
|
3 /* Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org> */
|
|
4 /* */
|
|
5 /* This program is free software; you can redistribute it and/or modify */
|
|
6 /* it under the terms of the GNU General Public License as published by */
|
|
7 /* the Free Software Foundation; either version 2 of the License, or */
|
|
8 /* (at your option) any later version. */
|
|
9 /* */
|
|
10 /* This program is distributed in the hope that it will be useful, */
|
|
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
|
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
|
13 /* GNU General Public License for more details. */
|
|
14 /* */
|
|
15 /* You should have received a copy of the GNU General Public License */
|
|
16 /* along with this program; if not, write to the Free Software */
|
|
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */
|
|
18 /*****************************************************************************/
|
|
19
|
|
20 #include "gftp-gtk.h"
|
|
21 static const char cvsid[] = "$Id$";
|
|
22
|
|
23
|
|
24 void
|
|
25 gftpui_lookup_file_colors (gftp_file * fle, char **start_color,
|
|
26 char ** end_color)
|
|
27 {
|
|
28 *start_color = GFTPUI_COMMON_COLOR_NONE;
|
|
29 *end_color = GFTPUI_COMMON_COLOR_NONE;
|
|
30 }
|
|
31
|
|
32
|
|
33 void
|
|
34 gftpui_run_command (GtkWidget * widget, gpointer data)
|
|
35 {
|
|
36 const char *txt;
|
|
37
|
|
38 txt = gtk_entry_get_text (GTK_ENTRY (gftpui_command_widget));
|
374
|
39 gftpui_common_process_command (&window1, window1.request,
|
|
40 &window2, window2.request, txt);
|
342
|
41 gtk_entry_set_text (GTK_ENTRY (gftpui_command_widget), "");
|
|
42 }
|
|
43
|
|
44
|
|
45 void
|
521
|
46 gftpui_refresh (void *uidata, int clear_cache_entry)
|
342
|
47 {
|
|
48 gftp_window_data * wdata;
|
879
|
49 int ret;
|
342
|
50
|
|
51 wdata = uidata;
|
879
|
52 wdata->request->refreshing = 1;
|
|
53
|
342
|
54 if (!check_status (_("Refresh"), wdata, 0, 0, 0, 1))
|
879
|
55 {
|
|
56 wdata->request->refreshing = 0;
|
|
57 return;
|
|
58 }
|
342
|
59
|
807
|
60 if (clear_cache_entry)
|
|
61 gftp_delete_cache_entry (wdata->request, NULL, 0);
|
|
62
|
342
|
63 if (check_reconnect (wdata) < 0)
|
879
|
64 {
|
|
65 wdata->request->refreshing = 0;
|
|
66 return;
|
|
67 }
|
342
|
68
|
|
69 gtk_clist_freeze (GTK_CLIST (wdata->listbox));
|
|
70 remove_files_window (wdata);
|
514
|
71
|
496
|
72 ftp_list_files (wdata);
|
342
|
73 gtk_clist_thaw (GTK_CLIST (wdata->listbox));
|
879
|
74
|
|
75 wdata->request->refreshing = 0;
|
342
|
76 }
|
|
77
|
|
78
|
356
|
79 #define _GFTPUI_GTK_USER_PW_SIZE 256
|
|
80
|
|
81 static void
|
380
|
82 _gftpui_gtk_set_username (gftp_request * request, gftp_dialog_data * ddata)
|
356
|
83 {
|
380
|
84 gftp_set_username (request, gtk_entry_get_text (GTK_ENTRY (ddata->edit)));
|
|
85 request->stopable = 0;
|
|
86 }
|
|
87
|
|
88
|
|
89 static void
|
|
90 _gftpui_gtk_set_password (gftp_request * request, gftp_dialog_data * ddata)
|
|
91 {
|
|
92 gftp_set_password (request, gtk_entry_get_text (GTK_ENTRY (ddata->edit)));
|
|
93 request->stopable = 0;
|
356
|
94 }
|
|
95
|
|
96
|
|
97 static void
|
380
|
98 _gftpui_gtk_abort (gftp_request * request, gftp_dialog_data * ddata)
|
356
|
99 {
|
380
|
100 request->stopable = 0;
|
356
|
101 }
|
|
102
|
775
|
103 void
|
|
104 gftpui_show_busy (gboolean busy)
|
|
105 {
|
|
106 GtkWidget * toplevel = gtk_widget_get_toplevel (openurl_btn);
|
|
107 GdkDisplay * display = gtk_widget_get_display (toplevel);
|
|
108
|
|
109 GdkCursor * busyCursor =
|
|
110 (busy) ? (gdk_cursor_new_for_display (display, GDK_WATCH)) : NULL;
|
|
111
|
|
112 gdk_window_set_cursor (toplevel->window, busyCursor);
|
|
113
|
|
114 if (busy)
|
|
115 gdk_cursor_unref (busyCursor);
|
|
116 }
|
356
|
117
|
380
|
118 void
|
356
|
119 gftpui_prompt_username (void *uidata, gftp_request * request)
|
|
120 {
|
|
121 MakeEditDialog (_("Enter Username"),
|
|
122 _("Please enter your username for this site"), NULL,
|
755
|
123 1, NULL, gftp_dialog_button_connect,
|
380
|
124 _gftpui_gtk_set_username, request,
|
|
125 _gftpui_gtk_abort, request);
|
356
|
126
|
380
|
127 request->stopable = 1;
|
|
128 while (request->stopable)
|
356
|
129 {
|
|
130 GDK_THREADS_LEAVE ();
|
|
131 #if GTK_MAJOR_VERSION == 1
|
|
132 g_main_iteration (TRUE);
|
|
133 #else
|
|
134 g_main_context_iteration (NULL, TRUE);
|
|
135 #endif
|
|
136 }
|
|
137 }
|
|
138
|
|
139
|
380
|
140 void
|
356
|
141 gftpui_prompt_password (void *uidata, gftp_request * request)
|
|
142 {
|
|
143 MakeEditDialog (_("Enter Password"),
|
|
144 _("Please enter your password for this site"), NULL,
|
|
145 0, NULL, gftp_dialog_button_connect,
|
380
|
146 _gftpui_gtk_set_password, request,
|
|
147 _gftpui_gtk_abort, request);
|
356
|
148
|
380
|
149 request->stopable = 1;
|
|
150 while (request->stopable)
|
356
|
151 {
|
|
152 GDK_THREADS_LEAVE ();
|
|
153 #if GTK_MAJOR_VERSION == 1
|
|
154 g_main_iteration (TRUE);
|
|
155 #else
|
|
156 g_main_context_iteration (NULL, TRUE);
|
|
157 #endif
|
|
158 }
|
|
159 }
|
|
160
|
|
161
|
355
|
162 /* The wakeup main thread functions are so that after the thread terminates
|
|
163 there won't be a delay in updating the GUI */
|
|
164 static void
|
|
165 _gftpui_wakeup_main_thread (gpointer data, gint source,
|
|
166 GdkInputCondition condition)
|
|
167 {
|
|
168 gftp_request * request;
|
|
169 char c;
|
|
170
|
|
171 request = data;
|
|
172 if (request->wakeup_main_thread[0] > 0)
|
|
173 read (request->wakeup_main_thread[0], &c, 1);
|
|
174 }
|
|
175
|
|
176
|
|
177 static gint
|
|
178 _gftpui_setup_wakeup_main_thread (gftp_request * request)
|
|
179 {
|
|
180 gint handler;
|
|
181
|
|
182 if (socketpair (AF_UNIX, SOCK_STREAM, 0, request->wakeup_main_thread) == 0)
|
|
183 {
|
|
184 handler = gdk_input_add (request->wakeup_main_thread[0],
|
|
185 GDK_INPUT_READ, _gftpui_wakeup_main_thread,
|
|
186 request);
|
|
187 }
|
|
188 else
|
|
189 {
|
|
190 request->wakeup_main_thread[0] = 0;
|
|
191 request->wakeup_main_thread[1] = 0;
|
|
192 handler = 0;
|
|
193 }
|
|
194
|
|
195 return (handler);
|
|
196 }
|
|
197
|
|
198
|
|
199 static void
|
|
200 _gftpui_teardown_wakeup_main_thread (gftp_request * request, gint handler)
|
|
201 {
|
|
202 if (request->wakeup_main_thread[0] > 0 && request->wakeup_main_thread[1] > 0)
|
|
203 {
|
|
204 gdk_input_remove (handler);
|
|
205 close (request->wakeup_main_thread[0]);
|
|
206 close (request->wakeup_main_thread[1]);
|
|
207 request->wakeup_main_thread[0] = 0;
|
|
208 request->wakeup_main_thread[1] = 0;
|
|
209 }
|
|
210 }
|
|
211
|
|
212 static void *
|
|
213 _gftpui_gtk_thread_func (void *data)
|
|
214 {
|
|
215 gftpui_gtk_thread_data * thread_data;
|
|
216 void *ret;
|
|
217
|
|
218 thread_data = data;
|
|
219 ret = thread_data->func (thread_data->cdata);
|
|
220
|
|
221 if (thread_data->cdata->request->wakeup_main_thread[1] > 0)
|
|
222 write (thread_data->cdata->request->wakeup_main_thread[1], " ", 1);
|
|
223
|
|
224 return (ret);
|
|
225 }
|
|
226
|
|
227
|
342
|
228 void *
|
|
229 gftpui_generic_thread (void * (*func) (void *), void *data)
|
|
230 {
|
355
|
231 gftpui_gtk_thread_data * thread_data;
|
342
|
232 gftpui_callback_data * cdata;
|
|
233 gftp_window_data * wdata;
|
518
|
234 gint handler;
|
342
|
235 void * ret;
|
|
236
|
|
237 cdata = data;
|
|
238 wdata = cdata->uidata;
|
|
239
|
|
240 wdata->request->stopable = 1;
|
|
241 gtk_widget_set_sensitive (stop_btn, 1);
|
355
|
242
|
|
243 thread_data = g_malloc0 (sizeof (*thread_data));
|
|
244 thread_data->func = func;
|
|
245 thread_data->cdata = cdata;
|
|
246
|
|
247 handler = _gftpui_setup_wakeup_main_thread (cdata->request);
|
|
248 pthread_create (&wdata->tid, NULL, _gftpui_gtk_thread_func, thread_data);
|
342
|
249
|
|
250 while (wdata->request->stopable)
|
|
251 {
|
|
252 GDK_THREADS_LEAVE ();
|
|
253 #if GTK_MAJOR_VERSION == 1
|
|
254 g_main_iteration (TRUE);
|
|
255 #else
|
|
256 g_main_context_iteration (NULL, TRUE);
|
|
257 #endif
|
|
258 }
|
|
259
|
355
|
260 _gftpui_teardown_wakeup_main_thread (cdata->request, handler);
|
|
261 g_free (thread_data);
|
|
262
|
342
|
263 pthread_join (wdata->tid, &ret);
|
|
264 gtk_widget_set_sensitive (stop_btn, 0);
|
|
265
|
|
266 if (!GFTP_IS_CONNECTED (wdata->request))
|
380
|
267 gftpui_disconnect (wdata);
|
342
|
268
|
|
269 return (ret);
|
|
270 }
|
|
271
|
|
272
|
|
273 int
|
|
274 gftpui_check_reconnect (gftpui_callback_data * cdata)
|
|
275 {
|
|
276 gftp_window_data * wdata;
|
|
277
|
|
278 wdata = cdata->uidata;
|
|
279 return (wdata->request->cached && wdata->request->datafd < 0 &&
|
|
280 !wdata->request->always_connected &&
|
767
|
281 !ftp_connect (wdata, wdata->request) ? -1 : 0);
|
342
|
282 }
|
|
283
|
|
284
|
|
285 void
|
|
286 gftpui_run_function_callback (gftp_window_data * wdata,
|
|
287 gftp_dialog_data * ddata)
|
|
288 {
|
|
289 gftpui_callback_data * cdata;
|
|
290 const char *edttext;
|
|
291
|
|
292 cdata = ddata->yespointer;
|
|
293 if (ddata->edit != NULL)
|
|
294 {
|
|
295 edttext = gtk_entry_get_text (GTK_ENTRY (ddata->edit));
|
|
296 if (*edttext == '\0')
|
|
297 {
|
677
|
298 ftp_log (gftp_logging_error, NULL,
|
342
|
299 _("Operation canceled...you must enter a string\n"));
|
|
300 return;
|
|
301 }
|
|
302
|
|
303 cdata->input_string = g_strdup (edttext);
|
|
304 }
|
|
305
|
633
|
306 if (ddata->checkbox != NULL)
|
|
307 cdata->toggled = GTK_TOGGLE_BUTTON (ddata->checkbox)->active;
|
|
308 else
|
|
309 cdata->toggled = 0;
|
|
310
|
679
|
311 gtk_widget_destroy (ddata->dialog);
|
|
312 ddata->dialog = NULL;
|
|
313
|
517
|
314 gftpui_common_run_callback_function (cdata);
|
342
|
315 }
|
|
316
|
|
317
|
|
318 void
|
|
319 gftpui_run_function_cancel_callback (gftp_window_data * wdata,
|
|
320 gftp_dialog_data * ddata)
|
|
321 {
|
|
322 gftpui_callback_data * cdata;
|
|
323
|
|
324 cdata = ddata->yespointer;
|
|
325 if (cdata->input_string != NULL)
|
|
326 g_free (cdata->input_string);
|
|
327 if (cdata->source_string != NULL)
|
|
328 g_free (cdata->source_string);
|
|
329 g_free (cdata);
|
|
330 }
|
|
331
|
|
332
|
|
333 void
|
|
334 gftpui_mkdir_dialog (gpointer data)
|
|
335 {
|
|
336 gftpui_callback_data * cdata;
|
|
337 gftp_window_data * wdata;
|
|
338
|
|
339 wdata = data;
|
|
340 cdata = g_malloc0 (sizeof (*cdata));
|
|
341 cdata->request = wdata->request;
|
|
342 cdata->uidata = wdata;
|
|
343 cdata->run_function = gftpui_common_run_mkdir;
|
|
344
|
|
345 if (!check_status (_("Mkdir"), wdata, gftpui_common_use_threads (wdata->request), 0, 0, wdata->request->mkdir != NULL))
|
|
346 return;
|
|
347
|
|
348 MakeEditDialog (_("Make Directory"), _("Enter name of directory to create"),
|
|
349 NULL, 1, NULL, gftp_dialog_button_create,
|
|
350 gftpui_run_function_callback, cdata,
|
|
351 gftpui_run_function_cancel_callback, cdata);
|
|
352 }
|
|
353
|
|
354
|
|
355 void
|
|
356 gftpui_rename_dialog (gpointer data)
|
|
357 {
|
|
358 gftpui_callback_data * cdata;
|
|
359 GList *templist, *filelist;
|
|
360 gftp_window_data * wdata;
|
|
361 gftp_file * curfle;
|
|
362 char *tempstr;
|
|
363 int num;
|
|
364
|
|
365 wdata = data;
|
|
366 cdata = g_malloc0 (sizeof (*cdata));
|
|
367 cdata->request = wdata->request;
|
|
368 cdata->uidata = wdata;
|
|
369 cdata->run_function = gftpui_common_run_rename;
|
|
370
|
|
371 if (!check_status (_("Rename"), wdata, gftpui_common_use_threads (wdata->request), 1, 1, wdata->request->rename != NULL))
|
|
372 return;
|
|
373
|
525
|
374 templist = gftp_gtk_get_list_selection (wdata);
|
342
|
375 num = 0;
|
|
376 filelist = wdata->files;
|
|
377 templist = get_next_selection (templist, &filelist, &num);
|
|
378 curfle = filelist->data;
|
|
379 cdata->source_string = g_strdup (curfle->file);
|
|
380
|
|
381 tempstr = g_strdup_printf (_("What would you like to rename %s to?"),
|
|
382 cdata->source_string);
|
|
383 MakeEditDialog (_("Rename"), tempstr, cdata->source_string, 1, NULL,
|
|
384 gftp_dialog_button_rename,
|
|
385 gftpui_run_function_callback, cdata,
|
|
386 gftpui_run_function_cancel_callback, cdata);
|
|
387 g_free (tempstr);
|
|
388 }
|
|
389
|
|
390
|
|
391 void
|
|
392 gftpui_site_dialog (gpointer data)
|
|
393 {
|
|
394 gftpui_callback_data * cdata;
|
|
395 gftp_window_data * wdata;
|
|
396
|
|
397 wdata = data;
|
|
398 cdata = g_malloc0 (sizeof (*cdata));
|
|
399 cdata->request = wdata->request;
|
|
400 cdata->uidata = wdata;
|
|
401 cdata->run_function = gftpui_common_run_site;
|
|
402
|
|
403 if (!check_status (_("Site"), wdata, 0, 0, 0, wdata->request->site != NULL))
|
|
404 return;
|
|
405
|
|
406 MakeEditDialog (_("Site"), _("Enter site-specific command"), NULL, 1,
|
633
|
407 _("Prepend with SITE"), gftp_dialog_button_ok,
|
342
|
408 gftpui_run_function_callback, cdata,
|
|
409 gftpui_run_function_cancel_callback, cdata);
|
|
410 }
|
|
411
|
|
412
|
|
413 int
|
|
414 gftpui_run_chdir (gpointer uidata, char *directory)
|
|
415 {
|
|
416 gftpui_callback_data * cdata;
|
|
417 gftp_window_data * wdata;
|
470
|
418 char *tempstr;
|
342
|
419 int ret;
|
|
420
|
556
|
421 wdata = uidata;
|
555
|
422 if ((tempstr = gftp_expand_path (wdata->request, directory)) == NULL)
|
470
|
423 return (FALSE);
|
|
424
|
342
|
425 cdata = g_malloc0 (sizeof (*cdata));
|
|
426 cdata->request = wdata->request;
|
|
427 cdata->uidata = wdata;
|
|
428 cdata->run_function = gftpui_common_run_chdir;
|
470
|
429 cdata->input_string = tempstr;
|
514
|
430 cdata->dont_clear_cache = 1;
|
342
|
431
|
|
432 ret = gftpui_common_run_callback_function (cdata);
|
|
433
|
470
|
434 g_free(tempstr);
|
342
|
435 g_free (cdata);
|
|
436 return (ret);
|
|
437 }
|
|
438
|
|
439
|
|
440 void
|
|
441 gftpui_chdir_dialog (gpointer data)
|
|
442 {
|
|
443 GList *templist, *filelist;
|
|
444 gftp_window_data * wdata;
|
|
445 gftp_file * curfle;
|
|
446 char *tempstr;
|
|
447 int num;
|
|
448
|
|
449 wdata = data;
|
|
450 if (!check_status (_("Chdir"), wdata, gftpui_common_use_threads (wdata->request), 1, 0,
|
|
451 wdata->request->chdir != NULL))
|
|
452 return;
|
|
453
|
525
|
454 templist = gftp_gtk_get_list_selection (wdata);
|
342
|
455 num = 0;
|
|
456 filelist = wdata->files;
|
|
457 templist = get_next_selection (templist, &filelist, &num);
|
|
458 curfle = filelist->data;
|
|
459
|
555
|
460 tempstr = gftp_build_path (wdata->request, wdata->request->directory,
|
|
461 curfle->file, NULL);
|
342
|
462 gftpui_run_chdir (wdata, tempstr);
|
|
463 g_free (tempstr);
|
|
464 }
|
|
465
|
380
|
466
|
|
467 void
|
|
468 gftpui_disconnect (void *uidata)
|
|
469 {
|
|
470 gftp_window_data * wdata;
|
|
471
|
|
472 wdata = uidata;
|
|
473 gftp_delete_cache_entry (wdata->request, NULL, 1);
|
|
474 gftp_disconnect (wdata->request);
|
|
475 remove_files_window (wdata);
|
604
|
476 update_window_info ();
|
380
|
477 }
|
|
478
|
397
|
479
|
|
480 char *
|
|
481 gftpui_gtk_get_utf8_file_pos (gftp_file * fle)
|
|
482 {
|
830
|
483 char *pos;
|
397
|
484
|
830
|
485 if ((pos = strrchr (fle->file, '/')) != NULL)
|
397
|
486 pos++;
|
|
487 else
|
830
|
488 pos = fle->file;
|
397
|
489
|
|
490 return (pos);
|
|
491 }
|
|
492
|
460
|
493
|
|
494 static void
|
|
495 _protocol_yes_answer (gpointer answer, gftp_dialog_data * ddata)
|
|
496 {
|
|
497 *(int *) answer = 1;
|
|
498 }
|
|
499
|
|
500
|
|
501 static void
|
|
502 _protocol_no_answer (gpointer answer, gftp_dialog_data * ddata)
|
|
503 {
|
|
504 *(int *) answer = 0;
|
|
505 }
|
|
506
|
|
507
|
|
508 int
|
|
509 gftpui_protocol_ask_yes_no (gftp_request * request, char *title,
|
|
510 char *question)
|
|
511 {
|
|
512 int answer = -1;
|
|
513
|
|
514 GDK_THREADS_ENTER ();
|
|
515
|
|
516 MakeYesNoDialog (title, question, _protocol_yes_answer, &answer,
|
|
517 _protocol_no_answer, &answer);
|
|
518
|
|
519 if (gftp_protocols[request->protonum].use_threads)
|
|
520 {
|
484
|
521 /* Let the main loop in the main thread run the events */
|
460
|
522 GDK_THREADS_LEAVE ();
|
|
523
|
|
524 while (answer == -1)
|
|
525 {
|
|
526 sleep (1);
|
|
527 }
|
|
528 }
|
|
529 else
|
|
530 {
|
|
531 while (answer == -1)
|
|
532 {
|
|
533 GDK_THREADS_LEAVE ();
|
|
534 #if GTK_MAJOR_VERSION == 1
|
|
535 g_main_iteration (TRUE);
|
|
536 #else
|
|
537 g_main_context_iteration (NULL, TRUE);
|
|
538 #endif
|
|
539 }
|
|
540 }
|
|
541
|
|
542 return (answer);
|
|
543 }
|
|
544
|
484
|
545
|
|
546 static void
|
|
547 _protocol_ok_answer (char *buf, gftp_dialog_data * ddata)
|
|
548 {
|
|
549 buf[1] = ' '; /* In case this is an empty string entered */
|
|
550 strncpy (buf, gtk_entry_get_text (GTK_ENTRY (ddata->edit)), BUFSIZ);
|
|
551 }
|
|
552
|
|
553
|
|
554 static void
|
|
555 _protocol_cancel_answer (char *buf, gftp_dialog_data * ddata)
|
|
556 {
|
|
557 buf[0] = '\0';
|
|
558 buf[1] = '\0';
|
|
559 }
|
|
560
|
|
561
|
|
562 char *
|
|
563 gftpui_protocol_ask_user_input (gftp_request * request, char *title,
|
|
564 char *question, int shown)
|
|
565 {
|
|
566 char buf[BUFSIZ];
|
|
567
|
|
568 GDK_THREADS_ENTER ();
|
|
569
|
|
570 *buf = '\0';
|
|
571 *(buf + 1) = ' ';
|
|
572 MakeEditDialog (title, question, NULL, shown, NULL, gftp_dialog_button_ok,
|
|
573 _protocol_ok_answer, &buf, _protocol_cancel_answer, &buf);
|
|
574
|
|
575 if (gftp_protocols[request->protonum].use_threads)
|
|
576 {
|
|
577 /* Let the main loop in the main thread run the events */
|
|
578 GDK_THREADS_LEAVE ();
|
|
579
|
|
580 while (*buf == '\0' && *(buf + 1) == ' ')
|
|
581 {
|
|
582 sleep (1);
|
|
583 }
|
|
584 }
|
|
585 else
|
|
586 {
|
|
587 while (*buf == '\0' && *(buf + 1) == ' ')
|
|
588 {
|
|
589 GDK_THREADS_LEAVE ();
|
|
590 #if GTK_MAJOR_VERSION == 1
|
|
591 g_main_iteration (TRUE);
|
|
592 #else
|
|
593 g_main_context_iteration (NULL, TRUE);
|
|
594 #endif
|
|
595 }
|
|
596 }
|
|
597
|
|
598 if (*buf != '\0')
|
|
599 return (g_strdup (buf));
|
|
600 else
|
|
601 return (NULL);
|
|
602 }
|
|
603
|