Mercurial > gftp.yaz
annotate src/gtk/view_dialog.c @ 885:1808cebed602
2007-3-12 Brian Masney <masneyb@gftp.org>
* lib/* src/* - updated copyright dates
author | masneyb |
---|---|
date | Tue, 13 Mar 2007 01:52:50 +0000 |
parents | 49cfbe02926b |
children | 14476a8a4d2b |
rev | line source |
---|---|
1 | 1 /*****************************************************************************/ |
2 /* view_dialog.c - view dialog box and ftp routines */ | |
885 | 3 /* Copyright (C) 1998-2007 Brian Masney <masneyb@gftp.org> */ |
1 | 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" | |
33 | 21 static const char cvsid[] = "$Id$"; |
1 | 22 |
23 static gftp_file * curfle; | |
24 | |
25 void | |
26 view_dialog (gpointer data) | |
27 { | |
28 GList * templist, * filelist, * newfile; | |
29 gftp_window_data * fromwdata, * towdata; | |
30 gftp_file * new_fle; | |
402 | 31 char *suffix; |
58 | 32 int num; |
1 | 33 |
34 fromwdata = data; | |
35 towdata = fromwdata == &window1 ? &window2 : &window1; | |
36 if (!check_status (_("View"), fromwdata, 0, 1, 1, 1)) | |
37 return; | |
38 | |
39 templist = GTK_CLIST (fromwdata->listbox)->selection; | |
40 num = 0; | |
41 filelist = fromwdata->files; | |
42 templist = get_next_selection (templist, &filelist, &num); | |
43 curfle = filelist->data; | |
44 | |
499 | 45 if (S_ISDIR (curfle->st_mode)) |
1 | 46 { |
677 | 47 ftp_log (gftp_logging_error, NULL, |
1 | 48 _("View: %s is a directory. Cannot view it.\n"), curfle->file); |
49 return; | |
50 } | |
51 | |
129 | 52 if (strcmp (gftp_protocols[fromwdata->request->protonum].name, "Local") == 0) |
1 | 53 view_file (curfle->file, 0, 1, 0, 1, 1, NULL, fromwdata); |
54 else | |
55 { | |
56 new_fle = copy_fdata (curfle); | |
57 if (new_fle->destfile) | |
58 g_free (new_fle->destfile); | |
402 | 59 |
60 if ((suffix = strrchr (curfle->file, '.')) != NULL) | |
61 { | |
62 new_fle->destfile = g_strconcat (g_get_tmp_dir (), | |
63 "/gftp-view.XXXXXX", suffix, NULL); | |
64 new_fle->fd = mkstemps (new_fle->destfile, strlen (suffix)); | |
65 } | |
66 else | |
67 { | |
68 new_fle->destfile = g_strconcat (g_get_tmp_dir (), | |
69 "/gftp-view.XXXXXX", NULL); | |
70 new_fle->fd = mkstemps (new_fle->destfile, 0); | |
71 } | |
72 | |
73 if (new_fle->fd < 0) | |
1 | 74 { |
677 | 75 ftp_log (gftp_logging_error, NULL, |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
1
diff
changeset
|
76 _("Error: Cannot open %s for writing: %s\n"), |
1 | 77 new_fle->destfile, g_strerror (errno)); |
598 | 78 gftp_file_destroy (new_fle, 1); |
1 | 79 return; |
80 } | |
81 | |
58 | 82 fchmod (new_fle->fd, S_IRUSR | S_IWUSR); |
1 | 83 |
84 new_fle->is_fd = 1; | |
85 new_fle->done_view = 1; | |
86 new_fle->done_rm = 1; | |
87 newfile = g_list_append (NULL, new_fle); | |
367 | 88 gftpui_common_add_file_transfer (fromwdata->request, towdata->request, |
89 fromwdata, towdata, newfile); | |
1 | 90 } |
91 } | |
92 | |
93 | |
94 void | |
95 edit_dialog (gpointer data) | |
96 { | |
97 gftp_window_data * fromwdata, * towdata; | |
98 GList * templist, * filelist, * newfile; | |
99 gftp_file * new_fle; | |
422 | 100 char *edit_program, *suffix; |
58 | 101 int num; |
1 | 102 |
103 fromwdata = data; | |
104 towdata = fromwdata == &window1 ? &window2 : &window1; | |
105 if (!check_status (_("Edit"), fromwdata, 0, 1, 1, 1)) | |
106 return; | |
107 | |
129 | 108 gftp_lookup_request_option (fromwdata->request, "edit_program", |
109 &edit_program); | |
110 | |
1 | 111 if (*edit_program == '\0') |
112 { | |
677 | 113 ftp_log (gftp_logging_error, NULL, |
1 | 114 _("Edit: You must specify an editor in the options dialog\n")); |
115 return; | |
116 } | |
117 | |
118 templist = GTK_CLIST (fromwdata->listbox)->selection; | |
119 num = 0; | |
120 filelist = fromwdata->files; | |
121 templist = get_next_selection (templist, &filelist, &num); | |
122 curfle = filelist->data; | |
123 | |
499 | 124 if (S_ISDIR (curfle->st_mode)) |
1 | 125 { |
677 | 126 ftp_log (gftp_logging_error, NULL, |
1 | 127 _("Edit: %s is a directory. Cannot edit it.\n"), curfle->file); |
128 return; | |
129 } | |
130 | |
129 | 131 if (strcmp (gftp_protocols[fromwdata->request->protonum].name, "Local") == 0) |
1 | 132 view_file (curfle->file, 0, 0, 0, 1, 1, NULL, fromwdata); |
133 else | |
134 { | |
135 new_fle = copy_fdata (curfle); | |
136 if (new_fle->destfile) | |
137 g_free (new_fle->destfile); | |
422 | 138 |
139 if ((suffix = strrchr (curfle->file, '.')) != NULL) | |
140 { | |
141 new_fle->destfile = g_strconcat (g_get_tmp_dir (), | |
142 "/gftp-view.XXXXXX", suffix, NULL); | |
143 new_fle->fd = mkstemps (new_fle->destfile, strlen (suffix)); | |
144 } | |
145 else | |
146 { | |
147 new_fle->destfile = g_strconcat (g_get_tmp_dir (), | |
148 "/gftp-view.XXXXXX", NULL); | |
149 new_fle->fd = mkstemps (new_fle->destfile, 0); | |
150 } | |
151 | |
152 if (new_fle->fd < 0) | |
1 | 153 { |
677 | 154 ftp_log (gftp_logging_error, NULL, |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
1
diff
changeset
|
155 _("Error: Cannot open %s for writing: %s\n"), |
1 | 156 new_fle->destfile, g_strerror (errno)); |
598 | 157 gftp_file_destroy (new_fle, 1); |
1 | 158 return; |
159 } | |
160 | |
58 | 161 fchmod (new_fle->fd, S_IRUSR | S_IWUSR); |
1 | 162 |
163 new_fle->is_fd = 1; | |
164 new_fle->done_edit = 1; | |
165 newfile = g_list_append (NULL, new_fle); | |
367 | 166 gftpui_common_add_file_transfer (fromwdata->request, towdata->request, |
167 fromwdata, towdata, newfile); | |
1 | 168 } |
169 } | |
170 | |
171 | |
48 | 172 static gftp_viewedit_data * |
173 fork_process (char *proc, char *filename, int fd, char *remote_filename, | |
767 | 174 unsigned int viewedit, unsigned int del_file, |
175 unsigned int dontupload, gftp_window_data * wdata) | |
48 | 176 { |
177 gftp_viewedit_data * newproc; | |
178 char *pos, *endpos, **argv; | |
179 pid_t ret; | |
180 int n; | |
181 | |
182 argv = NULL; | |
183 n = 0; | |
184 pos = proc; | |
185 while ((endpos = strchr (pos, ' ')) != NULL) | |
186 { | |
187 *endpos = '\0'; | |
188 n++; | |
767 | 189 argv = g_realloc (argv, (gulong) n * sizeof (char *)); |
129 | 190 argv[n - 1] = g_strdup (pos); |
48 | 191 *endpos = ' '; |
192 pos = endpos + 1; | |
193 } | |
767 | 194 argv = g_realloc (argv, (gulong) (n + 3) * sizeof (char *)); |
129 | 195 argv[n] = g_strdup (pos); |
196 argv[n + 1] = g_strdup (filename); | |
48 | 197 argv[n + 2] = NULL; |
198 | |
199 newproc = NULL; | |
200 switch ((ret = fork ())) | |
201 { | |
202 case 0: | |
203 close (fd); | |
204 execvp (argv[0], argv); | |
205 _exit (1); | |
206 case -1: | |
207 for (n = 0; argv[n] != NULL; n++) | |
208 g_free (argv[n]); | |
209 ftp_log (gftp_logging_error, NULL, | |
210 _("View: Cannot fork another process: %s\n"), g_strerror (errno)); | |
211 break; | |
212 default: | |
213 ftp_log (gftp_logging_misc, NULL, _("Running program: %s %s\n"), proc, | |
214 filename); | |
215 newproc = g_malloc0 (sizeof (*newproc)); | |
216 newproc->pid = ret; | |
217 newproc->argv = argv; | |
218 if (wdata == &window2) | |
219 { | |
220 newproc->fromwdata = &window2; | |
221 newproc->towdata = &window1; | |
222 } | |
223 else | |
224 { | |
225 newproc->fromwdata = &window1; | |
226 newproc->towdata = &window2; | |
227 } | |
368 | 228 newproc->torequest = gftp_copy_request (newproc->towdata->request); |
129 | 229 newproc->filename = g_strdup (filename); |
48 | 230 if (remote_filename != NULL) |
129 | 231 newproc->remote_filename = g_strdup (remote_filename); |
48 | 232 newproc->view = viewedit; |
233 newproc->rm = del_file; | |
234 newproc->dontupload = dontupload; | |
235 viewedit_processes = g_list_append (viewedit_processes, newproc); | |
236 } | |
237 return (newproc); | |
238 } | |
239 | |
240 | |
1 | 241 void |
767 | 242 view_file (char *filename, int fd, unsigned int viewedit, unsigned int del_file, |
243 unsigned int start_pos, unsigned int dontupload, | |
244 char *remote_filename, gftp_window_data * wdata) | |
1 | 245 { |
246 GtkWidget * dialog, * view, * table, * tempwid; | |
129 | 247 char buf[8192], *view_program, *edit_program; |
248 gftp_config_list_vars * tmplistvar; | |
1 | 249 gftp_file_extensions * tempext; |
250 gftp_viewedit_data * newproc; | |
251 GtkAdjustment * vadj; | |
252 GList * templist; | |
521 | 253 size_t stlen; |
254 int doclose; | |
1 | 255 ssize_t n; |
45 | 256 #if GTK_MAJOR_VERSION > 1 |
1 | 257 GtkTextBuffer * textbuf; |
258 GtkTextIter iter; | |
259 #endif | |
260 | |
261 doclose = 1; | |
262 stlen = strlen (filename); | |
129 | 263 gftp_lookup_global_option ("ext", &tmplistvar); |
264 for (templist = tmplistvar->list; templist != NULL; templist = templist->next) | |
1 | 265 { |
266 tempext = templist->data; | |
267 if (stlen >= tempext->stlen && | |
268 strcmp (&filename[stlen - tempext->stlen], tempext->ext) == 0) | |
269 { | |
270 if (*tempext->view_program == '\0') | |
271 break; | |
272 ftp_log (gftp_logging_misc, NULL, _("Opening %s with %s\n"), | |
273 filename, tempext->view_program); | |
274 fork_process (tempext->view_program, filename, fd, remote_filename, | |
275 viewedit, del_file, dontupload, wdata); | |
276 return; | |
277 } | |
278 } | |
279 | |
129 | 280 if (wdata != NULL) |
281 { | |
282 gftp_lookup_request_option (wdata->request, "view_program", &view_program); | |
283 gftp_lookup_request_option (wdata->request, "edit_program", &edit_program); | |
284 } | |
285 else | |
286 { | |
287 gftp_lookup_global_option ("view_program", &view_program); | |
288 gftp_lookup_global_option ("edit_program", &edit_program); | |
289 } | |
290 | |
1 | 291 if (viewedit && *view_program != '\0') |
292 { | |
293 /* Open the file with the default file viewer */ | |
294 fork_process (view_program, filename, fd, remote_filename, viewedit, | |
295 del_file, dontupload, wdata); | |
296 return; | |
297 } | |
129 | 298 else if (!viewedit && *edit_program != '\0') |
1 | 299 { |
300 /* Open the file with the default file editor */ | |
301 newproc = fork_process (edit_program, filename, fd, remote_filename, | |
302 viewedit, del_file, dontupload, wdata); | |
303 stat (filename, &newproc->st); | |
304 return; | |
305 } | |
306 | |
307 ftp_log (gftp_logging_misc, NULL, _("Viewing file %s\n"), filename); | |
308 | |
309 if (fd == 0) | |
310 { | |
311 if ((fd = open (filename, O_RDONLY)) < 0) | |
312 { | |
677 | 313 ftp_log (gftp_logging_error, NULL, |
1 | 314 _("View: Cannot open file %s: %s\n"), filename, |
315 g_strerror (errno)); | |
316 return; | |
317 } | |
318 doclose = 1; | |
319 } | |
320 else | |
321 { | |
322 lseek (fd, 0, SEEK_SET); | |
323 doclose = 0; | |
324 } | |
325 | |
326 if (del_file) | |
327 { | |
328 if (unlink (filename) == 0) | |
329 ftp_log (gftp_logging_misc, NULL, _("Successfully removed %s\n"), | |
330 filename); | |
331 else | |
332 ftp_log (gftp_logging_error, NULL, | |
333 _("Error: Could not remove file %s: %s\n"), filename, | |
334 g_strerror (errno)); | |
335 } | |
336 | |
45 | 337 #if GTK_MAJOR_VERSION == 1 |
1 | 338 dialog = gtk_dialog_new (); |
339 gtk_window_set_title (GTK_WINDOW (dialog), filename); | |
340 gtk_container_border_width (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area), | |
341 5); | |
19 | 342 gtk_box_set_homogeneous (GTK_BOX (GTK_DIALOG (dialog)->action_area), TRUE); |
343 #else | |
344 dialog = gtk_dialog_new_with_buttons (filename, NULL, 0, | |
345 GTK_STOCK_CLOSE, | |
346 GTK_RESPONSE_CLOSE, | |
347 NULL); | |
348 #endif | |
349 gtk_window_set_wmclass (GTK_WINDOW(dialog), "fileview", "gFTP"); | |
350 gtk_container_border_width (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), 5); | |
1 | 351 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 5); |
19 | 352 gtk_widget_realize (dialog); |
353 | |
354 if (gftp_icon != NULL) | |
355 { | |
356 gdk_window_set_icon (dialog->window, NULL, gftp_icon->pixmap, | |
357 gftp_icon->bitmap); | |
168 | 358 gdk_window_set_icon_name (dialog->window, gftp_version); |
19 | 359 } |
1 | 360 |
361 table = gtk_table_new (1, 2, FALSE); | |
362 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), table, TRUE, TRUE, 0); | |
363 | |
45 | 364 #if GTK_MAJOR_VERSION == 1 |
1 | 365 view = gtk_text_new (NULL, NULL); |
366 gtk_text_set_editable (GTK_TEXT (view), FALSE); | |
367 gtk_text_set_word_wrap (GTK_TEXT (view), TRUE); | |
368 | |
369 gtk_table_attach (GTK_TABLE (table), view, 0, 1, 0, 1, | |
370 GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND | GTK_SHRINK, | |
371 0, 0); | |
372 gtk_widget_show (view); | |
373 | |
374 tempwid = gtk_vscrollbar_new (GTK_TEXT (view)->vadj); | |
375 gtk_table_attach (GTK_TABLE (table), tempwid, 1, 2, 0, 1, | |
376 GTK_FILL, GTK_EXPAND | GTK_FILL | GTK_SHRINK, 0, 0); | |
377 gtk_widget_show (tempwid); | |
378 | |
379 vadj = GTK_TEXT (view)->vadj; | |
380 #else | |
381 view = gtk_text_view_new (); | |
382 gtk_text_view_set_editable (GTK_TEXT_VIEW (view), FALSE); | |
383 gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (view), FALSE); | |
384 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (view), GTK_WRAP_WORD); | |
385 | |
386 tempwid = gtk_scrolled_window_new (NULL, NULL); | |
387 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (tempwid), | |
388 GTK_POLICY_AUTOMATIC, | |
389 GTK_POLICY_AUTOMATIC); | |
390 | |
391 gtk_container_add (GTK_CONTAINER (tempwid), view); | |
392 gtk_widget_show (view); | |
393 | |
394 gtk_table_attach (GTK_TABLE (table), tempwid, 0, 1, 0, 1, | |
395 GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND | GTK_SHRINK, | |
396 0, 0); | |
397 gtk_widget_show (tempwid); | |
398 | |
399 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (tempwid)); | |
400 #endif | |
401 gtk_widget_set_size_request (table, 500, 400); | |
402 gtk_widget_show (table); | |
403 | |
45 | 404 #if GTK_MAJOR_VERSION == 1 |
1 | 405 tempwid = gtk_button_new_with_label (_(" Close ")); |
406 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area), tempwid, | |
407 FALSE, FALSE, 0); | |
408 gtk_signal_connect_object (GTK_OBJECT (tempwid), "clicked", | |
409 GTK_SIGNAL_FUNC (gtk_widget_destroy), | |
410 GTK_OBJECT (dialog)); | |
411 gtk_widget_show (tempwid); | |
19 | 412 #else |
413 g_signal_connect_swapped (GTK_OBJECT (dialog), "response", | |
414 G_CALLBACK (gtk_widget_destroy), | |
415 GTK_OBJECT (dialog)); | |
416 #endif | |
1 | 417 |
418 buf[sizeof (buf) - 1] = '\0'; | |
444 | 419 while ((n = read (fd, buf, sizeof (buf) - 1)) > 0) |
1 | 420 { |
421 buf[n] = '\0'; | |
45 | 422 #if GTK_MAJOR_VERSION == 1 |
1 | 423 gtk_text_insert (GTK_TEXT (view), NULL, NULL, NULL, buf, -1); |
424 #else | |
425 textbuf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)); | |
588 | 426 gtk_text_buffer_get_iter_at_offset (textbuf, &iter, -1); |
427 gtk_text_buffer_insert (textbuf, &iter, buf, -1); | |
1 | 428 #endif |
429 } | |
430 | |
431 if (doclose) | |
432 close (fd); | |
433 | |
434 gtk_widget_show (dialog); | |
435 | |
436 if (!start_pos) | |
437 gtk_adjustment_set_value (vadj, vadj->upper); | |
438 } | |
439 |