summaryrefslogtreecommitdiff
path: root/src/user_share-common.c
blob: 0a1fc0639d7242f51b26cb7bfc9bd5f0f1ad3447 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */

/*
 *  Copyright (C) 2004-2009 Red Hat, Inc.
 *  Copyright (C) 2012-2021 MATE Developers
 *
 *  Caja 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 of the
 *  License, or (at your option) any later version.
 *
 *  Caja 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *  Authors: Alexander Larsson <alexl@redhat.com>
 *  Bastien Nocera <hadess@hadess.net>
 *
 */

#include <string.h>

#include "user_share-common.h"

static char *
lookup_special_dir (GUserDirectory directory,
		    const char *name,
		    gboolean create_dir)
{
	const char *special_dir;
	char *dir;

	special_dir = g_get_user_special_dir (directory);
	if (special_dir != NULL && strcmp (special_dir, g_get_home_dir ()) != 0) {
		if (create_dir != FALSE)
			g_mkdir_with_parents (special_dir, 0755);
		return g_strdup (special_dir);
	}

	dir = g_build_filename (g_get_home_dir (), name, NULL);
	if (create_dir != FALSE)
		g_mkdir_with_parents (dir, 0755);
	return dir;
}

char *
lookup_public_dir (void)
{
	return lookup_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE,
				   "Public",
				   TRUE);
}

char *
lookup_download_dir (void)
{
	return lookup_special_dir (G_USER_DIRECTORY_DOWNLOAD,
				   "Downloads",
				   TRUE);
}

GFile *
lookup_dir_with_fallback (GUserDirectory directory)
{
	GFile *file;
	char *path;
	const char *name;

	if (directory == G_USER_DIRECTORY_PUBLIC_SHARE)
		name = "Public";
	else if (directory == G_USER_DIRECTORY_DOWNLOAD)
		name = "Downloads";
	else
		g_assert_not_reached ();

	path = lookup_special_dir (directory,
				   name,
				   FALSE);

	if (path == NULL)
		return NULL;

	file = g_file_new_for_path (path);
	g_free (path);

	return file;
}