~ubuntu-branches/ubuntu/wily/kbibtex/wily

« back to all changes in this revision

Viewing changes to src/documentwidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2009-06-15 12:41:19 UTC
  • mfrom: (1.1.3 upstream) (2.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090615124119-eoojhlvkmw3o9tkm
Tags: 0.2.2-1
* New upstream version (Closes: #487301, #507837, #518959, #520392).
* Bumped Standards-Version to 3.8.1. No changes necessary.
* Added 'DM-Upload-Allowed: yes'.
* Added missing '${misc:Depends}'.
* Support for per-file encoding settings (Closes: #514356)
* Upgrade debhelper compatibility to version 5.
* Update debian/copyright for 2009 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/***************************************************************************
2
 
*   Copyright (C) 2004-2008 by Thomas Fischer                             *
 
2
*   Copyright (C) 2004-2009 by Thomas Fischer                             *
3
3
*   fischer@unix-ag.uni-kl.de                                             *
4
4
*                                                                         *
5
5
*   This program is free software; you can redistribute it and/or modify  *
38
38
#include <ktempfile.h>
39
39
#include <kglobalsettings.h>
40
40
#include <kurl.h>
 
41
#include <kinputdialog.h>
41
42
#include <kpopupmenu.h>
42
43
#include <kiconloader.h>
43
44
#include <kio/netaccess.h>
62
63
#include <commentwidget.h>
63
64
#include <entrywidget.h>
64
65
#include <fileimporterbibtex.h>
65
 
#ifdef HAVE_LIBBIBUTILS
66
66
#include <fileimporterbibutils.h>
67
 
#else // HAVE_LIBBIBUTILS
68
67
#include <fileimporterris.h>
69
 
#endif // HAVE_LIBBIBUTILS
70
68
#include <fileexporterbibtex.h>
71
 
#ifdef HAVE_LIBBIBUTILS
72
69
#include <fileexporterbibutils.h>
73
 
#else // HAVE_LIBBIBUTILS
74
70
#include <fileexporterris.h>
75
 
#endif // HAVE_LIBBIBUTILS
76
71
#include <fileexporterxml.h>
77
72
#include <fileexporterrtf.h>
 
73
#include <fileexporterdocbook5.h>
78
74
#include <fileexporterpdf.h>
79
75
#include <fileexporterps.h>
80
76
#include <fileexporterxslt.h>
88
84
#include <preamble.h>
89
85
#include <settings.h>
90
86
#include <webquery.h>
91
 
#include <mergeentries.h>
 
87
#include <mergeelements.h>
 
88
#include <idsuggestions.h>
92
89
 
93
90
#include "documentwidget.h"
94
91
 
204
201
        if ( usingDefaultImporter )
205
202
        {
206
203
            Settings * settings = Settings::self( NULL );
207
 
            importer = new BibTeX::FileImporterBibTeX( settings->editing_FirstNameFirst );
 
204
            importer = new BibTeX::FileImporterBibTeX( settings->editing_FirstNameFirst, settings->fileIO_Encoding );
208
205
        }
209
206
 
210
207
        startProgress( label, importer );
218
215
        {
219
216
            if ( mergeOnly )
220
217
            {
221
 
                MergeEntries::mergeBibTeXFiles( m_bibtexfile, newFile );
 
218
                for ( QValueList<BibTeX::Element*>::ConstIterator eit = newFile->constBegin(); eit != newFile->constEnd(); ++eit )
 
219
                    m_bibtexfile->appendElement(( *eit )->clone() );
 
220
 
 
221
                if ( KMessageBox::questionYesNo( this, i18n( "Do you want to search for duplicates in the merged document?" ), i18n( "Find duplicates?" ), KGuiItem( i18n( "Find Duplicates" ) ) ) == KMessageBox::Yes )
 
222
                {
 
223
                    MergeElements *me = new MergeElements( this );
 
224
                    me->mergeDuplicates( m_bibtexfile );
 
225
                    delete me;
 
226
                }
222
227
                delete newFile;
223
228
            }
224
229
            else
252
257
    {
253
258
        bool result = FALSE;
254
259
        m_dirWatch.removeFile( m_filename );
 
260
        Settings * settings = Settings::self( NULL );
255
261
 
256
262
        BibTeX::File::FileFormat format = BibTeX::File::formatUndefined;
257
263
        if ( fileName.endsWith( ".rtf", FALSE ) )
260
266
            format = BibTeX::File::formatPDF;
261
267
        else if ( fileName.endsWith( ".bib", FALSE ) )
262
268
            format = BibTeX::File::formatBibTeX;
263
 
#ifdef HAVE_LIBBIBUTILS
264
 
        else if ( fileName.endsWith( ".ref", FALSE ) || fileName.endsWith( ".refer", FALSE ) || fileName.endsWith( ".txt", FALSE ) || fileName.endsWith( ".rfr", FALSE ) )
 
269
        else if ( settings->external_xml2bibAvailable && settings->external_end2xmlAvailable && ( fileName.endsWith( ".ref", FALSE ) || fileName.endsWith( ".refer", FALSE ) || fileName.endsWith( ".txt", FALSE ) || fileName.endsWith( ".rfr", FALSE ) ) )
265
270
            format = BibTeX::File::formatEndNote;
266
 
        else if ( fileName.endsWith( ".isi", FALSE ) || fileName.endsWith( ".cgi", FALSE ) )
 
271
        else if ( settings->external_xml2bibAvailable && settings->external_end2xmlAvailable && ( fileName.endsWith( ".isi", FALSE ) || fileName.endsWith( ".cgi", FALSE ) ) )
267
272
            format = BibTeX::File::formatISI;
268
 
#endif
269
273
        else if ( fileName.endsWith( ".ris", FALSE ) )
270
274
            format = BibTeX::File::formatRIS;
271
275
        else if ( fileName.endsWith( ".ps", FALSE ) )
272
276
            format = BibTeX::File::formatPS;
273
277
        else if ( fileName.endsWith( ".xml", FALSE ) )
274
 
            format = BibTeX::File::formatXML;
 
278
        {
 
279
            QStringList options = QStringList::split( '|', ( settings->external_xml2bibAvailable && settings->external_end2xmlAvailable ? "DocBook5|MODS|internal XML" : "DocBook5|internal XML" ) );
 
280
            bool ok = false;
 
281
            QString answer = KInputDialog::getItem( i18n( "Choose file format" ), QString( i18n( "Choose file format of file '%1':" ) ).arg( fileName ), options, 0, false, &ok, this );
 
282
            if ( ok && !answer.isNull() && answer == "DocBook5" )
 
283
                format = BibTeX::File::formatDocBook5;
 
284
            else if ( ok && !answer.isNull() && answer == "MODS" )
 
285
                format = BibTeX::File::formatMODS;
 
286
            else if ( ok && !answer.isNull() && answer == "internal XML" )
 
287
                format = BibTeX::File::formatXML;
 
288
        }
275
289
        else if ( fileName.endsWith( ".html", FALSE ) || fileName.endsWith( ".xhtml", FALSE ) || fileName.endsWith( ".htm", FALSE ) )
276
290
            format = BibTeX::File::formatHTML;
277
291
 
313
327
        switch ( format )
314
328
        {
315
329
        case BibTeX::File::formatBibTeX:
316
 
            {
317
 
                BibTeX::FileExporterBibTeX * bibtexExporter = new BibTeX::FileExporterBibTeX();
318
 
                bibtexExporter->setStringDelimiter( settings->fileIO_BibtexStringOpenDelimiter, settings->fileIO_BibtexStringCloseDelimiter );
319
 
                bibtexExporter->setKeywordCasing( settings->fileIO_KeywordCasing );
320
 
                bibtexExporter->setEncoding( settings->fileIO_Encoding );
321
 
                bibtexExporter->setEnclosingCurlyBrackets( settings->fileIO_EnclosingCurlyBrackets );
322
 
                exporter = bibtexExporter;
323
 
            }
324
 
            break;
325
 
#ifdef HAVE_LIBBIBUTILS
 
330
        {
 
331
            BibTeX::FileExporterBibTeX * bibtexExporter = new BibTeX::FileExporterBibTeX();
 
332
            bibtexExporter->setStringDelimiter( settings->fileIO_BibtexStringOpenDelimiter, settings->fileIO_BibtexStringCloseDelimiter );
 
333
            bibtexExporter->setKeywordCasing( settings->fileIO_KeywordCasing );
 
334
            bibtexExporter->setEncoding( settings->fileIO_Encoding );
 
335
            bibtexExporter->setEnclosingCurlyBrackets( settings->fileIO_EnclosingCurlyBrackets );
 
336
            exporter = bibtexExporter;
 
337
        }
 
338
        break;
326
339
        case BibTeX::File::formatRIS:
327
 
            exporter = new BibTeX::FileExporterBibUtils( BibTeX::FileExporterBibUtils::ofRIS );
 
340
            if ( settings->external_xml2bibAvailable && settings->external_end2xmlAvailable && settings->fileIO_useBibUtils )
 
341
                exporter = new BibTeX::FileExporterBibUtils( BibTeX::File::formatRIS );
 
342
            else
 
343
                exporter = new BibTeX::FileExporterRIS( );
328
344
            break;
329
345
        case BibTeX::File::formatEndNote:
330
 
            exporter = new BibTeX::FileExporterBibUtils( BibTeX::FileExporterBibUtils::ofEndNote );
331
 
            break;
 
346
        case BibTeX::File::formatEndNoteXML:
 
347
        case BibTeX::File::formatMODS:
332
348
        case BibTeX::File::formatISI:
333
 
            exporter = new BibTeX::FileExporterBibUtils( BibTeX::FileExporterBibUtils::ofISI );
334
 
            break;
335
 
#else
336
 
        case BibTeX::File::formatRIS:
337
 
            exporter = new BibTeX::FileExporterRIS( );
338
 
            break;
339
 
#endif
 
349
            if ( settings->external_xml2bibAvailable && settings->external_end2xmlAvailable )
 
350
                exporter = new BibTeX::FileExporterBibUtils( format );
 
351
            break;
340
352
        case BibTeX::File::formatXML:
341
353
            exporter = new BibTeX::FileExporterXML();
342
354
            break;
345
357
            {
346
358
            case BibTeX::FileExporterExternal::exporterNone:
347
359
            case BibTeX::FileExporterExternal::exporterXSLT:
348
 
                {
349
 
                    KStandardDirs * kstd = KGlobal::dirs();
350
 
                    QString resPath = kstd->findResource( "data", "kbibtexpart/xslt/html.xsl" );
351
 
                    if ( resPath != NULL )
352
 
                        transform = new BibTeX::XSLTransform( resPath );
 
360
            {
 
361
                KStandardDirs * kstd = KGlobal::dirs();
 
362
                QString resPath = kstd->findResource( "data", "kbibtexpart/xslt/html.xsl" );
 
363
                if ( resPath != NULL )
 
364
                    transform = new BibTeX::XSLTransform( resPath );
353
365
 
354
 
                    if ( transform != NULL )
355
 
                        exporter = new BibTeX::FileExporterXSLT( transform );
356
 
                    else
357
 
                    {
358
 
                        kdDebug() << "XSLT for HTML export is not available" << endl;
359
 
                    }
 
366
                if ( transform != NULL )
 
367
                    exporter = new BibTeX::FileExporterXSLT( transform );
 
368
                else
 
369
                {
 
370
                    kdDebug() << "XSLT for HTML export is not available" << endl;
360
371
                }
361
 
                break;
 
372
            }
 
373
            break;
362
374
            default:
363
 
                {
364
 
                    exporter = new BibTeX::FileExporterExternal( settings->fileIO_ExporterHTML, BibTeX::File::formatHTML );
365
 
                }
 
375
            {
 
376
                exporter = new BibTeX::FileExporterExternal( settings->fileIO_ExporterHTML, BibTeX::File::formatHTML );
 
377
            }
366
378
            }
367
379
            break;
368
380
        case BibTeX::File::formatRTF:
369
 
            {
370
 
                if ( !settings->external_latex2rtfAvailable )
371
 
                {
372
 
                    QString msg = i18n( "To export a BibTeX document to the Rich Text Format (RTF) you need to have installed the program latex2rtf." ) ;
373
 
                    KMessageBox::information( this, msg );
374
 
                    errorLog->append( msg );
375
 
                    return FALSE;
376
 
                }
377
 
 
 
381
        {
 
382
            if ( !settings->external_latex2rtfAvailable )
 
383
            {
 
384
                QString msg = i18n( "To export a BibTeX document to the Rich Text Format (RTF) KBibTeX requires the program 'latex2rtf'." ) ;
 
385
                KMessageBox::information( this, msg );
 
386
                errorLog->append( msg );
 
387
            }
 
388
            else
 
389
            {
378
390
                BibTeX::FileExporterRTF *rtfExporter = new BibTeX::FileExporterRTF();
379
391
                rtfExporter->setLaTeXLanguage( settings->fileIO_ExportLanguage );
380
392
                rtfExporter->setLaTeXBibliographyStyle( settings->fileIO_ExportBibliographyStyle );
381
393
                exporter = rtfExporter;
382
394
            }
383
 
            break;
 
395
        }
 
396
        break;
 
397
        case BibTeX::File::formatDocBook5:
 
398
        {
 
399
            if ( settings->fileIO_bib2db5ClassPath == QString::null )
 
400
            {
 
401
                QString msg = i18n( "To export a BibTeX document to the DocBook5 format KBibTeX requires the program 'bib2db5'." ) ;
 
402
                KMessageBox::information( this, msg );
 
403
                errorLog->append( msg );
 
404
            }
 
405
            else
 
406
            {
 
407
                BibTeX::FileExporterDocBook5 *db5Exporter = new BibTeX::FileExporterDocBook5( settings->fileIO_bib2db5ClassPath );
 
408
                exporter = db5Exporter;
 
409
            }
 
410
        }
 
411
        break;
384
412
        case BibTeX::File::formatPDF:
 
413
        {
 
414
            if ( settings->fileIO_EmbedFiles && !Settings::kpsewhich( "embedfile.sty" ) )
385
415
            {
386
 
                if ( settings->fileIO_EmbedFiles && !Settings::kpsewhich( "embedfile.sty" ) )
387
 
                {
388
 
                    KMessageBox::sorry( this, i18n( "Embedding files into the PDF file is enabled, but the required file 'embedfile.sty' was not found. Embedding files will be disabled." ), i18n( "Embedding files disabled" ) );
389
 
                    settings->fileIO_EmbedFiles = FALSE;
390
 
                }
 
416
                KMessageBox::sorry( this, i18n( "Embedding files into the PDF file is enabled, but the required file 'embedfile.sty' was not found. Embedding files will be disabled." ), i18n( "Embedding files disabled" ) );
 
417
                settings->fileIO_EmbedFiles = FALSE;
 
418
            }
391
419
 
392
 
                BibTeX::FileExporterPDF *pdfExporter = new BibTeX::FileExporterPDF( settings->fileIO_EmbedFiles );
393
 
                pdfExporter->setLaTeXLanguage( settings->fileIO_ExportLanguage );
394
 
                pdfExporter->setLaTeXBibliographyStyle( settings->fileIO_ExportBibliographyStyle );
395
 
                QStringList searchPaths;
396
 
                searchPaths.append( settings->editing_DocumentSearchPath );
397
 
                if ( m_bibtexfile->fileName != QString::null )
398
 
                    searchPaths.append( KURL( m_bibtexfile->fileName ).directory( FALSE, FALSE ) );
399
 
                pdfExporter->setDocumentSearchPaths( searchPaths );
400
 
                exporter = pdfExporter;
401
 
            }
402
 
            break;
 
420
            BibTeX::FileExporterPDF *pdfExporter = new BibTeX::FileExporterPDF( settings->fileIO_EmbedFiles );
 
421
            pdfExporter->setLaTeXLanguage( settings->fileIO_ExportLanguage );
 
422
            pdfExporter->setLaTeXBibliographyStyle( settings->fileIO_ExportBibliographyStyle );
 
423
            QStringList searchPaths;
 
424
            for ( QStringList::Iterator it = settings->editing_DocumentSearchPaths.begin(); it != settings->editing_DocumentSearchPaths.end(); ++it )
 
425
                searchPaths.append( *it );
 
426
            if ( m_bibtexfile->fileName != QString::null )
 
427
                searchPaths.append( KURL( m_bibtexfile->fileName ).directory( FALSE, FALSE ) );
 
428
            pdfExporter->setDocumentSearchPaths( searchPaths );
 
429
            exporter = pdfExporter;
 
430
        }
 
431
        break;
403
432
        case BibTeX::File::formatPS:
404
 
            {
405
 
                BibTeX::FileExporterPS *psExporter = new BibTeX::FileExporterPS();
406
 
                psExporter->setLaTeXLanguage( settings->fileIO_ExportLanguage );
407
 
                psExporter->setLaTeXBibliographyStyle( settings->fileIO_ExportBibliographyStyle );
408
 
                exporter = psExporter;
409
 
            }
410
 
            break;
 
433
        {
 
434
            BibTeX::FileExporterPS *psExporter = new BibTeX::FileExporterPS();
 
435
            psExporter->setLaTeXLanguage( settings->fileIO_ExportLanguage );
 
436
            psExporter->setLaTeXBibliographyStyle( settings->fileIO_ExportBibliographyStyle );
 
437
            exporter = psExporter;
 
438
        }
 
439
        break;
411
440
        default:
412
441
            kdDebug() << "Unsupported export format selected" << endl;
413
442
        }
579
608
 
580
609
    void DocumentWidget::refreshBibTeXFile()
581
610
    {
582
 
        m_listViewElements->setBibTeXFile( m_bibtexfile );
583
 
        m_sourceView->setBibTeXFile( m_bibtexfile );
 
611
        if ( currentPage() == m_sourceView )
 
612
            m_sourceView->setBibTeXFile( m_bibtexfile );
 
613
        else if ( currentPage() == m_container )
 
614
            m_listViewElements->setBibTeXFile( m_bibtexfile );
584
615
    }
585
616
 
586
617
    void DocumentWidget::setFactory( KXMLGUIFactory *factory, KXMLGUIClient *client )
671
702
        QStringList fileKeywords;
672
703
 
673
704
        /**
674
 
        * Fetch keywords from current entry into entryKeywords list
 
705
        * Fetch keywords from selected entries into entryKeywords list
675
706
        */
676
 
        QListViewItem * item = m_listViewElements->selectedItem();
677
 
        if ( item == NULL )
678
 
            item = m_listViewElements->currentItem();
679
 
        DocumentListViewItem * dlvi = dynamic_cast<DocumentListViewItem*>( item );
680
 
        BibTeX::Element * currentElement = NULL;
681
 
        BibTeX::Entry *entry = NULL;
682
 
        BibTeX::EntryField *field = NULL;
683
 
        BibTeX::Value * valueKeywords = NULL;
684
 
        if ( dlvi != NULL && ( currentElement = dlvi->element() ) != NULL && ( entry = dynamic_cast<BibTeX::Entry*>( currentElement ) ) != NULL && ( field = entry->getField( BibTeX::EntryField::ftKeywords ) ) != NULL && ( valueKeywords = field->value() ) != NULL )
685
 
            for ( QValueList<BibTeX::ValueItem*>::ConstIterator it = valueKeywords->items.begin();it != valueKeywords->items.end();++it )
 
707
        QValueList<BibTeX::Entry*> entryList;
 
708
        for ( QListViewItemIterator it( m_listViewElements, QListViewItemIterator::Selected ); it.current(); ++it )
 
709
        {
 
710
            BibTeX::Element * currentElement = NULL;
 
711
            DocumentListViewItem * dlvi = dynamic_cast<DocumentListViewItem*>( it.current() );
 
712
            if ( dlvi && (( currentElement = dlvi->element() ) != NULL ) )
686
713
            {
687
 
                BibTeX::KeywordContainer *container = dynamic_cast<BibTeX::KeywordContainer*>( *it );
688
 
                if ( container != NULL )
689
 
                    for ( QValueList<BibTeX::Keyword*>::ConstIterator kit = container->keywords.begin();kit != container->keywords.end();++kit )
690
 
                        entryKeywords.append(( *kit )->text() );
 
714
                BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( currentElement );
 
715
                if ( entry != NULL )
 
716
                    entryList << entry;
691
717
            }
 
718
        }
 
719
 
 
720
        for ( QValueList<BibTeX::Entry*>::Iterator it = entryList.begin(); it != entryList.end(); ++it )
 
721
        {
 
722
            BibTeX::Entry *entry = *it;
 
723
            BibTeX::EntryField *field = NULL;
 
724
            BibTeX::Value *valueKeywords = NULL;
 
725
            if (( field = entry->getField( BibTeX::EntryField::ftKeywords ) ) != NULL && ( valueKeywords = field->value() ) != NULL )
 
726
                for ( QValueList<BibTeX::ValueItem*>::ConstIterator it = valueKeywords->items.begin();it != valueKeywords->items.end();++it )
 
727
                {
 
728
                    BibTeX::KeywordContainer *container = dynamic_cast<BibTeX::KeywordContainer*>( *it );
 
729
                    if ( container != NULL )
 
730
                        for ( QValueList<BibTeX::Keyword*>::ConstIterator kit = container->keywords.begin();kit != container->keywords.end();++kit )
 
731
                            entryKeywords.append(( *kit )->text() );
 
732
                }
 
733
        }
692
734
 
693
735
        /**
694
736
        * Fetch all keywords from current file into fileKeywords
718
760
        */
719
761
        Settings * settings = Settings::self( m_bibtexfile );
720
762
        QStringList allKeywords = QStringList( fileKeywords );
721
 
        for ( QStringList::Iterator it =  settings->keyword_GlobalList .begin(); it !=  settings->keyword_GlobalList .end(); ++it )
 
763
        for ( QStringList::Iterator it =  settings->keyword_GlobalList.begin(); it !=  settings->keyword_GlobalList.end(); ++it )
722
764
            if ( !allKeywords.contains( *it ) )
723
765
                allKeywords.append( *it );
724
766
        allKeywords.sort();
839
881
            BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( item->element() );
840
882
            if ( entry != NULL )
841
883
            {
842
 
                QStringList urls = entry->urls();
843
 
                for ( QStringList::Iterator it = urls.begin(); it != urls.end(); ++it )
 
884
                /** prefer local urls over remote urls, so first check for file:// and use other protocols only if no file:// was found */
 
885
                QString protocol = "file";
 
886
                KURL::List urls = getEntryURLs( entry );
 
887
                KURL selected = KURL();
 
888
                while ( !selected.isValid() && protocol != QString::null )
844
889
                {
845
 
                    KURL url = KURL( *it );
846
 
                    if ( url.isValid() && ( !url.isLocalFile() || QFile::exists( url.path() ) ) )
 
890
                    for ( KURL::List::Iterator it = urls.begin(); !selected.isValid() && it != urls.end(); ++it )
847
891
                    {
848
 
                        new KRun( url, this );
849
 
                        openingDocumentOK = TRUE;
850
 
                        break;
 
892
                        KURL url = *it;
 
893
                        if ( url.isValid() && ( !url.isLocalFile() || QFile::exists( url.path() ) ) && url.protocol().startsWith( protocol ) )
 
894
                            selected = url;
851
895
                    }
 
896
                    /** little trick setting protocol prefix in the first round to "file://", then to "", and setting to null to quit the loop */
 
897
                    if ( !protocol.isEmpty() ) protocol = "";
 
898
                    else protocol = QString::null;
 
899
                }
 
900
 
 
901
                if ( selected.isValid() )
 
902
                {
 
903
                    new KRun( selected, this );
 
904
                    openingDocumentOK = TRUE;
852
905
                }
853
906
            }
854
907
        }
857
910
            editElement( item );
858
911
    }
859
912
 
860
 
    void DocumentWidget::editElement()
 
913
    bool DocumentWidget::editElement()
861
914
    {
 
915
        bool result = false;
862
916
        QListViewItem * item = m_listViewElements->selectedItem();
863
917
        if ( item == NULL )
864
918
            item = m_listViewElements->currentItem();
865
919
        DocumentListViewItem * dlvi = dynamic_cast<DocumentListViewItem*>( item );
866
920
        if ( dlvi )
867
 
            editElement( dlvi );
 
921
            result =  editElement( dlvi );
 
922
        return result;
868
923
    }
869
924
 
870
 
    void DocumentWidget::editElement( DocumentListViewItem*item )
 
925
    bool DocumentWidget::editElement( DocumentListViewItem*item )
871
926
    {
872
927
        BibTeX::Element * element = item->element();
873
 
        editElement( element );
874
 
        item->updateItem();
 
928
        bool result = editElement( element );
 
929
        if ( result )
 
930
        {
 
931
            item->updateItem();
 
932
            slotPreviewElement( item );
 
933
        }
 
934
        return result;
875
935
    }
876
936
 
877
 
    void DocumentWidget::editElement( BibTeX::Element*element )
 
937
    bool DocumentWidget::editElement( BibTeX::Element*element )
878
938
    {
879
939
        m_dirWatch.stopScan();
880
 
        editElementDialog( element );
 
940
        bool result = editElementDialog( element );
881
941
        m_dirWatch.startScan( );
882
942
 
883
943
        Settings * settings = Settings::self( m_bibtexfile );
884
944
        settings->addToCompletion( element );
885
945
        m_sideBar->refreshLists( m_bibtexfile );
 
946
 
 
947
        return result;
886
948
    }
887
949
 
888
950
    bool DocumentWidget::editElementDialog( BibTeX::Element *element )
933
995
        if ( m_editMode == emList )
934
996
        {
935
997
            QValueList<BibTeX::Element*> elements = m_listViewElements->selectedItems();
936
 
            QStringList refsToSend;
937
 
 
938
 
            for ( QValueList<BibTeX::Element*>::Iterator it = elements.begin(); it != elements.end(); ++it )
939
 
            {
940
 
                BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( *it );
941
 
                if ( entry != NULL ) refsToSend.append( entry->id() );
942
 
            }
 
998
            QString genericMsg = i18n( "\n\nEither LyX is not running or has not been correctly configured to send references to." );
 
999
            QString title = i18n( "Error communicating with LyX" );
943
1000
 
944
1001
            Settings * settings = Settings::self( m_bibtexfile );
945
1002
            QString lyxPipeFilename = settings->detectLyXInPipe();
946
1003
            kdDebug() << "sendSelectedToLyx: lyxPipeFilename= " << lyxPipeFilename << endl;
947
 
            QFile pipe( lyxPipeFilename );
948
 
            if ( pipe.exists() && pipe.open( IO_WriteOnly ) )
 
1004
 
 
1005
            if ( lyxPipeFilename.isNull() )
 
1006
                KMessageBox::error( this, i18n( "Cannot determine how to send references to LyX." ).append( genericMsg ), title );
 
1007
            else
949
1008
            {
950
 
                QTextStream * writer = new QTextStream( &pipe );
951
 
                QString msg = "LYXCMD:kbibtex:citation-insert:" + refsToSend.join( "," );
952
 
                *writer << msg << endl;
953
 
                delete writer;
954
 
                pipe.close();
 
1009
                QFile pipe( lyxPipeFilename );
 
1010
                if ( !pipe.exists() )
 
1011
                    KMessageBox::error( this, i18n( "The inpipe as configured in LyX does not exist." ).append( genericMsg ), title );
 
1012
                else
 
1013
                {
 
1014
                    if ( !pipe.open( IO_WriteOnly ) )
 
1015
                        KMessageBox::error( this, i18n( "Cannot open the inpipe as configured in LyX." ).append( genericMsg ), title );
 
1016
                    else
 
1017
                    {
 
1018
                        QStringList refsToSend;
 
1019
                        for ( QValueList<BibTeX::Element*>::Iterator it = elements.begin(); it != elements.end(); ++it )
 
1020
                        {
 
1021
                            BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( *it );
 
1022
                            if ( entry != NULL ) refsToSend.append( entry->id() );
 
1023
                        }
 
1024
 
 
1025
                        QTextStream * writer = new QTextStream( &pipe );
 
1026
                        QString msg = "LYXCMD:kbibtex:citation-insert:" + refsToSend.join( "," );
 
1027
                        *writer << msg << endl;
 
1028
                        delete writer;
 
1029
                        pipe.close();
 
1030
                    }
 
1031
                }
955
1032
            }
956
 
            else
957
 
                KMessageBox::error( this, ( lyxPipeFilename.isEmpty() ? i18n( "Cannot establish a link to LyX" ) : QString( i18n( "Cannot establish a link to LyX using the pipe '%1'" ) ).arg( lyxPipeFilename ) ) + i18n( "\nMaybe LyX is not running?" ), i18n( "Error communicating with LyX" ) );
958
 
 
959
1033
        }
960
1034
    }
