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
***************************************************************************/
22
#include <qstringlist.h>
23
#include <qapplication.h>
26
#include <qwaitcondition.h>
28
#include <fileexporterbibtex.h>
29
#include "fileexporterbibutils.h"
34
FileExporterBibUtils::FileExporterBibUtils( BibTeX::File::FileFormat outputFormat )
35
: FileExporter(), m_outputFormat( outputFormat ), m_bibTeXExporter( new FileExporterBibTeX() )
37
m_bibTeXExporter->setEncoding( "utf-8" );
38
m_processBuffer = new QBuffer();
41
FileExporterBibUtils::~FileExporterBibUtils()
43
delete m_processBuffer;
44
delete m_bibTeXExporter;
47
bool FileExporterBibUtils::save( QIODevice* iodevice, const Element* element, QStringList* errorLog )
51
if ( !toBuffer( element, &bibBuffer, errorLog ) )
53
if ( !bufferToXMLbuffer( &bibBuffer ) || m_cancelFlag )
55
return !m_cancelFlag && xmlBufferToIOdevice( iodevice );
58
bool FileExporterBibUtils::save( QIODevice* iodevice, const File* bibtexfile, QStringList* errorLog )
60
emit progress( 0, 3 );
63
if ( !toBuffer( bibtexfile, &bibBuffer, errorLog ) )
65
emit progress( 1, 3 );
66
if ( m_cancelFlag || !bufferToXMLbuffer( &bibBuffer ) )
68
emit progress( 2, 3 );
69
if ( m_cancelFlag || !xmlBufferToIOdevice( iodevice ) )
71
emit progress( 3, 3 );
75
void FileExporterBibUtils::cancel()
77
m_bibTeXExporter->cancel();
81
bool FileExporterBibUtils::toBuffer( const File *bibFile, QBuffer *buffer, QStringList* errorLog )
83
buffer->open( IO_WriteOnly );
84
bool result = m_bibTeXExporter->save( buffer, bibFile, errorLog );
89
bool FileExporterBibUtils::toBuffer( const Element *bibElement, QBuffer *buffer, QStringList* errorLog )
91
buffer->open( IO_WriteOnly );
92
bool result = m_bibTeXExporter->save( buffer, bibElement, errorLog );
97
bool FileExporterBibUtils::bufferToXMLbuffer( QBuffer *bibBuffer )
101
m_processBuffer->open( IO_WriteOnly );
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() ) );
109
if ( m_process->isRunning() )
111
bibBuffer->open( IO_ReadOnly );
112
m_process->writeToStdin( bibBuffer->readAll() );
113
qApp->processEvents();
114
m_process->closeStdin();
117
int nothingHappens = 20;
121
qApp->processEvents();
125
if ( nothingHappens <= 0 )
128
if ( !m_process->normalExit() )
130
qDebug( "%s did not exit in a clean fashion", m_process->arguments()[0].latin1() );
137
qDebug( "%s did not start", m_process->arguments()[0].latin1() );
142
m_processBuffer->close();
148
bool FileExporterBibUtils::xmlBufferToIOdevice( QIODevice *iodevice )
154
switch ( m_outputFormat )
156
case BibTeX::File::formatISI:
157
m_process = new QProcess( QStringList::split( ' ', "xml2isi" ) );
159
case BibTeX::File::formatWordBib:
160
m_process = new QProcess( QStringList::split( ' ', "xml2wordbib" ) );
162
case BibTeX::File::formatAds:
163
m_process = new QProcess( QStringList::split( ' ', "xml2ads" ) );
165
case BibTeX::File::formatEndNote:
166
m_process = new QProcess( QStringList::split( ' ', "xml2end" ) );
168
case BibTeX::File::formatRIS:
169
m_process = new QProcess( QStringList::split( ' ', "xml2ris" ) );
171
case BibTeX::File::formatMODS:
172
/* m_process = NULL; */
175
qDebug( "Cannot handle output format %i", m_outputFormat );
179
if ( m_process != NULL )
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() ) );
185
if ( m_process->start() )
187
QBuffer *tempBuffer = m_processBuffer;
188
m_processBuffer = new QBuffer();
190
tempBuffer->open( IO_ReadOnly );
191
m_process->writeToStdin( tempBuffer->readAll() );
192
qApp->processEvents();
193
m_process->closeStdin();
196
m_processBuffer->open( IO_WriteOnly );
197
int nothingHappens = 20;
201
qApp->processEvents();
204
m_processBuffer->close();
208
if ( nothingHappens <= 0 )
211
if ( ! m_process->normalExit() )
217
m_processBuffer->open( IO_ReadOnly );
218
iodevice->writeBlock( m_processBuffer->buffer() );
219
m_processBuffer->close();
225
qDebug( "%s did not start", m_process->arguments()[0].latin1() );
232
m_processBuffer->open( IO_ReadOnly );
233
iodevice->writeBlock( m_processBuffer->buffer() );
234
m_processBuffer->close();
240
void FileExporterBibUtils::wakeUp()
245
void FileExporterBibUtils::slotReadyStdout()
247
m_processBuffer->writeBlock( m_process->readStdout() );
250
void FileExporterBibUtils::slotReadyStderr()
252
QByteArray ba = m_process->readStderr();
253
QTextStream bats( ba, IO_ReadOnly );
254
bats.setEncoding( QTextStream::UnicodeUTF8 );
255
qDebug( "%s", bats.read().latin1() );
259
#include "fileexporterbibutils.moc"