Introducing: Audio Notifications!
authorManuel Nickschas <sputnick@quassel-irc.org>
Thu, 18 Dec 2008 23:17:38 +0000 (00:17 +0100)
committerManuel Nickschas <sputnick@quassel-irc.org>
Thu, 18 Dec 2008 23:22:16 +0000 (00:22 +0100)
Finally, you can get notified with an arbitrary sound.
Thanks to Phonon, this should work on all platforms as long as Phonon
(either Qt's or KDE's) is available.

Enable sound notifications via F7 -> Behavior -> Notifications.

src/qtui/CMakeLists.txt
src/qtui/mainwin.cpp
src/qtui/phononnotificationbackend.cpp [new file with mode: 0644]
src/qtui/phononnotificationbackend.h [new file with mode: 0644]
src/qtui/settingspages/notificationssettingspage.cpp
src/qtui/ui/phononnotificationconfigwidget.ui [new file with mode: 0644]

index 9ff3bf4..a9e98e6 100644 (file)
@@ -122,9 +122,9 @@ IF(HAVE_DBUS)
 ENDIF(HAVE_DBUS)
 
 IF(HAVE_PHONON)
 ENDIF(HAVE_DBUS)
 
 IF(HAVE_PHONON)
-  set(SOURCES ${SOURCES} )
-  set(MOC_HDRS ${MOC_HDRS} )
-  set(FORMS ${FORMS} )
+  set(SOURCES ${SOURCES} phononnotificationbackend.cpp)
+  set(MOC_HDRS ${MOC_HDRS} phononnotificationbackend.h)
+  set(FORMS ${FORMS} phononnotificationconfigwidget.ui)
   include_directories(${PHONON_INCLUDES})
 ENDIF(HAVE_PHONON)
 
   include_directories(${PHONON_INCLUDES})
 ENDIF(HAVE_PHONON)
 
index 724a3c7..32b9834 100644 (file)
@@ -56,6 +56,9 @@
 #ifdef HAVE_DBUS
 #  include "desktopnotificationbackend.h"
 #endif
 #ifdef HAVE_DBUS
 #  include "desktopnotificationbackend.h"
 #endif
+#ifdef HAVE_PHONON
+#  include "phononnotificationbackend.h"
+#endif
 #include "systraynotificationbackend.h"
 #include "taskbarnotificationbackend.h"
 
 #include "systraynotificationbackend.h"
 #include "taskbarnotificationbackend.h"
 
@@ -95,6 +98,9 @@ MainWin::MainWin(QWidget *parent)
 
   QtUi::registerNotificationBackend(new TaskbarNotificationBackend(this));
   QtUi::registerNotificationBackend(new SystrayNotificationBackend(this));
 
   QtUi::registerNotificationBackend(new TaskbarNotificationBackend(this));
   QtUi::registerNotificationBackend(new SystrayNotificationBackend(this));
+#ifdef HAVE_PHONON
+  QtUi::registerNotificationBackend(new PhononNotificationBackend(this));
+#endif
 #ifdef HAVE_DBUS
   QtUi::registerNotificationBackend(new DesktopNotificationBackend(this));
 #endif
 #ifdef HAVE_DBUS
   QtUi::registerNotificationBackend(new DesktopNotificationBackend(this));
 #endif
@@ -481,7 +487,7 @@ void MainWin::connectedToCore() {
   connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigAdded(int)), this, SLOT(addBufferView(int)));
   connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigDeleted(int)), this, SLOT(removeBufferView(int)));
   connect(Client::bufferViewManager(), SIGNAL(initDone()), this, SLOT(loadLayout()));
   connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigAdded(int)), this, SLOT(addBufferView(int)));
   connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigDeleted(int)), this, SLOT(removeBufferView(int)));
   connect(Client::bufferViewManager(), SIGNAL(initDone()), this, SLOT(loadLayout()));
-  
+
   setConnectedState();
 }
 
   setConnectedState();
 }
 
