File: Attributes.m

package info (click to toggle)
gworkspace 0.9.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 20,832 kB
  • sloc: objc: 69,382; sh: 488; makefile: 39
file content (853 lines) | stat: -rw-r--r-- 23,707 bytes parent folder | download
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
/* Attributes.m
 *  
 * Copyright (C) 2004-2016 Free Software Foundation, Inc.
 *
 * Author: Enrico Sersale <enrico@imago.ro>
 * Date: January 2004
 *
 * This file is part of the GNUstep GWorkspace application
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
 */

#include <math.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import "Attributes.h"
#import "Inspector.h"
#import "IconView.h"
#import "TimeDateView.h"
#import "Functions.h"
#import "FSNodeRep.h"

#define SINGLE 0
#define MULTIPLE 1

#ifdef __WIN32__
	#define S_IRUSR _S_IRUSR
	#define S_IWUSR _S_IWUSR
	#define S_IXUSR _S_IXUSR
#endif

#define ICNSIZE 48

static NSString *nibName = @"Attributes";

static BOOL sizeStop = NO;

@implementation Attributes

- (void)dealloc
{
  [nc removeObserver: self];  
  DESTROY (sizerConn);
  DESTROY (sizer);
  RELEASE (mainBox);
  RELEASE (calculateButt);
  RELEASE (insppaths);
  RELEASE (attributes);
  RELEASE (currentPath);
  RELEASE (onImage);
  RELEASE (offImage);
  RELEASE (multipleImage);  
  
  [super dealloc];
}

- (id)initForInspector:(id)insp
{
  self = [super init];
  
  if (self) {
    NSBundle *bundle = [NSBundle bundleForClass: [insp class]];
    NSString *imagepath;

    if ([NSBundle loadNibNamed: nibName owner: self] == NO) {
      NSLog(@"failed to load %@!", nibName);
      DESTROY (self);
      return self;
    } 

    RETAIN (mainBox);
    RELEASE (win);

    inspector = insp;
    [iconView setInspector: inspector];
		insppaths = nil;
		attributes = nil;    
    currentPath = nil;
    sizer = nil;
    
    fm = [NSFileManager defaultManager];	
    nc = [NSNotificationCenter defaultCenter];
    
    autocalculate = [[NSUserDefaults standardUserDefaults] boolForKey: @"auto_calculate_sizes"];
    RETAIN (calculateButt);
    
    if (autocalculate) {
      [calculateButt removeFromSuperview];
    }
        
    imagepath = [bundle pathForResource: @"switchOn" ofType: @"tiff"];
    onImage = [[NSImage alloc] initWithContentsOfFile: imagepath]; 
    imagepath = [bundle pathForResource: @"switchOff" ofType: @"tiff"];
    offImage = [[NSImage alloc] initWithContentsOfFile: imagepath]; 
    imagepath = [bundle pathForResource: @"switchMultiple" ofType: @"tiff"];
    multipleImage = [[NSImage alloc] initWithContentsOfFile: imagepath]; 

    [ureadbutt setImage: offImage];
    [ureadbutt setAlternateImage: onImage];           
    [ureadbutt setTag: SINGLE];           
    [greadbutt setImage: offImage];
    [greadbutt setAlternateImage: onImage]; 
    [greadbutt setTag: SINGLE];               
    [oreadbutt setImage: offImage];
    [oreadbutt setAlternateImage: onImage];  
    [oreadbutt setTag: SINGLE];             
    [uwritebutt setImage: offImage];
    [uwritebutt setAlternateImage: onImage]; 
    [uwritebutt setTag: SINGLE];              
    [gwritebutt setImage: offImage];
    [gwritebutt setAlternateImage: onImage]; 
    [gwritebutt setTag: SINGLE];              
    [owritebutt setImage: offImage];
    [owritebutt setAlternateImage: onImage]; 
    [owritebutt setTag: SINGLE];              
    [uexebutt setImage: offImage];
    [uexebutt setAlternateImage: onImage];   
    [uexebutt setTag: SINGLE];             
    [gexebutt setImage: offImage];
    [gexebutt setAlternateImage: onImage];   
    [gexebutt setTag: SINGLE];             
    [oexebutt setImage: offImage];
    [oexebutt setAlternateImage: onImage];     
    [oexebutt setTag: SINGLE];       

    [revertButt setEnabled: NO];
    [okButt setEnabled: NO];
  
    /* Internationalization */
    [linkToLabel setStringValue: NSLocalizedString(@"Link to:", @"")];
    [sizeLabel setStringValue: NSLocalizedString(@"Size:", @"")];
    [calculateButt setTitle: NSLocalizedString(@"Calculate", @"")];
    [ownerLabel setStringValue: NSLocalizedString(@"Owner:", @"")];
    [groupLabel setStringValue: NSLocalizedString(@"Group:", @"")];
    [changedDateBox setTitle: NSLocalizedString(@"Changed", @"")];
    [permsBox setTitle: NSLocalizedString(@"Permissions", @"")];
    [readLabel setStringValue: NSLocalizedString(@"Read", @"")];
    [writeLabel setStringValue: NSLocalizedString(@"Write", @"")];
    [executeLabel setStringValue: NSLocalizedString(@"Execute", @"")];
    [uLabel setStringValue: NSLocalizedString(@"Owner_short", @"")];
    [gLabel setStringValue: NSLocalizedString(@"Group", @"")];
    [oLabel setStringValue: NSLocalizedString(@"Other", @"")];
    [insideButt setTitle: NSLocalizedString(@"also apply to files inside selection", @"")];
    [revertButt setTitle: NSLocalizedString(@"Revert", @"")];
    [okButt setTitle: NSLocalizedString(@"OK", @"")];
  } 
		
  return self;
}

