]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-file-helpers.c
Move mime-type functions from document-facrory to file-helpers so that it
[evince.git] / libdocument / ev-file-helpers.c
1 /*
2  *  Copyright (C) 2002 Jorn Baayen
3  *
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)
7  *  any later version.
8  *
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.
13  *
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.
17  *
18  *  $Id$
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <glib.h>
30 #include <glib/gstdio.h>
31 #include <errno.h>
32
33 #if WITH_GNOME
34 #include <libgnome/gnome-init.h>
35 #endif
36
37 #include "ev-file-helpers.h"
38
39 static gchar *dot_dir = NULL;
40 static gchar *tmp_dir = NULL;
41 static gint   count = 0;
42
43 static gboolean
44 ensure_dir_exists (const char *dir)
45 {
46         if (g_file_test (dir, G_FILE_TEST_IS_DIR))
47                 return TRUE;
48         
49         if (g_mkdir_with_parents (dir, 488) == 0)
50                 return TRUE;
51
52         if (errno == EEXIST)
53                 return g_file_test (dir, G_FILE_TEST_IS_DIR);
54         
55         g_warning ("Failed to create directory %s: %s", dir, strerror (errno));
56         return FALSE;
57 }
58
59 const gchar *
60 ev_dot_dir (void)
61 {
62         if (dot_dir == NULL) {
63                 gboolean exists;
64
65                 dot_dir = g_build_filename (g_get_home_dir (),
66                                             ".gnome2",
67                                             "evince",
68                                             NULL);
69
70                 exists = ensure_dir_exists (dot_dir);
71                 if (!exists)
72                         exit (1);
73         }
74
75         return dot_dir;
76 }
77
78 const gchar *
79 ev_tmp_dir (void)
80 {
81         if (tmp_dir == NULL) {
82                 gboolean exists;
83                 gchar   *dirname;
84
85                 dirname = g_strdup_printf ("evince-%u", getpid ());
86                 tmp_dir = g_build_filename (g_get_tmp_dir (),
87                                             dirname,
88                                             NULL);
89                 g_free (dirname);
90
91                 exists = ensure_dir_exists (tmp_dir);
92                 g_assert (exists);
93         }
94
95         return tmp_dir;
96 }
97
98 void
99 ev_file_helpers_init (void)
100 {
101 }
102
103 void
104 ev_file_helpers_shutdown (void)
105 {       
106         if (tmp_dir != NULL)    
107                 g_rmdir (tmp_dir);
108
109         g_free (tmp_dir);
110         g_free (dot_dir);
111
112         dot_dir = NULL;
113         tmp_dir = NULL;
114 }
115
116 GFile *
117 ev_tmp_file_get (const gchar *prefix)
118 {
119         gchar *path;
120         GFile *file;
121
122         path = ev_tmp_filename (prefix);
123         file = g_file_new_for_path (path);
124         
125         g_free (path);
126         
127         return file;
128 }
129
130 gchar * 
131 ev_tmp_filename (const gchar *prefix)
132 {
133         gchar *basename;
134         gchar *filename = NULL;
135
136         do {
137                 if (filename != NULL)
138                         g_free (filename);
139                         
140                 basename = g_strdup_printf ("%s-%d",
141                                             prefix ? prefix : "document",
142                                             count ++);
143                 
144                 filename = g_build_filename (ev_tmp_dir (),
145                                              basename, NULL);
146                 
147                 g_free (basename);
148         } while (g_file_test (filename, G_FILE_TEST_EXISTS));
149                         
150         return filename;
151 }
152
153 /* Remove a local temp file created by evince */
154 void
155 ev_tmp_filename_unlink (const gchar *filename)
156 {
157         const gchar *tempdir;
158         
159         if (!filename)
160                 return;
161
162         tempdir = g_get_tmp_dir ();
163         if (g_ascii_strncasecmp (filename, tempdir, strlen (tempdir)) == 0) {
164                 g_unlink (filename);
165         }
166 }
167
168 void
169 ev_tmp_file_unlink (GFile *file)
170 {
171         gboolean res;
172
173         if (!file)
174                 return;
175         
176         res = g_file_delete (file, NULL, NULL);
177         if (!res) {
178                 char *uri;
179                 
180                 uri = g_file_get_uri (file);
181                 g_warning ("Unable to delete temp file %s\n", uri);
182                 g_free (uri);
183         }
184 }
185
186 void
187 ev_tmp_uri_unlink (const gchar *uri)
188 {
189         GFile *file;
190         
191         if (!uri)
192                 return;
193         
194         file = g_file_new_for_uri (uri);
195         if (!g_file_is_native (file)) {
196                 g_warning ("Attempting to delete non native uri: %s\n", uri);
197                 g_object_unref (file);
198                 return;
199         }
200         
201         ev_tmp_file_unlink (file);
202         g_object_unref (file);
203 }
204
205 gboolean
206 ev_xfer_uri_simple (const char *from,
207                     const char *to,
208                     GError     **error)
209 {
210         GFile *source_file;
211         GFile *target_file;
212         GError *ioerror = NULL;
213         gboolean result;
214         
215         if (!from)
216                 return FALSE;
217         
218         source_file = g_file_new_for_uri (from);
219         target_file = g_file_new_for_uri (to);
220         
221         result = g_file_copy (source_file, target_file,
222                               G_FILE_COPY_OVERWRITE,
223                               NULL, NULL, NULL, &ioerror);
224
225         g_object_unref (target_file);
226         g_object_unref (source_file);
227     
228         if (!result) {
229                 g_propagate_error (error, ioerror);
230         }
231         return result;
232
233 }
234
235 static gchar *
236 get_mime_type_from_uri (const gchar *uri, GError **error)
237 {
238         GFile     *file;
239         GFileInfo *file_info;
240         gchar     *mime_type;
241
242         file = g_file_new_for_uri (uri);
243         file_info = g_file_query_info (file,
244                                        G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
245                                        0, NULL, error);
246         g_object_unref (file);
247
248         if (file_info == NULL)
249                 return NULL;
250
251         mime_type = g_strdup (g_file_info_get_content_type (file_info));
252         g_object_unref (file_info);
253
254         return mime_type;
255 }
256
257 static gchar *
258 get_mime_type_from_data (const gchar *uri, GError **error)
259 {
260         GFile            *file;
261         GFileInputStream *input_stream;
262         gssize            size_read;
263         guchar            buffer[1024];
264
265         file = g_file_new_for_uri (uri);
266         
267         input_stream = g_file_read (file, NULL, error);
268         if (!input_stream) {
269                 g_object_unref (file);
270                 return NULL;
271         }
272
273         size_read = g_input_stream_read (G_INPUT_STREAM (input_stream),
274                                          buffer, 1024, NULL, NULL);
275         g_input_stream_close (G_INPUT_STREAM (input_stream), NULL, error);
276
277         g_object_unref (file);
278
279         if (size_read == -1)
280                 return NULL;
281
282         return g_content_type_guess (NULL, /* no filename */
283                                      buffer, 1024,
284                                      NULL);
285 }
286
287 gchar *
288 ev_file_get_mime_type (const gchar *uri,
289                        gboolean     fast,
290                        GError     **error)
291 {
292         return fast ? get_mime_type_from_uri (uri, error) : get_mime_type_from_data (uri, error);
293 }
294
295 /* Compressed files support */
296 #define BZIPCOMMAND "bzip2"
297 #define GZIPCOMMAND "gzip"
298 #define N_ARGS      4
299 #define BUFFER_SIZE 1024
300
301 static gchar *
302 compression_run (const gchar       *uri,
303                  EvCompressionType  type,
304                  gboolean           compress, 
305                  GError           **error)
306 {
307         gchar *argv[N_ARGS];
308         gchar *uri_dst = NULL;
309         gchar *filename, *filename_dst;
310         gchar *cmd;
311         gint   fd, pout;
312
313         if (type == EV_COMPRESSION_NONE)
314                 return NULL;
315
316         cmd = g_find_program_in_path ((type == EV_COMPRESSION_BZIP2) ? BZIPCOMMAND : GZIPCOMMAND);
317         if (!cmd)
318                 return NULL;
319
320         filename = g_filename_from_uri (uri, NULL, NULL);
321         if (!filename) {
322                 g_free (cmd);
323                 return NULL;
324         }
325         
326         filename_dst = g_build_filename (ev_tmp_dir (), "evinceXXXXXX", NULL);
327         fd = g_mkstemp (filename_dst);
328         if (fd < 0) {
329                 g_free (cmd);
330                 g_free (filename);
331                 g_free (filename_dst);
332                 return NULL;
333         }
334
335         argv[0] = cmd;
336         argv[1] = compress ? "-c" : "-cd";
337         argv[2] = filename;
338         argv[3] = NULL;
339
340         if (g_spawn_async_with_pipes (NULL, argv, NULL,
341                                       G_SPAWN_STDERR_TO_DEV_NULL,
342                                       NULL, NULL, NULL,
343                                       NULL, &pout, NULL, error)) {
344                 GIOChannel *in, *out;
345                 gchar buf[BUFFER_SIZE];
346                 GIOStatus read_st, write_st;
347                 gsize bytes_read, bytes_written;
348
349                 in = g_io_channel_unix_new (pout);
350                 g_io_channel_set_encoding (in, NULL, NULL);
351                 out = g_io_channel_unix_new (fd);
352                 g_io_channel_set_encoding (out, NULL, NULL);
353
354                 do {
355                         read_st = g_io_channel_read_chars (in, buf,
356                                                            BUFFER_SIZE,
357                                                            &bytes_read,
358                                                            error);
359                         if (read_st == G_IO_STATUS_NORMAL) {
360                                 write_st = g_io_channel_write_chars (out, buf,
361                                                                      bytes_read,
362                                                                      &bytes_written,
363                                                                      error);
364                                 if (write_st == G_IO_STATUS_ERROR)
365                                         break;
366                         } else if (read_st == G_IO_STATUS_ERROR) {
367                                 break;
368                         }
369                 } while (bytes_read > 0);
370
371                 g_io_channel_unref (in);
372                 g_io_channel_unref (out);
373         }
374
375         close (fd);
376
377         if (*error == NULL) {
378                 uri_dst = g_filename_to_uri (filename_dst, NULL, NULL);
379         }
380
381         g_free (cmd);
382         g_free (filename);
383         g_free (filename_dst);
384
385         return uri_dst;
386 }
387
388 gchar *
389 ev_file_uncompress (const gchar       *uri,
390                     EvCompressionType  type,
391                     GError           **error)
392 {
393         g_return_val_if_fail (uri != NULL, NULL);
394
395         return compression_run (uri, type, FALSE, error);
396 }
397
398 gchar *
399 ev_file_compress (const gchar       *uri,
400                   EvCompressionType  type,
401                   GError           **error)
402 {
403         g_return_val_if_fail (uri != NULL, NULL);
404
405         return compression_run (uri, type, TRUE, error);
406 }