2 * Copyright (C) 2002 Jorn Baayen
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <sys/types.h>
30 #include <glib/gstdio.h>
32 #include <libgnomevfs/gnome-vfs-uri.h>
33 #include <libgnomevfs/gnome-vfs-utils.h>
34 #include <libgnomevfs/gnome-vfs-ops.h>
35 #include <libgnomevfs/gnome-vfs-xfer.h>
36 #include <libgnome/gnome-init.h>
38 #include "ev-file-helpers.h"
40 static gchar *dot_dir = NULL;
41 static gchar *tmp_dir = NULL;
42 static gint count = 0;
45 ensure_dir_exists (const char *dir)
47 if (g_file_test (dir, G_FILE_TEST_IS_DIR))
50 if (g_mkdir (dir, 488) == 0)
54 return g_file_test (dir, G_FILE_TEST_IS_DIR);
56 g_warning ("Failed to create directory %s: %s", dir, strerror (errno));
63 if (dot_dir == NULL) {
66 dot_dir = g_build_filename (gnome_user_dir_get (),
70 exists = ensure_dir_exists (dot_dir);
81 if (tmp_dir == NULL) {
85 dirname = g_strdup_printf ("evince-%u", getpid ());
86 tmp_dir = g_build_filename (g_get_tmp_dir (),
91 exists = ensure_dir_exists (tmp_dir);
99 ev_file_helpers_init (void)
104 ev_file_helpers_shutdown (void)
117 ev_tmp_filename (const gchar *prefix)
120 gchar *filename = NULL;
123 if (filename != NULL)
126 basename = g_strdup_printf ("%s-%d",
127 prefix ? prefix : "document",
130 filename = g_build_filename (ev_tmp_dir (),
134 } while (g_file_test (filename, G_FILE_TEST_EXISTS));
140 ev_xfer_uri_simple (const char *from,
144 GnomeVFSResult result;
145 GnomeVFSURI *source_uri;
146 GnomeVFSURI *target_uri;
151 source_uri = gnome_vfs_uri_new (from);
152 target_uri = gnome_vfs_uri_new (to);
154 result = gnome_vfs_xfer_uri (source_uri, target_uri,
155 GNOME_VFS_XFER_DEFAULT | GNOME_VFS_XFER_FOLLOW_LINKS,
156 GNOME_VFS_XFER_ERROR_MODE_ABORT,
157 GNOME_VFS_XFER_OVERWRITE_MODE_REPLACE,
160 gnome_vfs_uri_unref (target_uri);
161 gnome_vfs_uri_unref (source_uri);
163 if (result != GNOME_VFS_OK)
167 gnome_vfs_result_to_string (result));
168 return (result == GNOME_VFS_OK);
172 /* Compressed files support */
173 #define BZIPCOMMAND "bzip2"
174 #define GZIPCOMMAND "gzip"
176 #define BUFFER_SIZE 1024
179 ev_file_uncompress (const gchar *uri,
180 EvCompressionType type,
184 gchar *uri_unc = NULL;
185 gchar *filename, *filename_unc;
189 g_return_val_if_fail (uri != NULL, NULL);
191 if (type == EV_COMPRESSION_NONE)
194 cmd = g_find_program_in_path ((type == EV_COMPRESSION_BZIP2) ? BZIPCOMMAND : GZIPCOMMAND);
199 filename = g_filename_from_uri (uri, NULL, NULL);
205 filename_unc = g_build_filename (ev_tmp_dir (), "evinceXXXXXX", NULL);
206 fd = g_mkstemp (filename_unc);
210 g_free (filename_unc);
217 argv[3] = filename_unc;
220 if (g_spawn_async_with_pipes (NULL, argv, NULL,
221 G_SPAWN_STDERR_TO_DEV_NULL,
223 NULL, &pout, NULL, error)) {
224 GIOChannel *in, *out;
225 gchar buf[BUFFER_SIZE];
226 GIOStatus read_st, write_st;
227 gsize bytes_read, bytes_written;
229 in = g_io_channel_unix_new (pout);
230 g_io_channel_set_encoding (in, NULL, NULL);
231 out = g_io_channel_unix_new (fd);
232 g_io_channel_set_encoding (out, NULL, NULL);
235 read_st = g_io_channel_read_chars (in, buf,
239 if (read_st == G_IO_STATUS_NORMAL) {
240 write_st = g_io_channel_write_chars (out, buf,
244 if (write_st == G_IO_STATUS_ERROR)
246 } else if (read_st == G_IO_STATUS_ERROR) {
249 } while (bytes_read > 0);
251 g_io_channel_unref (in);
252 g_io_channel_unref (out);
257 if (*error == NULL) {
258 uri_unc = g_filename_to_uri (filename_unc,
264 g_free (filename_unc);