- (NSView *)inspView
{
  return mainBox;
}

- (NSString *)winname
{
  return NSLocalizedString(@"Attributes Inspector", @"");
}

- (void)activateForPaths:(NSArray *)paths
{
  NSString *fpath;
  NSString *ftype;
  NSString *usr, *grp, *tmpusr, *tmpgrp;
  NSDate *date;
  NSCalendarDate *cdate;
  NSDictionary *attrs;
  unsigned long perms;
  BOOL sameOwner, sameGroup;
  int i;

  sizeStop = YES;

  if (paths == nil) {
    DESTROY (insppaths);
    return;
  }
  	
  attrs = [fm fileAttributesAtPath: [paths objectAtIndex: 0] traverseLink: NO];

  ASSIGN (insppaths, paths);
  pathscount = [insppaths count];	
  ASSIGN (currentPath, [paths objectAtIndex: 0]);		
  ASSIGN (attributes, attrs);	

  [revertButt setEnabled: NO];
  [okButt setEnabled: NO];
  	
  if (pathscount == 1)
    { /* Single Selection */

      FSNode *node = [FSNode nodeWithPath: currentPath];
      NSImage *icon = [[FSNodeRep sharedInstance] iconOfSize: ICNSIZE forNode: node];
      
      [iconView setImage: icon];
      [titleField setStringValue: [node name]];
      
      usr = [attributes objectForKey: NSFileOwnerAccountName];
      grp = [attributes objectForKey: NSFileGroupOwnerAccountName];
      date = [attributes objectForKey: NSFileModificationDate];
      perms = [[attributes objectForKey: NSFilePosixPermissions] unsignedLongValue];			
      
#ifdef __WIN32__
      iamRoot = YES;
#else
      iamRoot = (geteuid() == 0);
#endif
      
      isMyFile = ([NSUserName() isEqual: usr]);
      
      [insideButt setState: NSOffState];
      
      ftype = [attributes objectForKey: NSFileType];
      if ([ftype isEqual: NSFileTypeDirectory] == NO)
        {	
          NSString *fsize = fsDescription([[attributes objectForKey: NSFileSize] unsignedLongLongValue]);
          [sizeField setStringValue: fsize]; 
          [calculateButt setEnabled: NO];
          [insideButt	setEnabled: NO];
        }
      else
        {
          [sizeField setStringValue: @"--"]; 
          
          if (autocalculate)
            {
              if (sizer == nil)
                [self startSizer];
              else
                [sizer computeSizeOfPaths: insppaths];
            }
          else
            {
              [calculateButt setEnabled: YES];
            }
          
          [insideButt	setEnabled: YES];
        }
      
                
      if ([ftype isEqual: NSFileTypeSymbolicLink])
        {
          NSString *s;

          s = [fm pathContentOfSymbolicLinkAtPath: currentPath];
          s = relativePathFit(linkToField, s);
          [linkToField setStringValue: s];
          [linkToLabel setTextColor: [NSColor blackColor]];		
          [linkToField setTextColor: [NSColor blackColor]];		      
        }
      else
        {
          [linkToField setStringValue: @""];
          [linkToLabel setTextColor: [NSColor darkGrayColor]];		
          [linkToField setTextColor: [NSColor darkGrayColor]];		
        }
      
      [ownerField setStringValue: usr]; 
      [groupField setStringValue: grp]; 
      
      [self setPermissions: perms isActive: (iamRoot || isMyFile)];
      
      cdate = [date dateWithCalendarFormat: nil timeZone: nil];	
      [timeDateView setDate: cdate];
      
    }
  else
    { /* Multiple Selection */
      NSImage *icon = [[FSNodeRep sharedInstance] multipleSelectionIconOfSize: ICNSIZE];
      NSString *items = NSLocalizedString(@"items", @"");
      
      items = [NSString stringWithFormat: @"%lu %@", (unsigned long)[paths count], items];
      [titleField setStringValue: items];  
      [iconView setImage: icon];
  
      [attributes objectForKey: NSFileType];
      
      [sizeField setStringValue: @"--"]; 
      
      if (autocalculate)
        {
          if (sizer == nil)
            [self startSizer];
          else
            [sizer computeSizeOfPaths: insppaths];
        }
      else
        {
          [calculateButt setEnabled: YES];
        }
    
      usr = [attributes objectForKey: NSFileOwnerAccountName];
      grp = [attributes objectForKey: NSFileGroupOwnerAccountName];
      date = [attributes objectForKey: NSFileModificationDate];		

      sameOwner = YES;
      sameGroup = YES;
		
      for (i = 0; i < [insppaths count]; i++)
        {
          fpath = [insppaths objectAtIndex: i];
          attrs = [fm fileAttributesAtPath: fpath traverseLink: NO];
          tmpusr = [attrs objectForKey: NSFileOwnerAccountName];
          if ([tmpusr isEqualToString: usr] == NO)
            sameOwner = NO;
          tmpgrp = [attrs objectForKey: NSFileGroupOwnerAccountName];
          if ([tmpgrp isEqualToString: grp] == NO)
            sameGroup = NO;
        }
      
      if(sameOwner == NO)
        usr = @"-";

      if(sameGroup == NO)
        grp = @"-";

      
#ifdef __WIN32__
      iamRoot = YES;
#else
      iamRoot = (geteuid() == 0);
#endif
                
      isMyFile = ([NSUserName() isEqualToString: usr]);	
				
      [linkToLabel setTextColor: [NSColor darkGrayColor]];		
      [linkToField setStringValue: @""];

      [ownerField setStringValue: usr]; 
      [groupField setStringValue: grp]; 
    
      [insideButt setEnabled: YES];
    
      [self setPermissions: 0 isActive: (iamRoot || isMyFile)];
		
      cdate = [date dateWithCalendarFormat: nil timeZone: nil];	
      [timeDateView setDate: cdate];
    }
	
  [mainBox setNeedsDisplay: YES];
}

