2018-10-19 15:50:35 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2017-present, Status Research and Development GmbH.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "desktopnotification.h"
|
|
|
|
#include "bridge.h"
|
|
|
|
#include "eventdispatcher.h"
|
|
|
|
|
|
|
|
#include "libsnore/application.h"
|
|
|
|
#include "libsnore/snore.h"
|
|
|
|
|
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
#include "libsnore/snore_static_plugins.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QDebug>
|
|
|
|
|
2018-11-06 16:01:43 +00:00
|
|
|
Q_LOGGING_CATEGORY(NOTIFICATION, "RCTNotification")
|
|
|
|
|
2018-11-15 07:19:57 +00:00
|
|
|
#if defined(Q_OS_LINUX) || defined(Q_OS_WIN)
|
2018-10-19 15:50:35 +00:00
|
|
|
namespace SnorePlugin {}
|
|
|
|
|
|
|
|
|
|
|
|
using namespace SnorePlugin;
|
2018-11-15 07:19:57 +00:00
|
|
|
#if defined(Q_OS_LINUX)
|
2018-10-19 15:50:35 +00:00
|
|
|
Q_IMPORT_PLUGIN(Freedesktop)
|
2018-11-15 07:19:57 +00:00
|
|
|
#elif defined(Q_OS_WIN)
|
|
|
|
Q_IMPORT_PLUGIN(WindowsToast)
|
|
|
|
#endif
|
2018-10-19 15:50:35 +00:00
|
|
|
Q_IMPORT_PLUGIN(Snore)
|
|
|
|
|
|
|
|
static void loadSnoreResources()
|
|
|
|
{
|
|
|
|
// prevent multiple symbols
|
|
|
|
static const auto load = []() {
|
|
|
|
Q_INIT_RESOURCE(snore);
|
|
|
|
Q_INIT_RESOURCE(snore_notification);
|
|
|
|
};
|
|
|
|
load();
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_COREAPP_STARTUP_FUNCTION(loadSnoreResources)
|
2018-11-15 07:19:57 +00:00
|
|
|
#endif // defined(Q_OS_LINUX) || defined(Q_OS_WIN)
|
2018-10-19 15:50:35 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct RegisterQMLMetaType {
|
|
|
|
RegisterQMLMetaType() { qRegisterMetaType<DesktopNotification *>(); }
|
|
|
|
} registerMetaType;
|
|
|
|
|
|
|
|
const QString NewMessageAlert = QStringLiteral("NewMessage");
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
class DesktopNotificationPrivate {
|
|
|
|
public:
|
|
|
|
Bridge *bridge = nullptr;
|
|
|
|
Snore::Application snoreApp;
|
|
|
|
};
|
|
|
|
|
|
|
|
DesktopNotification::DesktopNotification(QObject *parent)
|
|
|
|
: QObject(parent), d_ptr(new DesktopNotificationPrivate) {
|
|
|
|
connect(qApp, &QGuiApplication::focusWindowChanged, this, [=](QWindow *focusWindow){
|
|
|
|
m_appHasFocus = (focusWindow != nullptr);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (Snore::SnoreCore::instance().pluginNames().isEmpty()) {
|
|
|
|
Snore::SnoreCore::instance().loadPlugins(Snore::SnorePlugin::Backend);
|
|
|
|
}
|
|
|
|
|
2018-11-06 16:01:43 +00:00
|
|
|
qCDebug(NOTIFICATION) << "DesktopNotification::DesktopNotification List of all loaded Snore plugins:"
|
|
|
|
<< Snore::SnoreCore::instance().pluginNames();
|
2018-10-19 15:50:35 +00:00
|
|
|
|
2018-11-14 17:10:54 +00:00
|
|
|
Snore::Icon icon(":/icon.png");
|
|
|
|
d_ptr->snoreApp = Snore::Application(QCoreApplication::applicationName(), icon);
|
2018-11-15 07:19:57 +00:00
|
|
|
d_ptr->snoreApp.hints().setValue("windows-app-id", "StatusIm.Status.Desktop.1");
|
2018-11-14 17:10:54 +00:00
|
|
|
d_ptr->snoreApp.addAlert(Snore::Alert(NewMessageAlert, icon));
|
|
|
|
|
2018-10-19 15:50:35 +00:00
|
|
|
Snore::SnoreCore::instance().registerApplication(d_ptr->snoreApp);
|
|
|
|
Snore::SnoreCore::instance().setDefaultApplication(d_ptr->snoreApp);
|
|
|
|
|
2018-11-06 16:01:43 +00:00
|
|
|
qCDebug(NOTIFICATION) << "DesktopNotification::DesktopNotification Current notification backend:"
|
|
|
|
<< Snore::SnoreCore::instance().primaryNotificationBackend();
|
2018-10-19 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DesktopNotification::~DesktopNotification() {
|
|
|
|
Snore::SnoreCore::instance().deregisterApplication(d_ptr->snoreApp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DesktopNotification::setBridge(Bridge *bridge) {
|
|
|
|
Q_D(DesktopNotification);
|
|
|
|
d->bridge = bridge;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DesktopNotification::moduleName() { return "DesktopNotification"; }
|
|
|
|
|
|
|
|
QList<ModuleMethod *> DesktopNotification::methodsToExport() {
|
|
|
|
return QList<ModuleMethod *>{};
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariantMap DesktopNotification::constantsToExport() { return QVariantMap(); }
|
|
|
|
|
2018-11-14 17:10:54 +00:00
|
|
|
void DesktopNotification::sendNotification(QString title, QString body, bool prioritary) {
|
2018-10-19 15:50:35 +00:00
|
|
|
Q_D(DesktopNotification);
|
2018-11-06 16:01:43 +00:00
|
|
|
qCDebug(NOTIFICATION) << "::sendNotification";
|
2018-10-19 15:50:35 +00:00
|
|
|
|
|
|
|
if (m_appHasFocus) {
|
2018-11-06 16:01:43 +00:00
|
|
|
qCDebug(NOTIFICATION) << "Not sending notification since an application window is active";
|
2018-10-19 15:50:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Snore::Notification notification(
|
2018-11-14 17:10:54 +00:00
|
|
|
d_ptr->snoreApp, d_ptr->snoreApp.alerts()[NewMessageAlert], title,
|
|
|
|
body, Snore::Icon::defaultIcon(),
|
|
|
|
prioritary ? Snore::Notification::Prioritys::High : Snore::Notification::Prioritys::Normal);
|
2018-10-19 15:50:35 +00:00
|
|
|
Snore::SnoreCore::instance().broadcastNotification(notification);
|
|
|
|
}
|
2018-10-19 20:15:01 +00:00
|
|
|
|
|
|
|
void DesktopNotification::setDockBadgeLabel(const QString label) {
|
|
|
|
Snore::SnoreCore::instance().setDockBadgeLabel(label);
|
|
|
|
}
|