/***************************************************************************
*   Copyright (C) 2004-2009 by Thomas Fischer                             *
*   fischer@unix-ag.uni-kl.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.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#include <tqbuffer.h>
#include <tqlayout.h>
#include <tqfile.h>

#include <klibloader.h>
#include <kxmlguifactory.h>
#include <tdeapplication.h>
#include <klibloader.h>
#include <kfinddialog.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <ktrader.h>
#include <kmimetype.h>
#include <kservice.h>
#include <kprogress.h>
#include <tdeparts/componentfactory.h>
#include <tdetexteditor/configinterface.h>
#include <tdetexteditor/editorchooser.h>
#include <tdetexteditor/undointerface.h>

#include "fileexporterbibtex.h"
#include "fileimporterbibtex.h"
#include "settings.h"
#include "documentsourceview.h"

namespace KBibTeX
{

    DocumentSourceView::DocumentSourceView( KBibTeX::DocumentWidget *docWidget, bool isReadOnly, TQWidget *parent, const char *name )
            : TQWidget( parent, name ), m_isReadOnly( isReadOnly ), m_docWidget( docWidget ), m_bibtexFile( NULL ), m_document( NULL ), m_view( NULL ), m_progDlg( NULL )
    {
        setupGUI();

        readConfig();
    }

    DocumentSourceView::~DocumentSourceView()
    {
        kapp->config() ->sync();
    }

    /**
     * Copy the selected text to the clipboard
     */
    void DocumentSourceView::copy()
    {
        KTextEditor::ClipboardInterface * clip = dynamic_cast<KTextEditor::ClipboardInterface*>( m_view );
        if ( clip )
            clip->copy();
    }

    /**
     * Cut the selected text to the clipboard
     */
    void DocumentSourceView::cut()
    {
        KTextEditor::ClipboardInterface * clip = dynamic_cast<KTextEditor::ClipboardInterface*>( m_view );
        if ( clip )
            clip->cut();
    }

    /**
     * Paste the clipboard's content into the document
     */
    bool DocumentSourceView::paste()
    {
        KTextEditor::ClipboardInterface * clip = dynamic_cast<KTextEditor::ClipboardInterface*>( m_view );
        if ( clip )
        {
            clip->paste();
            return true;
        }
        else
            return false;
    }

    /**
     * Paste a line of text into the document at the designated position
     */
    void DocumentSourceView::insertLines( const TQString& text, int line )
    {
        m_editInterface->insertLine( line < 0 ? m_editInterface->numLines() : ( uint ) line, text );

        KTextEditor::ViewCursorInterface * cursorIf = dynamic_cast<KTextEditor::ViewCursorInterface*>( m_view );
        if ( cursorIf )
            cursorIf->setCursorPosition( line < 0 ? m_editInterface->numLines() - 1 : ( uint ) line, 0 );
    }

    void DocumentSourceView::selectAll()
    {
        KTextEditor::SelectionInterface * selI = dynamic_cast<KTextEditor::SelectionInterface*>( m_view );
        if ( selI )
            selI->selectAll();
    }

    void DocumentSourceView::setReadOnly( bool isReadOnly )
    {
        m_isReadOnly = isReadOnly;
        m_document->setReadWrite( !m_isReadOnly );
    }

    void DocumentSourceView::setFactory( KXMLGUIFactory *factory, KXMLGUIClient * client )
    {
        KTextEditor::PopupMenuInterface * popupInterface = dynamic_cast<KTextEditor::PopupMenuInterface*>( m_view );
        popupInterface->installPopup(( TQPopupMenu* )( factory ->container( "tdetexteditor_popup", client ) ) );
    }

    void DocumentSourceView::configureEditor()
    {
        KTextEditor::ConfigInterface * conf = KTextEditor::configInterface( m_document );
        if ( !conf )
        {
            kdDebug() << "*** No KTextEditor::ConfigInterface for part!" << endl;
            return ;
        }

        // show the modal config dialog for this part if it has a ConfigInterface
        conf->configDialog();
        conf->writeConfig();
    }

    void DocumentSourceView::undo()
    {
        KTextEditor::UndoInterface * undoIf = dynamic_cast<KTextEditor::UndoInterface*>( m_document );
        if ( undoIf != NULL )
            undoIf->undo();
        else
            kdDebug() << "Cannot determine undo interface" << endl;
    }

    void DocumentSourceView::find()
    {
        KFindDialog dlg( true, this );
        dlg.setFindHistory( m_findHistory );
        dlg.setHasSelection( false );
        dlg.setSupportsWholeWordsFind( false );
        dlg.setSupportsBackwardsFind( false );
        dlg.setSupportsCaseSensitiveFind( false );
        dlg.setSupportsRegularExpressionFind( false );
        if ( dlg.exec() == TQDialog::Accepted )
        {
            m_lastSearchTerm = dlg.pattern();
            m_findHistory = dlg.findHistory();



            KTextEditor::ViewCursorInterface * cursorIf = dynamic_cast<KTextEditor::ViewCursorInterface*>( m_view );

            if ( cursorIf )
            {
                unsigned int fromLine = 0;
                unsigned int fromCol = 0;
                if ( dlg.options() && KFindDialog::FromCursor )
                {
                    cursorIf->cursorPosition( &fromLine, &fromCol );
                    fromCol++;
                }

                search( fromLine, fromCol );
            }
            else
                kdDebug() << "Failed to instantiate ViewCursorInterface, SearchInterface, or SelectionInterface" << endl;
        }
    }

    void DocumentSourceView::findNext()
    {
        if ( m_lastSearchTerm.isEmpty() )
            find();
        else
        {
            KTextEditor::ViewCursorInterface * cursorIf = dynamic_cast<KTextEditor::ViewCursorInterface*>( m_view );

            if ( cursorIf )
            {
                unsigned int fromLine = 0;
                unsigned int fromCol = 0;
                cursorIf->cursorPosition( &fromLine, &fromCol );

                search( fromLine, fromCol );
            }
            else
                kdDebug() << "Failed to instantiate ViewCursorInterface, SearchInterface, or SelectionInterface" << endl;
        }
    }

    bool DocumentSourceView::setBibTeXFile( BibTeX::File *bibtexFile )
    {
        Settings * settings = Settings::self();

        bool result = false;
        m_progDlg = new KProgressDialog( this, NULL, i18n( "Source View" ), i18n( "Converting BibTeX document to plain text ..." ), true );
        m_progDlg->setAllowCancel( false );
        kapp->processEvents();

        TQBuffer buffer;
        buffer.open( IO_WriteOnly );
        BibTeX::FileExporterBibTeX * exporter = new BibTeX::FileExporterBibTeX();
        connect( exporter, TQ_SIGNAL( progress( int, int ) ), this, TQ_SLOT( updateProgress( int, int ) ) );
        exporter->setStringDelimiter( settings->fileIO_BibtexStringOpenDelimiter, settings->fileIO_BibtexStringCloseDelimiter );
        exporter->setKeywordCasing( settings->fileIO_KeywordCasing );
        exporter->setEncoding( "latex" );
        exporter->setEnclosingCurlyBrackets( settings->fileIO_EnclosingCurlyBrackets );
        result = exporter->save( &buffer, bibtexFile );
        delete exporter;
        buffer.close();

        if ( result )
        {
            kapp->processEvents();
            buffer.open( IO_ReadOnly );
            TQTextStream in( &buffer );
            in.setEncoding( TQTextStream::UnicodeUTF8 );
            TQString text = in.read();
            buffer.close();

            if ( m_editInterface )
            {
                kapp->processEvents();
                // very strange: to set the text, you have to set
                // readwrite to true...
                m_document->setReadWrite( true );
                m_editInterface->setText( text );
                m_document->setReadWrite( !m_isReadOnly );
            }

            m_bibtexFile = bibtexFile;
        }

        kapp->processEvents();
        delete m_progDlg;

        return result;
    }

    BibTeX::File* DocumentSourceView::getBibTeXFile()
    {
        if ( m_editInterface )
        {
            TQBuffer buffer;

            buffer.open( IO_WriteOnly );
            TQTextStream stream( &buffer );
            stream.setEncoding( TQTextStream::UnicodeUTF8 );
            stream << m_editInterface->text();
            buffer.close();

            Settings * settings = Settings::self( m_bibtexFile );

            buffer.open( IO_ReadOnly );
            BibTeX::FileImporter *importer = new BibTeX::FileImporterBibTeX( settings->editing_FirstNameFirst );
            BibTeX::File *result = importer->load( &buffer );
            delete importer;
            buffer.close();

            return result;
        }

        return NULL;
    }

    void DocumentSourceView::focusInEvent( TQFocusEvent* /*event*/ )
    {
        if ( m_view != NULL )
            m_view->setFocus();
    }

    void DocumentSourceView::setupGUI()
    {
        // create the Kate::Document
        m_document = Kate::createDocument( this, "Kate::Document" );

        m_view = ( Kate::View * ) m_document->createView( this, 0L );
        m_editInterface = editInterface( m_document );
        m_document->setReadWrite( !m_isReadOnly );
        if ( !m_isReadOnly )
            connect( m_document, TQ_SIGNAL( textChanged() ), this, TQ_SIGNAL( modified() ) );
        TQBoxLayout * layout = new TQVBoxLayout( this );
        layout->addWidget( static_cast<TQWidget*>( m_view ) );

        int c = m_document->hlModeCount() ;
        int hlIdx = -1;
        for ( int i = 0; i < c; i++ )
            if ( m_document->hlModeName( i ) .compare( "BibTeX" ) == 0 )
            {
                hlIdx = i;
                break;
            }
        if ( hlIdx > -1 )
            m_document-> setHlMode( hlIdx );
    }

    void DocumentSourceView::readConfig()
    {
        TDEConfig * config = kapp->config();
        readConfig( config );
    }

    void DocumentSourceView::writeConfig()
    {
        TDEConfig * config = kapp->config();
        writeConfig( config );
    }

    void DocumentSourceView::readConfig( TDEConfig *config )
    {
        if ( m_view )
        {
            KTextEditor::ConfigInterface * conf = KTextEditor::configInterface( m_document );
            if ( conf )
                conf->readConfig( config );
        }
    }

    void DocumentSourceView::writeConfig( TDEConfig *config )
    {
        if ( m_view )
        {
            KTextEditor::ConfigInterface * conf = KTextEditor::configInterface( m_document );
            if ( conf )
                conf->writeConfig( config );
        }
        config->sync();
    }

    void DocumentSourceView::search( int fromLine, int fromCol )
    {
        unsigned int foundAtLine, foundAtCol, matchLen;

        KTextEditor::SearchInterface * searchIf = KTextEditor::searchInterface( m_document );
        KTextEditor::SelectionInterface *selectionIf = KTextEditor::selectionInterface( m_document );
        KTextEditor::ViewCursorInterface * cursorIf = dynamic_cast<KTextEditor::ViewCursorInterface*>( m_view );

        do
        {
            bool result = searchIf->searchText( fromLine, fromCol, m_lastSearchTerm, &foundAtLine, &foundAtCol, &matchLen, false );
            if ( result )
            {
                selectionIf->setSelection( foundAtLine, foundAtCol, foundAtLine, foundAtCol + matchLen );
                cursorIf->setCursorPositionReal( foundAtLine, foundAtCol + matchLen );
                break;
            }

            fromLine = 0;
            fromCol = 0;
        }
        while ( KMessageBox::questionYesNo( this, TQString( i18n( "Could not find text '%1' in the document.\nStart from the beginning?" ) ).arg( m_lastSearchTerm ), i18n( "Find text in source view" ), KGuiItem( i18n( "Restart search" ) ) ) == KMessageBox::Yes );
    }

    void DocumentSourceView::updateProgress( int current, int total )
    {
        m_progDlg->progressBar()->setTotalSteps( total );
        m_progDlg->progressBar()->setValue( current );
        kapp->processEvents();
    }

}
#include "documentsourceview.moc"