- (IBAction)permsButtonsAction:(id)sender
{
	if (multiplePaths == YES) {
		if ([sender state] == NSOffState) {
			if ([sender tag] == MULTIPLE) {
				[sender setImage: offImage];	
				[sender setTag: SINGLE];	
      }
		} else {
			if ([sender tag] == SINGLE) {
				[sender setImage: multipleImage];
				[sender setTag: MULTIPLE];	
			}
		}
	}	

	if ((iamRoot || isMyFile) == NO) {
		return;
	}

	[revertButt setEnabled: YES];	
	[okButt setEnabled: YES];
}

- (IBAction)insideButtonAction:(id)sender
{
  [okButt setEnabled: YES];
}

- (IBAction)changePermissions:(id)sender
{
	NSMutableDictionary *attrs;
	NSDirectoryEnumerator *enumerator;	
	NSString *path, *fpath;
	unsigned long oldperms, newperms;
  int i;
	BOOL isdir;
	BOOL recursive;
  
  recursive = ([insideButt isEnabled] && ([insideButt state] == NSOnState));
  
	if (pathscount == 1) {
		oldperms = [[attributes objectForKey: NSFilePosixPermissions] unsignedLongValue];
		newperms = [self getPermissions: oldperms];		
		attrs = [attributes mutableCopy];
		[attrs setObject: [NSNumber numberWithInt: newperms] forKey: NSFilePosixPermissions];
		[fm changeFileAttributes: attrs atPath: currentPath];	
    RELEASE (attrs);

		[fm fileExistsAtPath: currentPath isDirectory: &isdir];

		if (isdir && recursive) {
			enumerator = [fm enumeratorAtPath: currentPath];
      
      while ((fpath = [enumerator nextObject])) {
        CREATE_AUTORELEASE_POOL(arp);  
      
				fpath = [currentPath stringByAppendingPathComponent: fpath];
				attrs = [[fm fileAttributesAtPath: fpath traverseLink: NO] mutableCopy];
				oldperms = [[attrs objectForKey: NSFilePosixPermissions] unsignedLongValue];	
				newperms = [self getPermissions: oldperms];			
				[attrs setObject: [NSNumber numberWithInt: newperms] forKey: NSFilePosixPermissions];
				[fm changeFileAttributes: attrs atPath: fpath];
        RELEASE (attrs);
        
        RELEASE (arp);
      }
                  
			ASSIGN (attributes, [fm fileAttributesAtPath: currentPath traverseLink: NO]);	
			[self setPermissions: 0 isActive: YES];

		} else {
			ASSIGN (attributes, [fm fileAttributesAtPath: currentPath traverseLink: NO]);	
			newperms = [[attributes objectForKey: NSFilePosixPermissions] unsignedLongValue];				
			[self setPermissions: newperms isActive: YES];
		}
	
	} else {
		for (i = 0; i < [insppaths count]; i++) {
			path = [insppaths objectAtIndex: i];			
 			attrs = [[fm fileAttributesAtPath: path traverseLink: NO] mutableCopy];
			oldperms = [[attrs objectForKey: NSFilePosixPermissions] unsignedLongValue];	
			newperms = [self getPermissions: oldperms];			
			[attrs setObject: [NSNumber numberWithInt: newperms] forKey: NSFilePosixPermissions];
			[fm changeFileAttributes: attrs atPath: path];	
      RELEASE (attrs);			

			[fm fileExistsAtPath: path isDirectory: &isdir];
     
			if (isdir && recursive) {
				enumerator = [fm enumeratorAtPath: path];
        
        while ((fpath = [enumerator nextObject])) {
          CREATE_AUTORELEASE_POOL(arp);  

					fpath = [path stringByAppendingPathComponent: fpath];
					attrs = [[fm fileAttributesAtPath: fpath traverseLink: NO] mutableCopy];
					oldperms = [[attrs objectForKey: NSFilePosixPermissions] unsignedLongValue];	
					newperms = [self getPermissions: oldperms];			
					[attrs setObject: [NSNumber numberWithInt: newperms] forKey: NSFilePosixPermissions];
					[fm changeFileAttributes: attrs atPath: fpath];
					RELEASE (attrs);

          RELEASE (arp);
        }
			}
		}
		
		ASSIGN (attributes, [fm fileAttributesAtPath: currentPath traverseLink: NO]);	
		[self setPermissions: 0 isActive: YES];
	}

	[okButt setEnabled: NO];
	[revertButt setEnabled: NO];
}

