Skip to content

Commit

Permalink
Added feature for desktop notification and minimize to tray.
Browse files Browse the repository at this point in the history
This should close #58.
  • Loading branch information
blizzard4591 committed Apr 5, 2020
1 parent 39c5a7b commit 930f5ea
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ m_databaseThread(),
m_databasePointerAuthority(),
m_databaseWrapper(&m_databasePointerAuthority),
m_audioNotifier(std::make_shared<openmittsu::utility::AudioNotification>()),
m_desktopNotifier(),
m_optionTryEmptyPassword(false),
m_optionAutoConnect(false),
m_optionMinimize(false),
Expand Down Expand Up @@ -275,6 +276,10 @@ Client::~Client() {
}

void Client::delayedStartup() {
// Restore Window location and size
restoreGeometry(m_optionMaster->getOptionAsQByteArray(openmittsu::options::Options::BINARY_MAINWINDOW_GEOMETRY));
restoreState(m_optionMaster->getOptionAsQByteArray(openmittsu::options::Options::BINARY_MAINWINDOW_STATE));

QString const databaseFile = m_optionMaster->getOptionAsQString(openmittsu::options::Options::FILEPATH_DATABASE);
QString const legacyClientConfiguration = m_optionMaster->getOptionAsQString(openmittsu::options::Options::FILEPATH_LEGACY_CLIENT_CONFIGURATION);
bool showFirstUseWizard = false;
Expand Down Expand Up @@ -326,22 +331,26 @@ void Client::delayedStartup() {
}
}

// Restore Window location and size
restoreGeometry(m_optionMaster->getOptionAsQByteArray(openmittsu::options::Options::BINARY_MAINWINDOW_GEOMETRY));
restoreState(m_optionMaster->getOptionAsQByteArray(openmittsu::options::Options::BINARY_MAINWINDOW_STATE));

// Autoconnect if asked to
if (m_optionAutoConnect) {
QTimer::singleShot(0, this, SLOT(btnConnectOnClick()));
}

m_desktopNotifier = std::make_shared<openmittsu::widgets::DesktopNotification>(this, openmittsu::options::OptionReaderFactory(m_databasePointerAuthority.getDatabaseWrapperFactory()));

// Minimize if asked to
if (m_optionMinimize) {
this->showMinimized();
}
}

void Client::closeEvent(QCloseEvent* event) {
if (m_optionMaster->getOptionAsBool(openmittsu::options::Options::BOOLEAN_MINIMIZE_TO_TRAY) && !m_desktopNotifier->isClosing()) {
this->hide();
event->ignore();
return;
}

// Save location and size of window
m_optionMaster->setOption(openmittsu::options::Options::BINARY_MAINWINDOW_GEOMETRY, saveGeometry());
m_optionMaster->setOption(openmittsu::options::Options::BINARY_MAINWINDOW_STATE, saveState());
Expand Down Expand Up @@ -685,6 +694,13 @@ void Client::onDatabaseReceivedNewContactMessage(openmittsu::protocol::ContactId
chatTab = m_tabController->getTab(identity);
}
onHasUnreadMessage(chatTab);
if (m_optionMaster->getOptionAsBool(openmittsu::options::Options::BOOLEAN_NOTIFICATION_ON_MESSAGE_RECEIVED)) {
if (m_desktopNotifier) {
auto const contactData = m_databaseWrapper.getContactData(identity, false);
QString const contactName = contactData.nickName;
m_desktopNotifier->showNotificationContact(contactName);
}
}
}
}

