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