- (IBAction)revertToOldPermissions:(id)sender
{
	if(pathscount == 1) {
		unsigned long perms = [[attributes objectForKey: NSFilePosixPermissions] unsignedLongValue];
		[self setPermissions: perms isActive: YES];	
	} else {
		[self setPermissions: 0 isActive: YES];
	}
	
	[revertButt setEnabled: NO];
	[okButt setEnabled: NO];
}

- (void)setPermissions:(unsigned long)perms 
              isActive:(BOOL)active
{
	if (active == NO) {
		[ureadbutt setEnabled: NO];						
		[uwritebutt setEnabled: NO];	
		[uexebutt setEnabled: NO];	

	#ifndef __WIN32__
		[greadbutt setEnabled: NO];						
		[gwritebutt setEnabled: NO];
		[gexebutt setEnabled: NO];			
		[oreadbutt setEnabled: NO];						
		[owritebutt setEnabled: NO];
		[oexebutt setEnabled: NO];	
	#endif
								
	} else {
		[ureadbutt setEnabled: YES];						
		[uwritebutt setEnabled: YES];		
		[uexebutt setEnabled: YES];	

	#ifndef __WIN32__
		[greadbutt setEnabled: YES];						
		[gwritebutt setEnabled: YES];		
		[gexebutt setEnabled: YES];	
		[oreadbutt setEnabled: YES];						
		[owritebutt setEnabled: YES];			
		[oexebutt setEnabled: YES];	
	#endif
	}

	if (perms == 0) {
		multiplePaths = YES;
		[ureadbutt setImage: multipleImage];
		[ureadbutt setState: NSOffState];
    [ureadbutt setTag: MULTIPLE];           
		[uwritebutt setImage: multipleImage];
		[uwritebutt setState: NSOffState];
    [uwritebutt setTag: MULTIPLE];               
		[uexebutt setImage: multipleImage];
		[uexebutt setState: NSOffState];	
    [uexebutt setTag: MULTIPLE];           

	#ifndef __WIN32__
		[greadbutt setImage: multipleImage];
		[greadbutt setState: NSOffState];
    [greadbutt setTag: MULTIPLE];               
		[gwritebutt setImage: multipleImage];
		[gwritebutt setState: NSOffState];
    [gwritebutt setTag: MULTIPLE];               
		[gexebutt setImage: multipleImage];
		[gexebutt setState: NSOffState];
    [gexebutt setTag: MULTIPLE];               
		[oreadbutt setImage: multipleImage];
		[oreadbutt setState: NSOffState];
    [oreadbutt setTag: MULTIPLE];               
		[owritebutt setImage: multipleImage];
		[owritebutt setState: NSOffState];
    [owritebutt setTag: MULTIPLE];               
		[oexebutt setImage: multipleImage];
		[oexebutt setState: NSOffState];
    [oexebutt setTag: MULTIPLE];           
	#endif
	
		return;
	} else {
		multiplePaths = NO;
		[ureadbutt setImage: offImage];
    [ureadbutt setTag: SINGLE];               
		[uwritebutt setImage: offImage];
    [uwritebutt setTag: SINGLE];               
		[uexebutt setImage: offImage];	
    [uexebutt setTag: SINGLE];           

	#ifndef __WIN32__
		[greadbutt setImage: offImage];
    [greadbutt setTag: SINGLE];               
		[gwritebutt setImage: offImage];
    [gwritebutt setTag: SINGLE];               
		[gexebutt setImage: offImage];
    [gexebutt setTag: SINGLE];               
		[oreadbutt setImage: offImage];
    [oreadbutt setTag: SINGLE];               
		[owritebutt setImage: offImage];
    [owritebutt setTag: SINGLE];               
		[oexebutt setImage: offImage];
    [oexebutt setTag: SINGLE];           
    
	#endif
	}

#define SET_BUTTON_STATE(b, v) { \
if ((perms & v) == v) [b setState: NSOnState]; \
else [b setState: NSOffState]; \
}

	SET_BUTTON_STATE (ureadbutt, S_IRUSR);				
	SET_BUTTON_STATE (uwritebutt, S_IWUSR);
	SET_BUTTON_STATE (uexebutt, S_IXUSR);

