app.cpp Example File
mavenartifacts/src/app.cpp
#include "app.hpp"
#include "ArtifactRequest.hpp"
#include <bb/cascades/AbstractPane>
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/data/JsonDataAccess>
#include <bb/utility/i18n/RelativeDateFormatter>
using namespace bb::cascades;
using namespace bb::data;
App::App(QObject *parent)
: QObject(parent)
, m_active(false)
, m_error(false)
, m_model(new GroupDataModel(QStringList() << "id", this))
{
m_model->setGrouping(ItemGrouping::None);
QmlDocument* qml = QmlDocument::create("asset:///main.qml").parent(this);
qml->setContextProperty("_artifactline", this);
AbstractPane* root = qml->createRootObject<AbstractPane>();
Application::instance()->setScene(root);
}
void App::reset()
{
m_error = false;
m_errorMessage.clear();
emit statusChanged();
}
void App::requestArtifact(const QString &artifactName)
{
if (m_active)
return;
const QStringList list = artifactName.split(QRegExp("\\s+"), QString::SkipEmptyParts);
if (list.isEmpty()) {
m_errorMessage = "please enter a valid artifact name";
m_error = true;
emit statusChanged();
return;
}
const QString artifactId = list.first();
ArtifactRequest* request = new ArtifactRequest(this);
bool ok = connect(request, SIGNAL(complete(QString, bool)), this, SLOT(onArtifactsline(QString, bool)));
Q_ASSERT(ok);
Q_UNUSED(ok);
request->requestArtifactline(artifactId);
m_active = true;
emit activeChanged();
}
void App::onArtifactsline(const QString &info, bool success)
{
ArtifactRequest *request = qobject_cast<ArtifactRequest*>(sender());
if (success) {
parseResponse(info);
emit artifactsLoaded();
} else {
m_errorMessage = info;
m_error = true;
emit statusChanged();
}
m_active = false;
emit activeChanged();
request->deleteLater();
}
void App::parseResponse(const QString &response)
{
m_model->clear();
if (response.trimmed().isEmpty())
return;
JsonDataAccess dataAccess;
const QVariantMap variant = dataAccess.loadFromBuffer(response).toMap();
const QVariantList feed = variant["response"].toMap()["docs"].toList();
foreach (const QVariant &artifact, feed) {
m_model->insert(artifact.toMap());
}
}
bool App::active() const
{
return m_active;
}
bool App::error() const
{
return m_error;
}
QString App::errorMessage() const
{
return m_errorMessage;
}
QString App::dateFromTimestamp(const QString ×tamp) {
QDateTime date;
date.setMSecsSinceEpoch(timestamp.toLongLong());
return date.toString();
}
bb::cascades::DataModel* App::model() const
{
return m_model;
}