diff --git a/src/qtui/phononnotificationbackend.cpp b/src/qtui/phononnotificationbackend.cpp
new file mode 100644 (file)
index 0000000..27d7afe
--- /dev/null
@@ -0,0 +1,159 @@
+/***************************************************************************
+*   Copyright (C) 2005-08 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.,                                       *
+*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+***************************************************************************/
+
+#include <QFileDialog>
+
+#include <phonon/mediaobject.h>
+
+#include "phononnotificationbackend.h"
+
+#include "clientsettings.h"
+#include "iconloader.h"
+#include "mainwin.h"
+#include "qtui.h"
+
+PhononNotificationBackend::PhononNotificationBackend(QObject *parent)
+  : AbstractNotificationBackend(parent),
+  _media(0)
+{
+  NotificationSettings notificationSettings;
+  _enabled = notificationSettings.value("Phonon/Enabled", true).toBool();
+  createMediaObject(notificationSettings.value("Phonon/AudioFile", QString()).toString());
+
+  notificationSettings.notify("Phonon/Enabled", this, SLOT(enabledChanged(const QVariant &)));
+  notificationSettings.notify("Phonon/AudioFile", this, SLOT(audioFileChanged(const QVariant &)));
+}
+
+PhononNotificationBackend::~PhononNotificationBackend() {
+  if(_media)
+    delete _media;
+}
+
+void PhononNotificationBackend::notify(const Notification &notification) {
+  Q_UNUSED(notification)
+  if(_enabled && _media) {
+    _media->play();
+  }
+}
+
+void PhononNotificationBackend::close(uint notificationId) {
+  Q_UNUSED(notificationId);
+}
+
+void PhononNotificationBackend::enabledChanged(const QVariant &v) {
+  _enabled = v.toBool();
+}
+
+void PhononNotificationBackend::audioFileChanged(const QVariant &v) {
+  createMediaObject(v.toString());
+}
+
+SettingsPage *PhononNotificationBackend::createConfigWidget() const {
+  return new ConfigWidget();
+}
+
+void PhononNotificationBackend::createMediaObject(const QString &file) {
+  if(_media)
+    delete _media;
+
+  if(file.isEmpty()) {
+    _media = 0;
+    return;
+  }
+
+  _media = Phonon::createPlayer(Phonon::NotificationCategory,
+                                Phonon::MediaSource(file));
+}
+
+/***************************************************************************/
+
+PhononNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
+: SettingsPage("Internal", "PhononNotification", parent),
+  audioPreview(0)
+{
+  ui.setupUi(this);
+  ui.play->setIcon(SmallIcon("media-playback-start"));
+  ui.open->setIcon(SmallIcon("document-open"));
+
+  connect(ui.enabled, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
+  connect(ui.filename, SIGNAL(textChanged(const QString &)), SLOT(widgetChanged()));
+}
+
+PhononNotificationBackend::ConfigWidget::~ConfigWidget() {
+  if(audioPreview)
+    delete audioPreview;
+}
+
+void PhononNotificationBackend::ConfigWidget::widgetChanged() {
+  ui.play->setEnabled(ui.enabled->isChecked() && !ui.filename->text().isEmpty());
+
+  bool changed = (enabled != ui.enabled->isChecked()
+               || filename != ui.filename->text());
+
+  if(changed != hasChanged()) setChangedState(changed);
+}
+
+bool PhononNotificationBackend::ConfigWidget::hasDefaults() const {
+  return true;
+}
+
+void PhononNotificationBackend::ConfigWidget::defaults() {
+  ui.enabled->setChecked(false);
+  ui.filename->setText(QString());
+  widgetChanged();
+}
+
+void PhononNotificationBackend::ConfigWidget::load() {
+  NotificationSettings s;
+  enabled = s.value("Phonon/Enabled", false).toBool();
+  filename = s.value("Phonon/AudioFile", QString()).toString();
+
+  ui.enabled->setChecked(enabled);
+  ui.filename->setText(filename);
+
+  setChangedState(false);
+}
+
+void PhononNotificationBackend::ConfigWidget::save() {
+  NotificationSettings s;
+  s.setValue("Phonon/Enabled", ui.enabled->isChecked());
+  s.setValue("Phonon/AudioFile", ui.filename->text());
+  load();
+}
+
+void PhononNotificationBackend::ConfigWidget::on_open_clicked() {
+  QString file = QFileDialog::getOpenFileName(this, tr("Select Audio File"));
+  if(!file.isEmpty()) {
+    ui.filename->setText(file);
+    ui.play->setEnabled(true);
+    widgetChanged();
+  }
+}
+
+void PhononNotificationBackend::ConfigWidget::on_play_clicked() {
+  if(!ui.filename->text().isEmpty()) {
+    if(audioPreview)
+      delete audioPreview;
+
+    audioPreview = Phonon::createPlayer(Phonon::NotificationCategory,
+                                        Phonon::MediaSource(ui.filename->text()));
+    audioPreview->play();
+  }
+}
diff --git a/src/qtui/phononnotificationbackend.h b/src/qtui/phononnotificationbackend.h
new file mode 100644 (file)
index 0000000..858a7bc
--- /dev/null
@@ -0,0 +1,79 @@
+/***************************************************************************
+*   Copyright (C) 2005-08 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.,                                       *
+*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+***************************************************************************/
+
+#ifndef PHONONNOTIFICATIONBACKEND_H_
+#define PHONONNOTIFICATIONBACKEND_H_
+
+#include <phonon/mediaobject.h>
+
+#include "abstractnotificationbackend.h"
+#include "settingspage.h"
+
+#include "ui_phononnotificationconfigwidget.h"
+
+class PhononNotificationBackend : public AbstractNotificationBackend {
+  Q_OBJECT
+
+public:
+  PhononNotificationBackend(QObject *parent = 0);
+  ~PhononNotificationBackend();
+
+  void notify(const Notification &);
+  void close(uint notificationId);
+  virtual SettingsPage *createConfigWidget() const;
+
+private slots:
+  void enabledChanged(const QVariant &);
+  void audioFileChanged(const QVariant &);
+  void createMediaObject(const QString &name);
+
+private:
+  class ConfigWidget;
+
+  bool _enabled;
+  Phonon::MediaObject *_media;
+};
+
+class PhononNotificationBackend::ConfigWidget : public SettingsPage {
+  Q_OBJECT
+
+public:
+  ConfigWidget(QWidget *parent = 0);
+  ~ConfigWidget();
+
+  void save();
+  void load();
+  bool hasDefaults() const;
+  void defaults();
+
+private slots:
+  void widgetChanged();
+  void on_open_clicked();
+  void on_play_clicked();
+
+private:
+  Ui::PhononNotificationConfigWidget ui;
+
+  bool enabled;
+  QString filename;
+  Phonon::MediaObject *audioPreview;
+};
+
+#endif
index 3f4f789..3407ac5 100644 (file)
@@ -38,6 +38,7 @@ NotificationsSettingsPage::NotificationsSettingsPage(QWidget *parent)
     }
   }
   layout->addStretch(1);
     }
   }
   layout->addStretch(1);
