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,
|
|
98 0, 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 &&
|
|
256 !ftp_connect (wdata, wdata->request, 0) ? -1 : 0);
|
|
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 {
|
|
273 ftp_log (gftp_logging_misc, NULL,
|
|
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
|
517
|
286 gftpui_common_run_callback_function (cdata);
|
342
|
287 }
|
|
288
|
|
289
|
|
290 void
|
|
291 gftpui_run_function_cancel_callback (gftp_window_data * wdata,
|
|
292 gftp_dialog_data * ddata)
|
|
293 {
|
|
294 gftpui_callback_data * cdata;
|
|
295
|
|
296 cdata = ddata->yespointer;
|
|
297 if (cdata->input_string != NULL)
|
|
298 g_free (cdata->input_string);
|
|
299 if (cdata->source_string != NULL)
|
|
300 g_free (cdata->source_string);
|
|
301 g_free (cdata);
|
|
302 }
|
|
303
|
|
304
|
|
305 void
|
|
306 gftpui_mkdir_dialog (gpointer data)
|
|
307 {
|
|
308 gftpui_callback_data * cdata;
|
|
309 gftp_window_data * wdata;
|
|
310
|
|
311 wdata = data;
|
|
312 cdata = g_malloc0 (sizeof (*cdata));
|
|
313 cdata->request = wdata->request;
|
|
314 cdata->uidata = wdata;
|
|
315 cdata->run_function = gftpui_common_run_mkdir;
|
|
316
|
|
317 if (!check_status (_("Mkdir"), wdata, gftpui_common_use_threads (wdata->request), 0, 0, wdata->request->mkdir != NULL))
|
|
318 return;
|
|
319
|
|
320 MakeEditDialog (_("Make Directory"), _("Enter name of directory to create"),
|
|
321 NULL, 1, NULL, gftp_dialog_button_create,
|
|
322 gftpui_run_function_callback, cdata,
|
|
323 gftpui_run_function_cancel_callback, cdata);
|
|
324 }
|
|
325
|
|
326
|
|
327 void
|
|
328 gftpui_rename_dialog (gpointer data)
|
|
329 {
|
|
330 gftpui_callback_data * cdata;
|
|
331 GList *templist, *filelist;
|
|
332 gftp_window_data * wdata;
|
|
333 gftp_file * curfle;
|
|
334 char *tempstr;
|
|
335 int num;
|
|
336
|
|
337 wdata = data;
|
|
338 cdata = g_malloc0 (sizeof (*cdata));
|
|
339 cdata->request = wdata->request;
|
|
340 cdata->uidata = wdata;
|
|
341 cdata->run_function = gftpui_common_run_rename;
|
|
342
|
|
343 if (!check_status (_("Rename"), wdata, gftpui_common_use_threads (wdata->request), 1, 1, wdata->request->rename != NULL))
|
|
344 return;
|
|
345
|
525
|
346 templist = gftp_gtk_get_list_selection (wdata);
|
342
|
347 num = 0;
|
|
348 filelist = wdata->files;
|
|
349 templist = get_next_selection (templist, &filelist, &num);
|
|
350 curfle = filelist->data;
|
|
351 cdata->source_string = g_strdup (curfle->file);
|
|
352
|
|
353 tempstr = g_strdup_printf (_("What would you like to rename %s to?"),
|
|
354 cdata->source_string);
|
|
355 MakeEditDialog (_("Rename"), tempstr, cdata->source_string, 1, NULL,
|
|
356 gftp_dialog_button_rename,
|
|
357 gftpui_run_function_callback, cdata,
|
|
358 gftpui_run_function_cancel_callback, cdata);
|
|
359 g_free (tempstr);
|
|
360 }
|
|
361
|
|
362
|
|
363 void
|
|
364 gftpui_site_dialog (gpointer data)
|
|
365 {
|
|
366 gftpui_callback_data * cdata;
|
|
367 gftp_window_data * wdata;
|
|
368
|
|
369 wdata = data;
|
|
370 cdata = g_malloc0 (sizeof (*cdata));
|
|
371 cdata->request = wdata->request;
|
|
372 cdata->uidata = wdata;
|
|
373 cdata->run_function = gftpui_common_run_site;
|
|
374
|
|
375 if (!check_status (_("Site"), wdata, 0, 0, 0, wdata->request->site != NULL))
|
|
376 return;
|
|
377
|
|
378 MakeEditDialog (_("Site"), _("Enter site-specific command"), NULL, 1,
|
633
|
379 _("Prepend with SITE"), gftp_dialog_button_ok,
|
342
|
380 gftpui_run_function_callback, cdata,
|
|
381 gftpui_run_function_cancel_callback, cdata);
|
|
382 }
|
|
383
|
|
384
|
|
385 int
|
|
386 gftpui_run_chdir (gpointer uidata, char *directory)
|
|
387 {
|
|
388 gftpui_callback_data * cdata;
|
|
389 gftp_window_data * wdata;
|
470
|
390 char *tempstr;
|
342
|
391 int ret;
|
|
392
|
556
|
393 wdata = uidata;
|
555
|
394 if ((tempstr = gftp_expand_path (wdata->request, directory)) == NULL)
|
470
|
395 return (FALSE);
|
|
396
|
342
|
397 cdata = g_malloc0 (sizeof (*cdata));
|
|
398 cdata->request = wdata->request;
|
|
399 cdata->uidata = wdata;
|
|
400 cdata->run_function = gftpui_common_run_chdir;
|
470
|
401 cdata->input_string = tempstr;
|
514
|
402 cdata->dont_clear_cache = 1;
|
342
|
403
|
|
404 ret = gftpui_common_run_callback_function (cdata);
|
|
405
|
470
|
406 g_free(tempstr);
|
342
|
407 g_free (cdata);
|
|
408 return (ret);
|
|
409 }
|
|
410
|
|
411
|
|
412 void
|
|
413 gftpui_chdir_dialog (gpointer data)
|
|
414 {
|
|
415 GList *templist, *filelist;
|
|
416 gftp_window_data * wdata;
|
|
417 gftp_file * curfle;
|
|
418 char *tempstr;
|
|
419 int num;
|
|
420
|
|
421 wdata = data;
|
|
422 if (!check_status (_("Chdir"), wdata, gftpui_common_use_threads (wdata->request), 1, 0,
|
|
423 wdata->request->chdir != NULL))
|
|
424 return;
|
|
425
|
525
|
426 templist = gftp_gtk_get_list_selection (wdata);
|
342
|
427 num = 0;
|
|
428 filelist = wdata->files;
|
|
429 templist = get_next_selection (templist, &filelist, &num);
|
|
430 curfle = filelist->data;
|
|
431
|
555
|
432 tempstr = gftp_build_path (wdata->request, wdata->request->directory,
|
|
433 curfle->file, NULL);
|
342
|
434 gftpui_run_chdir (wdata, tempstr);
|
|
435 g_free (tempstr);
|
|
436 }
|
|
437
|
380
|
438
|
|
439 void
|
|
440 gftpui_disconnect (void *uidata)
|
|
441 {
|
|
442 gftp_window_data * wdata;
|
|
443
|
|
444 wdata = uidata;
|
|
445 gftp_delete_cache_entry (wdata->request, NULL, 1);
|
|
446 gftp_disconnect (wdata->request);
|
|
447 remove_files_window (wdata);
|
604
|
448 update_window_info ();
|
380
|
449 }
|
|
450
|
397
|
451
|
|
452 char *
|
|
453 gftpui_gtk_get_utf8_file_pos (gftp_file * fle)
|
|
454 {
|
|
455 char *disp_item, *pos;
|
|
456
|
|
457 if (fle->utf8_file != NULL)
|
|
458 disp_item = fle->utf8_file;
|
|
459 else
|
|
460 disp_item = fle->file;
|
|
461
|
|
462 if ((pos = strrchr (disp_item, '/')) != NULL)
|
|
463 pos++;
|
|
464 else
|
|
465 pos = disp_item;
|
|
466
|
|
467 return (pos);
|
|
468 }
|
|
469
|
460
|
470
|
|
471 static void
|
|
472 _protocol_yes_answer (gpointer answer, gftp_dialog_data * ddata)
|
|
473 {
|
|
474 *(int *) answer = 1;
|
|
475 }
|
|
476
|
|
477
|
|
478 static void
|
|
479 _protocol_no_answer (gpointer answer, gftp_dialog_data * ddata)
|
|
480 {
|
|
481 *(int *) answer = 0;
|
|
482 }
|
|
483
|
|
484
|
|
485 int
|
|
486 gftpui_protocol_ask_yes_no (gftp_request * request, char *title,
|
|
487 char *question)
|
|
488 {
|
|
489 int answer = -1;
|
|
490
|
|
491 GDK_THREADS_ENTER ();
|
|
492
|
|
493 MakeYesNoDialog (title, question, _protocol_yes_answer, &answer,
|
|
494 _protocol_no_answer, &answer);
|
|
495
|
|
496 if (gftp_protocols[request->protonum].use_threads)
|
|
497 {
|
484
|
498 /* Let the main loop in the main thread run the events */
|
460
|
499 GDK_THREADS_LEAVE ();
|
|
500
|
|
501 while (answer == -1)
|
|
502 {
|
|
503 sleep (1);
|
|
504 }
|
|
505 }
|
|
506 else
|
|
507 {
|
|
508 while (answer == -1)
|
|
509 {
|
|
510 GDK_THREADS_LEAVE ();
|
|
511 #if GTK_MAJOR_VERSION == 1
|
|
512 g_main_iteration (TRUE);
|
|
513 #else
|
|
514 g_main_context_iteration (NULL, TRUE);
|
|
515 #endif
|
|
516 }
|
|
517 }
|
|
518
|
|
519 return (answer);
|
|
520 }
|
|
521
|
484
|
522
|
|
523 static void
|
|
524 _protocol_ok_answer (char *buf, gftp_dialog_data * ddata)
|
|
525 {
|
|
526 buf[1] = ' '; /* In case this is an empty string entered */
|
|
527 strncpy (buf, gtk_entry_get_text (GTK_ENTRY (ddata->edit)), BUFSIZ);
|
|
528 }
|
|
529
|
|
530
|
|
531 static void
|
|
532 _protocol_cancel_answer (char *buf, gftp_dialog_data * ddata)
|
|
533 {
|
|
534 buf[0] = '\0';
|
|
535 buf[1] = '\0';
|
|
536 }
|
|
537
|
|
538
|
|
539 char *
|
|
540 gftpui_protocol_ask_user_input (gftp_request * request, char *title,
|
|
541 char *question, int shown)
|
|
542 {
|
|
543 char buf[BUFSIZ];
|
|
544
|
|
545 GDK_THREADS_ENTER ();
|
|
546
|
|
547 *buf = '\0';
|
|
548 *(buf + 1) = ' ';
|
|
549 MakeEditDialog (title, question, NULL, shown, NULL, gftp_dialog_button_ok,
|
|
550 _protocol_ok_answer, &buf, _protocol_cancel_answer, &buf);
|
|
551
|
|
552 if (gftp_protocols[request->protonum].use_threads)
|
|
553 {
|
|
554 /* Let the main loop in the main thread run the events */
|
|
555 GDK_THREADS_LEAVE ();
|
|
556
|
|
557 while (*buf == '\0' && *(buf + 1) == ' ')
|
|
558 {
|
|
559 sleep (1);
|
|
560 }
|
|
561 }
|
|
562 else
|
|
563 {
|
|
564 while (*buf == '\0' && *(buf + 1) == ' ')
|
|
565 {
|
|
566 GDK_THREADS_LEAVE ();
|
|
567 #if GTK_MAJOR_VERSION == 1
|
|
568 g_main_iteration (TRUE);
|
|
569 #else
|
|
570 g_main_context_iteration (NULL, TRUE);
|
|
571 #endif
|
|
572 }
|
|
573 }
|
|
574
|
|
575 if (*buf != '\0')
|
|
576 return (g_strdup (buf));
|
|
577 else
|
|
578 return (NULL);
|
|
579 }
|
|
580
|