/*
 * Copyright 1990 by Baylor College of Medicine ALL RIGHTS RESERVED. 
 *
 * This program is subject to a license agreement between 
 * Baylor College of Medicine and MIT. Any use inconsistent with
 * said license and any use by persons other than the faculty, 
 * students and staff at MIT or any use on a computer not operated 
 * as part of the Athena Computing Environment (ACE) is expressly 
 * prohibited.
 */
#ifndef __SRVLIB__
#define __SRVLIB__

#include <stdio.h>
#include <rast_img.h>

/* error levels */
#define SRV_OK              0
#define SRV_WARNING         1
#define SRV_ERROR           2
#define SRV_FATAL           3

/* error flags */
#define SRV_READ            4
#define SRV_WRITE           5
#define SRV_WAIT            6
#define SRV_MALLOC          7
#define SRV_BAD_SERVER      8
#define SRV_BAD_DATABASE    9
#define SRV_BAD_PORT       10
#define SRV_GETHOST        11
#define SRV_SOCKET         12
#define SRV_CONNECT        13
#define SRV_VERSION        14
#define SRV_CONTEXT        15
#define SRV_NOT_AUTHORIZED 16

/* object types */
typedef enum {
	OT_IMAGE,
	OT_TXT,
	OT_LINK,
	OT_ACTION_LINK
} Object_Type;

/* srvlib event types */
typedef enum {
	SRV_DELETE_NOTEBOOK,
	SRV_DELETE_PAGE,
	SRV_DELETE_OBJECT,
	SRV_ACCESS_ALLOWED,
	SRV_ACCESS_DISALLOWED,
	SRV_CREATE_PAGE,
	SRV_CREATE_OBJECT,
	SRV_MODIFY_NOTEBOOK,
	SRV_MODIFY_PAGE,
	SRV_MODIFY_OBJECT,
	SRV_OBJECT_CONTENTS_CHANGED,
	SRV_DELETE_AUTHOR,
	SRV_AUTHOR_LOGIN,
	SRV_AUTHOR_LOGOUT,
	SRV_SHUTDOWN,
} SrvEventType ;

typedef struct _SrvEventRec *SrvEventTable;

typedef struct _SrvEventRec
{
	SrvEventType type;
	void (*proc)();
	char *data;
	SrvEventTable next;
} SrvEventRec;

typedef struct
{
	SrvEventType type ;
	int nid ;
} SrvDeleteNotebookEvent ;

typedef struct
{
	SrvEventType type ;
	int nid ;
	int pid ;
	int del_links ;
} SrvDeletePageEvent ;

typedef struct
{
	SrvEventType type ;
	int nid ;
	int pid ;
	int oid ;
} SrvDeleteObjectEvent ;

typedef struct
{
	SrvEventType type ;
	int nid ;
} SrvAccessAllowedEvent ;

typedef struct
{
	SrvEventType type ;
	int nid ;
} SrvAccessDisallowedEvent ;

typedef struct
{
	SrvEventType type ;
	int nid ;
} SrvModifyNotebookEvent ;

typedef struct
{
	SrvEventType type ;
	int nid ;
	int pid ;
} SrvCreatePageEvent ;

typedef struct
{
	SrvEventType type ;
	int nid ;
	int pid ;
} SrvModifyPageEvent ;

typedef struct
{
	SrvEventType type ;
	int nid ;
	int pid ;
	int oid ;
} SrvCreateObjectEvent ;

typedef struct
{
	SrvEventType type ;
	int nid ;
	int pid ;
	int oid ;
} SrvModifyObjectEvent ;

typedef struct
{
	SrvEventType type ;
	int aid ;
} SrvDeleteAuthorEvent ;

typedef struct
{
	SrvEventType type ;
	int aid ;
} SrvAuthorLoginEvent ;

typedef struct
{
	SrvEventType type ;
	int aid ;
} SrvAuthorLogoutEvent ;

typedef struct
{
	SrvEventType type ;
	int flag ;
} SrvShutdownEvent ;

typedef struct
{
	SrvEventType type ;
} SrvAnyEvent ;

typedef union
{
	SrvEventType				type ;
	SrvAnyEvent					any ;
	SrvCreateObjectEvent		create_object ;
	SrvCreatePageEvent			create_page ;
	SrvAccessAllowedEvent		access_allowed ;
	SrvAccessDisallowedEvent	access_disallowed ;
	SrvModifyObjectEvent		modify_object ;
	SrvModifyPageEvent			modify_page ;
	SrvModifyNotebookEvent		modify_notebook ;
	SrvDeleteObjectEvent		delete_object ;
	SrvDeletePageEvent			delete_page ;
	SrvDeleteNotebookEvent		delete_notebook ;
	SrvDeleteAuthorEvent		delete_author ;
	SrvAuthorLoginEvent         author_login ;
	SrvAuthorLogoutEvent        author_logout ;
	SrvShutdownEvent            shutdown ;
} SrvEvent ;

typedef struct
{
		long dest_note ;
        long dest_page ;
        char font[256] ;
        char title[256] ;
        char color[256] ;
} Link_Info ;

typedef struct
{
        char font[256] ;
        char color[256] ;
} Txt_Info ;

typedef struct
{
        long create_cmap ;
        long low_res_sw ;
} Img_Info ;

typedef struct
{
        char command[256] ;
        char font[256] ;
        char title[256] ;
        char color[256] ;
} Action_Link_Info ;

typedef union {
	Link_Info l_info ;
	Txt_Info t_info ;
	Img_Info i_info ;
	Action_Link_Info al_info ;
} Object_Data ;

typedef struct
{
	Object_Type type ;
	long x ;
	long y ;
	long width ;
	long height ;
	long aid ;
	Object_Data data ;
} Object_Info ;

typedef struct
{
	int oid ;
	Object_Info info ;
	char *txt ;
	Rast_Img img ;
} Object_Block ;

typedef struct
{
	char title[256] ;
	int can_read ;
	int can_write ;
	long aid ;
	long home_page ;
} Notebook_Info ;

typedef struct
{
	int nid;
	Notebook_Info info;
} Notebook_Block;

typedef struct
{
	char title[256] ;
	int width ;
	int height ;
	char color[256] ;
	char create_date[256] ;
	long aid ;
} Page_Info ;

typedef struct
{
	long aid ;
	long access ;
} Access_Item ;

/* error lookup table */
typedef struct _ErrorRec
{
  int level;    /* the level of the error:
                   0 - no error
                   1 - warning
                   2 - error (but recoverable)
                   3 - fatal error */
  int id;       /* the error id */
  char *name;   /* string representation for error */
} ErrorRec;

/* linked list of events */
typedef struct _SrvEventNode *SrvEventNode ;
struct _SrvEventNode
{
	SrvEvent event ;
	SrvEventNode next ;
} ;

/* connection record */
typedef struct _SrvContext
{
	int trace;                  /* level of tracing */
	int me;                     /* user id returned by getuid */
	int sock;                   /* sock file id */
	char *hostname;             /* name of host (dbsrv location) */
	int port;                   /* port number used for connection */
	char *db;                   /* database name */
	SrvEventNode last;          /* pointer to last event in queue */
	SrvEventNode first;         /* pointer to first event in queue */
	SrvEventTable event_table;  /* list of event handlers */
	FILE *logfile;              /* logfile for this connection */
	int authorized;             /* user authorization flag */
} SrvContext;

SrvContext *srv_init();
char *srv_get_object_text() ;
char *srv_host();
char *srv_database();

#endif /* __SRVLIB__ */
