diff options
author | Tomaz Canabrava <tcanabrava@kde.org> | 2013-06-06 11:31:55 -0300 |
---|---|---|
committer | Tomaz Canabrava <tcanabrava@kde.org> | 2013-06-06 11:31:55 -0300 |
commit | f55ae1570738ec4e890ec35504e2c1e5c81364cc (patch) | |
tree | 7185ea93b9b90ab9b0bc3b0a406d6f0e5343396f /qt-ui | |
parent | c1cf6c02a8a82296840d3a4c6948bc04a5bb5e51 (diff) | |
download | subsurface-f55ae1570738ec4e890ec35504e2c1e5c81364cc.tar.gz |
Hoocked up the buttons and the parsing of the XML.
The XML is now being correctly parsed, Clicking on Help
will open the browser pointing to the api site, and clicking
on cancel will cancel the download.
Clicking on Apply still doesn't apply, but that's next. :)
Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
Diffstat (limited to 'qt-ui')
-rw-r--r-- | qt-ui/subsurfacewebservices.cpp | 99 | ||||
-rw-r--r-- | qt-ui/subsurfacewebservices.h | 11 | ||||
-rw-r--r-- | qt-ui/subsurfacewebservices.ui | 2 |
3 files changed, 101 insertions, 11 deletions
diff --git a/qt-ui/subsurfacewebservices.cpp b/qt-ui/subsurfacewebservices.cpp index 48e56aa30..c6314bf03 100644 --- a/qt-ui/subsurfacewebservices.cpp +++ b/qt-ui/subsurfacewebservices.cpp @@ -1,8 +1,13 @@ #include "subsurfacewebservices.h" #include "ui_subsurfacewebservices.h" +#include "../webservice.h" + +#include <libxml/parser.h> + #include <QNetworkAccessManager> #include <QNetworkReply> #include <QDebug> +#include <qdesktopservices.h> SubsurfaceWebServices* SubsurfaceWebServices::instance() { @@ -15,45 +20,123 @@ SubsurfaceWebServices::SubsurfaceWebServices(QWidget* parent, Qt::WindowFlags f) ui->setupUi(this); connect(ui->buttonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(buttonClicked(QAbstractButton*))); connect(ui->download, SIGNAL(clicked(bool)), this, SLOT(startDownload())); + ui->buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false); } void SubsurfaceWebServices::buttonClicked(QAbstractButton* button) { - + ui->buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false); + switch(ui->buttonBox->buttonRole(button)){ + case QDialogButtonBox::ApplyRole: + qDebug() << "Apply Clicked"; + break; + case QDialogButtonBox::RejectRole: + manager->deleteLater(); + reply->deleteLater(); + ui->progressBar->setMaximum(1); + break; + case QDialogButtonBox::HelpRole: + QDesktopServices::openUrl(QUrl("http://api.hohndel.org")); + break; + default: + break; + } } void SubsurfaceWebServices::startDownload() { QUrl url("http://api.hohndel.org/api/dive/get/"); url.setQueryItems( QList<QPair<QString,QString> >() << qMakePair(QString("login"), ui->userID->text())); - qDebug() << url; - - QNetworkAccessManager *manager = new QNetworkAccessManager(this); + + manager = new QNetworkAccessManager(this); QNetworkRequest request; request.setUrl(url); request.setRawHeader("Accept", "text/xml"); reply = manager->get(request); - ui->progressBar->setRange(0,0); + ui->progressBar->setRange(0,0); // this makes the progressbar do an 'infinite spin' + ui->download->setEnabled(false); + ui->buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false); + connect(reply, SIGNAL(finished()), this, SLOT(downloadFinished())); - connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(downloadError(QNetworkReply::NetworkError))); + connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), + this, SLOT(downloadError(QNetworkReply::NetworkError))); } void SubsurfaceWebServices::downloadFinished() { ui->progressBar->setRange(0,1); - QByteArray result = reply->readAll(); - qDebug() << result; + downloadedData = reply->readAll(); + + ui->download->setEnabled(true); ui->status->setText(tr("Download Finished")); + + uint resultCode = download_dialog_parse_response(downloadedData); + setStatusText(resultCode); + if (resultCode == DD_STATUS_OK){ + ui->buttonBox->button(QDialogButtonBox::Apply)->setEnabled(true); + } + manager->deleteLater(); + reply->deleteLater(); } void SubsurfaceWebServices::downloadError(QNetworkReply::NetworkError error) { + ui->download->setEnabled(true); ui->progressBar->setRange(0,1); ui->status->setText(QString::number((int)QNetworkRequest::HttpStatusCodeAttribute)); + manager->deleteLater(); + reply->deleteLater(); } +void SubsurfaceWebServices::setStatusText(int status) +{ + QString text; + switch (status) { + case DD_STATUS_ERROR_CONNECT: text = tr("Connection Error: "); break; + case DD_STATUS_ERROR_ID: text = tr("Invalid user identifier!"); break; + case DD_STATUS_ERROR_PARSE: text = tr("Cannot parse response!"); break; + case DD_STATUS_OK: text = tr("Download Success!"); break; + } + ui->status->setText(text); +} void SubsurfaceWebServices::runDialog() { show(); } + +/* requires that there is a <download> or <error> tag under the <root> tag */ +void SubsurfaceWebServices::download_dialog_traverse_xml(xmlNodePtr node, unsigned int *download_status) +{ + xmlNodePtr cur_node; + for (cur_node = node; cur_node; cur_node = cur_node->next) { + if ((!strcmp((const char *)cur_node->name, (const char *)"download")) && + (!strcmp((const char *)xmlNodeGetContent(cur_node), (const char *)"ok"))) { + *download_status = DD_STATUS_OK; + return; + } else if (!strcmp((const char *)cur_node->name, (const char *)"error")) { + *download_status = DD_STATUS_ERROR_ID; + return; + } + } +} + +unsigned int SubsurfaceWebServices::download_dialog_parse_response(const QByteArray& xml) +{ + xmlNodePtr root; + xmlDocPtr doc = xmlParseMemory(xml.data(), xml.length()); + unsigned int status = DD_STATUS_ERROR_PARSE; + + if (!doc) + return DD_STATUS_ERROR_PARSE; + root = xmlDocGetRootElement(doc); + if (!root) { + status = DD_STATUS_ERROR_PARSE; + goto end; + } + if (root->children) + download_dialog_traverse_xml(root->children, &status); + end: + xmlFreeDoc(doc); + return status; +} diff --git a/qt-ui/subsurfacewebservices.h b/qt-ui/subsurfacewebservices.h index ffcc28f82..9e85db155 100644 --- a/qt-ui/subsurfacewebservices.h +++ b/qt-ui/subsurfacewebservices.h @@ -3,6 +3,7 @@ #include <QDialog> #include <QNetworkReply> +#include <libxml/tree.h> namespace Ui{ class SubsurfaceWebServices; @@ -23,9 +24,15 @@ private slots: void downloadError(QNetworkReply::NetworkError error); private: - explicit SubsurfaceWebServices(QWidget* parent = 0, Qt::WindowFlags f = 0); - Ui::SubsurfaceWebServices *ui; + void setStatusText(int status); + void download_dialog_traverse_xml(xmlNodePtr node, unsigned int *download_status); + unsigned int download_dialog_parse_response(const QByteArray& length); + + explicit SubsurfaceWebServices(QWidget* parent = 0, Qt::WindowFlags f = 0); + Ui::SubsurfaceWebServices *ui; QNetworkReply *reply; + QNetworkAccessManager *manager; + QByteArray downloadedData; }; #endif
\ No newline at end of file diff --git a/qt-ui/subsurfacewebservices.ui b/qt-ui/subsurfacewebservices.ui index e1232dc38..899eea909 100644 --- a/qt-ui/subsurfacewebservices.ui +++ b/qt-ui/subsurfacewebservices.ui @@ -27,7 +27,7 @@ <enum>Qt::Horizontal</enum> </property> <property name="standardButtons"> - <set>QDialogButtonBox::Cancel|QDialogButtonBox::Help|QDialogButtonBox::Ok</set> + <set>QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Help</set> </property> </widget> </item> |