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