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>
This commit is contained in:
Tomaz Canabrava 2013-06-06 11:31:55 -03:00
parent c1cf6c02a8
commit f55ae15707
5 changed files with 114 additions and 71 deletions

View file

@ -1,8 +1,13 @@
#include "subsurfacewebservices.h" #include "subsurfacewebservices.h"
#include "ui_subsurfacewebservices.h" #include "ui_subsurfacewebservices.h"
#include "../webservice.h"
#include <libxml/parser.h>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include <QNetworkReply> #include <QNetworkReply>
#include <QDebug> #include <QDebug>
#include <qdesktopservices.h>
SubsurfaceWebServices* SubsurfaceWebServices::instance() SubsurfaceWebServices* SubsurfaceWebServices::instance()
{ {
@ -15,45 +20,123 @@ SubsurfaceWebServices::SubsurfaceWebServices(QWidget* parent, Qt::WindowFlags f)
ui->setupUi(this); ui->setupUi(this);
connect(ui->buttonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(buttonClicked(QAbstractButton*))); connect(ui->buttonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(buttonClicked(QAbstractButton*)));
connect(ui->download, SIGNAL(clicked(bool)), this, SLOT(startDownload())); connect(ui->download, SIGNAL(clicked(bool)), this, SLOT(startDownload()));
ui->buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
} }
void SubsurfaceWebServices::buttonClicked(QAbstractButton* button) 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() void SubsurfaceWebServices::startDownload()
{ {
QUrl url("http://api.hohndel.org/api/dive/get/"); QUrl url("http://api.hohndel.org/api/dive/get/");
url.setQueryItems( QList<QPair<QString,QString> >() << qMakePair(QString("login"), ui->userID->text())); url.setQueryItems( QList<QPair<QString,QString> >() << qMakePair(QString("login"), ui->userID->text()));
qDebug() << url;
manager = new QNetworkAccessManager(this);
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QNetworkRequest request; QNetworkRequest request;
request.setUrl(url); request.setUrl(url);
request.setRawHeader("Accept", "text/xml"); request.setRawHeader("Accept", "text/xml");
reply = manager->get(request); 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(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() void SubsurfaceWebServices::downloadFinished()
{ {
ui->progressBar->setRange(0,1); ui->progressBar->setRange(0,1);
QByteArray result = reply->readAll(); downloadedData = reply->readAll();
qDebug() << result;
ui->download->setEnabled(true);
ui->status->setText(tr("Download Finished")); 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) void SubsurfaceWebServices::downloadError(QNetworkReply::NetworkError error)
{ {
ui->download->setEnabled(true);
ui->progressBar->setRange(0,1); ui->progressBar->setRange(0,1);
ui->status->setText(QString::number((int)QNetworkRequest::HttpStatusCodeAttribute)); 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() void SubsurfaceWebServices::runDialog()
{ {
show(); 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;
}

View file

@ -3,6 +3,7 @@
#include <QDialog> #include <QDialog>
#include <QNetworkReply> #include <QNetworkReply>
#include <libxml/tree.h>
namespace Ui{ namespace Ui{
class SubsurfaceWebServices; class SubsurfaceWebServices;
@ -23,9 +24,15 @@ private slots:
void downloadError(QNetworkReply::NetworkError error); void downloadError(QNetworkReply::NetworkError error);
private: private:
explicit SubsurfaceWebServices(QWidget* parent = 0, Qt::WindowFlags f = 0); void setStatusText(int status);
Ui::SubsurfaceWebServices *ui; 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; QNetworkReply *reply;
QNetworkAccessManager *manager;
QByteArray downloadedData;
}; };
#endif #endif

View file

@ -27,7 +27,7 @@
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
</property> </property>
<property name="standardButtons"> <property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Help|QDialogButtonBox::Ok</set> <set>QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Help</set>
</property> </property>
</widget> </widget>
</item> </item>

View file

@ -1,36 +1,16 @@
#include <libintl.h> #include <libintl.h>
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include <libsoup/soup.h> #include <libsoup/soup.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include "dive.h" #include "dive.h"
#include "divelist.h" #include "divelist.h"
#include "display-gtk.h" #include "display-gtk.h"
#include "file.h" #include "file.h"
#include "webservice.h"
struct dive_table gps_location_table; struct dive_table gps_location_table;
static gboolean merge_locations_into_dives(void); static gboolean merge_locations_into_dives(void);
enum {
DD_STATUS_OK,
DD_STATUS_ERROR_CONNECT,
DD_STATUS_ERROR_ID,
DD_STATUS_ERROR_PARSE,
};
static const gchar *download_dialog_status_text(const gint status)
{
switch (status) {
case DD_STATUS_ERROR_CONNECT:
return _("Connection Error: ");
case DD_STATUS_ERROR_ID:
return _("Invalid user identifier!");
case DD_STATUS_ERROR_PARSE:
return _("Cannot parse response!");
}
return _("Download Success!");
}
/* provides a state of the download dialog contents and the downloaded xml */ /* provides a state of the download dialog contents and the downloaded xml */
struct download_dialog_state { struct download_dialog_state {
GtkWidget *uid; GtkWidget *uid;
@ -73,42 +53,6 @@ gboolean webservice_request_user_xml(const gchar *user_id,
return ret; return ret;
} }
/* requires that there is a <download> or <error> tag under the <root> tag */
static void download_dialog_traverse_xml(xmlNodePtr node, guint *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;
}
}
}
static guint download_dialog_parse_response(gchar *xmldata, guint len)
{
xmlNodePtr root;
xmlDocPtr doc = xmlParseMemory(xmldata, len);
guint 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;
}
static void download_dialog_connect_cb(GtkWidget *w, gpointer data) static void download_dialog_connect_cb(GtkWidget *w, gpointer data)
{ {
struct download_dialog_state *state = (struct download_dialog_state *)data; struct download_dialog_state *state = (struct download_dialog_state *)data;

View file

@ -2,9 +2,18 @@
extern "C" { extern "C" {
#endif #endif
extern void webservice_download_dialog(void); //extern void webservice_download_dialog(void);
extern gboolean webservice_request_user_xml(const gchar *, gchar **, guint *, guint *); //extern bool webservice_request_user_xml(const gchar *, gchar **, unsigned int *, unsigned int *);
extern int divelogde_upload(char *fn, char **error); extern int divelogde_upload(char *fn, char **error);
extern unsigned int download_dialog_parse_response(char *xmldata, unsigned int len);
enum {
DD_STATUS_OK,
DD_STATUS_ERROR_CONNECT,
DD_STATUS_ERROR_ID,
DD_STATUS_ERROR_PARSE,
};
#ifdef __cplusplus #ifdef __cplusplus
} }