summaryrefslogtreecommitdiff
path: root/libnautilus-private/nautilus-file-utilities.c
blob: 7876cd27c6ca42866a0bb69abccd73d012434c65 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* -*- 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 <config.h>
#include "nautilus-file-utilities.h"

#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-util.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include "nautilus-file.h"
#include "nautilus-link-set.h"
#include "nautilus-metadata.h"

#define NAUTILUS_USER_DIRECTORY_NAME ".nautilus"
#define DEFAULT_NAUTILUS_DIRECTORY_MODE (0755)

#define DESKTOP_DIRECTORY_NAME "desktop"
#define DEFAULT_DESKTOP_DIRECTORY_MODE (0755)

#define NAUTILUS_USER_MAIN_DIRECTORY_NAME "Nautilus"

/**
 * 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.
 **/
char * 
nautilus_make_path(const char *path, const char* name)
{
    	gboolean insert_separator;
    	gint     path_length;
	char	*result;

	path_length = strlen (path);
    	insert_separator = path_length > 0 && 
    			   name[0] != '\0' > 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;
}

/* FIXME bugzilla.eazel.com 1117: Change file-utilities.c to always create user
 * directorie if needed.  See bug for detail
 */

/**
 * nautilus_user_directory:
 * 
 * Get the path for the directory containing nautilus settings.
 *
 * Return value: the directory path.
 **/
const char *
nautilus_get_user_directory (void)
{
	static char *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);
		}

	}

	return user_directory;
}

/**
 * nautilus_desktop_directory:
 * 
 * Get the path for the directory containing files on the desktop.
 *
 * Return value: the directory path.
 **/
const char *
nautilus_get_desktop_directory (void)
{
	static char *desktop_directory = NULL;

	if (desktop_directory == NULL) {
		desktop_directory = nautilus_make_path (nautilus_get_user_directory (),
							DESKTOP_DIRECTORY_NAME);
		if (!g_file_exists (desktop_directory)) {
			mkdir (desktop_directory, DEFAULT_DESKTOP_DIRECTORY_MODE);
		}

	}

	return desktop_directory;
}

/**
 * nautilus_user_main_directory:
 * 
 * Get the path for the user's main Nautilus directory.  
 * Usually ~/Nautilus
 *
 * Return value: the directory path.
 **/
const char *
nautilus_get_user_main_directory (void)
{
	static char *user_main_directory = NULL;
	NautilusFile *file;
	char *command, *file_uri, *image_uri, *temp_str;

	
	if (user_main_directory == NULL)
	{
		user_main_directory = g_strdup_printf ("%s/%s",
							g_get_home_dir(),
							NAUTILUS_USER_MAIN_DIRECTORY_NAME);
												
		if (!g_file_exists (user_main_directory)) {
			/* FIXME bugzilla.eazel.com 1285: 
			 * Is it OK to use cp like this? What about quoting the parameters? 
			 */
			command = g_strdup_printf ("cp -R %s %s",
						   NAUTILUS_PREFIX "/share/nautilus/top",
						   user_main_directory);

			/* FIXME bugzilla.eazel.com 1286: 
			 * Is a g_warning good enough here? This seems like a big problem. 
			 */
			if (system (command) != 0) {
				g_warning ("could not execute '%s'.  Make sure you typed 'make install'", 
					   command);
			}
			
			g_free (command);
		
			/* assign a custom image for the directory icon */
			file_uri = g_strdup_printf("file://%s", user_main_directory);
			temp_str = gnome_pixmap_file ("nautilus/nautilus-logo.png");
			image_uri = g_strdup_printf("file://%s", temp_str);
			g_free(temp_str);
			
			file = nautilus_file_get (file_uri);
			if (file != NULL) {
				nautilus_file_set_metadata (file,
							    NAUTILUS_METADATA_KEY_CUSTOM_ICON,
							    NULL,
							    image_uri);
				nautilus_file_unref (file);
			}
			g_free (file_uri);

			/* now do the same for the about file */
			file_uri = g_strdup_printf("file://%s/About.html", user_main_directory);
			
			file = nautilus_file_get (file_uri);
			if (file != NULL) {
				nautilus_file_set_metadata (file,
							    NAUTILUS_METADATA_KEY_CUSTOM_ICON,
							    NULL,
							    image_uri);
				nautilus_file_unref (file);
			}
			g_free (file_uri);
			g_free (image_uri);

			/* install the default link set */
			nautilus_link_set_install(user_main_directory, "apps");
			/*
			nautilus_link_set_install(user_main_directory, "search_engines");
			*/
		}
	}

	return user_main_directory;
}