summaryrefslogtreecommitdiff
path: root/libnautilus-private/nautilus-directory-metafile.c
blob: 7fa081869f7970d910dd36dae5e7dc0f237b43bf (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-

   nautilus-directory-metafile.c: Nautilus directory model.
 
   Copyright (C) 2000, 2001 Eazel, Inc.
  
   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 of the
   License, 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.
  
   Authors: Darin Adler <darin@bentspoon.com>,
            Mike Engber <engber@eazel.com>
*/

#include <config.h>
#include "nautilus-directory-metafile.h"

#include "nautilus-directory-private.h"
#include "nautilus-file-private.h"
#include "nautilus-metafile.h"
#include <eel/eel-debug.h>
#include <eel/eel-string.h>
#include <stdio.h>


static NautilusMetafile *
get_metafile (NautilusDirectory *directory)
{
	char *uri;

	if (directory->details->metafile == NULL) {
		uri = nautilus_directory_get_uri (directory);
		directory->details->metafile = nautilus_metafile_get_for_uri (uri);
		g_free (uri);
	}

	return directory->details->metafile;	
}

gboolean
nautilus_directory_is_metadata_read (NautilusDirectory *directory)
{
	NautilusMetafile *metafile;

	g_return_val_if_fail (NAUTILUS_IS_DIRECTORY (directory), FALSE);
	
	metafile = get_metafile (directory);

	if (metafile == NULL) {
		return TRUE;
	}
		
	return nautilus_metafile_is_read (metafile);
}

char *
nautilus_directory_get_file_metadata (NautilusDirectory *directory,
				      const char *file_name,
				      const char *key,
				      const char *default_metadata)
{
	NautilusMetafile *metafile;

	g_return_val_if_fail (NAUTILUS_IS_DIRECTORY (directory), g_strdup (default_metadata));
	g_return_val_if_fail (!eel_str_is_empty (file_name), g_strdup (default_metadata));
	g_return_val_if_fail (!eel_str_is_empty (key), g_strdup (default_metadata));
	
	metafile = get_metafile (directory);

	return nautilus_metafile_get (metafile, file_name, key, default_metadata);
}

GList *
nautilus_directory_get_file_metadata_list (NautilusDirectory *directory,
					   const char *file_name,
					   const char *list_key,
					   const char *list_subkey)
{
	NautilusMetafile *     metafile;

	g_return_val_if_fail (NAUTILUS_IS_DIRECTORY (directory), NULL);
	g_return_val_if_fail (!eel_str_is_empty (file_name), NULL);
	g_return_val_if_fail (!eel_str_is_empty (list_key), NULL);
	g_return_val_if_fail (!eel_str_is_empty (list_subkey), NULL);
	
	metafile = get_metafile (directory);
	return nautilus_metafile_get_list (metafile, file_name, list_key, list_subkey);
}

void
nautilus_directory_set_file_metadata (NautilusDirectory *directory,
				      const char *file_name,
				      const char *key,
				      const char *default_metadata,
				      const char *metadata)
{
	NautilusMetafile *metafile;

	g_return_if_fail (NAUTILUS_IS_DIRECTORY (directory));
	g_return_if_fail (!eel_str_is_empty (file_name));
	g_return_if_fail (!eel_str_is_empty (key));
	
	metafile = get_metafile (directory);
	nautilus_metafile_set (metafile, file_name, key, default_metadata, metadata);
}

void
nautilus_directory_set_file_metadata_list (NautilusDirectory *directory,
					   const char *file_name,
					   const char *list_key,
					   const char *list_subkey,
					   GList *list)
{
	NautilusMetafile *metafile;

	g_return_if_fail (NAUTILUS_IS_DIRECTORY (directory));
	g_return_if_fail (!eel_str_is_empty (file_name));
	g_return_if_fail (!eel_str_is_empty (list_key));
	g_return_if_fail (!eel_str_is_empty (list_subkey));

	metafile = get_metafile (directory);
	nautilus_metafile_set_list (metafile,
				    file_name,
				    list_key,
				    list_subkey,
				    list);
}

gboolean 
nautilus_directory_get_boolean_file_metadata (NautilusDirectory *directory,
					      const char *file_name,
					      const char *key,
					      gboolean default_metadata)
{
	char *result_as_string;
	gboolean result;

	result_as_string = nautilus_directory_get_file_metadata
		(directory, file_name, key,
		 default_metadata ? "true" : "false");
	g_assert (result_as_string != NULL);
	
	if (g_ascii_strcasecmp (result_as_string, "true") == 0) {
		result = TRUE;
	} else if (g_ascii_strcasecmp (result_as_string, "false") == 0) {
		result = FALSE;
	} else {
		g_error ("boolean metadata with value other than true or false");
		result = default_metadata;
	}

	g_free (result_as_string);
	return result;
}

void
nautilus_directory_set_boolean_file_metadata (NautilusDirectory *directory,
					      const char *file_name,
					      const char *key,
					      gboolean default_metadata,
					      gboolean metadata)
{
	nautilus_directory_set_file_metadata
		(directory, file_name, key,
		 default_metadata ? "true" : "false",
		 metadata ? "true" : "false");
}

int 
nautilus_directory_get_integer_file_metadata (NautilusDirectory *directory,
					      const char *file_name,
					      const char *key,
					      int default_metadata)
{
	char *result_as_string;
	char default_as_string[32];
	int result;
	char c;

	g_snprintf (default_as_string, sizeof (default_as_string), "%d", default_metadata);
	result_as_string = nautilus_directory_get_file_metadata
		(directory, file_name, key, default_as_string);

	/* Normally we can't get a a NULL, but we check for it here to
	 * handle the oddball case of a non-existent directory.
	 */
	if (result_as_string == NULL) {
		result = default_metadata;
	} else {
		if (sscanf (result_as_string, " %d %c", &result, &c) != 1) {
			result = default_metadata;
		}
		g_free (result_as_string);
	}

	return result;
}

void
nautilus_directory_set_integer_file_metadata (NautilusDirectory *directory,
					      const char *file_name,
					      const char *key,
					      int default_metadata,
					      int metadata)
{
	char value_as_string[32];
	char default_as_string[32];

	g_snprintf (value_as_string, sizeof (value_as_string), "%d", metadata);
	g_snprintf (default_as_string, sizeof (default_as_string), "%d", default_metadata);

	nautilus_directory_set_file_metadata
		(directory, file_name, key,
		 default_as_string, value_as_string);
}

void
nautilus_directory_copy_file_metadata (NautilusDirectory *source_directory,
				       const char *source_file_name,
				       NautilusDirectory *destination_directory,
				       const char *destination_file_name)
{
	char *destination_uri;
	NautilusMetafile *metafile;

	g_return_if_fail (NAUTILUS_IS_DIRECTORY (source_directory));
	g_return_if_fail (source_file_name != NULL);
	g_return_if_fail (NAUTILUS_IS_DIRECTORY (destination_directory));
	g_return_if_fail (destination_file_name != NULL);

	metafile = get_metafile (source_directory);
	destination_uri = nautilus_directory_get_uri (destination_directory);

	nautilus_metafile_copy (metafile, source_file_name,
				destination_uri, destination_file_name);

	g_free (destination_uri);
}

void
nautilus_directory_remove_file_metadata (NautilusDirectory *directory,
					 const char *file_name)
{
	NautilusMetafile *metafile;

	g_return_if_fail (NAUTILUS_IS_DIRECTORY (directory));
	g_return_if_fail (file_name != NULL);

	metafile = get_metafile (directory);
	nautilus_metafile_remove (metafile, file_name);
}

void
nautilus_directory_rename_file_metadata (NautilusDirectory *directory,
					 const char *old_file_name,
					 const char *new_file_name)
{
	NautilusMetafile *metafile;

	g_return_if_fail (NAUTILUS_IS_DIRECTORY (directory));
	g_return_if_fail (old_file_name != NULL);
	g_return_if_fail (new_file_name != NULL);

	metafile = get_metafile (directory);
	nautilus_metafile_rename (metafile, old_file_name,new_file_name);
}

void
nautilus_directory_rename_directory_metadata (NautilusDirectory *directory,
					      const char *new_directory_uri)
{
	NautilusMetafile *metafile;

	g_return_if_fail (NAUTILUS_IS_DIRECTORY (directory));
	g_return_if_fail (new_directory_uri != NULL);

	metafile = get_metafile (directory);
	nautilus_metafile_rename_directory (metafile, new_directory_uri);
}

static void
metafile_changed_cb (NautilusMetafile *metafile,
		     GList *file_names,
		     NautilusDirectory *directory)
{
	GList                   *file_list;
	NautilusFile		*file;
	
	file_list = NULL;
	while (file_names != NULL) {
		file = nautilus_directory_find_file_by_internal_filename
			(directory, file_names->data);

		if (file != NULL) {
			if (nautilus_file_is_self_owned (file)) {
				nautilus_file_emit_changed (file);
			} else {
				file_list = g_list_prepend (file_list, file);
			}
		}
		file_names = file_names->next;
	}
	
	if (file_list != NULL) {
		file_list = g_list_reverse (file_list);
		nautilus_directory_emit_change_signals (directory, file_list);
		g_list_free (file_list);
	}
}

static void
metafile_ready_cb (NautilusMetafile *metafile,
		   NautilusDirectory *directory)
{
	emit_change_signals_for_all_files (directory);
	nautilus_directory_async_state_changed (directory);
}


void
nautilus_directory_register_metadata_monitor (NautilusDirectory *directory)
{
	NautilusMetafile *metafile;

	g_return_if_fail (NAUTILUS_IS_DIRECTORY (directory));

	if (directory->details->metafile_monitored) {
		return;
	}

	metafile = get_metafile (directory);
	directory->details->metafile_monitored = TRUE;

	g_signal_connect (metafile, "changed", (GCallback)metafile_changed_cb, directory);
	g_signal_connect (metafile, "ready", (GCallback)metafile_ready_cb, directory);
	
	nautilus_metafile_load (metafile);
}

void
nautilus_directory_unregister_metadata_monitor (NautilusDirectory *directory)
{
	NautilusMetafile *metafile;

	g_return_if_fail (NAUTILUS_IS_DIRECTORY (directory));	

	directory->details->metafile_monitored = FALSE;

	metafile = get_metafile (directory);
	g_signal_handlers_disconnect_by_func (metafile, metafile_changed_cb, directory);
	g_signal_handlers_disconnect_by_func (metafile, metafile_ready_cb, directory);
}