From: David Sansome Date: Fri, 23 Nov 2012 14:12:35 +0000 (+1100) Subject: Show OS X notification center notifications X-Git-Tag: 0.9-beta1~22 X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=commitdiff_plain;h=771fdcaf0d0c9bbd981a72c5861222f58241829b;hp=4676ff82af669595edaf090c97a28161d67782a1 Show OS X notification center notifications --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 6cdd59fd..353511d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) + option(WITH_NOTIFICATION_CENTER "Enable OS X Notification Center support" ON) 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) + # 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 diff --git a/src/qtui/CMakeLists.txt b/src/qtui/CMakeLists.txt index 86217b8f..8d8dce05 100644 --- a/src/qtui/CMakeLists.txt +++ b/src/qtui/CMakeLists.txt @@ -182,6 +182,11 @@ if(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}) diff --git a/src/qtui/mainwin.cpp b/src/qtui/mainwin.cpp index ac1fd977..bf7cda34 100644 --- a/src/qtui/mainwin.cpp +++ b/src/qtui/mainwin.cpp @@ -108,6 +108,10 @@ #include "indicatornotificationbackend.h" #endif +#ifdef HAVE_NOTIFICATION_CENTER + #include "osxnotificationbackend.h" +#endif + #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 +#ifdef HAVE_NOTIFICATION_CENTER + QtUi::registerNotificationBackend(new OSXNotificationBackend(this)); +#endif + // 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 index 00000000..55a8d58e --- /dev/null +++ b/src/qtui/osxnotificationbackend.h @@ -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 index 00000000..738ee265 --- /dev/null +++ b/src/qtui/osxnotificationbackend.mm @@ -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 +#include + +#import + +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 ¬ification) +{ + 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(); +}