summaryrefslogtreecommitdiffstats
path: root/src/entrywidgetexternal.cpp
blob: ef5e84c858e06f9a18688726118fad38ef06e92a (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/***************************************************************************
*   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 <ntqlabel.h>
#include <ntqtooltip.h>

#include <kpushbutton.h>
#include <kdialog.h>
#include <tdelocale.h>
#include <tdefiledialog.h>
#include <kurl.h>
#include <kurllabel.h>
#include <kiconloader.h>
#include <tdepopupmenu.h>

#include <fieldlineedit.h>
#include <settings.h>
#include "entrywidgetexternal.h"

namespace KBibTeX
{

    const int EntryWidgetExternal::m_menuIdCurrentDirectory = 8192;
    const int EntryWidgetExternal::m_menuIdPreviousDirectory = 8191;
    TQString EntryWidgetExternal::m_previousDirectory = TQString::null;

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

    EntryWidgetExternal::~EntryWidgetExternal()
    {
        // nothing
    }

    bool EntryWidgetExternal::isModified()
    {
        return m_fieldLineEditURL->isModified() || m_fieldLineEditDoi->isModified() || m_fieldLineEditLocalFile->isModified();
    }

    void EntryWidgetExternal::updateGUI( BibTeX::Entry::EntryType entryType, bool enableAll )
    {
        bool enableWidget = enableAll || BibTeX::Entry::getRequireStatus( entryType, BibTeX::EntryField::ftURL ) != BibTeX::Entry::frsIgnored;
        m_fieldLineEditURL->setEnabled( enableWidget );

        enableWidget = enableAll || BibTeX::Entry::getRequireStatus( entryType, BibTeX::EntryField::ftDoi ) != BibTeX::Entry::frsIgnored;
        m_fieldLineEditDoi->setEnabled( enableWidget );

        enableWidget = enableAll || BibTeX::Entry::getRequireStatus( entryType, BibTeX::EntryField::ftLocalFile ) != BibTeX::Entry::frsIgnored;
        m_fieldLineEditLocalFile->setEnabled( enableWidget );
        m_pushButtonBrowseLocalFile->setEnabled( !m_isReadOnly && enableWidget );

        updateGUI();
    }

    void EntryWidgetExternal::apply( BibTeX::Entry *entry )
    {
        BibTeX::Value * value = m_fieldLineEditURL->value();
        setValue( entry, BibTeX::EntryField::ftURL, value );
        delete value;

        value = m_fieldLineEditDoi->value();
        setValue( entry, BibTeX::EntryField::ftDoi, value );
        delete value;

        value = m_fieldLineEditLocalFile->value();
        setValue( entry, BibTeX::EntryField::ftLocalFile, value );
        delete value;
    }

    void EntryWidgetExternal::reset( BibTeX::Entry *entry )
    {
        disconnect( m_fieldLineEditURL, SIGNAL( textChanged() ), this, SLOT( updateGUI() ) );
        disconnect( m_fieldLineEditDoi, SIGNAL( textChanged() ), this, SLOT( updateGUI() ) );
        disconnect( m_fieldLineEditLocalFile, SIGNAL( textChanged() ), this, SLOT( updateGUI() ) );


        BibTeX::EntryField * field = entry->getField( BibTeX::EntryField::ftURL );
        m_fieldLineEditURL->setValue( field != NULL ? field->value() : NULL );

        field = entry->getField( BibTeX::EntryField::ftDoi );
        m_fieldLineEditDoi->setValue( field != NULL ? field->value() : NULL );

        field = entry->getField( BibTeX::EntryField::ftLocalFile );
        m_fieldLineEditLocalFile->setValue( field != NULL ? field->value() : NULL );

        updateGUI();

        connect( m_fieldLineEditURL, SIGNAL( textChanged() ), this, SLOT( updateGUI() ) );
        connect( m_fieldLineEditDoi, SIGNAL( textChanged() ), this, SLOT( updateGUI() ) );
        connect( m_fieldLineEditLocalFile, SIGNAL( textChanged() ), this, SLOT( updateGUI() ) );
    }

    void EntryWidgetExternal::updateWarnings( BibTeX::Entry::EntryType entryType, TQListView *listViewWarnings )
    {
        addMissingWarning( entryType, BibTeX::EntryField::ftURL, m_fieldLineEditURL->caption(), !m_fieldLineEditURL->isEmpty(), m_fieldLineEditURL, listViewWarnings );

        addMissingWarning( entryType, BibTeX::EntryField::ftDoi, m_fieldLineEditDoi->caption(), !m_fieldLineEditDoi->isEmpty(), m_fieldLineEditDoi, listViewWarnings );

        addMissingWarning( entryType, BibTeX::EntryField::ftLocalFile, m_fieldLineEditDoi->caption(), !m_fieldLineEditDoi->isEmpty(), m_fieldLineEditLocalFile, listViewWarnings );

        addFieldLineEditWarning( m_fieldLineEditURL, m_fieldLineEditURL->caption(), listViewWarnings );

        addFieldLineEditWarning( m_fieldLineEditDoi, m_fieldLineEditDoi->caption(), listViewWarnings );

        updateGUI();
    }

    void EntryWidgetExternal::openURL()
    {
        BibTeX::Value * value = m_fieldLineEditURL->value();
        KURL url = Settings::locateFile( value->text(), m_bibtexfile->fileName, this );
        if ( url.isValid() )
            Settings::openUrl( url, this );
        else
            m_pushButtonOpenURL->setEnabled( FALSE );
    }

    void EntryWidgetExternal::openURL( const TQString &url )
    {
        Settings::openUrl( KURL( url ), this );
    }

    void EntryWidgetExternal::openDoi()
    {
        BibTeX::Value * value = m_fieldLineEditDoi->value();
        KURL url = Settings::doiURL( value->text() );
        if ( url.isValid() )
            Settings::openUrl( url, this );
        else
            m_pushButtonOpenDoi->setEnabled( FALSE );
    }

    void EntryWidgetExternal::openLocalFile()
    {
        BibTeX::Value * value = m_fieldLineEditLocalFile->value();
        KURL url = Settings::locateFile( value->text(), m_bibtexfile->fileName, this );
        if ( url.isValid() )
            Settings::openUrl( url, this );
        else
            m_pushButtonOpenLocalFile->setEnabled( FALSE );
    }

    void EntryWidgetExternal::browseLocalFile( int id )
    {
        Settings * settings = Settings::self();
        TQString startDir = TQString::null;
        if ( id == m_menuIdPreviousDirectory && m_previousDirectory != TQString::null )
            startDir = m_previousDirectory;
        else if ( settings->editing_DocumentSearchPaths.size() > ( unsigned int )id )
            startDir = *settings->editing_DocumentSearchPaths.at( id );
        else
            startDir = TQDir::currentDirPath();
        TQString filename = KFileDialog::getOpenFileName( startDir );

        if ( !filename.isEmpty() )
        {
            /** if the user started browsing for files from a search path,
            * remove this search path prefix from the absolute path returned
            * by the file open dialog.                                               */
            if ( id >= 0 && filename.startsWith( startDir ) )
            {
                int len = startDir.length();
                if ( startDir == "/" )
                    len = 0;
                else if ( !startDir.endsWith( "/" ) )
                    ++len;
                filename = filename.mid( len );
            }

            BibTeX::Value * value = new BibTeX::Value();
            value->items.append( new BibTeX::PlainText( filename ) );
            m_fieldLineEditLocalFile->setValue( value );

            if ( m_previousDirectory == TQString::null )
            {
                TQPopupMenu *menu = m_pushButtonBrowseLocalFile->popup();
                menu->insertItem( TQIconSet( SmallIcon( "favorite" ) ), i18n( "Previously used directory" ), m_menuIdPreviousDirectory, 2 );
            }
            TQFileInfo fileInfo( filename );
            m_previousDirectory = fileInfo.dirPath( true );
        }
    }

    void EntryWidgetExternal::updateGUI()
    {
        BibTeX::Value * value = m_fieldLineEditURL->value();
        m_pushButtonOpenURL->setEnabled( value != NULL && Settings::locateFile( value->text(), m_bibtexfile->fileName, this ).isValid() );

        value = m_fieldLineEditLocalFile->value();
        m_pushButtonOpenLocalFile->setEnabled( value != NULL && Settings::locateFile( value->text(), m_bibtexfile->fileName, this ).isValid() );

        value = m_fieldLineEditDoi->value();
        m_pushButtonOpenDoi->setEnabled( value != NULL && Settings::doiURL( value->text() ).isValid() );
    }

    void EntryWidgetExternal::setupGUI()
    {
        Settings * settings = Settings::self();

        TQGridLayout * gridLayout = new TQGridLayout( this, 5, 3, KDialog::marginHint(), KDialog::spacingHint(), "gridLayout" );
        gridLayout->setRowStretch( 4, 10 );

        TQLabel *label = new TQLabel( TQString( "%1:" ).arg( i18n( "URL" ) ), this );
        gridLayout->addWidget( label, 0, 0 );
        m_fieldLineEditURL = new KBibTeX::FieldLineEdit( i18n( "URL" ), KBibTeX::FieldLineEdit::itSingleLine, m_isReadOnly, this, "m_fieldLineEditURL" );
        m_fieldLineEditURL->setFieldType( BibTeX::EntryField::ftURL );
        gridLayout->addWidget( m_fieldLineEditURL, 0, 1 );
        label->setBuddy( m_fieldLineEditURL );
        connect( m_fieldLineEditURL, SIGNAL( textChanged() ), this, SLOT( updateGUI() ) );

        m_pushButtonOpenURL = new KPushButton( this );
        gridLayout->addWidget( m_pushButtonOpenURL, 0, 2 );
        m_pushButtonOpenURL->setIconSet( TQIconSet( SmallIcon( "fileopen" ) ) );
        m_pushButtonOpenURL->setSizePolicy( TQSizePolicy::Minimum, TQSizePolicy::Minimum );
        TQToolTip::add( m_pushButtonOpenURL, TQString( i18n( "Open %1" ) ).arg( i18n( "URL" ) ) );
        connect( m_pushButtonOpenURL, SIGNAL( clicked() ), this, SLOT( openURL() ) );

        KURLLabel *urllabel = new KURLLabel( "http://www.doi.org/", TQString( "%1:" ).arg( i18n( "DOI" ) ), this );
        TQToolTip::add( urllabel, i18n( "Digital Object Identifier" ) );
        urllabel->setFocusPolicy( TQWidget::NoFocus );
        gridLayout->addWidget( urllabel, 1, 0 );
        m_fieldLineEditDoi = new KBibTeX::FieldLineEdit( i18n( "DOI" ), KBibTeX::FieldLineEdit::itSingleLine, m_isReadOnly, this, "m_fieldLineEditDoi" );
        m_fieldLineEditDoi->setFieldType( BibTeX::EntryField::ftDoi );
        gridLayout->addWidget( m_fieldLineEditDoi, 1, 1 );
        urllabel->setBuddy( m_fieldLineEditDoi );
        connect( m_fieldLineEditDoi, SIGNAL( textChanged() ), this, SLOT( updateGUI() ) );
        connect( urllabel, SIGNAL( leftClickedURL( const TQString& ) ), this, SLOT( openURL( const TQString& ) ) );

        m_pushButtonOpenDoi = new KPushButton( this );
        gridLayout->addWidget( m_pushButtonOpenDoi, 1, 2 );
        m_pushButtonOpenDoi->setIconSet( TQIconSet( SmallIcon( "fileopen" ) ) );
        m_pushButtonOpenDoi->setSizePolicy( TQSizePolicy::Minimum, TQSizePolicy::Minimum );
        TQToolTip::add( m_pushButtonOpenDoi, i18n( "Open DOI" ) );
        connect( m_pushButtonOpenDoi, SIGNAL( clicked() ), this, SLOT( openDoi() ) );

        label = new TQLabel( TQString( "%1:" ).arg( i18n( "Local File" ) ), this );
        gridLayout->addWidget( label, 2, 0 );
        m_fieldLineEditLocalFile = new KBibTeX::FieldLineEdit( i18n( "Local File" ), KBibTeX::FieldLineEdit::itSingleLine, m_isReadOnly, this, "m_fieldLineEditLocalFile" );
        m_fieldLineEditLocalFile->setFieldType( BibTeX::EntryField::ftLocalFile );
        gridLayout->addWidget( m_fieldLineEditLocalFile, 2, 1 );
        label->setBuddy( m_fieldLineEditLocalFile );
        connect( m_fieldLineEditLocalFile, SIGNAL( textChanged() ), this, SLOT( updateGUI() ) );

        m_pushButtonOpenLocalFile = new KPushButton( this );
        gridLayout->addWidget( m_pushButtonOpenLocalFile, 2, 2 );
        m_pushButtonOpenLocalFile->setIconSet( TQIconSet( SmallIcon( "fileopen" ) ) );
        m_pushButtonOpenLocalFile->setSizePolicy( TQSizePolicy::Minimum, TQSizePolicy::Minimum );
        TQToolTip::add( m_pushButtonOpenLocalFile, TQString( i18n( "Open %1" ) ).arg( i18n( "Local File" ) ) );
        connect( m_pushButtonOpenLocalFile, SIGNAL( clicked() ), this, SLOT( openLocalFile() ) );

        TQWidget *container = new TQWidget( this );
        TQHBoxLayout *containerLayout = new TQHBoxLayout( container );
        gridLayout->addWidget( container, 3, 1 );
        m_pushButtonBrowseLocalFile = new KPushButton( i18n( "&Browse..." ), container );
        TQToolTip::add( m_pushButtonBrowseLocalFile, i18n( "Browse for a local file" ) );
        m_pushButtonBrowseLocalFile->setSizePolicy( TQSizePolicy::Minimum, TQSizePolicy::Minimum );
        containerLayout->addWidget( m_pushButtonBrowseLocalFile );
        containerLayout->addStretch( 10 );
        m_pushButtonBrowseLocalFile->setIconSet( TQIconSet( SmallIcon( "fileopen" ) ) );

        TDEPopupMenu *menu = new TDEPopupMenu( m_pushButtonBrowseLocalFile );
        menu->insertTitle( i18n( "Select base directory" ) );
        menu->insertItem( TQIconSet( SmallIcon( "favorite" ) ), i18n( "Current directory" ), m_menuIdCurrentDirectory );
        if ( m_previousDirectory != TQString::null )
            menu->insertItem( TQIconSet( SmallIcon( "favorite" ) ), i18n( "Previously used directory" ), m_menuIdPreviousDirectory );
        if ( !settings->editing_DocumentSearchPaths.isEmpty() )
        {
            menu->insertSeparator();
            int i = 0;
            for ( TQStringList::Iterator it = settings->editing_DocumentSearchPaths.begin(); it != settings->editing_DocumentSearchPaths.end(); ++it, ++i )
                menu->insertItem( TQIconSet( SmallIcon( "folder" ) ), *it, i );
        }
        m_pushButtonBrowseLocalFile->setPopup( menu );
        connect( menu, SIGNAL( activated( int ) ), this, SLOT( browseLocalFile( int ) ) );
    }
}
#include "entrywidgetexternal.moc"