/*
 * 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: note.c
 * Date: 04/29/91
 *
 * Description:
 *   This file contains several support functions for managing
 *   the notebook list.
 *
 * Revisions:
 ****************************************************************/
#include <stdio.h>
#include <malloc.h>

#include "VnsP.h"

/****************************************************************
 *              Public Functions
 ****************************************************************/

void
sort_notebooks(blocks,n)
	Notebook_Block *blocks;
	int n;
{
	int gap,i,j;
	Notebook_Block tmp;

	for (gap = n/2; gap > 0; gap /= 2)
	{
		for (i = gap; i < n; i++)
		{
			for (j = i-gap; j >= 0; j -= gap)
			{
				if (strcmp(blocks[j].info.title,blocks[j+gap].info.title) <= 0)
					break;
				tmp = blocks[j];
				blocks[j] = blocks[j+gap];
				blocks[j+gap] = tmp;
			}
		}
	}
}

VnsNotebook **
VnsFindNotebook(db,nid)
	VnsDatabase *db;
	int nid;
{
	VnsNotebook **p = &db->first_note;

	while(*p != NULL)
	{
		if ((*p)->nid == nid)
		{
			return p;
		}
		p = &((*p)->next);
	}
	return p;
}

VnsNotebook *
add_notebook(db,nid)
	VnsDatabase *db;
	int nid;
{
	VnsNotebook **nl = VnsFindNotebook(db,nid);

	if (*nl == NULL)
	{
		VnsNotebook *new = (VnsNotebook *)malloc(sizeof *new);
		Notebook_Info info;

		srv_inq_notebook(db->srv,nid,&info);

		new->nid = nid;
		new->info = info;
		new->db = db;
		new->first_page = NULL;
		new->next = NULL;
		new->last_page = -1;
		*nl = new;
		find_last_page_of_note(new);

		/* call any callbacks before removing the notebook */
		VnsCallCallbacks(db->vns,CB_NOTEBOOK_CREATE,(VnsPointer)new);
	}
}

VnsNotebook *
add_notebook_block(db,note)
	VnsDatabase *db;
	Notebook_Block *note;
{
	VnsNotebook **nl = VnsFindNotebook(db,note->nid);

	if (*nl == NULL)
	{
		VnsNotebook *new = (VnsNotebook *)malloc(sizeof *new);

		new->nid = note->nid;
		new->info = note->info;
		new->db = db;
		new->first_page = NULL;
		new->next = NULL;
		new->last_page = -1;
		*nl = new;
		find_last_page_of_note(new);
	}
	return *nl;
}

void
load_notebooks(db)
	VnsDatabase *db;
{
	Notebook_Block *notes;
	VnsNotebook *list, *last;
	int num_notes;
	int i;

	srv_get_blurted_note(db->srv,&notes,&num_notes);
	if (num_notes < 0)
	{
		fprintf(stderr,"Error: retrieving blurted notebooks\n");
		exit(-1);
	}

	sort_notebooks(notes,num_notes);

	for (i = 0; i < num_notes; i++)
	{
		add_notebook_block(db,notes[i]);
	}

	free((char *)notes);
}

void
del_notebook(nl)
	VnsNotebook *nl;
{
	VnsDatabase *db = NoteToDb(nl);
	VnsNotebook **p = VnsFindNotebook(db,nl->nid);

	if(*p != NULL)
	{
		/* Get rid of pages that have been opened */
		while (nl->first_page != NULL)
		{
			/* update the links to this page */
			update_page_refs(nl->first_page);
			/* destroy the displayed page */
			del_page(nl->first_page);
		}

		/* notify the property manager */
		VnsPropResetNotebook(nl);

		/* call any callbacks before removing the notebook */
		VnsCallCallbacks(db->vns,CB_NOTEBOOK_DELETE,(VnsPointer)nl);

		*p = nl->next;
		free((char *)nl);
	}
}
