From b1117bb9e31018460b5adc05428842555d47c1b9 Mon Sep 17 00:00:00 2001 From: Nils Schwabe Date: Thu, 6 Mar 2014 16:02:08 +0100 Subject: vmchooser is now able to get vsessions from a given url --- src/dialog.cpp | 28 ++++++++++++++++++++++ src/dialog.h | 4 ++++ src/httpxmldownloader.cpp | 20 +++------------- src/httpxmldownloader.h | 3 +-- src/main.cpp | 14 +++++------ src/vsession.cpp | 61 +++++++---------------------------------------- src/vsession.h | 2 +- 7 files changed, 51 insertions(+), 81 deletions(-) (limited to 'src') diff --git a/src/dialog.cpp b/src/dialog.cpp index d758b0a..def152c 100644 --- a/src/dialog.cpp +++ b/src/dialog.cpp @@ -12,6 +12,7 @@ #include "save_restore_session.h" #include "sessiontreeitem.h" #include "globals.h" +#include "vsession.h" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { @@ -246,3 +247,30 @@ void Dialog::on_centerTimer() { } } +void Dialog::addSessionsAfterDownload(QNetworkReply* reply) { + if (reply->error() != QNetworkReply::NoError) { + qDebug() << "Error reading from URL: " << reply->error(); + return; + } + + QString xml_doc(reply->readAll()); + + QFile file("/tmp/vmchooser2.xml"); + if (!file.open(QIODevice::WriteOnly)) { + if (debugMode) { + qDebug() << "Could not write XML to /tmp"; + } + return; + } + + QByteArray data = xml_doc.toUtf8(); + + if (file.write(data) != data.length()) { + return; + } + + file.close(); + + this->addItems(VSession::readXmlFile(file.fileName()), QCoreApplication::instance()->translate("Dialog", "Virtual Sessions")); +} + diff --git a/src/dialog.h b/src/dialog.h index d1daa68..7e9b1d5 100644 --- a/src/dialog.h +++ b/src/dialog.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "session.h" #include "sessiontreemodel.h" @@ -45,6 +46,9 @@ class Dialog : public QDialog { void on_pushButtonAbort_clicked(); void on_treeView_activated(QModelIndex index); void on_centerTimer(); + + public slots: + void addSessionsAfterDownload(QNetworkReply* reply); }; #endif // DIALOG_H diff --git a/src/httpxmldownloader.cpp b/src/httpxmldownloader.cpp index 788f8f2..0d09e6f 100644 --- a/src/httpxmldownloader.cpp +++ b/src/httpxmldownloader.cpp @@ -4,27 +4,13 @@ httpxmldownloader::httpxmldownloader() { nam = new QNetworkAccessManager(this); - QObject::connect(nam, SIGNAL(finished(QNetworkReply*)), - this, SLOT(finishedSlot(QNetworkReply*))); - /** - * TODO: - * connect readyRead() - * - */ - - qDebug() << "httpdownloader"; } QNetworkReply* httpxmldownloader::makeRequest(const QString& xmlurl) { return nam->get(QNetworkRequest(QUrl(xmlurl))); } -void httpxmldownloader::finishedSlot(QNetworkReply* reply) { - // no errors received - if (reply->error() == QNetworkReply::NoError) { - QString xml_doc(reply->readAll()); - qDebug() << xml_doc; - } else { - qDebug() << "Error reading from URL: " << reply->error(); - } +void httpxmldownloader::connectSlot(QObject* obj, const char* slot) { + QObject::connect(nam, SIGNAL(finished(QNetworkReply*)), + obj, slot); } diff --git a/src/httpxmldownloader.h b/src/httpxmldownloader.h index 6cfd1a7..c30e0f8 100644 --- a/src/httpxmldownloader.h +++ b/src/httpxmldownloader.h @@ -13,8 +13,7 @@ class httpxmldownloader : public QObject { public: httpxmldownloader(); QNetworkReply* makeRequest(const QString& xmlurl); -private slots: - void finishedSlot(QNetworkReply*); + void connectSlot(QObject* obj, const char* slot); }; #endif /* HTTPTEST_H_ */ diff --git a/src/main.cpp b/src/main.cpp index 365287e..3c16e17 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -187,18 +187,16 @@ int main(int argc, char *argv[]) { QList xsessions(XSession::readSessions(xSessionPath)); QList vsessions(VSession::readXmlDir(vSessionPath)); - /* add sessions from remote xml if possible */ + Dialog w; + httpxmldownloader httpxmldownloader; + httpxmldownloader.connectSlot(&w, SLOT(addSessionsAfterDownload(QNetworkReply*))); + if (!vSessionUrl.isEmpty()) { - vsessions.append(VSession::readXmlUrl(vSessionUrl, httpxmldownloader)); - } else { - if (debugMode) { - qDebug() << "empty URl given"; - } + // read xml and add items later + httpxmldownloader.makeRequest(vSessionUrl); } - Dialog w; - w.setTheme(); w.setWindowFlags(Qt::FramelessWindowHint); diff --git a/src/vsession.cpp b/src/vsession.cpp index 7e41b51..8134bc6 100644 --- a/src/vsession.cpp +++ b/src/vsession.cpp @@ -300,66 +300,21 @@ int VSession::type() const { return Session::VSESSION; } -QList VSession::readXmlUrl(const QString& xmlurl, httpxmldownloader& httpxmldownloader) { - QList retval; - - // create thread - //QThread thread; - - QNetworkReply* reply = httpxmldownloader.makeRequest(xmlurl); - - if (reply->isFinished()) {} - -// for (int i = 0; i < 5; i++) { -// qDebug() << reply->isRunning(); -// sleep(1); -// } -// -// //downloader.moveToThread(&thread); -// //thread.start(); -// -// -// if (debugMode) { -// qDebug() << "Made http request with: " << xmlurl; -// } -// -// QDomDocument doc; -// -// QString xml_doc(reply->readAll()); -// -// if (!doc.setContent(QString(reply->readAll()))) { -// // error in xml, return emtpy list -// return retval; -// } - -// // TODO: what dirName??# -// // copied from readXmlFile(...) -// QString dirName = "/loaded/from/url"; -// QDomElement settingsNode = doc.firstChildElement("settings"); -// for (QDomElement el(settingsNode.firstChildElement("eintrag")); -// !el.isNull(); -// el = el.nextSiblingElement("eintrag")) { -// QDomDocument dummy; -// dummy.appendChild(dummy.importNode(el, true)); -// VSession* e = new VSession; -// if (e->init(dummy.toString(), dirName)) { -// retval.append(e); -// } -// } - return retval; -} - QList VSession::readXmlFile(const QString& filepath) { QList retval; QDomDocument doc; QFile file(filepath); if (!file.open(QIODevice::ReadOnly)) { - // TODO: error message + if (debugMode) { + qDebug() << "Cannot read file: " << file.fileName(); + } return retval; } if (!doc.setContent(&file)) { - // TODO: error message + if (debugMode) { + qDebug() << "XML file not valid: " << file.fileName(); + } file.close(); return retval; } @@ -368,8 +323,8 @@ QList VSession::readXmlFile(const QString& filepath) { QString dirName(QFileInfo(filepath).dir().absolutePath()); QDomElement settingsNode = doc.firstChildElement("settings"); for (QDomElement el(settingsNode.firstChildElement("eintrag")); - !el.isNull(); - el = el.nextSiblingElement("eintrag")) { + !el.isNull(); + el = el.nextSiblingElement("eintrag")) { QDomDocument dummy; dummy.appendChild(dummy.importNode(el, true)); VSession* e = new VSession; diff --git a/src/vsession.h b/src/vsession.h index f13c377..3afcd2b 100644 --- a/src/vsession.h +++ b/src/vsession.h @@ -62,7 +62,7 @@ class VSession : public Session { static QList readXmlDir(const QString& path); static QList readXmlFile(const QString& filepath); - static QList readXmlUrl(const QString& xmlurl, httpxmldownloader& httpxmldownloader); + static void addSessionsAfterDownload(QNetworkReply* reply); private: QDomDocument doc_; -- cgit v1.2.3-55-g7522