applicationui.cpp Example File
headlesserviceui/src/applicationui.cpp
#include "applicationui.hpp"
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/LocaleHandler>
#include <QDebug>
#include <QTimer>
const QString ApplicationHeadless::m_author = "Example Inc.";
const QString ApplicationHeadless::m_appName = "HeadlesServiceApp";
const QString ApplicationHeadless::m_serviceStatus = "ServiceStatus";
const QString ApplicationHeadless::m_flashNumber = "FlashCount";
const QString ApplicationHeadless::m_remainingCount = "RemainingFlashCount";
const QString ApplicationHeadless::m_reset = "Reset";
using namespace bb::cascades;
ApplicationHeadless::ApplicationHeadless(bb::cascades::Application *app)
: QObject(app)
, m_remainingFlashCount(-1)
{
m_pTranslator = new QTranslator(this);
m_pLocaleHandler = new LocaleHandler(this);
QSettings settings(m_author, m_appName);
settings.sync();
settingsWatcher = new QFileSystemWatcher(this);
settingsWatcher->addPath(settings.fileName());
connect(settingsWatcher, SIGNAL(fileChanged(const QString&)), this, SLOT(settingsChanged(const QString&)));
QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
qml->setContextProperty("_app", this);
AbstractPane *root = qml->createRootObject<AbstractPane>();
app->setScene(root);
}
bool ApplicationHeadless::isServiceRunning()
{
qDebug() << "check for service running via qsettings...";
QSettings settings(m_author, m_appName);
if (settings.value("ServiceStatus").isNull()) {
qDebug() << "found null value for ServiceStatus key...";
} else {
QString status = settings.value("ServiceStatus").toString();
if (status == "running") {
settingsChanged("");
return true;
}
}
return false;
}
int ApplicationHeadless::flashCount()
{
QSettings settings(m_author, m_appName);
if (settings.contains(m_flashNumber)) {
return settings.value(m_flashNumber).toInt();
}
return 0;
}
void ApplicationHeadless::resetLED()
{
QSettings settings(m_author, m_appName);
settings.setValue(m_reset, true);
}
void ApplicationHeadless::setRemainingFlashCount(int x)
{
m_remainingFlashCount = x;
qDebug() << "emitting update signal flc";
Q_EMIT remainingFlashCountChanged();
}
int ApplicationHeadless::remainingFlashCount()
{
return m_remainingFlashCount;
}
void ApplicationHeadless::settingsChanged(const QString & path)
{
qDebug() << "updating flash remaining counter";
QSettings settings(m_author, m_appName);
setRemainingFlashCount(settings.value(m_remainingCount).toInt());
qDebug() << "remaining count: " << settings.value(m_remainingCount).toString();
}