/*

File: viewer.c
Author: Neil Cafferkey
Copyright (C) 2000 Neil Cafferkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.

*/

#include <exec/types.h>
#include <workbench/workbench.h>
#include "viewer.h"

#include "general_protos.h"
#include "paedia_protos.h"
#include "article_protos.h"
#include "search_protos.h"
#include "index_protos.h"
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/icon.h>


const TEXT *version_string="$VER: WhirlDisc 0.1 (9.8.2000)";
const TEXT *prog_name="WhirlDisc";

const TEXT *intuition_name="intuition.library";
const TEXT *dos_name="dos.library";
const TEXT *graphics_name="graphics.library";
const TEXT *gadtools_name="gadtools.library";
const TEXT *icon_name="icon.library";


const TEXT *datadrawer_tt_name="DATADRAWER";
const TEXT *default_paedia_path="WBISS600:";


#define EXEC_VERSION 39
#define INTUITION_VERSION 39
#define DOS_VERSION 39
#define GRAPHICS_VERSION 39
#define GADTOOLS_VERSION 39
#define ICON_VERSION 39


static BOOL OpenLibs();
static VOID CloseLibs();


ULONG main()
{
   Paedia paedia;
   Search search;
   Article article;
   BOOL quit=FALSE;
   struct IntuiMessage *msg;
   struct MsgPort *msg_port;
   struct MinList object_list;
   BPTR prog_dir,old_dir;
   struct DiskObject *disk_object;
   STRPTR *tooltypes,paedia_path=default_paedia_path;

   if(OpenLibs())
   {

      /* Initialise object list and shared message port */

      NewList(&object_list);
      msg_port=CreateMsgPort();

      /* Get paedia's directory name from tooltypes */

      prog_dir=GetProgramDir();
      old_dir=CurrentDir(prog_dir);
      disk_object=GetDiskObject(prog_name);

      if(disk_object)
      {
         tooltypes=disk_object->do_ToolTypes;
         paedia_path=FindToolType(tooltypes,datadrawer_tt_name);
      }

      /* Initialise paedia */

      paedia=CreatePaedia("links",paedia_path);

      if((paedia!=NULL)&&(msg_port!=NULL))
      {

         search=CreateSearch(msg_port,paedia,&object_list,50,50);
         if(search!=NULL)
         {

            AddHead((struct List *)&object_list,&search->node);

            while(quit!=TRUE)
            {
               WaitPort(msg_port);
               while((msg=(struct IntuiMessage *)GetMsg(
                  msg_port))!=NULL)
               {
                  if(((Article)msg->IDCMPWindow->UserData)->node.ln_Type==
                     NT_ARTICLE)
                  {
                     article=(Article)msg->IDCMPWindow->UserData;
                     HandleArticleInput(article,msg);
                  }
                  else
                  {
                     search=(Search)msg->IDCMPWindow->UserData;
                     HandleSearchInput(search,msg);
                  }

               }

               /* Check if list is empty */

               if(IsListEmpty((struct List *)&object_list))
                  quit=TRUE;
            }

         }
      }

      if(paedia!=NULL)
         KillPaedia(paedia);

      if(disk_object!=NULL)
         FreeDiskObject(disk_object);
      CurrentDir(old_dir);

      DeleteMsgPort(msg_port);
   }

   CloseLibs();

   return RETURN_OK;
}



int wbmain(struct Msg *wbmsg)
{
   main();
}



/* Function: OpenLibs
 * ==================
 */

static BOOL OpenLibs()
{
   BOOL success=TRUE;

   if(!(IntuitionBase=OpenLibrary(intuition_name,INTUITION_VERSION)))
   {
      success=FALSE;
   }

   if(success)
      if(!(DOSBase=(struct DosLibrary *)OpenLibrary(dos_name,DOS_VERSION)))
      {
         ReportError(NULL,ERROR_REPORT_LIB,dos_name,DOS_VERSION);
         success=FALSE;
      }

   if(success)
      if(!(GfxBase=OpenLibrary(graphics_name,GRAPHICS_VERSION)))
      {
         ReportError(NULL,ERROR_REPORT_LIB,graphics_name,GRAPHICS_VERSION);
         success=FALSE;
      }

   if(success)
      if(!(GadToolsBase=OpenLibrary(gadtools_name,GADTOOLS_VERSION)))
      {
         ReportError(NULL,ERROR_REPORT_LIB,gadtools_name,GADTOOLS_VERSION);
         success=FALSE;
      }

   if(success)
      if(!(IconBase=OpenLibrary(icon_name,ICON_VERSION)))
      {
         ReportError(NULL,ERROR_REPORT_LIB,icon_name,ICON_VERSION);
         success=FALSE;
      }

   return success;
}



/* Function: CloseLibs
 * ===================
 */

static VOID CloseLibs()
{
   if(IconBase)
      CloseLibrary(IconBase);

   if(GadToolsBase)
      CloseLibrary(GadToolsBase);

   if(GfxBase)
      CloseLibrary(GfxBase);

   if(DOSBase)
      CloseLibrary((struct Library *)DOSBase);

   if(IntuitionBase)
      CloseLibrary(IntuitionBase);

   return;
}



