mark string untranslatable
[quassel.git] / src / qtui / snorenotificationbackend.cpp
1 /***************************************************************************
2 *   Copyright (C) 2011-2013 by Patrick von Reth                           *
3 *   vonreth@kde.org                                                       *
4 *                                                                         *
5 *   This program is free software; you can redistribute it and/or modify  *
6 *   it under the terms of the GNU General Public License as published by  *
7 *   the Free Software Foundation; either version 2 of the License, or     *
8 *   (at your option) version 3.                                           *
9 *                                                                         *
10 *   This program is distributed in the hope that it will be useful,       *
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13 *   GNU General Public License for more details.                          *
14 *                                                                         *
15 *   You should have received a copy of the GNU General Public License     *
16 *   along with this program; if not, write to the                         *
17 *   Free Software Foundation, Inc.,                                       *
18 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19 ***************************************************************************/
20
21 #include "snorenotificationbackend.h"
22
23 #include <QtGui>
24 #include <QtGlobal>
25 #include <QDebug>
26
27 #include "client.h"
28 #include "iconloader.h"
29 #include "networkmodel.h"
30 #include "systraynotificationbackend.h"
31 #include "qtui.h"
32
33 #include <iostream>
34
35
36 #include <snore/core/snore.h>
37 #include <snore/core/notification/notification.h>
38
39
40 SnoreNotificationBackend::SnoreNotificationBackend (QObject *parent)
41     :AbstractNotificationBackend(parent),
42       m_systrayBackend(NULL)
43 {
44     NotificationSettings notificationSettings;
45     QString backend = notificationSettings.value("Snore/Backend", "SystemTray").toString();
46     m_timeout = notificationSettings.value("Snore/Timeout",10).toInt();
47
48     notificationSettings.notify("Snore/Backend", this, SLOT(backendChanged(const QVariant &)));
49     notificationSettings.notify("Snore/Backend", this, SLOT(timeoutChanged(const QVariant &)));
50
51     //TODO: try to get an instance of the tray icon to be able to show popups
52     m_snore = new Snore::SnoreCore();
53     m_snore->hints().setValue("WINDOWS_APP_ID","QuasselProject.QuasselIRC");
54     m_snore->loadPlugins(Snore::PluginContainer::BACKEND);
55     Snore::Application *a = new Snore::Application("Quassel",Snore::Icon(DesktopIcon("quassel").toImage()));
56
57     connect(m_snore,SIGNAL(actionInvoked(Snore::Notification)),this,SLOT(actionInvoked(Snore::Notification)));
58
59     m_icon = Snore::Icon(DesktopIcon("dialog-information").toImage());
60
61     a->addAlert(new Snore::Alert(tr("Private Message"),tr("Private Message")));
62
63     m_snore->addApplication(a);
64     m_snore->applicationIsInitialized (a);
65
66     backendChanged(QVariant::fromValue(backend));
67 }
68
69 SnoreNotificationBackend::~SnoreNotificationBackend(){
70     m_snore->removeApplication("Quassel");
71     m_snore->deleteLater();
72 }
73
74 void SnoreNotificationBackend::backendChanged(const QVariant &v){
75     QString backend = v.toString();
76     if(backend == "SystemTray"){
77         if(m_systrayBackend == NULL){
78             m_systrayBackend = new SystrayNotificationBackend(this);
79             QtUi::registerNotificationBackend(m_systrayBackend);
80         }
81     }else{
82         if(m_systrayBackend != NULL){
83             QtUi::unregisterNotificationBackend(m_systrayBackend);
84             delete m_systrayBackend;
85             m_systrayBackend = NULL;
86         }
87         m_snore->setPrimaryNotificationBackend(backend);
88     }
89 }
90
91 void SnoreNotificationBackend::timeoutChanged(const QVariant &v){
92     m_timeout = v.toInt();
93 }
94
95 void SnoreNotificationBackend::notify(const Notification &n){
96     if(m_systrayBackend != NULL)
97         return;
98     QString title = Client::networkModel()->networkName(n.bufferId) + " - " + Client::networkModel()->bufferName(n.bufferId);
99     QString message = QString("<%1> %2").arg(n.sender, n.message);
100     Snore::Notification noti("Quassel",tr("Private Message"),title,message,m_icon,m_timeout);
101     noti.hints().setValue("QUASSEL_ID",n.notificationId);
102     m_notificationIds.insert(n.notificationId,noti.id());
103     m_snore->broadcastNotification(noti);
104 }
105
106 void SnoreNotificationBackend::close(uint notificationId){
107     if(m_systrayBackend != NULL)
108         return;
109     Snore::Notification n = m_snore->getActiveNotificationByID(m_notificationIds.take(notificationId));
110     m_snore->requestCloseNotification(n,Snore::NotificationEnums::CloseReasons::CLOSED);
111 }
112
113 void SnoreNotificationBackend::actionInvoked(Snore::Notification n){
114     emit activated(n.hints().value("QUASSEL_ID").toUInt());
115 }
116
117 SettingsPage *SnoreNotificationBackend::createConfigWidget()const{
118     return new ConfigWidget(m_snore);
119 }
120
121
122 /***************************************************************************/
123
124 SnoreNotificationBackend::ConfigWidget::ConfigWidget(Snore::SnoreCore *snore,QWidget *parent)
125     :SettingsPage("Internal", "SnoreNotification", parent),
126       m_snore(snore)
127 {
128     ui.setupUi(this);
129     ui.backends->insertItems(0,m_snore->notificationBackends());
130
131     connect(ui.backends, SIGNAL(currentIndexChanged(QString)), SLOT(backendChanged(QString)));
132     connect(ui.timeout,SIGNAL(valueChanged(int)),this,SLOT(timeoutChanged(int)));
133 }
134
135 void SnoreNotificationBackend::ConfigWidget::backendChanged(const QString &b){
136         ui.backends->setCurrentIndex(ui.backends->findText(b));
137         setChangedState(true);
138 }
139
140 void SnoreNotificationBackend::ConfigWidget::timeoutChanged(int i){
141     ui.timeout->setValue(i);
142     setChangedState(true);
143
144 }
145
146 bool SnoreNotificationBackend::ConfigWidget::hasDefaults() const {
147     return true;
148 }
149
150 void SnoreNotificationBackend::ConfigWidget::defaults() {
151     backendChanged("SystemTray");
152     timeoutChanged(10);
153 }
154
155 void SnoreNotificationBackend::ConfigWidget::load() {
156     NotificationSettings s;
157     QString backend = s.value("Snore/Backend", "SystemTray").toString();
158     int timeout = s.value("Snore/Timeout",10).toInt();
159     ui.backends->setCurrentIndex(ui.backends->findText(backend));
160     ui.timeout->setValue(timeout);
161     setChangedState(false);
162 }
163
164 void SnoreNotificationBackend::ConfigWidget::save() {
165     NotificationSettings s;
166     s.setValue("Snore/Backend", ui.backends->currentText());
167     s.setValue("Snore/Timeout",ui.timeout->value());
168     load();
169 }