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) {
65 dot_dir = g_build_filename (g_get_home_dir (),
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_file_get (const gchar *prefix)
122 path = ev_tmp_filename (prefix);
123 file = g_file_new_for_path (path);
131 ev_tmp_filename (const gchar *prefix)
134 gchar *filename = NULL;
137 if (filename != NULL)
140 basename = g_strdup_printf ("%s-%d",
141 prefix ? prefix : "document",
144 filename = g_build_filename (ev_tmp_dir (),
148 } while (g_file_test (filename, G_FILE_TEST_EXISTS));
153 /* Remove a local temp file created by evince */
155 ev_tmp_filename_unlink (const gchar *filename)
157 const gchar *tempdir;
162 tempdir = g_get_tmp_dir ();
163 if (g_ascii_strncasecmp (filename, tempdir, strlen (tempdir)) == 0) {
169 ev_tmp_file_unlink (GFile *file)
172 GError *error = NULL;
177 res = g_file_delete (file, NULL, &error);
181 uri = g_file_get_uri (file);
182 g_warning ("Unable to delete temp file %s: %s\n", uri, error->message);
184 g_error_free (error);
189 ev_tmp_uri_unlink (const gchar *uri)
196 file = g_file_new_for_uri (uri);
197 if (!g_file_is_native (file)) {
198 g_warning ("Attempting to delete non native uri: %s\n", uri);
199 g_object_unref (file);
203 ev_tmp_file_unlink (file);
204 g_object_unref (file);
208 ev_xfer_uri_simple (const char *from,
214 GError *ioerror = NULL;
220 source_file = g_file_new_for_uri (from);
221 target_file = g_file_new_for_uri (to);
223 result = g_file_copy (source_file, target_file,
224 G_FILE_COPY_OVERWRITE,
225 NULL, NULL, NULL, &ioerror);
227 g_object_unref (target_file);
228 g_object_unref (source_file);
231 g_propagate_error (error, ioerror);
238 get_mime_type_from_uri (const gchar *uri, GError **error)
241 GFileInfo *file_info;
244 file = g_file_new_for_uri (uri);
245 file_info = g_file_query_info (file,
246 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
248 g_object_unref (file);
250 if (file_info == NULL)
253 mime_type = g_content_type_get_mime_type (
254 g_file_info_get_content_type (file_info));
255 g_object_unref (file_info);
261 get_mime_type_from_data (const gchar *uri, GError **error)
264 GFileInputStream *input_stream;
268 file = g_file_new_for_uri (uri);
270 input_stream = g_file_read (file, NULL, error);
272 g_object_unref (file);
276 size_read = g_input_stream_read (G_INPUT_STREAM (input_stream),
277 buffer, 1024, NULL, NULL);
278 g_input_stream_close (G_INPUT_STREAM (input_stream), NULL, error);
280 g_object_unref (file);
285 return g_content_type_guess (NULL, /* no filename */
291 ev_file_get_mime_type (const gchar *uri,
295 return fast ? get_mime_type_from_uri (uri, error) : get_mime_type_from_data (uri, error);
298 /* Compressed files support */
299 #define BZIPCOMMAND "bzip2"
300 #define GZIPCOMMAND "gzip"
302 #define BUFFER_SIZE 1024
305 compression_run (const gchar *uri,
306 EvCompressionType type,
311 gchar *uri_dst = NULL;
312 gchar *filename, *filename_dst;
316 if (type == EV_COMPRESSION_NONE)
319 cmd = g_find_program_in_path ((type == EV_COMPRESSION_BZIP2) ? BZIPCOMMAND : GZIPCOMMAND);
323 filename = g_filename_from_uri (uri, NULL, NULL);
329 filename_dst = g_build_filename (ev_tmp_dir (), "evinceXXXXXX", NULL);
330 fd = g_mkstemp (filename_dst);
334 g_free (filename_dst);
339 argv[1] = compress ? "-c" : "-cd";
343 if (g_spawn_async_with_pipes (NULL, argv, NULL,
344 G_SPAWN_STDERR_TO_DEV_NULL,
346 NULL, &pout, NULL, error)) {
347 GIOChannel *in, *out;
348 gchar buf[BUFFER_SIZE];
349 GIOStatus read_st, write_st;
350 gsize bytes_read, bytes_written;
352 in = g_io_channel_unix_new (pout);
353 g_io_channel_set_encoding (in, NULL, NULL);
354 out = g_io_channel_unix_new (fd);
355 g_io_channel_set_encoding (out, NULL, NULL);
358 read_st = g_io_channel_read_chars (in, buf,
362 if (read_st == G_IO_STATUS_NORMAL) {
363 write_st = g_io_channel_write_chars (out, buf,
367 if (write_st == G_IO_STATUS_ERROR)
369 } else if (read_st == G_IO_STATUS_ERROR) {
372 } while (bytes_read > 0);
374 g_io_channel_unref (in);
375 g_io_channel_unref (out);
380 if (*error == NULL) {
381 uri_dst = g_filename_to_uri (filename_dst, NULL, NULL);
386 g_free (filename_dst);
392 ev_file_uncompress (const gchar *uri,
393 EvCompressionType type,
396 g_return_val_if_fail (uri != NULL, NULL);
398 return compression_run (uri, type, FALSE, error);
402 ev_file_compress (const gchar *uri,
403 EvCompressionType type,
406 g_return_val_if_fail (uri != NULL, NULL);
408 return compression_run (uri, type, TRUE, error);