RegisterService.cpp Example File
pushCollector/10.0/pushCollector/src/service/RegisterService.cpp
#include "RegisterService.hpp"
#include <QtCore/QDebug>
#include <bps/bps.h>
#include <bps/deviceinfo.h>
RegisterService::RegisterService(QObject *parent)
: QObject(parent)
, m_reply(0)
{
connect(&m_accessManager,SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError>& )),
this, SLOT(onSslErrors(QNetworkReply*, const QList<QSslError>&)));
}
void RegisterService::subscribeToPushInitiator(const User& user, const QString& token)
{
m_currentUser = user;
const Configuration config = m_configurationService.configuration();
QUrl url(config.pushInitiatorUrl() + "/subscribe");
url.addQueryItem("appid",config.providerApplicationId());
url.addQueryItem("address",token);
url.addQueryItem("osversion",deviceVersion());
url.addQueryItem("model",deviceModel());
url.addQueryItem("username",user.userId());
url.addQueryItem("password",user.password());
if (config.usingPublicPushProxyGateway()) {
url.addQueryItem("type","public");
} else {
url.addQueryItem("type","bds");
}
qDebug() << "URL: " << url;
m_reply = m_accessManager.get(QNetworkRequest(url));
connect(m_reply, SIGNAL(finished()), this, SLOT(httpFinished()));
}
void RegisterService::httpFinished()
{
qDebug() << "httpFinished called";
int code = -1;
QString description;
if (m_reply->error() == QNetworkReply::NoError) {
const QString returnCode = QString::fromUtf8(m_reply->readAll());
qDebug() << "returnCode: " << returnCode;
if (returnCode == "rc=200") {
m_userDAO.save(m_currentUser);
code = 200;
} else if (returnCode == "rc=10001") {
code = 10001;
description = tr("Error: The token from the create channel was null, empty, or longer than 40 characters in length.");
} else if (returnCode == "rc=10011") {
code = 10011;
description = tr("Error: The OS version or device model of the BlackBerry was invalid.");
} else if (returnCode == "rc=10002") {
code = 10002;
description = tr("Error: The application ID specified in the configuration settings could not be found, or it was found to be inactive or expired.");
} else if (returnCode == "rc=10020") {
code = 10020;
description = tr("Error: The subscriber ID generated by the Push Initiator (based on the username and password specified) was null or empty, "
"longer than 42 characters in length, or matched the 'push_all' keyword.");
} else if (returnCode == "rc=10025") {
code = 10025;
description = tr("Error: The Push Initiator application has the bypass subscription flag set to true (so no subscribe is allowed).");
} else if (returnCode == "rc=10026") {
code = 10026;
description = tr("Error: The username or password specified was incorrect.");
} else if (returnCode == "rc=10027") {
code = 10027;
description = tr("Error: A CPSubscriptionFailureException was thrown by the onSubscribeSuccess method of the implementation "
"being used of the ContentProviderSubscriptionService interface.");
} else if (returnCode == "rc=10028") {
code = 10028;
description = tr("Error: The type specified was null, empty, or not one of 'public' or 'bds', or invalid for the push application type.");
} else if (returnCode == "rc=-9999") {
code = -9999;
description = tr("Error: General error (i.e. rc=-9999).");
} else {
description = tr("Error: Unknown error code: %0.").arg(returnCode);
}
} else {
qDebug() << "network error";
code = m_reply->error();
description = m_reply->errorString();
}
emit piRegistrationCompleted(code, description);
m_reply->deleteLater();
}
void RegisterService::onSslErrors(QNetworkReply * reply, const QList<QSslError> & errors)
{
qDebug() << "onSslErrors called";
reply->ignoreSslErrors(errors);
}
QString RegisterService::deviceVersion() const
{
QString deviceVersion;
if (bps_initialize() == BPS_SUCCESS) {
qDebug() << "bps initialized";
deviceinfo_details_t *deviceDetails = 0;
if (deviceinfo_get_details(&deviceDetails) == BPS_SUCCESS) {
deviceVersion = deviceinfo_details_get_device_os_version(deviceDetails);
deviceinfo_free_details(&deviceDetails);
} else {
qDebug() << "error retrieving device details";
}
bps_shutdown();
} else {
qDebug() << "error initializing bps";
}
return deviceVersion;
}
QString RegisterService::deviceModel() const
{
QString deviceModel;
if (bps_initialize() == BPS_SUCCESS) {
qDebug() << "bps initialized";
deviceinfo_details_t *deviceDetails = 0;
if (deviceinfo_get_details(&deviceDetails) == BPS_SUCCESS) {
deviceModel = deviceinfo_details_get_hardware_id(deviceDetails);
deviceinfo_free_details(&deviceDetails);
} else {
qDebug() << "error retrieving device details";
}
bps_shutdown();
} else {
qDebug() << "error initializing bps";
}
return deviceModel;
}