summaryrefslogtreecommitdiffstats
path: root/src/fileexporterbibutils.cpp
blob: 4de7aeb8147eb212d9ac6d836b4b01e024f477e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/***************************************************************************
 *   Copyright (C) 2004-2009 by Thomas Fischer                             *
 *   [email protected]                                             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include <qfile.h>
#include <qdir.h>
#include <qstringlist.h>
#include <qapplication.h>
#include <qbuffer.h>
#include <qprocess.h>
#include <qwaitcondition.h>

#include <fileexporterbibtex.h>
#include "fileexporterbibutils.h"

namespace BibTeX
{

    FileExporterBibUtils::FileExporterBibUtils( BibTeX::File::FileFormat outputFormat )
            : FileExporter(), m_outputFormat( outputFormat ), m_bibTeXExporter( new FileExporterBibTeX() )
    {
        m_bibTeXExporter->setEncoding( "utf-8" );
        m_processBuffer = new QBuffer();
    }

    FileExporterBibUtils::~FileExporterBibUtils()
    {
        delete m_processBuffer;
        delete m_bibTeXExporter;
    }

    bool FileExporterBibUtils::save( QIODevice* iodevice, const Element* element, QStringList* errorLog )
    {
        m_cancelFlag = false;
        QBuffer bibBuffer;
        if ( !toBuffer( element, &bibBuffer, errorLog ) )
            return false;
        if ( !bufferToXMLbuffer( &bibBuffer ) || m_cancelFlag )
            return false;
        return !m_cancelFlag && xmlBufferToIOdevice( iodevice );
    }

    bool FileExporterBibUtils::save( QIODevice* iodevice, const File* bibtexfile, QStringList* errorLog )
    {
        emit progress( 0, 3 );
        m_cancelFlag = false;
        QBuffer bibBuffer;
        if ( !toBuffer( bibtexfile, &bibBuffer, errorLog ) )
            return false;
        emit progress( 1, 3 );
        if ( m_cancelFlag || !bufferToXMLbuffer( &bibBuffer ) )
            return false;
        emit progress( 2, 3 );
        if ( m_cancelFlag || !xmlBufferToIOdevice( iodevice ) )
            return false;
        emit progress( 3, 3 );
        return !m_cancelFlag;
    }

    void FileExporterBibUtils::cancel()
    {
        m_bibTeXExporter->cancel();
        m_cancelFlag = true;
    }

    bool FileExporterBibUtils::toBuffer( const File *bibFile, QBuffer *buffer, QStringList* errorLog )
    {
        buffer->open( IO_WriteOnly );
        bool result = m_bibTeXExporter->save( buffer, bibFile, errorLog );
        buffer->close();
        return result;
    }

    bool FileExporterBibUtils::toBuffer( const Element *bibElement, QBuffer *buffer, QStringList* errorLog )
    {
        buffer->open( IO_WriteOnly );
        bool result = m_bibTeXExporter->save( buffer, bibElement, errorLog );
        buffer->close();
        return result;
    }

    bool FileExporterBibUtils::bufferToXMLbuffer( QBuffer *bibBuffer )
    {
        QWaitCondition wc;

        m_processBuffer->open( IO_WriteOnly );
        m_waiting = true;
        m_process = new QProcess( QStringList::split( ' ', "bib2xml -i utf8" ) );
        connect( m_process, SIGNAL( processExited() ), this, SLOT( wakeUp() ) );
        connect( m_process, SIGNAL( readyReadStdout() ), this, SLOT( slotReadyStdout() ) );
        connect( m_process, SIGNAL( readyReadStderr() ), this, SLOT( slotReadyStderr() ) );

        m_process->start();
        if ( m_process->isRunning() )
        {
            bibBuffer->open( IO_ReadOnly );
            m_process->writeToStdin( bibBuffer->readAll() );
            qApp->processEvents();
            m_process->closeStdin();
            bibBuffer->close();

            int nothingHappens = 20;
            while ( m_waiting )
            {
                wc.wait( 250 );
                qApp->processEvents();
                --nothingHappens;
            }

            if ( nothingHappens <= 0 )
                m_process->kill();

            if ( !m_process->normalExit() )
            {
                qDebug( "%s did not exit in a clean fashion", m_process->arguments()[0].latin1() );
                delete m_process;
                return false;
            }
        }
        else
        {
            qDebug( "%s did not start", m_process->arguments()[0].latin1() );
            delete m_process;
            return false;
        }

        m_processBuffer->close();

        delete m_process;
        return true;
    }

    bool FileExporterBibUtils::xmlBufferToIOdevice( QIODevice *iodevice )
    {
        QWaitCondition wc;
        m_waiting = true;

        m_process = NULL;
        switch ( m_outputFormat )
        {
        case BibTeX::File::formatISI:
            m_process = new QProcess( QStringList::split( ' ', "xml2isi" ) );
            break;
        case BibTeX::File::formatWordBib:
            m_process = new QProcess( QStringList::split( ' ', "xml2wordbib" ) );
            break;
        case BibTeX::File::formatAds:
            m_process = new QProcess( QStringList::split( ' ', "xml2ads" ) );
            break;
        case BibTeX::File::formatEndNote:
            m_process = new QProcess( QStringList::split( ' ', "xml2end" ) );
            break;
        case BibTeX::File::formatRIS:
            m_process = new QProcess( QStringList::split( ' ', "xml2ris" ) );
            break;
        case BibTeX::File::formatMODS:
            /* m_process = NULL; */
            break;
        default:
            qDebug( "Cannot handle output format %i", m_outputFormat );
            return false;
        }

        if ( m_process != NULL )
        {
            connect( m_process, SIGNAL( processExited() ), this, SLOT( wakeUp() ) );
            connect( m_process, SIGNAL( readyReadStdout() ), this, SLOT( slotReadyStdout() ) );
            connect( m_process, SIGNAL( readyReadStderr() ), this, SLOT( slotReadyStderr() ) );

            if ( m_process->start() )
            {
                QBuffer *tempBuffer = m_processBuffer;
                m_processBuffer = new QBuffer();

                tempBuffer->open( IO_ReadOnly );
                m_process->writeToStdin( tempBuffer->readAll() );
                qApp->processEvents();
                m_process->closeStdin();
                tempBuffer->close();

                m_processBuffer->open( IO_WriteOnly );
                int nothingHappens = 20;
                while ( m_waiting )
                {
                    wc.wait( 250 );
                    qApp->processEvents();
                    --nothingHappens;
                }
                m_processBuffer->close();

                delete tempBuffer;

                if ( nothingHappens <= 0 )
                    m_process->kill();

                if ( ! m_process->normalExit() )
                {
                    delete m_process;
                    return false;
                }

                m_processBuffer->open( IO_ReadOnly );
                iodevice->writeBlock( m_processBuffer->buffer() );
                m_processBuffer->close();

                delete m_process;
            }
            else
            {
                qDebug( "%s did not start", m_process->arguments()[0].latin1() );
                delete m_process;
                return false;
            }
        }
        else
        {
            m_processBuffer->open( IO_ReadOnly );
            iodevice->writeBlock( m_processBuffer->buffer() );
            m_processBuffer->close();
        }

        return true;
    }

    void FileExporterBibUtils::wakeUp()
    {
        m_waiting = false;
    }

    void FileExporterBibUtils::slotReadyStdout()
    {
        m_processBuffer->writeBlock( m_process->readStdout() );
    }

    void FileExporterBibUtils::slotReadyStderr()
    {
        QByteArray ba = m_process->readStderr();
        QTextStream bats( ba, IO_ReadOnly );
        bats.setEncoding( QTextStream::UnicodeUTF8 );
        qDebug( "%s", bats.read().latin1() );
    }

}
#include "fileexporterbibutils.moc"