]> www.fi.muni.cz Git - evince.git/blob - lib/ev-file-helpers.c
Translation updated.
[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 <sys/stat.h>
26 #include <unistd.h>
27 #include <glib.h>
28 #include <libgnome/gnome-init.h>
29 #include <libgnomevfs/gnome-vfs-uri.h>
30 #include <libgnomevfs/gnome-vfs-utils.h>
31 #include <libgnomevfs/gnome-vfs-ops.h>
32 #include <libgnomevfs/gnome-vfs-xfer.h>
33
34 #include "ev-file-helpers.h"
35
36 static char *dot_dir = NULL;
37 static char *tmp_dir = NULL;
38 static int  count = 0;
39
40 static gboolean
41 ensure_dir_exists (const char *dir)
42 {
43         if (g_file_test (dir, G_FILE_TEST_IS_DIR) == FALSE)
44         {
45                 if (g_file_test (dir, G_FILE_TEST_EXISTS) == TRUE)
46                 {
47                         g_warning ("%s exists, please move it out of the way.", dir);
48                         return FALSE;
49                 }
50
51                 if (mkdir (dir, 488) != 0)
52                 {
53                         g_warning ("Failed to create directory %s.", dir);
54                         return FALSE;
55                 }
56         }
57
58         return TRUE;
59 }
60
61 const char *
62 ev_dot_dir (void)
63 {
64         if (dot_dir == NULL)
65         {
66                 gboolean exists;
67
68                 dot_dir = g_build_filename (gnome_user_dir_get (),
69                                             "evince",
70                                             NULL);
71
72                 exists = ensure_dir_exists (dot_dir);
73                 g_assert (exists);
74         }
75
76         return dot_dir;
77 }
78
79 void
80 ev_file_helpers_init (void)
81 {
82 }
83
84 void
85 ev_file_helpers_shutdown (void)
86 {       
87         if (tmp_dir != NULL)    
88                 rmdir (tmp_dir);
89
90         g_free (tmp_dir);
91         g_free (dot_dir);
92
93         dot_dir = NULL;
94         tmp_dir = NULL;
95 }
96
97 gchar* 
98 ev_tmp_filename (void)
99 {
100         gchar *basename;
101         gchar *filename = NULL;
102
103         if (tmp_dir == NULL) {
104                 gboolean exists;
105                 gchar   *dirname;
106                 
107                 dirname = g_strdup_printf ("evince-%u", getpid());
108                 tmp_dir = g_build_filename (g_get_tmp_dir (),
109                                             dirname,
110                                             NULL);
111                 g_free (dirname);
112
113                 exists = ensure_dir_exists (tmp_dir);
114                 g_assert (exists);
115         }
116         
117         
118         do {
119                 if (filename != NULL)
120                         g_free (filename);
121                         
122                 basename = g_strdup_printf ("document-%d", count ++);
123                 
124                 filename = g_build_filename (tmp_dir, basename, NULL);
125                 
126                 g_free (basename);
127         } while (g_file_test (filename, G_FILE_TEST_EXISTS));
128                         
129         return filename;
130 }
131
132 gboolean
133 ev_xfer_uri_simple (const char *from,
134                     const char *to,
135                     GError     **error)
136 {
137         GnomeVFSResult result;
138         GnomeVFSURI *source_uri;
139         GnomeVFSURI *target_uri;
140         
141         if (!from)
142                 return FALSE;
143         
144         source_uri = gnome_vfs_uri_new (from);
145         target_uri = gnome_vfs_uri_new (to);
146
147         result = gnome_vfs_xfer_uri (source_uri, target_uri, 
148                                      GNOME_VFS_XFER_DEFAULT | GNOME_VFS_XFER_FOLLOW_LINKS,
149                                      GNOME_VFS_XFER_ERROR_MODE_ABORT,
150                                      GNOME_VFS_XFER_OVERWRITE_MODE_REPLACE,
151                                      NULL,
152                                      NULL);
153         gnome_vfs_uri_unref (target_uri);
154         gnome_vfs_uri_unref (source_uri);
155     
156         if (result != GNOME_VFS_OK)
157                 g_set_error (error,
158                              G_FILE_ERROR,
159                              G_FILE_ERROR_FAILED,
160                              gnome_vfs_result_to_string (result));
161         return (result == GNOME_VFS_OK);
162
163 }