+  load();
 }
 
 bool NotificationsSettingsPage::hasDefaults() const {
 }
 
 bool NotificationsSettingsPage::hasDefaults() const {
diff --git a/src/qtui/ui/phononnotificationconfigwidget.ui b/src/qtui/ui/phononnotificationconfigwidget.ui
new file mode 100644 (file)
index 0000000..8d3b066
--- /dev/null
@@ -0,0 +1,59 @@
+<ui version="4.0" >
+ <class>PhononNotificationConfigWidget</class>
+ <widget class="QWidget" name="PhononNotificationConfigWidget" >
+  <property name="geometry" >
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>482</width>
+    <height>78</height>
+   </rect>
+  </property>
+  <property name="windowTitle" >
+   <string>Form</string>
+  </property>
+  <layout class="QVBoxLayout" name="verticalLayout" >
+   <item>
+    <widget class="QGroupBox" name="enabled" >
+     <property name="title" >
+      <string>Audio Notification (via Phonon)</string>
+     </property>
+     <property name="checkable" >
+      <bool>true</bool>
+     </property>
+     <layout class="QHBoxLayout" name="horizontalLayout" >
+      <item>
+       <widget class="QLabel" name="label" >
+        <property name="text" >
+         <string>Play File:</string>
+        </property>
+       </widget>
+      </item>
+      <item>
+       <widget class="QLineEdit" name="filename" />
+      </item>
+      <item>
+       <widget class="QToolButton" name="play" >
+        <property name="enabled" >
+         <bool>false</bool>
+        </property>
+        <property name="text" >
+         <string/>
+        </property>
+       </widget>
+      </item>
+      <item>
+       <widget class="QToolButton" name="open" >
+        <property name="text" >
+         <string/>
+        </property>
+       </widget>
+      </item>
+     </layout>
+    </widget>
+   </item>
+  </layout>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>