Mercurial > gftp.yaz
annotate src/gtk/view_dialog.c @ 934:5cd6d60d43a1
Updated Arabic Translation by Khaled Hosny.
author | djihed |
---|---|
date | Mon, 22 Oct 2007 22:23:55 +0000 |
parents | c2be64b831d9 |
children |
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); |
905 | 155 |
156 if (wdata != NULL) | |
157 { | |
158 if ((argv[n + 1] = gftp_filename_from_utf8 (wdata->request, filename, NULL)) == NULL) | |
159 argv[n + 1] = g_strdup (filename); | |
160 } | |
161 else | |
162 argv[n + 1] = g_strdup (filename); | |
163 | |
48 | 164 argv[n + 2] = NULL; |
165 | |
166 newproc = NULL; | |
167 switch ((ret = fork ())) | |
168 { | |
169 case 0: | |
170 close (fd); | |
171 execvp (argv[0], argv); | |
172 _exit (1); | |
173 case -1: | |
174 for (n = 0; argv[n] != NULL; n++) | |
175 g_free (argv[n]); | |
176 ftp_log (gftp_logging_error, NULL, | |
177 _("View: Cannot fork another process: %s\n"), g_strerror (errno)); | |
178 break; | |
179 default: | |
180 ftp_log (gftp_logging_misc, NULL, _("Running program: %s %s\n"), proc, | |
181 filename); | |
182 newproc = g_malloc0 (sizeof (*newproc)); | |
183 newproc->pid = ret; | |
184 newproc->argv = argv; | |
185 if (wdata == &window2) | |
186 { | |
187 newproc->fromwdata = &window2; | |
188 newproc->towdata = &window1; | |
189 } | |
190 else | |
191 { | |
192 newproc->fromwdata = &window1; | |
193 newproc->towdata = &window2; | |
194 } | |
368 | 195 newproc->torequest = gftp_copy_request (newproc->towdata->request); |
129 | 196 newproc->filename = g_strdup (filename); |
48 | 197 if (remote_filename != NULL) |
129 | 198 newproc->remote_filename = g_strdup (remote_filename); |
48 | 199 newproc->view = viewedit; |
200 newproc->rm = del_file; | |
201 newproc->dontupload = dontupload; | |
202 viewedit_processes = g_list_append (viewedit_processes, newproc); | |
203 } | |
204 return (newproc); | |
205 } | |
206 | |
207 | |
1 | 208 void |
767 | 209 view_file (char *filename, int fd, unsigned int viewedit, unsigned int del_file, |
210 unsigned int start_pos, unsigned int dontupload, | |
211 char *remote_filename, gftp_window_data * wdata) | |
1 | 212 { |
213 GtkWidget * dialog, * view, * table, * tempwid; | |
129 | 214 char buf[8192], *view_program, *edit_program; |
215 gftp_config_list_vars * tmplistvar; | |
1 | 216 gftp_file_extensions * tempext; |
217 gftp_viewedit_data * newproc; | |
218 GtkAdjustment * vadj; | |
219 GList * templist; | |
521 | 220 size_t stlen; |
221 int doclose; | |
1 | 222 ssize_t n; |
905 | 223 char * non_utf8; |
45 | 224 #if GTK_MAJOR_VERSION > 1 |
1 | 225 GtkTextBuffer * textbuf; |
226 GtkTextIter iter; | |
227 #endif | |
228 | |
229 doclose = 1; | |
230 stlen = strlen (filename); | |
129 | 231 gftp_lookup_global_option ("ext", &tmplistvar); |
232 for (templist = tmplistvar->list; templist != NULL; templist = templist->next) | |
1 | 233 { |
234 tempext = templist->data; | |
235 if (stlen >= tempext->stlen && | |
236 strcmp (&filename[stlen - tempext->stlen], tempext->ext) == 0) | |
237 { | |
238 if (*tempext->view_program == '\0') | |
239 break; | |
240 ftp_log (gftp_logging_misc, NULL, _("Opening %s with %s\n"), | |
241 filename, tempext->view_program); | |
242 fork_process (tempext->view_program, filename, fd, remote_filename, | |
243 viewedit, del_file, dontupload, wdata); | |
244 return; | |
245 } | |
246 } | |
247 | |
129 | 248 if (wdata != NULL) |
249 { | |
250 gftp_lookup_request_option (wdata->request, "view_program", &view_program); | |
251 gftp_lookup_request_option (wdata->request, "edit_program", &edit_program); | |
905 | 252 if ((non_utf8 = gftp_filename_from_utf8 (wdata->request, filename, NULL)) == NULL) /* freeme later! */ |
253 non_utf8 = filename; | |
129 | 254 } |
255 else | |
256 { | |
257 gftp_lookup_global_option ("view_program", &view_program); | |
258 gftp_lookup_global_option ("edit_program", &edit_program); | |
905 | 259 non_utf8 = filename; |
129 | 260 } |
261 | |
1 | 262 if (viewedit && *view_program != '\0') |
263 { | |
264 /* Open the file with the default file viewer */ | |
265 fork_process (view_program, filename, fd, remote_filename, viewedit, | |
266 del_file, dontupload, wdata); | |
905 | 267 if (non_utf8 != filename && non_utf8) |
268 g_free (non_utf8); | |
1 | 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); | |
905 | 276 stat (non_utf8, &newproc->st); |
277 if (non_utf8 != filename && non_utf8) | |
278 g_free (non_utf8); | |
1 | 279 return; |
280 } | |
281 | |
282 ftp_log (gftp_logging_misc, NULL, _("Viewing file %s\n"), filename); | |
283 | |
284 if (fd == 0) | |
285 { | |
905 | 286 if ((fd = open (non_utf8, O_RDONLY)) < 0) |
1 | 287 { |
677 | 288 ftp_log (gftp_logging_error, NULL, |
905 | 289 _("View: Cannot open file %s: %s\n"), non_utf8, |
1 | 290 g_strerror (errno)); |
905 | 291 if (non_utf8 != filename && non_utf8) |
292 g_free (non_utf8); | |
1 | 293 return; |
294 } | |
295 doclose = 1; | |
296 } | |
297 else | |
298 { | |
299 lseek (fd, 0, SEEK_SET); | |
300 doclose = 0; | |
301 } | |
302 | |
303 if (del_file) | |
304 { | |
905 | 305 if (unlink (non_utf8) == 0) |
1 | 306 ftp_log (gftp_logging_misc, NULL, _("Successfully removed %s\n"), |
307 filename); | |
308 else | |
309 ftp_log (gftp_logging_error, NULL, | |
310 _("Error: Could not remove file %s: %s\n"), filename, | |
311 g_strerror (errno)); | |
312 } | |
313 | |
905 | 314 if (non_utf8 != filename && non_utf8) |
315 g_free (non_utf8); | |
316 | |
45 | 317 #if GTK_MAJOR_VERSION == 1 |
1 | 318 dialog = gtk_dialog_new (); |
319 gtk_window_set_title (GTK_WINDOW (dialog), filename); | |
320 gtk_container_border_width (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area), | |
321 5); | |
19 | 322 gtk_box_set_homogeneous (GTK_BOX (GTK_DIALOG (dialog)->action_area), TRUE); |
323 #else | |
324 dialog = gtk_dialog_new_with_buttons (filename, NULL, 0, | |
325 GTK_STOCK_CLOSE, | |
326 GTK_RESPONSE_CLOSE, | |
327 NULL); | |
328 #endif | |
329 gtk_window_set_wmclass (GTK_WINDOW(dialog), "fileview", "gFTP"); | |
330 gtk_container_border_width (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), 5); | |
1 | 331 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 5); |
19 | 332 gtk_widget_realize (dialog); |
333 | |
334 if (gftp_icon != NULL) | |
335 { | |
336 gdk_window_set_icon (dialog->window, NULL, gftp_icon->pixmap, | |
337 gftp_icon->bitmap); | |
168 | 338 gdk_window_set_icon_name (dialog->window, gftp_version); |
19 | 339 } |
1 | 340 |
341 table = gtk_table_new (1, 2, FALSE); | |
342 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), table, TRUE, TRUE, 0); | |
343 | |
45 | 344 #if GTK_MAJOR_VERSION == 1 |
1 | 345 view = gtk_text_new (NULL, NULL); |
346 gtk_text_set_editable (GTK_TEXT (view), FALSE); | |
347 gtk_text_set_word_wrap (GTK_TEXT (view), TRUE); | |
348 | |
349 gtk_table_attach (GTK_TABLE (table), view, 0, 1, 0, 1, | |
350 GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND | GTK_SHRINK, | |
351 0, 0); | |
352 gtk_widget_show (view); | |
353 | |
354 tempwid = gtk_vscrollbar_new (GTK_TEXT (view)->vadj); | |
355 gtk_table_attach (GTK_TABLE (table), tempwid, 1, 2, 0, 1, | |
356 GTK_FILL, GTK_EXPAND | GTK_FILL | GTK_SHRINK, 0, 0); | |
357 gtk_widget_show (tempwid); | |
358 | |
359 vadj = GTK_TEXT (view)->vadj; | |
360 #else | |
361 view = gtk_text_view_new (); | |
362 gtk_text_view_set_editable (GTK_TEXT_VIEW (view), FALSE); | |
363 gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (view), FALSE); | |
364 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (view), GTK_WRAP_WORD); | |
365 | |
366 tempwid = gtk_scrolled_window_new (NULL, NULL); | |
367 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (tempwid), | |
368 GTK_POLICY_AUTOMATIC, | |
369 GTK_POLICY_AUTOMATIC); | |
370 | |
371 gtk_container_add (GTK_CONTAINER (tempwid), view); | |
372 gtk_widget_show (view); | |
373 | |
374 gtk_table_attach (GTK_TABLE (table), tempwid, 0, 1, 0, 1, | |
375 GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND | GTK_SHRINK, | |
376 0, 0); | |
377 gtk_widget_show (tempwid); | |
378 | |
379 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (tempwid)); | |
380 #endif | |
381 gtk_widget_set_size_request (table, 500, 400); | |
382 gtk_widget_show (table); | |
383 | |
45 | 384 #if GTK_MAJOR_VERSION == 1 |
1 | 385 tempwid = gtk_button_new_with_label (_(" Close ")); |
386 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area), tempwid, | |
387 FALSE, FALSE, 0); | |
388 gtk_signal_connect_object (GTK_OBJECT (tempwid), "clicked", | |
389 GTK_SIGNAL_FUNC (gtk_widget_destroy), | |
390 GTK_OBJECT (dialog)); | |
391 gtk_widget_show (tempwid); | |
19 | 392 #else |
393 g_signal_connect_swapped (GTK_OBJECT (dialog), "response", | |
394 G_CALLBACK (gtk_widget_destroy), | |
395 GTK_OBJECT (dialog)); | |
396 #endif | |
1 | 397 |
398 buf[sizeof (buf) - 1] = '\0'; | |
444 | 399 while ((n = read (fd, buf, sizeof (buf) - 1)) > 0) |
1 | 400 { |
401 buf[n] = '\0'; | |
45 | 402 #if GTK_MAJOR_VERSION == 1 |
1 | 403 gtk_text_insert (GTK_TEXT (view), NULL, NULL, NULL, buf, -1); |
404 #else | |
405 textbuf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)); | |
588 | 406 gtk_text_buffer_get_iter_at_offset (textbuf, &iter, -1); |
407 gtk_text_buffer_insert (textbuf, &iter, buf, -1); | |
1 | 408 #endif |
409 } | |
410 | |
411 if (doclose) | |
412 close (fd); | |
413 | |
414 gtk_widget_show (dialog); | |
415 | |
416 if (!start_pos) | |
417 gtk_adjustment_set_value (vadj, vadj->upper); | |
418 } | |
419 |