#ifndef __WIN32__
	SET_BUTTON_STATE (greadbutt, S_IRGRP);
	SET_BUTTON_STATE (gwritebutt, S_IWGRP);
	SET_BUTTON_STATE (gexebutt, S_IXGRP);
	SET_BUTTON_STATE (oreadbutt, S_IROTH);
	SET_BUTTON_STATE (owritebutt, S_IWOTH);
	SET_BUTTON_STATE (oexebutt, S_IXOTH);
#endif
}

- (unsigned long)getPermissions:(unsigned long)oldperms
{
	unsigned long perms = 0;

#define GET_BUTTON_STATE(b, v) { \
if ([b state] == NSOnState) { \
perms |= v; \
} else { \
if ((oldperms & v) == v) { \
if ([b tag] == MULTIPLE) perms |= v; \
} } \
}

	GET_BUTTON_STATE (ureadbutt, S_IRUSR);
	GET_BUTTON_STATE (uwritebutt, S_IWUSR);
	GET_BUTTON_STATE (uexebutt, S_IXUSR);

#ifndef __WIN32__	
	if ((oldperms & S_ISUID) == S_ISUID) perms |= S_ISUID;

	GET_BUTTON_STATE (greadbutt, S_IRGRP);
	GET_BUTTON_STATE (gwritebutt, S_IWGRP);
	GET_BUTTON_STATE (gexebutt, S_IXGRP);
		
	if ((oldperms & S_ISGID) == S_ISGID) perms |= S_ISGID;

	GET_BUTTON_STATE (oreadbutt, S_IROTH);
	GET_BUTTON_STATE (owritebutt, S_IWOTH);
	GET_BUTTON_STATE (oexebutt, S_IXOTH);
		
	if ((oldperms & S_ISVTX) == S_ISVTX) perms |= S_ISVTX;
