summaryrefslogtreecommitdiff
path: root/libnautilus-extensions/nautilus-file-utilities.c
blob: 4417c758e32979b3cb2e424698181a578e1467e3 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* nautilus-file-utilities..c - implementation of file manipulation routines.

   Copyright (C) 1999, 2000 Eazel, Inc.

   The Gnome Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Authors: John Sullivan <sullivan@eazel.com>
*/

#include <gnome.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>


#include "nautilus-file-utilities.h"

const char* const nautilus_user_directory_name = ".gnomad";
const unsigned default_nautilus_directory_mode = 0755;


/**
 * nautilus_make_path:
 * 
 * Make a path name from a base path and name. The base path
 * can end with or without a separator character.
 *
 * Return value: the combined path name.
 **/
gchar * 
nautilus_make_path(const gchar *path, const gchar* name)
{
    	gboolean insert_separator;
    	gint     path_length;
	gchar	*result;

	path_length = strlen(path);
    	insert_separator = path_length > 0 && 
    			   strlen(name) > 0 && 
    			   path[path_length - 1] != G_DIR_SEPARATOR;

    	if (insert_separator)
    	{
    		result = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
    	} 
    	else
    	{
    		result = g_strconcat(path, name, NULL);
    	}

	return result;
}



/**
 * nautilus_user_directory:
 * 
 * Get the path for the directory containing nautilus settings.
 *
 * Return value: the directory path.
 **/
const gchar *
nautilus_user_directory()
{
	static gchar *user_directory = NULL;

	if (user_directory == NULL)
	{
		user_directory = nautilus_make_path(g_get_home_dir(),
						    nautilus_user_directory_name);

		if (!g_file_exists(user_directory))
		{
			mkdir(user_directory, default_nautilus_directory_mode);
		}

	}

	if (!g_file_test(user_directory, G_FILE_TEST_ISDIR))
	{
		/* Bad news, directory still isn't there.
		 * Report this to user somehow. 
		 */
		g_assert_not_reached();
	}

	return user_directory;
}