Show OS X notification center notifications
authorDavid Sansome <me@davidsansome.com>
Fri, 23 Nov 2012 14:12:35 +0000 (01:12 +1100)
committerManuel Nickschas <sputnick@quassel-irc.org>
Tue, 19 Feb 2013 21:47:37 +0000 (22:47 +0100)
CMakeLists.txt
src/qtui/CMakeLists.txt
src/qtui/mainwin.cpp
src/qtui/osxnotificationbackend.h [new file with mode: 0644]
src/qtui/osxnotificationbackend.mm [new file with mode: 0644]

index 6cdd59f..353511d 100644 (file)
@@ -68,6 +68,7 @@ option(STATIC        "Enable static building (might not be portable)" OFF)
 
 if(APPLE)
   option(DEPLOY        "Mac OS X only! Adds required libs to bundle resources and create a dmg. Note: requires Qt to be built with 10.4u SDK" OFF)
 
 if(APPLE)
   option(DEPLOY        "Mac OS X only! Adds required libs to bundle resources and create a dmg. Note: requires Qt to be built with 10.4u SDK" OFF)
+  option(WITH_NOTIFICATION_CENTER "Enable OS X Notification Center support" ON)
 endif(APPLE)
 
 # Default to embedding data in the static case
 endif(APPLE)
 
 # Default to embedding data in the static case
@@ -370,6 +371,15 @@ if(BUILD_GUI)
     set(INDICATEQT_LIBRARIES "")
   endif(WITH_LIBINDICATE AND NOT WITH_QT5)
 
     set(INDICATEQT_LIBRARIES "")
   endif(WITH_LIBINDICATE AND NOT WITH_QT5)
 
+  # Setup OS X notification center support
+  if(WITH_NOTIFICATION_CENTER AND APPLE)
+    set(HAVE_NOTIFICATION_CENTER true)
+    add_definitions(-DHAVE_NOTIFICATION_CENTER)
+    set(CLIENT_LIBRARIES ${CLIENT_LIBRARIES}
+      /System/Library/Frameworks/Foundation.framework
+    )
+  endif()
+
 endif(BUILD_GUI)
 
 # Core-only deps
 endif(BUILD_GUI)
 
 # Core-only deps
index 86217b8..8d8dce0 100644 (file)
@@ -182,6 +182,11 @@ if(INDICATEQT_FOUND)
   include_directories(${INDICATEQT_INCLUDE_DIRS})
 endif(INDICATEQT_FOUND)
 
   include_directories(${INDICATEQT_INCLUDE_DIRS})
 endif(INDICATEQT_FOUND)
 
+if(HAVE_NOTIFICATION_CENTER)
+  set(SOURCES ${SOURCES} osxnotificationbackend.mm)
+  set(MOC_HDRS ${MOC_HDRS} osxnotificationbackend.h)
+endif()
+
 foreach(FORM ${FORMS})
   set(FORMPATH ${FORMPATH} ui/${FORM})
 endforeach(FORM ${FORMS})
 foreach(FORM ${FORMS})
   set(FORMPATH ${FORMPATH} ui/${FORM})
 endforeach(FORM ${FORMS})
index ac1fd97..bf7cda3 100644 (file)
   #include "indicatornotificationbackend.h"
 #endif
 
   #include "indicatornotificationbackend.h"
 #endif
 
+#ifdef HAVE_NOTIFICATION_CENTER
+  #include "osxnotificationbackend.h"
+#endif
+
 #include "settingspages/aliasessettingspage.h"
 #include "settingspages/appearancesettingspage.h"
 #include "settingspages/backlogsettingspage.h"
 #include "settingspages/aliasessettingspage.h"
 #include "settingspages/appearancesettingspage.h"
 #include "settingspages/backlogsettingspage.h"
@@ -218,6 +222,10 @@ void MainWin::init()
     QtUi::registerNotificationBackend(new IndicatorNotificationBackend(this));
 #endif
 
     QtUi::registerNotificationBackend(new IndicatorNotificationBackend(this));
 #endif
 
+#ifdef HAVE_NOTIFICATION_CENTER
+    QtUi::registerNotificationBackend(new OSXNotificationBackend(this));
+#endif
+
     // we assume that at this point, all configurable actions are defined!
     QtUi::loadShortcuts();
 
     // we assume that at this point, all configurable actions are defined!
     QtUi::loadShortcuts();
 
