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