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