Commit 59b84430 authored by Gregor Lang's avatar Gregor Lang Committed by Philipp Erhardt
Browse files

Add URL handling feature

If given file is not a local file it is handled as URL and stored to
temporary directory.
parent cba5b4aa
#include "download.h"
#include <iostream>
#include <QEventLoop>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QByteArray>
#include <QDir>
#include <QFileInfo>
Download::Download() :
manager(new QNetworkAccessManager()),
file(NULL) {
}
Download::~Download() {
delete manager;
delete file;
}
QString Download::load(QString f) {
QUrl url(f);
if( url.isLocalFile() || url.isRelative() ) {
// found local file
// do not download
return QDir::toNativeSeparators(url.toLocalFile());
}
QEventLoop loop;
QObject::connect(manager, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()));
QNetworkRequest request(url);
QNetworkReply *reply = manager->get(request);
loop.exec();
#ifdef DEBUG
std::cerr << "File downloaded" << std::endl;
#endif
// find uniq temporary filename
QFileInfo fileInfo(url.path());
QString fileName = fileInfo.fileName();
delete file;
file = new QTemporaryFile(QDir::tempPath() + QDir::separator() + fileName);
file->setAutoRemove(true);
file->open();
if(reply->error() != QNetworkReply::NoError || !reply->isReadable()) {
std::cerr << "Download failed" << std::cerr;
return NULL;
}
// store data in tempfile
QByteArray data = reply->readAll();
qint64 len = file->write(data);
file->close();
#ifdef DEBUG
std::cerr << "filename: " << file.toStdString() << std::endl;
#endif
return file->fileName();
}
#ifndef DOWNLOAD_H
#define DOWNLOAD_H
#include <QString>
#include <QNetworkAccessManager>
#include <QTemporaryFile>
class Download {
public:
Download();
~Download();
QString load(QString);
private:
QNetworkAccessManager *manager;
QTemporaryFile *file;
};
#endif
......@@ -3,10 +3,11 @@ TARGET = katarakt
DEPENDPATH += .
INCLUDEPATH += .
CONFIG += qt
QT += network
QMAKE_CXXFLAGS_DEBUG += -DDEBUG
# Input
HEADERS += layout.h viewer.h canvas.h resourcemanager.h grid.h search.h gotoline.h config.h
SOURCES += main.cpp layout.cpp viewer.cpp canvas.cpp resourcemanager.cpp grid.cpp search.cpp gotoline.cpp config.cpp
HEADERS += layout.h viewer.h canvas.h resourcemanager.h grid.h search.h gotoline.h config.h download.h
SOURCES += main.cpp layout.cpp viewer.cpp canvas.cpp resourcemanager.cpp grid.cpp search.cpp gotoline.cpp config.cpp download.cpp
unix:LIBS += -lpoppler-qt4
......@@ -2,6 +2,7 @@
#include <QString>
#include <iostream>
#include <getopt.h>
#include "download.h"
#include "resourcemanager.h"
#include "viewer.h"
#include "config.h"
......@@ -58,7 +59,11 @@ int main(int argc, char *argv[]) {
return 1;
}
Viewer katarakt(QString::fromUtf8(argv[optind]));
Download download;
QString file = download.load(QString::fromUtf8(argv[optind]));
std::cerr << "filename: " << file.toStdString() << std::endl;
Viewer katarakt(file);
if (!katarakt.is_valid()) {
return 1;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment