#include "PushNotificationService.hpp"
#include <QDebug>
using namespace bb::network;
PushNotificationService::PushNotificationService(QObject *parent)
: QObject(parent)
, m_pushService(0)
{
}
void PushNotificationService::createSession()
{
initializePushService();
if (m_pushService->hasConnection()){
m_pushService->createSession();
} else {
emit noPushServiceConnection();
}
}
void PushNotificationService::createChannel()
{
if (m_pushService->hasConnection()){
m_pushService->createChannel(m_configurationService.configuration().ppgUrl());
} else {
emit noPushServiceConnection();
}
}
void PushNotificationService::initializePushService()
{
const Configuration config = m_configurationService.configuration();
if (!m_pushService || (m_previousApplicationId != config.providerApplicationId())) {
if (m_pushService) {
QObject::disconnect(m_pushService, SIGNAL(createSessionCompleted(const bb::network::PushStatus&)),
this, SIGNAL(createSessionCompleted(const bb::network::PushStatus&)));
QObject::disconnect(m_pushService, SIGNAL(createChannelCompleted(const bb::network::PushStatus&, const QString)),
this, SIGNAL(createChannelCompleted(const bb::network::PushStatus&, const QString)));
QObject::disconnect(m_pushService, SIGNAL(destroyChannelCompleted(const bb::network::PushStatus&)),
this, SIGNAL(destroyChannelCompleted(const bb::network::PushStatus&)));
QObject::disconnect(m_pushService, SIGNAL(registerToLaunchCompleted(const bb::network::PushStatus&)),
this, SIGNAL(registerToLaunchCompleted(const bb::network::PushStatus&)));
QObject::disconnect(m_pushService, SIGNAL(unregisterFromLaunchCompleted(const bb::network::PushStatus&)),
this, SIGNAL(unregisterFromLaunchCompleted(const bb::network::PushStatus&)));
QObject::disconnect(m_pushService, SIGNAL(simChanged()),
this, SIGNAL(simChanged()));
QObject::disconnect(m_pushService, SIGNAL(pushTransportReady(bb::network::PushCommand::Type)),
this, SIGNAL(pushTransportReady(bb::network::PushCommand::Type)));
QObject::disconnect(&m_registerService, SIGNAL(piRegistrationCompleted(int, const QString)),
this, SIGNAL(piRegistrationCompleted(int, const QString)));
QObject::disconnect(&m_unregisterService, SIGNAL(piDeregistrationCompleted(int, const QString)),
this, SIGNAL(piDeregistrationCompleted(int, const QString)));
delete m_pushService;
m_pushService = NULL;
}
m_previousApplicationId = config.providerApplicationId();
m_pushService = new PushService(config.providerApplicationId(), INVOKE_TARGET_KEY_PUSH, this);
QObject::connect(m_pushService, SIGNAL(createSessionCompleted(const bb::network::PushStatus&)),
this, SIGNAL(createSessionCompleted(const bb::network::PushStatus&)));
QObject::connect(m_pushService, SIGNAL(createChannelCompleted(const bb::network::PushStatus&, const QString)),
this, SIGNAL(createChannelCompleted(const bb::network::PushStatus&, const QString)));
QObject::connect(m_pushService, SIGNAL(destroyChannelCompleted(const bb::network::PushStatus&)),
this, SIGNAL(destroyChannelCompleted(const bb::network::PushStatus&)));
QObject::connect(m_pushService, SIGNAL(registerToLaunchCompleted(const bb::network::PushStatus&)),
this, SIGNAL(registerToLaunchCompleted(const bb::network::PushStatus&)));
QObject::connect(m_pushService, SIGNAL(unregisterFromLaunchCompleted(const bb::network::PushStatus&)),
this, SIGNAL(unregisterFromLaunchCompleted(const bb::network::PushStatus&)));
QObject::connect(m_pushService, SIGNAL(simChanged()),
this, SIGNAL(simChanged()));
QObject::connect(m_pushService, SIGNAL(pushTransportReady(bb::network::PushCommand::Type)),
this, SIGNAL(pushTransportReady(bb::network::PushCommand::Type)));
QObject::connect(&m_registerService, SIGNAL(piRegistrationCompleted(int, const QString)),
this, SIGNAL(piRegistrationCompleted(int, const QString)));
QObject::connect(&m_unregisterService, SIGNAL(piDeregistrationCompleted(int, const QString)),
this, SIGNAL(piDeregistrationCompleted(int, const QString)));
}
}
void PushNotificationService::registerToLaunch()
{
m_pushService->registerToLaunch();
}
void PushNotificationService::unregisterFromLaunch()
{
m_pushService->unregisterFromLaunch();
}
User PushNotificationService::getCurrentlyRegisteredUser()
{
return m_unregisterService.getCurrentlyRegisteredUser();
}
void PushNotificationService::subscribeToPushInitiator(const User &user, const QString &token)
{
m_registerService.subscribeToPushInitiator(user,token);
}
void PushNotificationService::unsubscribeFromPushInitiator(const User &user)
{
m_unregisterService.unsubscribeFromPushInitiator(user);
}
void PushNotificationService::destroyChannel()
{
if (m_pushService->hasConnection()){
m_pushService->destroyChannel();
} else {
emit noPushServiceConnection();
}
}
void PushNotificationService::acceptPush(const QString &payloadId)
{
m_pushService->acceptPush(payloadId);
}
void PushNotificationService::rejectPush(const QString &payloadId)
{
m_pushService->rejectPush(payloadId);
}
bool PushNotificationService::checkForDuplicatePush(const PushHistoryItem &pushHistoryItem)
{
return m_pushHandler.checkForDuplicate(pushHistoryItem);
}
int PushNotificationService::savePush(const Push &push)
{
return m_pushHandler.save(push);
}
Push PushNotificationService::push(int pushSeqNum)
{
return m_pushHandler.push(pushSeqNum);
}
QVariantList PushNotificationService::pushes()
{
return m_pushHandler.pushes();
}
bool PushNotificationService::removePush(int pushSeqNum)
{
return m_pushHandler.remove(pushSeqNum);
}
bool PushNotificationService::removeAllPushes()
{
bool pushesRemoved = m_pushHandler.removeAll();
emit allPushesRemoved();
return pushesRemoved;
}
bool PushNotificationService::removeAllPushHistory()
{
return m_pushHandler.removeAllPushHistory();
}
bool PushNotificationService::markPushAsRead(int pushSeqNum)
{
return m_pushHandler.markAsRead(pushSeqNum);
}
bool PushNotificationService::markAllPushesAsRead()
{
return m_pushHandler.markAllAsRead();
}
void PushNotificationService::handleSimChange()
{
const Configuration config = m_configurationService.configuration();
const User user = getCurrentlyRegisteredUser();
removeAllPushes();
removeAllPushHistory();
if (!config.pushInitiatorUrl().isEmpty() && !user.userId().isEmpty() && !user.password().isEmpty()) {
m_simChangeUnregisterService.unsubscribeFromPushInitiator(user);
m_simChangeUnregisterService.removeUser();
}
}