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 *tmp_dir = NULL;
40 static gint count = 0;
43 ev_dir_ensure_exists (const gchar *dir,
46 if (g_mkdir_with_parents (dir, mode) == 0)
50 return g_file_test (dir, G_FILE_TEST_IS_DIR);
52 g_warning ("Failed to create directory %s: %s", dir, g_strerror (errno));
59 if (tmp_dir == NULL) {
61 gchar *dirname, *prgname;
63 prgname = g_get_prgname ();
64 dirname = g_strdup_printf ("%s-%u", prgname ? prgname : "unknown", getpid ());
65 tmp_dir = g_build_filename (g_get_tmp_dir (),
70 exists = ev_dir_ensure_exists (tmp_dir, 0700);
78 _ev_file_helpers_init (void)
83 _ev_file_helpers_shutdown (void)
93 ev_tmp_file_get (const gchar *prefix)
98 path = ev_tmp_filename (prefix);
99 file = g_file_new_for_path (path);
107 ev_tmp_filename (const gchar *prefix)
110 gchar *filename = NULL;
113 if (filename != NULL)
116 basename = g_strdup_printf ("%s-%d",
117 prefix ? prefix : "document",
120 filename = g_build_filename (ev_tmp_dir (),
124 } while (g_file_test (filename, G_FILE_TEST_EXISTS));
129 /* Remove a local temp file created by evince */
131 ev_tmp_filename_unlink (const gchar *filename)
133 const gchar *tempdir;
138 tempdir = g_get_tmp_dir ();
139 if (g_ascii_strncasecmp (filename, tempdir, strlen (tempdir)) == 0) {
145 ev_tmp_file_unlink (GFile *file)
148 GError *error = NULL;
153 res = g_file_delete (file, NULL, &error);
157 uri = g_file_get_uri (file);
158 g_warning ("Unable to delete temp file %s: %s\n", uri, error->message);
160 g_error_free (error);
165 ev_tmp_uri_unlink (const gchar *uri)
172 file = g_file_new_for_uri (uri);
173 if (!g_file_is_native (file)) {
174 g_warning ("Attempting to delete non native uri: %s\n", uri);
175 g_object_unref (file);
179 ev_tmp_file_unlink (file);
180 g_object_unref (file);
184 ev_xfer_uri_simple (const char *from,
190 GError *ioerror = NULL;
196 source_file = g_file_new_for_uri (from);
197 target_file = g_file_new_for_uri (to);
199 result = g_file_copy (source_file, target_file,
200 #if GLIB_CHECK_VERSION(2,19,0)
201 G_FILE_COPY_TARGET_DEFAULT_PERMS |
203 G_FILE_COPY_OVERWRITE,
204 NULL, NULL, NULL, &ioerror);
206 g_object_unref (target_file);
207 g_object_unref (source_file);
210 g_propagate_error (error, ioerror);
217 get_mime_type_from_uri (const gchar *uri, GError **error)
220 GFileInfo *file_info;
223 file = g_file_new_for_uri (uri);
224 file_info = g_file_query_info (file,
225 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
227 g_object_unref (file);
229 if (file_info == NULL)
232 mime_type = g_content_type_get_mime_type (
233 g_file_info_get_content_type (file_info));
234 g_object_unref (file_info);
240 get_mime_type_from_data (const gchar *uri, GError **error)
243 GFileInputStream *input_stream;
247 file = g_file_new_for_uri (uri);
249 input_stream = g_file_read (file, NULL, error);
251 g_object_unref (file);
255 size_read = g_input_stream_read (G_INPUT_STREAM (input_stream),
256 buffer, 1024, NULL, NULL);
257 g_input_stream_close (G_INPUT_STREAM (input_stream), NULL, error);
259 g_object_unref (file);
264 return g_content_type_guess (NULL, /* no filename */
270 ev_file_get_mime_type (const gchar *uri,
274 return fast ? get_mime_type_from_uri (uri, error) : get_mime_type_from_data (uri, error);
277 /* Compressed files support */
278 #define BZIPCOMMAND "bzip2"
279 #define GZIPCOMMAND "gzip"
281 #define BUFFER_SIZE 1024
284 compression_run (const gchar *uri,
285 EvCompressionType type,
290 gchar *uri_dst = NULL;
291 gchar *filename, *filename_dst;
295 if (type == EV_COMPRESSION_NONE)
298 cmd = g_find_program_in_path ((type == EV_COMPRESSION_BZIP2) ? BZIPCOMMAND : GZIPCOMMAND);
302 filename = g_filename_from_uri (uri, NULL, NULL);
308 filename_dst = g_build_filename (ev_tmp_dir (), "evinceXXXXXX", NULL);
309 fd = g_mkstemp (filename_dst);
313 g_free (filename_dst);
318 argv[1] = compress ? "-c" : "-cd";
322 if (g_spawn_async_with_pipes (NULL, argv, NULL,
323 G_SPAWN_STDERR_TO_DEV_NULL,
325 NULL, &pout, NULL, error)) {
326 GIOChannel *in, *out;
327 gchar buf[BUFFER_SIZE];
328 GIOStatus read_st, write_st;
329 gsize bytes_read, bytes_written;
331 in = g_io_channel_unix_new (pout);
332 g_io_channel_set_encoding (in, NULL, NULL);
333 out = g_io_channel_unix_new (fd);
334 g_io_channel_set_encoding (out, NULL, NULL);
337 read_st = g_io_channel_read_chars (in, buf,
341 if (read_st == G_IO_STATUS_NORMAL) {
342 write_st = g_io_channel_write_chars (out, buf,
346 if (write_st == G_IO_STATUS_ERROR)
348 } else if (read_st == G_IO_STATUS_ERROR) {
351 } while (bytes_read > 0);
353 g_io_channel_unref (in);
354 g_io_channel_unref (out);
359 if (*error == NULL) {
360 uri_dst = g_filename_to_uri (filename_dst, NULL, NULL);
365 g_free (filename_dst);
371 ev_file_uncompress (const gchar *uri,
372 EvCompressionType type,
375 g_return_val_if_fail (uri != NULL, NULL);
377 return compression_run (uri, type, FALSE, error);
381 ev_file_compress (const gchar *uri,
382 EvCompressionType type,
385 g_return_val_if_fail (uri != NULL, NULL);
387 return compression_run (uri, type, TRUE, error);