/* BBAppManager.m				 
 *
 * This is the basic controller of the App. It manages all big abstraction
 * tasks for the panels, documents and connections to the outside.
 *
 * For interface-info see the header file. The comments in this file mostly
 * cover only the real implementation details.
 *
 * Written by: 		Thomas Engel
 * Created:    		23.10.1993 (Copyleft)
 * Last modified: 	07.05.1994
 */

#import "BBAppManager.h"
#import "BBBeaker.h"

#import "Info.subproj/BBInfo.h"
#import "Preferences.subproj/BBPreferencesManager.h"
#import "Preferences.subproj/BBDefaultApp.h"
#import "AtomLibrary.subproj/BBAtomLibraryManager.h"
#import "Inspector.subproj/BBMasterInspector.h"
#import "Rotator.subproj/BBRotator.h"
#import "ToolBoy.subproj/ToolBoy.h"
#import "FileManager.subproj/BBFileFilterManager.h"

#import <remote/NXProxy.h>

@implementation BBAppManager

- 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 beaker we need the have the Preferences,
	// FileFilterManager and AtomLibrary up and running.

	preferences = [BBPreferencesManager new];
	atomLibrary = [BBAtomLibraryManager new];
	fileManager = [BBFileFilterManager new];
	
	return self;
}

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

	id	ourServer;
	
	inspector = [BBMasterInspector new];
	rotator = [BBRotator new];

	// Let's try to connect to the 3DDeviceServer. If we fail it should not
	// matter. This part will change....naming and prefs!
		
	deviceServer = [NXConnection connectToName:"localhost/3DDeviceServer"];
	ourServer = [deviceServer connectionForProxy];
	[ourServer runFromAppKit];
		
	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.
	// If the file is of any type and we can handle it...lets do it.
	
	id	newBeaker;
	
  	if( type )
	{
  		// If we really have a new beaker...let the world know about it.
  	
	 	newBeaker = [[BBBeaker alloc] initFromFile:path];
 		if( newBeaker ) return YES;
	}
	return NO;
}

- (BOOL)appAcceptsAnotherFile:sender
{
	// Inform the workspace that we can open multiple files.
	return YES;
}

- appDidBecomeActive:sender
{
	// Well will give our rotator the chance to register at the 3DDeviceServer
	// if we want to use a 3D mouse.
	
	if( deviceServer ) [rotator check3DMouse:self];
	return self;
}

- appWillTerminate:sender
{
	if( deviceServer ) [rotator check3DMouse:nil];
	return self;
}

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

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

- preferences
{
	return preferences;
}

- showLibrary:sender
{
	// We will open the Library file from our MoleculeDatabases directory.
	// This should be configurable in the future via the preferences and the
	// file might be located in the ~/Library etc.
	
	char	path[MAXPATHLEN+1];
	
	[[NXBundle mainBundle] getPath:path
					       forResource:"MoleculeDatabase/"
						   			   "IntroBBMoleculeLibrary.rtfd"
						   ofType:"rtfd"];
	[libraryText openRTFDFrom:path];
	[library makeKeyAndOrderFront:self];
	return self;
}

- showAtomLibrary:sender
{
	[atomLibrary makeKeyAndOrderFront:self];
	return self;
}

- atomLibrary
{
	return atomLibrary;
}

- showInspector:sender
{
	[inspector makeKeyAndOrderFront:self];
	return self;
}

- inspector
{
	return inspector;
}

- showRotator:sender
{
	[rotator makeKeyAndOrderFront:self];
	return self;
}

- rotator
{
	return rotator;
}

- showToolBoy:sender
{
	if( !toolBoy ) toolBoy = [ToolBoy new];

	[toolBoy makeKeyAndOrderFront:self];
	return self;
}

- fileManager
{
	return fileManager;
}

- deviceServer
{
	return deviceServer;
}

- open:sender
{
	id		newBeaker;
	id 		openPanel;
	char 	fullName[MAXPATHLEN];
	const char *const *files;
	const char *const fileType[6] = { "lookMol",
									  "alchMol",
									  "macMol",
									  "mvt",
									  "b&sMol",
									  NULL};

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

 	// Run the open panel, filtering for our type of documents
	// These types are provided by the fileFormatManager
 	// We try to open every file the panel returns.
  
  	if( [openPanel runModalForTypes:fileType] )
  	{
  		files = [openPanel filenames];
		for( files = [openPanel filenames]; files && *files; files++)
		{
			// Now the merge the fullFilename from the directory and the
			// current filename and try to init a beaker from this file.
			// Errors are handled by the fileFormatManager.
      		
			sprintf( fullName, "%s/%s", [openPanel directory], *files );
      		newBeaker = [[BBBeaker alloc] initFromFile:fullName];
    	}
  	}
  	return self;
}

- new:sender
{
	id	newBeaker;
	
	newBeaker = [BBBeaker new];
	return self;
}

- save:sender
{
	return self;
}

- saveAs:sender
{
	return self;
}

- saveTo:sender
{
	return self;
}

- saveAll:sender
{
	return self;
}

- revert:sender
{
	return self;
}

- close:sender
{
	return self;
}

- print:sender
{
	return self;
}

@end

/*
 * History: 07.05.94 Changed to BB.., added the deviceServer.
 *
 *			25.03.94 Added b&sMol and macMol formats.
 *
 *			03.01.94 A lot of document-handling-methods added.
 *
 *			28.12.93 Added some basic document (beaker) handling.
 *
 *			25.11.93 Well just change some object ids and included the
 *					 inspector. (still just prototyping)
 *
 *			23.10.93 Here is the startup for our nice BeakerBoy.
 *
 *
 * Bugs: No bugs and birds seen....
 */