Mercurial > gftp.yaz
annotate src/gtk/view_dialog.c @ 444:d469882002d9
2004-3-21 Brian Masney <masneyb@gftp.org>
* src/gtk/view_dialog.c src/gtk/menu-items.c - cleanups to the view
log code
author | masneyb |
---|---|
date | Sun, 21 Mar 2004 13:22:41 +0000 |
parents | 38bfc112ab46 |
children | 39e9945288ea |
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; | |
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 | |
45 if (curfle->isdir) | |
46 { | |
47 ftp_log (gftp_logging_misc, NULL, | |
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 { |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
1
diff
changeset
|
75 ftp_log (gftp_logging_misc, NULL, |
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)); |
78 free_fdata (new_fle); | |
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 { | |
113 ftp_log (gftp_logging_misc, NULL, | |
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 | |
124 if (curfle->isdir) | |
125 { | |
126 ftp_log (gftp_logging_misc, NULL, | |
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 { |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
1
diff
changeset
|
154 ftp_log (gftp_logging_misc, NULL, |
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)); |
157 free_fdata (new_fle); | |
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, | |
174 int viewedit, int del_file, int dontupload, | |
175 gftp_window_data * wdata) | |
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++; | |
189 argv = g_realloc (argv, n * sizeof (char *)); | |
129 | 190 argv[n - 1] = g_strdup (pos); |
48 | 191 *endpos = ' '; |
192 pos = endpos + 1; | |
193 } | |
194 argv = g_realloc (argv, (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 |
242 view_file (char *filename, int fd, int viewedit, int del_file, int start_pos, | |
243 int dontupload, char *remote_filename, gftp_window_data * wdata) | |
244 { | |
245 GtkWidget * dialog, * view, * table, * tempwid; | |
129 | 246 char buf[8192], *view_program, *edit_program; |
247 gftp_config_list_vars * tmplistvar; | |
1 | 248 gftp_file_extensions * tempext; |
249 gftp_viewedit_data * newproc; | |
250 GtkAdjustment * vadj; | |
251 int stlen, doclose; | |
252 GList * templist; | |
253 ssize_t n; | |
45 | 254 #if GTK_MAJOR_VERSION > 1 |
1 | 255 GtkTextBuffer * textbuf; |
256 GtkTextIter iter; | |
257 guint len; | |
258 #endif | |
259 | |
260 doclose = 1; | |
261 stlen = strlen (filename); | |
129 | 262 gftp_lookup_global_option ("ext", &tmplistvar); |
263 for (templist = tmplistvar->list; templist != NULL; templist = templist->next) | |
1 | 264 { |
265 tempext = templist->data; | |
266 if (stlen >= tempext->stlen && | |
267 strcmp (&filename[stlen - tempext->stlen], tempext->ext) == 0) | |
268 { | |
269 if (*tempext->view_program == '\0') | |
270 break; | |
271 ftp_log (gftp_logging_misc, NULL, _("Opening %s with %s\n"), | |
272 filename, tempext->view_program); | |
273 fork_process (tempext->view_program, filename, fd, remote_filename, | |
274 viewedit, del_file, dontupload, wdata); | |
275 return; | |
276 } | |
277 } | |
278 | |
129 | 279 if (wdata != NULL) |
280 { | |
281 gftp_lookup_request_option (wdata->request, "view_program", &view_program); | |
282 gftp_lookup_request_option (wdata->request, "edit_program", &edit_program); | |
283 } | |
284 else | |
285 { | |
286 gftp_lookup_global_option ("view_program", &view_program); | |
287 gftp_lookup_global_option ("edit_program", &edit_program); | |
288 } | |
289 | |
1 | 290 if (viewedit && *view_program != '\0') |
291 { | |
292 /* Open the file with the default file viewer */ | |
293 fork_process (view_program, filename, fd, remote_filename, viewedit, | |
294 del_file, dontupload, wdata); | |
295 return; | |
296 } | |
129 | 297 else if (!viewedit && *edit_program != '\0') |
1 | 298 { |
299 /* Open the file with the default file editor */ | |
300 newproc = fork_process (edit_program, filename, fd, remote_filename, | |
301 viewedit, del_file, dontupload, wdata); | |
302 stat (filename, &newproc->st); | |
303 return; | |
304 } | |
305 | |
306 ftp_log (gftp_logging_misc, NULL, _("Viewing file %s\n"), filename); | |
307 | |
308 if (fd == 0) | |
309 { | |
310 if ((fd = open (filename, O_RDONLY)) < 0) | |
311 { | |
312 ftp_log (gftp_logging_misc, NULL, | |
313 _("View: Cannot open file %s: %s\n"), filename, | |
314 g_strerror (errno)); | |
315 return; | |
316 } | |
317 doclose = 1; | |
318 } | |
319 else | |
320 { | |
321 lseek (fd, 0, SEEK_SET); | |
322 doclose = 0; | |
323 } | |
324 | |
325 if (del_file) | |
326 { | |
327 if (unlink (filename) == 0) | |
328 ftp_log (gftp_logging_misc, NULL, _("Successfully removed %s\n"), | |
329 filename); | |
330 else | |
331 ftp_log (gftp_logging_error, NULL, | |
332 _("Error: Could not remove file %s: %s\n"), filename, | |
333 g_strerror (errno)); | |
334 } | |
335 | |
45 | 336 #if GTK_MAJOR_VERSION == 1 |
1 | 337 dialog = gtk_dialog_new (); |
338 gtk_window_set_title (GTK_WINDOW (dialog), filename); | |
339 gtk_container_border_width (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area), | |
340 5); | |
19 | 341 gtk_box_set_homogeneous (GTK_BOX (GTK_DIALOG (dialog)->action_area), TRUE); |
342 #else | |
343 dialog = gtk_dialog_new_with_buttons (filename, NULL, 0, | |
344 GTK_STOCK_CLOSE, | |
345 GTK_RESPONSE_CLOSE, | |
346 NULL); | |
347 #endif | |
348 gtk_window_set_wmclass (GTK_WINDOW(dialog), "fileview", "gFTP"); | |
349 gtk_container_border_width (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), 5); | |
1 | 350 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 5); |
19 | 351 gtk_widget_realize (dialog); |
352 | |
353 if (gftp_icon != NULL) | |
354 { | |
355 gdk_window_set_icon (dialog->window, NULL, gftp_icon->pixmap, | |
356 gftp_icon->bitmap); | |
168 | 357 gdk_window_set_icon_name (dialog->window, gftp_version); |
19 | 358 } |
1 | 359 |
360 table = gtk_table_new (1, 2, FALSE); | |
361 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), table, TRUE, TRUE, 0); | |
362 | |
45 | 363 #if GTK_MAJOR_VERSION == 1 |
1 | 364 view = gtk_text_new (NULL, NULL); |
365 gtk_text_set_editable (GTK_TEXT (view), FALSE); | |
366 gtk_text_set_word_wrap (GTK_TEXT (view), TRUE); | |
367 | |
368 gtk_table_attach (GTK_TABLE (table), view, 0, 1, 0, 1, | |
369 GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND | GTK_SHRINK, | |
370 0, 0); | |
371 gtk_widget_show (view); | |
372 | |
373 tempwid = gtk_vscrollbar_new (GTK_TEXT (view)->vadj); | |
374 gtk_table_attach (GTK_TABLE (table), tempwid, 1, 2, 0, 1, | |
375 GTK_FILL, GTK_EXPAND | GTK_FILL | GTK_SHRINK, 0, 0); | |
376 gtk_widget_show (tempwid); | |
377 | |
378 vadj = GTK_TEXT (view)->vadj; | |
379 #else | |
380 view = gtk_text_view_new (); | |
381 gtk_text_view_set_editable (GTK_TEXT_VIEW (view), FALSE); | |
382 gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (view), FALSE); | |
383 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (view), GTK_WRAP_WORD); | |
384 | |
385 tempwid = gtk_scrolled_window_new (NULL, NULL); | |
386 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (tempwid), | |
387 GTK_POLICY_AUTOMATIC, | |
388 GTK_POLICY_AUTOMATIC); | |
389 | |
390 gtk_container_add (GTK_CONTAINER (tempwid), view); | |
391 gtk_widget_show (view); | |
392 | |
393 gtk_table_attach (GTK_TABLE (table), tempwid, 0, 1, 0, 1, | |
394 GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND | GTK_SHRINK, | |
395 0, 0); | |
396 gtk_widget_show (tempwid); | |
397 | |
398 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (tempwid)); | |
399 #endif | |
400 gtk_widget_set_size_request (table, 500, 400); | |
401 gtk_widget_show (table); | |
402 | |
45 | 403 #if GTK_MAJOR_VERSION == 1 |
1 | 404 tempwid = gtk_button_new_with_label (_(" Close ")); |
405 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area), tempwid, | |
406 FALSE, FALSE, 0); | |
407 gtk_signal_connect_object (GTK_OBJECT (tempwid), "clicked", | |
408 GTK_SIGNAL_FUNC (gtk_widget_destroy), | |
409 GTK_OBJECT (dialog)); | |
410 gtk_widget_show (tempwid); | |
19 | 411 #else |
412 g_signal_connect_swapped (GTK_OBJECT (dialog), "response", | |
413 G_CALLBACK (gtk_widget_destroy), | |
414 GTK_OBJECT (dialog)); | |
415 #endif | |
1 | 416 |
417 buf[sizeof (buf) - 1] = '\0'; | |
444 | 418 while ((n = read (fd, buf, sizeof (buf) - 1)) > 0) |
1 | 419 { |
420 buf[n] = '\0'; | |
45 | 421 #if GTK_MAJOR_VERSION == 1 |
1 | 422 gtk_text_insert (GTK_TEXT (view), NULL, NULL, NULL, buf, -1); |
423 #else | |
424 textbuf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)); | |
425 len = gtk_text_buffer_get_char_count (textbuf); | |
444 | 426 gtk_text_buffer_get_iter_at_offset (textbuf, &iter, len); |
427 gtk_text_buffer_insert (textbuf, &iter, buf, n); | |
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 |