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

« back to all changes in this revision

Viewing changes to src/libkbibtexio/fileexporterblg.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-2010 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>
 
21
#include <QStringList>
 
22
#include <QUrl>
 
23
#include <QTextStream>
 
24
 
 
25
#include <KDebug>
 
26
#include <KLocale>
 
27
 
 
28
#include <element.h>
 
29
#include <entry.h>
 
30
#include <fileexporterbibtex.h>
 
31
#include "fileexporterblg.h"
 
32
 
 
33
FileExporterBLG::FileExporterBLG()
 
34
        : FileExporterToolchain(), m_latexLanguage("english"), m_latexBibStyle("plain")
 
35
{
 
36
    m_laTeXFilename = tempDir.name() + QLatin1String("/bibtex-to-blg.tex");
 
37
    m_bibTeXFilename = tempDir.name() + QLatin1String("/bibtex-to-blg.bib");
 
38
}
 
39
 
 
40
FileExporterBLG::~FileExporterBLG()
 
41
{
 
42
    // nothing
 
43
}
 
44
 
 
45
bool FileExporterBLG::save(QIODevice*ioDevice, const File* bibtexfile, QStringList *errorLog)
 
46
{
 
47
    bool result = false;
 
48
 
 
49
    QFile output(m_bibTeXFilename);
 
50
    if (output.open(QIODevice::WriteOnly)) {
 
51
        FileExporter* bibtexExporter = new FileExporterBibTeX();
 
52
        result = bibtexExporter->save(&output, bibtexfile, errorLog);
 
53
        bibtexExporter->save(ioDevice, bibtexfile, NULL);
 
54
        output.close();
 
55
        delete bibtexExporter;
 
56
    }
 
57
 
 
58
    if (result)
 
59
        result = generateBLG(errorLog);
 
60
 
 
61
    return result;
 
62
}
 
63
 
 
64
bool FileExporterBLG::save(QIODevice *ioDevice, const Element* element, QStringList *errorLog)
 
65
{
 
66
    bool result = false;
 
67
 
 
68
    QFile output(m_bibTeXFilename);
 
69
    if (output.open(QIODevice::WriteOnly)) {
 
70
        FileExporter * bibtexExporter = new FileExporterBibTeX();
 
71
        result = bibtexExporter->save(&output, element, errorLog);
 
72
        bibtexExporter->save(ioDevice, element, NULL);
 
73
        output.close();
 
74
        delete bibtexExporter;
 
75
    }
 
76
 
 
77
    if (result)
 
78
        result = generateBLG(errorLog);
 
79
 
 
80
    return result;
 
81
}
 
82
 
 
83
void FileExporterBLG::setLaTeXLanguage(const QString& language)
 
84
{
 
85
    m_latexLanguage = language;
 
86
}
 
87
 
 
88
void FileExporterBLG::setLaTeXBibliographyStyle(const QString& bibStyle)
 
89
{
 
90
    m_latexBibStyle = bibStyle;
 
91
}
 
92
 
 
93
bool FileExporterBLG::generateBLG(QStringList *errorLog)
 
94
{
 
95
    QStringList cmdLines = QStringList() << QLatin1String("pdflatex -halt-on-error bibtex-to-blg.tex") << QLatin1String("bibtex bibtex-to-blg");
 
96
 
 
97
    if (writeLatexFile(m_laTeXFilename) && runProcesses(cmdLines, errorLog))
 
98
        return true;
 
99
    else {
 
100
        kWarning() << "Generating BLG failed";
 
101
        return false;
 
102
    }
 
103
}
 
104
 
 
105
bool FileExporterBLG::writeLatexFile(const QString &filename)
 
106
{
 
107
    QFile latexFile(filename);
 
108
    if (latexFile.open(QIODevice::WriteOnly)) {
 
109
        QTextStream ts(&latexFile);
 
110
        ts.setCodec("UTF-8");
 
111
        ts << "\\documentclass{article}\n";
 
112
        ts << "\\usepackage[T1]{fontenc}\n";
 
113
        ts << "\\usepackage[utf8]{inputenc}\n";
 
114
        ts << "\\usepackage[" << m_latexLanguage << "]{babel}\n";
 
115
        if (kpsewhich("hyperref.sty"))
 
116
            ts << "\\usepackage[pdfproducer={KBibTeX: http://home.gna.org/kbibtex/},pdftex]{hyperref}\n";
 
117
        else if (kpsewhich("url.sty"))
 
118
            ts << "\\usepackage{url}\n";
 
119
        if (m_latexBibStyle.startsWith("apacite") && kpsewhich("apacite.sty"))
 
120
            ts << "\\usepackage[bibnewpage]{apacite}\n";
 
121
        ts << "\\bibliographystyle{" << m_latexBibStyle << "}\n";
 
122
        ts << "\\begin{document}\n";
 
123
        ts << "\\nocite{*}\n";
 
124
        ts << "\\bibliography{bibtex-to-blg}\n";
 
125
        ts << "\\end{document}\n";
 
126
        latexFile.close();
 
127
        return true;
 
128
    } else
 
129
        return false;
 
130
}