Skip to content

Commit

Permalink
Getting rid from KDE stuff in the fileprovider.
Browse files Browse the repository at this point in the history
  • Loading branch information
martonmiklos committed Feb 1, 2018
1 parent 8ba0d2f commit df66a59
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 38 deletions.
50 changes: 16 additions & 34 deletions common/fileprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@
*/

#include <unistd.h>
#include <kdefakes.h> // usleep

#include <QCoreApplication>
#include <QEventLoop>
#include <QFile>

#include <kio/job.h>
#include <ktemporaryfile.h>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QTemporaryFile>

#include "fileprovider.h"

FileProvider::FileProvider()
: QObject( 0 ), mBlocked( false )
FileProvider::FileProvider(QObject *parent)
: QObject( parent )
{
connect(&mManager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(downloadFinished(QNetworkReply*)));
}

bool FileProvider::get( const QString &url, QString &target )
Expand All @@ -42,30 +43,24 @@ bool FileProvider::get( const QString &url, QString &target )
cleanUp();

if ( target.isEmpty() ) {
KTemporaryFile tmpFile;
QTemporaryFile tmpFile;
tmpFile.setAutoRemove(false);
tmpFile.open();
target = tmpFile.fileName();
mFileName = target;
}

mData.truncate( 0 );

qDebug( "Downloading external schema '%s'", qPrintable( url ) );

KIO::TransferJob* job = KIO::get( KUrl( url ), KIO::NoReload, KIO::HideProgressInfo );
connect( job, SIGNAL( data( KIO::Job*, const QByteArray& ) ),
this, SLOT( slotData( KIO::Job*, const QByteArray& ) ) );
connect( job, SIGNAL( result( KJob* ) ),
this, SLOT( slotResult( KJob* ) ) );
QNetworkRequest request = QNetworkRequest(QUrl(url));
QNetworkReply *reply = mManager.get(request);

mBlocked = true;
while ( mBlocked ) {
while ( !reply->isFinished() ) {
QCoreApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
usleep( 500 );
}

return true;
return (reply->error() == QNetworkReply::NoError);
}

void FileProvider::cleanUp()
Expand All @@ -74,35 +69,22 @@ void FileProvider::cleanUp()
mFileName = QString();
}

void FileProvider::slotData( KIO::Job*, const QByteArray &data )
{
unsigned int oldSize = mData.size();
mData.resize( oldSize + data.size() );
memcpy( mData.data() + oldSize, data.data(), data.size() );
}

void FileProvider::slotResult( KJob *job )
void FileProvider::downloadFinished(QNetworkReply *reply )
{
if ( job->error() ) {
qDebug( "%s", qPrintable( job->errorText() ) );
mBlocked = false;
if ( reply->error() != QNetworkReply::NoError) {
qDebug( "%s", qPrintable( reply->errorString() ) );
return;
}

QFile file( mFileName );
if ( !file.open( QIODevice::WriteOnly ) ) {
qDebug( "Unable to create temporary file" );
mBlocked = false;
return;
}

qDebug( "Download successful" );
file.write( mData );
file.write( reply->readAll() );
file.close();

mData.truncate( 0 );

mBlocked = false;
}

#include "fileprovider.moc"
8 changes: 4 additions & 4 deletions common/fileprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#define FILEPROVIDER_H

#include <QObject>
#include <QNetworkAccessManager>

#include <kode_export.h>

Expand All @@ -35,19 +36,18 @@ class KXMLCOMMON_EXPORT FileProvider : QObject
Q_OBJECT

public:
FileProvider();
FileProvider(QObject *parent = NULL);

bool get( const QString &url, QString &target );
void cleanUp();

private slots:
void slotData( KIO::Job*, const QByteArray& );
void slotResult( KJob* );
void downloadFinished( QNetworkReply *reply );

private:
QString mFileName;
QByteArray mData;
bool mBlocked;
QNetworkAccessManager mManager;
};

#endif

0 comments on commit df66a59

Please sign in to comment.