/*******************************************************************************

Copyright (c) 1997-2004, Perforce Software, Inc.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1.  Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.

2.  Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTR
IBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*******************************************************************************/

/*******************************************************************************
 * Name		: p4clientapi.h
 *
 * Author	: Tony Smith <tony@perforce.com> or <tony@smee.org>
 *
 * Description	: Ruby bindings for the Perforce API. Definitions of our
 * 		  main interface to Perforce
 *
 ******************************************************************************/


/*******************************************************************************
 * P4ClientApi class - where we register our Ruby classes and plumb together 
 * the components
 ******************************************************************************/

class P4ClientApi
{
    public:
    P4ClientApi();
    ~P4ClientApi();

    // Protocol options. Call before Connect()
    void Tagged();
    void ParseForms();

    void SetClient( const char *c )	{ client.SetClient( c );	}
    void SetCwd( const char *c )	{ client.SetCwd( c ); 		}
    void SetHost( const char *h )	{ client.SetHost( h );		}
    void SetPassword( const char *p )	{ client.SetPassword( p );	}
    void SetPort( const char *p )	{ client.SetPort( p );		}
    void SetUser( const char *u )	{ client.SetUser( u );		}

    const StrPtr &GetClient()		{ return client.GetClient();	}
    const StrPtr &GetCwd()		{ return client.GetCwd();	}
    const StrPtr &GetHost()		{ return client.GetHost();	}
    const StrPtr &GetPassword()		{ return client.GetPassword();	}
    const StrPtr &GetPort()		{ return client.GetPort();	}
    const StrPtr &GetUser()		{ return client.GetUser();	}

    // Session management
    VALUE Connect();		// P4Exception on error
    VALUE Disconnect();

    // Executing commands. 
    VALUE Run( const char *cmd, int argc, char * const *argv );
    VALUE SetInput( VALUE input );

    // Result handling
    VALUE GetOutput()		{ return ui.GetResults().GetOutput();}
    VALUE GetErrors()		{ return ui.GetResults().GetErrors();}
    VALUE GetWarnings()		{ return ui.GetResults().GetWarnings();}

    // Exception levels:
    //
    // 		0 - No exceptions raised
    // 		1 - Exceptions raised for errors
    // 		2 - Exceptions raised for errors and warnings
    //
    void  ExceptionLevel( int i )	{ exceptionLevel = i; 	}
    int   ExceptionLevel()		{ return exceptionLevel; }

    void  Except( const char *func, Error *e );
    void  Except( const char *func, const char *msg );
    void  Except( const char *func, const char *msg, const char *cmd );

    //
    // Debugging support. Debug levels are:
    //
    //     1:	Debugs commands being executed
    //     2:	Debug UI method calls
    //     3:	Show garbage collection
    //
    void  SetDebug( int d );

    // Ruby garbage collection
    void  GCMark();


    private:

    void RunCmd(const char *cmd, ClientUser *ui, int argc, char * const *argv);

    public:
    ClientApi		client;

    private:
    ClientUserRuby	ui;
    int			initCount;
    int			debug;
    int			exceptionLevel;
    int			server2;
    int			tagged;
};

