• 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
  • filters
  • stringreplacer
stringreplacerproc.cpp
Go to the documentation of this file.
1 /***************************************************** vim:set ts=4 sw=4 sts=4:
2  Generic String Replacement Filter Processing class.
3  -------------------
4  Copyright:
5  (C) 2005 by Gary Cramblitt <garycramblitt@comcast.net>
6  -------------------
7  Original author: Gary Cramblitt <garycramblitt@comcast.net>
8 
9  This program is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  ******************************************************************************/
23 
24 // StringReplacer includes.
25 #include "stringreplacerproc.h"
26 #include "stringreplacerproc.moc"
27 
28 // Qt includes.
29 #include <QtXml/QDomDocument>
30 #include <QtCore/QFile>
31 
32 // KDE includes.
33 #include <kdebug.h>
34 #include <klocale.h>
35 #include <kconfig.h>
36 #include <kconfiggroup.h>
37 #include <kglobal.h>
38 #include <kstandarddirs.h>
39 
40 // KTTS includes.
41 #include "filterproc.h"
42 #include "talkercode.h"
43 #include "cdataescaper.h"
44 
48 StringReplacerProc::StringReplacerProc( QObject *parent, QVariantList list) :
49  KttsFilterProc(parent, list)
50 {
51 }
52 
56 /*virtual*/ StringReplacerProc::~StringReplacerProc()
57 {
58  m_matchList.clear();
59  m_substList.clear();
60 }
61 
62 bool StringReplacerProc::init(KConfig* c, const QString& configGroup){
63  //kDebug() << "StringReplacerProc::init: Running";
64  QString wordsFilename =
65  KGlobal::dirs()->saveLocation( "data" ,QLatin1String( "kttsd/stringreplacer/" ), false );
66  if ( wordsFilename.isEmpty() ) return false;
67  wordsFilename += configGroup;
68  KConfigGroup config( c, configGroup );
69  wordsFilename = config.readEntry( "WordListFile", wordsFilename );
70 
71  // Open existing word list.
72  QFile file( wordsFilename );
73  if ( !file.open( QIODevice::ReadOnly ) )
74  {
75  //kDebug() << "StringReplacerProc::init: couldn't open file " << wordsFilename;
76  return false;
77  }
78  QDomDocument doc( QLatin1String( "" ) );
79  if ( !doc.setContent( &file ) ) {
80  //kDebug() << "StringReplacerProc::init: couldn't get xml from file " << wordsFilename;
81  file.close();
82  return false;
83  }
84  file.close();
85 
86  // Clear list.
87  m_matchList.clear();
88  m_substList.clear();
89 
90  // Name setting.
91  // QDomNodeList nameList = doc.elementsByTagName( "name" );
92  // QDomNode nameNode = nameList.item( 0 );
93  // m_widget->nameLineEdit->setText( nameNode.toElement().text() );
94 
95  // Language Codes setting. List may be single element of comma-separated values,
96  // or multiple elements.
97  m_languageCodeList.clear();
98  QDomNodeList languageList = doc.elementsByTagName( QLatin1String( "language-code" ) );
99  for ( int ndx=0; ndx < languageList.count(); ++ndx )
100  {
101  QDomNode languageNode = languageList.item( ndx );
102  m_languageCodeList += languageNode.toElement().text().split( QLatin1Char(','), QString::SkipEmptyParts);
103  }
104 
105  // AppId. Apply this filter only if DCOP appId of application that queued
106  // the text contains this string. List may be single element of comma-separated values,
107  // or multiple elements.
108  m_appIdList.clear();
109  QDomNodeList appIdList = doc.elementsByTagName( QLatin1String( "appid" ) );
110  for ( int ndx=0; ndx < appIdList.count(); ++ndx )
111  {
112  QDomNode appIdNode = appIdList.item( ndx );
113  m_appIdList += appIdNode.toElement().text().split( QLatin1Char( ',' ), QString::SkipEmptyParts);
114  }
115 
116  // Word list.
117  QDomNodeList wordList = doc.elementsByTagName(QLatin1String( "word" ) );
118  const int wordListCount = wordList.count();
119  for (int wordIndex = 0; wordIndex < wordListCount; ++wordIndex)
120  {
121  QDomNode wordNode = wordList.item(wordIndex);
122  QDomNodeList propList = wordNode.childNodes();
123  QString wordType;
124  QString matchCase = QLatin1String( "No" ); // Default for old (v<=3.5.3) config files with no <case/>.
125  QString match;
126  QString subst;
127  const int propListCount = propList.count();
128  for (int propIndex = 0; propIndex < propListCount; ++propIndex)
129  {
130  QDomNode propNode = propList.item(propIndex);
131  QDomElement prop = propNode.toElement();
132  if (prop.tagName() == QLatin1String( "type" )) wordType = prop.text();
133  if (prop.tagName() == QLatin1String( "case" )) matchCase = prop.text();
134  if (prop.tagName() == QLatin1String( "match" ))
135  {
136  match = prop.text();
137  cdataUnescape( &match );
138  }
139  if (prop.tagName() == QLatin1String( "subst" ))
140  {
141  subst = prop.text();
142  cdataUnescape( &subst );
143  }
144  }
145  // Build Regular Expression for each word's match string.
146  QRegExp rx;
147  rx.setCaseSensitivity(matchCase == QLatin1String( "Yes" )?Qt::CaseInsensitive:Qt::CaseSensitive);
148  if ( wordType == QLatin1String( "Word" ) )
149  {
150  // TODO: Does \b honor strange non-Latin1 encodings?
151  rx.setPattern( QLatin1String( "\\b" ) + match + QLatin1String( "\\b" ) );
152  }
153  else
154  {
155  rx.setPattern( match );
156  }
157  // Add Regular Expression to list (if valid).
158  if ( rx.isValid() )
159  {
160  m_matchList.append( rx );
161  m_substList.append( subst );
162  }
163  }
164  return true;
165 }
166 
176 /*virtual*/ QString StringReplacerProc::convert(const QString& inputText, TalkerCode* talkerCode,
177  const QString& appId)
178 {
179  Q_UNUSED(talkerCode);
180  m_wasModified = false;
181  // If language doesn't match, return input unmolested.
182  //if ( !m_languageCodeList.isEmpty() )
183  //{
184  // QString languageCode = talkerCode->languageCode();
185  // //kDebug() << "StringReplacerProc::convert: converting " << inputText <<
186  // // " if language code " << languageCode << " matches " << m_languageCodeList << endl;
187  // if ( !m_languageCodeList.contains( languageCode ) )
188  // {
189  // if ( !talkerCode->countryCode().isEmpty() )
190  // {
191  // languageCode += '_' + talkerCode->countryCode();
192  // //kDebug() << "StringReplacerProc::convert: converting " << inputText <<
193  // // " if language code " << languageCode << " matches " << m_languageCodeList << endl;
194  // if ( !m_languageCodeList.contains( languageCode ) ) return inputText;
195  // } else return inputText;
196  // }
197  //}
198  // If appId doesn't match, return input unmolested.
199  if ( !m_appIdList.isEmpty() )
200  {
201  //kDebug() << "StringReplacerProc::convert: converting " << inputText << " if appId "
202  // << appId << " matches " << m_appIdList << endl;
203  bool found = false;
204  QString appIdStr = appId;
205  for ( int ndx=0; ndx < m_appIdList.count(); ++ndx )
206  {
207  if ( appIdStr.contains(m_appIdList[ndx]) )
208  {
209  found = true;
210  break;
211  }
212  }
213  if ( !found )
214  {
215  //kDebug() << "StringReplacerProc::convert: appId not found";
216  return inputText;
217  }
218  }
219  QString newText = inputText;
220  const int listCount = m_matchList.count();
221  for ( int index = 0; index < listCount; ++index )
222  {
223  //kDebug() << "newtext = " << newText << " matching " << m_matchList[index].pattern() << " replacing with " << m_substList[index];
224  newText.replace( m_matchList[index], m_substList[index] );
225  }
226  m_wasModified = true;
227  return newText;
228 }
229 
234 /*virtual*/ bool StringReplacerProc::wasModified() { return m_wasModified; }
235 
QObject
stringreplacerproc.h
filterproc.h
KttsFilterProc
Definition: filterproc.h:38
talkercode.h
StringReplacerProc::StringReplacerProc
StringReplacerProc(QObject *parent, QVariantList list=QVariantList())
Constructor.
Definition: stringreplacerproc.cpp:48
TalkerCode
Definition: talkercode.h:38
StringReplacerProc::convert
virtual QString convert(const QString &inputText, TalkerCode *talkerCode, const QString &appId)
Convert input, returning output.
Definition: stringreplacerproc.cpp:176
StringReplacerProc::~StringReplacerProc
virtual ~StringReplacerProc()
Destructor.
Definition: stringreplacerproc.cpp:56
cdataescaper.h
cdataUnescape
void cdataUnescape(QString *s)
Definition: cdataescaper.cpp:15
StringReplacerProc::init
virtual bool init(KConfig *c, const QString &configGroup)
Initialize the filter.
Definition: stringreplacerproc.cpp:62
StringReplacerProc::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: stringreplacerproc.cpp:234
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