This repository has been archived on 2023-11-19. You can view files and clone it, but cannot push or open issues or pull requests.
goober-bb10/src/applicationui.cpp

145 lines
4.5 KiB
C++

/*
* Copyright (c) 2011-2015 BlackBerry Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "applicationui.hpp"
#include "Pnut.h"
#include "ActiveFrameQML.h"
#include "netimagemanager.h"
#include "netimagetracker.h"
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/LocaleHandler>
#include <bb/ApplicationInfo>
#include <bb/system/Clipboard>
#include <bb/system/InvokeRequest>
#include <bb/system/InvokeManager>
#include <bb/system/InvokeTargetReply>
using namespace bb::cascades;
ApplicationUI::ApplicationUI() :
QObject()
{
// setup the invoke manager
invokeManager = new bb::system::InvokeManager(this);
connect(invokeManager, SIGNAL(invoked(const bb::system::InvokeRequest&)),
this, SLOT(onInvoke(const bb::system::InvokeRequest&)));
// prepare the localization
m_pTranslator = new QTranslator(this);
m_pLocaleHandler = new LocaleHandler(this);
bool res = QObject::connect(m_pLocaleHandler, SIGNAL(systemLanguageChanged()), this, SLOT(onSystemLanguageChanged()));
// This is only available in Debug builds
Q_ASSERT(res);
// Since the variable is not used in the app, this is added to avoid a
// compiler warning
Q_UNUSED(res);
// initial load
onSystemLanguageChanged();
qmlRegisterType<Pnut>("com.monkeystew.pnut", 1, 0, "Pnut");
qmlRegisterType<QTimer> ("com.monkeystew.qtimer", 1, 0, "QTimer");
qmlRegisterType<NetImageTracker>("com.netimage", 1, 0, "NetImageTracker");
qmlRegisterType<NetImageManager>("com.netimage", 1, 0, "NetImageManager");
m_appSettings = new QSettings("Morgan McMillian", "Goober");
// Create scene document from main.qml asset, the parent is set
// to ensure the document gets destroyed properly at shut down.
QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
qml->setContextProperty("_app", this);
ActiveFrameQML *activeFrame = new ActiveFrameQML();
Application::instance()->setCover(activeFrame);
qml->setContextProperty("_activeFrame", activeFrame);
// Create root object for the UI
AbstractPane *root = qml->createRootObject<AbstractPane>();
// Set created root object as the application scene
Application::instance()->setScene(root);
}
QVariant ApplicationUI::setting(const QString& key)
{
return m_appSettings->value(key);
}
void ApplicationUI::setSetting(const QString& key, const QString& value)
{
m_appSettings->setValue(key, value);
}
void ApplicationUI::onSystemLanguageChanged()
{
QCoreApplication::instance()->removeTranslator(m_pTranslator);
// Initiate, load and install the application translation files.
QString locale_string = QLocale().name();
QString file_name = QString("Goober_%1").arg(locale_string);
if (m_pTranslator->load(file_name, "app/native/qm")) {
QCoreApplication::instance()->installTranslator(m_pTranslator);
}
}
QString ApplicationUI::appversion()
{
bb::ApplicationInfo appinfo;
return appinfo.version();
}
void ApplicationUI::copyText(QString text)
{
bb::system::Clipboard clipboard;
clipboard.clear();
QByteArray textstr;
textstr.append(text.toUtf8());
clipboard.insert("text/plain", textstr);
}
void ApplicationUI::onInvoke(const bb::system::InvokeRequest& request)
{
if (request.action() == "bb.action.SHARE")
{
emit createPost(request.data());
}
}
void ApplicationUI::showImage(const QString& filename)
{
qDebug() << "showImage called!";
bb::system::InvokeManager manager;
bb::system::InvokeRequest request;
request.setUri(QUrl::fromLocalFile(filename));
request.setTarget("sys.pictures.card.previewer");
request.setAction("bb.action.VIEW");
bb::system::InvokeTargetReply *targetReply = manager.invoke(request);
manager.setParent(this);
if (targetReply == NULL) {
qDebug() << "InvokeTargetReply is NULL: targetReply = " << targetReply;
} else {
targetReply->setParent(this);
}
qDebug() << "targetReply = " << targetReply;
}