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
***************************************************************************/
25
#include <qstringlist.h>
27
#include <qwaitcondition.h>
28
#include <qapplication.h>
31
#include <encoderlatex.h>
32
#include "fileimporterbibutils.h"
37
FileImporterBibUtils::FileImporterBibUtils( BibTeX::File::FileFormat inputFormat )
38
: FileImporter(), m_workingDir( createTempDir() ), m_inputFormat( inputFormat ), m_bibTeXImporter( new FileImporterBibTeX( false, "utf-8" ) )
40
m_processBuffer = new QBuffer();
43
FileImporterBibUtils::~FileImporterBibUtils()
45
delete m_processBuffer;
46
deleteTempDir( m_workingDir );
47
delete m_bibTeXImporter;
50
File* FileImporterBibUtils::load( QIODevice *iodevice )
53
if ( !iodevice->isReadable() )
55
qDebug( "iodevice is not readable" );
58
if ( !iodevice->isOpen() )
60
qDebug( "iodevice is not open" );
64
if ( !iodeviceToXMLbuffer( iodevice ) || m_cancelFlag )
67
return xmlBufferToBibTeXFile();
70
bool FileImporterBibUtils::guessCanDecode( const QString & text )
72
return guessInputFormat( text ) != BibTeX::File::formatUndefined;
75
File::FileFormat FileImporterBibUtils::guessInputFormat( const QString &text )
77
if ( text.find( "TY - " ) >= 0 )
78
return BibTeX::File::formatRIS;
79
else if ( text.find( "%A " ) >= 0 )
80
return BibTeX::File::formatEndNote;
81
else if ( text.find( "FN ISI Export Format" ) >= 0 )
82
return BibTeX::File::formatISI;
84
return BibTeX::File::formatUndefined;
87
void FileImporterBibUtils::cancel()
89
m_bibTeXImporter->cancel();
93
bool FileImporterBibUtils::iodeviceToXMLbuffer( QIODevice *iodevice )
97
m_processBuffer->open( IO_WriteOnly );
99
switch ( m_inputFormat )
101
case BibTeX::File::formatISI:
102
m_process = new QProcess( QStringList::split( ' ', "isi2xml -i utf8 -u" ) );
104
case BibTeX::File::formatWordBib:
105
m_process = new QProcess( QStringList::split( ' ', "wordbib2xml -i utf8 -u" ) );
107
case BibTeX::File::formatAds:
108
m_process = new QProcess( QStringList::split( ' ', "ads2xml -i utf8 -u" ) );
110
case BibTeX::File::formatEndNote:
111
m_process = new QProcess( QStringList::split( ' ', "end2xml -i utf8 -u" ) );
113
case BibTeX::File::formatEndNoteXML:
114
m_process = new QProcess( QStringList::split( ' ', "endx2xml -i utf8 -u" ) );
116
case BibTeX::File::formatRIS:
117
m_process = new QProcess( QStringList::split( ' ', "ris2xml -i utf8 -u" ) );
119
case BibTeX::File::formatMODS:
120
/* m_process = NULL; */
123
qDebug( "Cannot handle input format %i", m_inputFormat );
127
if ( m_process != NULL )
130
connect( m_process, SIGNAL( processExited() ), this, SLOT( wakeUp() ) );
131
connect( m_process, SIGNAL( readyReadStdout() ), this, SLOT( slotReadyStdout() ) );
132
connect( m_process, SIGNAL( readyReadStderr() ), this, SLOT( slotReadyStderr() ) );
135
if ( m_process->isRunning() )
137
QByteArray inData = iodevice->readAll();
138
m_process->writeToStdin( inData );
139
qApp->processEvents();
140
m_process->closeStdin();
142
int nothingHappens = 20;
146
qApp->processEvents();
150
if ( nothingHappens <= 0 )
153
if ( !m_process->normalExit() )
155
qDebug( "%s did not exit in a clean fashion", m_process->arguments()[0].latin1() );
162
qDebug( "%s did not start", m_process->arguments()[0].latin1() );
169
m_processBuffer->writeBlock( iodevice->readAll() );
172
m_processBuffer->close();
178
BibTeX::File* FileImporterBibUtils::xmlBufferToBibTeXFile()
183
m_process = new QProcess( QStringList::split( ' ', "xml2bib -i utf8 -o utf8 -sk" ) );
184
connect( m_process, SIGNAL( processExited() ), this, SLOT( wakeUp() ) );
185
connect( m_process, SIGNAL( readyReadStdout() ), this, SLOT( slotReadyStdout() ) );
186
connect( m_process, SIGNAL( readyReadStderr() ), this, SLOT( slotReadyStderr() ) );
188
if ( m_process->start() )
190
QBuffer *tempBuffer = m_processBuffer;
191
m_processBuffer = new QBuffer();
193
tempBuffer->open( IO_ReadOnly );
194
m_process->writeToStdin( tempBuffer->readAll() );
195
qApp->processEvents();
196
m_process->closeStdin();
199
m_processBuffer->open( IO_WriteOnly );
200
int nothingHappens = 20;
204
qApp->processEvents();
207
m_processBuffer->close();
209
if ( nothingHappens <= 0 )
214
if ( ! m_process->normalExit() )
220
m_processBuffer->open( IO_ReadOnly );
221
File *bibTeXFile = m_bibTeXImporter->load( m_processBuffer );
222
m_processBuffer->close();
237
QString FileImporterBibUtils::createTempDir()
239
QString result = QString::null;
240
QFile *devrandom = new QFile( "/dev/random" );
242
if ( devrandom->open( IO_ReadOnly ) )
244
Q_UINT32 randomNumber;
245
if ( devrandom->readBlock(( char* ) & randomNumber, sizeof( randomNumber ) ) > 0 )
247
randomNumber |= 0x10000000;
248
result = QString( "/tmp/bibtex-%1" ).arg( randomNumber, sizeof( randomNumber ) * 2, 16 );
249
if ( !QDir().mkdir( result ) )
250
result = QString::null;
260
void FileImporterBibUtils::deleteTempDir( const QString& directory )
262
QDir dir = QDir( directory );
263
QStringList subDirs = dir.entryList( QDir::Dirs );
264
for ( QStringList::Iterator it = subDirs.begin(); it != subDirs.end(); it++ )
266
if (( QString::compare( *it, "." ) != 0 ) && ( QString::compare( *it, ".." ) != 0 ) )
267
deleteTempDir( *it );
269
QStringList allEntries = dir.entryList( QDir::All );
270
for ( QStringList::Iterator it = allEntries.begin(); it != allEntries.end(); it++ )
273
QDir().rmdir( directory );
276
void FileImporterBibUtils::wakeUp()
281
void FileImporterBibUtils::slotReadyStdout()
283
m_processBuffer->writeBlock( m_process->readStdout() );
286
void FileImporterBibUtils::slotReadyStderr()
288
QByteArray ba = m_process->readStderr();
289
QTextStream bats( ba, IO_ReadOnly );
290
bats.setEncoding( QTextStream::UnicodeUTF8 );
291
qDebug( "%s", bats.read().latin1() );
295
#include "fileimporterbibutils.moc"