This is just a hack I used to test some concepts, and with an early version of Mobility:
Code:
#ifndef TESTMOBILITY_H
#define TESTMOBILITY_H
#include <QtGui/QMainWindow>
#include "ui_TestMobility.h"
#include <QNetworkSession>
#include <QList>
#include <QSslError>
#include <QSemaphore>
class QNetworkAccessManager;
class QNetworkRequest;
class QNetworkReply;
using namespace QtMobility;
class TestMobility : public QMainWindow {
Q_OBJECT
public:
TestMobility(QWidget *parent = 0);
~TestMobility();
signals:
void signalNetworkingCancelled();
private slots:
void slotDelayedInit();
void slotNetworkSessionOpened();
void slotCheckFinished();
void slotRequestFinished(QNetworkReply* reply);
void slotNetworkSessionError(QNetworkSession::SessionError error);
void slotSslError(QNetworkReply* reply, const QList<QSslError>& errors);
private:
Ui::TestMobility ui;
QNetworkAccessManager* mNetworkAccessManager;
QNetworkSession* mSession;
QNetworkReply* mReply;
QString mImei;
QSemaphore mSemaphore;
bool mHandled;
void requestFinished(QNetworkReply* reply);
};
#endif // TESTMOBILITY_H
Code:
#include "TestMobility.h"
#include "secrets.h"
#include <QtNetwork>
#include <QNetworkConfigurationManager>
#include <QSystemDeviceInfo>
#include <QNetworkAccessManager>
#include <QNetworkSession>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QSslCertificate>
#include <QCryptographicHash>
#include <QByteArray>
#include <QList>
#include <QVariant>
#include <QMessageBox>
#include <QTimer>
using namespace QtMobility;
TestMobility::TestMobility(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
QNetworkConfigurationManager manager;
QNetworkConfigurationManager::Capabilities cap = manager.capabilities();
bool startStop = cap & QNetworkConfigurationManager::CanStartAndStopInterfaces;
bool dirCon = cap & QNetworkConfigurationManager::DirectConnectionRouting;
bool sysSesSup = cap & QNetworkConfigurationManager::SystemSessionSupport;
bool alr = cap & QNetworkConfigurationManager::ApplicationLevelRoaming;
bool forced = cap & QNetworkConfigurationManager::ForcedRoaming;
bool dataStat = cap & QNetworkConfigurationManager::DataStatistics;
QString text;
// Get IMEI string (International Mobile Equipment Identity)
QSystemDeviceInfo info;
mImei = info.imei();
#ifdef __WINS__
if (mImei == "000000000000000") {
mImei = "354182030698607";
}
#endif
mNetworkAccessManager = new QNetworkAccessManager(this);
bool ok;
ok = connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(slotRequestFinished(QNetworkReply*)));
Q_ASSERT(ok);
ok = connect(mNetworkAccessManager, SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError>&)),
this, SLOT(slotSslError(QNetworkReply*, const QList<QSslError>&)));
Q_ASSERT(ok);
Q_UNUSED(ok);
QTimer::singleShot(0, this, SLOT(slotDelayedInit()));
}
void TestMobility::slotDelayedInit() {
QNetworkConfigurationManager manager;
const bool canStartIAP = (manager.capabilities()
& QNetworkConfigurationManager::CanStartAndStopInterfaces);
// Note: Should be calling updateConfigurations here. See QNetworkConfigurationManager::allConfigurations writeup.
QNetworkConfiguration cfg = manager.defaultConfiguration();
if (!cfg.isValid() || (!canStartIAP && cfg.state() != QNetworkConfiguration::Active)) {
QMessageBox::information(this, tr("TestMobility"), tr("Available Access Points not found."));
return;
}
if (cfg.type() == QNetworkConfiguration::UserChoice) {
QNetworkConfiguration::StateFlags flags = QNetworkConfiguration::Discovered | QNetworkConfiguration::Active;
QList<QNetworkConfiguration> allCfg = manager.allConfigurations(flags);
// Just take the first one
if (allCfg.count() > 0) {
// cfg = allCfg.at(0); // Works, but like the feedback of the prompt
}
}
mSession = new QNetworkSession(cfg, this);
bool ok = connect(mSession,
SIGNAL(error(QNetworkSession::SessionError)),
this,
SLOT(slotNetworkSessionError(QNetworkSession::SessionError)));
Q_ASSERT(ok);
ok = connect(mSession, SIGNAL(opened()), this, SLOT(slotNetworkSessionOpened()));
Q_ASSERT(ok);
ok = connect(this, SIGNAL(signalNetworkingCancelled()), qApp, SLOT(quit()));
Q_ASSERT(ok);
Q_UNUSED(ok);
mSession->open();
}
void TestMobility::slotNetworkSessionOpened() {
// Construct HTTP request
QString user("dhicks");
sigString.append(appId);
sigString.append(":");
sigString.append(user);
QByteArray byteResult = QCryptographicHash::hash(sigString.toAscii(), QCryptographicHash::Md5);
QString strResult(byteResult.toHex());
if (strResult.count() % 2 != 0) {
strResult = '0' + byteResult;
}
strResult = strResult.toLower();
// Construct JSON string
QString json;
json.append("&json=");
json.append("{\"filter\":{},");
json.append("\"content\":{");
json.append("\"patients\":[");
json.append("{\"interchangeSerial\":\"Patient12345\",");
json.append("\"interchangePatientId\":3456,");
json.append("\"display\":\"William\",");
json.append("\"deleted\":0,");
json.append("\"timezone\":\"CST\"}");
json.append("]}");
json.append("}");
// Construct request string
QString urlString;
urlString.append(ServerUrl);
urlString.append("?user=");
urlString.append(user);
urlString.append(strResult);
urlString.append(json);
QUrl url(urlString);
QNetworkRequest request(url);
// request.setSslConfiguration(QSslConfiguration::defaultConfiguration());
if (QSslConfiguration::defaultConfiguration().isNull()) {
bool nullDefault = true;
}
QByteArray data;
mReply = mNetworkAccessManager ->post(request, data);
mHandled = false;
mSemaphore.release();
QTimer::singleShot(10000, this, SLOT(slotCheckFinished()));
}
void TestMobility::slotCheckFinished() {
mSemaphore.acquire();
// Note that we must check mHandled first, since mReply could have been deleted
if (mHandled == false && mReply ->isFinished()) {
mHandled = true;
mSemaphore.release();
requestFinished(mReply);
}
else {
mSemaphore.release();
}
}
void TestMobility::slotRequestFinished(QNetworkReply* reply) {
mSemaphore.acquire();
if (mHandled == false) {
mHandled == true;
mSemaphore.release();
requestFinished(reply);
}
else {
mSemaphore.release();
}
}
void TestMobility::requestFinished(QNetworkReply* reply) {
QNetworkRequest request = reply ->request();
QNetworkReply::NetworkError error = reply ->error();
if (error != QNetworkReply::NoError) {
Q_ASSERT(false);
}
int status = reply ->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
QVariant encrypted = reply ->attribute(QNetworkRequest::ConnectionEncryptedAttribute);
QSslConfiguration config = request.sslConfiguration();
QSslCertificate cert = config.peerCertificate();
ui.mLabel2 ->setText(QString("HTTP status code = %1").arg(status));
// Get return data
QByteArray result;
result = reply ->readAll();
if (result.count() == 0) {
ui.mLabel ->setText("Zero length result");
}
else {
QString resultString(result);
// Enable better word wrap
resultString = resultString.replace(':', ": ");
resultString = resultString.replace(',', ", ");
resultString.append("\nEncrypted = ");
resultString.append(encrypted.toBool() ? "true" : "false");
resultString.append("\nSSL config is null = ");
resultString.append(config.isNull() ? "true" : "false");
resultString.append("\nProtocol = ");
resultString.append(QString::number(config.protocol()));
resultString.append("\nCert serial number = ");
resultString.append(QString(cert.serialNumber()));
resultString.append("\nCert expire date = ");
resultString.append(cert.expiryDate().toString());
ui.mLabel ->setWordWrap(true);
ui.mLabel ->setText(resultString);
}
reply ->deleteLater();
}
void TestMobility::slotSslError(QNetworkReply* reply, const QList<QSslError>& errors) {
QString message;
message.append("SSL error");
for (int i = 0; i < errors.count(); i++) {
message.append("\n");
message.append(errors.at(i).errorString());
}
}
void TestMobility::slotNetworkSessionError(QNetworkSession::SessionError error) {
if (error == QNetworkSession::UnknownSessionError) {
if (mSession->state() == QNetworkSession::Connecting) {
QMessageBox msgBox(qobject_cast<QWidget *>(parent()));
msgBox.setText("This application requires network access to function.");
msgBox.setInformativeText("Press Cancel to quit the application.");
msgBox.setIcon(QMessageBox::Information);
msgBox.setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Retry);
int ret = msgBox.exec();
if (ret == QMessageBox::Retry) {
QTimer::singleShot(0, mSession, SLOT(open()));
} else if (ret == QMessageBox::Cancel) {
emit signalNetworkingCancelled();
}
}
} else if (error == QNetworkSession::SessionAbortedError) {
QMessageBox msgBox(qobject_cast<QWidget *>(parent()));
msgBox.setText("Out of range of network.");
msgBox.setInformativeText("Move back into range and press Retry, or press Cancel to quit the application.");
msgBox.setIcon(QMessageBox::Warning);
msgBox.setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Retry);
int ret = msgBox.exec();
if (ret == QMessageBox::Retry) {
QTimer::singleShot(0, mSession, SLOT(open()));
} else if (ret == QMessageBox::Cancel) {
emit signalNetworkingCancelled();
}
}
}
TestMobility::~TestMobility()
{
}