/*
 * 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.
 */
/****************************************************************
 * File: session.c
 * Date: 05/06/91
 *
 * Description:
 *   This file contains the functions for loading and saving
 *   the VNS session file.
 *
 * Revisions:
 ****************************************************************/
#include <stdio.h>
#include <X11/StringDefs.h>
#include <X11/Intrinsic.h>
#include <malloc.h>

#include "VnsP.h"

/* Retrieves name of vns init file */
static
char *
get_vns_init_fname()
{
	char *getenv();
	static char buf[256];
	char *home = getenv("HOME");
	sprintf(buf,"%s/.vns_init",home);
	return buf;
}

PUBLIC
void
VnsLoadSession(vns)
	VnsContext *vns;
{
	FILE *fp = fopen(get_vns_init_fname(),"r");

	if (fp == NULL)
	{
		return;
	}

	/* allocate the session structure */
	vns->session = (Session *)malloc(sizeof(Session));
	vns->session->pages = NULL;
	vns->session->show_grid = FALSE;
	vns->session->show_outlines = FALSE;
	vns->session->snap_to_grid = FALSE;
	vns->session->auto_page_open = FALSE;

	/* parse the session file and store options in session structure */
	for(;;)
	{
		char command[256];

		if (fscanf(fp,"%s",command) != 1)
		{
			break;
		}
		if (!strcmp(command,"open_page"))
		{
			OpenPage *new = (OpenPage *)malloc(sizeof(OpenPage));
			int x,y;
			char c;
			int l = 0;

			while((c = fgetc(fp)) == ' ');
			if (c == '"')
			{
				while((c = fgetc(fp)) != '"' && c >= 0)
				{
					if (c == '\\')
					{
						c = fgetc(fp);
					}
					if (l < (sizeof(new->notebook)) - 1)
					{
						new->notebook[l++] = c;
					}
				}
				new->notebook[l] = 0;
			}
			else
			{
				ungetc(c,fp);
				if (fscanf(fp,"%s",new->notebook) != 1)
				{
					break;
				}
			}
			if (fscanf(fp,"%d %d %d",&new->page,&x,&y) != 3)
			{
				break;
			}
			sprintf(new->geom,"+%d+%d",x,y);
			new->next = NULL;

			new->next = vns->session->pages;
			vns->session->pages = new;
		}
		else if (!strcmp(command,"command_panel"))
		{
			int x,y;
			if (fscanf(fp,"%d %d",&x,&y) != 2)
			{
				break;
			}
			sprintf(vns->session->panel_geom,"+%d+%d",x,y);
		}
		else if (!strcmp(command,"show_grid"))
		{
			vns->session->show_grid = TRUE;
		}
		else if (!strcmp(command,"show_outlines"))
		{
			vns->session->show_outlines = TRUE;
		}
		else if (!strcmp(command,"snap_to_grid"))
		{
			vns->session->snap_to_grid = TRUE;
		}
		else if (!strcmp(command,"open_page_upon_creation"))
		{
			vns->session->auto_page_open = TRUE;
		}
	}

	fclose(fp);
}

/* Callback to save the current session */
PUBLIC
void
VnsSaveSession(vns)
	VnsContext *vns;
{
	FILE *fp = fopen(get_vns_init_fname(),"w");

	if (fp != NULL)
	{
		Position x,y;
		VnsPage *pl = vns->first_page;
		if (vns->session->snap_to_grid)
		{
			fprintf(fp,"snap_to_grid\n");
		}
		if (vns->session->show_grid)
		{
			fprintf(fp,"show_grid\n");
		}
		if (vns->session->show_outlines)
		{
			fprintf(fp,"show_outlines\n");
		}
		if (vns->session->auto_page_open)
		{
			fprintf(fp,"open_page_upon_creation\n");
		}
		{
			XtVaGetValues(vns->toplevel,
				XtNx,&x,
				XtNy,&y,
				NULL);
			fprintf(fp,"command_panel %d %d\n",x,y);
		}
		while(pl != NULL)
		{
			VnsNotebook *nl = PageToNote(pl);

			XtVaGetValues(pl->pwd,
				XtNx,&x,
				XtNy,&y,
				NULL);
			fprintf(fp,"open_page \"%s\" %d %d %d\n",nl->info.title,pl->pid,x,y);
			pl = pl->npage;
		}

		fclose(fp);
	}
}


/*
 * This function has been written based upon the VnsContext having
 * a "current" database pointer. The database information should 
 * really be stored in the session file and used to look-up the
 * database pointer in the VnsContext. 
 */
PUBLIC
void
VnsOpenSessionPages(vns)
	VnsContext *vns;
{
	OpenPage *list;
	VnsDatabase *db;

	if (vns == NULL) return;
	if (vns->session == NULL) return;

	db = vns->current_db;
	list = vns->session->pages;

	while(list != NULL)
	{
		int nid = srv_find_note_by_name(db->srv,list->notebook);
		if (nid < 0)
		{
			break;
		}
		if (srv_validate_page(db->srv,nid,list->page) == 1)
		{
			VnsNotebook *nl = *(VnsFindNotebook(db,nid));
			VnsAddPage(nl,list->page,(struct return_page *)NULL,
				(char *)NULL,(VnsPage *)NULL);
		}

		list = list->next;
	}
}
