/* --------------------------------------------------------------------------

Class:			PHAppManager
Version:		0.1
File:			PHAppManager.m				 

Written by:		Thomas Engel
Created:		17.05.1995	(Copyleft)
Last modified:	17.05.1995


Note:	For a detailed description please read the class documentation.

 ------------------------------------------------------------------------- */

#import "PHAppManager.h"
#import "PHDocConverter.h"
#import "PHInfo.h"

#import <misckit/MiscString.h>


@implementation PHAppManager

- appWillInit:sender
{
	// This is the init part we need to pass before we get the messages that
	// we have to open some files.
	// To be able to init a class we need the have the Preferences,

	// preferences = [CEPreferencesManager new];
	
	// Now lets load the docu templates
	
	return self;
}

- appDidInit:sender
{
	// This is some kind of late init. These object are need for working but
	// not for reading the classes or having a class initialized.

	
	return self;
}

- (int)app:sender openFile:(const char *)path type:(const char *)type
{
	// This method is performed whenever a user opens a document from the
	// Workspace Manager.
	// We use our default file open method with some default params.
	
	return [self openFile:path];
}

- (BOOL)appAcceptsAnotherFile:sender
{
	// Inform the workspace that we can open multiple files.
	// At least we can open another one...but we will drop the current..
	
	return YES;
}

- appDidBecomeActive:sender
{
	return self;
}

- appWillTerminate:sender
{
	return self;
}

- (BOOL)_closeAllWindows
{
	return YES;
}

- (int)openFile:(const char *)path
{
	// This method is performed whenever a user opens a document.
	// It takes care that every file is only opened once.
	
	id	aString;
	id	filetype;
	id	newConverter;

	// check if it is of an accepted type:

	aString = [MiscString newWithString:path];
	filetype = [aString fileExtension];
	newConverter = nil;

	if( [filetype cmp:"ps"] != 0 )
	{
		// Looks like this is no valid file !
		
		[aString free];
		[filetype free];
		return -1;
	}

  	// If we really have a new editor...let the world know about it.
  	// This is all prepared for multiDocument once it is desired...I guess
	// it might never happen but it has become some kind of standard template 
	// now.
	
	if( ![self _alreadyLoaded:(char *)path] )
	{
		if( !sharedDocConverter )
		{
			newConverter = [[PHDocConverter alloc] initFromFile:path];
			sharedDocConverter = newConverter;
		}
		else	[sharedDocConverter setFilename:(char *)path];
		
		if( newConverter ) return YES;
	}
	return NO;
}

- (BOOL)_alreadyLoaded:(char *)aFile
{
	id	aString;
	id	windowList;
	id	aDelegate;
	int	i;
	BOOL	loaded;

	// First we have to check if we already know this file !
	// Some window delegate should have the same filename
	// Using the window list might not be the safest way..but I guess it
	// will work.
	
	aString = [MiscString newWithString:aFile];
	windowList = [NXApp windowList];
	loaded = NO;

	for( i=0; i<[windowList count]; i++ )
	{
		aDelegate = [[windowList objectAt:i] delegate];

		if( aDelegate != nil &&
			[aDelegate isKindOf:[PHDocConverter class]])
		{
			if( [aString compareTo:
					[(PHDocConverter *)aDelegate filename]] == 0 )
			{
				// Looks like we have found a delegate that belongs to exactly
				// the same path.
				// We must to ask this delegate now what window we should top.
				
				[[aDelegate window] makeKeyAndOrderFront:self];
				loaded = YES;
			}
		}
	}
	[aString free];
	return loaded;
}

- showInfo:sender
{
	if( !info ) info = [PHInfo new];
		
	[info makeKeyAndOrderFront:self];
	return self;
}

- showPreferences:sender
{
//	if( preferences ) [preferences makeKeyAndOrderFront:self];
	return self;
}

- preferences
{
	return self;
}

- open:sender
{
	id 		openPanel;
	char 	fullName[MAXPATHLEN];
	const char *const *files;
	const char *const fileType[7] = { "ps",
									   NULL};

 	openPanel = [[OpenPanel new] allowMultipleFiles:NO];

 	// Run the open panel, filtering for our type of documents
 	// We try to open every file the panel returns.
  
  	if( [openPanel runModalForTypes:fileType] )
  	{
  		files = [openPanel filenames];
		for( files = [openPanel filenames]; files && *files; files++)
		{
			// Now lets merge the fullFilename from the directory and the
			// current filename and try to init a editor from this file.
			// We use the default open method with some default params.
      		
			sprintf( fullName, "%s/%s", 
					[(OpenPanel *)openPanel directory], *files );
      		[self openFile:fullName];
    		}
	}
  	return self;
}

@end

/*
 * History: 13.01.95 Bla.
 *
 *
 * Bugs: No bugs and birds seen....
 */