Expand All @@ -699,6 +715,13 @@ void Client::onDatabaseReceivedNewGroupMessage(openmittsu::protocol::GroupId con
chatTab = m_tabController->getTab(group);
}
onHasUnreadMessage(chatTab);
if (m_optionMaster->getOptionAsBool(openmittsu::options::Options::BOOLEAN_NOTIFICATION_ON_MESSAGE_RECEIVED)) {
if (m_desktopNotifier) {
auto const groupData = m_databaseWrapper.getGroupData(group, false);
QString const groupName = groupData.title;
m_desktopNotifier->showNotificationGroup(groupName);
}
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "src/utility/AudioNotification.h"
#include "src/utility/ThreadContainer.h"

#include "src/widgets/DesktopNotification.h"
#include "src/widgets/TabController.h"

namespace Ui {
Expand Down Expand Up @@ -139,6 +140,7 @@ public slots:
openmittsu::database::DatabaseWrapper m_databaseWrapper;

std::shared_ptr<openmittsu::utility::AudioNotification> m_audioNotifier;
std::shared_ptr<openmittsu::widgets::DesktopNotification> m_desktopNotifier;

// Commandline options
bool m_optionTryEmptyPassword;
Expand Down
2 changes: 2 additions & 0 deletions src/options/OptionReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,13 @@ namespace openmittsu {
target->registerOption(OptionGroups::GROUP_NOTIFICATIONS, Options::BOOLEAN_FORCE_FOREGROUND_ON_MESSAGE_RECEIVED, QStringLiteral("options/forceForegroundOnMessageReceived"), tr("Force window into foreground on incoming message"), true, OptionTypes::TYPE_BOOL, OptionStorage::STORAGE_DATABASE);
target->registerOption(OptionGroups::GROUP_NOTIFICATIONS, Options::BOOLEAN_PLAY_SOUND_ON_MESSAGE_RECEIVED, QStringLiteral("options/playSoundOnMessageReceived"), tr("Play sound on incoming message"), true, OptionTypes::TYPE_BOOL, OptionStorage::STORAGE_DATABASE);
target->registerOption(OptionGroups::GROUP_NOTIFICATIONS, Options::BOOLEAN_BLINK_ICON_ON_MESSAGE_RECEIVED, QStringLiteral("options/blinkIconOnMessageReceived"), tr("Blink the App icon on incoming message"), true, OptionTypes::TYPE_BOOL, OptionStorage::STORAGE_DATABASE);
target->registerOption(OptionGroups::GROUP_NOTIFICATIONS, Options::BOOLEAN_NOTIFICATION_ON_MESSAGE_RECEIVED, QStringLiteral("options/notificationOnMessageReceived"), tr("Show notification on incoming message"), true, OptionTypes::TYPE_BOOL, OptionStorage::STORAGE_DATABASE);
target->registerOption(OptionGroups::GROUP_GENERAL, Options::BOOLEAN_RECONNECT_ON_CONNECTION_LOSS, QStringLiteral("options/reconnectOnConnectionLoss"), tr("Automatically attempt reconnect on a connection loss"), true, OptionTypes::TYPE_BOOL, OptionStorage::STORAGE_DATABASE);
target->registerOption(OptionGroups::GROUP_GENERAL, Options::BOOLEAN_TRUST_OTHERS, QStringLiteral("options/trustOthers"), tr("Accept messages from users whose group membership has not (yet) been confirmed"), false, OptionTypes::TYPE_BOOL, OptionStorage::STORAGE_DATABASE);
target->registerOption(OptionGroups::GROUP_GENERAL, Options::BOOLEAN_UPDATE_FEATURE_LEVEL, QStringLiteral("options/updateFeatureLevel"), tr("Increase identity feature level to software feature level if possible"), true, OptionTypes::TYPE_BOOL, OptionStorage::STORAGE_DATABASE);
target->registerOption(OptionGroups::GROUP_GENERAL, Options::FILEPATH_DATABASE, QStringLiteral("options/database/databaseFile"), tr("Main database file path"), "", OptionTypes::TYPE_FILEPATH, OptionStorage::STORAGE_SIMPLE);
target->registerOption(OptionGroups::GROUP_GENERAL, Options::BOOLEAN_IGNORE_LEGACY_CONTACTS_DATABASE, QStringLiteral("options/ignoreLegacyContactsDatabase"), tr("Ignore still configured legacy contact database files (relevant when still using old versions in parallel)"), false, OptionTypes::TYPE_BOOL, OptionStorage::STORAGE_DATABASE);
target->registerOption(OptionGroups::GROUP_GENERAL, Options::BOOLEAN_MINIMIZE_TO_TRAY, QStringLiteral("options/minimizeToTray"), tr("Minimize to tray instead of closing the app entirely when clicking the close icon"), true, OptionTypes::TYPE_BOOL, OptionStorage::STORAGE_SIMPLE);
target->registerOption(OptionGroups::GROUP_INTERNAL, Options::BINARY_MAINWINDOW_GEOMETRY, QStringLiteral("options/internal/clientMainWindowGeometry"), "", QByteArray(), OptionTypes::TYPE_BINARY, OptionStorage::STORAGE_SIMPLE);
target->registerOption(OptionGroups::GROUP_INTERNAL, Options::BINARY_MAINWINDOW_STATE, QStringLiteral("options/internal/clientMainWindowState"), "", QByteArray(), OptionTypes::TYPE_BINARY, OptionStorage::STORAGE_SIMPLE);
target->registerOption(OptionGroups::GROUP_INTERNAL, Options::FILEPATH_LEGACY_CONTACTS_DATABASE, QStringLiteral("options/database/contactsFile"), "", "", OptionTypes::TYPE_FILEPATH, OptionStorage::STORAGE_SIMPLE);
Expand Down
2 changes: 2 additions & 0 deletions src/options/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ namespace openmittsu {
BOOLEAN_FORCE_FOREGROUND_ON_MESSAGE_RECEIVED,
BOOLEAN_PLAY_SOUND_ON_MESSAGE_RECEIVED,
BOOLEAN_BLINK_ICON_ON_MESSAGE_RECEIVED,
BOOLEAN_NOTIFICATION_ON_MESSAGE_RECEIVED,
BOOLEAN_RECONNECT_ON_CONNECTION_LOSS,
BOOLEAN_UPDATE_FEATURE_LEVEL,
BOOLEAN_TRUST_OTHERS,
BOOLEAN_IGNORE_LEGACY_CONTACTS_DATABASE,
BOOLEAN_MINIMIZE_TO_TRAY,
FILEPATH_DATABASE,
FILEPATH_LEGACY_CLIENT_CONFIGURATION,
FILEPATH_LEGACY_CONTACTS_DATABASE,
Expand Down
77 changes: 77 additions & 0 deletions src/widgets/DesktopNotification.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "src/widgets/DesktopNotification.h"

#include "src/exceptions/InternalErrorException.h"
#include "src/utility/MakeUnique.h"
#include "src/utility/QObjectConnectionMacro.h"
#include "src/utility/Logging.h"

#include <QApplication>
#include <QIcon>

namespace openmittsu {
namespace widgets {

DesktopNotification::DesktopNotification(QMainWindow* mainWindow, openmittsu::options::OptionReaderFactory const& optionReaderFactory) : QObject(), m_mainWindow(mainWindow), m_trayIcon(QIcon(":/icons/icon.png"), this), m_contextMenu(), m_optionReader(optionReaderFactory.getOptionReader()), m_isClosing(false) {
if (QSystemTrayIcon::isSystemTrayAvailable()) {
LOGGER()->info("System Tray is available.");
OPENMITTSU_CONNECT(&m_trayIcon, activated(QSystemTrayIcon::ActivationReason), this, trayIcon_onActivated(QSystemTrayIcon::ActivationReason));

QAction* exitAction = new QAction(QString(tr("Close")), &m_contextMenu);
OPENMITTSU_CONNECT(exitAction, triggered(bool), this, trayMenu_exitOnTriggered());

m_contextMenu.addAction(exitAction);
m_trayIcon.setContextMenu(&m_contextMenu);
m_trayIcon.show();
} else {
LOGGER()->warn("System Tray is not available.");
}

if (QSystemTrayIcon::supportsMessages()) {
LOGGER()->info("System Tray supports messages.");
} else {
LOGGER()->warn("System Tray does not support messages.");
}
}

DesktopNotification::~DesktopNotification() {
//
}

void DesktopNotification::showNotificationContact(QString const& contactName) {
if (QSystemTrayIcon::supportsMessages()) {
m_trayIcon.showMessage("OpenMittsu", QString(tr("New message from %1")).arg(contactName));
}
}

void DesktopNotification::showNotificationGroup(QString const& groupName) {
if (QSystemTrayIcon::supportsMessages()) {
// Sadly, the handler currently does not know the contact sending the message...
//m_trayIcon.showMessage("OpenMittsu", QString(tr("New message from %1 in group %2")).arg(contactName).arg(groupName));
m_trayIcon.showMessage("OpenMittsu", QString(tr("New message in group %1")).arg(groupName));
}
}

void DesktopNotification::trayMenu_exitOnTriggered() {
m_isClosing = true;
m_mainWindow->close();
}

bool DesktopNotification::isClosing() const {
return m_isClosing;
}

void DesktopNotification::trayIcon_onActivated(QSystemTrayIcon::ActivationReason reason) {
if (reason == QSystemTrayIcon::Trigger) {
if (m_mainWindow->isVisible()) {
LOGGER()->info("Minimizing to Tray.");
m_mainWindow->hide();
} else {
LOGGER()->info("Maximizing from Tray.");
m_mainWindow->show();
m_mainWindow->activateWindow();
}
}
}

}
}
44 changes: 44 additions & 0 deletions src/widgets/DesktopNotification.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef OPENMITTSU_WIDGETS_DESKTOPNOTIFICATION_H_
#define OPENMITTSU_WIDGETS_DESKTOPNOTIFICATION_H_

#include <QAction>
#include <QMainWindow>
#include <QMenu>
#include <QObject>
#include <QString>
#include <QSystemTrayIcon>

#include <memory>

#include "src/options/OptionReaderFactory.h"
#include "src/options/OptionReader.h"

namespace openmittsu {
namespace widgets {

class DesktopNotification : public QObject {
Q_OBJECT
public:
DesktopNotification(QMainWindow* mainWindow, openmittsu::options::OptionReaderFactory const& optionReaderFactory);
virtual ~DesktopNotification();

bool isClosing() const;
public slots:
void showNotificationContact(QString const& contactName);
void showNotificationGroup(QString const& groupName);
private slots:
void trayMenu_exitOnTriggered();
void trayIcon_onActivated(QSystemTrayIcon::ActivationReason reason);
private:
QMainWindow* m_mainWindow;
QSystemTrayIcon m_trayIcon;
QMenu m_contextMenu;
std::unique_ptr<openmittsu::options::OptionReader> m_optionReader;

bool m_isClosing;
};

}
}

#endif // OPENMITTSU_WIDGETS_DESKTOPNOTIFICATION_H_

0 comments on commit 930f5ea

Please sign in to comment.