Source

Murky / Source / HgFile.m

Full commit
//
//  HgFile.m
//  Murky
//
//  Copyright 2008-2009 Jens Alfke. All rights reserved.
//

#import "HgFile.h"
#import "HgDir.h"
#import "HgRepository.h"
#import "HgRevision.h"


@implementation HgFile


- (id) initWithName: (NSString*)name directory: (HgDir*)parentDir
{
    self = [super init];
    if (self != nil) {
        _name = [name copy];
        _dir = parentDir;
        _status = kNotTracked;
    }
    return self;
}


- (NSString*) description
{
    return [NSString stringWithFormat: @"%@[%@]",[self class],_name];
}


@synthesize name=_name, directory=_dir;

- (BOOL) isFile         {return YES;}
- (BOOL) isDirectory    {return NO;}
- (BOOL) isRoot         {return _dir==nil;}

- (HgRevision*) revision        {return self.root.revision;}
- (HgRepository*) repository    {return self.root.revision.repository;}

- (HgDir*) root
{
    HgFile *f = self;
    while(YES) {
        HgDir *dir = f->_dir;
        if( ! dir )
            return (HgDir*)f;
        else
            f = dir;
    }
}

- (NSMutableString*) _getPath: (BOOL)absolute
{
    NSMutableString *path = [_dir _getPath: absolute];
    [path appendString: _name];
    return path;
}

- (NSString*) path
{
    return [self _getPath: NO];
}

- (NSString*) absolutePath
{
    return [self _getPath: YES];
}

- (NSURL*) URL
{
    return [NSURL fileURLWithPath: self.absolutePath];
}

- (NSIndexPath*) indexPath
{
    if( _dir ) {
        NSUInteger index = [_dir.files indexOfObjectIdenticalTo: self];
        return [_dir.indexPath indexPathByAddingIndex: index];
    } else
        return [[NSIndexPath alloc] initWithIndexes: NULL length: 0];
}

- (NSImage*) icon
{
    if( ! _icon ) {
        _icon = [[NSWorkspace sharedWorkspace] iconForFile: self.absolutePath];
    }
    return _icon;
}

- (NSDictionary*) fileAttributes
{
    return [[NSFileManager defaultManager] fileAttributesAtPath: self.absolutePath traverseLink: YES];
}

- (CFAbsoluteTime) modTime
{
    return self.fileAttributes.fileModificationDate.timeIntervalSinceReferenceDate;
}


- (HgStatus) status
{
    return _status;
}

- (void) setStatus: (HgStatus)status
{
    if( status != _status ) {
        _status = status;
    }
}

- (BOOL) isUncommitted
{
    return strchr("MAR!?", _status) != NULL;
}


- (int) compare: (id)object
{
    return [_name caseInsensitiveCompare: [object name]];
}


- (void) addFilesMatching: (Predicate)predicate toArray: (NSMutableArray*)array
{
    if( CheckPredicate(predicate,self) )
        [array addObject: self];
}


- (void) remove
{
    [_dir _removeFile: self];
}



#pragma mark -
#pragma mark ACTIONS:

- (NSImage*) statusImage
{
	
	if( _status == kModified ){
		return [NSImage imageNamed:@"status_modified"];
	}else if( _status == kNeedsUpdate){
		return [NSImage imageNamed:@"status_needs_update"];
	}else if( _status == kAdded){
		return [NSImage imageNamed:@"status_added"];
	}else if( _status == kRemoved){
		return [NSImage imageNamed:@"status_removed"];
	}else if( _status == kDeleted){
		return [NSImage imageNamed:@"status_deleted"];
	}else if( _status == kIgnored){
		return [NSImage imageNamed:@"status_ignored"];
	}else if( _status == kNotTracked){
		return [NSImage imageNamed:@"status_nottracked"];
	}
    
	return [NSImage imageNamed:@"status_null"];
	
}

+ (NSArray*) keyPathsForValuesAffectingStatusString
{
    return [NSArray arrayWithObject: @"status"];
}


#pragma mark -
#pragma mark REVISIONS:


- (NSArray*) revisions
{
    NSError *error;
    return [self.repository revisionsOfFile: self error: &error];
}


@end