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

« back to all changes in this revision

Viewing changes to src/fileimporterbibutils.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 <stdio.h>
21
 
 
22
 
#include <qfile.h>
23
 
#include <qdir.h>
24
 
#include <qprocess.h>
25
 
#include <qstringlist.h>
26
 
#include <qbuffer.h>
27
 
#include <qwaitcondition.h>
28
 
#include <qapplication.h>
29
 
 
30
 
#include <file.h>
31
 
#include <encoderlatex.h>
32
 
#include "fileimporterbibutils.h"
33
 
 
34
 
namespace BibTeX
35
 
{
36
 
 
37
 
    FileImporterBibUtils::FileImporterBibUtils( BibTeX::File::FileFormat inputFormat )
38
 
            : FileImporter(), m_workingDir( createTempDir() ), m_inputFormat( inputFormat ), m_bibTeXImporter( new FileImporterBibTeX( false, "utf-8" ) )
39
 
    {
40
 
        m_processBuffer = new QBuffer();
41
 
    }
42
 
 
43
 
    FileImporterBibUtils::~FileImporterBibUtils()
44
 
    {
45
 
        delete m_processBuffer;
46
 
        deleteTempDir( m_workingDir );
47
 
        delete m_bibTeXImporter;
48
 
    }
49
 
 
50
 
    File* FileImporterBibUtils::load( QIODevice *iodevice )
51
 
    {
52
 
        m_cancelFlag = false;
53
 
        if ( !iodevice->isReadable() )
54
 
        {
55
 
            qDebug( "iodevice is not readable" );
56
 
            return NULL;
57
 
        }
58
 
        if ( !iodevice->isOpen() )
59
 
        {
60
 
            qDebug( "iodevice is not open" );
61
 
            return NULL;
62
 
        }
63
 
 
64
 
        if ( !iodeviceToXMLbuffer( iodevice ) || m_cancelFlag )
65
 
            return NULL;
66
 
 
67
 
        return xmlBufferToBibTeXFile();
68
 
    }
69
 
 
70
 
    bool FileImporterBibUtils::guessCanDecode( const QString & text )
71
 
    {
72
 
        return guessInputFormat( text ) != BibTeX::File::formatUndefined;
73
 
    }
74
 
 
75
 
    File::FileFormat FileImporterBibUtils::guessInputFormat( const QString &text )
76
 
    {
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;
83
 
        else
84
 
            return BibTeX::File::formatUndefined;
85
 
    }
86
 
 
87
 
    void FileImporterBibUtils::cancel()
88
 
    {
89
 
        m_bibTeXImporter->cancel();
90
 
        m_cancelFlag = true;
91
 
    }
92
 
 
93
 
    bool FileImporterBibUtils::iodeviceToXMLbuffer( QIODevice *iodevice )
94
 
    {
95
 
        QWaitCondition wc;
96
 
 
97
 
        m_processBuffer->open( IO_WriteOnly );
98
 
        m_process = NULL;
99
 
        switch ( m_inputFormat )
100
 
        {
101
 
        case BibTeX::File::formatISI:
102
 
            m_process = new QProcess( QStringList::split( ' ', "isi2xml -i utf8 -u" ) );
103
 
            break;
104
 
        case BibTeX::File::formatWordBib:
105
 
            m_process = new QProcess( QStringList::split( ' ', "wordbib2xml -i utf8 -u" ) );
106
 
            break;
107
 
        case BibTeX::File::formatAds:
108
 
            m_process = new QProcess( QStringList::split( ' ', "ads2xml -i utf8 -u" ) );
109
 
            break;
110
 
        case BibTeX::File::formatEndNote:
111
 
            m_process = new QProcess( QStringList::split( ' ', "end2xml -i utf8 -u" ) );
112
 
            break;
113
 
        case BibTeX::File::formatEndNoteXML:
114
 
            m_process = new QProcess( QStringList::split( ' ', "endx2xml -i utf8 -u" ) );
115
 
            break;
116
 
        case BibTeX::File::formatRIS:
117
 
            m_process = new QProcess( QStringList::split( ' ', "ris2xml -i utf8 -u" ) );
118
 
            break;
119
 
        case BibTeX::File::formatMODS:
120
 
            /* m_process = NULL; */
121
 
            break;
122
 
        default:
123
 
            qDebug( "Cannot handle input format %i", m_inputFormat );
124
 
            return false;
125
 
        }
126
 
 
127
 
        if ( m_process != NULL )
128
 
        {
129
 
            m_waiting = true;
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() ) );
133
 
 
134
 
            m_process->start();
135
 
            if ( m_process->isRunning() )
136
 
            {
137
 
                QByteArray inData = iodevice->readAll();
138
 
                m_process->writeToStdin( inData );
139
 
                qApp->processEvents();
140
 
                m_process->closeStdin();
141
 
 
142
 
                int nothingHappens = 20;
143
 
                while ( m_waiting )
144
 
                {
145
 
                    wc.wait( 250 );
146
 
                    qApp->processEvents();
147
 
                    --nothingHappens;
148
 
                }
149
 
 
150
 
                if ( nothingHappens <= 0 )
151
 
                    m_process->kill();
152
 
 
153
 
                if ( !m_process->normalExit() )
154
 
                {
155
 
                    qDebug( "%s did not exit in a clean fashion", m_process->arguments()[0].latin1() );
156
 
                    delete m_process;
157
 
                    return false;
158
 
                }
159
 
            }
160
 
            else
161
 
            {
162
 
                qDebug( "%s did not start", m_process->arguments()[0].latin1() );
163
 
                delete m_process;
164
 
                return false;
165
 
            }
166
 
        }
