/* TSShelfItem.m created by tsengel on Fri 15-Aug-1997 */

#import "TSShelfItem.h"

@implementation TSShelfItem

- (id)init
{
    return [self initWithContentsOfFile:nil];
}

- (id)initWithContentsOfFile:(NSString *)aString
{
    id		defaultTitle;

    self = [super init];
    if( !self) return nil;

    // IF we don't have a path then we have just been created...which means there is something to save
    // Otherwie each shelf has to decied on its own if changes have been made..

    if( !aString )
    {
        [self setTitle:@"Untitled"];
        unsavedChanges = YES;
    }
    else
    {
        // Set a default title according to the files name

        defaultTitle = [[aString lastPathComponent] stringByDeletingPathExtension];
        [self setTitle:defaultTitle];
        unsavedChanges = NO;
    }
    path = [aString copy];

    return self;
}

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

- (void)ok:(id)sender
{
    // Ensure that swapping out saves the changes... this minimized the risk of lost changes
    // if the app crashes.
    // <<HACK>> Should be smarter and only save if changes have been made !!

    [super ok:sender];

    if( [self contentDidChange] ) [self save];
}


- (void)setTitle:(NSString *)aString
{
    id		aPath;
    id		defaultTitle;

    [title autorelease];
    title = [aString copy];

    // If we are a private shelf then we will just set a different title...but not change the name.
    // But if we are not shared then we should also try to rename our storage file if the name did change.

/*
    if( !sharedShelf )
    {
        aPath = [self path];
        defaultTitle = [[aPath lastPathComponent] stringByDeletingPathExtension];

        if( ![aString isEqual:defaultTitle] )
        {
            if( ![[NSFileManager defaultManager] fileExistsAtPath:]
        }
    }
*/
}

- (NSString *)title
{
    if( !title )
        return @"";
    else
        return [[title copy] autorelease];
}

- (NSString *)path
{
    int		count = 0;
    id		pathBasename;

    if( !path )
    {
        path = [[NSUserDefaults standardUserDefaults] stringForKey:@"DefaultShelfLibraryPath"];
        path = [path stringByStandardizingPath];

        // If our global "TheShelf  folder does not exist yet...lets create it

        if( ![[NSFileManager defaultManager] fileExistsAtPath:path] )
            [[NSFileManager defaultManager] createDirectoryAtPath:path attributes:nil];

        pathBasename = [path stringByAppendingPathComponent:[self title]];
        path = [pathBasename stringByAppendingPathExtension:@"aShelf"];

        // Now check if the path is really unique.

        while( [[NSFileManager defaultManager] fileExistsAtPath:path] )
        {
            count++;
            path = [pathBasename stringByAppendingFormat:@"-%i", count];
            path = [path stringByAppendingPathExtension:@"aShelf"];
            path = [path stringByStandardizingPath];
        }
        [[NSFileManager defaultManager] createDirectoryAtPath:path attributes:nil];

        [path retain];
    }
    return path;
}

- (void)setSharedShelf:(BOOL)flag
{
    sharedShelf = flag;
}

- (BOOL)isSharedShelf
{
    return sharedShelf;
}

- (void)setContentDidChange:(BOOL)flag
{
    unsavedChanges = flag;
}

- (BOOL)contentDidChange
{
    return unsavedChanges;
}

- (void)save
{
    // Lets note that we did save our changes..so there is no need to save them again...

    unsavedChanges = NO;

    // If the path that we should save ourself to does not exist yet...create that folder first.

    NSLog( @"Saving %@", [self path]);

    if( ![[NSFileManager defaultManager] fileExistsAtPath:[self path]] )
        [[NSFileManager defaultManager] createDirectoryAtPath:[self path] attributes:nil];
}

@end
