A few new icons...
[kdeaccessibility.git] / kmouth / wordcompletion / wordcompletion.cpp
blobd4ae6515db3feda43789918a4f1defbbc1034197
1 #include <qregexp.h>
2 #include <qfile.h>
4 #include <kapplication.h>
5 #include <kstandarddirs.h>
6 #include <kconfig.h>
8 #include "wordcompletion.h"
10 class WordCompletion::WordCompletionPrivate {
11 friend class WordCompletion;
12 private:
13 typedef QMap<QString,int> WordMap;
14 struct DictionaryDetails {
15 QString filename;
16 QString language;
19 QString lastText;
20 QMap<QString,int> map;
21 QMap<QString,int> addedWords;
22 QMap<QString,DictionaryDetails> dictDetails;
23 QStringList dictionaries;
24 QString current;
25 bool blockCurrentListSignal;
26 bool wordsToSave;
29 WordCompletion::WordCompletion() : KCompletion () {
30 d = new WordCompletionPrivate();
31 d->blockCurrentListSignal = false;
32 d->wordsToSave = false;
33 configure ();
36 WordCompletion::~WordCompletion() {
37 save ();
38 delete d;
41 typedef QPair<int,QString> Match;
42 typedef QValueList<Match> MatchList;
44 QString WordCompletion::makeCompletion(const QString &text) {
45 if (d->lastText != text) {
46 d->lastText = text;
47 KCompletion::clear();
49 int border = text.findRev(QRegExp("\\W"));
50 QString suffix = text.right (text.length() - border - 1).lower();
51 QString prefix = text.left (border + 1);
53 if (suffix.length() > 0) {
54 MatchList matches;
55 QMap<QString,int>::ConstIterator it;
56 for (it = d->map.begin(); it != d->map.end(); ++it)
57 if (it.key().startsWith(suffix))
58 matches += Match (-it.data(), it.key());
59 qHeapSort(matches);
61 MatchList::ConstIterator iter = matches.begin();
62 for (int count = 0; (iter != matches.end()) && (count < 10); ++iter, ++count) {
63 int length = (*iter).second.length() + prefix.length() - text.length();
64 KCompletion::addItem(text + (*iter).second.right(length), -(*iter).first);
69 // call the KCompletion::makeCompletion(...) method
70 return KCompletion::makeCompletion (text);
73 QStringList WordCompletion::wordLists() {
74 return d->dictionaries;
77 QStringList WordCompletion::wordLists(const QString &language) {
78 QStringList result;
79 for (QStringList::Iterator it = d->dictionaries.begin();
80 it != d->dictionaries.end(); ++it)
81 if (d->dictDetails[*it].language == language)
82 result += *it;
83 return result;
86 QString WordCompletion::languageOfWordList(const QString &wordlist) {
87 if (d->dictDetails.contains(wordlist))
88 return d->dictDetails[wordlist].language;
89 else
90 return QString::null;
93 QString WordCompletion::currentWordList() {
94 return d->current;
97 bool WordCompletion::isConfigured() {
98 KConfig *config = new KConfig("kmouthrc");
99 bool result = config->hasGroup("Dictionary 0");
100 delete config;
102 return result;
105 void WordCompletion::configure() {
106 if (d->wordsToSave)
107 save ();
108 d->wordsToSave = false;
110 d->dictionaries.clear();
111 d->dictDetails.clear();
113 KConfig *config = new KConfig("kmouthrc");
114 QStringList groups = config->groupList();
115 for (QStringList::Iterator it = groups.begin(); it != groups.end(); ++it)
116 if ((*it).startsWith ("Dictionary ")) {
117 config->setGroup(*it);
118 WordCompletionPrivate::DictionaryDetails details;
119 details.filename = config->readEntry("Filename");
120 details.language = config->readEntry("Language");
121 QString name = config->readEntry("Name");
122 d->dictDetails[name] = details;
123 d->dictionaries += name;
125 delete config;
127 d->blockCurrentListSignal = true;
128 setWordList(d->current);
129 d->blockCurrentListSignal = false;
130 emit wordListsChanged (wordLists());
131 emit currentListChanged (d->current);
134 bool WordCompletion::setWordList(const QString &wordlist) {
135 if (d->wordsToSave)
136 save ();
137 d->wordsToSave = false;
139 d->map.clear();
140 bool result = d->dictDetails.contains (wordlist);
141 if (result)
142 d->current = wordlist;
143 else
144 d->current = d->dictionaries[0];
146 QString filename = d->dictDetails[d->current].filename;
147 QString dictionaryFile = KApplication::kApplication()->dirs()->findResource("appdata", filename);
148 QFile file(dictionaryFile);
149 if (file.exists() && file.open(IO_ReadOnly)) {
150 QTextStream stream(&file);
151 stream.setEncoding (QTextStream::UnicodeUTF8);
152 if (!stream.atEnd()) {
153 if (stream.readLine() == "WPDictFile") {
154 while (!stream.atEnd()) {
155 QString s = stream.readLine();
156 if (!(s.isNull() || s.isEmpty())) {
157 QStringList list = QStringList::split("\t", s);
158 bool ok;
159 int weight = list[1].toInt(&ok);
160 if (ok && (weight > 0))
161 d->map [list[0]] = weight;
166 file.close();
168 if (!d->blockCurrentListSignal)
169 emit currentListChanged (d->current);
170 d->lastText = "";
171 d->wordsToSave = false;
172 return result;
175 void WordCompletion::addSentence (const QString &sentence) {
176 QStringList words = QStringList::split(QRegExp("\\W"), sentence);
178 QStringList::ConstIterator it;
179 for (it = words.begin(); it != words.end(); ++it) {
180 if (!(*it).contains(QRegExp("\\d|_"))) {
181 QString key = (*it).lower();
182 if (d->map.contains(key))
183 d->map[key] += 1;
184 else
185 d->map[key] = 1;
186 if (d->addedWords.contains(key))
187 d->addedWords[key] += 1;
188 else
189 d->addedWords[key] = 1;
192 d->wordsToSave = true;
195 void WordCompletion::save () {
196 if (d->wordsToSave) {
197 QString filename = d->dictDetails[d->current].filename;
198 QString dictionaryFile = KApplication::kApplication()->dirs()->findResource("appdata", filename);
199 QFile file(dictionaryFile);
200 if (!file.exists())
201 return;
202 if (!file.open(IO_WriteOnly))
203 return;
205 QTextStream stream(&file);
206 stream.setEncoding (QTextStream::UnicodeUTF8);
208 stream << "WPDictFile\n";
209 QMap<QString,int>::ConstIterator it;
210 for (it = d->map.begin(); it != d->map.end(); ++it) {
211 if (d->addedWords.contains(it.key())) {
212 stream << it.key() << "\t" << d->addedWords[it.key()] << "\t1\n";
213 stream << it.key() << "\t" << it.data() - d->addedWords[it.key()] << "\t2\n";
215 else
216 stream << it.key() << "\t" << it.data() << "\t2\n";
218 file.close();
219 d->wordsToSave = false;
223 #include "wordcompletion.moc"