summaryrefslogtreecommitdiff
path: root/libnautilus-extensions/nautilus-file.c
blob: e758c4720a1d757fb7b0964ce290a5ff0e541f47 (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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-

   nautilus-file.c: Nautilus file model.
 
   Copyright (C) 1999, 2000 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.
  
   Author: Darin Adler <darin@eazel.com>
*/

#include <config.h>
#include "nautilus-file-private.h"

#include <grp.h>
#include <pwd.h>

#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-i18n.h>

#include "nautilus-glib-extensions.h"
#include "nautilus-string.h"
#include "nautilus-directory-private.h"

typedef enum {
	NAUTILUS_DATE_TYPE_MODIFIED,
	NAUTILUS_DATE_TYPE_CHANGED,
	NAUTILUS_DATE_TYPE_ACCESSED
} NautilusDateType;

/* FIXME: This hack needs to die eventually. See comments with function */
static int   get_directory_item_count_hack           (NautilusFile         *file,
						      gboolean              ignore_invisible_items);

static int   nautilus_file_compare_by_type           (NautilusFile         *file_1,
						      NautilusFile         *file_2);
static int   nautilus_file_compare_for_sort_internal (NautilusFile         *file_1,
						      NautilusFile         *file_2,
						      NautilusFileSortType  sort_type,
						      gboolean              reversed);
static char *nautilus_file_get_date_as_string        (NautilusFile         *file,
						      NautilusDateType      date_type);
static char *nautilus_file_get_owner_as_string       (NautilusFile         *file);
static char *nautilus_file_get_group_as_string       (NautilusFile         *file);
static char *nautilus_file_get_permissions_as_string (NautilusFile         *file);
static char *nautilus_file_get_size_as_string        (NautilusFile         *file);
static char *nautilus_file_get_type_as_string        (NautilusFile         *file);

/**
 * nautilus_file_get:
 * @uri: URI of file to get.
 *
 * Get a file given a uri.
 * Returns a referenced object. Unref when finished.
 * If two windows are viewing the same uri, the file object is shared.
 */
NautilusFile *
nautilus_file_get (const char *uri)
{
	GnomeVFSResult result;
	GnomeVFSFileInfo *file_info;
	GnomeVFSURI *vfs_uri, *directory_vfs_uri;
	char *directory_uri;
	NautilusDirectory *directory;
	NautilusFile *file;

	/* Get info on the file. */
	file_info = gnome_vfs_file_info_new ();
	result = gnome_vfs_get_file_info (uri, file_info,
					  GNOME_VFS_FILE_INFO_GETMIMETYPE
					  | GNOME_VFS_FILE_INFO_FASTMIMETYPE
		  			  | GNOME_VFS_FILE_INFO_FOLLOWLINKS, NULL);
	if (result != GNOME_VFS_OK) {
		return NULL;
	}

	/* Make VFS version of URI. */
	vfs_uri = gnome_vfs_uri_new (uri);
	if (vfs_uri == NULL) {
		return NULL;
	}

	/* Make VFS version of directory URI. */
	directory_vfs_uri = gnome_vfs_uri_get_parent (vfs_uri);
	gnome_vfs_uri_unref (vfs_uri);
	if (directory_vfs_uri == NULL) {
		return NULL;
	}

	/* Make text version of directory URI. */
	directory_uri = gnome_vfs_uri_to_string (directory_vfs_uri,
						 GNOME_VFS_URI_HIDE_NONE);
	gnome_vfs_uri_unref (directory_vfs_uri);

	/* Get object that represents the directory. */
	directory = nautilus_directory_get (directory_uri);
	g_free (directory_uri);
	if (directory == NULL) {
		return NULL;
	}

	file = nautilus_directory_find_file (directory, file_info->name);
	if (file == NULL) {
		file = nautilus_directory_new_file (directory, file_info);
		directory->details->files =
			g_list_append (directory->details->files, file);
	}

	gnome_vfs_file_info_unref (file_info);
	nautilus_file_ref (file);
	gtk_object_unref (GTK_OBJECT (directory));
	
	return file;
}

void
nautilus_file_ref (NautilusFile *file)
{
	g_return_if_fail (file != NULL);

	g_assert (file->ref_count < G_MAXINT);
	g_assert (file->directory != NULL);

	/* Increment the ref count. */
	if (file->ref_count++ != 0) {
		return;
	}

	/* As soon as someone other than the directory holds a ref, 
	 * we need to hold the directory too. */
	gtk_object_ref (GTK_OBJECT (file->directory));
}

void
nautilus_file_unref (NautilusFile *file)
{
	g_return_if_fail (file != NULL);

	g_assert (file->ref_count != 0);
	g_assert (file->directory != NULL);

	/* Decrement the ref count. */
	if (--file->ref_count != 0) {
		return;
	}

	/* No references left, so it's time to release our hold on the directory. */
	gtk_object_unref (GTK_OBJECT (file->directory));
	if (file->is_gone) {
		nautilus_file_free (file);
	}
}

void
nautilus_file_free (NautilusFile *file)
{
	g_assert (file->ref_count == 0);

	/* Destroy the file object. */
	gnome_vfs_file_info_unref (file->info);
	g_free (file);
}

static int
nautilus_file_compare_by_size_with_directories (NautilusFile *file_1, NautilusFile *file_2)
{
	gboolean is_directory_1;
	gboolean is_directory_2;
	int item_count_1;
	int item_count_2;

	is_directory_1 = nautilus_file_is_directory (file_1);
	is_directory_2 = nautilus_file_is_directory (file_2);

	if (is_directory_1 && !is_directory_2) {
		return -1;
	}

	if (is_directory_2 && !is_directory_1) {
		return +1;
	}

	if (!is_directory_1 && !is_directory_2) {
		return 0;
	}

	/* Both are directories, compare by item count. */
	/* FIXME: get_directory_item_count_hack is slow, and calling
	 * it for every pairwise comparison here is nasty. Need to
	 * change this to (not-yet-existent) architecture where the
	 * item count can be calculated once in a deferred way, and
	 * then stored or cached.
	 */
	item_count_1 = get_directory_item_count_hack (file_1, FALSE);
	item_count_2 = get_directory_item_count_hack (file_2, FALSE);

	if (item_count_1 < item_count_2) {
		return -1;
	}

	if (item_count_2 < item_count_1) {
		return +1;
	}

	return 0;
}

static int
nautilus_file_compare_by_type (NautilusFile *file_1, NautilusFile *file_2)
{
	gboolean is_directory_1;
	gboolean is_directory_2;
	char * type_string_1;
	char * type_string_2;
	int result;

	/* Directories go first. Then, if mime types are identical,
	 * don't bother getting strings (for speed). This assumes
	 * that the string is dependent entirely on the mime type,
	 * which is true now but might not be later.
	 */
	is_directory_1 = nautilus_file_is_directory (file_1);
	is_directory_2 = nautilus_file_is_directory (file_2);
	
	if (is_directory_1 && is_directory_2) {
		return 0;
	}

	if (is_directory_1) {
		return -1;
	}

	if (is_directory_2) {
		return +1;
	}

	if (nautilus_strcmp (file_1->info->mime_type, file_2->info->mime_type) == 0) {
		return 0;
	}

	type_string_1 = nautilus_file_get_type_as_string (file_1);
	type_string_2 = nautilus_file_get_type_as_string (file_2);

	result = nautilus_strcmp (type_string_1, type_string_2);

	g_free (type_string_1);
	g_free (type_string_2);

	return result;
}

static int
nautilus_file_compare_for_sort_internal (NautilusFile *file_1,
					 NautilusFile *file_2,
					 NautilusFileSortType sort_type,
					 gboolean reversed)
{
	GnomeVFSDirectorySortRule rules[3];
	int compare;

	g_return_val_if_fail (file_1 != NULL, 0);
	g_return_val_if_fail (file_2 != NULL, 0);
	g_return_val_if_fail (sort_type != NAUTILUS_FILE_SORT_NONE, 0);

	switch (sort_type) {
	case NAUTILUS_FILE_SORT_BY_NAME:
		/* Note: This used to put directories first. I
		 * thought that was counterintuitive and removed it,
		 * but I can imagine discussing this further.
		 * John Sullivan <sullivan@eazel.com>
		 */
		rules[0] = GNOME_VFS_DIRECTORY_SORT_BYNAME_IGNORECASE;
		rules[1] = GNOME_VFS_DIRECTORY_SORT_NONE;
		break;
	case NAUTILUS_FILE_SORT_BY_SIZE:
		/* Compare directory sizes ourselves, then if necessary
		 * use GnomeVFS to compare file sizes.
		 */
		compare = nautilus_file_compare_by_size_with_directories (file_1, file_2);
		if (compare != 0) {
			return compare;
		}
		rules[0] = GNOME_VFS_DIRECTORY_SORT_BYSIZE;
		rules[1] = GNOME_VFS_DIRECTORY_SORT_BYNAME_IGNORECASE;
		rules[2] = GNOME_VFS_DIRECTORY_SORT_NONE;
		break;
	case NAUTILUS_FILE_SORT_BY_TYPE:
		/* GnomeVFS doesn't know about our special text for certain
		 * mime types, so we handle the mime-type sorting ourselves.
		 */
		compare = nautilus_file_compare_by_type (file_1, file_2);
		if (compare != 0) {
			return compare;
		}
		rules[0] = GNOME_VFS_DIRECTORY_SORT_BYNAME_IGNORECASE;
		rules[1] = GNOME_VFS_DIRECTORY_SORT_NONE;
		break;
	case NAUTILUS_FILE_SORT_BY_MTIME:
		rules[0] = GNOME_VFS_DIRECTORY_SORT_BYMTIME;
		rules[1] = GNOME_VFS_DIRECTORY_SORT_BYNAME_IGNORECASE;
		rules[2] = GNOME_VFS_DIRECTORY_SORT_NONE;
		break;
	default:
		g_assert_not_reached ();
		return 0;
	}

	if (reversed) {
		return gnome_vfs_file_info_compare_for_sort_reversed (file_1->info,
								      file_2->info,
								      rules);
	} else {
		return gnome_vfs_file_info_compare_for_sort (file_1->info,
							     file_2->info,
							     rules);
	}
}

/**
 * nautilus_file_compare_for_sort:
 * @file_1: A file object
 * @file_2: Another file object
 * @sort_type: Sort criterion
 * 
 * Return value: int < 0 if @file_1 should come before file_2 in a smallest-to-largest
 * sorted list; int > 0 if @file_2 should come before file_1 in a smallest-to-largest
 * sorted list; 0 if @file_1 and @file_2 are equal for this sort criterion. Note
 * that each named sort type may actually break ties several ways, with the name
 * of the sort criterion being the primary but not only differentiator.
 **/
int
nautilus_file_compare_for_sort (NautilusFile *file_1,
				NautilusFile *file_2,
				NautilusFileSortType sort_type)
{
	return nautilus_file_compare_for_sort_internal (file_1, file_2, sort_type, FALSE);
}

/**
 * nautilus_file_compare_for_sort_reversed:
 * @file_1: A file object
 * @file_2: Another file object
 * @sort_type: Sort criterion
 * 
 * Return value: The opposite of nautilus_file_compare_for_sort: int > 0 if @file_1 
 * should come before file_2 in a smallest-to-largest sorted list; int < 0 if @file_2 
 * should come before file_1 in a smallest-to-largest sorted list; 0 if @file_1 
 * and @file_2 are equal for this sort criterion. Note that each named sort type 
 * may actually break ties several ways, with the name of the sort criterion 
 * being the primary but not only differentiator.
 **/
int
nautilus_file_compare_for_sort_reversed (NautilusFile *file_1,
					 NautilusFile *file_2,
					 NautilusFileSortType sort_type)
{
	return nautilus_file_compare_for_sort_internal (file_1, file_2, sort_type, TRUE);
}

char *
nautilus_file_get_metadata (NautilusFile *file,
			    const char *tag,
			    const char *default_metadata)
{
	g_return_val_if_fail (file != NULL, NULL);

	return nautilus_directory_get_file_metadata (file->directory,
						     file->info->name,
						     tag,
						     default_metadata);
}

void
nautilus_file_set_metadata (NautilusFile *file,
			    const char *tag,
			    const char *default_metadata,
			    const char *metadata)
{
	g_return_if_fail (file != NULL);

	nautilus_directory_set_file_metadata (file->directory,
					      file->info->name,
					      tag,
					      default_metadata,
					      metadata);
}

char *
nautilus_file_get_name (NautilusFile *file)
{
	g_return_val_if_fail (file != NULL, NULL);

	g_assert (file->directory == NULL || NAUTILUS_IS_DIRECTORY (file->directory));
	g_assert (file->info->name != NULL);
	g_assert (file->info->name[0] != '\0');

	return g_strdup (file->info->name);
}

char *
nautilus_file_get_uri (NautilusFile *file)
{
	GnomeVFSURI *uri;
	char *uri_text;

	g_return_val_if_fail (file != NULL, NULL);
	g_return_val_if_fail (file->directory != NULL, NULL);

	uri = gnome_vfs_uri_append_path (file->directory->details->uri, file->info->name);
	uri_text = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE);
	gnome_vfs_uri_unref (uri);
	return uri_text;
}

