/* ****************************************************************************
  This file is part of KBabel

  Copyright (C) 1999-2000 by Matthias Kiefer
                            <matthias.kiefer@gmx.de>

  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.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

**************************************************************************** */
#include <qtextedit.h>
#include <kcursor.h>
#include <kglobalsettings.h>
#include <kglobal.h>
#include <kconfig.h>
#include <klocale.h>
#include <kmessagebox.h>


#include "catalog.h"
#include "headereditor.h"

HeaderEditor::HeaderEditor(Catalog* cat,const char* name)
             : KDialogBase((QWidget*)0,name,false,QString::null, Ok|Cancel|Default|User1)
{
   restoreSettings();
   
   _catalog=cat;
   connect(_catalog,SIGNAL(signalFileOpened(bool)),this,SLOT(readHeader(bool)));
   connect(_catalog,SIGNAL(signalHeaderChanged()),this,SLOT(updateHeader()));

   setButtonText(User1,i18n("&Update"));
   setButtonText(Default,i18n("&Reset"));

   _editor=new QTextEdit(this,"internal headereditor");
   _editor->setMinimumSize(_editorSize);
   _editor->setFont(KGlobalSettings::generalFont());
   _editor->setWordWrap( QTextEdit::NoWrap );

   KCursor::setAutoHideCursor(_editor,true);

   readHeader(cat->isReadOnly());
   updateHeader();

   setMainWidget(_editor);
}

HeaderEditor::~HeaderEditor()
{
   saveSettings();
}

void HeaderEditor::saveSettings()
{
    KConfig* config = KGlobal::config();
    
    KConfigGroupSaver saver(config, "HeaderEditor" );
    
    config->writeEntry( "Size", _editor->size() );
}

void HeaderEditor::restoreSettings()
{
    KConfig* config = KGlobal::config();
    
    KConfigGroupSaver saver(config, "HeaderEditor" );
    
    QSize defaultSize(350,250);
    _editorSize = config->readSizeEntry("Size", &defaultSize );
}

bool HeaderEditor::isModified()
{
   return _editor->isModified();
}

void HeaderEditor::readHeader(bool readOnly)
{
   setCaption(i18n("Header Editor for %1").arg(_catalog->currentURL().url()));

   _editor->setReadOnly(readOnly);
   enableButton(User1,!readOnly);
}

void HeaderEditor::updateHeader()
{
    _editor->setText(_catalog->header().asString());
    _editor->setModified(false);
}


// update button
void HeaderEditor::slotUser1()
{
   CatalogItem header(_editor->text());

   if(!header.isValid())
   {
      QString msg=i18n("<qt><p>This is not a valid header!</p>\n\
<p>A valid header has the format:\n\
<blockquote><i>optional comment</i><br/>msgid \"\"<br/>msgstr \"\"<br/>\
<i>optional lines surrounded by \"\"</i></blockquote></p>\n");
      msg+=i18n("<p>Please edit the header before updating!</p></qt>");

      KMessageBox::sorry(this,msg);

      return;
   }

   header=_catalog->updatedHeader(header,false);

   QString temp=header.asString();
   _editor->setText(temp);
}


void HeaderEditor::slotDefault()
{
   updateHeader();
}

void HeaderEditor::slotCancel()
{
   updateHeader();

   QDialog::reject();
}

void HeaderEditor::slotOk()
{
   if(isModified())
   {
      CatalogItem header(_editor->text());

      if(!header.isValid())
      {
         QString msg=i18n("<qt><p>This is not a valid header!</p>\n\
<p>A valid header has the format:\n\
<blockquote><i>optional comment</i><br/>msgid \"\"<br/>msgstr \"\"<br/>\
<i>optional lines surrounded by \"\"</i></blockquote></p>\n");
         msg+=i18n("<p>Do you want to discard your changes or edit the header again?</p></qt>");

         switch(KMessageBox::warningYesNo(this,msg,i18n("Warning"),i18n("Discard"),i18n("Edit")))
         {
            case KMessageBox::Yes:
            {
               slotCancel();
               return;
            }
            default:
               return;
         }
      }

      _catalog->setHeader(header);
   }

   QDialog::accept();
}


#include "headereditor.moc"
