summaryrefslogtreecommitdiffstats
path: root/src/entrywidgetsource.cpp
blob: f3f03d9c67dde4c266edbfd041739d0784806493 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/***************************************************************************
*   Copyright (C) 2004-2009 by Thomas Fischer                             *
*   [email protected]                                             *
*                                                                         *
*   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 <ntqlayout.h>
#include <ntqtextedit.h>
#include <ntqbuffer.h>

#include <kdialog.h>
#include <kpushbutton.h>
#include <klocale.h>
#include <kglobalsettings.h>

#include <fileexporterbibtex.h>
#include <fileimporterbibtex.h>
#include <entry.h>
#include <settings.h>
#include "entrywidgetsource.h"

namespace KBibTeX
{

    EntryWidgetSource::EntryWidgetSource( BibTeX::File *bibtexfile, bool isReadOnly, TQWidget *parent, const char *name )
            : EntryWidgetTab( bibtexfile, isReadOnly, parent, name ), m_entry( NULL )
    {
        setupGUI();
    }

    EntryWidgetSource::~EntryWidgetSource()
    {
        if ( m_entry != NULL ) delete m_entry;
    }

    bool EntryWidgetSource::isModified()
    {
        return m_textEditSource->isModified();
    }

    bool EntryWidgetSource::containsValidText()
    {
        BibTeX::Entry dummy( BibTeX::Entry::etArticle, "iaKWjDMVuB2vTQDuYRK49Y85tyxa9" );
        apply( &dummy );
        return dummy.id().compare( "iaKWjDMVuB2vTQDuYRK49Y85tyxa9" ) != 0;
    }

    TQString EntryWidgetSource::text()
    {
        return m_textEditSource->text();
    }

    void EntryWidgetSource::setText( const TQString& text )
    {
        m_textEditSource->setText( text );
    }

    void EntryWidgetSource::updateGUI( BibTeX::Entry::EntryType /*entryType*/, bool /*enableAll*/ )
    {
        // nothing
    }

    void EntryWidgetSource::apply( BibTeX::Entry *entry )
    {
        Settings * settings = Settings::self();
        TQBuffer buffer;
        BibTeX::FileImporterBibTeX importer( settings->editing_FirstNameFirst );

        buffer.open( IO_WriteOnly );
        TQTextStream ts( &buffer );
        ts.setEncoding( TQTextStream::UnicodeUTF8 );
        ts << m_textEditSource->text() << endl;
        buffer.close();

        buffer.open( IO_ReadOnly );
        BibTeX::File *bibtexFile = importer.load( &buffer );
        buffer.close();

        if ( bibtexFile != NULL )
        {
            if ( bibtexFile->count() == 1 )
            {
                BibTeX::Entry * parsedEntry = dynamic_cast<BibTeX::Entry*>( bibtexFile->at( 0 ) );
                if ( parsedEntry != NULL )
                {
                    entry->copyFrom( parsedEntry );
                    settings->addToCompletion( parsedEntry );
                }
            }
            delete bibtexFile;
        }
    }

    void EntryWidgetSource::reset( BibTeX::Entry *entry )
    {
        if ( m_entry != entry )
        {
            if ( m_entry != NULL ) m_entry->copyFrom( entry ); else m_entry = new BibTeX::Entry( entry );
        }

        Settings * settings = Settings::self();

        TQBuffer buffer;
        BibTeX::FileExporterBibTeX exporter;
        exporter.setEncoding( "latex" );
        exporter.setStringDelimiter( settings->fileIO_BibtexStringOpenDelimiter, settings->fileIO_BibtexStringCloseDelimiter );
        exporter.setKeywordCasing( settings->fileIO_KeywordCasing );
        exporter.setEnclosingCurlyBrackets( settings->fileIO_EnclosingCurlyBrackets );

        buffer.open( IO_WriteOnly );
        exporter.save( &buffer, entry );
        buffer.close();

        buffer.open( IO_ReadOnly );
        TQTextStream ts( &buffer );
        ts.setEncoding( TQTextStream::UnicodeUTF8 );
        m_textEditSource->setText( ts.read() );
        buffer.close();
    }

    void EntryWidgetSource::restore()
    {
        reset( m_entry );
    }

    void EntryWidgetSource::updateWarnings( BibTeX::Entry::EntryType /*entryType*/, TQListView * /*listViewWarnings*/ )
    {
        // nothing
    }

    void EntryWidgetSource::setupGUI()
    {
        TQGridLayout * gridLayout = new TQGridLayout( this, 2, 2, KDialog::marginHint(), KDialog::spacingHint(), "gridLayout" );
        gridLayout->setRowStretch( 0, 5 );
        gridLayout->setColStretch( 0, 5 );

        m_textEditSource = new TQTextEdit( this );
        gridLayout->addMultiCellWidget( m_textEditSource, 0, 0, 0, 1 );
        m_textEditSource->setFont( KGlobalSettings::fixedFont() );
        m_textEditSource->setReadOnly( m_isReadOnly );

        KPushButton *btnReset = new KPushButton( i18n( "Restore" ), this );
        gridLayout->addWidget( btnReset, 1, 1 );
        connect( btnReset, SIGNAL( clicked() ), this, SLOT( restore() ) );
    }

}
#include "entrywidgetsource.moc"