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 compression_run (const gchar *uri,
180 EvCompressionType type,
185 gchar *uri_dst = NULL;
186 gchar *filename, *filename_dst;
190 if (type == EV_COMPRESSION_NONE)
193 cmd = g_find_program_in_path ((type == EV_COMPRESSION_BZIP2) ? BZIPCOMMAND : GZIPCOMMAND);
197 filename = g_filename_from_uri (uri, NULL, NULL);
203 filename_dst = g_build_filename (ev_tmp_dir (), "evinceXXXXXX", NULL);
204 fd = g_mkstemp (filename_dst);
208 g_free (filename_dst);
213 argv[1] = compress ? "-c" : "-cd";
217 if (g_spawn_async_with_pipes (NULL, argv, NULL,
218 G_SPAWN_STDERR_TO_DEV_NULL,
220 NULL, &pout, NULL, error)) {
221 GIOChannel *in, *out;
222 gchar buf[BUFFER_SIZE];
223 GIOStatus read_st, write_st;
224 gsize bytes_read, bytes_written;
226 in = g_io_channel_unix_new (pout);
227 g_io_channel_set_encoding (in, NULL, NULL);
228 out = g_io_channel_unix_new (fd);
229 g_io_channel_set_encoding (out, NULL, NULL);
232 read_st = g_io_channel_read_chars (in, buf,
236 if (read_st == G_IO_STATUS_NORMAL) {
237 write_st = g_io_channel_write_chars (out, buf,
241 if (write_st == G_IO_STATUS_ERROR)
243 } else if (read_st == G_IO_STATUS_ERROR) {
246 } while (bytes_read > 0);
248 g_io_channel_unref (in);
249 g_io_channel_unref (out);
254 if (*error == NULL) {
255 uri_dst = g_filename_to_uri (filename_dst, NULL, NULL);
260 g_free (filename_dst);
266 ev_file_uncompress (const gchar *uri,
267 EvCompressionType type,
270 g_return_val_if_fail (uri != NULL, NULL);
272 return compression_run (uri, type, FALSE, error);
276 ev_file_compress (const gchar *uri,
277 EvCompressionType type,
280 g_return_val_if_fail (uri != NULL, NULL);
282 return compression_run (uri, type, TRUE, error);