1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
/***************************************************************************
* Copyright (C) 2008 by Pino Toscano <pino@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 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "tts.h"
#include <qdbusservicewatcher.h>
#include <qset.h>
#include <klocale.h>
#include <kspeech.h>
#include <ktoolinvocation.h>
#include "kspeechinterface.h"
/* Private storage. */
class OkularTTS::Private
{
public:
Private( OkularTTS *qq )
: q( qq ), kspeech( 0 )
, watcher( "org.kde.kttsd", QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForUnregistration )
{
}
void setupIface();
void teardownIface();
OkularTTS *q;
org::kde::KSpeech* kspeech;
QSet< int > jobs;
QDBusServiceWatcher watcher;
};
void OkularTTS::Private::setupIface()
{
if ( kspeech )
return;
// If KTTSD not running, start it.
QDBusReply<bool> reply = QDBusConnection::sessionBus().interface()->isServiceRegistered( "org.kde.kttsd" );
bool kttsdactive = false;
if ( reply.isValid() )
kttsdactive = reply.value();
if ( !kttsdactive )
{
QString error;
if ( KToolInvocation::startServiceByDesktopName( "kttsd", QStringList(), &error ) )
{
emit q->errorMessage( i18n( "Starting Jovie Text-to-Speech service Failed: %1", error ) );
}
else
{
kttsdactive = true;
}
}
if ( kttsdactive )
{
// creating the connection to the kspeech interface
kspeech = new org::kde::KSpeech( "org.kde.kttsd", "/KSpeech", QDBusConnection::sessionBus() );
kspeech->setParent( q );
kspeech->setApplicationName( "Okular" );
connect( kspeech, SIGNAL(jobStateChanged(QString,int,int)),
q, SLOT(slotJobStateChanged(QString,int,int)) );
}
}
void OkularTTS::Private::teardownIface()
{
delete kspeech;
kspeech = 0;
}
OkularTTS::OkularTTS( QObject *parent )
: QObject( parent ), d( new Private( this ) )
{
connect( &d->watcher, SIGNAL(serviceUnregistered(QString)),
this, SLOT(slotServiceUnregistered(QString)) );
}
OkularTTS::~OkularTTS()
{
disconnect( &d->watcher, 0, this, 0 );
delete d;
}
void OkularTTS::say( const QString &text )
{
if ( text.isEmpty() )
return;
d->setupIface();
if ( d->kspeech )
{
QDBusReply< int > reply = d->kspeech->say( text, KSpeech::soPlainText );
if ( reply.isValid() )
{
d->jobs.insert( reply.value() );
emit hasSpeechs( true );
}
}
}
void OkularTTS::stopAllSpeechs()
{
if ( !d->kspeech )
return;
d->kspeech->removeAllJobs();
}
void OkularTTS::slotServiceUnregistered( const QString &service )
{
if ( service == QLatin1String( "org.kde.kttsd" ) )
{
d->teardownIface();
}
}
void OkularTTS::slotJobStateChanged( const QString &appId, int jobNum, int state )
{
// discard non ours job
if ( appId != QDBusConnection::sessionBus().baseService() || !d->kspeech )
return;
switch ( state )
{
case KSpeech::jsDeleted:
d->jobs.remove( jobNum );
emit hasSpeechs( !d->jobs.isEmpty() );
break;
case KSpeech::jsFinished:
d->kspeech->removeJob( jobNum );
break;
}
}
#include "tts.moc"
|