/**
 * nautilus_file_get_date_as_string:
 * 
 * Get a user-displayable string representing a file modification date. 
 * The caller is responsible for g_free-ing this string.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: Newly allocated string ready to display to the user.
 * 
 **/
static char *
nautilus_file_get_date_as_string (NautilusFile *file, NautilusDateType date_type)
{	 
	struct tm *file_time;
	const char *format;
	GDate *today;
	GDate *file_date;
	guint32 file_date_age;

	g_return_val_if_fail (file != NULL, NULL);

	switch (date_type)
	{
		case NAUTILUS_DATE_TYPE_CHANGED:
			file_time = localtime(&file->info->ctime);
			break;
		case NAUTILUS_DATE_TYPE_ACCESSED:
			file_time = localtime(&file->info->atime);
			break;
		case NAUTILUS_DATE_TYPE_MODIFIED:
			file_time = localtime(&file->info->mtime);
			break;
		default:
			g_assert_not_reached ();
	}
	file_date = nautilus_g_date_new_tm (file_time);
	
	today = g_date_new ();
	g_date_set_time (today, time (NULL));

	/* Overflow results in a large number; fine for our purposes. */
	file_date_age = g_date_julian (today) - g_date_julian (file_date);

	g_date_free (file_date);
	g_date_free (today);

	/* Format varies depending on how old the date is. This minimizes
	 * the length (and thus clutter & complication) of typical dates
	 * while providing sufficient detail for recent dates to make
	 * them maximally understandable at a glance. Keep all format
	 * strings separate rather than combining bits & pieces for
	 * internationalization's sake.
	 */

	if (file_date_age == 0)	{
		/* today, use special word */
		format = _("today %-I:%M %p");
	} else if (file_date_age == 1) {
		/* yesterday, use special word */
		format = _("yesterday %-I:%M %p");
	} else if (file_date_age < 7) {
		/* current week, include day of week */
		format = _("%A %-m/%-d/%y %-I:%M %p");
	} else {
		format = _("%-m/%-d/%y %-I:%M %p");
	}

	return nautilus_strdup_strftime (format, file_time);
}

/**
 * nautilus_file_get_directory_item_count
 * 
 * Get the number of items in a directory.
 * @file: NautilusFile representing a directory. It is an error to
 * call this function on a file that is not a directory.
 * @ignore_invisible_items: TRUE if invisible items should not be
 * included in count.
 * 
 * Returns: item count for this directory.
 * 
 **/
guint
nautilus_file_get_directory_item_count (NautilusFile *file, 
					gboolean ignore_invisible_items)
{
	g_return_val_if_fail (nautilus_file_is_directory (file), 0);

	return get_directory_item_count_hack (file, ignore_invisible_items);
}

/**
 * nautilus_file_get_size
 * 
 * Get the file size.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: Size in bytes.
 * 
 **/
GnomeVFSFileSize
nautilus_file_get_size (NautilusFile *file)
{
	g_return_val_if_fail (file != NULL, 0);

	return file->info->size;
}

/**
 * nautilus_file_get_permissions_as_string:
 * 
 * Get a user-displayable string representing a file's permissions. The caller
 * is responsible for g_free-ing this string.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: Newly allocated string ready to display to the user.
 * 
 **/
static char *
nautilus_file_get_permissions_as_string (NautilusFile *file)
{
	GnomeVFSFilePermissions permissions;
	gboolean is_directory;
	gboolean is_link;

	permissions = file->info->permissions;
	is_directory = nautilus_file_is_directory (file);
	is_link = GNOME_VFS_FILE_INFO_SYMLINK(file->info);
	
	return g_strdup_printf ("%c%c%c%c%c%c%c%c%c%c",
				 is_link ? 'l' : is_directory ? 'd' : '-',
		 		 permissions & GNOME_VFS_PERM_USER_READ ? 'r' : '-',
				 permissions & GNOME_VFS_PERM_USER_WRITE ? 'w' : '-',
				 permissions & GNOME_VFS_PERM_USER_EXEC ? 'x' : '-',
				 permissions & GNOME_VFS_PERM_GROUP_READ ? 'r' : '-',
				 permissions & GNOME_VFS_PERM_GROUP_WRITE ? 'w' : '-',
				 permissions & GNOME_VFS_PERM_GROUP_EXEC ? 'x' : '-',
				 permissions & GNOME_VFS_PERM_OTHER_READ ? 'r' : '-',
				 permissions & GNOME_VFS_PERM_OTHER_WRITE ? 'w' : '-',
				 permissions & GNOME_VFS_PERM_OTHER_EXEC ? 'x' : '-');
}

