1/***************************************************************************
2 A KTextEditor (Kate Part) plugin for speaking text.
3
4 Copyright:
5 (C) 2003-2004 by Olaf Schmidt <ojschmidt@kde.org>
6 (C) 2005 by Gary Cramblitt <garycramblitt@comcast.net>
7 (C) 2009 by Laurent Montel <montel@kde.org>
8
9 Original Author: Olaf Schmidt <ojschmidt@kde.org>
10 ***************************************************************************/
11
12/***************************************************************************
13 * *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
18 * *
19 ***************************************************************************/
20
21// KateKttsdPlugin includes.
22#include "katekttsd.h"
23#include "katekttsd.moc"
24#include <ktexteditor/document.h>
25// Qt includes.
26#include <QtCore/QTimer>
27#include <QtDBus/QtDBus>
28
29// KDE includes.
30#include <kmessagebox.h>
31#include <kaction.h>
32#include <klocale.h>
33#include <kstandarddirs.h>
34#include <ktoolinvocation.h>
35#include <KActionCollection>
36#include <KAboutData>
37#include <kate/mainwindow.h>
38
39K_PLUGIN_FACTORY(KateKttsdFactory, registerPlugin<KateKttsdPlugin>();)
40K_EXPORT_PLUGIN(KateKttsdFactory(KAboutData("kate_kttsd","kate_kttsd",ki18n("Jovie Text-to-Speech Plugin"), "0.1", ki18n("Jovie Text-to-Speech Plugin"), KAboutData::License_LGPL_V2)) )
41
42KateKttsdPlugin::KateKttsdPlugin(QObject* parent, const QList<QVariant>&)
43 : Kate::Plugin ((Kate::Application*)parent)
44{
45}
46
47Kate::PluginView *KateKttsdPlugin::createView (Kate::MainWindow *mainWindow)
48{
49 return new KateKttsdPluginView(mainWindow);
50}
51
52KateKttsdPluginView::KateKttsdPluginView( Kate::MainWindow *mw )
53 : Kate::PluginView (mw),
54 Kate::XMLGUIClient(KateKttsdFactory::componentData())
55{
56 KGlobal::locale()->insertCatalog("kttsd");
57 KAction *a = actionCollection()->addAction("tools_kttsd");
58 a->setText(i18n("Speak Text"));
59 a->setIcon(KIcon("preferences-desktop-text-to-speech"));
60 connect( a, SIGNAL(triggered(bool)), this, SLOT(slotReadOut()) );
61
62 mainWindow()->guiFactory()->addClient(this);
63}
64
65KateKttsdPluginView::~KateKttsdPluginView()
66{
67 mainWindow()->guiFactory()->removeClient( this );
68}
69
70
71void KateKttsdPluginView::slotReadOut()
72{
73 KTextEditor::View *v = mainWindow()->activeView();
74 if ( !v )
75 return;
76 KTextEditor::Document *doc = v->document();
77 QString text;
78 if ( v->selection() )
79 {
80 text = v->selectionText();
81 }
82 else
83 text = doc->text();
84 if ( text.isEmpty() )
85 return;
86
87 // If KTTSD not running, start it.
88 if (!QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.kttsd"))
89 {
90 QString error;
91 if (KToolInvocation::startServiceByDesktopName("kttsd", QStringList(), &error))
92 {
93 KMessageBox::error(0, i18n( "Starting Jovie Text-to-Speech Service Failed"), error );
94 return;
95 }
96 }
97
98 QDBusInterface kttsd( "org.kde.kttsd", "/KSpeech", "org.kde.KSpeech" );
99
100 QDBusReply<int> reply = kttsd.call("say", text,0);
101 if ( !reply.isValid())
102 KMessageBox::error( 0, i18n( "D-Bus Call Failed" ),
103 i18n( "The D-Bus call say failed." ));
104}
105
106