77 lines
2.3 KiB
C++
77 lines
2.3 KiB
C++
/*! @file
|
|
|
|
@id $Id$
|
|
*/
|
|
// 1 2 3 4 5 6 7 8
|
|
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
|
#ifndef WEBPAGE_HXX
|
|
#define WEBPAGE_HXX
|
|
|
|
#include <exceptions.hxx>
|
|
#include <networkaccessmanager.hxx>
|
|
#include <QWebPage>
|
|
#include <QWebFrame>
|
|
#include <QWebSettings>
|
|
#include <QWidget>
|
|
#include <QFileDialog>
|
|
#include <QFile>
|
|
|
|
#include <cassert>
|
|
#ifndef LOG
|
|
#include <iostream>
|
|
#define LOG(X) std::clog<<X<<std::endl;
|
|
inline std::ostream& operator<<(std::ostream& o, const QString& s) {
|
|
return o<<s.toStdString();
|
|
}
|
|
#endif
|
|
|
|
class TestWebPage: public QWebPage {
|
|
Q_OBJECT;
|
|
public:
|
|
TestWebPage(QObject* parent = 0, bool unattended = false):
|
|
QWebPage(parent),
|
|
_unattended(unattended) {
|
|
setNetworkAccessManager(new NetworkAccessManager(this));
|
|
settings()->setOfflineStorageDefaultQuota(1024*1024*1024);
|
|
settings()->setOfflineStoragePath("/tmp/");
|
|
settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, true);
|
|
//settings()->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled, true);
|
|
settings()->setAttribute(QWebSettings::LocalStorageEnabled, true);
|
|
}
|
|
virtual ~TestWebPage() {
|
|
if (!_nextFile.isEmpty() && !std::uncaught_exception())
|
|
throw LastFileNotUploaded(_nextFile);
|
|
}
|
|
void setNextUploadFile(QString nextFile) {
|
|
if (!_unattended) throw NotUnattended();
|
|
if (!_nextFile.isEmpty()) throw LastFileNotUploaded(_nextFile);
|
|
if (nextFile.isEmpty()) throw EmptyUploadFile();
|
|
_nextFile = nextFile;
|
|
}
|
|
bool uploadPrepared() {
|
|
return !_nextFile.isEmpty();
|
|
}
|
|
Q_SIGNALS:
|
|
void uploadFile(QString filename);
|
|
protected:
|
|
virtual QString chooseFile(QWebFrame* frame, const QString&) {
|
|
if (_unattended) {
|
|
if (_nextFile.isEmpty()) throw NoUploadFile();
|
|
if (!QFile(_nextFile).exists()) throw FileNotFound(_nextFile);
|
|
QString filename(_nextFile);
|
|
_nextFile.clear();
|
|
return filename;
|
|
} else {
|
|
QString filename(QFileDialog::getOpenFileName
|
|
(frame->page()->view(), tr("File to Upload")));
|
|
if (filename.size()) uploadFile(filename);
|
|
return filename;
|
|
}
|
|
}
|
|
private:
|
|
bool _unattended;
|
|
QString _nextFile;
|
|
};
|
|
|
|
#endif
|