/**
 * nautilus_file_get_owner_as_string:
 * 
 * Get a user-displayable string representing a file's owner. The caller
 * is responsible for g_free-ing this string.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: Newly allocated string ready to display to the user.
 * 
 **/
static char *
nautilus_file_get_owner_as_string (NautilusFile *file)
{
	struct passwd *password_info;

	/* FIXME: Can we trust the uid in the file info? Might
	 * there be garbage there? What will it do for non-local files?
	 */
	/* No need to free result of getpwuid */
	password_info = getpwuid (file->info->uid);

	g_print ("pointer to password info is %p\n", password_info);

	if (password_info == NULL) {
		return g_strdup (_("unknown owner"));
	}
	
	return g_strdup (password_info->pw_name);
}

/**
 * nautilus_file_get_group_as_string:
 * 
 * Get a user-displayable string representing a file's group. The caller
 * is responsible for g_free-ing this string.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: Newly allocated string ready to display to the user.
 * 
 **/
static char *
nautilus_file_get_group_as_string (NautilusFile *file)
{
	struct group *group_info;

	/* FIXME: Can we trust the gid in the file info? Might
	 * there be garbage there? What will it do for non-local files?
	 */
	/* No need to free result of getgrgid */
	group_info = getgrgid (file->info->gid);

	if (group_info == NULL) {
		return g_strdup (_("unknown group"));
	}
	
	return g_strdup (group_info->gr_name);
}


