-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added feature for desktop notification and minimize to tray.
This should close #58.
- Loading branch information
1 parent
39c5a7b
commit 930f5ea
Showing
6 changed files
with
154 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |