/*                                                                             */
/*   ___ _            _    _            ___       __ _                         */
/*  | _ ) |_  _ ___  | |  (_)___ _ _   / __| ___ / _| |___ __ ____ _ _ _ ___   */
/*  | _ \ | || / -_) | |__| / _ \ ' \  \__ \/ _ \  _|  _\ V  V / _` | '_/ -_)  */
/*  |___/_|\_,_\___| |____|_\___/_||_| |___/\___/_|  \__|\_/\_/\__,_|_| \___|  */
/*                                                                             */
/*             Copyright 2002, Miklós Németh (Blue Lion Software)              */
/*                        email: desco@freemail.hu                             */
/*                   WWW: http://amigos.amiga.hu/bluelion                      */
/*                                                                             */
#define NEED_INLINES

#include "wordfinder.h"
#include "locale.h"

enum { MENU,MENU_ABOUT,MENU_ABOUTMUI,MENU_QUIT,MENU_SETMUI };

void ReadPrefs(void);

/* --- Library bases --- */
extern struct Library *SysBase;
struct Library *MUIMasterBase,*IntuitionBase,*DosBase,*LocaleBase;

/* --- Global variables --- */
APTR app,mainmenu,
    wi_main,cm_show,st_word,cy_dic,tx_sense,scrb_sense,pop_path,st_path,
    wi_about,wi_aboutmui=NULL;

struct Catalog *catalog;

extern struct Hook QuitHook,FindHook,AboutmuiHook;

char *dicname[20];
char *dictype[20];

/* --- Failure handler --- */
void fail(APTR app, char *str)
{
    int x;

    for (x=0;x<20;x++)
    {
        if (dicname[x]) FreeVec(dicname[x]);
        if (dictype[x]) FreeVec(dictype[x]);
    }

    if (str)
    {
        puts(str);
        exit(20);
    }

    if (app)
        MUI_DisposeObject(app);

    if (MUIMasterBase)
        CloseLibrary(MUIMasterBase);

    if (IntuitionBase)
        CloseLibrary(IntuitionBase);

    if (DosBase)
        CloseLibrary(DosBase);

    if (catalog)
        CloseCatalog(catalog);

    if (LocaleBase)
        CloseLibrary(LocaleBase);

    exit(0);
}

