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

« back to all changes in this revision

Viewing changes to src/fileexporterrtf.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
 
 
23
 
#include <settings.h>
24
 
#include <element.h>
25
 
#include <fileexporterbibtex.h>
26
 
#include "fileexporterrtf.h"
27
 
 
28
 
namespace BibTeX
29
 
{
30
 
 
31
 
    FileExporterRTF::FileExporterRTF() : FileExporterToolchain(), m_latexLanguage( "english" ), m_latexBibStyle( "plain" )
32
 
    {
33
 
        laTeXFilename = QString( workingDir ).append( "/bibtex-to-rtf.tex" );
34
 
        bibTeXFilename = QString( workingDir ).append( "/bibtex-to-rtf.bib" );
35
 
        outputFilename = QString( workingDir ).append( "/bibtex-to-rtf.rtf" );
36
 
    }
37
 
 
38
 
    FileExporterRTF::~FileExporterRTF()
39
 
    {
40
 
        // nothing
41
 
    }
42
 
 
43
 
    bool FileExporterRTF::save( QIODevice* iodevice, const File* bibtexfile, QStringList *errorLog )
44
 
    {
45
 
        m_mutex.lock();
46
 
        bool result = FALSE;
47
 
 
48
 
        QFile bibtexFile( bibTeXFilename );
49
 
        if ( bibtexFile.open( IO_WriteOnly ) )
50
 
        {
51
 
            FileExporter * bibtexExporter = new FileExporterBibTeX();
52
 
            result = bibtexExporter->save( &bibtexFile, bibtexfile, errorLog );
53
 
            bibtexFile.close();
54
 
            delete bibtexExporter;
55
 
        }
56
 
 
57
 
        if ( result )
58
 
            result = generateRTF( iodevice, errorLog );
59
 
 
60
 
        m_mutex.unlock();
61
 
        return result;
62
 
    }
63
 
 
64
 
    bool FileExporterRTF::save( QIODevice* iodevice, const Element* element, QStringList *errorLog )
65
 
    {
66
 
        m_mutex.lock();
67
 
        bool result = FALSE;
68
 
 
69
 
        QFile bibtexFile( bibTeXFilename );
70
 
        if ( bibtexFile.open( IO_WriteOnly ) )
71
 
        {
72
 
            FileExporter * bibtexExporter = new FileExporterBibTeX();
73
 
            result = bibtexExporter->save( &bibtexFile, element, errorLog );
74
 
            bibtexFile.close();
75
 
            delete bibtexExporter;
76
 
        }
77
 
 
78
 
        if ( result )
79
 
            result = generateRTF( iodevice, errorLog );
80
 
 
81
 
        m_mutex.unlock();
82
 
        return result;
83
 
    }
84
 
 
85
 
    void FileExporterRTF::setLaTeXLanguage( const QString& language )
86
 
    {
87
 
        m_latexLanguage = language;
88
 
    }
89
 
 
90
 
    void FileExporterRTF::setLaTeXBibliographyStyle( const QString& bibStyle )
91
 
    {
92
 
        m_latexBibStyle = bibStyle;
93
 
    }
94
 
 
95
 
    bool FileExporterRTF::generateRTF( QIODevice* iodevice, QStringList *errorLog )
96
 
    {
97
 
        QStringList cmdLines = QStringList::split( '|', "latex bibtex-to-rtf.tex|bibtex bibtex-to-rtf|latex bibtex-to-rtf.tex|latex2rtf bibtex-to-rtf.tex" );
98
 
 
99
 
        if ( writeLatexFile( laTeXFilename ) && runProcesses( cmdLines, errorLog ) && writeFileToIODevice( outputFilename, iodevice ) )
100
 
            return TRUE;
101
 
        else
102
 
            return FALSE;
103
 
    }
104
 
 
105
 
    bool FileExporterRTF::writeLatexFile( const QString &filename )
106
 
    {
107
 
        QFile latexFile( filename );
108
 
        if ( latexFile.open( IO_WriteOnly ) )
109
 
        {
110
 
            QTextStream ts( &latexFile );
111
 
            ts.setEncoding( QTextStream::UnicodeUTF8 );
112
 
            ts << "\\documentclass{article}\n";
113
 
            if ( kpsewhich( "t1enc.dfu" ) )
114
 
                ts << "\\usepackage[T1]{fontenc}\n";
115
 
            if ( kpsewhich( "babel.sty" ) )
116
 
                ts << "\\usepackage[" << m_latexLanguage << "]{babel}\n";
117
 
            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-rtf}\n";
125
 
            ts << "\\end{document}\n";
126
 
            latexFile.close();
127
 
            return TRUE;
128
 
        }
129
 
        else
130
 
            return FALSE;
131
 
 
132
 
    }
133
 
 
134
 
}