#include "html2odf.h"

bool save(const QString &fileName)
{
    if (!QFile::exists(fileName))
        return false;
    QFile file(fileName);
    if (!file.open(QFile::ReadOnly))
        return false;

    QByteArray data = file.readAll();
    QTextCodec *codec = Qt::codecForHtml(data);
    QString str = codec->toUnicode(data);
    if (Qt::mightBeRichText(str))
    {
        QTextDocument d;
        d.setHtml(str);
        QTextDocumentWriter  writer(fileName + ".odt", "odf");
        return writer.write(&d);
    }
    else
    {
        return false;
    }
}

Window::Window(QWidget *parent)
    : QWidget(parent)
{
    setWindowTitle(tr("html2odf"));

    files = new QStringList();

    odfSaving = new QFutureWatcher<bool>(this);
    connect(odfSaving, SIGNAL(progressValueChanged(int)), SLOT(showProgress(int)));
    connect(odfSaving, SIGNAL(resultReadyAt(int)), SLOT(savingResult(int)));
    connect(odfSaving, SIGNAL(finished()), SLOT(finished()));

    openButton = new QPushButton(tr("Select HTML files"));
    connect(openButton, SIGNAL(clicked()), SLOT(open()));

    cancelButton = new QPushButton(tr("Cancel"));
    cancelButton->setEnabled(false);
    connect(cancelButton, SIGNAL(clicked()), odfSaving, SLOT(cancel()));

    pauseButton = new QPushButton(tr("Pause/Resume"));
    pauseButton->setEnabled(false);
    connect(pauseButton, SIGNAL(clicked()), odfSaving, SLOT(togglePaused()));

    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->addWidget(openButton);
    buttonLayout->addWidget(cancelButton);
    buttonLayout->addWidget(pauseButton);
    buttonLayout->addStretch();

    progressLayout = new QHBoxLayout();
    progress = new QProgressBar();
    progressLayout->addWidget(progress);

    mainLayout = new QVBoxLayout();
    mainLayout->addLayout(buttonLayout);
    mainLayout->addLayout(progressLayout);
    mainLayout->addStretch();
    setLayout(mainLayout);
}

Window::~Window()
{
    odfSaving->cancel();
    odfSaving->waitForFinished();
}

void Window::open()
{
    if (odfSaving->isRunning())
    {
        odfSaving->cancel();
        odfSaving->waitForFinished();
    }

    *files = QFileDialog::getOpenFileNames(this, tr("Select HTML files"),
                                           QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation),
                                           "*.htm *.html");

    if (!files->isEmpty())
    {
        odfSaving->setFuture(QtConcurrent::mapped(*files, save));
        progress->setMinimum(odfSaving->progressMinimum());
        progress->setMaximum(odfSaving->progressMaximum());
        openButton->setEnabled(false);
        cancelButton->setEnabled(true);
        pauseButton->setEnabled(true);
    }
}

void Window::showProgress(int num)
{
    progress->setValue(num);
}

void Window::savingResult(int index)
{
    qDebug() << files->at(index) + ".odt" << " saved: " << odfSaving->resultAt(index) ;
}

void Window::finished()
{
    openButton->setEnabled(true);
    cancelButton->setEnabled(false);
    pauseButton->setEnabled(false);
}