/* This #include is part of the following hack, and should be removed with it */
#include <dirent.h>

static int
get_directory_item_count_hack (NautilusFile *file, gboolean ignore_invisible_items)
{
 	/* Code borrowed from Gnomad and hacked into here for now */

	char * uri;
	char * path;
	DIR* directory;
	int count;
	struct dirent * entry;
	
	g_assert (nautilus_file_is_directory (file));
	
	uri = nautilus_file_get_uri (file);
	if (nautilus_str_has_prefix (uri, "file://")) {
		path = uri + 7;
	} else {
		path = uri;
	}
	
	directory = opendir (path);
	
	g_free (uri);
	
	if (!directory) {
		return 0;
	}
        
	count = 0;
	
	while ((entry = readdir(directory)) != NULL) {
		// Only count invisible items if requested.
		if (!ignore_invisible_items || entry->d_name[0] != '.') {
			count += 1;
		}
	}
	
	closedir(directory);
	
	/* This way of getting the count includes . and .., so we subtract those out */
	if (!ignore_invisible_items) {
		count -= 2;
	}

	return count;
}

/**
 * nautilus_file_get_size_as_string:
 * 
 * Get a user-displayable string representing a file size. The caller
 * is responsible for g_free-ing this string. The string is an item
 * count for directories.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: Newly allocated string ready to display to the user.
 * 
 **/
