1
/***************************************************************************
2
* Copyright (C) 2004-2009 by Thomas Fischer *
3
* fischer@unix-ag.uni-kl.de *
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. *
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. *
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
***************************************************************************/
21
#include <qstringlist.h>
27
#include <fileexporterbibtex.h>
28
#include "fileexporterpdf.h"
33
FileExporterPDF::FileExporterPDF( bool embedFiles )
34
: FileExporterToolchain(), m_latexLanguage( "english" ), m_latexBibStyle( "plain" ), m_embedFiles( embedFiles )
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" );
41
FileExporterPDF::~FileExporterPDF()
46
bool FileExporterPDF::save( QIODevice* iodevice, const File* bibtexfile, QStringList *errorLog )
50
m_embeddedFileList.clear();
53
m_embeddedFileList.append( QString( "%1|%2" ).arg( "BibTeX source" ).arg( bibTeXFilename ) );
54
fillEmbeddedFileList( bibtexfile );
57
QFile bibtexFile( bibTeXFilename );
58
if ( bibtexFile.open( IO_WriteOnly ) )
60
FileExporter * bibtexExporter = new FileExporterBibTeX();
61
result = bibtexExporter->save( &bibtexFile, bibtexfile, errorLog );
63
delete bibtexExporter;
67
result = generatePDF( iodevice, errorLog );
73
bool FileExporterPDF::save( QIODevice* iodevice, const Element* element, QStringList *errorLog )
77
m_embeddedFileList.clear();
79
fillEmbeddedFileList( element );
81
QFile bibtexFile( bibTeXFilename );
82
if ( bibtexFile.open( IO_WriteOnly ) )
84
FileExporter * bibtexExporter = new FileExporterBibTeX();
85
result = bibtexExporter->save( &bibtexFile, element, errorLog );
87
delete bibtexExporter;
91
result = generatePDF( iodevice, errorLog );
97
void FileExporterPDF::setLaTeXLanguage( const QString& language )
99
m_latexLanguage = language;
102
void FileExporterPDF::setLaTeXBibliographyStyle( const QString& bibStyle )
104
m_latexBibStyle = bibStyle;
107
void FileExporterPDF::setDocumentSearchPaths( const QStringList& searchPaths )
109
m_searchPaths = searchPaths;
112
bool FileExporterPDF::generatePDF( QIODevice* iodevice, QStringList *errorLog )
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" );
116
if ( writeLatexFile( laTeXFilename ) && runProcesses( cmdLines, errorLog ) && writeFileToIODevice( outputFilename, iodevice ) )
122
bool FileExporterPDF::writeLatexFile( const QString &filename )
124
QFile latexFile( filename );
125
if ( latexFile.open( IO_WriteOnly ) )
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";
142
ts << "\\usepackage{embedfile}\n";
143
ts << "\\bibliographystyle{" << m_latexBibStyle << "}\n";
144
ts << "\\begin{document}\n";
147
for ( QStringList::ConstIterator it = m_embeddedFileList.begin(); it != m_embeddedFileList.end(); ++it )
149
QStringList param = QStringList::split( "|", *it );
150
QFile file( param[1] );
152
ts << "\\embedfile[desc={" << param[0] << "}]{" << param[1] << "}\n";
155
ts << "\\nocite{*}\n";
156
ts << "\\bibliography{bibtex-to-pdf}\n";
157
ts << "\\end{document}\n";
165
void FileExporterPDF::fillEmbeddedFileList( const File* bibtexfile )
167
for ( BibTeX::File::ElementList::ConstIterator it = bibtexfile->constBegin(); it != bibtexfile->constEnd(); ++it )
168
fillEmbeddedFileList( *it );
171
void FileExporterPDF::fillEmbeddedFileList( const Element* element )
173
const Entry *entry = dynamic_cast<const Entry*>( element );
176
QString id = entry->id();
177
QStringList urls = entry->urls();
178
for ( QStringList::Iterator it = urls.begin(); it != urls.end(); ++it )
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() ) );
184
for ( QStringList::Iterator path_it = m_searchPaths.begin(); path_it != m_searchPaths.end(); ++path_it )
186
url = QUrl( QString( *path_it ).append( "/" ).append( *it ) );
187
if ( url.isValid() && url.isLocalFile() && !( *it ).endsWith( "/" ) && QFile( url.path() ).exists() )
189
m_embeddedFileList.append( QString( "%1|%2" ).arg( id ).arg( url.path() ) );