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

extern struct Catalog *catalog;

extern APTR app,wi_main,lv_vol,lv_dir,gr_dir,
    wi_aboutmui,
    wi_makedir,st_makedir,bt_mdok;

extern BOOL aborted;

void QuitFunc(struct Hook *hook __asm("a0"), APTR obj __asm("a2"), ULONG *param __asm("a1"))
{
    if (MUI_RequestA(app,wi_main,0L,Gcs(41,MSG_41),Gcs(43,MSG_43),Gcs(44,MSG_44),NULL))
        DoMethod(app, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
}

struct Hook QuitHook = {{NULL,NULL},(void *)QuitFunc,NULL,NULL};

void DClickFunc(struct Hook *hook __asm("a0"), APTR obj __asm("a2"), ULONG *param __asm("a1"))
{
    char *str;
    struct FileInfoBlock fib;
    BPTR lock;

    if (*param == 0)
    {
        DoMethod(lv_vol, MUIM_List_GetEntry, MUIV_List_GetEntry_Active, &str);
        set(lv_dir, MUIA_Dirlist_Directory, str);
        set(gr_dir, MUIA_Group_ActivePage, 1);
    }
    else if (*param == 1)
    {
        get(lv_dir, MUIA_Dirlist_Path, &str);

        if ((lock = Lock(str,ACCESS_READ))!=NULL)
        {
            if (Examine(lock,&fib))
            {
                if (fib.fib_DirEntryType > 0)
                    set(lv_dir, MUIA_Dirlist_Directory, str);
            }
        UnLock(lock);
        }
    }
}

struct Hook DClickHook = {{NULL,NULL},(void *)DClickFunc,NULL,NULL};

void AboutmuiFunc(struct Hook *hook __asm("a0"), APTR obj __asm("a2"), ULONG *param __asm("a1"))
{

    if (!wi_aboutmui)
    {
        wi_aboutmui = AboutmuiObject,
            MUIA_Window_RefWindow,wi_main,
            MUIA_Aboutmui_Application, app,
        End;
    }

    if (wi_aboutmui)
        set(wi_aboutmui, MUIA_Window_Open, TRUE);

}

struct Hook AboutmuiHook = {{NULL,NULL},(void *)AboutmuiFunc,NULL,NULL};

void ParentFunc(struct Hook *hook __asm("a0"), APTR obj __asm("a2"), ULONG *param __asm("a1"))
{
    char dir[256];
    char *uf;
    BPTR lock,nlock;

    get(lv_dir, MUIA_Dirlist_Directory, &uf);

    if ((lock = Lock(uf,ACCESS_READ))!=NULL)
    {
        if ((nlock = ParentDir(lock))!=NULL)
        {
            if (NameFromLock(nlock,dir,1000))
                set(lv_dir, MUIA_Dirlist_Directory, dir);
        UnLock(nlock);
        }
    UnLock(lock);
    }

}

struct Hook ParentHook = {{NULL,NULL},(void *)ParentFunc,NULL,NULL};

void MakedirFunc(struct Hook *hook __asm("a0"), APTR obj __asm("a2"), ULONG *param __asm("a1"))
{
    char *odir,*dir;
    char ndir[256];
    BPTR lock;

    if (*param == 0)
    {
        set(wi_makedir,MUIA_Window_Open,TRUE);
        set(wi_makedir,MUIA_Window_ActiveObject,st_makedir);
    }
    else if (*param == 1)
    {
        get(st_makedir,MUIA_String_Contents, &dir);

        if ((strlen(dir))>0)
        {
            get(lv_dir,MUIA_Dirlist_Directory,&odir);
            strcpy(ndir,odir);
            AddPart(ndir,dir,255);

            if ((lock = CreateDir(ndir))!=NULL)
            {
                set(wi_makedir, MUIA_Window_Open,FALSE);
                set(st_makedir, MUIA_String_Contents, "");
                DoMethod(lv_dir, MUIM_Dirlist_ReRead);
            UnLock(lock);
            }
        }
        else
            set(wi_makedir,MUIA_Window_ActiveObject, st_makedir);
    }

}

struct Hook MakedirHook ={{NULL,NULL},(void *)MakedirFunc,NULL,NULL};

void DeleteFunc(struct Hook *hook __asm("a0"), APTR obj __asm("a2"), ULONG *param __asm("a1"))
{
    struct FileInfoBlock *fib;
    LONG id;
    char *dir;
    char path[256];

    DoMethod(lv_dir,MUIM_List_GetEntry,MUIV_List_GetEntry_Active,&fib);

    if (fib)
    {
        if (MUI_Request(app,wi_main,0,Gcs(41,MSG_41),Gcs(43,MSG_43),Gcs(45,MSG_45),NULL))
        {
            get(lv_dir,MUIA_Dirlist_Directory,&dir);
            id =MUIV_List_NextSelected_Start;

            for(;;)
            {
                DoMethod(lv_dir, MUIM_List_NextSelected, &id);
                if (id==MUIV_List_NextSelected_End) break;
                DoMethod(lv_dir, MUIM_List_GetEntry, id, &fib);
                strcpy(path,dir);
                AddPart(path,fib->fib_FileName,100);
                DeleteFile(path);
            }
        DoMethod(lv_dir, MUIM_Dirlist_ReRead);
        }
    }
}

struct Hook DeleteHook = {{NULL,NULL},(void *)DeleteFunc,NULL,NULL};

void AbortFunc(struct Hook *hook __asm("a0"), APTR obj __asm("a2"), ULONG *param __asm("a1"))
{
    aborted = TRUE;
}

struct Hook AbortHook = {{NULL,NULL},(void *)AbortFunc,NULL,NULL};