static char *
nautilus_file_get_size_as_string (NautilusFile *file)
{
	g_return_val_if_fail (file != NULL, NULL);

	if (nautilus_file_is_directory (file)) {
		/* FIXME: Since computing the item count is slow, we
		 * want to do it in a deferred way. However, that
		 * architecture doesn't exist yet, so we're hacking
		 * it in for now.
		 */
		int item_count;

		item_count = get_directory_item_count_hack (file, FALSE);
		if (item_count == 0) {
			return g_strdup (_("0 items"));
		} else if (item_count == 1) {
			return g_strdup (_("1 item"));
		} else {
			return g_strdup_printf (_("%d items"), item_count);
		}
	}

	return gnome_vfs_file_size_to_string (file->info->size);
}

/**
 * nautilus_file_get_string_attribute:
 * 
 * Get a user-displayable string from a named attribute. Use g_free to
 * free this string.
 * 
 * @file: NautilusFile representing the file in question.
 * @attribute_name: The name of the desired attribute. The currently supported
 * set includes "name", "type", "size", "date_modified", "date_changed",
 * "date_accessed", "owner", "group", "permissions".
 * 
 * Returns: Newly allocated string ready to display to the user, or NULL
 * if @attribute_name is not supported.
 * 
 **/
char *
nautilus_file_get_string_attribute (NautilusFile *file, const char *attribute_name)
{
	/* FIXME: Use hash table and switch statement or function pointers for speed? */

	if (strcmp (attribute_name, "name") == 0) {
		return nautilus_file_get_name (file);
	}

	if (strcmp (attribute_name, "type") == 0) {
		return nautilus_file_get_type_as_string (file);
	}

	if (strcmp (attribute_name, "size") == 0) {
		return nautilus_file_get_size_as_string (file);
	}

	if (strcmp (attribute_name, "date_modified") == 0) {
		return nautilus_file_get_date_as_string (file, 
							 NAUTILUS_DATE_TYPE_MODIFIED);
	}

	if (strcmp (attribute_name, "date_changed") == 0) {
		return nautilus_file_get_date_as_string (file, 
							 NAUTILUS_DATE_TYPE_CHANGED);
	}

	if (strcmp (attribute_name, "date_accessed") == 0) {
		return nautilus_file_get_date_as_string (file,
							 NAUTILUS_DATE_TYPE_ACCESSED);
	}

	if (strcmp (attribute_name, "permissions") == 0) {
		return nautilus_file_get_permissions_as_string (file);
	}

	if (strcmp (attribute_name, "owner") == 0) {
		return nautilus_file_get_owner_as_string (file);
	}

	if (strcmp (attribute_name, "group") == 0) {
		return nautilus_file_get_group_as_string (file);
	}

	return NULL;
}

