/**
*** Copyright (c) 1996 by Bill Bumgarner.  All Rights Reserved
*** 
*** This software may be used and distributed freely for any purpose
*** provided that this notice is included unchanged on any and all copies.
*** The author does not warrant or guarantee this software in any way.
*** 
*** 
*** All queries, patches, and discussions of the PyObjC project should be
*** directed to <objc-sig@python.org>.  The author can be reached via
*** <bbum@friday.com>.
*** 
*** $RCSfile: PyServicesDelegate.m,v $
*** 
*** $Revision: 1.3 $
*** 
*** $Date: 1996/11/15 02:33:44 $
*** 
**/

#import <objc/Object.h>

#import <python1.4/OC_PythonObject.h>
#import <python1.4/OC_PythonString.h>
#import <python1.4/OC_PythonInt.h>

#import <appkit/Listener.h>

#import "PyServicesDelegate.h"

#define file_version "$Revision: 1.3 $"
#define file_id      "$Id: PyServicesDelegate.m,v 1.3 1996/11/15 02:33:44 bbum Exp $"

// invoked by python when module is imported
static PyMethodDef PyServicesDelegate_methods[] =
{
  { 0, 0, 0, 0 }
};

void initPyServicesDelegate()
{
  PyObject *m;
  m = Py_InitModule4 ("PyServicesDelegate", PyServicesDelegate_methods,
		      NULL, NULL, PYTHON_API_VERSION);
  [PyServicesDelegate class];
}

@interface PyServicesDelegate (HandlerMethod)
- serviceForwarder: f
    withPasteboard: pb
	  userData: (id <PythonObject>) ud;
@end

@implementation PyServicesDelegate
/*"
 * An instance of PyServicesDelegate acts as a Listener's services
 * delegate.  It currently only responds to services aimed at the that send
 * the 'forwardService' message.  This is not really a limitation as one
 * can easily dispatch custom services according to the information
 * contained in the %userData field.
 * 
 * See the python example usage for more information as to how to use this
 * class.
"*/

/* ========================================================================
** Instantiation
** ========================================================================
*/
+ newWithHandler: (id <PythonObject>) aHandler; 
{
    id returnObject;

    if (!aHandler)
	return nil;

    returnObject = [self alloc];

    [returnObject initWithHandler: aHandler];

    return returnObject;
}

- initWithHandler: (id <PythonObject>) aHandler; 
{
    [super init];

    handler = aHandler;

    return self;
}

/* ========================================================================
** Destruction
** ========================================================================
*/
- free
{
    if (errMessageStore)
	free((char *)errMessageStore);

    return [super free];
}

/* ========================================================================
** Setting the error message
** ========================================================================
*/
- (void) setErrorMessage: (const char *) aMessage;
{
    errMessageStore = NXCopyStringBuffer(aMessage);
}

/* ========================================================================
** Services Handler
** ========================================================================
*/
- forwardService: (Pasteboard *) pb
	userData: (const char *)userData
	   error: (char **)errorMessage;
{
    errMessageStore = NULL;

    [(id)handler
	  serviceForwarder: self
	  withPasteboard: pb
	  userData: [OC_PythonString fromString:(char *)userData]];
    
    if (errMessageStore)
	*errorMessage = (char *)errMessageStore;

    return self;    
}
@end