diff --git a/src/qtui/osxnotificationbackend.h b/src/qtui/osxnotificationbackend.h
new file mode 100644 (file)
index 0000000..55a8d58
--- /dev/null
@@ -0,0 +1,66 @@
+/***************************************************************************
+ *   Copyright (C) 2005-2012 by the Quassel Project                        *
+ *   devel@quassel-irc.org                                                 *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) version 3.                                           *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
+ ***************************************************************************/
+
+#ifndef OSXNOTIFICATIONBACKEND_H_
+#define OSXNOTIFICATIONBACKEND_H_
+
+#include "abstractnotificationbackend.h"
+#include "settingspage.h"
+
+class OSXNotificationBackend : public AbstractNotificationBackend
+{
+    Q_OBJECT
+
+public:
+    OSXNotificationBackend(QObject *parent = 0);
+
+    void notify(const Notification &);
+    void close(uint notificationId);
+    virtual SettingsPage *createConfigWidget() const;
+
+private slots:
+    void enabledChanged(const QVariant &value);
+
+private:
+    class ConfigWidget;
+
+    bool _enabled;
+};
+
+class OSXNotificationBackend::ConfigWidget : public SettingsPage
+{
+    Q_OBJECT
+
+public:
+    ConfigWidget(QWidget *parent = 0);
+    void save();
+    void load();
+    bool hasDefaults() const;
+    void defaults();
+
+private slots:
+    void widgetChanged();
+
+private:
+    QCheckBox *_enabledBox;
+    bool _enabled;
+};
+
+#endif // OSXNOTIFICATIONBACKEND_H_
diff --git a/src/qtui/osxnotificationbackend.mm b/src/qtui/osxnotificationbackend.mm
new file mode 100644 (file)
index 0000000..738ee26
--- /dev/null
@@ -0,0 +1,126 @@
+/***************************************************************************
+ *   Copyright (C) 2005-2012 by the Quassel Project                        *
+ *   devel@quassel-irc.org                                                 *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) version 3.                                           *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
+ ***************************************************************************/
+
+#include "clientsettings.h"
+#include "osxnotificationbackend.h"
+
+#include <QCheckBox>
+#include <QHBoxLayout>
+
+#import <Foundation/NSUserNotification.h>
+
+namespace {
+
+void SendNotificationCenterMessage(NSString* title, NSString* subtitle) {
+    NSUserNotificationCenter* center =
+            [NSUserNotificationCenter defaultUserNotificationCenter];
+    NSUserNotification* notification =
+            [[NSUserNotification alloc] init];
+
+    [notification setTitle: title];
+    [notification setSubtitle: subtitle];
+
+    [center deliverNotification: notification];
+
+    [notification release];
+}
+
+}
+
+OSXNotificationBackend::OSXNotificationBackend(QObject *parent)
+    : AbstractNotificationBackend(parent),
+      _enabled(true)
+{
+    NotificationSettings notificationSettings;
+    notificationSettings.initAndNotify("OSXNotification/Enabled", this, SLOT(enabledChanged(QVariant)), true);
+}
+
+void OSXNotificationBackend::enabledChanged(const QVariant &value)
+{
+    _enabled = value.toBool();
+}
+
+void OSXNotificationBackend::notify(const Notification &notification)
+{
+    if (!_enabled)
+    {
+        return;
+    }
+
+    NSString* message = [[NSString alloc] initWithUTF8String:notification.sender.toUtf8().constData()];
+    NSString* summary = [[NSString alloc] initWithUTF8String:notification.message.toUtf8().constData()];
+
+    SendNotificationCenterMessage(message, summary);
+
+    [message release];
+    [summary release];
+}
+
+void OSXNotificationBackend::close(uint notificationId)
+{
+}
+
+SettingsPage *OSXNotificationBackend::createConfigWidget() const
+{
+    return new ConfigWidget();
+}
+
+OSXNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
+    : SettingsPage("Internal", "OSXNotification", parent)
+{
+    _enabledBox = new QCheckBox(tr("Show OS X notifications"));
+    connect(_enabledBox, SIGNAL(toggled(bool)), this, SLOT(widgetChanged()));
+    QHBoxLayout *layout = new QHBoxLayout(this);
+    layout->addWidget(_enabledBox);
+}
+
+void OSXNotificationBackend::ConfigWidget::widgetChanged()
+{
+    bool changed = (_enabled != _enabledBox->isChecked());
+    if (changed != hasChanged())
+        setChangedState(changed);
+}
+
+bool OSXNotificationBackend::ConfigWidget::hasDefaults() const
+{
+    return true;
+}
+
+void OSXNotificationBackend::ConfigWidget::defaults()
+{
+    _enabledBox->setChecked(true);
+    widgetChanged();
+}
+
+void OSXNotificationBackend::ConfigWidget::load()
+{
+    NotificationSettings s;
+    _enabled = s.value("OSXNotification/Enabled", false).toBool();
+    _enabledBox->setChecked(_enabled);
+    setChangedState(false);
+}
+
+
+void OSXNotificationBackend::ConfigWidget::save()
+{
+    NotificationSettings s;
+    s.setValue("OSXNotification/Enabled", _enabledBox->isChecked());
+    load();
+}