~ubuntu-branches/ubuntu/precise/kbibtex/precise

« back to all changes in this revision

Viewing changes to src/fileexporterpdf.cpp

  • Committer: Package Import Robot
  • Author(s): Michael Hanke
  • Date: 2011-07-18 09:29:48 UTC
  • mfrom: (1.1.6) (2.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20110718092948-ksxjmg7kdfamolmg
Tags: 0.3-1
* First upstream release for KDE4 (Closes: #634255). A number of search
  engines are still missing, in comparison to the 0.2 series.
* Bumped Standards-Version to 3.9.2, no changes necessary.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
*   Copyright (C) 2004-2009 by Thomas Fischer                             *
3
 
*   fischer@unix-ag.uni-kl.de                                             *
4
 
*                                                                         *
5
 
*   This program is free software; you can redistribute it and/or modify  *
6
 
*   it under the terms of the GNU General Public License as published by  *
7
 
*   the Free Software Foundation; either version 2 of the License, or     *
8
 
*   (at your option) any later version.                                   *
9
 
*                                                                         *
10
 
*   This program is distributed in the hope that it will be useful,       *
11
 
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12
 
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13
 
*   GNU General Public License for more details.                          *
14
 
*                                                                         *
15
 
*   You should have received a copy of the GNU General Public License     *
16
 
*   along with this program; if not, write to the                         *
17
 
*   Free Software Foundation, Inc.,                                       *
18
 
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19
 
***************************************************************************/
20
 
#include <qfile.h>
21
 
#include <qstringlist.h>
22
 
#include <qurl.h>
23
 
 
24
 
#include <settings.h>
25
 
#include <element.h>
26
 
#include <entry.h>
27
 
#include <fileexporterbibtex.h>
28
 
#include "fileexporterpdf.h"
29
 
 
30
 
namespace BibTeX
31
 
{
32
 
 
33
 
    FileExporterPDF::FileExporterPDF( bool embedFiles )
34
 
            : FileExporterToolchain(), m_latexLanguage( "english" ), m_latexBibStyle( "plain" ), m_embedFiles( embedFiles )
35
 
    {
36
 
        laTeXFilename = QString( workingDir ).append( "/bibtex-to-pdf.tex" );
37
 
        bibTeXFilename = QString( workingDir ).append( "/bibtex-to-pdf.bib" );
38
 
        outputFilename = QString( workingDir ).append( "/bibtex-to-pdf.pdf" );
39
 
    }
40
 
 
41
 
    FileExporterPDF::~FileExporterPDF()
42
 
    {
43
 
        // nothing
44
 
    }
45
 
 
46
 
    bool FileExporterPDF::save( QIODevice* iodevice, const File* bibtexfile, QStringList *errorLog )
47
 
    {
48
 
        m_mutex.lock();
49
 
        bool result = FALSE;
50
 
        m_embeddedFileList.clear();
51
 
        if ( m_embedFiles )
52
 
        {
53
 
            m_embeddedFileList.append( QString( "%1|%2" ).arg( "BibTeX source" ).arg( bibTeXFilename ) );
54
 
            fillEmbeddedFileList( bibtexfile );
55
 
        }
56
 
 
57
 
        QFile bibtexFile( bibTeXFilename );
58
 
        if ( bibtexFile.open( IO_WriteOnly ) )
59
 
        {
60
 
            FileExporter * bibtexExporter = new FileExporterBibTeX();
61
 
            result = bibtexExporter->save( &bibtexFile, bibtexfile, errorLog );
62
 
            bibtexFile.close();
63
 
            delete bibtexExporter;
64
 
        }
65
 
 
66
 
        if ( result )
67
 
            result = generatePDF( iodevice, errorLog );
68
 
 
69
 
        m_mutex.unlock();
70
 
        return result;
71
 
    }
72
 
 
73
 
    bool FileExporterPDF::save( QIODevice* iodevice, const Element* element, QStringList *errorLog )
74
 
    {
75
 
        m_mutex.lock();
76
 
        bool result = FALSE;
77
 
        m_embeddedFileList.clear();
78
 
        if ( m_embedFiles )
79
 
            fillEmbeddedFileList( element );
80
 
 
81
 
        QFile bibtexFile( bibTeXFilename );
82
 
        if ( bibtexFile.open( IO_WriteOnly ) )
83
 
        {
84
 
            FileExporter * bibtexExporter = new FileExporterBibTeX();
85
 
            result = bibtexExporter->save( &bibtexFile, element, errorLog );
86
 
            bibtexFile.close();
87
 
            delete bibtexExporter;
88
 
        }
89
 
 
90
 
        if ( result )
91
 
            result = generatePDF( iodevice, errorLog );
92
 
 
93
 
        m_mutex.unlock();
94
 
        return result;
95
 
    }
96
 
 
97
 
    void FileExporterPDF::setLaTeXLanguage( const QString& language )
98
 
    {
99
 
        m_latexLanguage = language;
100
 
    }
101
 
 
102
 
    void FileExporterPDF::setLaTeXBibliographyStyle( const QString& bibStyle )
103
 
    {
104
 
        m_latexBibStyle = bibStyle;
105
 
    }
106
 
 
107
 
    void FileExporterPDF::setDocumentSearchPaths( const QStringList& searchPaths )
108
 
    {
109
 
        m_searchPaths = searchPaths;
110
 
    }
111
 
 
112
 
    bool FileExporterPDF::generatePDF( QIODevice* iodevice, QStringList *errorLog )
113
 
    {
114
 
        QStringList cmdLines = QStringList::split( '|', "pdflatex -halt-on-error bibtex-to-pdf.tex|bibtex bibtex-to-pdf|pdflatex -halt-on-error bibtex-to-pdf.tex|pdflatex -halt-on-error bibtex-to-pdf.tex" );
115
 
 
116
 
        if ( writeLatexFile( laTeXFilename ) && runProcesses( cmdLines, errorLog ) && writeFileToIODevice( outputFilename, iodevice ) )
117
 
            return TRUE;
118
 
        else
119
 
            return FALSE;
120
 
    }
121
 
 
122
 
    bool FileExporterPDF::writeLatexFile( const QString &filename )
123
 
    {
124
 
        QFile latexFile( filename );
125
 
        if ( latexFile.open( IO_WriteOnly ) )
126
 
        {
127
 
            m_embedFiles &= kpsewhich( "embedfile.sty" );
128
 
            QTextStream ts( &latexFile );
129
 
            ts.setEncoding( QTextStream::UnicodeUTF8 );
130
 
            ts << "\\documentclass{article}\n";
131
 
            if ( kpsewhich( "t1enc.dfu" ) )
132
 
                ts << "\\usepackage[T1]{fontenc}\n";
133
 
            if ( kpsewhich( "babel.sty" ) )
134
 
                ts << "\\usepackage[" << m_latexLanguage << "]{babel}\n";
135
 
            if ( kpsewhich( "hyperref.sty" ) )
136
 
                ts << "\\usepackage[pdfproducer={KBibTeX: http://www.t-fischer.net/kbibtex/},pdftex]{hyperref}\n";
137
 
            else if ( kpsewhich( "url.sty" ) )
138
 
                ts << "\\usepackage{url}\n";
139
 
            if ( m_latexBibStyle.startsWith( "apacite" ) && kpsewhich( "apacite.sty" ) )
140
 
                ts << "\\usepackage[bibnewpage]{apacite}\n";
141
 
            if ( m_embedFiles )
142
 
                ts << "\\usepackage{embedfile}\n";
143
 
            ts << "\\bibliographystyle{" << m_latexBibStyle << "}\n";
144
 
            ts << "\\begin{document}\n";
145
 
 
146
 
            if ( m_embedFiles )
147
 
                for ( QStringList::ConstIterator it = m_embeddedFileList.begin(); it != m_embeddedFileList.end(); ++it )
148
 
                {
149
 
                    QStringList param = QStringList::split( "|", *it );
150
 
                    QFile file( param[1] );
151
 
                    if ( file.exists() )
152
 
                        ts << "\\embedfile[desc={" << param[0] << "}]{" << param[1] << "}\n";
153
 
                }
154
 
 
155
 
            ts << "\\nocite{*}\n";
156
 
            ts << "\\bibliography{bibtex-to-pdf}\n";
157
 
            ts << "\\end{document}\n";
158
 
            latexFile.close();
159
 
            return TRUE;
160
 
        }
161
 
        else
162
 
            return FALSE;
163
 
    }
164
 
 
165
 
    void FileExporterPDF::fillEmbeddedFileList( const File* bibtexfile )
166
 
    {
167
 
        for ( BibTeX::File::ElementList::ConstIterator it = bibtexfile->constBegin(); it != bibtexfile->constEnd(); ++it )
168
 
            fillEmbeddedFileList( *it );
169
 
    }
170
 
 
171
 
    void FileExporterPDF::fillEmbeddedFileList( const Element* element )
172
 
    {
173
 
        const Entry *entry = dynamic_cast<const Entry*>( element );
174
 
        if ( entry != NULL )
175
 
        {
176
 
            QString id = entry->id();
177
 
            QStringList urls = entry->urls();
178
 
            for ( QStringList::Iterator it = urls.begin(); it != urls.end(); ++it )
179
 
            {
180
 
                QUrl url = QUrl( *it );
181
 
                if ( url.isValid() && url.isLocalFile() && !( *it ).endsWith( "/" ) && QFile( url.path() ).exists() )
182
 
                    m_embeddedFileList.append( QString( "%1|%2" ).arg( id ).arg( url.path() ) );
183
 
                else
184
 
                    for ( QStringList::Iterator path_it = m_searchPaths.begin(); path_it != m_searchPaths.end(); ++path_it )
185
 
                    {
186
 
                        url = QUrl( QString( *path_it ).append( "/" ).append( *it ) );
187
 
                        if ( url.isValid() && url.isLocalFile() && !( *it ).endsWith( "/" ) && QFile( url.path() ).exists() )
188
 
                        {
189
 
                            m_embeddedFileList.append( QString( "%1|%2" ).arg( id ).arg( url.path() ) );
190
 
                            break;
191
 
                        }
192
 
                    }
193
 
            }
194
 
        }
195
 
    }
196
 
 
197
 
}