961
1035
 
1132
1206
        }
1133
1207
 
1134
1208
        if ( queryString != QString::null )
 
1209
        {
 
1210
            queryString = queryString.stripWhiteSpace().replace( '$', "" ).replace( "%", "%25" ).replace( "+", "%2B" ).replace( " ", "%20" ).replace( "#", "%23" ).replace( "&", "%26" ).replace( "?", "%3F" );
1135
1211
            kapp->invokeBrowser( QString( searchURL ).arg( queryString.replace( '{', "" ).replace( '}', "" ) ) );
 
1212
        }
1136
1213
    }
1137
1214
 
1138
1215
    void DocumentWidget::onlineSearch()
1146
1223
                Settings * settings = Settings::self( m_bibtexfile );
1147
1224
                for ( QValueList<BibTeX::Entry*>::Iterator it = list.begin(); it != list.end(); ++it )
1148
1225
                {
 
1226
                    ( *it )->setId( IdSuggestions::resolveConflict( m_bibtexfile, ( *it )->id() ) );
1149
1227
                    if ( m_editMode == emList )
1150
1228
                        m_listViewElements->insertItem( new BibTeX::Entry( *it ) );
1151
1229
                    else
1177
1255
        }
1178
1256
    }
1179
1257
 
 
1258
    void DocumentWidget::findDuplicates()
 
