compile
[kdegraphics.git] / okular / ui / tts.cpp
blobd02d77e2e2cedd66d67d1a1c6048cdb7fe2f7a60
1 /***************************************************************************
2 * Copyright (C) 2008 by Pino Toscano <pino@kde.org> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 ***************************************************************************/
10 #include "tts.h"
12 #include <qset.h>
14 #include <klocale.h>
15 #include <kspeech.h>
16 #include <ktoolinvocation.h>
18 #include "kspeechinterface.h"
20 /* Private storage. */
21 class OkularTTS::Private
23 public:
24 Private( OkularTTS *qq )
25 : q( qq ), kspeech( 0 )
29 void setupIface();
30 void teardownIface();
32 OkularTTS *q;
33 org::kde::KSpeech* kspeech;
34 QSet< int > jobs;
37 void OkularTTS::Private::setupIface()
39 if ( kspeech )
40 return;
42 // If KTTSD not running, start it.
43 QDBusReply<bool> reply = QDBusConnection::sessionBus().interface()->isServiceRegistered( "org.kde.kttsd" );
44 bool kttsdactive = false;
45 if ( reply.isValid() )
46 kttsdactive = reply.value();
47 if ( !kttsdactive )
49 QString error;
50 if ( KToolInvocation::startServiceByDesktopName( "kttsd", QStringList(), &error ) )
52 emit q->errorMessage( i18n( "Starting KTTSD Failed: %1", error ) );
54 else
56 kttsdactive = true;
59 if ( kttsdactive )
61 // creating the connection to the kspeech interface
62 kspeech = new org::kde::KSpeech( "org.kde.kttsd", "/KSpeech", QDBusConnection::sessionBus() );
63 kspeech->setParent( q );
64 kspeech->setApplicationName( "Okular" );
65 connect( kspeech, SIGNAL( jobStateChanged( const QString &, int, int ) ),
66 q, SLOT( slotJobStateChanged( const QString &, int, int ) ) );
67 connect( QDBusConnection::sessionBus().interface(), SIGNAL( serviceUnregistered( const QString & ) ),
68 q, SLOT( slotServiceUnregistered( const QString & ) ) );
69 connect( QDBusConnection::sessionBus().interface(), SIGNAL( serviceOwnerChanged( const QString &, const QString &, const QString & ) ),
70 q, SLOT( slotServiceOwnerChanged( const QString &, const QString &, const QString & ) ) );
74 void OkularTTS::Private::teardownIface()
76 disconnect( QDBusConnection::sessionBus().interface(), 0, q, 0 );
78 delete kspeech;
79 kspeech = 0;
83 OkularTTS::OkularTTS( QObject *parent )
84 : QObject( parent ), d( new Private( this ) )
88 OkularTTS::~OkularTTS()
90 delete d;
93 void OkularTTS::say( const QString &text )
95 if ( text.isEmpty() )
96 return;
98 d->setupIface();
99 if ( d->kspeech )
101 QDBusReply< int > reply = d->kspeech->say( text, KSpeech::soPlainText );
102 if ( reply.isValid() )
104 d->jobs.insert( reply.value() );
105 emit hasSpeechs( true );
110 void OkularTTS::stopAllSpeechs()
112 if ( !d->kspeech )
113 return;
115 d->kspeech->removeAllJobs();
118 void OkularTTS::slotServiceUnregistered( const QString &service )
120 if ( service == QLatin1String( "org.kde.kttsd" ) )
122 d->teardownIface();
126 void OkularTTS::slotServiceOwnerChanged( const QString &service, const QString &, const QString &newOwner )
128 if ( service == QLatin1String( "org.kde.kttsd" ) && newOwner.isEmpty() )
130 d->teardownIface();
134 void OkularTTS::slotJobStateChanged( const QString &appId, int jobNum, int state )
136 // discard non ours job
137 if ( appId != QDBusConnection::sessionBus().baseService() || !d->kspeech )
138 return;
140 switch ( state )
142 case KSpeech::jsDeleted:
143 d->jobs.remove( jobNum );
144 emit hasSpeechs( !d->jobs.isEmpty() );
145 break;
146 case KSpeech::jsFinished:
147 d->kspeech->removeJob( jobNum );
148 break;
152 #include "tts.moc"