#import "OIDocumentController.h"

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <OmniBase/OmniBase.h>
#import <OmniFoundation/OmniFoundation.h>
#import <OmniAppKit/OmniAppKit.h>
#import <OWF/OWF.h>
#import <OIF/OIF.h>

#import "OIDocument.h"

RCS_ID("$Header: /Network/Developer/Source/CVS/OmniGroup/OmniImage/OIDocumentController.m,v 1.3 1998/04/12 19:30:42 kc Exp $")

@interface OIDocumentController (Private)
- (void)registerImageFileTypes;
@end

@implementation OIDocumentController 

static NSString *OIAppVersion = @"4.0-beta-3";
static NSString *OIAppFullVersion = nil;

+ (void)initialize;
{
    static BOOL initialized = NO;

    [super initialize];
    if (initialized)
	return;
    initialized = YES;

    OIAppFullVersion = [[NSString alloc] initWithFormat:@"version %@\n(%s)", OIAppVersion, __DATE__];
}

- (NSArray *)imageFileTypes;
{
    return imageExtensions;
}

// API

- (NSString *)appVersion;
{
    return OIAppVersion;
}
 
- (NSString *)appFullVersion;
{
    return OIAppFullVersion;
}


// Actions

- (IBAction)showPreferencesPanel:(id)sender;
{
    [[OAPreferenceController sharedPreferenceController] showPreferencesPanel:sender];
}

- (IBAction)flushCache:(id)sender;
{
    [OWContentCache flushCachedContent];
}


// NSApplication delegate

- (void)applicationWillFinishLaunching:(NSNotification *)notification;
{
    NSNotificationCenter *defaultCenter;

    [super applicationWillFinishLaunching:notification];

    defaultCenter = [NSNotificationCenter defaultCenter];

    // This notification is for setting up a class (reading defaults, etc.).  At this point it shouldn't rely on any other classes (except OFUserDefaults) being set up yet.
    [defaultCenter postNotificationName:OFControllerDidInitNotification object:self];

    // Register OIDocument as the document class for all image file types.  This must happen before the first -application:openFile: (which gets called before -applicationDidFinishLaunching:).
    [self registerImageFileTypes];
}

- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
    NSNotificationCenter *defaultCenter;

    defaultCenter = [NSNotificationCenter defaultCenter];

    [OFQueueProcessor startAppkitQueueProcessor];

    [[NSPrintInfo sharedPrintInfo] setVerticallyCentered:NO];

    // This notification is for resetting the state of a class to the way it was when the user last left the program:  for instance, popping up a window that was open.
    [defaultCenter postNotificationName:OFControllerStartRunningNotification object:self];

    [super applicationDidFinishLaunching:notification];

#ifdef WIN32
    // Start with an open panel
    [self open:nil];
#endif
}

- (BOOL)applicationShouldTerminate:(id)sender
{
    if (![super applicationShouldTerminate:sender])
        return NO;

    [[NSFileManager defaultManager] removeScratchDirectory];
    return YES;
}

@end

@implementation OIDocumentController (Private)

- (void)registerImageFileTypes;
{
    NSEnumerator *possibleTypesEnumerator;
    OWContentType *possibleType;

    imageExtensions = [[NSMutableArray alloc] initWithCapacity:16];
    possibleTypesEnumerator = [[[OIImage contentType] indirectSourceContentTypes] objectEnumerator];
    while ((possibleType = [possibleTypesEnumerator nextObject])) {
        if ([possibleType isPublic]) {
            unsigned int extensionsIndex, extensionsCount;
            NSArray *extensions;

            extensions = [possibleType extensions];
            extensionsCount = [extensions count];
            for (extensionsIndex = 0; extensionsIndex < extensionsCount; extensionsIndex++) {
                NSString *extension;

                extension = [extensions objectAtIndex:extensionsIndex];
                [imageExtensions addObject:extension];
                [self registerDocumentClass:[OIDocument class] forFileExtension:extension];
            }
        }
    }
}


@end
