snorenotify/src/trayicon.cpp

131 lines
4.2 KiB
C++
Raw Normal View History

2013-07-07 10:33:14 +02:00
/*
SnoreNotify is a Notification Framework based on Qt
2014-01-10 18:36:33 +01:00
Copyright (C) 2013-2014 Patrick von Reth <vonreth@kde.org>
2013-07-07 10:33:14 +02:00
2013-07-07 12:38:39 +02:00
SnoreNotify is free software: you can redistribute it and/or modify
2013-07-07 10:33:14 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2013-07-07 12:38:39 +02:00
SnoreNotify is distributed in the hope that it will be useful,
2013-07-07 10:33:14 +02:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
2013-07-07 12:38:39 +02:00
along with SnoreNotify. If not, see <http://www.gnu.org/licenses/>.
2013-07-07 10:33:14 +02:00
*/
#include "trayicon.h"
2012-01-30 22:10:32 +01:00
#include "core/snore.h"
2014-01-14 10:10:58 +01:00
#include "core/snore_p.h"
#include <QSystemTrayIcon>
#include <QMenu>
#include <QAction>
2013-07-23 01:10:04 +02:00
#include "core/version.h"
2011-11-09 23:13:46 +01:00
using namespace Snore;
2014-01-12 18:00:45 +01:00
TrayIcon::TrayIcon():
2014-01-19 17:43:23 +01:00
m_trayIcon(new QSystemTrayIcon(QIcon(":/root/snore.png"))),
m_app("SnoreNotify Test", Icon(":/root/snore.png")),
m_alert("Default", Icon(":/root/snore.png"))
{
2014-01-19 17:43:23 +01:00
m_app.addAlert(m_alert);
}
2014-01-12 18:00:45 +01:00
void TrayIcon::initConextMenu(SnoreCore *snore)
{
m_snore = snore;
m_trayIcon->setVisible(true);
m_trayMenu = new QMenu("SnoreNotify");
2014-07-18 11:42:45 +02:00
QString version = QString("SnoreNotify %1").arg(Version::version());
if (Version::revision() != "") {
2014-07-18 11:42:45 +02:00
version += QString("-%1").arg(Version::revision());
}
m_trayMenu->addAction(version);
2014-01-12 18:00:45 +01:00
m_trayMenu->addSeparator();
m_trayMenu->addAction("Test Notification", this, SLOT(slotTestNotification()));
m_trayMenu->addSeparator();
2014-02-04 16:35:16 +01:00
m_backendActions = new QActionGroup(m_trayMenu);
m_backendActions->setExclusive(true);
foreach(const QString & back, m_snore->notificationBackends()) {
2014-01-12 18:00:45 +01:00
QAction *b = m_trayMenu->addAction(back, this, SLOT(setPrimaryBackend()));
2012-01-28 15:21:04 +01:00
b->setCheckable(true);
if (back == m_snore->primaryNotificationBackend()) {
2012-01-28 15:21:04 +01:00
b->setChecked(true);
2014-01-12 18:00:45 +01:00
}
2014-02-04 16:35:16 +01:00
m_backendActions->addAction(b);
}
2014-01-12 18:00:45 +01:00
m_trayMenu->addSeparator();
m_trayMenu->addAction("Exit", qApp, SLOT(quit()));
2014-01-12 18:00:45 +01:00
m_trayIcon->setContextMenu(m_trayMenu);
}
2014-01-12 18:00:45 +01:00
void TrayIcon::hide()
{
m_trayIcon->setVisible(false);
}
QSystemTrayIcon *TrayIcon::trayIcon()
2014-01-12 18:00:45 +01:00
{
return m_trayIcon;
}
void TrayIcon::setPrimaryBackend()
{
QAction *a = qobject_cast<QAction *>(sender());
2014-01-12 18:00:45 +01:00
m_snore->setPrimaryNotificationBackend(a->text());
2011-08-12 14:51:20 +02:00
foreach(QAction * action, m_backendActions->actions()) {
if (action->text() == m_snore->primaryNotificationBackend()) {
2014-02-04 16:35:16 +01:00
action->setChecked(true);
break;
}
2012-01-28 15:21:04 +01:00
}
}
void TrayIcon::slotTestNotification()
{
2014-01-19 17:43:23 +01:00
if (!m_snore->aplications().contains(m_app.name())) {
2014-01-19 17:43:23 +01:00
m_snore->registerApplication(m_app);
}
Notification noti(m_app, m_alert, "Hello World",
"<i>This is Snore</i><br>"
"<a href=\"https://github.com/TheOneRing/Snorenotify\">Project Website</a><br>"
"1<br>"
"2<br>"
"3<br>"
"4<br>"
"5<br>", Icon(":/root/snore.png"));
noti.addAction(Action(1, "Test Action"));
m_snore->broadcastNotification(noti);
2014-01-19 17:43:23 +01:00
QTimer *timer = new QTimer(this);
m_notifications[timer] = noti;
timer->setSingleShot(true);
timer->setInterval(noti.timeout() / 2 * 1000);
connect(timer, SIGNAL(timeout()), this, SLOT(sloutUpdateTestNotification()));
timer->start();
2014-01-19 17:43:23 +01:00
// m_snore->deregisterApplication(app);
}
void TrayIcon::sloutUpdateTestNotification()
{
QTimer *timer = qobject_cast<QTimer *>(sender());
Notification noti = m_notifications.take(timer);
Notification update(noti, "Hello World",
"<b>This is Snore</b><br>"
2014-02-20 20:40:04 +01:00
"<u>This icon is quite a long line of text, isnt it I think it is what do you think? btw the icon should be in color</u><br>"
"<a href=\"https://github.com/TheOneRing/Snorenotify\">Project Website</a>", Icon("http://jweatherwatch.googlecode.com/svn/trunk/iconset/04.png"));
2014-01-19 17:43:23 +01:00
m_snore->broadcastNotification(update);
timer->deleteLater();
}