1
/***************************************************************************
2
* Copyright (C) 2004-2005 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
***************************************************************************/
24
#ifdef HAVE_LIBBIBUTILS
30
#include <qstringlist.h>
32
#include <fileimporterbibtex.h>
33
#include "fileimporterbibutils.h"
38
FileImporterBibUtils::FileImporterBibUtils( InputFormat inputFormat )
39
: FileImporter(), m_workingDir( createTempDir() ), m_inputFormat( inputFormat ), m_bibTeXImporter( new FileImporterBibTeX() )
44
FileImporterBibUtils::~FileImporterBibUtils()
46
deleteTempDir( m_workingDir );
47
delete m_bibTeXImporter;
50
File* FileImporterBibUtils::load( QIODevice *iodevice )
54
QString inputFileName = QString( m_workingDir ).append( "/input.tmp" );
55
QString bibFileName = QString( m_workingDir ).append( "/input.bib" );
57
/** write temporary data to file to be read by bibutils */
58
QFile inputFile( inputFileName );
59
if ( inputFile.open( IO_WriteOnly ) )
61
const Q_LONG bufferSize = 16384;
62
char *buffer = new char[bufferSize];
64
while (( readBytes = iodevice->readBlock( buffer, bufferSize ) ) > 0 )
66
if ( inputFile.writeBlock( buffer, readBytes ) != readBytes )
74
if ( readBytes == -1 )
83
bibl_initparams( &p, ( int )m_inputFormat, BIBL_BIBTEXOUT );
85
FILE *fp = fopen( inputFileName.ascii(), "r" );
86
int err = BIBL_ERR_BADINPUT;
89
err = bibl_read( &b, fp, inputFileName.ascii(), ( int )m_inputFormat, &p );
94
qDebug( "Num read references: %ld\n", b.nrefs );
96
fp = fopen( bibFileName.ascii(), "w" );
100
err = bibl_write( &b, fp, BIBL_BIBTEXOUT, &p );
104
if ( err != BIBL_OK )
107
QFile bibFile( bibFileName );
108
if ( bibFile.open( IO_ReadOnly ) )
110
result = m_bibTeXImporter->load( &bibFile );
117
bool FileImporterBibUtils::guessCanDecode( const QString & /*text*/ )
119
return FALSE; // FIXME
122
void FileImporterBibUtils::cancel()
124
m_bibTeXImporter->cancel();
128
QString FileImporterBibUtils::createTempDir()
130
QString result = QString::null;
131
QFile *devrandom = new QFile( "/dev/random" );
133
if ( devrandom->open( IO_ReadOnly ) )
135
Q_UINT32 randomNumber;
136
if ( devrandom->readBlock(( char* ) & randomNumber, sizeof( randomNumber ) ) > 0 )
138
randomNumber |= 0x10000000;
139
result = QString( "/tmp/bibtex-%1" ).arg( randomNumber, sizeof( randomNumber ) * 2, 16 );
140
if ( !QDir().mkdir( result ) )
141
result = QString::null;
151
void FileImporterBibUtils::deleteTempDir( const QString& directory )
153
QDir dir = QDir( directory );
154
QStringList subDirs = dir.entryList( QDir::Dirs );
155
for ( QStringList::Iterator it = subDirs.begin(); it != subDirs.end(); it++ )
157
if (( QString::compare( *it, "." ) != 0 ) && ( QString::compare( *it, ".." ) != 0 ) )
158
deleteTempDir( *it );
160
QStringList allEntries = dir.entryList( QDir::All );
161
for ( QStringList::Iterator it = allEntries.begin(); it != allEntries.end(); it++ )
164
QDir().rmdir( directory );
169
#endif // HAVE_LIBBIBUTILS