#endif

	return perms;
}

- (void)watchedPathDidChange:(NSDictionary *)info
{
}

- (void)setCalculateSizes:(BOOL)value
{
  autocalculate = value;
  
  if (autocalculate) {
    if ([calculateButt superview]) {
      [calculateButt removeFromSuperview];
    }
  } else {
    if ([calculateButt superview] == nil) {
      [mainBox addSubview: calculateButt];
    }
  }
}

- (IBAction)calculateSizes:(id)sender
{
  if (sizer == nil) {
    [self startSizer];
  } else {
    [sizeField setStringValue: @"--"]; 
    [sizer computeSizeOfPaths: insppaths];
  }
  [calculateButt setEnabled: NO];
}

- (void)startSizer
{
  NSPort *port[2];  
  NSArray *portArray;

  port[0] = (NSPort *)[NSPort port];
  port[1] = (NSPort *)[NSPort port];
  portArray = [NSArray arrayWithObjects: port[1], port[0], nil];

  sizerConn = [[NSConnection alloc] initWithReceivePort: (NSPort *)port[0]
                                               sendPort: (NSPort *)port[1]];
  [sizerConn setRootObject: self];
  [sizerConn setDelegate: self];
  [sizerConn enableMultipleThreads];

  [nc addObserver: self 
				 selector: @selector(sizerConnDidDie:)
	    			 name: NSConnectionDidDieNotification 
           object: sizerConn];

  NS_DURING
  {
    [NSThread detachNewThreadSelector: @selector(createSizerWithPorts:)
                             toTarget: [Sizer class]
                           withObject: portArray];
  }
  NS_HANDLER
  {
    NSLog(@"Error! A fatal error occurred while detaching the thread.");
  }
  NS_ENDHANDLER
}

