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

« back to all changes in this revision

Viewing changes to src/fileexporterbibutils.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 <qdir.h>
22
 
#include <qstringlist.h>
23
 
#include <qapplication.h>
24
 
#include <qbuffer.h>
25
 
#include <qprocess.h>
26
 
#include <qwaitcondition.h>
27
 
 
28
 
#include <fileexporterbibtex.h>
29
 
#include "fileexporterbibutils.h"
30
 
 
31
 
namespace BibTeX
32
 
{
33
 
 
34
 
    FileExporterBibUtils::FileExporterBibUtils( BibTeX::File::FileFormat outputFormat )
35
 
            : FileExporter(), m_outputFormat( outputFormat ), m_bibTeXExporter( new FileExporterBibTeX() )
36
 
    {
37
 
        m_bibTeXExporter->setEncoding( "utf-8" );
38
 
        m_processBuffer = new QBuffer();
39
 
    }
40
 
 
41
 
    FileExporterBibUtils::~FileExporterBibUtils()
42
 
    {
43
 
        delete m_processBuffer;
44
 
        delete m_bibTeXExporter;
45
 
    }
46
 
 
47
 
    bool FileExporterBibUtils::save( QIODevice* iodevice, const Element* element, QStringList* errorLog )
48
 
    {
49
 
        m_cancelFlag = false;
50
 
        QBuffer bibBuffer;
51
 
        if ( !toBuffer( element, &bibBuffer, errorLog ) )
52
 
            return false;
53
 
        if ( !bufferToXMLbuffer( &bibBuffer ) || m_cancelFlag )
54
 
            return false;
55
 
        return !m_cancelFlag && xmlBufferToIOdevice( iodevice );
56
 
    }
57
 
 
58
 
    bool FileExporterBibUtils::save( QIODevice* iodevice, const File* bibtexfile, QStringList* errorLog )
59
 
    {
60
 
        emit progress( 0, 3 );
61
 
        m_cancelFlag = false;
62
 
        QBuffer bibBuffer;
63
 
        if ( !toBuffer( bibtexfile, &bibBuffer, errorLog ) )
64
 
            return false;
65
 
        emit progress( 1, 3 );
66
 
        if ( m_cancelFlag || !bufferToXMLbuffer( &bibBuffer ) )
67
 
            return false;
68
 
        emit progress( 2, 3 );
69
 
        if ( m_cancelFlag || !xmlBufferToIOdevice( iodevice ) )
70
 
            return false;
71
 
        emit progress( 3, 3 );
72
 
        return !m_cancelFlag;
73
 
    }
74
 
 
75
 
    void FileExporterBibUtils::cancel()
76
 
    {
77
 
        m_bibTeXExporter->cancel();
78
 
        m_cancelFlag = true;
79
 
    }
80
 
 
81
 
    bool FileExporterBibUtils::toBuffer( const File *bibFile, QBuffer *buffer, QStringList* errorLog )
82
 
    {
83
 
        buffer->open( IO_WriteOnly );
84
 
        bool result = m_bibTeXExporter->save( buffer, bibFile, errorLog );
85
 
        buffer->close();
86
 
        return result;
87
 
    }
88
 
 
89
 
    bool FileExporterBibUtils::toBuffer( const Element *bibElement, QBuffer *buffer, QStringList* errorLog )
90
 
    {
91
 
        buffer->open( IO_WriteOnly );
92
 
        bool result = m_bibTeXExporter->save( buffer, bibElement, errorLog );
93
 
        buffer->close();
94
 
        return result;
95
 
    }
96
 
 
97
 
    bool FileExporterBibUtils::bufferToXMLbuffer( QBuffer *bibBuffer )
98
 
    {
99
 
        QWaitCondition wc;
100
 
 
101
 
        m_processBuffer->open( IO_WriteOnly );
102
 
        m_waiting = true;
103
 
        m_process = new QProcess( QStringList::split( ' ', "bib2xml -i utf8" ) );
104
 
        connect( m_process, SIGNAL( processExited() ), this, SLOT( wakeUp() ) );
105
 
        connect( m_process, SIGNAL( readyReadStdout() ), this, SLOT( slotReadyStdout() ) );
106
 
        connect( m_process, SIGNAL( readyReadStderr() ), this, SLOT( slotReadyStderr() ) );
107
 
 
108
 
        m_process->start();
109
 
        if ( m_process->isRunning() )
110
 
        {
111
 
            bibBuffer->open( IO_ReadOnly );
112
 
            m_process->writeToStdin( bibBuffer->readAll() );
113
 
            qApp->processEvents();
114
 
            m_process->closeStdin();
115
 
            bibBuffer->close();
116
 
 
117
 
            int nothingHappens = 20;
118
 
            while ( m_waiting )
119
 
            {
120
 
                wc.wait( 250 );
121
 
                qApp->processEvents();
122
 
                --nothingHappens;
123
 
            }
124
 
 
125
 
            if ( nothingHappens <= 0 )
126
 
                m_process->kill();
127
 
 
128
 
            if ( !m_process->normalExit() )
129
 
            {
130
 
                qDebug( "%s did not exit in a clean fashion", m_process->arguments()[0].latin1() );
131
 
                delete m_process;
132
 
                return false;
133
 
            }
134
 
        }
135
 
        else
136
 
        {
137
 
            qDebug( "%s did not start", m_process->arguments()[0].latin1() );
138
 
            delete m_process;
139
 
            return false;
140
 
        }
141
 
 
142
 
        m_processBuffer->close();
143
 
 
144
 
        delete m_process;
145
 
        return true;
146
 
    }
147
 
 
148
 
    bool FileExporterBibUtils::xmlBufferToIOdevice( QIODevice *iodevice )
149
 
    {
150
 
        QWaitCondition wc;
151
 
        m_waiting = true;
152
 
 
153
 
        m_process = NULL;
154
 
        switch ( m_outputFormat )
155
 
        {
156
 
        case BibTeX::File::formatISI:
157
 
            m_process = new QProcess( QStringList::split( ' ', "xml2isi" ) );
158
 
            break;
159
 
        case BibTeX::File::formatWordBib:
160
 
            m_process = new QProcess( QStringList::split( ' ', "xml2wordbib" ) );
161
 
            break;
162
 
        case BibTeX::File::formatAds:
163
 
            m_process = new QProcess( QStringList::split( ' ', "xml2ads" ) );
164
 
            break;
165
 
        case BibTeX::File::formatEndNote:
166
 
            m_process = new QProcess( QStringList::split( ' ', "xml2end" ) );
167
 
            break;
168
 
        case BibTeX::File::formatRIS:
169
 
            m_process = new QProcess( QStringList::split( ' ', "xml2ris" ) );
170
 
            break;
171
 
        case BibTeX::File::formatMODS:
172
 
            /* m_process = NULL; */
173
 
            break;
174
 
        default:
175
 
            qDebug( "Cannot handle output format %i", m_outputFormat );
176
 
            return false;
177
 
        }
178
 
 
179
 
        if ( m_process != NULL )
180
 
        {
181
 
            connect( m_process, SIGNAL( processExited() ), this, SLOT( wakeUp() ) );
182
 
            connect( m_process, SIGNAL( readyReadStdout() ), this, SLOT( slotReadyStdout() ) );
183
 
            connect( m_process, SIGNAL( readyReadStderr() ), this, SLOT( slotReadyStderr() ) );
184
 
 
185
 
            if ( m_process->start() )
186
 
            {
187
 
                QBuffer *tempBuffer = m_processBuffer;
188
 
                m_processBuffer = new QBuffer();
189
 
 
190
 
                tempBuffer->open( IO_ReadOnly );
191
 
                m_process->writeToStdin( tempBuffer->readAll() );
192
 
                qApp->processEvents();
193
 
                m_process->closeStdin();
194
 
                tempBuffer->close();
195
 
 
196
 
                m_processBuffer->open( IO_WriteOnly );
197
 
                int nothingHappens = 20;
198
 
                while ( m_waiting )
199
 
                {
200
 
                    wc.wait( 250 );
201
 
                    qApp->processEvents();
202
 
                    --nothingHappens;
203
 
                }
204
 
                m_processBuffer->close();
205
 
 
206
 
                delete tempBuffer;
207
 
 
208
 
                if ( nothingHappens <= 0 )
209
 
                    m_process->kill();
210
 
 
211
 
                if ( ! m_process->normalExit() )
212
 
                {
213
 
                    delete m_process;
214
 
                    return false;
215
 
                }
216
 
 
217
 
                m_processBuffer->open( IO_ReadOnly );
218
 
                iodevice->writeBlock( m_processBuffer->buffer() );
219
 
                m_processBuffer->close();
220
 
 
221
 
                delete m_process;
222
 
            }
223
 
            else
224
 
            {
225
 
                qDebug( "%s did not start", m_process->arguments()[0].latin1() );
226
 
                delete m_process;
227
 
                return false;
228
 
            }
229
 
        }
230
 
        else
231
 
        {
232
 
            m_processBuffer->open( IO_ReadOnly );
233
 
            iodevice->writeBlock( m_processBuffer->buffer() );
234
 
            m_processBuffer->close();
235
 
        }
236
 
 
237
 
        return true;
238
 
    }
239
 
 
240
 
    void FileExporterBibUtils::wakeUp()
241
 
    {
242
 
        m_waiting = false;
243
 
    }
244
 
 
245
 
    void FileExporterBibUtils::slotReadyStdout()
246
 
    {
247
 
        m_processBuffer->writeBlock( m_process->readStdout() );
248
 
    }
249
 
 
250
 
    void FileExporterBibUtils::slotReadyStderr()
251
 
    {
252
 
        QByteArray ba = m_process->readStderr();
253
 
        QTextStream bats( ba, IO_ReadOnly );
254
 
        bats.setEncoding( QTextStream::UnicodeUTF8 );
255
 
        qDebug( "%s", bats.read().latin1() );
256
 
    }
257
 
 
258
 
}
259
 
#include "fileexporterbibutils.moc"