1/*
2 Copyright (C) 2007 by Olivier Goffart <ogoffart at kde.org>
3 Copyright (C) 2009 by Laurent Montel <montel@kde.org>
4
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 */
21#include "notifybyktts.h"
22#include <QtDBus/QtDBus>
23#include <QHash>
24#include <ktoolinvocation.h>
25#include <kmessagebox.h>
26#include <kmacroexpander.h>
27#include <klocale.h>
28#include <knotifyconfig.h>
29
30NotifyByKTTS::NotifyByKTTS(QObject *parent) : KNotifyPlugin(parent),m_kspeech(0), tryToStartKttsd( false )
31{
32}
33
34
35NotifyByKTTS::~NotifyByKTTS()
36{
37}
38
39void NotifyByKTTS::setupKttsd()
40{
41 m_kspeech = new org::kde::KSpeech("org.kde.kttsd", "/KSpeech", QDBusConnection::sessionBus());
42 m_kspeech->setParent(this);
43 m_kspeech->setApplicationName("KNotify");
44
45 QDBusServiceWatcher *watcher = new QDBusServiceWatcher(this);
46 watcher->setConnection(QDBusConnection::sessionBus());
47 watcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration);
48 watcher->addWatchedService("org.kde.kttsd");
49 connect(watcher, SIGNAL(serviceUnregistered( const QString & ) ), this, SLOT( removeSpeech() ));
50}
51
52void NotifyByKTTS::notify( int id, KNotifyConfig * config )
53{
54 if( !m_kspeech)
55 {
56 if ( tryToStartKttsd ) //don't try to restart it all the time.
57 return;
58 // If KTTSD not running, start it.
59 if (!QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.kttsd"))
60 {
61 QString error;
62 if (KToolInvocation::startServiceByDesktopName("kttsd", QStringList(), &error))
63 {
64 KMessageBox::error(0, i18n( "Starting Jovie Text-to-Speech Service Failed"), error );
65 tryToStartKttsd = true;
66 return;
67 }
68 }
69 setupKttsd();
70 }
71
72 QString say = config->readEntry( "KTTS" );
73
74 if (!say.isEmpty()) {
75 QHash<QChar,QString> subst;
76 subst.insert( 'e', config->eventid );
77 subst.insert( 'a', config->appname );
78 subst.insert( 's', config->text );
79 subst.insert( 'w', QString::number( (quintptr)config->winId ));
80 subst.insert( 'i', QString::number( id ));
81 subst.insert( 'm', config->text );
82 say = KMacroExpander::expandMacrosShellQuote( say, subst );
83 }
84
85 if ( say.isEmpty() )
86 say = config->text; // fallback
87
88 m_kspeech->setApplicationName(config->appname);
89 m_kspeech->call(QDBus::NoBlock, "say", say, 0);
90
91 finished(id);
92}
93
94void NotifyByKTTS::removeSpeech()
95{
96 tryToStartKttsd = false;
97
98 delete m_kspeech;
99 m_kspeech = 0;
100}
101
102#include "notifybyktts.moc"
103