1 /***************************************************************************
2 * Copyright (C) 2008 by Pino Toscano <pino@kde.org> *
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 ***************************************************************************/
16 #include <ktoolinvocation.h>
18 #include "kspeechinterface.h"
20 /* Private storage. */
21 class OkularTTS::Private
24 Private( OkularTTS
*qq
)
25 : q( qq
), kspeech( 0 )
33 org::kde::KSpeech
* kspeech
;
37 void OkularTTS::Private::setupIface()
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();
50 if ( KToolInvocation::startServiceByDesktopName( "kttsd", QStringList(), &error
) )
52 emit q
->errorMessage( i18n( "Starting KTTSD Failed: %1", error
) );
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 );
83 OkularTTS::OkularTTS( QObject
*parent
)
84 : QObject( parent
), d( new Private( this ) )
88 OkularTTS::~OkularTTS()
93 void OkularTTS::say( const QString
&text
)
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()
115 d
->kspeech
->removeAllJobs();
118 void OkularTTS::slotServiceUnregistered( const QString
&service
)
120 if ( service
== QLatin1String( "org.kde.kttsd" ) )
126 void OkularTTS::slotServiceOwnerChanged( const QString
&service
, const QString
&, const QString
&newOwner
)
128 if ( service
== QLatin1String( "org.kde.kttsd" ) && newOwner
.isEmpty() )
134 void OkularTTS::slotJobStateChanged( const QString
&appId
, int jobNum
, int state
)
136 // discard non ours job
137 if ( appId
!= QDBusConnection::sessionBus().baseService() || !d
->kspeech
)
142 case KSpeech::jsDeleted
:
143 d
->jobs
.remove( jobNum
);
144 emit
hasSpeechs( !d
->jobs
.isEmpty() );
146 case KSpeech::jsFinished
:
147 d
->kspeech
->removeJob( jobNum
);