Logo Search packages:      
Sourcecode: kbibtex version File versions  Download package

settings.cpp

/***************************************************************************
*   Copyright (C) 2004-2006 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 <qstring.h>
#include <qprocess.h>
#include <qstringlist.h>
#include <qfont.h>
#include <qregexp.h>

#include <kconfig.h>
#include <kstandarddirs.h>
#include <kmessagebox.h>
#include <klocale.h>
#include <kbibtex_part.h>
#include <kdebug.h>

#include <entryfield.h>
#include <element.h>
#include <entry.h>
#include <macro.h>
#include "settings.h"

namespace KBibTeX
{
    Settings::Settings()
    {
        checkExternalToolsAvailable();

        KStandardDirs * kstd = KGlobal::dirs();
        external_XSLTStylesheetHTML = kstd->findResource( "data", "kbibtexpart/xslt/html.xsl" );
        if ( external_XSLTStylesheetHTML == NULL )
            KMessageBox::error( NULL, i18n( "Could not determine filename for XSLT file" ), i18n( "Initialization failed" ) );

        int numCompletions = BibTeX::EntryField::ftYear - BibTeX::EntryField::ftAbstract + 4;
        m_completion = new KCompletion * [ numCompletions ];
        for ( int i = 0; i < numCompletions;i++ )
        {
            m_completion[ i ] = new KCompletion();
            m_completion[ i ] ->setIgnoreCase( TRUE );
        }
    }

    Settings::~Settings()
    {
        int numCompletions = BibTeX::EntryField::ftYear - BibTeX::EntryField::ftAbstract + 4;
        for ( int i = 0; i < numCompletions;i++ )
            delete m_completion[ i ];
        delete[] m_completion;

        for ( QValueList<SearchURL*>::ConstIterator it = searchURLs.begin(); it != searchURLs.end(); ++it )
            delete *it;
    }

    Settings* Settings::self()
    {
        static Settings * result = new Settings( );
        return result;
    }

    void Settings::load( KConfig * config )
    {
        config->setGroup( "FileIO" );
        fileIO_Encoding = ( BibTeX::File::Encoding ) ( config->readNumEntry( "Encoding", ( int ) ( BibTeX::File::encLaTeX - 1 ) ) + 1 );
        fileIO_ExportLanguage = config->readEntry( "ExportLanguage", "english" );
        fileIO_ExportBibliographyStyle = config->readEntry( "ExportBibliographyStyle", "plain" );
        fileIO_ExporterHTML = ( BibTeX::FileExporterExternal::Exporter ) config->readNumEntry( "ExporterHTML", ( int ) BibTeX::FileExporterExternal::exporterNone );
        fileIO_BibtexStringOpenDelimiter = config->readEntry( "BibtexStringOpenDelimiter", "\"" ).at( 0 );
        fileIO_BibtexStringCloseDelimiter = config->readEntry( "BibtexStringCloseDelimiter", "\"" ).at( 0 );

        config->setGroup( "Editing" );
        editing_EnableAllFields = config->readBoolEntry( "EnableAllFields", false );
        editing_MainListDoubleClickAction = config->readNumEntry( "MainListDoubleClickAction", 0 );
        editing_MainListSortingColumn = config->readNumEntry( "MainListSortingColumn", -1 );
        editing_MainListSortingOrder = config->readNumEntry( "MainListSortingOrder", 1 );
        editing_MainListColumnsWidth = config->readIntListEntry( "MainListColumnsWidth" );
        editing_MainListColumnsIndex = config->readIntListEntry( "MainListColumnsIndex" );
        editing_FilterHistory = config->readListEntry( "FilterHistory" );
        editing_ShowComments = config->readBoolEntry( "ShowComments", TRUE );
        editing_ShowMacros = config->readBoolEntry( "ShowMacros", TRUE );
        editing_HorSplitterSizes = config->readIntListEntry( "HorizontalSplitterSizes" );
        editing_VertSplitterSizes = config->readIntListEntry( "VerticalSplitterSizes" );
        QFont defaultFont = KGlobalSettings::generalFont();
        editing_SpecialFont = config->readFontEntry( "SpecialFont", &defaultFont );
        editing_UseSpecialFont = config->readBoolEntry( "UseSpecialFont", FALSE );

        if ( editing_MainListColumnsWidth.isEmpty() || editing_MainListColumnsIndex.isEmpty() )
        {
            editing_MainListColumnsWidth.clear();
            editing_MainListColumnsIndex.clear();

            int column = 0;
            // for type and id column
            editing_MainListColumnsWidth.append( 0xffff );
            editing_MainListColumnsWidth.append( 0xffff );
            editing_MainListColumnsIndex.append( column++ );
            editing_MainListColumnsIndex.append( column++ );

            for ( int i = 0; i <= ( int ) BibTeX::EntryField::ftYear - ( int ) BibTeX::EntryField::ftAbstract; i++ )
            {
                BibTeX::EntryField::FieldType fieldType = ( BibTeX::EntryField::FieldType ) ( i + ( int ) BibTeX::EntryField::ftAbstract );
                if ( fieldType == BibTeX::EntryField::ftAuthor || fieldType == BibTeX::EntryField::ftTitle )
                    editing_MainListColumnsWidth.append( 0xffff );
                else
                    editing_MainListColumnsWidth.append( 0 );
                editing_MainListColumnsIndex.append( column++ );
            }
        }

        config->setGroup( "SearchURLs" );
        searchURLs.clear();
        for ( int i = 1; i < 1024; i++ )
        {
            QString descr = config->readEntry( QString( "SearchURLDescription%1" ).arg( i ), QString::null );
            QString url = config->readEntry( QString( "SearchURL%1" ).arg( i ), QString::null );
            if ( descr != QString::null && url != QString::null )
            {
                SearchURL * searchURL = new SearchURL;
                searchURL->description = descr;
                searchURL->url = url;
                searchURLs.append( searchURL );
            }
            else
                break;
        }

        if ( searchURLs.count() == 0 )
            restoreDefaultSearchURLs();
    }

    void Settings::save( KConfig * config )
    {
        config->setGroup( "FileIO" );
        config->writeEntry( "Encoding", ( int ) fileIO_Encoding - 1 );
        config->writeEntry( "ExportLanguage", fileIO_ExportLanguage );
        config->writeEntry( "ExportBibliographyStyle", fileIO_ExportBibliographyStyle );
        config->writeEntry( "ExporterHTML", ( int ) fileIO_ExporterHTML );
        config->writeEntry( "BibtexStringOpenDelimiter", QString( fileIO_BibtexStringOpenDelimiter ) );
        config->writeEntry( "BibtexStringCloseDelimiter", QString( fileIO_BibtexStringCloseDelimiter ) );

        config->setGroup( "Editing" );
        config->writeEntry( "EnableAllFields", editing_EnableAllFields );
        config->writeEntry( "MainListDoubleClickAction", editing_MainListDoubleClickAction );
        config->writeEntry( "MainListSortingColumn", editing_MainListSortingColumn );
        config->writeEntry( "MainListSortingOrder", editing_MainListSortingOrder );
        config->writeEntry( "MainListColumnsWidth", editing_MainListColumnsWidth );
        config->writeEntry( "MainListColumnsIndex", editing_MainListColumnsIndex );
        config->writeEntry( "FilterHistory", editing_FilterHistory );
        config->writeEntry( "ShowComments", editing_ShowComments );
        config->writeEntry( "ShowMacros", editing_ShowMacros );
        config->writeEntry( "HorizontalSplitterSizes", editing_HorSplitterSizes );
        config->writeEntry( "VerticalSplitterSizes", editing_VertSplitterSizes );
        config->writeEntry( "SpecialFont", editing_SpecialFont );
        config->writeEntry( "UseSpecialFont", editing_UseSpecialFont );

        config->setGroup( "SearchURLs" );
        int i = 1;
        for ( QValueList<SearchURL*>::ConstIterator it = searchURLs.begin(); it != searchURLs.end(); ++it )
        {
            config->writeEntry( QString( "SearchURLDescription%1" ).arg( i ), ( *it ) ->description );
            config->writeEntry( QString( "SearchURL%1" ).arg( i ), ( *it ) ->url );
        }
    }

    void Settings::checkExternalToolsAvailable()
    {
        external_bibconvAvailable = checkExternalToolAvailable( "bibconv" );
        external_bibtex2htmlAvailable = checkExternalToolAvailable( "bibtex2html" );
        external_bib2xhtmlAvailable = checkExternalToolAvailable( "bib2xhtml" );
        external_latex2rtfAvailable = checkExternalToolAvailable( "latex2rtf" );
    }

    bool Settings::checkExternalToolAvailable( const QString &binary )
    {
        QProcess process( binary );
        if ( !process.start() )
            return FALSE;

        if ( process.normalExit() )
            return TRUE;

        if ( process.isRunning() )
        {
            process.kill();
            return TRUE;
        }

        return FALSE;
    }

    void Settings::addToCompletion( BibTeX::File *file )
    {
        for ( BibTeX::File::ElementList::iterator it = file->begin(); it != file->end(); it++ )
            addToCompletion( *it );
    }

    void Settings::addToCompletion( BibTeX::Element *element )
    {
        BibTeX::Entry * entry = dynamic_cast<BibTeX::Entry*>( element );
        if ( entry != NULL )
        {
            for ( BibTeX::Entry::EntryFields::const_iterator ite = entry->begin(); ite != entry->end(); ite++ )
            {
                BibTeX::EntryField::FieldType fieldType = ( *ite ) ->fieldType();
                int index = completionFieldTypeToIndex( fieldType );

                BibTeX::Value *value = ( *ite ) ->value();
                for ( QValueList<BibTeX::ValueItem*>::const_iterator itv = value->begin(); itv != value->end(); itv++ )
                {
                    BibTeX::ValueItem *item = *itv;
                    if ( !item->isStringKey() )
                    {
                        if ( ( *ite ) ->fieldType() == BibTeX::EntryField::ftAuthor || ( *ite ) ->fieldType() == BibTeX::EntryField::ftEditor )
                        {
                            QStringList elements = QStringList::split( QRegExp( "\\s+(,|and|&)\\s+", FALSE ), item->text() );
                            for ( QStringList::Iterator it = elements.begin(); it != elements.end(); it++ )
                                m_completion[ index ] ->addItem( *it );
                        }
                        else
                            m_completion[ index ] ->addItem( item->text() );
                    }
                }
            }
        }
        else
        {

            BibTeX::Macro * macro = dynamic_cast<BibTeX::Macro*>( element );
            if ( macro != NULL )
            {
                m_completion[ 0 ] ->addItem( macro->key() );
                BibTeX::Value *value = macro->value() ;
                for ( QValueList<BibTeX::ValueItem*>::const_iterator it = value->begin(); it != value->end(); it++ )
                {
                    BibTeX::ValueItem *item = *it;
                    m_completion[ 0 ] ->addItem( item->text() );
                }
            }
        }
    }

    KCompletion *Settings::completion( BibTeX::EntryField::FieldType fieldType )
    {
        return m_completion[ completionFieldTypeToIndex( fieldType ) ];
    }

    int Settings::completionFieldTypeToIndex( BibTeX::EntryField::FieldType fieldType )
    {
        int index = 0;
        if ( fieldType == BibTeX::EntryField::ftEditor )
            fieldType = BibTeX::EntryField::ftAuthor;
        if ( fieldType != BibTeX::EntryField::ftUnknown )
            index = ( int ) ( fieldType - BibTeX::EntryField::ftAbstract ) + 1;
        return index;
    }

    void Settings::restoreDefaultSearchURLs()
    {
        QStringList defaultDescriptions;
        defaultDescriptions.append( "Google Scholar" );
        defaultDescriptions.append( "Google .bib Search" );
        defaultDescriptions.append( "Google Document Search" );
        defaultDescriptions.append( "Google" );
        defaultDescriptions.append( "CiteSeer" );
        defaultDescriptions.append( "PubMed" );
        defaultDescriptions.append( "PubMed Central" );
        defaultDescriptions.append( "DBLP (Computer Science)" );
        defaultDescriptions.append( "citebase" );
        defaultDescriptions.append( "BASE" );
        defaultDescriptions.append( "Forschungsportal.Net" );
        defaultDescriptions.append( "scirus" );
        defaultDescriptions.append( "ScientificCommons" );
        defaultDescriptions.append( "Amatex (US)" );
        defaultDescriptions.append( "SpringerLink" );

        QValueList<SearchURL*> toDelete;
        for ( QValueList<SearchURL*>::ConstIterator it = searchURLs.begin(); it != searchURLs.end(); ++it )
        {
            if ( defaultDescriptions.contains( ( *it ) ->description ) )
            {
                toDelete.append( *it );
            }
        }

        for ( QValueList<SearchURL*>::ConstIterator it = toDelete.begin(); it != toDelete.end(); ++it )
        {
            delete *it;
            searchURLs.remove( *it );
        }

        SearchURL *searchURL = new SearchURL;
        searchURL->description = "Google";
        searchURL->url = "http://www.google.com/search?q=%1&ie=UTF-8&oe=UTF-8";
        searchURLs.append( searchURL );

        searchURL = new SearchURL;
        searchURL->description = "Google Scholar";
        searchURL->url = "http://scholar.google.com/scholar?q=%1&ie=UTF-8&oe=UTF-8";
        searchURLs.append( searchURL );

        searchURL = new SearchURL;
        searchURL->description = "Google .bib Search";
        searchURL->url = "http://www.google.com/search?q=%1%20filetype%3Abib&ie=UTF-8&oe=UTF-8";
        searchURLs.append( searchURL );

        searchURL = new SearchURL;
        searchURL->description = "Google Document Search";
        searchURL->url = "http://www.google.com/search?q=%1%20filetype%3Apdf%20OR%20filetype%3Aps&ie=UTF-8&oe=UTF-8";
        searchURLs.append( searchURL );

        searchURL = new SearchURL;
        searchURL->description = "PubMed";
        searchURL->url = "http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=pubmed&cmd=search&pmfilter_Fulltext=on&pmfilter_Relevance=on&term=%1&search=Find+Articles";
        searchURLs.append( searchURL );

        searchURL = new SearchURL;
        searchURL->description = "PubMed Central";
        searchURL->url = "http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=pubmed&cmd=search&term=%1";
        searchURLs.append( searchURL );

        searchURL = new SearchURL;
        searchURL->description = "Amatex (US)";
        searchURL->url = "http://www.2ndminute.org:8080/amatex/search.do?querry=%1&suchart=kwd&lang=DE";
        searchURLs.append( searchURL );

        searchURL = new SearchURL;
        searchURL->description = "DBLP (Computer Science)";
        searchURL->url = "http://www.informatik.uni-trier.de/ley/dbbin/dblpquery.cgi?title=%1";
        searchURLs.append( searchURL );

        searchURL = new SearchURL;
        searchURL->description = "CiteSeer";
        searchURL->url = "http://citeseer.ist.psu.edu/cis?q=%1&submit=Search+Documents";
        searchURLs.append( searchURL );

        searchURL = new SearchURL;
        searchURL->description = "citebase";
        searchURL->url = "http://www.citebase.org/search?type=metadata&author=&title=%1&publication=&yearfrom=&yearuntil=&order=DESC&rank=paperimpact&submitted=Search";
        searchURLs.append( searchURL );

        searchURL = new SearchURL;
        searchURL->description = "BASE";
        searchURL->url = "http://digital.ub.uni-bielefeld.de/index.php?q=%1&s=free";
        searchURLs.append( searchURL );

        searchURL = new SearchURL;
        searchURL->description = "Forschungsportal.Net";
        searchURL->url = "http://www.forschungsportal.net/fpj/q/?q=%1&pp=5&art=dok&html=1&pdf=1&ps=1&dvi=1";
        searchURLs.append( searchURL );

        searchURL = new SearchURL;
        searchURL->description = "scirus";
        searchURL->url = "http://www.scirus.com/srsapp/search?q=%1&ds=jnl&ds=nom&ds=web&g=s&t=all";
        searchURLs.append( searchURL );

        searchURL = new SearchURL;
        searchURL->description = "ScientificCommons";
        searchURL->url = "http://en.scientificcommons.org/#search_string=%1";
        searchURLs.append( searchURL );

        searchURL = new SearchURL;
        searchURL->description = "SpringerLink";
        searchURL->url = "http://www.springerlink.com/content/?k=%1";
        searchURLs.append( searchURL );
    }

    QString Settings::fieldTypeToI18NString( const BibTeX::EntryField::FieldType fieldType )
    {
        switch ( fieldType )
        {
        case BibTeX::EntryField::ftAbstract:
            return QString( i18n( "Abstract" ) );
        case BibTeX::EntryField::ftAddress:
            return QString( i18n( "Address" ) );
        case BibTeX::EntryField::ftAnnote:
            return QString( i18n( "Annote" ) );
        case BibTeX::EntryField::ftAuthor:
            return QString( i18n( "Author" ) );
        case BibTeX::EntryField::ftBookTitle:
            return QString( i18n( "Book Title" ) );
        case BibTeX::EntryField::ftChapter:
            return QString( i18n( "Chapter" ) );
        case BibTeX::EntryField::ftCrossRef:
            return QString( i18n( "Crossref" ) );
        case BibTeX::EntryField::ftDoi:
            return QString( i18n( "DOI" ) );
        case BibTeX::EntryField::ftEdition:
            return QString( i18n( "Edition" ) );
        case BibTeX::EntryField::ftEditor:
            return QString( i18n( "Editor" ) );
        case BibTeX::EntryField::ftHowPublished:
            return QString( i18n( "How Published" ) );
        case BibTeX::EntryField::ftInstitution:
            return QString( i18n( "Institution" ) );
        case BibTeX::EntryField::ftISBN:
            return QString( i18n( "ISBN" ) );
        case BibTeX::EntryField::ftISSN:
            return QString( i18n( "ISSN" ) );
        case BibTeX::EntryField::ftJournal:
            return QString( i18n( "Journal" ) );
        case BibTeX::EntryField::ftKey:
            return QString( i18n( "Key" ) );
        case BibTeX::EntryField::ftLocalFile:
            return QString( i18n( "Local File" ) );
        case BibTeX::EntryField::ftLocation:
            return QString( i18n( "Location" ) );
        case BibTeX::EntryField::ftMonth:
            return QString( i18n( "Month" ) );
        case BibTeX::EntryField::ftNote:
            return QString( i18n( "Note" ) );
        case BibTeX::EntryField::ftNumber:
            return QString( i18n( "Number" ) );
        case BibTeX::EntryField::ftOrganization:
            return QString( i18n( "Organization" ) );
        case BibTeX::EntryField::ftPages:
            return QString( i18n( "Pages" ) );
        case BibTeX::EntryField::ftPublisher:
            return QString( i18n( "Publisher" ) );
        case BibTeX::EntryField::ftSeries:
            return QString( i18n( "Series" ) );
        case BibTeX::EntryField::ftSchool:
            return QString( i18n( "School" ) );
        case BibTeX::EntryField::ftTitle:
            return QString( i18n( "Title" ) );
        case BibTeX::EntryField::ftType:
            return QString( i18n( "Type" ) );
        case BibTeX::EntryField::ftURL:
            return QString( i18n( "URL" ) );
        case BibTeX::EntryField::ftVolume:
            return QString( i18n( "Volume" ) );
        case BibTeX::EntryField::ftYear:
            return QString( i18n( "Year" ) );
        default:
            return QString( i18n( "Unknown" ) );
        }
    }

    KURL Settings::doiURL( const QString& doiText )
    {
        KURL result( doiText );
        if ( result.isValid() )
            return result;

        result = KURL( QString( "http://dx.doi.org/%1" ).arg( doiText ) );
        if ( result.isValid() )
            return result;

        return KURL();
    }

}


Generated by  Doxygen 1.6.0   Back to index