- (void)sizerConnDidDie:(NSNotification *)notification
{
  id diedconn = [notification object];

  if (diedconn == sizerConn)
    {
      [nc removeObserver: self
		    name: NSConnectionDidDieNotification 
		  object: sizerConn];
      DESTROY (sizer);
      DESTROY (sizerConn);
      NSLog(@"sizer connection died");
    }
}

- (void)setSizer:(id)anObject
{
  if (sizer == nil) {
    [anObject setProtocolForProxy: @protocol(SizerProtocol)];
    sizer = (id <SizerProtocol>)anObject;
    RETAIN (sizer);
    if (insppaths) {
      sizeStop = YES;
      [sizeField setStringValue: @"--"];
      [sizer computeSizeOfPaths: insppaths];
    }
  }
}

- (void)sizeReady:(NSString *)sizeStr
{
	[sizeField setStringValue: sizeStr]; 
}

- (void)updateDefaults
{
  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  [defaults setBool: autocalculate forKey: @"auto_calculate_sizes"];
}

@end


@implementation Sizer

- (void)dealloc
{
  [super dealloc];
}

+ (void)createSizerWithPorts:(NSArray *)portArray
{
  NSAutoreleasePool *pool;
  id attrs;
  NSConnection *conn;
  NSPort *port[2];
  Sizer *sizer;
	
  pool = [[NSAutoreleasePool alloc] init];
	  
  port[0] = [portArray objectAtIndex: 0];
  port[1] = [portArray objectAtIndex: 1];
  conn = [NSConnection connectionWithReceivePort: port[0] sendPort: port[1]];
  attrs = (id)[conn rootProxy];
  sizer = [[Sizer alloc] initWithAttributesConnection: conn];
  [attrs setSizer: sizer];
  RELEASE (sizer);
	
  [[NSRunLoop currentRunLoop] run];
  [pool release];
}

- (id)initWithAttributesConnection:(NSConnection *)conn
{
  self = [super init];
  
  if (self) {
    id attrs = (id)[conn rootProxy];
    [attrs setProtocolForProxy: @protocol(AttributesSizeProtocol)];
    attributes = (id <AttributesSizeProtocol>)attrs;
    fm = [NSFileManager defaultManager];	
  }
  
  return self;
}

- (void)computeSizeOfPaths:(NSArray *)paths
{
	unsigned long long dirsize = 0;
	unsigned long long fsize = 0;
  int i;
	
  sizeStop = NO;
  
 	for (i = 0; i < [paths count]; i++) {
    CREATE_AUTORELEASE_POOL (arp1);
		NSString *path, *filePath;
		NSDictionary *fileAttrs;
		BOOL isdir;
		
    if (sizeStop) {
      RELEASE (arp1);
      return;
    }
    
		path = [paths objectAtIndex: i];
		 
		fileAttrs = [fm fileAttributesAtPath: path traverseLink: NO];
		if (fileAttrs) {
			fsize = [[fileAttrs objectForKey: NSFileSize] unsignedLongLongValue];
			dirsize += fsize;
		}
     
		[fm fileExistsAtPath: path isDirectory: &isdir];
          
		if (isdir) {
			NSDirectoryEnumerator *enumerator = [fm enumeratorAtPath: path];
			
      while (1) {
        CREATE_AUTORELEASE_POOL (arp2);
        
        filePath = [enumerator nextObject];
      
        if (filePath) {
          if (sizeStop) {
            RELEASE (arp2);
            RELEASE (arp1);
            return;
          }
        
			    filePath = [path stringByAppendingPathComponent: filePath];
			    fileAttrs = [fm fileAttributesAtPath: filePath traverseLink: NO];
			    if (fileAttrs) {
				    fsize = [[fileAttrs objectForKey: NSFileSize] unsignedLongLongValue];
				    dirsize += fsize;
			    }
      
        } else {
          RELEASE (arp2);
          break;   
        } 
      
        RELEASE (arp2);
      }
		}
    
    RELEASE (arp1);
	}	

  if (sizeStop == NO) {
    [attributes sizeReady: fsDescription(dirsize)]; 
  }
}

@end