/**********************************************************************

	--- Dlgedit generated file ---

	File: ThemeDlg.cpp
	Last generated: Fri Nov 14 21:28:20 1997

 *********************************************************************/
#include <qdir.h>
#include <qlist.h>
#include "ThemeDlg.h"
#include "ThemeDlg.moc"
#include "ThemeDlgData.moc"
#include "ircapp.h"

#define Inherited ThemeDlgData

ThemeDlg::ThemeDlg
(
	QWidget* parent,
	const char* name
)
	:
	Inherited( parent, name )
{
    child = NULL;
    theme = Settings::theme;
    theme = Settings::theme;
    ApplyChanges();
    GetThemeList();
}


ThemeDlg::~ThemeDlg()
{
}

void ThemeDlg::InstallEventFilters(QWidget *widget)
{
    QList<QObject> *list = (QList<QObject> *)(widget->children());
    if (list)
    {
        for (unsigned int i=0; i< list->count(); i++)
        {
            QWidget *w = (QWidget *)list->at(i);
            InstallEventFilters(w);
        }
    }
    widget->installEventFilter(this);
}

void ThemeDlg::SaveAll()
{
    theme = Settings::theme;
    QString str = "themes/";
    str += themeListBox->text(themeListBox->currentItem());
    Settings::themeFile = str.copy();
}

Theme ThemeDlg::GetTheme()
{
    return theme;
}

void ThemeDlg::ApplyChanges()
{
    if (child)
    {
        child->recreate(0,0,QPoint(0,0));
        delete child;
    }
    /* child = new IrcWindow (window, "#kIRC");
    window->SetCaption("#kIRC: The official kIRC channel");
    window->SetChild(child);
    child->SetMDIWindow(window);
    window->setGeometry(window->x(), window->y(), window->width(), window->height());
    InstallEventFilters(window);
    mdi->setPalette(IrcApp::DefaultPalette());
    IrcApp::SetThemeProperties(mdi, Settings::theme.mainWindow);
    window->setPalette(IrcApp::DefaultPalette());
    window->setFont(IrcApp::DefaultFont());
    fileMenu->setPalette(IrcApp::DefaultPalette());
    fileMenu->setFont(IrcApp::DefaultFont());
    IrcApp::SetThemeProperties(fileMenu, Settings::theme.app);
    IrcApp::SetThemeProperties(fileMenu, Settings::theme.mainWindow);
    child->ApplyTheme();
    child->Output("Normal text.", Output::NORMAL);
    child->Output("Internal message.", Output::INTERNAL_MESSAGE);
    child->Output("Server text.", Output::SERVER_MESSAGE);
    child->Output("Notice", Output::NOTICE);
    child->Output("Info", Output::INFO);
    child->Output("Error", Output::ERROR);
    child->Output("Private message.", Output::PRIVMESSAGE);
    child->Output("Action", Output::ACTION);
    child->Output("My text.", Output::OWN);
    child->AddNick("@chanop");
    child->AddNick("+hasvoice");
    child->AddNick("kircuser");*/
}

bool ThemeDlg::eventFilter(QObject *, QEvent *e)
{ 
    if (e->type() == Event_Paint || e->type() == Event_Resize)
        return false;
    else
        return true;
}

void ThemeDlg::GetThemeList()
{

    QString str = Settings::sysFileDir + "/themes/";
    QDir dir(str);
    QStrList *tmp = (QStrList *)(dir.entryList("*", QDir::Files, QDir::Name));
    QList<QString> *themeList = new QList<QString>();
    if (tmp)
    {
        for (char *buff = tmp->first(); buff != 0; buff = tmp->next())
        {
            themeList->append(new QString(buff));
        }
    }
    str = Settings::fileDir + "/themes/";
    QDir dir2( str);
    tmp = (QStrList *)(dir2.entryList("*", QDir::Files, QDir::Name));
    QList<QString> *homeList = new QList<QString>();
    if (tmp)
    {
        for (unsigned int i=0; i<tmp->count(); i++)
        {
            homeList->append(new QString(tmp->at(i)));
        }
    }
    for (unsigned int i =0; i<homeList->count(); i++)
    {
        themeList->inSort(homeList->at(i));
    }
    
    for (unsigned int i=0; i<themeList->count(); i++)
    {
        QString str = themeList->at(i)->data();
        themeListBox->insertItem(str);
    }
    delete themeList;
    delete homeList;
}

void ThemeDlg::SetTheme(const char *themeFile)
{
    QString str = "themes/";
    str += themeFile;
    Theme newTheme;
    newTheme.Load(IrcApp::GetFile(str));
    Settings::theme = newTheme;
    ApplyChanges();
}
