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 (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));
149 ev_xfer_uri_simple (const char *from,
153 GnomeVFSResult result;
154 GnomeVFSURI *source_uri;
155 GnomeVFSURI *target_uri;
160 source_uri = gnome_vfs_uri_new (from);
161 target_uri = gnome_vfs_uri_new (to);
163 result = gnome_vfs_xfer_uri (source_uri, target_uri,
164 GNOME_VFS_XFER_DEFAULT | GNOME_VFS_XFER_FOLLOW_LINKS,
165 GNOME_VFS_XFER_ERROR_MODE_ABORT,
166 GNOME_VFS_XFER_OVERWRITE_MODE_REPLACE,
169 gnome_vfs_uri_unref (target_uri);
170 gnome_vfs_uri_unref (source_uri);
172 if (result != GNOME_VFS_OK)
176 gnome_vfs_result_to_string (result));
177 return (result == GNOME_VFS_OK);
181 /* Compressed files support */
182 #define BZIPCOMMAND "bzip2"
183 #define GZIPCOMMAND "gzip"
185 #define BUFFER_SIZE 1024
188 compression_run (const gchar *uri,
189 EvCompressionType type,
194 gchar *uri_dst = NULL;
195 gchar *filename, *filename_dst;
199 if (type == EV_COMPRESSION_NONE)
202 cmd = g_find_program_in_path ((type == EV_COMPRESSION_BZIP2) ? BZIPCOMMAND : GZIPCOMMAND);
206 filename = g_filename_from_uri (uri, NULL, NULL);
212 filename_dst = g_build_filename (ev_tmp_dir (), "evinceXXXXXX", NULL);
213 fd = g_mkstemp (filename_dst);
217 g_free (filename_dst);
222 argv[1] = compress ? "-c" : "-cd";
226 if (g_spawn_async_with_pipes (NULL, argv, NULL,
227 G_SPAWN_STDERR_TO_DEV_NULL,
229 NULL, &pout, NULL, error)) {
230 GIOChannel *in, *out;
231 gchar buf[BUFFER_SIZE];
232 GIOStatus read_st, write_st;
233 gsize bytes_read, bytes_written;
235 in = g_io_channel_unix_new (pout);
236 g_io_channel_set_encoding (in, NULL, NULL);
237 out = g_io_channel_unix_new (fd);
238 g_io_channel_set_encoding (out, NULL, NULL);
241 read_st = g_io_channel_read_chars (in, buf,
245 if (read_st == G_IO_STATUS_NORMAL) {
246 write_st = g_io_channel_write_chars (out, buf,
250 if (write_st == G_IO_STATUS_ERROR)
252 } else if (read_st == G_IO_STATUS_ERROR) {
255 } while (bytes_read > 0);
257 g_io_channel_unref (in);
258 g_io_channel_unref (out);
263 if (*error == NULL) {
264 uri_dst = g_filename_to_uri (filename_dst, NULL, NULL);
269 g_free (filename_dst);
275 ev_file_uncompress (const gchar *uri,
276 EvCompressionType type,
279 g_return_val_if_fail (uri != NULL, NULL);
281 return compression_run (uri, type, FALSE, error);
285 ev_file_compress (const gchar *uri,
286 EvCompressionType type,
289 g_return_val_if_fail (uri != NULL, NULL);
291 return compression_run (uri, type, TRUE, error);