#include "kurlrequester.h"
#include "k9common.h"
#include "ui_kurlrequester.h"
#include <QCompleter>
#include <QDirModel>
KUrlRequester::KUrlRequester(QWidget *parent) :
    QWidget(parent),
    m_ui(new Ui::KUrlRequester)
{
    m_ui->setupUi(this);
    setMode(KUrlRequester::SAVE);

    QCompleter *completer=new QCompleter(this);
    m_model=new QDirModel(completer);
    m_model->setSorting(QDir::DirsFirst);
    completer->setModel(m_model);
    m_ui->lineEdit->setCompleter(completer);
    m_ui->toolButton->hide();
    m_ui->toolButton->setIcon(qApp->style()->standardIcon(QStyle::SP_DialogOpenButton));
    m_ui->toolButton->setFixedSize(height(),height());
    m_ui->toolButton->show();
}

KUrlRequester::~KUrlRequester()
{
    delete m_ui;
}

void KUrlRequester::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void KUrlRequester::setMode(eMode mode) {
    m_mode=mode;
    switch(m_mode) {
    case DIRECTORY:
        m_ui->toolButton->setIcon(qApp->style()->standardIcon(QStyle::SP_DirOpenIcon));
        m_model->setFilter(QDir::Dirs |QDir::AllDirs);
        break;
    case OPEN:
        m_ui->toolButton->setIcon(qApp->style()->standardIcon(QStyle::SP_DialogOpenButton));
        break;
    case SAVE:
        m_ui->toolButton->setIcon(qApp->style()->standardIcon(QStyle::SP_DialogSaveButton));
        break;
    }
}

void KUrlRequester::toolButtonClicked() {
    QString filename="";
    switch (m_mode) {
        case KUrlRequester::SAVE:
            if (m_title.isEmpty())
                m_title=i18n("Save file");
            filename=k9Dialogs::getSaveFileName(m_ui->lineEdit->text(),m_filter,this,m_title);
            break;
        case KUrlRequester::OPEN:
            if (m_title.isEmpty())
                m_title=i18n("Open file");
            filename=k9Dialogs::getOpenFileName(m_ui->lineEdit->text(),m_filter,this,m_title);
            break;
        case KUrlRequester::DIRECTORY:
            if (m_title.isEmpty())
                m_title=i18n("Select directory");
            filename=k9Dialogs::getExistingDirectory(m_ui->lineEdit->text());
            break;
    }

    if (! filename.isEmpty()) {
        m_ui->lineEdit->setText(filename);
        KUrl url(filename);
        emit urlSelected(url);
    }
}

void KUrlRequester::setUrl(const KUrl &_url) {
    m_ui->lineEdit->setText(_url.path());
}


KUrl KUrlRequester::url() {
    return KUrl(m_ui->lineEdit->text());
}

void KUrlRequester::setTitle(const QString &_text) {
    m_title=_text;
}

void KUrlRequester::setFilter(const QString &_filter) {
    m_filter=_filter;
    QStringList filter;
    QStringList filters=_filter.split(";;");
    foreach(QString s,filters) {
        int d=s.indexOf("(");
        int f=-1;
        if (d!=-1)
            f=s.mid(d).indexOf(")");
        if (f!=-1) {
            s=s.mid(d+1,f-1);
        }
        foreach (QString s2,s.split(" ")) {
            filter << s2;
        }

    }


    m_model->setNameFilters(filter);
    m_model->setFilter(QDir::Files|QDir::AllDirs);
}
