]> www.fi.muni.cz Git - evince.git/blob - lib/ev-file-helpers.c
9763831d783bab59e3fafdebf48d14d8f1872bed
[evince.git] / lib / 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 <libgnome/gnome-init.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>
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 (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 (gnome_user_dir_get (),
66                                             "evince",
67                                             NULL);
68
69                 exists = ensure_dir_exists (dot_dir);
70                 if (!exists)
71                         exit (1);
72         }
73
74         return dot_dir;
75 }
76
77 const gchar *
78 ev_tmp_dir (void)
79 {
80         if (tmp_dir == NULL) {
81                 gboolean exists;
82                 gchar   *dirname;
83
84                 dirname = g_strdup_printf ("evince-%u", getpid ());
85                 tmp_dir = g_build_filename (g_get_tmp_dir (),
86                                             dirname,
87                                             NULL);
88                 g_free (dirname);
89
90                 exists = ensure_dir_exists (tmp_dir);
91                 g_assert (exists);
92         }
93
94         return tmp_dir;
95 }
96
97 void
98 ev_file_helpers_init (void)
99 {
100 }
101
102 void
103 ev_file_helpers_shutdown (void)
104 {       
105         if (tmp_dir != NULL)    
106                 g_rmdir (tmp_dir);
107
108         g_free (tmp_dir);
109         g_free (dot_dir);
110
111         dot_dir = NULL;
112         tmp_dir = NULL;
113 }
114
115 gchar * 
116 ev_tmp_filename (void)
117 {
118         gchar *basename;
119         gchar *filename = NULL;
120
121         do {
122                 if (filename != NULL)
123                         g_free (filename);
124                         
125                 basename = g_strdup_printf ("document-%d", count ++);
126                 
127                 filename = g_build_filename (ev_tmp_dir (),
128                                              basename, NULL);
129                 
130                 g_free (basename);
131         } while (g_file_test (filename, G_FILE_TEST_EXISTS));
132                         
133         return filename;
134 }
135
136 gboolean
137 ev_xfer_uri_simple (const char *from,
138                     const char *to,
139                     GError     **error)
140 {
141         GnomeVFSResult result;
142         GnomeVFSURI *source_uri;
143         GnomeVFSURI *target_uri;
144         
145         if (!from)
146                 return FALSE;
147         
148         source_uri = gnome_vfs_uri_new (from);
149         target_uri = gnome_vfs_uri_new (to);
150
151         result = gnome_vfs_xfer_uri (source_uri, target_uri, 
152                                      GNOME_VFS_XFER_DEFAULT | GNOME_VFS_XFER_FOLLOW_LINKS,
153                                      GNOME_VFS_XFER_ERROR_MODE_ABORT,
154                                      GNOME_VFS_XFER_OVERWRITE_MODE_REPLACE,
155                                      NULL,
156                                      NULL);
157         gnome_vfs_uri_unref (target_uri);
158         gnome_vfs_uri_unref (source_uri);
159     
160         if (result != GNOME_VFS_OK)
161                 g_set_error (error,
162                              G_FILE_ERROR,
163                              G_FILE_ERROR_FAILED,
164                              gnome_vfs_result_to_string (result));
165         return (result == GNOME_VFS_OK);
166
167 }