2020-05-29 18:06:40 +00:00
|
|
|
|
2016-10-02 09:30:28 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-10-02 09:30:28 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-10-02 09:30:28 +00:00
|
|
|
*/
|
|
|
|
#include "platform/linux/notifications_manager_linux.h"
|
|
|
|
|
2020-06-22 01:28:23 +00:00
|
|
|
#include "window/notifications_utilities.h"
|
2020-06-16 21:23:39 +00:00
|
|
|
#include "base/platform/base_platform_info.h"
|
2021-02-28 02:34:41 +00:00
|
|
|
#include "base/platform/linux/base_linux_glibmm_helper.h"
|
|
|
|
#include "base/platform/linux/base_linux_dbus_utilities.h"
|
2020-01-30 18:41:24 +00:00
|
|
|
#include "platform/linux/specific_linux.h"
|
2020-06-18 18:04:16 +00:00
|
|
|
#include "core/application.h"
|
|
|
|
#include "core/core_settings.h"
|
2018-01-25 14:19:14 +00:00
|
|
|
#include "history/history.h"
|
2020-06-19 12:59:31 +00:00
|
|
|
#include "main/main_session.h"
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2021-05-03 10:22:58 +00:00
|
|
|
#include "base/weak_ptr.h"
|
2016-10-03 08:56:03 +00:00
|
|
|
|
2020-01-24 12:26:32 +00:00
|
|
|
#include <QtCore/QVersionNumber>
|
2021-03-12 04:55:31 +00:00
|
|
|
#include <QtGui/QGuiApplication>
|
2020-08-01 11:03:17 +00:00
|
|
|
|
2021-02-28 02:34:41 +00:00
|
|
|
#include <glibmm.h>
|
|
|
|
#include <giomm.h>
|
2019-12-29 15:41:45 +00:00
|
|
|
|
2016-10-02 09:30:28 +00:00
|
|
|
namespace Platform {
|
|
|
|
namespace Notifications {
|
2016-10-03 08:56:03 +00:00
|
|
|
namespace {
|
|
|
|
|
2020-01-29 09:44:37 +00:00
|
|
|
constexpr auto kService = "org.freedesktop.Notifications"_cs;
|
|
|
|
constexpr auto kObjectPath = "/org/freedesktop/Notifications"_cs;
|
2019-12-29 15:41:45 +00:00
|
|
|
constexpr auto kInterface = kService;
|
2020-03-03 08:52:05 +00:00
|
|
|
constexpr auto kPropertiesInterface = "org.freedesktop.DBus.Properties"_cs;
|
2019-12-29 15:41:45 +00:00
|
|
|
|
2021-05-03 09:08:50 +00:00
|
|
|
using namespace base::Platform;
|
|
|
|
|
2021-01-16 04:47:07 +00:00
|
|
|
struct ServerInformation {
|
|
|
|
QString name;
|
|
|
|
QString vendor;
|
|
|
|
QVersionNumber version;
|
|
|
|
QVersionNumber specVersion;
|
|
|
|
};
|
|
|
|
|
2021-01-09 02:10:06 +00:00
|
|
|
bool ServiceRegistered = false;
|
2021-01-13 09:56:49 +00:00
|
|
|
bool InhibitionSupported = false;
|
2021-01-16 04:47:07 +00:00
|
|
|
std::optional<ServerInformation> CurrentServerInformation;
|
2021-01-09 02:10:06 +00:00
|
|
|
QStringList CurrentCapabilities;
|
2020-03-10 08:36:06 +00:00
|
|
|
|
2021-07-18 03:26:42 +00:00
|
|
|
std::unique_ptr<base::Platform::DBus::ServiceWatcher> CreateServiceWatcher() {
|
|
|
|
try {
|
|
|
|
const auto connection = Gio::DBus::Connection::get_sync(
|
|
|
|
Gio::DBus::BusType::BUS_TYPE_SESSION);
|
|
|
|
|
|
|
|
const auto activatable = [&] {
|
|
|
|
try {
|
|
|
|
return ranges::contains(
|
|
|
|
base::Platform::DBus::ListActivatableNames(connection),
|
|
|
|
Glib::ustring(std::string(kService)));
|
|
|
|
} catch (...) {
|
|
|
|
// avoid service restart loop in sandboxed environments
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
return std::make_unique<base::Platform::DBus::ServiceWatcher>(
|
|
|
|
connection,
|
|
|
|
std::string(kService),
|
|
|
|
[=](
|
|
|
|
const Glib::ustring &service,
|
|
|
|
const Glib::ustring &oldOwner,
|
|
|
|
const Glib::ustring &newOwner) {
|
|
|
|
if (activatable && newOwner.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
crl::on_main([] {
|
|
|
|
Core::App().notifications().createManager();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-05-02 06:30:47 +00:00
|
|
|
void StartServiceAsync(Fn<void()> callback) {
|
2021-02-23 02:34:01 +00:00
|
|
|
try {
|
|
|
|
const auto connection = Gio::DBus::Connection::get_sync(
|
|
|
|
Gio::DBus::BusType::BUS_TYPE_SESSION);
|
|
|
|
|
2021-05-03 09:08:50 +00:00
|
|
|
DBus::StartServiceByNameAsync(
|
2021-02-23 02:34:01 +00:00
|
|
|
connection,
|
|
|
|
std::string(kService),
|
2021-05-03 09:08:50 +00:00
|
|
|
[=](Fn<DBus::StartReply()> result) {
|
2021-02-23 02:34:01 +00:00
|
|
|
try {
|
|
|
|
result(); // get the error if any
|
|
|
|
} catch (const Glib::Error &e) {
|
2021-04-27 12:17:10 +00:00
|
|
|
static const auto NotSupportedErrors = {
|
|
|
|
"org.freedesktop.DBus.Error.ServiceUnknown",
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto errorName =
|
|
|
|
Gio::DBus::ErrorUtils::get_remote_error(e);
|
|
|
|
|
|
|
|
if (!ranges::contains(NotSupportedErrors, errorName)) {
|
|
|
|
LOG(("Native Notification Error: %1").arg(
|
|
|
|
QString::fromStdString(e.what())));
|
|
|
|
}
|
2021-02-23 02:34:01 +00:00
|
|
|
} catch (const std::exception &e) {
|
|
|
|
LOG(("Native Notification Error: %1").arg(
|
|
|
|
QString::fromStdString(e.what())));
|
|
|
|
}
|
|
|
|
|
2021-05-03 09:08:50 +00:00
|
|
|
crl::on_main(callback);
|
2021-05-02 06:30:47 +00:00
|
|
|
});
|
2021-02-23 02:34:01 +00:00
|
|
|
|
|
|
|
return;
|
2021-04-27 12:17:10 +00:00
|
|
|
} catch (...) {
|
2021-02-23 02:34:01 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:08:50 +00:00
|
|
|
crl::on_main(callback);
|
2021-02-23 02:34:01 +00:00
|
|
|
}
|
|
|
|
|
2021-01-09 02:10:06 +00:00
|
|
|
bool GetServiceRegistered() {
|
2021-02-28 02:34:41 +00:00
|
|
|
try {
|
|
|
|
const auto connection = Gio::DBus::Connection::get_sync(
|
|
|
|
Gio::DBus::BusType::BUS_TYPE_SESSION);
|
|
|
|
|
2021-03-10 07:05:03 +00:00
|
|
|
const auto hasOwner = [&] {
|
|
|
|
try {
|
2021-05-03 09:08:50 +00:00
|
|
|
return DBus::NameHasOwner(
|
2021-03-10 07:05:03 +00:00
|
|
|
connection,
|
|
|
|
std::string(kService));
|
|
|
|
} catch (...) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
static const auto activatable = [&] {
|
|
|
|
try {
|
|
|
|
return ranges::contains(
|
2021-05-03 09:08:50 +00:00
|
|
|
DBus::ListActivatableNames(connection),
|
2021-03-10 07:05:03 +00:00
|
|
|
Glib::ustring(std::string(kService)));
|
|
|
|
} catch (...) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
return hasOwner || activatable;
|
2021-02-28 02:34:41 +00:00
|
|
|
} catch (...) {
|
|
|
|
}
|
2020-04-28 04:32:24 +00:00
|
|
|
|
2021-02-28 02:34:41 +00:00
|
|
|
return false;
|
2020-04-28 04:32:24 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 02:34:41 +00:00
|
|
|
void GetServerInformation(
|
2021-03-02 10:20:51 +00:00
|
|
|
Fn<void(const std::optional<ServerInformation> &)> callback) {
|
|
|
|
try {
|
|
|
|
const auto connection = Gio::DBus::Connection::get_sync(
|
|
|
|
Gio::DBus::BusType::BUS_TYPE_SESSION);
|
2019-12-29 15:41:45 +00:00
|
|
|
|
2021-03-02 10:20:51 +00:00
|
|
|
connection->call(
|
|
|
|
std::string(kObjectPath),
|
|
|
|
std::string(kInterface),
|
|
|
|
"GetServerInformation",
|
|
|
|
{},
|
|
|
|
[=](const Glib::RefPtr<Gio::AsyncResult> &result) {
|
|
|
|
try {
|
|
|
|
auto reply = connection->call_finish(result);
|
|
|
|
|
2021-05-03 09:08:50 +00:00
|
|
|
const auto name = GlibVariantCast<Glib::ustring>(
|
|
|
|
reply.get_child(0));
|
2021-03-02 10:20:51 +00:00
|
|
|
|
2021-05-03 09:08:50 +00:00
|
|
|
const auto vendor = GlibVariantCast<Glib::ustring>(
|
|
|
|
reply.get_child(1));
|
2021-03-02 10:20:51 +00:00
|
|
|
|
2021-05-03 09:08:50 +00:00
|
|
|
const auto version = GlibVariantCast<Glib::ustring>(
|
|
|
|
reply.get_child(2));
|
2021-03-02 10:20:51 +00:00
|
|
|
|
2021-05-03 09:08:50 +00:00
|
|
|
const auto specVersion = GlibVariantCast<Glib::ustring>(
|
|
|
|
reply.get_child(3));
|
2021-03-02 10:20:51 +00:00
|
|
|
|
|
|
|
crl::on_main([=] {
|
|
|
|
callback(ServerInformation{
|
|
|
|
QString::fromStdString(name),
|
|
|
|
QString::fromStdString(vendor),
|
|
|
|
QVersionNumber::fromString(
|
|
|
|
QString::fromStdString(version)),
|
|
|
|
QVersionNumber::fromString(
|
|
|
|
QString::fromStdString(specVersion)),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
} catch (const Glib::Error &e) {
|
|
|
|
LOG(("Native Notification Error: %1").arg(
|
|
|
|
QString::fromStdString(e.what())));
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
LOG(("Native Notification Error: %1").arg(
|
|
|
|
QString::fromStdString(e.what())));
|
|
|
|
}
|
2021-03-04 15:29:01 +00:00
|
|
|
|
2021-02-23 02:34:01 +00:00
|
|
|
crl::on_main([=] { callback(std::nullopt); });
|
2021-03-02 10:20:51 +00:00
|
|
|
},
|
|
|
|
std::string(kService));
|
2021-01-21 10:18:40 +00:00
|
|
|
|
2021-03-02 10:20:51 +00:00
|
|
|
return;
|
|
|
|
} catch (const Glib::Error &e) {
|
|
|
|
LOG(("Native Notification Error: %1").arg(
|
|
|
|
QString::fromStdString(e.what())));
|
|
|
|
}
|
2021-03-04 15:29:01 +00:00
|
|
|
|
2021-03-02 10:20:51 +00:00
|
|
|
crl::on_main([=] { callback(std::nullopt); });
|
2016-10-03 15:07:50 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 10:20:51 +00:00
|
|
|
void GetCapabilities(Fn<void(const QStringList &)> callback) {
|
|
|
|
try {
|
|
|
|
const auto connection = Gio::DBus::Connection::get_sync(
|
|
|
|
Gio::DBus::BusType::BUS_TYPE_SESSION);
|
2018-11-08 10:58:04 +00:00
|
|
|
|
2021-03-02 10:20:51 +00:00
|
|
|
connection->call(
|
|
|
|
std::string(kObjectPath),
|
|
|
|
std::string(kInterface),
|
|
|
|
"GetCapabilities",
|
|
|
|
{},
|
|
|
|
[=](const Glib::RefPtr<Gio::AsyncResult> &result) {
|
|
|
|
try {
|
|
|
|
auto reply = connection->call_finish(result);
|
|
|
|
|
|
|
|
QStringList value;
|
|
|
|
ranges::transform(
|
2021-05-03 09:08:50 +00:00
|
|
|
GlibVariantCast<std::vector<Glib::ustring>>(
|
|
|
|
reply.get_child(0)),
|
2021-03-02 10:20:51 +00:00
|
|
|
ranges::back_inserter(value),
|
|
|
|
QString::fromStdString);
|
|
|
|
|
|
|
|
crl::on_main([=] {
|
|
|
|
callback(value);
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
} catch (const Glib::Error &e) {
|
|
|
|
LOG(("Native Notification Error: %1").arg(
|
|
|
|
QString::fromStdString(e.what())));
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
LOG(("Native Notification Error: %1").arg(
|
|
|
|
QString::fromStdString(e.what())));
|
|
|
|
}
|
2021-03-04 15:29:01 +00:00
|
|
|
|
2021-03-02 10:20:51 +00:00
|
|
|
crl::on_main([=] { callback({}); });
|
|
|
|
},
|
|
|
|
std::string(kService));
|
2021-01-21 10:18:40 +00:00
|
|
|
|
2021-03-02 10:20:51 +00:00
|
|
|
return;
|
|
|
|
} catch (const Glib::Error &e) {
|
|
|
|
LOG(("Native Notification Error: %1").arg(
|
|
|
|
QString::fromStdString(e.what())));
|
|
|
|
}
|
2021-03-04 15:29:01 +00:00
|
|
|
|
2021-03-02 10:20:51 +00:00
|
|
|
crl::on_main([=] { callback({}); });
|
2019-12-29 15:41:45 +00:00
|
|
|
}
|
2016-10-03 15:07:50 +00:00
|
|
|
|
2021-01-21 10:18:40 +00:00
|
|
|
void GetInhibitionSupported(Fn<void(bool)> callback) {
|
2021-03-02 10:20:51 +00:00
|
|
|
try {
|
|
|
|
const auto connection = Gio::DBus::Connection::get_sync(
|
|
|
|
Gio::DBus::BusType::BUS_TYPE_SESSION);
|
2021-01-13 09:56:49 +00:00
|
|
|
|
2021-03-02 10:20:51 +00:00
|
|
|
connection->call(
|
|
|
|
std::string(kObjectPath),
|
|
|
|
std::string(kPropertiesInterface),
|
|
|
|
"Get",
|
2021-05-03 09:08:50 +00:00
|
|
|
MakeGlibVariant(std::tuple{
|
2021-03-02 10:20:51 +00:00
|
|
|
Glib::ustring(std::string(kInterface)),
|
|
|
|
Glib::ustring("Inhibited"),
|
|
|
|
}),
|
|
|
|
[=](const Glib::RefPtr<Gio::AsyncResult> &result) {
|
|
|
|
try {
|
|
|
|
connection->call_finish(result);
|
|
|
|
|
|
|
|
crl::on_main([=] {
|
|
|
|
callback(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
} catch (const Glib::Error &e) {
|
|
|
|
static const auto DontLogErrors = {
|
|
|
|
"org.freedesktop.DBus.Error.InvalidArgs",
|
|
|
|
"org.freedesktop.DBus.Error.UnknownMethod",
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto errorName = Gio::DBus::ErrorUtils::get_remote_error(e);
|
|
|
|
if (!ranges::contains(DontLogErrors, errorName)) {
|
|
|
|
LOG(("Native Notification Error: %1").arg(
|
|
|
|
QString::fromStdString(e.what())));
|
|
|
|
}
|
|
|
|
}
|
2021-03-04 15:29:01 +00:00
|
|
|
|
2021-03-02 10:20:51 +00:00
|
|
|
crl::on_main([=] { callback(false); });
|
|
|
|
},
|
|
|
|
std::string(kService));
|
2021-01-21 10:18:40 +00:00
|
|
|
|
2021-03-02 10:20:51 +00:00
|
|
|
return;
|
|
|
|
} catch (const Glib::Error &e) {
|
|
|
|
LOG(("Native Notification Error: %1").arg(
|
|
|
|
QString::fromStdString(e.what())));
|
|
|
|
}
|
2021-03-04 15:29:01 +00:00
|
|
|
|
2021-03-02 10:20:51 +00:00
|
|
|
crl::on_main([=] { callback(false); });
|
2021-01-13 09:56:49 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 08:52:05 +00:00
|
|
|
bool Inhibited() {
|
2021-01-09 02:10:06 +00:00
|
|
|
if (!Supported()
|
|
|
|
|| !CurrentCapabilities.contains(qsl("inhibitions"))
|
2021-01-13 09:56:49 +00:00
|
|
|
|| !InhibitionSupported) {
|
2021-01-09 02:10:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-02 10:20:51 +00:00
|
|
|
try {
|
|
|
|
const auto connection = Gio::DBus::Connection::get_sync(
|
|
|
|
Gio::DBus::BusType::BUS_TYPE_SESSION);
|
2020-03-03 08:52:05 +00:00
|
|
|
|
2021-02-23 02:34:01 +00:00
|
|
|
// a hack for snap's activation restriction
|
2021-05-03 09:08:50 +00:00
|
|
|
DBus::StartServiceByName(
|
2021-02-23 02:34:01 +00:00
|
|
|
connection,
|
|
|
|
std::string(kService));
|
|
|
|
|
2021-03-02 10:20:51 +00:00
|
|
|
auto reply = connection->call_sync(
|
|
|
|
std::string(kObjectPath),
|
|
|
|
std::string(kPropertiesInterface),
|
|
|
|
"Get",
|
2021-05-03 09:08:50 +00:00
|
|
|
MakeGlibVariant(std::tuple{
|
2021-03-02 10:20:51 +00:00
|
|
|
Glib::ustring(std::string(kInterface)),
|
|
|
|
Glib::ustring("Inhibited"),
|
|
|
|
}),
|
|
|
|
std::string(kService));
|
2020-03-03 08:52:05 +00:00
|
|
|
|
2021-05-03 09:08:50 +00:00
|
|
|
return GlibVariantCast<bool>(
|
|
|
|
GlibVariantCast<Glib::VariantBase>(reply.get_child(0)));
|
2021-03-02 10:20:51 +00:00
|
|
|
} catch (const Glib::Error &e) {
|
|
|
|
LOG(("Native Notification Error: %1").arg(
|
|
|
|
QString::fromStdString(e.what())));
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
LOG(("Native Notification Error: %1").arg(
|
|
|
|
QString::fromStdString(e.what())));
|
2020-03-03 08:52:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-13 09:56:49 +00:00
|
|
|
bool IsQualifiedDaemon() {
|
|
|
|
// A list of capabilities that offer feature parity
|
|
|
|
// with custom notifications
|
|
|
|
static const auto NeededCapabilities = {
|
|
|
|
// To show message content
|
|
|
|
qsl("body"),
|
|
|
|
// To make the sender name bold
|
|
|
|
qsl("body-markup"),
|
|
|
|
// To have buttons on notifications
|
|
|
|
qsl("actions"),
|
|
|
|
// To have quick reply
|
|
|
|
qsl("inline-reply"),
|
|
|
|
// To not to play sound with Don't Disturb activated
|
|
|
|
// (no, using sound capability is not a way)
|
|
|
|
qsl("inhibitions"),
|
|
|
|
};
|
|
|
|
|
|
|
|
return ranges::all_of(NeededCapabilities, [&](const auto &capability) {
|
|
|
|
return CurrentCapabilities.contains(capability);
|
|
|
|
}) && InhibitionSupported;
|
|
|
|
}
|
|
|
|
|
2021-01-16 04:47:07 +00:00
|
|
|
ServerInformation CurrentServerInformationValue() {
|
|
|
|
return CurrentServerInformation.value_or(ServerInformation{});
|
2019-12-29 15:41:45 +00:00
|
|
|
}
|
2016-10-03 15:07:50 +00:00
|
|
|
|
2021-02-28 02:34:41 +00:00
|
|
|
Glib::ustring GetImageKey(const QVersionNumber &specificationVersion) {
|
2021-01-16 04:47:07 +00:00
|
|
|
const auto normalizedVersion = specificationVersion.normalized();
|
|
|
|
|
|
|
|
if (normalizedVersion.isNull()) {
|
2021-01-22 12:07:31 +00:00
|
|
|
LOG(("Native Notification Error: specification version is null"));
|
2021-02-28 02:34:41 +00:00
|
|
|
return {};
|
2021-01-16 04:47:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (normalizedVersion >= QVersionNumber(1, 2)) {
|
2021-02-28 02:34:41 +00:00
|
|
|
return "image-data";
|
2021-01-16 04:47:07 +00:00
|
|
|
} else if (normalizedVersion == QVersionNumber(1, 1)) {
|
2021-02-28 02:34:41 +00:00
|
|
|
return "image_data";
|
2020-02-24 23:45:46 +00:00
|
|
|
}
|
2020-02-29 03:42:24 +00:00
|
|
|
|
2021-02-28 02:34:41 +00:00
|
|
|
return "icon_data";
|
2020-02-24 23:45:46 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:08:50 +00:00
|
|
|
class NotificationData final : public base::has_weak_ptr {
|
2020-08-01 11:03:17 +00:00
|
|
|
public:
|
|
|
|
using NotificationId = Window::Notifications::Manager::NotificationId;
|
|
|
|
|
|
|
|
NotificationData(
|
2021-05-03 09:08:50 +00:00
|
|
|
not_null<Manager*> manager,
|
|
|
|
NotificationId id);
|
|
|
|
|
|
|
|
[[nodiscard]] bool init(
|
2020-08-01 11:03:17 +00:00
|
|
|
const QString &title,
|
|
|
|
const QString &subtitle,
|
|
|
|
const QString &msg,
|
|
|
|
bool hideReplyButton);
|
|
|
|
|
|
|
|
NotificationData(const NotificationData &other) = delete;
|
|
|
|
NotificationData &operator=(const NotificationData &other) = delete;
|
|
|
|
NotificationData(NotificationData &&other) = delete;
|
|
|
|
NotificationData &operator=(NotificationData &&other) = delete;
|
|
|
|
|
|
|
|
~NotificationData();
|
|
|
|
|
2021-01-19 22:31:27 +00:00
|
|
|
void show();
|
2020-08-01 11:03:17 +00:00
|
|
|
void close();
|
|
|
|
void setImage(const QString &imagePath);
|
|
|
|
|
|
|
|
private:
|
2021-05-03 09:08:50 +00:00
|
|
|
const not_null<Manager*> _manager;
|
|
|
|
NotificationId _id;
|
2020-08-01 11:03:17 +00:00
|
|
|
|
2021-05-03 09:08:50 +00:00
|
|
|
Glib::RefPtr<Gio::DBus::Connection> _dbusConnection;
|
2021-02-28 02:34:41 +00:00
|
|
|
Glib::ustring _title;
|
|
|
|
Glib::ustring _body;
|
|
|
|
std::vector<Glib::ustring> _actions;
|
|
|
|
std::map<Glib::ustring, Glib::VariantBase> _hints;
|
|
|
|
Glib::ustring _imageKey;
|
2020-08-01 11:03:17 +00:00
|
|
|
|
|
|
|
uint _notificationId = 0;
|
2021-02-28 02:34:41 +00:00
|
|
|
uint _actionInvokedSignalId = 0;
|
|
|
|
uint _notificationRepliedSignalId = 0;
|
|
|
|
uint _notificationClosedSignalId = 0;
|
2020-08-01 11:03:17 +00:00
|
|
|
|
2020-10-27 10:13:28 +00:00
|
|
|
void notificationClosed(uint id, uint reason);
|
2021-02-28 02:34:41 +00:00
|
|
|
void actionInvoked(uint id, const Glib::ustring &actionName);
|
|
|
|
void notificationReplied(uint id, const Glib::ustring &text);
|
|
|
|
|
2020-08-01 11:03:17 +00:00
|
|
|
};
|
|
|
|
|
2021-05-03 09:08:50 +00:00
|
|
|
using Notification = std::unique_ptr<NotificationData>;
|
2016-10-03 15:07:50 +00:00
|
|
|
|
2019-12-29 15:41:45 +00:00
|
|
|
NotificationData::NotificationData(
|
2021-05-03 09:08:50 +00:00
|
|
|
not_null<Manager*> manager,
|
|
|
|
NotificationId id)
|
2021-05-02 06:30:47 +00:00
|
|
|
: _manager(manager)
|
2020-06-24 09:05:56 +00:00
|
|
|
, _id(id) {
|
2021-05-03 09:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool NotificationData::init(
|
|
|
|
const QString &title,
|
|
|
|
const QString &subtitle,
|
|
|
|
const QString &msg,
|
|
|
|
bool hideReplyButton) {
|
2021-02-28 02:34:41 +00:00
|
|
|
try {
|
|
|
|
_dbusConnection = Gio::DBus::Connection::get_sync(
|
|
|
|
Gio::DBus::BusType::BUS_TYPE_SESSION);
|
|
|
|
} catch (const Glib::Error &e) {
|
|
|
|
LOG(("Native Notification Error: %1").arg(
|
|
|
|
QString::fromStdString(e.what())));
|
2021-05-03 09:08:50 +00:00
|
|
|
return false;
|
2020-10-08 16:40:32 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:08:50 +00:00
|
|
|
const auto weak = base::make_weak(this);
|
2021-01-09 02:10:06 +00:00
|
|
|
const auto capabilities = CurrentCapabilities;
|
2019-12-29 15:41:45 +00:00
|
|
|
|
2021-05-03 09:08:50 +00:00
|
|
|
const auto signalEmitted = [=](
|
|
|
|
const Glib::RefPtr<Gio::DBus::Connection> &connection,
|
|
|
|
const Glib::ustring &sender_name,
|
|
|
|
const Glib::ustring &object_path,
|
|
|
|
const Glib::ustring &interface_name,
|
|
|
|
const Glib::ustring &signal_name,
|
|
|
|
Glib::VariantContainerBase parameters) {
|
2021-05-02 06:30:47 +00:00
|
|
|
try {
|
|
|
|
if (signal_name == "ActionInvoked") {
|
2021-05-03 09:08:50 +00:00
|
|
|
const auto id = GlibVariantCast<uint>(
|
|
|
|
parameters.get_child(0));
|
2021-05-02 06:30:47 +00:00
|
|
|
|
2021-05-03 09:08:50 +00:00
|
|
|
const auto actionName = GlibVariantCast<Glib::ustring>(
|
|
|
|
parameters.get_child(1));
|
2021-05-02 06:30:47 +00:00
|
|
|
|
2021-05-03 09:08:50 +00:00
|
|
|
crl::on_main(weak, [=] { actionInvoked(id, actionName); });
|
2021-05-02 06:30:47 +00:00
|
|
|
} else if (signal_name == "NotificationReplied") {
|
2021-05-03 09:08:50 +00:00
|
|
|
const auto id = GlibVariantCast<uint>(
|
|
|
|
parameters.get_child(0));
|
2021-05-02 06:30:47 +00:00
|
|
|
|
2021-05-03 09:08:50 +00:00
|
|
|
const auto text = GlibVariantCast<Glib::ustring>(
|
|
|
|
parameters.get_child(1));
|
2021-05-02 06:30:47 +00:00
|
|
|
|
2021-05-03 09:08:50 +00:00
|
|
|
crl::on_main(weak, [=] { notificationReplied(id, text); });
|
2021-05-02 06:30:47 +00:00
|
|
|
} else if (signal_name == "NotificationClosed") {
|
2021-05-03 09:08:50 +00:00
|
|
|
const auto id = GlibVariantCast<uint>(
|
|
|
|
parameters.get_child(0));
|
2021-05-02 06:30:47 +00:00
|
|
|
|
2021-05-03 09:08:50 +00:00
|
|
|
const auto reason = GlibVariantCast<uint>(
|
|
|
|
parameters.get_child(1));
|
2021-05-02 06:30:47 +00:00
|
|
|
|
2021-05-03 09:08:50 +00:00
|
|
|
crl::on_main(weak, [=] { notificationClosed(id, reason); });
|
2021-05-02 06:30:47 +00:00
|
|
|
}
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
LOG(("Native Notification Error: %1").arg(
|
|
|
|
QString::fromStdString(e.what())));
|
|
|
|
}
|
2021-05-03 09:08:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
_title = title.toStdString();
|
|
|
|
_imageKey = GetImageKey(CurrentServerInformationValue().specVersion);
|
2021-05-02 06:30:47 +00:00
|
|
|
|
2020-01-20 21:57:50 +00:00
|
|
|
if (capabilities.contains(qsl("body-markup"))) {
|
2019-12-29 15:41:45 +00:00
|
|
|
_body = subtitle.isEmpty()
|
2021-02-28 02:34:41 +00:00
|
|
|
? msg.toHtmlEscaped().toStdString()
|
2021-03-13 11:50:34 +00:00
|
|
|
: qsl("<b>%1</b>\n%2").arg(
|
|
|
|
subtitle.toHtmlEscaped(),
|
|
|
|
msg.toHtmlEscaped()).toStdString();
|
2019-12-29 15:41:45 +00:00
|
|
|
} else {
|
|
|
|
_body = subtitle.isEmpty()
|
2021-02-28 02:34:41 +00:00
|
|
|
? msg.toStdString()
|
2021-03-13 11:50:34 +00:00
|
|
|
: qsl("%1\n%2").arg(subtitle, msg).toStdString();
|
2016-10-03 15:07:50 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 02:34:41 +00:00
|
|
|
if (capabilities.contains("actions")) {
|
|
|
|
_actions.push_back("default");
|
|
|
|
_actions.push_back({});
|
2020-01-17 07:18:25 +00:00
|
|
|
|
2020-10-12 08:05:43 +00:00
|
|
|
if (!hideReplyButton) {
|
2021-02-28 02:34:41 +00:00
|
|
|
_actions.push_back("mail-mark-read");
|
|
|
|
_actions.push_back(
|
|
|
|
tr::lng_context_mark_read(tr::now).toStdString());
|
2020-10-12 08:05:43 +00:00
|
|
|
}
|
2020-01-21 12:27:50 +00:00
|
|
|
|
2021-02-28 02:34:41 +00:00
|
|
|
if (capabilities.contains("inline-reply") && !hideReplyButton) {
|
|
|
|
_actions.push_back("inline-reply");
|
|
|
|
_actions.push_back(
|
|
|
|
tr::lng_notification_reply(tr::now).toStdString());
|
2020-10-08 16:40:32 +00:00
|
|
|
|
2021-02-28 02:34:41 +00:00
|
|
|
_notificationRepliedSignalId = _dbusConnection->signal_subscribe(
|
2021-05-02 06:30:47 +00:00
|
|
|
signalEmitted,
|
2021-02-28 02:34:41 +00:00
|
|
|
std::string(kService),
|
|
|
|
std::string(kInterface),
|
2020-10-08 16:40:32 +00:00
|
|
|
"NotificationReplied",
|
2021-02-28 02:34:41 +00:00
|
|
|
std::string(kObjectPath));
|
2020-01-21 12:27:50 +00:00
|
|
|
} else {
|
|
|
|
// icon name according to https://specifications.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
|
2021-02-28 02:34:41 +00:00
|
|
|
_actions.push_back("mail-reply-sender");
|
|
|
|
_actions.push_back(
|
|
|
|
tr::lng_notification_reply(tr::now).toStdString());
|
2020-01-21 12:27:50 +00:00
|
|
|
}
|
2020-10-08 16:40:32 +00:00
|
|
|
|
2021-02-28 02:34:41 +00:00
|
|
|
_actionInvokedSignalId = _dbusConnection->signal_subscribe(
|
2021-05-02 06:30:47 +00:00
|
|
|
signalEmitted,
|
2021-02-28 02:34:41 +00:00
|
|
|
std::string(kService),
|
|
|
|
std::string(kInterface),
|
2020-10-08 16:40:32 +00:00
|
|
|
"ActionInvoked",
|
2021-02-28 02:34:41 +00:00
|
|
|
std::string(kObjectPath));
|
2016-10-03 15:07:50 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 02:34:41 +00:00
|
|
|
if (capabilities.contains("action-icons")) {
|
|
|
|
_hints["action-icons"] = Glib::Variant<bool>::create(true);
|
2016-10-03 15:07:50 +00:00
|
|
|
}
|
|
|
|
|
2020-08-01 11:03:17 +00:00
|
|
|
// suppress system sound if telegram sound activated,
|
|
|
|
// otherwise use system sound
|
2021-02-28 02:34:41 +00:00
|
|
|
if (capabilities.contains("sound")) {
|
2020-06-18 18:04:16 +00:00
|
|
|
if (Core::App().settings().soundNotify()) {
|
2021-02-28 02:34:41 +00:00
|
|
|
_hints["suppress-sound"] = Glib::Variant<bool>::create(true);
|
2019-12-29 15:41:45 +00:00
|
|
|
} else {
|
|
|
|
// sound name according to http://0pointer.de/public/sound-naming-spec.html
|
2021-02-28 02:34:41 +00:00
|
|
|
_hints["sound-name"] = Glib::Variant<Glib::ustring>::create(
|
|
|
|
"message-new-instant");
|
2016-10-04 13:36:50 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-29 15:41:45 +00:00
|
|
|
|
2021-02-28 02:34:41 +00:00
|
|
|
if (capabilities.contains("x-canonical-append")) {
|
|
|
|
_hints["x-canonical-append"] = Glib::Variant<Glib::ustring>::create(
|
|
|
|
"true");
|
2016-10-03 15:07:50 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 02:34:41 +00:00
|
|
|
_hints["category"] = Glib::Variant<Glib::ustring>::create("im.received");
|
2016-10-03 15:07:50 +00:00
|
|
|
|
2021-02-28 02:34:41 +00:00
|
|
|
_hints["desktop-entry"] = Glib::Variant<Glib::ustring>::create(
|
2021-03-12 04:55:31 +00:00
|
|
|
QGuiApplication::desktopFileName().chopped(8).toStdString());
|
2020-08-01 11:03:17 +00:00
|
|
|
|
2021-02-28 02:34:41 +00:00
|
|
|
_notificationClosedSignalId = _dbusConnection->signal_subscribe(
|
2021-05-02 06:30:47 +00:00
|
|
|
signalEmitted,
|
2021-02-28 02:34:41 +00:00
|
|
|
std::string(kService),
|
|
|
|
std::string(kInterface),
|
2020-10-08 16:40:32 +00:00
|
|
|
"NotificationClosed",
|
2021-02-28 02:34:41 +00:00
|
|
|
std::string(kObjectPath));
|
2021-05-03 09:08:50 +00:00
|
|
|
return true;
|
2020-08-01 11:03:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NotificationData::~NotificationData() {
|
2020-10-08 16:40:32 +00:00
|
|
|
if (_dbusConnection) {
|
|
|
|
if (_actionInvokedSignalId != 0) {
|
2021-02-28 02:34:41 +00:00
|
|
|
_dbusConnection->signal_unsubscribe(_actionInvokedSignalId);
|
2020-10-08 16:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_notificationRepliedSignalId != 0) {
|
2021-02-28 02:34:41 +00:00
|
|
|
_dbusConnection->signal_unsubscribe(_notificationRepliedSignalId);
|
2020-10-08 16:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_notificationClosedSignalId != 0) {
|
2021-02-28 02:34:41 +00:00
|
|
|
_dbusConnection->signal_unsubscribe(_notificationClosedSignalId);
|
2020-10-08 16:40:32 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-29 15:41:45 +00:00
|
|
|
}
|
2016-10-03 15:07:50 +00:00
|
|
|
|
2021-01-19 22:31:27 +00:00
|
|
|
void NotificationData::show() {
|
2021-02-23 02:34:01 +00:00
|
|
|
// a hack for snap's activation restriction
|
2021-05-03 09:08:50 +00:00
|
|
|
const auto weak = base::make_weak(this);
|
|
|
|
StartServiceAsync(crl::guard(weak, [=] {
|
2021-02-28 02:34:41 +00:00
|
|
|
const auto iconName = _imageKey.empty()
|
|
|
|
|| _hints.find(_imageKey) == end(_hints)
|
|
|
|
? Glib::ustring(GetIconName().toStdString())
|
|
|
|
: Glib::ustring();
|
2021-05-12 08:10:55 +00:00
|
|
|
const auto connection = _dbusConnection;
|
2021-02-28 02:34:41 +00:00
|
|
|
|
2021-05-12 08:10:55 +00:00
|
|
|
connection->call(
|
2021-02-28 02:34:41 +00:00
|
|
|
std::string(kObjectPath),
|
|
|
|
std::string(kInterface),
|
|
|
|
"Notify",
|
2021-05-03 09:08:50 +00:00
|
|
|
MakeGlibVariant(std::tuple{
|
2021-02-28 02:34:41 +00:00
|
|
|
Glib::ustring(std::string(AppName)),
|
|
|
|
uint(0),
|
|
|
|
iconName,
|
|
|
|
_title,
|
|
|
|
_body,
|
|
|
|
_actions,
|
|
|
|
_hints,
|
|
|
|
-1,
|
|
|
|
}),
|
2021-05-03 09:08:50 +00:00
|
|
|
[=](const Glib::RefPtr<Gio::AsyncResult> &result) {
|
2021-05-02 06:30:47 +00:00
|
|
|
try {
|
2021-05-12 08:10:55 +00:00
|
|
|
auto reply = connection->call_finish(result);
|
2021-05-03 09:08:50 +00:00
|
|
|
const auto notificationId = GlibVariantCast<uint>(
|
2021-05-02 06:30:47 +00:00
|
|
|
reply.get_child(0));
|
2021-05-03 09:08:50 +00:00
|
|
|
crl::on_main(weak, [=] {
|
|
|
|
_notificationId = notificationId;
|
|
|
|
});
|
2021-05-02 06:30:47 +00:00
|
|
|
return;
|
|
|
|
} catch (const Glib::Error &e) {
|
|
|
|
LOG(("Native Notification Error: %1").arg(
|
|
|
|
QString::fromStdString(e.what())));
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
LOG(("Native Notification Error: %1").arg(
|
|
|
|
QString::fromStdString(e.what())));
|
|
|
|
}
|
2021-05-03 09:08:50 +00:00
|
|
|
crl::on_main(weak, [=] {
|
|
|
|
_manager->clearNotification(_id);
|
2021-05-02 06:30:47 +00:00
|
|
|
});
|
2021-05-03 09:08:50 +00:00
|
|
|
},
|
2021-05-02 06:30:47 +00:00
|
|
|
std::string(kService));
|
|
|
|
}));
|
2019-12-29 15:41:45 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 15:55:29 +00:00
|
|
|
void NotificationData::close() {
|
2021-02-23 02:34:01 +00:00
|
|
|
_dbusConnection->call(
|
|
|
|
std::string(kObjectPath),
|
|
|
|
std::string(kInterface),
|
|
|
|
"CloseNotification",
|
2021-05-03 09:08:50 +00:00
|
|
|
MakeGlibVariant(std::tuple{
|
2021-02-23 02:34:01 +00:00
|
|
|
_notificationId,
|
|
|
|
}),
|
|
|
|
{},
|
|
|
|
std::string(kService));
|
2021-05-03 09:08:50 +00:00
|
|
|
_manager->clearNotification(_id);
|
2016-10-02 09:30:28 +00:00
|
|
|
}
|
|
|
|
|
2019-12-29 15:41:45 +00:00
|
|
|
void NotificationData::setImage(const QString &imagePath) {
|
2021-07-18 19:07:11 +00:00
|
|
|
if (imagePath.isEmpty() || _imageKey.empty()) {
|
2019-12-29 15:41:45 +00:00
|
|
|
return;
|
2016-10-03 15:07:50 +00:00
|
|
|
}
|
2017-03-04 19:36:59 +00:00
|
|
|
|
2021-02-28 02:34:41 +00:00
|
|
|
const auto image = QImage(imagePath)
|
|
|
|
.convertToFormat(QImage::Format_RGBA8888);
|
2020-01-24 19:55:01 +00:00
|
|
|
|
2021-07-18 19:07:11 +00:00
|
|
|
if (image.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-03 09:08:50 +00:00
|
|
|
_hints[_imageKey] = MakeGlibVariant(std::tuple{
|
2021-02-28 02:34:41 +00:00
|
|
|
image.width(),
|
|
|
|
image.height(),
|
|
|
|
image.bytesPerLine(),
|
2020-08-01 11:03:17 +00:00
|
|
|
true,
|
|
|
|
8,
|
|
|
|
4,
|
2021-02-28 02:34:41 +00:00
|
|
|
std::vector<uchar>(
|
|
|
|
image.constBits(),
|
|
|
|
image.constBits() + image.sizeInBytes()),
|
|
|
|
});
|
2020-08-01 11:03:17 +00:00
|
|
|
}
|
2019-12-29 15:41:45 +00:00
|
|
|
|
2020-08-01 11:03:17 +00:00
|
|
|
void NotificationData::notificationClosed(uint id, uint reason) {
|
2019-12-29 15:41:45 +00:00
|
|
|
if (id == _notificationId) {
|
2021-05-03 09:08:50 +00:00
|
|
|
_manager->clearNotification(_id);
|
2017-03-04 19:36:59 +00:00
|
|
|
}
|
2016-10-02 09:30:28 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 02:34:41 +00:00
|
|
|
void NotificationData::actionInvoked(
|
|
|
|
uint id,
|
|
|
|
const Glib::ustring &actionName) {
|
2020-02-06 09:49:22 +00:00
|
|
|
if (id != _notificationId) {
|
|
|
|
return;
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
2020-02-06 09:49:22 +00:00
|
|
|
|
2021-02-28 02:34:41 +00:00
|
|
|
if (actionName == "default"
|
|
|
|
|| actionName == "mail-reply-sender") {
|
2021-05-03 09:08:50 +00:00
|
|
|
_manager->notificationActivated(_id);
|
2021-02-28 02:34:41 +00:00
|
|
|
} else if (actionName == "mail-mark-read") {
|
2021-05-03 09:08:50 +00:00
|
|
|
_manager->notificationReplied(_id, {});
|
2020-02-06 09:49:22 +00:00
|
|
|
}
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 02:34:41 +00:00
|
|
|
void NotificationData::notificationReplied(
|
|
|
|
uint id,
|
|
|
|
const Glib::ustring &text) {
|
2020-01-21 12:27:50 +00:00
|
|
|
if (id == _notificationId) {
|
2021-05-03 09:08:50 +00:00
|
|
|
_manager->notificationReplied(
|
|
|
|
_id,
|
|
|
|
{ QString::fromStdString(text), {} });
|
2020-01-21 12:27:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-01 11:03:17 +00:00
|
|
|
} // namespace
|
2016-10-03 15:07:50 +00:00
|
|
|
|
2021-04-28 07:20:39 +00:00
|
|
|
bool SkipAudioForCustom() {
|
|
|
|
return false;
|
2020-03-03 08:52:05 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 07:20:39 +00:00
|
|
|
bool SkipToastForCustom() {
|
|
|
|
return false;
|
2020-05-12 10:04:53 +00:00
|
|
|
}
|
2020-03-03 08:52:05 +00:00
|
|
|
|
2021-04-28 07:20:39 +00:00
|
|
|
bool SkipFlashBounceForCustom() {
|
|
|
|
return false;
|
2020-03-03 08:52:05 +00:00
|
|
|
}
|
|
|
|
|
2019-12-29 15:41:45 +00:00
|
|
|
bool Supported() {
|
2021-01-09 02:10:06 +00:00
|
|
|
return ServiceRegistered;
|
2019-12-29 15:41:45 +00:00
|
|
|
}
|
2016-10-03 08:56:03 +00:00
|
|
|
|
2021-01-13 09:56:49 +00:00
|
|
|
bool Enforced() {
|
|
|
|
// Wayland doesn't support positioning
|
|
|
|
// and custom notifications don't work here
|
2021-01-24 23:16:32 +00:00
|
|
|
return IsWayland();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ByDefault() {
|
|
|
|
return IsQualifiedDaemon();
|
2021-01-13 09:56:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 10:18:40 +00:00
|
|
|
void Create(Window::Notifications::System *system) {
|
2021-07-18 03:26:42 +00:00
|
|
|
static const auto ServiceWatcher = CreateServiceWatcher();
|
|
|
|
|
2021-01-21 10:18:40 +00:00
|
|
|
const auto managerSetter = [=] {
|
|
|
|
using ManagerType = Window::Notifications::ManagerType;
|
|
|
|
if ((Core::App().settings().nativeNotifications() && Supported())
|
|
|
|
|| Enforced()) {
|
2021-02-09 11:26:33 +00:00
|
|
|
if (!system->managerType().has_value()
|
|
|
|
|| *system->managerType() != ManagerType::Native) {
|
2021-01-21 10:18:40 +00:00
|
|
|
system->setManager(std::make_unique<Manager>(system));
|
|
|
|
}
|
2021-02-09 11:26:33 +00:00
|
|
|
} else if (!system->managerType().has_value()
|
|
|
|
|| *system->managerType() != ManagerType::Default) {
|
|
|
|
system->setManager(nullptr);
|
2021-01-21 10:18:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-01-24 21:00:01 +00:00
|
|
|
const auto counter = std::make_shared<int>(3);
|
|
|
|
const auto oneReady = [=] {
|
|
|
|
if (!--*counter) {
|
|
|
|
managerSetter();
|
|
|
|
}
|
|
|
|
};
|
2021-01-21 10:18:40 +00:00
|
|
|
|
2021-02-23 02:34:01 +00:00
|
|
|
const auto serviceActivated = [=] {
|
|
|
|
ServiceRegistered = GetServiceRegistered();
|
2021-01-21 10:18:40 +00:00
|
|
|
|
2021-02-23 02:34:01 +00:00
|
|
|
if (!ServiceRegistered) {
|
|
|
|
CurrentServerInformation = std::nullopt;
|
|
|
|
CurrentCapabilities = QStringList{};
|
|
|
|
InhibitionSupported = false;
|
|
|
|
managerSetter();
|
|
|
|
return;
|
|
|
|
}
|
2021-01-21 10:18:40 +00:00
|
|
|
|
2021-02-23 02:34:01 +00:00
|
|
|
GetServerInformation([=](const std::optional<ServerInformation> &result) {
|
|
|
|
CurrentServerInformation = result;
|
|
|
|
oneReady();
|
|
|
|
});
|
|
|
|
|
|
|
|
GetCapabilities([=](const QStringList &result) {
|
|
|
|
CurrentCapabilities = result;
|
|
|
|
oneReady();
|
|
|
|
});
|
|
|
|
|
|
|
|
GetInhibitionSupported([=](bool result) {
|
|
|
|
InhibitionSupported = result;
|
|
|
|
oneReady();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// There are some asserts that manager is not nullptr,
|
|
|
|
// avoid crashes until some real manager is created
|
|
|
|
if (!system->managerType().has_value()) {
|
|
|
|
using DummyManager = Window::Notifications::DummyManager;
|
|
|
|
system->setManager(std::make_unique<DummyManager>(system));
|
|
|
|
}
|
|
|
|
|
|
|
|
// snap doesn't allow access when the daemon is not running :(
|
|
|
|
StartServiceAsync(serviceActivated);
|
2019-12-29 15:41:45 +00:00
|
|
|
}
|
2017-03-04 19:36:59 +00:00
|
|
|
|
2020-06-22 01:28:23 +00:00
|
|
|
class Manager::Private {
|
|
|
|
public:
|
|
|
|
using Type = Window::Notifications::CachedUserpics::Type;
|
|
|
|
explicit Private(not_null<Manager*> manager, Type type);
|
|
|
|
|
|
|
|
void showNotification(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
std::shared_ptr<Data::CloudImageView> &userpicView,
|
|
|
|
MsgId msgId,
|
|
|
|
const QString &title,
|
|
|
|
const QString &subtitle,
|
|
|
|
const QString &msg,
|
|
|
|
bool hideNameAndPhoto,
|
|
|
|
bool hideReplyButton);
|
|
|
|
void clearAll();
|
|
|
|
void clearFromHistory(not_null<History*> history);
|
|
|
|
void clearFromSession(not_null<Main::Session*> session);
|
|
|
|
void clearNotification(NotificationId id);
|
|
|
|
|
|
|
|
~Private();
|
|
|
|
|
|
|
|
private:
|
2021-05-03 09:08:50 +00:00
|
|
|
const not_null<Manager*> _manager;
|
|
|
|
|
2020-06-22 01:28:23 +00:00
|
|
|
base::flat_map<
|
|
|
|
FullPeer,
|
|
|
|
base::flat_map<MsgId, Notification>> _notifications;
|
|
|
|
|
|
|
|
Window::Notifications::CachedUserpics _cachedUserpics;
|
2021-05-03 09:08:50 +00:00
|
|
|
|
2020-06-22 01:28:23 +00:00
|
|
|
};
|
|
|
|
|
2020-02-29 03:42:24 +00:00
|
|
|
Manager::Private::Private(not_null<Manager*> manager, Type type)
|
2021-05-03 09:08:50 +00:00
|
|
|
: _manager(manager)
|
|
|
|
, _cachedUserpics(type) {
|
2020-06-16 21:23:39 +00:00
|
|
|
if (!Supported()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-09 02:10:06 +00:00
|
|
|
const auto serverInformation = CurrentServerInformation;
|
|
|
|
const auto capabilities = CurrentCapabilities;
|
2016-10-04 13:36:50 +00:00
|
|
|
|
2021-01-16 04:47:07 +00:00
|
|
|
if (serverInformation.has_value()) {
|
2020-03-10 08:36:06 +00:00
|
|
|
LOG(("Notification daemon product name: %1")
|
2021-01-16 04:47:07 +00:00
|
|
|
.arg(serverInformation->name));
|
2020-03-10 08:36:06 +00:00
|
|
|
|
|
|
|
LOG(("Notification daemon vendor name: %1")
|
2021-01-16 04:47:07 +00:00
|
|
|
.arg(serverInformation->vendor));
|
2020-03-10 08:36:06 +00:00
|
|
|
|
|
|
|
LOG(("Notification daemon version: %1")
|
2021-01-16 04:47:07 +00:00
|
|
|
.arg(serverInformation->version.toString()));
|
2020-03-10 08:36:06 +00:00
|
|
|
|
|
|
|
LOG(("Notification daemon specification version: %1")
|
2021-01-16 04:47:07 +00:00
|
|
|
.arg(serverInformation->specVersion.toString()));
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 15:55:29 +00:00
|
|
|
if (!capabilities.isEmpty()) {
|
2020-03-10 08:36:06 +00:00
|
|
|
LOG(("Notification daemon capabilities: %1")
|
|
|
|
.arg(capabilities.join(", ")));
|
2019-12-29 15:41:45 +00:00
|
|
|
}
|
2016-10-22 13:57:13 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 14:24:12 +00:00
|
|
|
void Manager::Private::showNotification(
|
|
|
|
not_null<PeerData*> peer,
|
2020-05-29 16:55:01 +00:00
|
|
|
std::shared_ptr<Data::CloudImageView> &userpicView,
|
2019-08-28 14:24:12 +00:00
|
|
|
MsgId msgId,
|
|
|
|
const QString &title,
|
|
|
|
const QString &subtitle,
|
|
|
|
const QString &msg,
|
|
|
|
bool hideNameAndPhoto,
|
|
|
|
bool hideReplyButton) {
|
2020-06-30 07:52:59 +00:00
|
|
|
if (!Supported()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-16 21:23:39 +00:00
|
|
|
|
2020-06-24 09:05:56 +00:00
|
|
|
const auto key = FullPeer{
|
|
|
|
.sessionId = peer->session().uniqueId(),
|
|
|
|
.peerId = peer->id
|
|
|
|
};
|
2021-05-03 09:08:50 +00:00
|
|
|
const auto notificationId = NotificationId{ .full = key, .msgId = msgId };
|
|
|
|
auto notification = std::make_unique<NotificationData>(
|
2019-12-29 15:41:45 +00:00
|
|
|
_manager,
|
2021-05-03 09:08:50 +00:00
|
|
|
notificationId);
|
|
|
|
const auto inited = notification->init(
|
2019-12-29 15:41:45 +00:00
|
|
|
title,
|
|
|
|
subtitle,
|
|
|
|
msg,
|
2020-02-22 16:36:01 +00:00
|
|
|
hideReplyButton);
|
2021-05-03 09:08:50 +00:00
|
|
|
if (!inited) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-03 08:56:03 +00:00
|
|
|
|
2020-02-22 16:36:01 +00:00
|
|
|
if (!hideNameAndPhoto) {
|
2020-06-19 12:59:31 +00:00
|
|
|
const auto userpicKey = peer->userpicUniqueKey(userpicView);
|
|
|
|
notification->setImage(
|
|
|
|
_cachedUserpics.get(userpicKey, peer, userpicView));
|
2020-02-22 16:36:01 +00:00
|
|
|
}
|
2016-10-03 08:56:03 +00:00
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
auto i = _notifications.find(key);
|
2021-05-03 09:08:50 +00:00
|
|
|
if (i != end(_notifications)) {
|
2020-06-19 12:59:31 +00:00
|
|
|
auto j = i->second.find(msgId);
|
2021-05-03 09:08:50 +00:00
|
|
|
if (j != end(i->second)) {
|
|
|
|
auto oldNotification = std::move(j->second);
|
2020-06-19 12:59:31 +00:00
|
|
|
i->second.erase(j);
|
2016-10-03 15:07:50 +00:00
|
|
|
oldNotification->close();
|
2020-06-19 12:59:31 +00:00
|
|
|
i = _notifications.find(key);
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-03 09:08:50 +00:00
|
|
|
if (i == end(_notifications)) {
|
2020-06-19 12:59:31 +00:00
|
|
|
i = _notifications.emplace(
|
|
|
|
key,
|
|
|
|
base::flat_map<MsgId, Notification>()).first;
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
2021-05-03 09:08:50 +00:00
|
|
|
const auto j = i->second.emplace(
|
|
|
|
msgId,
|
|
|
|
std::move(notification)).first;
|
|
|
|
j->second->show();
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
void Manager::Private::clearAll() {
|
2020-06-30 07:52:59 +00:00
|
|
|
if (!Supported()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-16 21:23:39 +00:00
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
for (const auto &[key, notifications] : base::take(_notifications)) {
|
|
|
|
for (const auto &[msgId, notification] : notifications) {
|
2016-10-03 15:07:50 +00:00
|
|
|
notification->close();
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 14:06:21 +00:00
|
|
|
void Manager::Private::clearFromHistory(not_null<History*> history) {
|
2020-06-30 07:52:59 +00:00
|
|
|
if (!Supported()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-16 21:23:39 +00:00
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
const auto key = FullPeer{
|
2020-06-24 09:05:56 +00:00
|
|
|
.sessionId = history->session().uniqueId(),
|
|
|
|
.peerId = history->peer->id
|
2020-06-19 12:59:31 +00:00
|
|
|
};
|
|
|
|
auto i = _notifications.find(key);
|
2016-10-03 08:56:03 +00:00
|
|
|
if (i != _notifications.cend()) {
|
2020-06-19 12:59:31 +00:00
|
|
|
const auto temp = base::take(i->second);
|
2016-10-03 08:56:03 +00:00
|
|
|
_notifications.erase(i);
|
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
for (const auto &[msgId, notification] : temp) {
|
2016-10-03 15:07:50 +00:00
|
|
|
notification->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
void Manager::Private::clearFromSession(not_null<Main::Session*> session) {
|
2020-06-30 07:52:59 +00:00
|
|
|
if (!Supported()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-16 21:23:39 +00:00
|
|
|
|
2020-06-24 09:05:56 +00:00
|
|
|
const auto sessionId = session->uniqueId();
|
2020-06-19 12:59:31 +00:00
|
|
|
for (auto i = _notifications.begin(); i != _notifications.end();) {
|
2020-06-30 07:52:59 +00:00
|
|
|
if (i->first.sessionId != sessionId) {
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const auto temp = base::take(i->second);
|
|
|
|
i = _notifications.erase(i);
|
2020-06-19 12:59:31 +00:00
|
|
|
|
2020-06-30 07:52:59 +00:00
|
|
|
for (const auto &[msgId, notification] : temp) {
|
|
|
|
notification->close();
|
2020-06-19 12:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::Private::clearNotification(NotificationId id) {
|
2020-06-30 07:52:59 +00:00
|
|
|
if (!Supported()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-19 12:59:31 +00:00
|
|
|
|
2020-06-24 09:05:56 +00:00
|
|
|
auto i = _notifications.find(id.full);
|
2016-10-03 15:07:50 +00:00
|
|
|
if (i != _notifications.cend()) {
|
2020-06-19 12:59:31 +00:00
|
|
|
if (i->second.remove(id.msgId) && i->second.empty()) {
|
2016-10-03 15:07:50 +00:00
|
|
|
_notifications.erase(i);
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
Manager::Private::~Private() {
|
|
|
|
clearAll();
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
|
2020-02-29 03:42:24 +00:00
|
|
|
Manager::Manager(not_null<Window::Notifications::System*> system)
|
2019-12-29 15:41:45 +00:00
|
|
|
: NativeManager(system)
|
|
|
|
, _private(std::make_unique<Private>(this, Private::Type::Rounded)) {
|
2016-10-04 13:36:50 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
void Manager::clearNotification(NotificationId id) {
|
|
|
|
_private->clearNotification(id);
|
2016-10-03 15:07:50 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 08:56:03 +00:00
|
|
|
Manager::~Manager() = default;
|
|
|
|
|
2019-08-28 14:24:12 +00:00
|
|
|
void Manager::doShowNativeNotification(
|
|
|
|
not_null<PeerData*> peer,
|
2020-05-29 16:55:01 +00:00
|
|
|
std::shared_ptr<Data::CloudImageView> &userpicView,
|
2019-08-28 14:24:12 +00:00
|
|
|
MsgId msgId,
|
|
|
|
const QString &title,
|
|
|
|
const QString &subtitle,
|
|
|
|
const QString &msg,
|
|
|
|
bool hideNameAndPhoto,
|
|
|
|
bool hideReplyButton) {
|
|
|
|
_private->showNotification(
|
|
|
|
peer,
|
2020-05-29 16:55:01 +00:00
|
|
|
userpicView,
|
2019-08-28 14:24:12 +00:00
|
|
|
msgId,
|
|
|
|
title,
|
|
|
|
subtitle,
|
|
|
|
msg,
|
|
|
|
hideNameAndPhoto,
|
|
|
|
hideReplyButton);
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::doClearAllFast() {
|
2017-03-04 19:36:59 +00:00
|
|
|
_private->clearAll();
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 14:06:21 +00:00
|
|
|
void Manager::doClearFromHistory(not_null<History*> history) {
|
2017-03-04 19:36:59 +00:00
|
|
|
_private->clearFromHistory(history);
|
2016-10-02 09:30:28 +00:00
|
|
|
}
|
2020-06-19 12:59:31 +00:00
|
|
|
|
|
|
|
void Manager::doClearFromSession(not_null<Main::Session*> session) {
|
|
|
|
_private->clearFromSession(session);
|
|
|
|
}
|
2016-10-02 09:30:28 +00:00
|
|
|
|
2021-04-28 07:20:39 +00:00
|
|
|
bool Manager::doSkipAudio() const {
|
|
|
|
return Inhibited();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Manager::doSkipToast() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Manager::doSkipFlashBounce() const {
|
|
|
|
return Inhibited();
|
|
|
|
}
|
|
|
|
|
2016-10-02 09:30:28 +00:00
|
|
|
} // namespace Notifications
|
|
|
|
} // namespace Platform
|