167
 
        else
168
 
        {
169
 
            m_processBuffer->writeBlock( iodevice->readAll() );
170
 
        }
171
 
 
172
 
        m_processBuffer->close();
173
 
 
174
 
        delete m_process;
175
 
        return true;
176
 
    }
177
 
 
178
 
    BibTeX::File* FileImporterBibUtils::xmlBufferToBibTeXFile()
179
 
    {
180
 
        QWaitCondition wc;
181
 
 
182
 
        m_waiting = true;
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() ) );
187
 
 
188
 
        if ( m_process->start() )
189
 
        {
190
 
            QBuffer *tempBuffer = m_processBuffer;
191
 
            m_processBuffer = new QBuffer();
192
 
 
193
 
            tempBuffer->open( IO_ReadOnly );
194
 
            m_process->writeToStdin( tempBuffer->readAll() );
195
 
            qApp->processEvents();
196
 
            m_process->closeStdin();
197
 
            tempBuffer->close();
198
 
 
199
 
            m_processBuffer->open( IO_WriteOnly );
200
 
            int nothingHappens = 20;
201
 
            while ( m_waiting )
202
 
            {
203
 
                wc.wait( 250 );
204
 
                qApp->processEvents();
205
 
                --nothingHappens;
206
 
            }
207
 
            m_processBuffer->close();
208
 
 
209
 
            if ( nothingHappens <= 0 )
210
 
                m_process->kill();
211
 
 
212
 
            delete tempBuffer;
213
 
 
214
 
            if ( ! m_process->normalExit() )
215
 
            {
216
 
                delete m_process;
217
 
                return NULL;
218
 
            }
219
 
 
220
 
            m_processBuffer->open( IO_ReadOnly );
221
 
            File *bibTeXFile = m_bibTeXImporter->load( m_processBuffer );
222
 
            m_processBuffer->close();
223
 
 
224
 
            delete m_process;
225
 
            return bibTeXFile;
226
 
        }
227
 
        else
228
 
        {
229
 
            delete m_process;
230
 
            return NULL;
231
 
        }
232
 
 
233
 
        delete m_process;
234
 
        return NULL;
235
 
    }
236
 
 
237
 
    QString FileImporterBibUtils::createTempDir()
238
 
    {
239
 
        QString result = QString::null;
240
 
        QFile *devrandom = new QFile( "/dev/random" );
241
 
 
242
 
        if ( devrandom->open( IO_ReadOnly ) )
243
 
        {
244
 
            Q_UINT32 randomNumber;
245
 
            if ( devrandom->readBlock(( char* ) & randomNumber, sizeof( randomNumber ) ) > 0 )
246
 
            {
247
 
                randomNumber |= 0x10000000;
248
 
                result = QString( "/tmp/bibtex-%1" ).arg( randomNumber, sizeof( randomNumber ) * 2, 16 );
249
 
                if ( !QDir().mkdir( result ) )
250
 
                    result = QString::null;
251
 
            }
252
 
            devrandom->close();
253
 
        }
254
 
 
255
 
        delete devrandom;
256
 
 
257
 
        return result;
258
 
    }
259
 
 
260
 
    void FileImporterBibUtils::deleteTempDir( const QString& directory )
261
 
    {
262
 
        QDir dir = QDir( directory );
263
 
        QStringList subDirs = dir.entryList( QDir::Dirs );
264
 
        for ( QStringList::Iterator it = subDirs.begin(); it != subDirs.end(); it++ )
265
 
        {
266
 
            if (( QString::compare( *it, "." ) != 0 ) && ( QString::compare( *it, ".." ) != 0 ) )
267
 
                deleteTempDir( *it );
268
 
        }
269
 
        QStringList allEntries = dir.entryList( QDir::All );
270
 
        for ( QStringList::Iterator it = allEntries.begin(); it != allEntries.end(); it++ )
271
 
            dir.remove( *it );
272
 
 
273
 
        QDir().rmdir( directory );
274
 
    }
275
 
 
276
 
    void FileImporterBibUtils::wakeUp()
277
 
    {
278
 
        m_waiting = false;
279
 
    }
280
 
 
281
 
    void FileImporterBibUtils::slotReadyStdout()
282
 
    {
283
 
        m_processBuffer->writeBlock( m_process->readStdout() );
284
 
    }
285
 
 
286
 
    void FileImporterBibUtils::slotReadyStderr()
287
 
    {
288
 
        QByteArray ba = m_process->readStderr();
289
 
        QTextStream bats( ba, IO_ReadOnly );
290
 
        bats.setEncoding( QTextStream::UnicodeUTF8 );
291
 
        qDebug( "%s", bats.read().latin1() );
292
 
    }
293
 
 
294
 
}
295
 
#include "fileimporterbibutils.moc"
296