Files
DefaultVPN/client/ui/notificationhandler.cpp

121 lines
3.1 KiB
C++
Raw Normal View History

2021-11-26 17:43:02 +03:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <QDebug>
#include "notificationhandler.h"
#if defined(Q_OS_IOS)
# include "platforms/ios/iosnotificationhandler.h"
#elif defined(Q_OS_ANDROID)
# include "platforms/android/android_notificationhandler.h"
#else
2021-11-28 17:28:25 +03:00
# include "systemtray_notificationhandler.h"
2021-11-26 17:43:02 +03:00
#endif
// static
NotificationHandler* NotificationHandler::create(QObject* parent) {
#if defined(Q_OS_IOS)
2021-11-28 17:28:25 +03:00
return new IOSNotificationHandler(parent);
2021-11-26 17:43:02 +03:00
#elif defined(Q_OS_ANDROID)
2021-11-28 17:28:25 +03:00
return new AndroidNotificationHandler(parent);
2021-11-26 17:43:02 +03:00
#else
# if defined(Q_OS_LINUX)
2022-04-14 16:41:39 -07:00
//if (LinuxSystemTrayNotificationHandler::requiredCustomImpl()) {
// return new LinuxSystemTrayNotificationHandler(parent);
//}
2021-11-26 17:43:02 +03:00
# endif
2021-11-28 17:28:25 +03:00
return new SystemTrayNotificationHandler(parent);
2021-11-26 17:43:02 +03:00
#endif
}
namespace {
NotificationHandler* s_instance = nullptr;
} // namespace
// static
NotificationHandler* NotificationHandler::instance() {
2021-11-28 17:28:25 +03:00
Q_ASSERT(s_instance);
return s_instance;
2021-11-26 17:43:02 +03:00
}
NotificationHandler::NotificationHandler(QObject* parent) : QObject(parent) {
2021-11-28 17:28:25 +03:00
Q_ASSERT(!s_instance);
s_instance = this;
2021-11-26 17:43:02 +03:00
}
NotificationHandler::~NotificationHandler() {
2021-11-28 17:28:25 +03:00
Q_ASSERT(s_instance == this);
s_instance = nullptr;
2021-11-26 17:43:02 +03:00
}
void NotificationHandler::setConnectionState(Vpn::ConnectionState state)
2021-11-26 17:43:02 +03:00
{
if (state != Vpn::ConnectionState::Connected && state != Vpn::ConnectionState::Disconnected) {
2021-11-28 17:28:25 +03:00
return;
}
2021-11-26 17:43:02 +03:00
2021-11-28 17:28:25 +03:00
QString title;
QString message;
2021-11-26 17:43:02 +03:00
2021-11-28 17:28:25 +03:00
switch (state) {
case Vpn::ConnectionState::Connected:
2021-11-28 17:28:25 +03:00
m_connected = true;
2021-11-26 17:43:02 +03:00
2021-11-28 17:28:25 +03:00
title = tr("AmneziaVPN");
message = tr("VPN Connected");
break;
2021-11-26 17:43:02 +03:00
case Vpn::ConnectionState::Disconnected:
2021-11-28 17:28:25 +03:00
if (m_connected) {
m_connected = false;
title = tr("AmneziaVPN");
message = tr("VPN Disconnected");
}
break;
2021-11-26 17:43:02 +03:00
default:
2021-11-28 17:28:25 +03:00
break;
}
2021-11-26 17:43:02 +03:00
2021-11-28 17:28:25 +03:00
Q_ASSERT(title.isEmpty() == message.isEmpty());
2021-11-26 17:43:02 +03:00
2021-11-28 17:28:25 +03:00
if (!title.isEmpty()) {
notifyInternal(VpnState, title, message, 2000);
}
2021-11-26 17:43:02 +03:00
}
void NotificationHandler::unsecuredNetworkNotification(const QString& networkName) {
2021-11-28 17:28:25 +03:00
qDebug() << "Unsecured network notification shown";
2021-11-26 17:43:02 +03:00
2021-11-28 17:28:25 +03:00
QString title = tr("AmneziaVPN notification");
2023-04-11 09:50:44 -04:00
QString message = tr("Unsecured network detected: ") + networkName;
2021-11-26 17:43:02 +03:00
2021-11-28 17:28:25 +03:00
notifyInternal(UnsecuredNetwork, title, message, 2000);
2021-11-26 17:43:02 +03:00
}
void NotificationHandler::notifyInternal(Message type, const QString& title,
const QString& message,
int timerMsec) {
2021-11-28 17:28:25 +03:00
m_lastMessage = type;
2021-11-26 17:43:02 +03:00
2021-11-28 17:28:25 +03:00
emit notificationShown(title, message);
notify(type, title, message, timerMsec);
2021-11-26 17:43:02 +03:00
}
void NotificationHandler::messageClickHandle() {
2021-11-28 17:28:25 +03:00
qDebug() << "Message clicked";
2021-11-26 17:43:02 +03:00
2021-11-28 17:28:25 +03:00
if (m_lastMessage == VpnState) {
qCritical() << "Random message clicked received";
return;
}
2021-11-26 17:43:02 +03:00
2021-11-28 17:28:25 +03:00
emit notificationClicked(m_lastMessage);
m_lastMessage = VpnState;
2021-11-26 17:43:02 +03:00
}