4 #include <kapplication.h>
5 #include <kstandarddirs.h>
8 #include "wordcompletion.h"
10 class WordCompletion::WordCompletionPrivate
{
11 friend class WordCompletion
;
13 typedef QMap
<QString
,int> WordMap
;
14 struct DictionaryDetails
{
20 QMap
<QString
,int> map
;
21 QMap
<QString
,int> addedWords
;
22 QMap
<QString
,DictionaryDetails
> dictDetails
;
23 QStringList dictionaries
;
25 bool blockCurrentListSignal
;
29 WordCompletion::WordCompletion() : KCompletion () {
30 d
= new WordCompletionPrivate();
31 d
->blockCurrentListSignal
= false;
32 d
->wordsToSave
= false;
36 WordCompletion::~WordCompletion() {
41 typedef QPair
<int,QString
> Match
;
42 typedef QValueList
<Match
> MatchList
;
44 QString
WordCompletion::makeCompletion(const QString
&text
) {
45 if (d
->lastText
!= text
) {
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) {
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());
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
) {
79 for (QStringList::Iterator it
= d
->dictionaries
.begin();
80 it
!= d
->dictionaries
.end(); ++it
)
81 if (d
->dictDetails
[*it
].language
== language
)
86 QString
WordCompletion::languageOfWordList(const QString
&wordlist
) {
87 if (d
->dictDetails
.contains(wordlist
))
88 return d
->dictDetails
[wordlist
].language
;
93 QString
WordCompletion::currentWordList() {
97 bool WordCompletion::isConfigured() {
98 KConfig
*config
= new KConfig("kmouthrc");
99 bool result
= config
->hasGroup("Dictionary 0");
105 void WordCompletion::configure() {
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
;
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
) {
137 d
->wordsToSave
= false;
140 bool result
= d
->dictDetails
.contains (wordlist
);
142 d
->current
= wordlist
;
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
);
159 int weight
= list
[1].toInt(&ok
);
160 if (ok
&& (weight
> 0))
161 d
->map
[list
[0]] = weight
;
168 if (!d
->blockCurrentListSignal
)
169 emit
currentListChanged (d
->current
);
171 d
->wordsToSave
= false;
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
))
186 if (d
->addedWords
.contains(key
))
187 d
->addedWords
[key
] += 1;
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
);
202 if (!file
.open(IO_WriteOnly
))
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";
216 stream
<< it
.key() << "\t" << it
.data() << "\t2\n";
219 d
->wordsToSave
= false;
223 #include "wordcompletion.moc"