1259
    {
 
1260
        MergeElements *me = new MergeElements( this );
 
1261
        if ( me->mergeDuplicates( m_bibtexfile ) == QDialog::Accepted )
 
1262
        {
 
1263
            refreshBibTeXFile();
 
1264
            slotModified();
 
1265
        }
 
1266
        delete me;
 
1267
    }
 
1268
 
1180
1269
    void DocumentWidget::updateFromGUI()
1181
1270
    {
1182
1271
        BibTeX::File * file = NULL;
1235
1324
 
1236
1325
    BibTeX::FileImporter *DocumentWidget::fileImporterFactory( const QString &fileName )
1237
1326
    {
 
1327
        Settings * settings = Settings::self( NULL );
1238
1328
        BibTeX::FileImporter * importer = NULL;
1239
1329
        BibTeX::File::FileFormat format = BibTeX::File::formatUndefined;
1240
1330
 
1241
1331
        if ( fileName.endsWith( ".bib", FALSE ) )
1242
1332
            format = BibTeX::File::formatBibTeX;
1243
 
#ifdef HAVE_LIBBIBUTILS
1244
 
        else if ( fileName.endsWith( ".ref", FALSE ) || fileName.endsWith( ".refer", FALSE ) || fileName.endsWith( ".txt", FALSE ) || fileName.endsWith( ".rfr", FALSE ) )
 
1333
        else if ( settings->external_xml2bibAvailable && settings->external_end2xmlAvailable && ( fileName.endsWith( ".ref", FALSE ) || fileName.endsWith( ".refer", FALSE ) || fileName.endsWith( ".txt", FALSE ) || fileName.endsWith( ".rfr", FALSE ) ) )
1245
1334
            format = BibTeX::File::formatEndNote;
1246
 
        else if ( fileName.endsWith( ".isi", FALSE ) || fileName.endsWith( ".cgi", FALSE ) )
 
1335
        else if ( settings->external_xml2bibAvailable && settings->external_end2xmlAvailable && ( fileName.endsWith( ".isi", FALSE ) || fileName.endsWith( ".cgi", FALSE ) ) )
1247
1336
            format = BibTeX::File::formatISI;
1248
 
#endif // HAVE_LIBBIBUTILS
1249
1337
        else if ( fileName.endsWith( ".ris", FALSE ) )
1250
1338
            format = BibTeX::File::formatRIS;
 
1339
        else if ( fileName.endsWith( ".xml", FALSE ) )
 
1340
        {
 
1341
            QStringList options = QStringList::split( '|', ( settings->external_xml2bibAvailable && settings->external_end2xmlAvailable ? "MODS|EndNote XML|internal XML" : "internal XML" ) );
 
1342
            bool ok = false;
 
1343
            QString answer = KInputDialog::getItem( i18n( "Choose file format" ), QString( i18n( "Choose file format of file '%1':" ) ).arg( fileName ), options, 0, false, &ok, this );
 
1344
            if ( ok && !answer.isNull() && answer == "DocBook5" )
 
1345
                format = BibTeX::File::formatDocBook5;
 
1346
            else if ( ok && !answer.isNull() && answer == "MODS" )
 
1347
                format = BibTeX::File::formatMODS;
 
1348
            else if ( ok && !answer.isNull() && answer == "EndNote XML" )
 
1349
                format = BibTeX::File::formatEndNoteXML;
 
1350
            else if ( ok && !answer.isNull() && answer == "internal XML" )
 
1351
                format = BibTeX::File::formatXML;
 
1352
        }
 
1353
        else
 
1354
        {
 
1355
            QStringList options = QStringList::split( '|', ( settings->external_xml2bibAvailable && settings->external_end2xmlAvailable ? "BibTeX|EndNote|ISI|RIS" : "BibTeX|RIS" ) );
 
1356
            bool ok = false;
 
1357
            QString answer = KInputDialog::getItem( i18n( "Choose file format" ), QString( i18n( "Choose file format of file '%1':" ) ).arg( fileName ), options, 0, false, &ok, this );
 
1358
            if ( ok && !answer.isNull() && answer == "BibTeX" )
 
1359
                format = BibTeX::File::formatBibTeX;
 
1360
            else if ( ok && !answer.isNull() && answer == "EndNote" )
 
1361
                format = BibTeX::File::formatEndNote;
 
1362
            else if ( ok && !answer.isNull() && answer == "ISI" )
 
1363
                format = BibTeX::File::formatISI;
 
1364
            else if ( ok && !answer.isNull() && answer == "RIS" )
 
1365
                format = BibTeX::File::formatRIS;
 
1366
        }
1251
1367
 
1252
1368
        if ( format != BibTeX::File::formatUndefined )
1253
1369
        {
 
1370
            Settings * settings = Settings::self( NULL );
1254
1371
            switch ( format )
1255
1372
            {
1256
1373
            case BibTeX::File::formatBibTeX:
1257
 
                {
1258
 
                    Settings * settings = Settings::self( NULL );
1259
 
                    importer = new BibTeX::FileImporterBibTeX( settings->editing_FirstNameFirst );
1260
 
                }
 
1374
            {
 
1375
                Settings * settings = Settings::self( NULL );
 
1376
                importer = new BibTeX::FileImporterBibTeX( settings->editing_FirstNameFirst, settings->fileIO_Encoding );
 
1377
            }
 
1378
            break;
 
1379
            case BibTeX::File::formatRIS:
 
1380
            {
 
1381
                if ( settings->external_xml2bibAvailable && settings->external_end2xmlAvailable && settings->fileIO_useBibUtils )
 
1382
                    importer = new BibTeX::FileImporterBibUtils( BibTeX::File::formatRIS );
 
1383
                else
 
1384
                    importer = new BibTeX::FileImporterRIS( );
1261
1385
                break;
1262
 
#ifdef HAVE_LIBBIBUTILS
 
1386
            }
1263
1387
            case BibTeX::File::formatEndNote:
1264
 
                importer = new BibTeX::FileImporterBibUtils( BibTeX::FileImporterBibUtils::ifEndNote );
1265
 
                break;
1266
 
            case BibTeX::File::formatRIS:
1267
 
                importer = new BibTeX::FileImporterBibUtils( BibTeX::FileImporterBibUtils::ifRIS );
1268
 
                break;
 
1388
            case BibTeX::File::formatEndNoteXML:
1269
1389
            case BibTeX::File::formatISI:
1270
 
                importer = new BibTeX::FileImporterBibUtils( BibTeX::FileImporterBibUtils::ifISI );
1271
 
                break;
1272
 
#else
1273
 
            case BibTeX::File::formatRIS:
1274
 
                importer = new BibTeX::FileImporterRIS( );
1275
 
                break;
1276
 
#endif
 
1390
            case BibTeX::File::formatMODS:
 
1391
                if ( settings->external_xml2bibAvailable && settings->external_end2xmlAvailable )
 
1392
                    importer = new BibTeX::FileImporterBibUtils( format );
 
1393
                break;
1277
1394
            default:
1278
 
                {
1279
 
                    KMessageBox::sorry( this, QString( i18n( "The file '%1' does not contain a known type of bibliography." ) ).arg( fileName ), i18n( "Unknown file format" ) );
1280
 
                }
 
1395
            {
 
1396
                KMessageBox::sorry( this, QString( i18n( "The file '%1' does not contain a known type of bibliography." ) ).arg( fileName ), i18n( "Unknown file format" ) );
 
1397
            }
1281
1398
            }
1282
1399
        }
1283
1400
 
1341
1458
    void DocumentWidget::slotTabChanged( QWidget *tab )
1342
1459
    {
1343
1460
        setEnabled( FALSE );
 
1461
        QApplication::setOverrideCursor( Qt::waitCursor );
1344
1462
 
1345
1463
        BibTeX::File *bibTeXFile = NULL;
1346
1464
        if ( m_editMode == emSource )
1406
1524
        if ( m_actionEditCopyRef )
1407
1525
            m_actionEditCopyRef->setEnabled( m_editMode == emList );
1408
1526
 
 
1527
        QApplication::restoreOverrideCursor();
1409
1528
 
1410
1529
        emit undoChanged( m_editMode == emSource );
1411
1530
    }
1441
1560
        new KRun( m_viewDocumentActionMenuURLs[ m_viewDocumentActionMenu->popupMenu() ->indexOf( id )], this );
1442
1561
    }
1443
1562
 
 
1563
    void DocumentWidget::slotViewFirstDocument()
 
1564
    {
 
1565
        for ( QStringList::Iterator it = m_viewDocumentActionMenuURLs.begin(); it != m_viewDocumentActionMenuURLs.end(); ++it )
 
1566
            if (( *it ).endsWith( ".pdf" ) || ( *it ).endsWith( ".ps" ) || ( *it ).endsWith( ".djv" ) )
 
1567
            {
 
1568
                new KRun( *it, this );
 
1569
                break;
 
1570
            }
 
1571
    }
 
1572
 
 
1573
    void DocumentWidget::slotViewFirstDocumentsOnlineRef()
 
1574
    {
 
1575
        for ( QStringList::Iterator it = m_viewDocumentActionMenuURLs.begin(); it != m_viewDocumentActionMenuURLs.end(); ++it )
 
1576
            if (( *it ).contains( "dx.doi.org" ) )
 
1577
            {
 
1578
                new KRun( *it, this );
 
1579
                break;
 
1580
            }
 
1581
    }
 
1582
 
1444
1583
    void DocumentWidget::slotAssignKeywords( int id )
1445
1584
    {
 
1585
        QValueList<BibTeX::Entry*> entryList;
 
1586
 
1446
1587
        QListViewItem * item = m_listViewElements->selectedItem();
1447
1588
        if ( item == NULL )
1448
1589
            item = m_listViewElements->currentItem();
 
1590
        for ( QListViewItemIterator it( m_listViewElements, QListViewItemIterator::Selected ); it.current(); ++it )
 
1591
        {
 
1592
            BibTeX::Element * currentElement = NULL;
 
1593
            DocumentListViewItem * dlvi = dynamic_cast<DocumentListViewItem*>( it.current() );
 
1594
            if ( dlvi && (( currentElement = dlvi->element() ) != NULL ) )
 
1595
            {
 
1596
                BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( currentElement );
 
1597
                if ( entry != NULL )
 
1598
                    entryList << entry;
 
1599
            }
 
1600
        }
1449
1601
 
1450
 
        BibTeX::Element * currentElement = NULL;
1451
 
        DocumentListViewItem * dlvi = dynamic_cast<DocumentListViewItem*>( item );
1452
 
        if ( dlvi && (( currentElement = dlvi->element() ) != NULL ) )
 
1602
        for ( QValueList<BibTeX::Entry*>::Iterator it = entryList.begin(); it != entryList.end(); ++it )
1453
1603
        {
1454
 
            BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( currentElement );
 
1604
            BibTeX::Entry *entry = *it;
1455
1605
            BibTeX::EntryField *field = NULL;
1456
1606
            BibTeX::KeywordContainer *keywordContainer = NULL;
1457
1607
            if (( field = entry->getField( BibTeX::EntryField::ftKeywords ) ) == NULL )
1480
1630
    {
1481
1631
        QString newKeyword = m_lineEditNewKeyword->text();
1482
1632
 
1483
 
        QListViewItem * item = m_listViewElements->selectedItem();
1484
 
        if ( item == NULL )
1485
 
            item = m_listViewElements->currentItem();
1486
 
        DocumentListViewItem * dlvi = dynamic_cast<DocumentListViewItem*>( item );
1487
 
        BibTeX::Element * currentElement = NULL;
1488
 
        BibTeX::Entry * entry = NULL;
1489
 
        if ( dlvi != NULL && ( currentElement = dlvi->element() ) != NULL && ( entry = dynamic_cast<BibTeX::Entry*>( currentElement ) ) != NULL )
 
1633
        QValueList<BibTeX::Entry*> entryList;
 
1634
        for ( QListViewItemIterator it( m_listViewElements, QListViewItemIterator::Selected ); it.current(); ++it )
1490
1635
        {
 
1636
            BibTeX::Element * currentElement = NULL;
 
1637
            DocumentListViewItem * dlvi = dynamic_cast<DocumentListViewItem*>( it.current() );
 
1638
            if ( dlvi && (( currentElement = dlvi->element() ) != NULL ) )
 
1639
            {
 
1640
                BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( currentElement );
 
1641
                if ( entry != NULL )
 
1642
                    entryList << entry;
 
1643
            }
 
1644
        }
1491
1645
 
 
1646
        for ( QValueList<BibTeX::Entry*>::Iterator it = entryList.begin(); it != entryList.end(); ++it )
 
1647
        {
 
1648
            BibTeX::Entry *entry = *it;
1492
1649
            BibTeX::EntryField *field = entry->getField( BibTeX::EntryField::ftKeywords );
1493
1650
            if ( field == NULL )
1494
1651
            {
1506
1663
                value->items.append( keywordContainer = new BibTeX::KeywordContainer() );
1507
1664
 
1508
1665
            keywordContainer->append( newKeyword );
 
1666
        }
1509
1667
 
 
1668
        if ( !entryList.isEmpty() )
 
1669
        {
1510
1670
            if ( m_lineEditNewKeyword->isVisible() &&
1511
1671
                    m_lineEditNewKeyword->parentWidget() &&
1512
1672
                    m_lineEditNewKeyword->parentWidget()->parentWidget() &&
1516
1676
            slotModified();
1517
1677
        }
1518
1678
    }
 
1679
 
 
1680
    void DocumentWidget::slotNormalizeIds()
 
1681
    {
 
1682
        Settings * settings = Settings::self();
 
1683
        QMap<BibTeX::Entry*, QString> entryToNewId;
 
1684
        QMap<BibTeX::Entry*, DocumentListViewItem*> entryToItem;
 
1685
        QStringList msgList;
 
1686
 
 
1687
        if ( settings->idSuggestions_default < 0 )
 
1688
        {
 
1689
            KMessageBox::sorry( this, i18n( "You must set a default id suggestion in the settings dialog." ) );
 
1690
            return;
 
1691
        }
 
1692
 
 
1693
        for ( QListViewItemIterator it( m_listViewElements, QListViewItemIterator::Selected ); it.current(); ++it )
 
1694
        {
 
1695
            BibTeX::Element * currentElement = NULL;
 
1696
            DocumentListViewItem * dlvi = dynamic_cast<DocumentListViewItem*>( it.current() );
 
1697
            if ( dlvi && (( currentElement = dlvi->element() ) != NULL ) )
 
1698
            {
 
1699
                BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( currentElement );
 
1700
                if ( entry != NULL )
 
1701
                {
 
1702
                    QString newEntryId = IdSuggestions::createDefaultSuggestion( m_bibtexfile, entry );
 
1703
                    entryToNewId[entry] = newEntryId;
 
1704
                    entryToItem[entry] = dlvi;
 
1705
                    msgList << "Replacing id of entry \"" + entry->id() + "\" with new id \"" + newEntryId + "\"";
 
1706
                }
 
1707
            }
 
1708
        }
 
1709
 
 
1710
        if ( entryToNewId.isEmpty() )
 
1711
            KMessageBox::information( this, i18n( "No entries were selected." ) );
 
1712
        else if ( KMessageBox::questionYesNoList( this, i18n( "The following changes will be applied to the currently selected elements.\nConflicts with duplicate entry ids will be resolved when applying the new ids." ), msgList, i18n( "Normalize Entry Ids" ), KGuiItem( i18n( "Replace Entry Ids" ), "run" ), KGuiItem( i18n( "Cancel" ), "cancel" ) ) == KMessageBox::Yes )
 
1713
        {
 
1714
            QApplication::setOverrideCursor( Qt::waitCursor );
 
1715
            QMap<BibTeX::Entry*, DocumentListViewItem*>::Iterator itItem = entryToItem.begin();
 
1716
            for ( QMap<BibTeX::Entry*, QString>::Iterator it = entryToNewId.begin(); it != entryToNewId.end(); ++it, ++itItem )
 
1717
            {
 
1718
                BibTeX::Entry *entry = it.key();
 
1719
                QString newEntryId = it.data();
 
1720
                newEntryId = IdSuggestions::resolveConflict( m_bibtexfile, newEntryId, entry );
 
1721
                qDebug( "Replacing id of entry \"%s\" with new id \"%s\"", entry->id().latin1(), newEntryId.latin1() );
 
1722
                entry->setId( newEntryId );
 
1723
                itItem.data()->updateItem();
 
1724
            }
 
1725
            slotModified();
 
1726
            m_sideBar->refreshLists( m_bibtexfile );
 
1727
            QApplication::restoreOverrideCursor();
 
1728
        }
 
1729
    }
 
1730
 
 
1731
    KDirWatch &DocumentWidget::dirWatch()
 
1732
    {
 
1733
        return m_dirWatch;
 
1734
    }
 
1735
 
1519
1736
}
1520
1737
 
1521
1738
#include "documentwidget.moc"