1 /***************************************************************************
2  *   Copyright (C) 2014 by Marcin Ziemiński <zieminn@gmail.com>            *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
18  ***************************************************************************/
19 
20 
21 #include "otr-notifications.h"
22 #include "chat-widget.h"
23 
24 #include <TelepathyQt/AvatarData>
25 
26 #include <QWidget>
27 #include <QObject>
28 
29 #include <KNotification>
30 #include <KLocalizedString>
31 
32 namespace OTRNotifications
33 {
34 
prepareNotification(QWidget * widget,const Tp::ContactPtr & contact)35     static KNotification* prepareNotification(QWidget *widget, const Tp::ContactPtr &contact)
36     {
37         const QString notificationType = QLatin1String("kde_telepathy_info_event");
38 
39         KNotification *notification = new KNotification(
40                 notificationType, widget,
41                 KNotification::RaiseWidgetOnActivation
42                 | KNotification::CloseWhenWidgetActivated
43                 | KNotification::CloseOnTimeout);
44 
45         notification->setComponentName(QStringLiteral("ktelepathy"));
46 
47         notification->setActions(QStringList(i18n("View")));
48 
49         QPixmap notificationPixmap;
50         if(notificationPixmap.load(contact->avatarData().fileName)) {
51             notification->setPixmap(notificationPixmap);
52         }
53 
54         return notification;
55     }
56 
otrSessionStarted(ChatWidget * widget,const Tp::ContactPtr & targetContact,bool verified)57     void otrSessionStarted(ChatWidget *widget, const Tp::ContactPtr &targetContact, bool verified)
58     {
59         KNotification *notification = prepareNotification(widget, targetContact);
60         if(verified) {
61             notification->setText(i18n("Private OTR session started with %1", targetContact->alias()));
62         } else {
63             notification->setText(i18n("Unverified OTR session started with %1", targetContact->alias()));
64         }
65 
66         if(widget) {
67             QObject::connect(notification, SIGNAL(activated(uint)), widget, SIGNAL(notificationClicked()));
68             QObject::connect(notification, SIGNAL(activated(uint)), notification, SLOT(close()));
69         }
70 
71         notification->sendEvent();
72     }
73 
otrSessionFinished(ChatWidget * widget,const Tp::ContactPtr & targetContact)74     void otrSessionFinished(ChatWidget *widget, const Tp::ContactPtr &targetContact)
75     {
76         KNotification *notification = prepareNotification(widget, targetContact);
77         notification->setText(i18n("Finished OTR session with %1", targetContact->alias()));
78 
79         if(widget) {
80             QObject::connect(notification, SIGNAL(activated(uint)), widget, SIGNAL(notificationClicked()));
81             QObject::connect(notification, SIGNAL(activated(uint)), notification, SLOT(close()));
82         }
83         notification->sendEvent();
84     }
85 
authenticationRequested(QWidget * widget,const Tp::ContactPtr & targetContact)86     void authenticationRequested(QWidget *widget, const Tp::ContactPtr &targetContact)
87     {
88         KNotification *notification = prepareNotification(widget, targetContact);
89         notification->setText(i18n("%1 has requested your authentication", targetContact->alias()));
90 
91         if(widget) {
92             QObject::connect(notification, SIGNAL(activated(uint)), widget, SLOT(notificationActivated(uint)));
93             QObject::connect(notification, SIGNAL(activated(uint)), notification, SLOT(close()));
94         }
95 
96         notification->sendEvent();
97     }
98 
authenticationConcluded(QWidget * widget,const Tp::ContactPtr & targetContact,bool success)99     void authenticationConcluded(QWidget *widget, const Tp::ContactPtr &targetContact, bool success)
100     {
101         KNotification *notification = prepareNotification(widget, targetContact);
102         if(success) {
103             notification->setText(i18n("Authentication with %1 completed successfully", targetContact->alias()));
104         } else {
105             notification->setText(i18n("Authentication with %1 failed", targetContact->alias()));
106         }
107 
108         if(widget) {
109             QObject::connect(notification, SIGNAL(activated(uint)), widget, SLOT(notificationActivated(uint)));
110             QObject::connect(notification, SIGNAL(activated(uint)), notification, SLOT(close()));
111         }
112 
113         notification->sendEvent();
114     }
115 
authenticationAborted(QWidget * widget,const Tp::ContactPtr & targetContact)116     void authenticationAborted(QWidget *widget, const Tp::ContactPtr &targetContact)
117     {
118         KNotification *notification = prepareNotification(widget, targetContact);
119         notification->setText(i18n("Authentication with %1 was aborted", targetContact->alias()));
120 
121         if(widget) {
122             QObject::connect(notification, SIGNAL(activated(uint)), widget, SLOT(notificationActivated(uint)));
123             QObject::connect(notification, SIGNAL(activated(uint)), notification, SLOT(close()));
124         }
125 
126         notification->sendEvent();
127     }
128 
authenticationFailed(QWidget * widget,const Tp::ContactPtr & targetContact)129     void authenticationFailed(QWidget *widget, const Tp::ContactPtr &targetContact)
130     {
131         KNotification *notification = prepareNotification(widget, targetContact);
132         notification->setText(i18n("Authentication with %1 failed", targetContact->alias()));
133 
134         if(widget) {
135             QObject::connect(notification, SIGNAL(activated(uint)), widget, SLOT(notificationActivated(uint)));
136             QObject::connect(notification, SIGNAL(activated(uint)), notification, SLOT(close()));
137         }
138 
139         notification->sendEvent();
140     }
141 }
142