Merge pull request #6 from Tucos/genversion
[quassel.git] / src / qtui / osxnotificationbackend.mm
1 /***************************************************************************
2  *   Copyright (C) 2005-2012 by the Quassel Project                        *
3  *   devel@quassel-irc.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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "clientsettings.h"
22 #include "osxnotificationbackend.h"
23
24 #include <QCheckBox>
25 #include <QHBoxLayout>
26
27 #import <Foundation/NSUserNotification.h>
28
29 namespace {
30
31 void SendNotificationCenterMessage(NSString* title, NSString* subtitle) {
32     NSUserNotificationCenter* center =
33             [NSUserNotificationCenter defaultUserNotificationCenter];
34     NSUserNotification* notification =
35             [[NSUserNotification alloc] init];
36
37     [notification setTitle: title];
38     [notification setSubtitle: subtitle];
39
40     [center deliverNotification: notification];
41
42     [notification release];
43 }
44
45 }
46
47 OSXNotificationBackend::OSXNotificationBackend(QObject *parent)
48     : AbstractNotificationBackend(parent),
49       _enabled(true)
50 {
51     NotificationSettings notificationSettings;
52     notificationSettings.initAndNotify("OSXNotification/Enabled", this, SLOT(enabledChanged(QVariant)), true);
53 }
54
55 void OSXNotificationBackend::enabledChanged(const QVariant &value)
56 {
57     _enabled = value.toBool();
58 }
59
60 void OSXNotificationBackend::notify(const Notification &notification)
61 {
62     if (!_enabled)
63     {
64         return;
65     }
66
67     NSString* message = [[NSString alloc] initWithUTF8String:notification.sender.toUtf8().constData()];
68     NSString* summary = [[NSString alloc] initWithUTF8String:notification.message.toUtf8().constData()];
69
70     SendNotificationCenterMessage(message, summary);
71
72     [message release];
73     [summary release];
74 }
75
76 void OSXNotificationBackend::close(uint notificationId)
77 {
78 }
79
80 SettingsPage *OSXNotificationBackend::createConfigWidget() const
81 {
82     return new ConfigWidget();
83 }
84
85 OSXNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
86     : SettingsPage("Internal", "OSXNotification", parent)
87 {
88     _enabledBox = new QCheckBox(tr("Show OS X notifications"));
89     connect(_enabledBox, SIGNAL(toggled(bool)), this, SLOT(widgetChanged()));
90     QHBoxLayout *layout = new QHBoxLayout(this);
91     layout->addWidget(_enabledBox);
92 }
93
94 void OSXNotificationBackend::ConfigWidget::widgetChanged()
95 {
96     bool changed = (_enabled != _enabledBox->isChecked());
97     if (changed != hasChanged())
98         setChangedState(changed);
99 }
100
101 bool OSXNotificationBackend::ConfigWidget::hasDefaults() const
102 {
103     return true;
104 }
105
106 void OSXNotificationBackend::ConfigWidget::defaults()
107 {
108     _enabledBox->setChecked(true);
109     widgetChanged();
110 }
111
112 void OSXNotificationBackend::ConfigWidget::load()
113 {
114     NotificationSettings s;
115     _enabled = s.value("OSXNotification/Enabled", false).toBool();
116     _enabledBox->setChecked(_enabled);
117     setChangedState(false);
118 }
119
120
121 void OSXNotificationBackend::ConfigWidget::save()
122 {
123     NotificationSettings s;
124     s.setValue("OSXNotification/Enabled", _enabledBox->isChecked());
125     load();
126 }