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

                   enter document title and author

    -----------------------------------------------------------------------

    begin          : Thu Mar 11 20:50:53 MET 1999

    copyright      : (C) 1999-2001 Ewald Arnold <kvoctrain@ewald-arnold.de>
                     (C) 2005 Peter Hedlund <peter.hedlund@kdemail.net>
                     (C) 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>

    -----------------------------------------------------------------------

 ***************************************************************************

 ***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "documentproperties.h"

#include "parleydocument.h"

#include <keduvocdocument.h>
#include <KDebug>
#include <KLineEdit>

#include <QTextEdit>
#include <QLabel>


DocumentProperties::DocumentProperties(KEduVocDocument * doc, bool languageSetup, QWidget* parent)
    :QWidget(parent), m_doc(doc), m_showLanguages(languageSetup)
{
    setupUi(this);

    titleLineEdit->setText(doc->title());
    authorLineEdit->setText(doc->author());
    contactLineEdit->setText(doc->authorContact());
    licenseComboBox->setEditText(doc->license());
    commentTextEdit->setText(doc->documentComment());
    categoryComboBox->setEditText(doc->category());

    if (languageSetup) {
        prepareLanguageSelection();
    } else {
        languageGroupBox->setVisible(false);
    }
}

void DocumentProperties::prepareLanguageSelection()
{
    QStringList codes = KGlobal::locale()->allLanguagesList();
    QStringList languageNames;
    foreach (const QString &code, codes){
        languageNames.append( KGlobal::locale()->languageCodeToName(code) );
    }
    languageNames.sort();

    firstLanguageComboBox->addItems(languageNames);
    firstLanguageComboBox->completionObject()->insertItems(languageNames);
    firstLanguageComboBox->completionObject()->setIgnoreCase(true);
    secondLanguageComboBox->addItems(languageNames);
    secondLanguageComboBox->completionObject()->insertItems(languageNames);
    secondLanguageComboBox->completionObject()->setIgnoreCase(true);

    languageGroupBox->setVisible(true);
}

void DocumentProperties::accept()
{
    ParleyDocument::instance()->setTitle(titleLineEdit->text());
    m_doc->setAuthor(authorLineEdit->text());
    m_doc->setAuthorContact(contactLineEdit->text());
    m_doc->setLicense(licenseComboBox->currentText());
    m_doc->setDocumentComment(commentTextEdit->toPlainText());
    m_doc->setCategory(categoryComboBox->currentText());

    if (m_showLanguages) {
        acceptLanguageConfiguration();
    }
}

void DocumentProperties::acceptLanguageConfiguration()
{
    QString firstLanguage = firstLanguageComboBox->currentText();
    QString firstLocale;
    QString secondLanguage = secondLanguageComboBox->currentText();
    QString secondLocale;

    // ugly but works for now: iterate over languages to check which code we have
    foreach ( const QString &code, KGlobal::locale()->allLanguagesList() ) {
        if ( firstLanguage == KGlobal::locale()->languageCodeToName(code) ) {
            firstLocale = code;
        }
        if ( secondLanguage == KGlobal::locale()->languageCodeToName(code) ) {
            secondLocale = code;
        }
    }

    m_doc->identifier(0).setLocale(firstLocale);
    m_doc->identifier(0).setName(firstLanguage);
    m_doc->identifier(1).setLocale(secondLocale);
    m_doc->identifier(1).setName(secondLanguage);
}

#include "documentproperties.moc"
