Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/lib/ev-file-helpers.c
blob: 247e511d11f67724b7d8eba84c9b98d21095f9b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 *  Copyright (C) 2002 Jorn Baayen
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *  $Id$
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <sys/stat.h>
#include <glib.h>
#include <libgnome/gnome-init.h>
#include <unistd.h>

#include "ev-file-helpers.h"

static char *dot_dir = NULL;
static char *tmp_dir = NULL;
static int  count = 0;

static gboolean
ensure_dir_exists (const char *dir)
{
	if (g_file_test (dir, G_FILE_TEST_IS_DIR) == FALSE)
	{
		if (g_file_test (dir, G_FILE_TEST_EXISTS) == TRUE)
		{
			g_warning ("%s exists, please move it out of the way.", dir);
			return FALSE;
		}

		if (mkdir (dir, 488) != 0)
		{
			g_warning ("Failed to create directory %s.", dir);
			return FALSE;
		}
	}

	return TRUE;
}

const char *
ev_dot_dir (void)
{
	if (dot_dir == NULL)
	{
		gboolean exists;

		dot_dir = g_build_filename (gnome_user_dir_get (),
					    "evince",
					    NULL);

		exists = ensure_dir_exists (dot_dir);
		g_assert (exists);
	}

	return dot_dir;
}

void
ev_file_helpers_init (void)
{
}

void
ev_file_helpers_shutdown (void)
{	
	if (tmp_dir != NULL)	
		rmdir (tmp_dir);

	g_free (tmp_dir);
	g_free (dot_dir);

	dot_dir = NULL;
	tmp_dir = NULL;
}

gchar* 
ev_tmp_filename (void)
{
	gchar *basename;
	gchar *filename = NULL;

	if (tmp_dir == NULL) {
		gboolean exists;
		gchar   *dirname;
		
		dirname = g_strdup_printf ("evince-%u", getpid());
		tmp_dir = g_build_filename (g_get_tmp_dir (),
					    dirname,
					    NULL);
		g_free (dirname);

		exists = ensure_dir_exists (tmp_dir);
		g_assert (exists);
	}
	
	
	do {
		if (filename != NULL)
			g_free (filename);
			
		basename = g_strdup_printf ("document-%d", count ++);
		
		filename = g_build_filename (tmp_dir, basename, NULL);
		
		g_free (basename);
	} while (g_file_test (filename, G_FILE_TEST_EXISTS));
			
	return filename;
}