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