1 /*
2     Copyright © 2019 by The qTox Project Contributors
3 
4     This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6     qTox is libre software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10 
11     qTox is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with qTox.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "desktopnotify.h"
21 
22 #include <src/persistence/settings.h>
23 
24 #include <libsnore/snore.h>
25 
26 #include <QDebug>
27 
DesktopNotify()28 DesktopNotify::DesktopNotify()
29     : notifyCore{Snore::SnoreCore::instance()}
30     , snoreIcon{":/img/icons/qtox.svg"}
31 {
32 
33     notifyCore.loadPlugins(Snore::SnorePlugin::Backend);
34     qDebug() << "primary notification backend:" << notifyCore.primaryNotificationBackend();
35 
36     snoreApp = Snore::Application("qTox", snoreIcon);
37 
38     notifyCore.registerApplication(snoreApp);
39 }
40 
createNotification(const QString & title,const QString & text,Snore::Icon & icon)41 void DesktopNotify::createNotification(const QString& title, const QString& text, Snore::Icon& icon)
42 {
43     const Settings& s = Settings::getInstance();
44     if(!(s.getNotify() && s.getDesktopNotify())) {
45         return;
46     }
47 
48     Snore::Notification notify{snoreApp, Snore::Alert(), title, text, icon};
49 
50     notifyCore.broadcastNotification(notify);
51 }
52 
notifyMessage(const QString & title,const QString & message)53 void DesktopNotify::notifyMessage(const QString& title, const QString& message)
54 {
55     createNotification(title, message, snoreIcon);
56 }
57 
notifyMessagePixmap(const QString & title,const QString & message,QPixmap avatar)58 void DesktopNotify::notifyMessagePixmap(const QString& title, const QString& message, QPixmap avatar)
59 {
60     Snore::Icon new_icon(avatar);
61     createNotification(title, message, new_icon);
62 }
63 
notifyMessageSimple(const MessageType type)64 void DesktopNotify::notifyMessageSimple(const MessageType type)
65 {
66     QString message;
67     switch (type) {
68     case MessageType::FRIEND: message = tr("New message"); break;
69     case MessageType::FRIEND_FILE: message = tr("Incoming file transfer"); break;
70     case MessageType::FRIEND_REQUEST: message = tr("Friend request received"); break;
71     case MessageType::GROUP: message = tr("New group message"); break;
72     case MessageType::GROUP_INVITE: message = tr("Group invite received"); break;
73     default: break;
74     }
75 
76     createNotification(message, {}, snoreIcon);
77 }
78