/**
 * nautilus_file_get_type_as_string:
 * 
 * Get a user-displayable string representing a file type. The caller
 * is responsible for g_free-ing this string.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: Newly allocated string ready to display to the user.
 * 
 **/
static char *
nautilus_file_get_type_as_string (NautilusFile *file)
{
	g_return_val_if_fail (file != NULL, NULL);

	if (nautilus_file_is_directory (file)) {
		/* Special-case this so it isn't "special/directory".
		 * FIXME: Should this be "folder" instead?
		 */		
		return g_strdup (_("directory"));
	}

	if (nautilus_strlen (file->info->mime_type) == 0) {
		return g_strdup (_("unknown type"));
	}

	return g_strdup (file->info->mime_type);
}

/**
 * nautilus_file_get_type
 * 
 * Return this file's type.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: The type.
 * 
 **/
GnomeVFSFileType
nautilus_file_get_type (NautilusFile *file)
{
	g_return_val_if_fail (file != NULL, FALSE);

	return file->info->type;
}

/**
 * nautilus_file_get_mime_type
 * 
 * Return this file's mime type.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: The mime type.
 * 
 **/
const char *
nautilus_file_get_mime_type (NautilusFile *file)
{
	g_return_val_if_fail (file != NULL, FALSE);

	return file->info->mime_type;
}

/**
 * nautilus_file_get_keywords
 * 
 * Return this file's keywords.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: A list of keywords.
 * 
 **/
GList *
nautilus_file_get_keywords (NautilusFile *file)
{
	GList *keywords;
	xmlNode *file_node, *child;
	xmlChar *property;

	g_return_val_if_fail (file != NULL, NULL);

	keywords = NULL;

	/* Put all the keywords into a list. */
	file_node = nautilus_directory_get_file_metadata_node (file->directory,
							       file->info->name,
							       FALSE);
	if (file_node != NULL) {
		for (child = file_node->childs; child != NULL; child = child->next) {
			if (strcmp (child->name, "KEYWORD") == 0) {
				property = xmlGetProp (child, "NAME");
				if (property != NULL) {
					keywords = g_list_prepend (keywords,
								   g_strdup (property));
				}
			}
		}
	}

	return g_list_reverse (keywords);
}

/**
 * nautilus_file_set_keywords
 * 
 * Change this file's keywords.
 * @file: NautilusFile representing the file in question.
 * @keywords: New set of keywords (a GList of strings).
 *
 **/
void
nautilus_file_set_keywords (NautilusFile *file, GList *keywords)
{
	xmlNode *file_node, *child, *next;
	GList *p;
	gboolean need_write;
	xmlChar *property;

	g_return_if_fail (file != NULL);

	/* Put all the keywords into a list. */
	file_node = nautilus_directory_get_file_metadata_node (file->directory,
							       file->info->name,
							       keywords != NULL);
	need_write = FALSE;
	if (file_node != NULL) {
		p = keywords;

		/* Remove any nodes except the ones we expect. */
		for (child = file_node->childs; child != NULL; child = next) {
			next = child->next;
			if (strcmp (child->name, "KEYWORD") == 0) {
				property = xmlGetProp (child, "NAME");
				if (property != NULL && p != NULL && strcmp (property, p->data) == 0) {
					p = p->next;
				} else {
					xmlUnlinkNode (child);
					xmlFreeNode (child);
					need_write = TRUE;
				}
			}
		}

		/* Add any additional nodes needed. */
		for (; p != NULL; p = p->next) {
			child = xmlNewChild (file_node, NULL, "KEYWORD", NULL);
			xmlSetProp (child, "NAME", p->data);
			need_write = TRUE;
		}
	}

	if (need_write) {
		/* Since we changed the tree, arrange for it to be written. */
		nautilus_directory_request_write_metafile (file->directory);
		nautilus_file_changed (file);
	}
}

/**
 * nautilus_file_is_symbolic_link
 * 
 * Check if this file is a symbolic link.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: True if the file is a symbolic link.
 * 
 **/
