[PATCH] KTTS in KNotify

Olivier Goffart ogoffart at kde.org
Mon Nov 12 16:50:09 GMT 2007


Hi,

KTTSD had a way to speech on notification.  This doesn't work anymore in KDE4 
because knotify doesn't emit anymore the same dcop signal.

I was planing to have notify plugin that could be dynamically loaded, but this 
will not go into KDE 4.0

So this patch adds support of speech directly in knotify.

It adds a runtime optional dependence against kttsd.  If the kttsd is not 
found, the corresponding ui will be hidden.

Well, this most probably break the freeze :-)
May I commit anyway ?

Attached patches are against  kdebase/runtime/knotify kdelibs/knotify/config 
kdeaccessibility/kttsd

-- 
Gof
-------------- next part --------------
Index: knotifyconfigactionswidget.cpp
===================================================================
--- knotifyconfigactionswidget.cpp	(revision 733476)
+++ knotifyconfigactionswidget.cpp	(working copy)
@@ -34,7 +34,17 @@
 	connect(m_ui.Logfile_check,SIGNAL(toggled(bool)), this, SIGNAL(changed()));
 	connect(m_ui.Execute_check,SIGNAL(toggled(bool)), this, SIGNAL(changed()));
 	connect(m_ui.Taskbar_check,SIGNAL(toggled(bool)), this, SIGNAL(changed()));
+	connect(m_ui.KTTS_check,SIGNAL(toggled(bool)), this, SLOT(slotKTTSComboChanged()));
 	connect(m_ui.Sound_play,SIGNAL(clicked()), this, SLOT(slotPlay()));
+	connect(m_ui.KTTS_combo,SIGNAL(currentIndexChanged(int)), this, SLOT(slotKTTSComboChanged()));
+	
+	if(!KNotifyConfigElement::have_kttsd())
+	{
+		m_ui.KTTS_check->setVisible(false);
+		m_ui.KTTS_select->setVisible(false);
+		m_ui.KTTS_combo->setVisible(false);
+	}
+	
 }
 
 void KNotifyConfigActionsWidget::setConfigElement( KNotifyConfigElement * config )
@@ -48,10 +58,18 @@
 	m_ui.Logfile_check->setChecked( actions.contains("Logfile") );
 	m_ui.Execute_check->setChecked( actions.contains("Execute") );
 	m_ui.Taskbar_check->setChecked( actions.contains("Taskbar") );
+	m_ui.KTTS_check->setChecked( actions.contains("KTTS") );
 
 	m_ui.Sound_select->setUrl( KUrl( config->readEntry( "Sound" , true ) ) );
 	m_ui.Logfile_select->setUrl( KUrl( config->readEntry( "Logfile" , true ) ) );
 	m_ui.Execute_select->setUrl( KUrl( config->readEntry( "Execute"  ) ) );
+	m_ui.KTTS_select->setText( config->readEntry( "KTTS"  )  );
+	if(m_ui.KTTS_select->text() == QLatin1String("%e"))
+		m_ui.KTTS_combo->setCurrentIndex(1);
+	else if(m_ui.KTTS_select->text() == QLatin1String("%m") || m_ui.KTTS_select->text() == QLatin1String("%s"))
+		m_ui.KTTS_combo->setCurrentIndex(0);
+	else
+		m_ui.KTTS_combo->setCurrentIndex(2);
 	blockSignals(blocked);
 }
 
@@ -68,12 +86,26 @@
 		actions << "Execute";
 	if(m_ui.Taskbar_check->isChecked())
 		actions << "Taskbar";
+	if(m_ui.KTTS_check->isChecked())
+		actions << "KTTS";
 
 	config->writeEntry( "Action" , actions.join("|") );
 
 	config->writeEntry( "Sound" , m_ui.Sound_select->url().url() );
 	config->writeEntry( "Logfile" , m_ui.Logfile_select->url().url() );
 	config->writeEntry( "Execute" , m_ui.Execute_select->url().url() );
+	switch(m_ui.KTTS_combo->currentIndex())
+	{
+		case 0:
+			config->writeEntry( "KTTS" , "%s" );
+			break;
+		case 1:
+			config->writeEntry( "KTTS" , "%e" );
+			break;
+		case 2:
+		default:
+			config->writeEntry( "KTTS" , m_ui.KTTS_select->text() );
+	}
 }
 
 void KNotifyConfigActionsWidget::slotPlay(  )
@@ -93,4 +125,10 @@
 	connect(media, SIGNAL(finished()), media, SLOT(deleteLater()));
 }
 
+void KNotifyConfigActionsWidget::slotKTTSComboChanged()
+{
+	m_ui.KTTS_select->setEnabled(m_ui.KTTS_check->isChecked() &&  m_ui.KTTS_combo->currentIndex() == 2);
+	emit changed();
+}
+
 #include "knotifyconfigactionswidget.moc"