int main(int argc, char **argv)
{

    if (!(LocaleBase = (struct Library *) OpenLibrary("locale.library",38)))
        fail(app,Gcs(21,MSG_21));

    catalog = OpenCatalog(NULL,"wordfinder.catalog",OC_BuiltInLanguage, (ULONG)"english",OC_Version,1,TAG_DONE);

    if (!(IntuitionBase =(struct Library *) OpenLibrary("intuition.library",39)))
        fail(app,Gcs(1,MSG_1));

    if (!(MUIMasterBase =(struct Library *) OpenLibrary("muimaster.library",19)))
        fail(app,Gcs(2,MSG_2));

    if (!(DosBase = (struct Library *) OpenLibrary("dos.library",39)))
        fail(app,Gcs(3,MSG_3));

    app = ApplicationObject,
        MUIA_Application_Author,        "Miklós Németh",
        MUIA_Application_Base,          "WordFinder",
        MUIA_Application_Copyright,     "(c) 2002 Miklós Németh (Blue Lion Software)",
        MUIA_Application_Description,   Gcs(4,MSG_4),
        MUIA_Application_Title,         "WordFinder",
        MUIA_Application_Version,       "$VER: WordFinder 1.1 (25.07.2002.)",
        SubWindow, wi_main = WindowObject,
            MUIA_Window_Title, "WordFinder v1.1",
            MUIA_Window_ID, MAKE_ID('W','I','M','A'),
            MUIA_Window_Menustrip, mainmenu = MenustripObject,
                MUI_Menu(Gcs(5,MSG_5)),
                    MUI_Menuitem(Gcs(6,MSG_6),"?",MENU_ABOUT),
                    MUI_Menuitem(Gcs(7,MSG_7),NULL,MENU_ABOUTMUI),
                    MUI_Bar,
                    MUI_Menuitem(Gcs(8,MSG_8),"Q",MENU_QUIT),
                End,
                MUI_Menu(Gcs(9,MSG_9)),
                    MUI_Menuitem("MUI...",NULL,MENU_SETMUI),
                End,
            End,
            WindowContents, VGroup,
                Child, HGroup,
                    Child, st_word = StringObject, StringFrame, End,
                    Child, cy_dic = CycleObject, MUIA_Weight,0,End,
                    Child, cm_show = CheckMark(FALSE),
                End,
                Child, HGroup,
                    Child, tx_sense = TextEditorObject,
                        MUIA_TextEditor_Slider, scrb_sense,
                        MUIA_TextEditor_ReadOnly, TRUE,
                    End,
                    Child, scrb_sense = ScrollbarObject, End,
                End,
                Child, pop_path = PopaslObject,
                    MUIA_ShowMe, FALSE,
                    MUIA_Popstring_String, st_path = StringObject,
                        StringFrame,
                        MUIA_String_Contents, "Ltools:Dics",
                        MUIA_ExportID, MAKE_ID('S','T','P','A'),
                    End,
                    MUIA_Popstring_Button, PopButton(MUII_PopDrawer),
                    ASLFR_TitleText, Gcs(22,MSG_22),
                    ASLFR_DrawersOnly, TRUE,
                End,
            End,
        End,
        SubWindow, wi_about = WindowObject,
            MUIA_Window_Title, Gcs(10,MSG_10),
            MUIA_Window_ID, MAKE_ID('W','I','A','B'),
            WindowContents, VGroup,
                Child, TextObject, TextFrame, MUIA_Text_PreParse,"\033b\033c", MUIA_Text_Contents, "WordFinder v1.1", End,
                Child, TextObject, MUIA_Text_PreParse, "\033c", MUIA_Text_Contents, Gcs(4,MSG_4), End,
                Child, TextObject, MUIA_Text_PreParse, "\033c\033i", MUIA_Text_Contents, "© 2002 Miklós Németh (Blue Lion Software)", End,
            End,
        End,
    End;

    if (!app)
        fail(app,Gcs(11,MSG_11));

    DoMethod(app, MUIM_Notify, MUIA_Application_MenuAction, MENU_ABOUT,
             wi_about, 3, MUIM_Set, MUIA_Window_Open, TRUE);

    DoMethod(app, MUIM_Notify, MUIA_Application_MenuAction, MENU_ABOUTMUI,
             app, 2, MUIM_CallHook, &AboutmuiHook);

    DoMethod(app, MUIM_Notify, MUIA_Application_MenuAction, MENU_QUIT,
             app, 2, MUIM_CallHook, &QuitHook);

    DoMethod(app, MUIM_Notify, MUIA_Application_MenuAction, MENU_SETMUI,
             app, 2, MUIM_Application_OpenConfigWindow, 0);

    DoMethod(wi_main, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
             app, 2, MUIM_CallHook, &QuitHook);

    DoMethod(st_word, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime,
             app, 2, MUIM_CallHook, &FindHook);

    DoMethod(cm_show, MUIM_Notify, MUIA_Pressed, MUIV_EveryTime,
             pop_path, 3, MUIM_Set, MUIA_ShowMe, MUIV_TriggerValue);

    DoMethod(wi_about, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
             wi_about, 3, MUIM_Set, MUIA_Window_Open, FALSE);

    ReadPrefs();

    set(wi_main, MUIA_Window_Open, TRUE);

    DoMethod(app, MUIM_Application_Load, MUIV_Application_Load_ENVARC);

    set(wi_main, MUIA_Window_ActiveObject, st_word);

    /* --- Event handling --- */
    {
        ULONG sigs = 0;

        while(DoMethod(app, MUIM_Application_NewInput, &sigs) != MUIV_Application_ReturnID_Quit)
        {
            if (sigs)
                sigs = Wait(sigs);
        }
    }

    DoMethod(app, MUIM_Application_Save, MUIV_Application_Save_ENVARC);

    /* --- Exit --- */
    fail(app, NULL);
}

void ReadPrefs(void)
{
    FILE *fh;
    int i,x,g,len;
    char buf[128];
    char *text;

    for (x=0;x<20;x++)
    {
        dicname[x]=NULL;
        dictype[x]=NULL;
    }

    if ((fh = fopen("PROGDIR:prefs/ltdic.list","r"))!=NULL)
    {
        fseek(fh,0,SEEK_END);
        len =ftell(fh);
        fseek(fh,0,SEEK_SET);

        if (text = AllocVec(len+16,MEMF_PUBLIC|MEMF_CLEAR))
        {
            fread(text,1,len,fh);
            x=0;
            g=0;
            while (x<len)
            {
                if (text[x]=='#')
                {
                    x++;
                    i=0;
                    while (text[x]!=',')
                    {
                        buf[i]=text[x];
                    x++;
                    i++;
                    }
                    buf[i]='\0';
                    dicname[g]=AllocVec(128,MEMF_PUBLIC|MEMF_CLEAR);
                    strcpy(dicname[g],buf);
                    x++;
                    i=0;
                    while (text[x]!='\n')
                    {
                        buf[i]=text[x];
                        x++;
                        i++;
                    }
                    buf[i]='\0';
                    dictype[g]=AllocVec(128,MEMF_PUBLIC|MEMF_CLEAR);
                    strcpy(dictype[g],buf);
                g++;
                }
            x++;
            }
            set(cy_dic,MUIA_Cycle_Entries,dicname);
        FreeVec(text);
        }
        else
            MUI_Request(app,wi_main,0,Gcs(12,MSG_12),Gcs(13,MSG_13),Gcs(14,MSG_14),NULL);
    fclose(fh);
    }
    else
        MUI_Request(app,wi_main,0,Gcs(12,MSG_12),Gcs(13,MSG_13),Gcs(15,MSG_15),NULL);

}