gboolean
nautilus_file_is_symbolic_link (NautilusFile *file)
{
	g_return_val_if_fail (file != NULL, FALSE);

	return GNOME_VFS_FILE_INFO_SYMLINK (file->info);
}

/**
 * nautilus_file_is_directory
 * 
 * Check if this file is a directory.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: TRUE if @file is a directory.
 * 
 **/
gboolean
nautilus_file_is_directory (NautilusFile *file)
{
	g_return_val_if_fail (file != NULL, FALSE);

	return nautilus_file_get_type (file) == GNOME_VFS_FILE_TYPE_DIRECTORY;
}

/**
 * nautilus_file_is_executable
 * 
 * Check if this file is executable at all.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: True if any of the execute bits are set.
 * 
 **/
gboolean
nautilus_file_is_executable (NautilusFile *file)
{
	g_return_val_if_fail (file != NULL, FALSE);

	return (file->info->flags & (GNOME_VFS_PERM_USER_EXEC
				     | GNOME_VFS_PERM_GROUP_EXEC
				     | GNOME_VFS_PERM_OTHER_EXEC)) != 0;
}

/**
 * nautilus_file_delete
 * 
 * Delete this file.
 * @file: NautilusFile representing the file in question.
 **/
void
nautilus_file_delete (NautilusFile *file)
{
	char *text_uri;
	GnomeVFSResult result;
	GList *removed_files;

	g_return_if_fail (file != NULL);

	/* Deleting a file that's already gone is easy. */
	if (file->is_gone) {
		return;
	}

	/* Do the actual deletion. */
	text_uri = nautilus_file_get_uri (file);
	if (nautilus_file_is_directory (file)) {
		result = gnome_vfs_remove_directory (text_uri);
	} else {
		result = gnome_vfs_unlink (text_uri);
	}
	g_free (text_uri);

	/* Mark the file gone. */
	if (result == GNOME_VFS_OK || result == GNOME_VFS_ERROR_NOTFOUND) {
		file->is_gone = TRUE;

		/* Let the directory know it's gone. */
		if (file->directory != NULL) {
		        file->directory->details->files
			     = g_list_remove (file->directory->details->files, file);

			/* Send out a signal. */
			removed_files = g_list_prepend (NULL, file);
			nautilus_directory_files_removed (file->directory, removed_files);
			g_list_free (removed_files);
		}
	}
}

/**
 * nautilus_file_changed
 * 
 * Notify the user that this file has changed.
 * @file: NautilusFile representing the file in question.
 **/
void
nautilus_file_changed (NautilusFile *file)
{
	GList *changed_files;

	/* Send out a signal. */
	changed_files = g_list_prepend (NULL, file);
	nautilus_directory_files_changed (file->directory, changed_files);
	g_list_free (changed_files);
}

/**
 * nautilus_file_is_gone
 * 
 * Check if a file has already been deleted.
 * @file: NautilusFile representing the file in question.
 *
 * Returns: TRUE if the file is already gone.
 **/
gboolean
nautilus_file_is_gone (NautilusFile *file)
{
	g_return_val_if_fail (file != NULL, FALSE);

	return file->is_gone;
}

/**
 * nautilus_file_list_ref
 *
 * Ref all the files in a list.
 * @file_list: GList of files.
 **/
void
nautilus_file_list_ref (GList *file_list)
{
	g_list_foreach (file_list, (GFunc) nautilus_file_ref, NULL);
}

/**
 * nautilus_file_list_unref
 *
 * Unref all the files in a list.
 * @file_list: GList of files.
 **/
void
nautilus_file_list_unref (GList *file_list)
{
	g_list_foreach (file_list, (GFunc) nautilus_file_ref, NULL);
}

/**
 * nautilus_file_list_free
 *
 * Free a list of files after unrefing them.
 * @file_list: GList of files.
 **/
void
nautilus_file_list_free (GList *file_list)
{
	nautilus_file_list_unref (file_list);
	g_list_free (file_list);
}