Index: knotifyeventlist.cpp
===================================================================
--- knotifyeventlist.cpp	(revision 733476)
+++ knotifyeventlist.cpp	(working copy)
@@ -75,6 +75,8 @@
 			icon = KIcon("services");
 		else if(key == "Logfile" )
 			icon = KIcon("text-plain");
+		else if(key == "KTTS" && KNotifyConfigElement::have_kttsd())
+			icon = KIcon("speak");
 		else
 			continue;
 
Index: knotifyconfigactionswidget.h
===================================================================
--- knotifyconfigactionswidget.h	(revision 733476)
+++ knotifyconfigactionswidget.h	(working copy)
@@ -43,6 +43,7 @@
 		void changed();
 	private Q_SLOTS:
 		void slotPlay();
+		void slotKTTSComboChanged();
 	private:
 		Ui::KNotifyConfigActionsWidgetBase m_ui;
 };
Index: knotifyconfigelement.cpp
===================================================================
--- knotifyconfigelement.cpp	(revision 733476)
+++ knotifyconfigelement.cpp	(working copy)
@@ -23,6 +23,7 @@
 #include <kconfiggroup.h>
 #include <kconfig.h>
 #include <kdebug.h>
+#include <kstandarddirs.h>
 
 KNotifyConfigElement::KNotifyConfigElement(const QString &eventid, KConfig *config)
 	: m_config( new KConfigGroup(config , "Event/" + eventid) )
@@ -56,3 +57,10 @@
 	}
 	m_config->sync();
 }
+
+
+bool KNotifyConfigElement::have_kttsd() //[static]
+{
+	static bool val = ! KStandardDirs::findExe("kttsd").isEmpty();
+	return val;
+}
\ No newline at end of file
Index: knotifyconfigactionswidgetbase.ui
===================================================================
--- knotifyconfigactionswidgetbase.ui	(revision 733476)
+++ knotifyconfigactionswidgetbase.ui	(working copy)
@@ -5,26 +5,11 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>478</width>
-    <height>149</height>
+    <width>355</width>
+    <height>192</height>
    </rect>
   </property>
   <layout class="QVBoxLayout" >
-   <property name="spacing" >
-    <number>6</number>
-   </property>
-   <property name="leftMargin" >
-    <number>9</number>
-   </property>
-   <property name="topMargin" >
-    <number>9</number>
-   </property>
-   <property name="rightMargin" >
-    <number>9</number>
-   </property>
-   <property name="bottomMargin" >
-    <number>9</number>
-   </property>
    <item>
     <layout class="QHBoxLayout" >
      <property name="spacing" >
@@ -175,12 +160,58 @@
         </sizepolicy>
        </property>
        <property name="toolTip" >
-        <string>Select the sound to play</string>
+        <string>Select the command to run</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
+   <item>
+    <layout class="QHBoxLayout" >
+     <item>
+      <widget class="QCheckBox" name="KTTS_check" >
+       <property name="text" >
+        <string>Sp&eech</string>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QComboBox" name="KTTS_combo" >
+       <property name="sizePolicy" >
+        <sizepolicy vsizetype="Preferred" hsizetype="Preferred" >
+         <horstretch>0</horstretch>
+         <verstretch>0</verstretch>
+        </sizepolicy>
+       </property>
+       <property name="whatsThis" >
+        <string><qt>Specifies how KTTS should speak the event when received.  If you select "Speak custom text", enter the text in the box.  You may use the following substitution strings in the text:<dl><dt>%e</dt><dd>Name of the event</dd><dt>%a</dt><dd>Application that sent the event</dd><dt>%m</dt><dd>The message sent by the application</dd></dl></qt></string>
+       </property>
+       <item>
+        <property name="text" >
+         <string>Speak event message</string>
+        </property>
+       </item>
+       <item>
+        <property name="text" >
+         <string>Speak event name</string>
+        </property>
+       </item>
+       <item>
+        <property name="text" >
+         <string>Speak custom text</string>
+        </property>
+       </item>
+      </widget>
+     </item>
+     <item>
+      <widget class="QLineEdit" name="KTTS_select" >
+       <property name="whatsThis" >
+        <string><qt>Specifies how KTTS should speak the event when received.  If you select "Speak custom text", enter the text in the box.  You may use the following substitution strings in the text:<dl><dt>%e</dt><dd>Name of the event</dd><dt>%a</dt><dd>Application that sent the event</dd><dt>%m</dt><dd>The message sent by the application</dd></dl></qt></string>
+       </property>
+      </widget>
+     </item>
+    </layout>
+   </item>
   </layout>
  </widget>
  <customwidgets>
@@ -220,8 +251,8 @@
      <y>16</y>
     </hint>
     <hint type="destinationlabel" >
-     <x>279</x>
-     <y>20</y>
+     <x>254</x>
+     <y>8</y>
     </hint>
    </hints>
   </connection>
@@ -236,8 +267,8 @@
      <y>74</y>
     </hint>
     <hint type="destinationlabel" >
-     <x>229</x>
-     <y>76</y>
+     <x>238</x>
+     <y>69</y>
     </hint>
    </hints>
   </connection>
