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>
38 #include <libgnome/gnome-init.h>
41 #include "ev-file-helpers.h"
43 static gchar *dot_dir = NULL;
44 static gchar *tmp_dir = NULL;
45 static gint count = 0;
48 ensure_dir_exists (const char *dir)
50 if (g_file_test (dir, G_FILE_TEST_IS_DIR))
53 if (g_mkdir_with_parents (dir, 488) == 0)
57 return g_file_test (dir, G_FILE_TEST_IS_DIR);
59 g_warning ("Failed to create directory %s: %s", dir, strerror (errno));
66 if (dot_dir == NULL) {
70 dot_dir = g_build_filename (gnome_user_dir_get (),
74 dot_dir = g_build_filename (g_get_user_config_dir (),
79 exists = ensure_dir_exists (dot_dir);
90 if (tmp_dir == NULL) {
94 dirname = g_strdup_printf ("evince-%u", getpid ());
95 tmp_dir = g_build_filename (g_get_tmp_dir (),
100 exists = ensure_dir_exists (tmp_dir);
108 ev_file_helpers_init (void)
113 ev_file_helpers_shutdown (void)
126 ev_tmp_filename (const gchar *prefix)
129 gchar *filename = NULL;
132 if (filename != NULL)
135 basename = g_strdup_printf ("%s-%d",
136 prefix ? prefix : "document",
139 filename = g_build_filename (ev_tmp_dir (),
143 } while (g_file_test (filename, G_FILE_TEST_EXISTS));
148 /* Remove a local temp file created by evince */
150 ev_tmp_filename_unlink (const gchar *filename)
152 const gchar *tempdir;
157 tempdir = g_get_tmp_dir ();
158 if (g_ascii_strncasecmp (filename, tempdir, strlen (tempdir)) == 0) {
164 ev_tmp_uri_unlink (const gchar *uri)
166 GnomeVFSURI *vfs_uri;
172 vfs_uri = gnome_vfs_uri_new (uri);
173 if (!gnome_vfs_uri_is_local (vfs_uri)) {
174 g_warning ("Attempting to delete non local uri: %s\n", uri);
175 gnome_vfs_uri_unref (vfs_uri);
178 gnome_vfs_uri_unref (vfs_uri);
180 filename = g_filename_from_uri (uri, NULL, NULL);
181 ev_tmp_filename_unlink (filename);
186 ev_xfer_uri_simple (const char *from,
190 GnomeVFSResult result;
191 GnomeVFSURI *source_uri;
192 GnomeVFSURI *target_uri;
197 source_uri = gnome_vfs_uri_new (from);
198 target_uri = gnome_vfs_uri_new (to);
200 result = gnome_vfs_xfer_uri (source_uri, target_uri,
201 GNOME_VFS_XFER_DEFAULT | GNOME_VFS_XFER_FOLLOW_LINKS,
202 GNOME_VFS_XFER_ERROR_MODE_ABORT,
203 GNOME_VFS_XFER_OVERWRITE_MODE_REPLACE,
206 gnome_vfs_uri_unref (target_uri);
207 gnome_vfs_uri_unref (source_uri);
209 if (result != GNOME_VFS_OK)
213 gnome_vfs_result_to_string (result));
214 return (result == GNOME_VFS_OK);
218 /* Compressed files support */
219 #define BZIPCOMMAND "bzip2"
220 #define GZIPCOMMAND "gzip"
222 #define BUFFER_SIZE 1024
225 compression_run (const gchar *uri,
226 EvCompressionType type,
231 gchar *uri_dst = NULL;
232 gchar *filename, *filename_dst;
236 if (type == EV_COMPRESSION_NONE)
239 cmd = g_find_program_in_path ((type == EV_COMPRESSION_BZIP2) ? BZIPCOMMAND : GZIPCOMMAND);
243 filename = g_filename_from_uri (uri, NULL, NULL);
249 filename_dst = g_build_filename (ev_tmp_dir (), "evinceXXXXXX", NULL);
250 fd = g_mkstemp (filename_dst);
254 g_free (filename_dst);
259 argv[1] = compress ? "-c" : "-cd";
263 if (g_spawn_async_with_pipes (NULL, argv, NULL,
264 G_SPAWN_STDERR_TO_DEV_NULL,
266 NULL, &pout, NULL, error)) {
267 GIOChannel *in, *out;
268 gchar buf[BUFFER_SIZE];
269 GIOStatus read_st, write_st;
270 gsize bytes_read, bytes_written;
272 in = g_io_channel_unix_new (pout);
273 g_io_channel_set_encoding (in, NULL, NULL);
274 out = g_io_channel_unix_new (fd);
275 g_io_channel_set_encoding (out, NULL, NULL);
278 read_st = g_io_channel_read_chars (in, buf,
282 if (read_st == G_IO_STATUS_NORMAL) {
283 write_st = g_io_channel_write_chars (out, buf,
287 if (write_st == G_IO_STATUS_ERROR)
289 } else if (read_st == G_IO_STATUS_ERROR) {
292 } while (bytes_read > 0);
294 g_io_channel_unref (in);
295 g_io_channel_unref (out);
300 if (*error == NULL) {
301 uri_dst = g_filename_to_uri (filename_dst, NULL, NULL);
306 g_free (filename_dst);
312 ev_file_uncompress (const gchar *uri,
313 EvCompressionType type,
316 g_return_val_if_fail (uri != NULL, NULL);
318 return compression_run (uri, type, FALSE, error);
322 ev_file_compress (const gchar *uri,
323 EvCompressionType type,
326 g_return_val_if_fail (uri != NULL, NULL);
328 return compression_run (uri, type, TRUE, error);