2008-03-05 02:13:09 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* 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"
|
|
|
|
|
2010-08-14 13:18:55 +00:00
|
|
|
#include <qdbusservicewatcher.h>
|
2008-03-05 12:02:14 +00:00
|
|
|
#include <qset.h>
|
|
|
|
|
2014-10-12 15:20:07 +00:00
|
|
|
#include <KLocalisedString>
|
2008-03-05 12:02:14 +00:00
|
|
|
#include <kspeech.h>
|
2008-03-05 02:13:09 +00:00
|
|
|
#include <ktoolinvocation.h>
|
|
|
|
|
2008-03-05 10:18:55 +00:00
|
|
|
#include "kspeechinterface.h"
|
2008-03-05 02:13:09 +00:00
|
|
|
|
|
|
|
/* Private storage. */
|
|
|
|
class OkularTTS::Private
|
|
|
|
{
|
|
|
|
public:
|
2008-03-05 10:18:55 +00:00
|
|
|
Private( OkularTTS *qq )
|
|
|
|
: q( qq ), kspeech( 0 )
|
2010-08-14 13:18:55 +00:00
|
|
|
, watcher( "org.kde.kttsd", QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForUnregistration )
|
2008-03-05 02:13:09 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-03-05 10:18:55 +00:00
|
|
|
void setupIface();
|
|
|
|
void teardownIface();
|
|
|
|
|
|
|
|
OkularTTS *q;
|
|
|
|
org::kde::KSpeech* kspeech;
|
2008-03-05 12:02:14 +00:00
|
|
|
QSet< int > jobs;
|
2010-08-14 13:18:55 +00:00
|
|
|
QDBusServiceWatcher watcher;
|
2008-03-05 02:13:09 +00:00
|
|
|
};
|
|
|
|
|
2008-03-05 10:18:55 +00:00
|
|
|
void OkularTTS::Private::setupIface()
|
2008-03-05 02:13:09 +00:00
|
|
|
{
|
2008-03-05 10:18:55 +00:00
|
|
|
if ( kspeech )
|
2008-03-05 02:13:09 +00:00
|
|
|
return;
|
|
|
|
|
2010-06-15 20:22:28 +00:00
|
|
|
// If KTTSD not running, start it.
|
|
|
|
QDBusReply<bool> reply = QDBusConnection::sessionBus().interface()->isServiceRegistered( "org.kde.kttsd" );
|
|
|
|
bool kttsdactive = false;
|
2008-03-05 02:13:09 +00:00
|
|
|
if ( reply.isValid() )
|
2010-06-15 20:22:28 +00:00
|
|
|
kttsdactive = reply.value();
|
|
|
|
if ( !kttsdactive )
|
2008-03-05 02:13:09 +00:00
|
|
|
{
|
|
|
|
QString error;
|
2010-06-15 20:22:28 +00:00
|
|
|
if ( KToolInvocation::startServiceByDesktopName( "kttsd", QStringList(), &error ) )
|
2008-03-05 02:13:09 +00:00
|
|
|
{
|
2010-09-16 05:24:03 +00:00
|
|
|
emit q->errorMessage( i18n( "Starting Jovie Text-to-Speech service Failed: %1", error ) );
|
2008-03-05 02:13:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-06-15 20:22:28 +00:00
|
|
|
kttsdactive = true;
|
2008-03-05 02:13:09 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-15 20:22:28 +00:00
|
|
|
if ( kttsdactive )
|
2008-03-05 02:13:09 +00:00
|
|
|
{
|
|
|
|
// creating the connection to the kspeech interface
|
2010-06-15 20:22:28 +00:00
|
|
|
kspeech = new org::kde::KSpeech( "org.kde.kttsd", "/KSpeech", QDBusConnection::sessionBus() );
|
2008-03-05 15:39:26 +00:00
|
|
|
kspeech->setParent( q );
|
2008-03-05 10:18:55 +00:00
|
|
|
kspeech->setApplicationName( "Okular" );
|
2014-10-01 17:40:48 +00:00
|
|
|
connect(kspeech, &org::kde::KSpeech::jobStateChanged, q, &OkularTTS::slotJobStateChanged);
|
2008-03-05 10:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OkularTTS::Private::teardownIface()
|
|
|
|
{
|
|
|
|
delete kspeech;
|
|
|
|
kspeech = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-23 11:09:04 +00:00
|
|
|
OkularTTS::OkularTTS( QObject *parent )
|
2008-03-05 10:18:55 +00:00
|
|
|
: QObject( parent ), d( new Private( this ) )
|
|
|
|
{
|
2014-10-01 17:40:48 +00:00
|
|
|
connect(&d->watcher, &QDBusServiceWatcher::serviceUnregistered, this, &OkularTTS::slotServiceUnregistered);
|
2008-03-05 10:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OkularTTS::~OkularTTS()
|
|
|
|
{
|
2010-08-14 13:18:55 +00:00
|
|
|
disconnect( &d->watcher, 0, this, 0 );
|
|
|
|
|
2008-03-05 10:18:55 +00:00
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OkularTTS::say( const QString &text )
|
|
|
|
{
|
|
|
|
if ( text.isEmpty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
d->setupIface();
|
|
|
|
if ( d->kspeech )
|
|
|
|
{
|
2008-03-05 12:02:14 +00:00
|
|
|
QDBusReply< int > reply = d->kspeech->say( text, KSpeech::soPlainText );
|
|
|
|
if ( reply.isValid() )
|
|
|
|
{
|
|
|
|
d->jobs.insert( reply.value() );
|
|
|
|
emit hasSpeechs( true );
|
|
|
|
}
|
2008-03-05 10:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-05 12:02:14 +00:00
|
|
|
void OkularTTS::stopAllSpeechs()
|
|
|
|
{
|
|
|
|
if ( !d->kspeech )
|
|
|
|
return;
|
|
|
|
|
|
|
|
d->kspeech->removeAllJobs();
|
|
|
|
}
|
|
|
|
|
2008-03-05 10:18:55 +00:00
|
|
|
void OkularTTS::slotServiceUnregistered( const QString &service )
|
|
|
|
{
|
2010-06-15 20:22:28 +00:00
|
|
|
if ( service == QLatin1String( "org.kde.kttsd" ) )
|
2008-03-05 10:18:55 +00:00
|
|
|
{
|
|
|
|
d->teardownIface();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-05 12:02:14 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 22:00:07 +00:00
|
|
|
#include "moc_tts.cpp"
|