~ubuntu-branches/ubuntu/raring/kbibtex/raring

« back to all changes in this revision

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