@@ -252,10 +283,26 @@
      <y>138</y>
     </hint>
     <hint type="destinationlabel" >
-     <x>235</x>
-     <y>141</y>
+     <x>244</x>
+     <y>127</y>
     </hint>
    </hints>
   </connection>
+  <connection>
+   <sender>KTTS_check</sender>
+   <signal>toggled(bool)</signal>
+   <receiver>KTTS_combo</receiver>
+   <slot>setEnabled(bool)</slot>
+   <hints>
+    <hint type="sourcelabel" >
+     <x>48</x>
+     <y>169</y>
+    </hint>
+    <hint type="destinationlabel" >
+     <x>131</x>
+     <y>169</y>
+    </hint>
+   </hints>
+  </connection>
  </connections>
 </ui>
Index: knotifyconfigelement.h
===================================================================
--- knotifyconfigelement.h	(revision 733476)
+++ knotifyconfigelement.h	(working copy)
@@ -41,6 +41,11 @@
 		
 		void save();
 		
+		/**
+		 * return wither kttsd is installed or not.
+		 */
+		static bool have_kttsd();
+		
 	private:
 		QMap<QString,QString> m_cache;
 		KConfigGroup* m_config;
-------------- next part --------------
Index: notifybyktts.cpp
===================================================================
--- notifybyktts.cpp	(revision 0)
+++ notifybyktts.cpp	(revision 0)
@@ -0,0 +1,61 @@
+/*
+   Copyright (C) 2007 by Olivier Goffart <ogoffart at kde.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, or (at your option)
+   any later version.
+
+   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 "notifybyktts.h" 
+#include <QtDBus>
+#include <QHash>
+
+#include <kmacroexpander.h>
+
+#include "knotifyconfig.h"
+
+NotifyByKTTS::NotifyByKTTS(QObject *parent) : KNotifyPlugin(parent) , kspeech("org.kde.kttsd", "/KSpeech", "org.kde.KSpeech")
+{
+	kspeech.call("setApplicationName", "KNotify");
+}
+
+
+NotifyByKTTS::~NotifyByKTTS()
+{
+}
+
+void NotifyByKTTS::notify( int id, KNotifyConfig * config )
+{
+	QString say = config->readEntry( "KTTS" );
+	
+	if (!say.isEmpty()) {
+		QHash<QChar,QString> subst;
+		subst.insert( 'e', config->eventid );
+		subst.insert( 'a', config->appname );
+		subst.insert( 's', config->text );
+		subst.insert( 'w', QString::number( (int)config->winId ));
+		subst.insert( 'i', QString::number( id ));
+		subst.insert( 'm', config->text );
+		say = KMacroExpander::expandMacrosShellQuote( say, subst );
+	}
+	
+	if ( say.isEmpty() )
+		say = config->text; // fallback
+	
+	kspeech.call(QDBus::NoBlock, "say", say, 0);
+
+	finished(id);
+}
+
+#include "notifybyktts.moc"
Index: notifybyktts.h
===================================================================
--- notifybyktts.h	(revision 0)
+++ notifybyktts.h	(revision 0)
@@ -0,0 +1,43 @@
+/*
+   Copyright (C) 2007 by Olivier Goffart <ogoffart at kde.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, or (at your option)
+   any later version.
+
+   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 NOTIFYBYKTTS_H
+#define NOTIFYBYKTTS_H
+
+#include "knotifyplugin.h"
+
+
+#include <QDBusInterface>
+
+class NotifyByKTTS : public KNotifyPlugin
+{ Q_OBJECT
+	public:
+		NotifyByKTTS(QObject *parent=0l);
+		virtual ~NotifyByKTTS();
+		
+		virtual QString optionName() { return "KTTS"; }
+		virtual void notify(int id , KNotifyConfig *config);
+	private:
+		QDBusInterface kspeech;
+};
+
+#endif
Index: knotify.cpp
===================================================================
--- knotify.cpp	(revision 735381)
+++ knotify.cpp	(working copy)
@@ -35,6 +35,7 @@
 #include "notifybyexecute.h"
 #include "notifybylogfile.h"
 #include "notifybytaskbar.h"
+#include "notifybyktts.h"
 
 
 
@@ -63,6 +64,7 @@
 	addPlugin(new NotifyByExecute(this));
 	addPlugin(new NotifyByLogfile(this));
 	addPlugin(new NotifyByTaskbar(this));
+	addPlugin(new NotifyByKTTS(this));
 }
 
 void KNotify::addPlugin( KNotifyPlugin * p )
Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt	(revision 735381)
+++ CMakeLists.txt	(working copy)
@@ -11,6 +11,7 @@
 notifybylogfile.cpp
 notifybytaskbar.cpp
 notifybyexecute.cpp
+notifybyktts.cpp
 )
 
 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: kttsnotify_kttsd.diff.zip
Type: application/x-zip
Size: 17408 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20071112/e7967884/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20071112/e7967884/attachment.sig>


More information about the kde-core-devel mailing list