• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeaccessibility API Reference
  • KDE Home
  • Contact Us
 

jovie

  • sources
  • kde-4.12
  • kdeaccessibility
  • jovie
  • jovie
filtermgr.cpp
Go to the documentation of this file.
1 /***************************************************** vim:set ts=4 sw=4 sts=4:
2  Description:t, applying each configured Filter in turn.
3  Runs synchronously via call to convert()
4 
5  Copyright:
6  (C) 2005 by Gary Cramblitt <garycramblitt@comcast.net>
7  (C) 2009 by Jeremy Whiting <jpwhiting@kde.org>
8  -------------------
9  Original author: Gary Cramblitt <garycramblitt@comcast.net>
10 
11  This program is free software; you can redistribute it and/or modify
12  it under the terms of the GNU General Public License as published by
13  the Free Software Foundation; either version 2 of the License, or
14  (at your option) any later version.
15 
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  ******************************************************************************/
25 
26 // FilterMgr includes.
27 #include "filtermgr.h"
28 #include "filtermgr.moc"
29 
30 // Qt includes
31 
32 // KDE includes.
33 #include <kdebug.h>
34 #include <kconfig.h>
35 #include <kconfiggroup.h>
36 #include <kpluginloader.h>
37 #include <ksharedconfig.h>
38 #include <kservicetypetrader.h>
39 
43 FilterMgr::FilterMgr( QObject *parent) :
44  KttsFilterProc(parent, QVariantList())
45 {
46  // kDebug() << "FilterMgr::FilterMgr: Running";
47  m_state = fsIdle;
48  m_talkerCode = 0;
49 }
50 
54 FilterMgr::~FilterMgr()
55 {
56  // kDebug() << "FilterMgr::~FilterMgr: Running";
57  qDeleteAll(m_filterList);
58  m_filterList.clear();
59 }
60 
65 bool FilterMgr::init()
66 {
67  // Load each of the filters and initialize.
68  KSharedConfig::Ptr pConfig = KSharedConfig::openConfig( QLatin1String( "kttsdrc" ) );
69  KConfigGroup config( pConfig, "General");
70  KConfig* rawconfig = new KConfig(QLatin1String( "kttsdrc" ));
71  QStringList filterIDsList = config.readEntry("FilterIDs", QStringList());
72  kDebug() << "FilterMgr::init: FilterIDs = " << filterIDsList;
73 
74  if ( !filterIDsList.isEmpty() )
75  {
76  QStringList::ConstIterator itEnd = filterIDsList.constEnd();
77  for (QStringList::ConstIterator it = filterIDsList.constBegin(); it != itEnd; ++it)
78  {
79  QString filterID = *it;
80  QString groupName = QLatin1String( "Filter_" ) + filterID;
81  KConfigGroup thisgroup = pConfig->group(groupName);
82  QString desktopEntryName = thisgroup.readEntry( "DesktopEntryName" );
83  // If a DesktopEntryName is not in the config file, it was configured before
84  // we started using them, when we stored translated plugin names instead.
85  // Try to convert the translated plugin name to a DesktopEntryName.
86  // DesktopEntryNames are better because user can change their desktop language
87  // and DesktopEntryName won't change.
88  if (desktopEntryName.isEmpty())
89  {
90  QString filterPlugInName = thisgroup.readEntry("PlugInName", QString());
91  // See if the translated name will untranslate. If not, well, sorry.
92  desktopEntryName = FilterNameToDesktopEntryName(filterPlugInName);
93  // Record the DesktopEntryName from now on.
94  if (!desktopEntryName.isEmpty())
95  thisgroup.writeEntry("DesktopEntryName", desktopEntryName);
96  }
97  if (thisgroup.readEntry("Enabled",false) || thisgroup.readEntry("IsSBD",false))
98  {
99  kDebug() << "FilterMgr::init: filterID = " << filterID;
100  KttsFilterProc* filterProc = loadFilterPlugin( desktopEntryName );
101  if ( filterProc )
102  {
103  filterProc->init( rawconfig, groupName );
104  m_filterList.append( filterProc );
105  }
106  //if (thisgroup.readEntry("DocType").contains("html") ||
107  // thisgroup.readEntry("RootElement").contains("html"))
108  //m_supportsHTML = true;
109  }
110  }
111  }
112  delete rawconfig;
113  return true;
114 }
115 
126 QString FilterMgr::convert(const QString& inputText, TalkerCode* talkerCode, const QString& appId)
127 {
128  m_text = inputText;
129  m_talkerCode = talkerCode;
130  m_appId = appId;
131  m_filterIndex = -1;
132  m_filterProc = 0;
133  m_state = fsFiltering;
134  //m_async = false;
135  while ( m_state == fsFiltering )
136  nextFilter();
137  return m_text;
138 }
139 
140 // Finishes up with current filter (if any) and goes on to the next filter.
141 void FilterMgr::nextFilter()
142 {
143  ++m_filterIndex;
144  if (m_filterIndex == m_filterList.count())
145  {
146  m_state = fsFinished;
147  return;
148  }
149  m_filterProc = m_filterList.at(m_filterIndex);
150  m_text = m_filterProc->convert( m_text, m_talkerCode, m_appId );
151  if (m_filterProc->wasModified())
152  kDebug() << "FilterMgr::nextFilter: Filter# " << m_filterIndex << " modified the text.";
153 }
154 
155 // Loads the processing plug in for a filter plug in given its DesktopEntryName.
156 KttsFilterProc* FilterMgr::loadFilterPlugin(const QString& desktopEntryName)
157 {
158  // kDebug() << "FilterMgr::loadFilterPlugin: Running";
159 
160  // Find the plugin.
161  KService::List offers = KServiceTypeTrader::self()->query(QLatin1String( "Jovie/FilterPlugin" ),
162  QString(QLatin1String( "DesktopEntryName == '%1'" )).arg(desktopEntryName));
163 
164  if (offers.count() == 1)
165  {
166  // When the entry is found, load the plug in
167  // First create a factory for the library
168  KPluginLoader loader(offers[0]->library());
169  KPluginFactory* factory = loader.factory();
170  if (!factory) {
171  // Something went wrong, returning null.
172  kDebug() << "FilterMgr::loadFilterPlugin: Unable to create Factory object for plugin "
173  << desktopEntryName << endl;
174  return NULL;
175  } else {
176  KttsFilterProc *plugIn = factory->create<KttsFilterProc>();
177  if (plugIn) {
178  return plugIn;
179  } else {
180  // Something went wrong, returning null.
181  kDebug() << "FilterMgr::loadFilterPlugin: Unable to instantiate "
182  << "KttsFilterProc class for plugin " << desktopEntryName;
183  return NULL;
184  }
185  }
186  //KLibFactory *factory = KLibLoader::self()->factory(offers[0]->library().toLatin1());
187  //if(factory){
188  // // If the factory is created successfully, instantiate the KttsFilterConf class for the
189  // // specific plug in to get the plug in configuration object.
190  // KttsFilterProc *plugIn =
191  // KLibLoader::createInstance<KttsFilterProc>(
192  // offers[0]->library().toLatin1(), NULL, QStringList(offers[0]->library().toLatin1()),
193  // &errorNo);
194  // if(plugIn){
195  // // If everything went ok, return the plug in pointer.
196  // // kDebug() << "FilterMgr::loadFilterPlugin: plugin " << offers[0]->library().toLatin1() << " loaded successfully.";
197  // return plugIn;
198  // } else {
199  // // Something went wrong, returning null.
200  // kDebug() << "FilterMgr::loadFilterPlugin: Unable to instantiate KttsFilterProc class for plugin " << desktopEntryName << " error: " << errorNo;
201  // return NULL;
202  // }
203  //} else {
204  // // Something went wrong, returning null.
205  // kDebug() << "FilterMgr::loadFilterPlugin: Unable to create Factory object for plugin "
206  // << desktopEntryName << endl;
207  // return NULL;
208  //}
209  }
210  // The plug in was not found (unexpected behaviour, returns null).
211  kDebug() << "FilterMgr::loadFilterPlugin: KTrader did not return an offer for plugin "
212  << desktopEntryName << endl;
213  return NULL;
214 }
215 
222 QString FilterMgr::FilterNameToDesktopEntryName(const QString& name)
223 {
224  if (name.isEmpty()) return QString();
225  KService::List offers = KServiceTypeTrader::self()->query(QLatin1String( "Jovie/FilterPlugin" ),
226  QString(QLatin1String( "Name == '%1'" )).arg(name));
227 
228  if (offers.count() == 1)
229  return offers[0]->desktopEntryName();
230  else
231  return QString();
232 }
233 
KttsFilterProc::init
virtual bool init(KConfig *config, const QString &configGroup)
Initialize the filter.
Definition: filterproc.cpp:57
KttsFilterProc::fsFinished
Definition: filterproc.h:47
KttsFilterProc::convert
virtual QString convert(const QString &inputText, TalkerCode *talkerCode, const QString &appId)
Convert input, returning output.
Definition: filterproc.cpp:90
QObject
filtermgr.h
FilterMgr::convert
virtual QString convert(const QString &inputText, TalkerCode *talkerCode, const QString &appId)
Synchronously convert text.
Definition: filtermgr.cpp:126
KttsFilterProc
Definition: filterproc.h:38
FilterMgr::~FilterMgr
~FilterMgr()
Destructor.
Definition: filtermgr.cpp:54
FilterMgr::init
virtual bool init()
Initialize the filters.
Definition: filtermgr.cpp:65
KttsFilterProc::wasModified
virtual bool wasModified()
Did this filter do anything? If the filter returns the input as output unmolested, it should return False when this method is called.
Definition: filterproc.cpp:143
FilterMgr::FilterMgr
FilterMgr(QObject *parent=0)
Constructor.
Definition: filtermgr.cpp:43
TalkerCode
Definition: talkercode.h:38
KttsFilterProc::fsIdle
Definition: filterproc.h:44
KttsFilterProc::fsFiltering
Definition: filterproc.h:45
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:32:25 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

jovie

Skip menu "jovie"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeaccessibility API Reference

Skip menu "kdeaccessibility API Reference"
  • jovie

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal