]> www.fi.muni.cz Git - evince.git/blob - lib/ev-file-helpers.c
Hungarian 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 <glib.h>
27 #include <libgnome/gnome-init.h>
28 #include <unistd.h>
29
30 #include "ev-file-helpers.h"
31
32 static char *dot_dir = NULL;
33 static char *tmp_dir = NULL;
34 static int  count = 0;
35
36 static gboolean
37 ensure_dir_exists (const char *dir)
38 {
39         if (g_file_test (dir, G_FILE_TEST_IS_DIR) == FALSE)
40         {
41                 if (g_file_test (dir, G_FILE_TEST_EXISTS) == TRUE)
42                 {
43                         g_warning ("%s exists, please move it out of the way.", dir);
44                         return FALSE;
45                 }
46
47                 if (mkdir (dir, 488) != 0)
48                 {
49                         g_warning ("Failed to create directory %s.", dir);
50                         return FALSE;
51                 }
52         }
53
54         return TRUE;
55 }
56
57 const char *
58 ev_dot_dir (void)
59 {
60         if (dot_dir == NULL)
61         {
62                 gboolean exists;
63
64                 dot_dir = g_build_filename (gnome_user_dir_get (),
65                                             "evince",
66                                             NULL);
67
68                 exists = ensure_dir_exists (dot_dir);
69                 g_assert (exists);
70         }
71
72         return dot_dir;
73 }
74
75 void
76 ev_file_helpers_init (void)
77 {
78 }
79
80 void
81 ev_file_helpers_shutdown (void)
82 {       
83         if (tmp_dir != NULL)    
84                 rmdir (tmp_dir);
85
86         g_free (tmp_dir);
87         g_free (dot_dir);
88
89         dot_dir = NULL;
90         tmp_dir = NULL;
91 }
92
93 gchar* 
94 ev_tmp_filename (void)
95 {
96         gchar *basename;
97         gchar *filename = NULL;
98
99         if (tmp_dir == NULL) {
100                 gboolean exists;
101                 gchar   *dirname;
102                 
103                 dirname = g_strdup_printf ("evince-%u", getpid());
104                 tmp_dir = g_build_filename (g_get_tmp_dir (),
105                                             dirname,
106                                             NULL);
107                 g_free (dirname);
108
109                 exists = ensure_dir_exists (tmp_dir);
110                 g_assert (exists);
111         }
112         
113         
114         do {
115                 if (filename != NULL)
116                         g_free (filename);
117                         
118                 basename = g_strdup_printf ("document-%d", count ++);
119                 
120                 filename = g_build_filename (tmp_dir, basename, NULL);
121                 
122                 g_free (basename);
123         } while (g_file_test (filename, G_FILE_TEST_EXISTS));
124                         
125         return filename;
126 }