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>
34 #include <libgnome/gnome-init.h>
37 #include "ev-file-helpers.h"
39 static gchar *dot_dir = NULL;
40 static gchar *tmp_dir = NULL;
41 static gint count = 0;
44 ensure_dir_exists (const char *dir)
46 if (g_file_test (dir, G_FILE_TEST_IS_DIR))
49 if (g_mkdir_with_parents (dir, 488) == 0)
53 return g_file_test (dir, G_FILE_TEST_IS_DIR);
55 g_warning ("Failed to create directory %s: %s", dir, strerror (errno));
62 if (dot_dir == NULL) {
66 dot_dir = g_build_filename (gnome_user_dir_get (),
70 dot_dir = g_build_filename (g_get_user_config_dir (),
75 exists = ensure_dir_exists (dot_dir);
86 if (tmp_dir == NULL) {
90 dirname = g_strdup_printf ("evince-%u", getpid ());
91 tmp_dir = g_build_filename (g_get_tmp_dir (),
96 exists = ensure_dir_exists (tmp_dir);
104 ev_file_helpers_init (void)
109 ev_file_helpers_shutdown (void)
122 ev_tmp_file_get (const gchar *prefix)
127 path = ev_tmp_filename (prefix);
128 file = g_file_new_for_path (path);
136 ev_tmp_filename (const gchar *prefix)
139 gchar *filename = NULL;
142 if (filename != NULL)
145 basename = g_strdup_printf ("%s-%d",
146 prefix ? prefix : "document",
149 filename = g_build_filename (ev_tmp_dir (),
153 } while (g_file_test (filename, G_FILE_TEST_EXISTS));
158 /* Remove a local temp file created by evince */
160 ev_tmp_filename_unlink (const gchar *filename)
162 const gchar *tempdir;
167 tempdir = g_get_tmp_dir ();
168 if (g_ascii_strncasecmp (filename, tempdir, strlen (tempdir)) == 0) {
174 ev_tmp_file_unlink (GFile *file)
181 res = g_file_delete (file, NULL, NULL);
185 uri = g_file_get_uri (file);
186 g_warning ("Unable to delete temp file %s\n", uri);
192 ev_tmp_uri_unlink (const gchar *uri)
199 file = g_file_new_for_uri (uri);
200 if (!g_file_is_native (file)) {
201 g_warning ("Attempting to delete non native uri: %s\n", uri);
202 g_object_unref (file);
206 ev_tmp_file_unlink (file);
207 g_object_unref (file);
211 ev_xfer_uri_simple (const char *from,
223 source_file = g_file_new_for_uri (from);
224 target_file = g_file_new_for_uri (to);
226 result = g_file_copy (source_file, target_file,
227 G_FILE_COPY_OVERWRITE,
228 NULL, NULL, NULL, &ioerror);
230 g_object_unref (target_file);
231 g_object_unref (source_file);
234 g_propagate_error (error, ioerror);
240 /* Compressed files support */
241 #define BZIPCOMMAND "bzip2"
242 #define GZIPCOMMAND "gzip"
244 #define BUFFER_SIZE 1024
247 compression_run (const gchar *uri,
248 EvCompressionType type,
253 gchar *uri_dst = NULL;
254 gchar *filename, *filename_dst;
258 if (type == EV_COMPRESSION_NONE)
261 cmd = g_find_program_in_path ((type == EV_COMPRESSION_BZIP2) ? BZIPCOMMAND : GZIPCOMMAND);
265 filename = g_filename_from_uri (uri, NULL, NULL);
271 filename_dst = g_build_filename (ev_tmp_dir (), "evinceXXXXXX", NULL);
272 fd = g_mkstemp (filename_dst);
276 g_free (filename_dst);
281 argv[1] = compress ? "-c" : "-cd";
285 if (g_spawn_async_with_pipes (NULL, argv, NULL,
286 G_SPAWN_STDERR_TO_DEV_NULL,
288 NULL, &pout, NULL, error)) {
289 GIOChannel *in, *out;
290 gchar buf[BUFFER_SIZE];
291 GIOStatus read_st, write_st;
292 gsize bytes_read, bytes_written;
294 in = g_io_channel_unix_new (pout);
295 g_io_channel_set_encoding (in, NULL, NULL);
296 out = g_io_channel_unix_new (fd);
297 g_io_channel_set_encoding (out, NULL, NULL);
300 read_st = g_io_channel_read_chars (in, buf,
304 if (read_st == G_IO_STATUS_NORMAL) {
305 write_st = g_io_channel_write_chars (out, buf,
309 if (write_st == G_IO_STATUS_ERROR)
311 } else if (read_st == G_IO_STATUS_ERROR) {
314 } while (bytes_read > 0);
316 g_io_channel_unref (in);
317 g_io_channel_unref (out);
322 if (*error == NULL) {
323 uri_dst = g_filename_to_uri (filename_dst, NULL, NULL);
328 g_free (filename_dst);
334 ev_file_uncompress (const gchar *uri,
335 EvCompressionType type,
338 g_return_val_if_fail (uri != NULL, NULL);
340 return compression_run (uri, type, FALSE, error);
344 ev_file_compress (const gchar *uri,
345 EvCompressionType type,
348 g_return_val_if_fail (uri != NULL, NULL);
350 return compression_run (uri, type, TRUE, error);