ksconfig.cpp
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1997 David Sweet <dsweet@kde.org> 00003 Copyright (C) 2000-2001 Wolfram Diestel <wolfram@steloj.de> 00004 Copyright (C) 2007 Kevin Kofler <Kevin@tigcc.ticalc.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License version 2 as published by the Free Software Foundation. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include <config.h> 00022 00023 #include <qcheckbox.h> 00024 #include <qcombobox.h> 00025 #include <qlabel.h> 00026 #include <qlayout.h> 00027 #include <qtimer.h> 00028 00029 #include <kapplication.h> 00030 #include <kconfig.h> 00031 #include <kdebug.h> 00032 #include <kdialog.h> 00033 #include <kfiledialog.h> 00034 #include <kglobal.h> 00035 #include <klineedit.h> 00036 #include <klocale.h> 00037 #include <kpushbutton.h> 00038 #include <kstdguiitem.h> 00039 00040 #include "ksconfig.h" 00041 00042 class KSpellConfigPrivate 00043 { 00044 public: 00045 QStringList replacelist; 00046 }; 00047 00048 00049 KSpellConfig::KSpellConfig (const KSpellConfig &_ksc) 00050 : QWidget(0, 0), nodialog(true) 00051 , kc(0) 00052 , cb1(0) 00053 , cb2(0) 00054 , dictlist(0) 00055 , dictcombo(0) 00056 , encodingcombo(0) 00057 , clientcombo(0) 00058 { 00059 d = new KSpellConfigPrivate; 00060 setReplaceAllList( _ksc.replaceAllList() ); 00061 setNoRootAffix( _ksc.noRootAffix() ); 00062 setRunTogether( _ksc.runTogether() ); 00063 setDictionary( _ksc.dictionary() ); 00064 setDictFromList( _ksc.dictFromList() ); 00065 // setPersonalDict (_ksc.personalDict()); 00066 setIgnoreList( _ksc.ignoreList() ); 00067 setEncoding( _ksc.encoding() ); 00068 setClient( _ksc.client() ); 00069 } 00070 00071 00072 KSpellConfig::KSpellConfig( QWidget *parent, const char *name, 00073 KSpellConfig *_ksc, bool addHelpButton ) 00074 : QWidget (parent, name), nodialog(false) 00075 , kc(0) 00076 , cb1(0) 00077 , cb2(0) 00078 , dictlist(0) 00079 , dictcombo(0) 00080 , encodingcombo(0) 00081 , clientcombo(0) 00082 { 00083 d = new KSpellConfigPrivate; 00084 kc = KGlobal::config(); 00085 00086 if( !_ksc ) 00087 { 00088 readGlobalSettings(); 00089 } 00090 else 00091 { 00092 setNoRootAffix( _ksc->noRootAffix() ); 00093 setRunTogether( _ksc->runTogether() ); 00094 setDictionary( _ksc->dictionary() ); 00095 setDictFromList( _ksc->dictFromList() ); 00096 //setPersonalDict (_ksc->personalDict()); 00097 setIgnoreList( _ksc->ignoreList() ); 00098 setEncoding( _ksc->encoding() ); 00099 setClient( _ksc->client() ); 00100 } 00101 00102 QGridLayout *glay = new QGridLayout( this, 6, 3, 0, KDialog::spacingHint() ); 00103 cb1 = new QCheckBox( i18n("Create &root/affix combinations" 00104 " not in dictionary"), this, "NoRootAffix" ); 00105 connect( cb1, SIGNAL(toggled(bool)), SLOT(sNoAff(bool)) ); 00106 glay->addMultiCellWidget( cb1, 0, 0, 0, 2 ); 00107 00108 cb2 = new QCheckBox( i18n("Consider run-together &words" 00109 " as spelling errors"), this, "RunTogether" ); 00110 connect( cb2, SIGNAL(toggled(bool)), SLOT(sRunTogether(bool)) ); 00111 glay->addMultiCellWidget( cb2, 1, 1, 0, 2 ); 00112 00113 dictcombo = new QComboBox( this, "DictFromList" ); 00114 dictcombo->setInsertionPolicy( QComboBox::NoInsertion ); 00115 connect( dictcombo, SIGNAL (activated(int)), 00116 this, SLOT (sSetDictionary(int)) ); 00117 glay->addMultiCellWidget( dictcombo, 2, 2, 1, 2 ); 00118 00119 dictlist = new QLabel( dictcombo, i18n("&Dictionary:"), this ); 00120 glay->addWidget( dictlist, 2 ,0 ); 00121 00122 encodingcombo = new QComboBox( this, "Encoding" ); 00123 encodingcombo->insertItem( "US-ASCII" ); 00124 encodingcombo->insertItem( "ISO 8859-1" ); 00125 encodingcombo->insertItem( "ISO 8859-2" ); 00126 encodingcombo->insertItem( "ISO 8859-3" ); 00127 encodingcombo->insertItem( "ISO 8859-4" ); 00128 encodingcombo->insertItem( "ISO 8859-5" ); 00129 encodingcombo->insertItem( "ISO 8859-7" ); 00130 encodingcombo->insertItem( "ISO 8859-8" ); 00131 encodingcombo->insertItem( "ISO 8859-9" ); 00132 encodingcombo->insertItem( "ISO 8859-13" ); 00133 encodingcombo->insertItem( "ISO 8859-15" ); 00134 encodingcombo->insertItem( "UTF-8" ); 00135 encodingcombo->insertItem( "KOI8-R" ); 00136 encodingcombo->insertItem( "KOI8-U" ); 00137 encodingcombo->insertItem( "CP1251" ); 00138 encodingcombo->insertItem( "CP1255" ); 00139 00140 connect( encodingcombo, SIGNAL(activated(int)), this, 00141 SLOT(sChangeEncoding(int)) ); 00142 glay->addMultiCellWidget( encodingcombo, 3, 3, 1, 2 ); 00143 00144 QLabel *tmpQLabel = new QLabel( encodingcombo, i18n("&Encoding:"), this); 00145 glay->addWidget( tmpQLabel, 3, 0 ); 00146 00147 00148 clientcombo = new QComboBox( this, "Client" ); 00149 clientcombo->insertItem( i18n("International Ispell") ); 00150 clientcombo->insertItem( i18n("Aspell") ); 00151 clientcombo->insertItem( i18n("Hspell") ); 00152 clientcombo->insertItem( i18n("Zemberek") ); 00153 clientcombo->insertItem( i18n("Hunspell") ); 00154 connect( clientcombo, SIGNAL (activated(int)), this, 00155 SLOT (sChangeClient(int)) ); 00156 glay->addMultiCellWidget( clientcombo, 4, 4, 1, 2 ); 00157 00158 tmpQLabel = new QLabel( clientcombo, i18n("&Client:"), this ); 00159 glay->addWidget( tmpQLabel, 4, 0 ); 00160 00161 if( addHelpButton ) 00162 { 00163 QPushButton *pushButton = new KPushButton( KStdGuiItem::help(), this ); 00164 connect( pushButton, SIGNAL(clicked()), this, SLOT(sHelp()) ); 00165 glay->addWidget(pushButton, 5, 2); 00166 } 00167 00168 fillInDialog(); 00169 } 00170 00171 KSpellConfig::~KSpellConfig() 00172 { 00173 delete d; 00174 } 00175 00176 00177 bool 00178 KSpellConfig::dictFromList() const 00179 { 00180 return dictfromlist; 00181 } 00182 00183 bool 00184 KSpellConfig::readGlobalSettings() 00185 { 00186 KConfigGroupSaver cs( kc,"KSpell" ); 00187 00188 setNoRootAffix ( kc->readNumEntry("KSpell_NoRootAffix", 0) ); 00189 setRunTogether ( kc->readNumEntry("KSpell_RunTogether", 0) ); 00190 setDictionary ( kc->readEntry("KSpell_Dictionary") ); 00191 setDictFromList ( kc->readNumEntry("KSpell_DictFromList", false) ); 00192 setEncoding ( kc->readNumEntry ("KSpell_Encoding", KS_E_ASCII) ); 00193 setClient ( kc->readNumEntry ("KSpell_Client", KS_CLIENT_ISPELL) ); 00194 00195 return true; 00196 } 00197 00198 bool 00199 KSpellConfig::writeGlobalSettings () 00200 { 00201 KConfigGroupSaver cs( kc,"KSpell" ); 00202 00203 kc->writeEntry ("KSpell_NoRootAffix",(int) noRootAffix(), true, true); 00204 kc->writeEntry ("KSpell_RunTogether", (int) runTogether(), true, true); 00205 kc->writeEntry ("KSpell_Dictionary", dictionary(), true, true); 00206 kc->writeEntry ("KSpell_DictFromList",(int) dictFromList(), true, true); 00207 kc->writeEntry ("KSpell_Encoding", (int) encoding(), 00208 true, true); 00209 kc->writeEntry ("KSpell_Client", client(), 00210 true, true); 00211 kc->sync(); 00212 00213 return true; 00214 } 00215 00216 void 00217 KSpellConfig::sChangeEncoding( int i ) 00218 { 00219 kdDebug(750) << "KSpellConfig::sChangeEncoding(" << i << ")" << endl; 00220 setEncoding( i ); 00221 emit configChanged(); 00222 } 00223 00224 void 00225 KSpellConfig::sChangeClient( int i ) 00226 { 00227 setClient( i ); 00228 00229 // read in new dict list 00230 if ( dictcombo ) { 00231 if ( iclient == KS_CLIENT_ISPELL ) 00232 getAvailDictsIspell(); 00233 else if ( iclient == KS_CLIENT_HSPELL ) 00234 { 00235 langfnames.clear(); 00236 dictcombo->clear(); 00237 dictcombo->insertItem( i18n("Hebrew") ); 00238 sChangeEncoding( KS_E_CP1255 ); 00239 } else if ( iclient == KS_CLIENT_ZEMBEREK ) { 00240 langfnames.clear(); 00241 dictcombo->clear(); 00242 dictcombo->insertItem( i18n("Turkish") ); 00243 sChangeEncoding( KS_E_UTF8 ); 00244 } 00245 else if ( iclient == KS_CLIENT_ASPELL ) 00246 getAvailDictsAspell(); 00247 else 00248 getAvailDictsHunspell(); 00249 } 00250 emit configChanged(); 00251 } 00252 00253 // KDE 4: Make it const QString & fname (only fname) 00254 bool 00255 KSpellConfig::interpret( QString &fname, QString &lname, 00256 QString &hname ) 00257 00258 { 00259 00260 kdDebug(750) << "KSpellConfig::interpret [" << fname << "]" << endl; 00261 00262 QString dname( fname ); 00263 00264 if( dname.endsWith( "+" ) ) 00265 dname.remove( dname.length()-1, 1 ); 00266 00267 if( dname.endsWith("sml") || dname.endsWith("med") || dname.endsWith("lrg") || 00268 dname.endsWith("xlg")) 00269 dname.remove(dname.length()-3,3); 00270 00271 QString extension; 00272 00273 int i = dname.find('-'); 00274 if ( i != -1 ) 00275 { 00276 extension = dname.mid(i+1); 00277 dname.truncate(i); 00278 } 00279 00280 // Aspell uses 2 alpha language codes or 2 alpha language + 2 alpha country, 00281 // but since aspell 0.6 also 3-character ISO-codes can be used 00282 if ( (dname.length() == 2) || (dname.length() == 3) ) { 00283 lname = dname; 00284 hname = KGlobal::locale()->twoAlphaToLanguageName( lname ); 00285 } 00286 else if ( (dname.length() == 5) && (dname[2] == '_') ) { 00287 lname = dname.left(2); 00288 hname = KGlobal::locale()->twoAlphaToLanguageName(lname); 00289 QString country = KGlobal::locale()->twoAlphaToCountryName( dname.right(2) ); 00290 if ( extension.isEmpty() ) 00291 extension = country; 00292 else 00293 extension = country + " - " + extension; 00294 } 00295 //These are mostly the ispell-langpack defaults 00296 else if ( dname=="english" || dname=="american" || 00297 dname=="british" || dname=="canadian" ) { 00298 lname="en"; hname=i18n("English"); 00299 } 00300 else if ( dname == "espa~nol" || dname == "espanol" ) { 00301 lname="es"; hname=i18n("Spanish"); 00302 } 00303 else if (dname=="dansk") { 00304 lname="da"; hname=i18n("Danish"); 00305 } 00306 else if (dname=="deutsch") { 00307 lname="de"; hname=i18n("German"); 00308 } 00309 else if (dname=="german") { 00310 lname="de"; hname=i18n("German (new spelling)"); 00311 } 00312 else if (dname=="portuguesb" || dname=="br") { 00313 lname="br"; hname=i18n("Brazilian Portuguese"); 00314 } 00315 else if (dname=="portugues") { 00316 lname="pt"; hname=i18n("Portuguese"); 00317 } 00318 else if (dname=="esperanto") { 00319 lname="eo"; hname=i18n("Esperanto"); 00320 } 00321 else if (dname=="norsk") { 00322 lname="no"; hname=i18n("Norwegian"); 00323 } 00324 else if (dname=="polish") { 00325 lname="pl"; hname=i18n("Polish"); sChangeEncoding(KS_E_LATIN2); 00326 } 00327 else if (dname=="russian") { 00328 lname="ru"; hname=i18n("Russian"); 00329 } 00330 else if (dname=="slovensko") { 00331 lname="si"; hname=i18n("Slovenian"); sChangeEncoding(KS_E_LATIN2); 00332 } 00333 else if (dname=="slovak"){ 00334 lname="sk"; hname=i18n("Slovak"); sChangeEncoding(KS_E_LATIN2); 00335 } 00336 else if (dname=="czech") { 00337 lname="cs"; hname=i18n("Czech"); sChangeEncoding(KS_E_LATIN2); 00338 } 00339 else if (dname=="svenska") { 00340 lname="sv"; hname=i18n("Swedish"); 00341 } 00342 else if (dname=="swiss") { 00343 lname="de"; hname=i18n("Swiss German"); 00344 } 00345 else if (dname=="ukrainian") { 00346 lname="uk"; hname=i18n("Ukrainian"); 00347 } 00348 else if (dname=="lietuviu" || dname=="lithuanian") { 00349 lname="lt"; hname=i18n("Lithuanian"); 00350 } 00351 else if (dname=="francais" || dname=="french") { 00352 lname="fr"; hname=i18n("French"); 00353 } 00354 else if (dname=="belarusian") { // waiting for post 2.2 to not dissapoint translators 00355 lname="be"; hname=i18n("Belarusian"); 00356 } 00357 else if( dname == "magyar" ) { 00358 lname="hu"; hname=i18n("Hungarian"); 00359 sChangeEncoding(KS_E_LATIN2); 00360 } 00361 else { 00362 lname=""; hname=i18n("Unknown ispell dictionary", "Unknown"); 00363 } 00364 if (!extension.isEmpty()) 00365 { 00366 hname = hname + " (" + extension + ")"; 00367 } 00368 00369 //We have explicitly chosen English as the default here. 00370 if ( ( KGlobal::locale()->language() == QString::fromLatin1("C") && 00371 lname==QString::fromLatin1("en") ) || 00372 KGlobal::locale()->language() == lname ) 00373 return true; 00374 00375 return false; 00376 } 00377 00378 void 00379 KSpellConfig::fillInDialog () 00380 { 00381 if ( nodialog ) 00382 return; 00383 00384 kdDebug(750) << "KSpellConfig::fillinDialog" << endl; 00385 00386 cb1->setChecked( noRootAffix() ); 00387 cb2->setChecked( runTogether() ); 00388 encodingcombo->setCurrentItem( encoding() ); 00389 clientcombo->setCurrentItem( client() ); 00390 00391 // get list of available dictionaries 00392 if ( iclient == KS_CLIENT_ISPELL ) 00393 getAvailDictsIspell(); 00394 else if ( iclient == KS_CLIENT_HSPELL ) 00395 { 00396 langfnames.clear(); 00397 dictcombo->clear(); 00398 langfnames.append(""); // Default 00399 dictcombo->insertItem( i18n("Hebrew") ); 00400 } else if ( iclient == KS_CLIENT_ZEMBEREK ) { 00401 langfnames.clear(); 00402 dictcombo->clear(); 00403 langfnames.append(""); 00404 dictcombo->insertItem( i18n("Turkish") ); 00405 } 00406 else if ( iclient == KS_CLIENT_ASPELL ) 00407 getAvailDictsAspell(); 00408 else 00409 getAvailDictsHunspell(); 00410 00411 // select the used dictionary in the list 00412 int whichelement=-1; 00413 00414 if ( dictFromList() ) 00415 whichelement = langfnames.findIndex(dictionary()); 00416 00417 dictcombo->setMinimumWidth (dictcombo->sizeHint().width()); 00418 00419 if (dictionary().isEmpty() || whichelement!=-1) 00420 { 00421 setDictFromList (true); 00422 if (whichelement!=-1) 00423 dictcombo->setCurrentItem(whichelement); 00424 } 00425 else 00426 // Current dictionary vanished, present the user with a default if possible. 00427 if ( !langfnames.empty() ) 00428 { 00429 setDictFromList( true ); 00430 dictcombo->setCurrentItem(0); 00431 } 00432 else 00433 setDictFromList( false ); 00434 00435 sDictionary( dictFromList() ); 00436 sPathDictionary( !dictFromList() ); 00437 00438 } 00439 00440 00441 void KSpellConfig::getAvailDictsIspell () { 00442 00443 langfnames.clear(); 00444 dictcombo->clear(); 00445 langfnames.append(""); // Default 00446 dictcombo->insertItem( i18n("ISpell Default") ); 00447 00448 // dictionary path 00449 QFileInfo dir ("/usr/lib" KDELIBSUFF "/ispell"); 00450 if (!dir.exists() || !dir.isDir()) 00451 dir.setFile ("/usr/local/lib" KDELIBSUFF "/ispell"); 00452 if (!dir.exists() || !dir.isDir()) 00453 dir.setFile ("/usr/local/share/ispell"); 00454 if (!dir.exists() || !dir.isDir()) 00455 dir.setFile ("/usr/share/ispell"); 00456 if (!dir.exists() || !dir.isDir()) 00457 dir.setFile ("/usr/pkg/lib"); 00458 /* TODO get them all instead of just one of them. 00459 * If /usr/local/lib exists, it skips the rest 00460 if (!dir.exists() || !dir.isDir()) 00461 dir.setFile ("/usr/local/lib"); 00462 */ 00463 if (!dir.exists() || !dir.isDir()) return; 00464 00465 kdDebug(750) << "KSpellConfig::getAvailDictsIspell " 00466 << dir.filePath() << " " << dir.dirPath() << endl; 00467 00468 const QDir thedir (dir.filePath(),"*.hash"); 00469 const QStringList entryList = thedir.entryList(); 00470 00471 kdDebug(750) << "KSpellConfig" << thedir.path() << "\n" << endl; 00472 kdDebug(750) << "entryList().count()=" 00473 << entryList.count() << endl; 00474 00475 QStringList::const_iterator entryListItr = entryList.constBegin(); 00476 const QStringList::const_iterator entryListEnd = entryList.constEnd(); 00477 00478 for ( ; entryListItr != entryListEnd; ++entryListItr) 00479 { 00480 QString fname, lname, hname; 00481 fname = *entryListItr; 00482 00483 // remove .hash 00484 if (fname.endsWith(".hash")) fname.remove (fname.length()-5,5); 00485 00486 if (interpret (fname, lname, hname) && langfnames.first().isEmpty()) 00487 { // This one is the KDE default language 00488 // so place it first in the lists (overwrite "Default") 00489 00490 langfnames.remove ( langfnames.begin() ); 00491 langfnames.prepend ( fname ); 00492 00493 hname=i18n("default spelling dictionary" 00494 ,"Default - %1 [%2]").arg(hname).arg(fname); 00495 00496 dictcombo->changeItem (hname,0); 00497 } 00498 else 00499 { 00500 langfnames.append (fname); 00501 hname=hname+" ["+fname+"]"; 00502 00503 dictcombo->insertItem (hname); 00504 } 00505 } 00506 } 00507 00508 void KSpellConfig::getAvailDictsAspell () { 00509 00510 langfnames.clear(); 00511 dictcombo->clear(); 00512 00513 langfnames.append(""); // Default 00514 dictcombo->insertItem (i18n("ASpell Default")); 00515 00516 // Aspell now have /usr/lib/aspell as 00517 // ASPELL_DATADIR default. 00518 QFileInfo dir ( ASPELL_DATADIR ); 00519 if (!dir.exists() || !dir.isDir()) 00520 dir.setFile ("/usr/lib" KDELIBSUFF "/aspell-0.60"); 00521 if (!dir.exists() || !dir.isDir()) 00522 dir.setFile ("/usr/local/lib" KDELIBSUFF "/aspell"); 00523 if (!dir.exists() || !dir.isDir()) 00524 dir.setFile ("/usr/share/aspell"); 00525 if (!dir.exists() || !dir.isDir()) 00526 dir.setFile ("/usr/local/share/aspell"); 00527 if (!dir.exists() || !dir.isDir()) 00528 dir.setFile ("/usr/pkg/lib/aspell"); 00529 if (!dir.exists() || !dir.isDir()) return; 00530 00531 kdDebug(750) << "KSpellConfig::getAvailDictsAspell " 00532 << dir.filePath() << " " << dir.dirPath() << endl; 00533 00534 const QDir thedir (dir.filePath(),"*"); 00535 const QStringList entryList = thedir.entryList(); 00536 00537 kdDebug(750) << "KSpellConfig" << thedir.path() << "\n" << endl; 00538 kdDebug(750) << "entryList().count()=" 00539 << entryList.count() << endl; 00540 00541 QStringList::const_iterator entryListItr = entryList.constBegin(); 00542 const QStringList::const_iterator entryListEnd = entryList.constEnd(); 00543 00544 for ( ; entryListItr != entryListEnd; ++entryListItr) 00545 { 00546 QString fname, lname, hname; 00547 fname = *entryListItr; 00548 00549 // consider only simple dicts without '-' in the name 00550 // FIXME: may be this is wrong an the list should contain 00551 // all *.multi files too, to allow using special dictionaries 00552 00553 // Well, KSpell2 has a better way to do this, but this code has to be 00554 // cleaned up somehow: since aspell 0.6 we have quite a lot of files in the 00555 // aspell dictionary that are not dictionaries. These must not be presented as "languages" 00556 // We only keep 00557 // *.rws: dictionary 00558 // *.multi: definition file to load several subdictionaries 00559 if ( !( fname.endsWith(".rws") || fname.endsWith(".multi") ) ) { 00560 // remove noise from the language list 00561 continue; 00562 } 00563 if (fname[0] != '.') 00564 { 00565 00566 // remove .multi 00567 if (fname.endsWith(".multi")) fname.remove (fname.length()-6,6); 00568 // remove .rws 00569 if (fname.endsWith(".rws")) fname.remove (fname.length()-4,4); 00570 00571 if (interpret (fname, lname, hname) && langfnames.first().isEmpty()) 00572 { // This one is the KDE default language 00573 // so place it first in the lists (overwrite "Default") 00574 00575 langfnames.remove ( langfnames.begin() ); 00576 langfnames.prepend ( fname ); 00577 00578 hname=i18n("default spelling dictionary" 00579 ,"Default - %1").arg(hname); 00580 00581 dictcombo->changeItem (hname,0); 00582 } 00583 else 00584 { 00585 langfnames.append (fname); 00586 dictcombo->insertItem (hname); 00587 } 00588 } 00589 } 00590 } 00591 00592 void KSpellConfig::getAvailDictsHunspell () { 00593 00594 langfnames.clear(); 00595 dictcombo->clear(); 00596 langfnames.append(""); // Default 00597 dictcombo->insertItem( i18n("Hunspell Default") ); 00598 00599 // dictionary path 00600 QFileInfo dir ("/usr/share/myspell"); 00601 if (!dir.exists() || !dir.isDir()) 00602 dir.setFile ("/usr/share/hunspell"); 00603 if (!dir.exists() || !dir.isDir()) return; 00604 00605 kdDebug(750) << "KSpellConfig::getAvailDictsHunspell " 00606 << dir.filePath() << " " << dir.dirPath() << endl; 00607 00608 const QDir thedir (dir.filePath(),"*.dic"); 00609 const QStringList entryList = thedir.entryList(); 00610 00611 kdDebug(750) << "KSpellConfig" << thedir.path() << "\n" << endl; 00612 kdDebug(750) << "entryList().count()=" 00613 << entryList.count() << endl; 00614 00615 QStringList::const_iterator entryListItr = entryList.constBegin(); 00616 const QStringList::const_iterator entryListEnd = entryList.constEnd(); 00617 00618 for ( ; entryListItr != entryListEnd; ++entryListItr) 00619 { 00620 QString fname, lname, hname; 00621 fname = *entryListItr; 00622 00623 // remove .dic 00624 if (fname.endsWith(".dic")) fname.remove (fname.length()-4,4); 00625 00626 if (interpret (fname, lname, hname) && langfnames.first().isEmpty()) 00627 { // This one is the KDE default language 00628 // so place it first in the lists (overwrite "Default") 00629 00630 langfnames.remove ( langfnames.begin() ); 00631 langfnames.prepend ( fname ); 00632 00633 hname=i18n("default spelling dictionary" 00634 ,"Default - %1 [%2]").arg(hname).arg(fname); 00635 00636 dictcombo->changeItem (hname,0); 00637 } 00638 else 00639 { 00640 langfnames.append (fname); 00641 hname=hname+" ["+fname+"]"; 00642 00643 dictcombo->insertItem (hname); 00644 } 00645 } 00646 } 00647 00648 void 00649 KSpellConfig::fillDicts( QComboBox* box, QStringList* dictionaries ) 00650 { 00651 langfnames.clear(); 00652 if ( box ) { 00653 if ( iclient == KS_CLIENT_ISPELL ) { 00654 box->clear(); 00655 langfnames.append(""); // Default 00656 box->insertItem( i18n("ISpell Default") ); 00657 00658 // dictionary path 00659 QFileInfo dir ("/usr/lib/ispell"); 00660 if (!dir.exists() || !dir.isDir()) 00661 dir.setFile ("/usr/local/lib/ispell"); 00662 if (!dir.exists() || !dir.isDir()) 00663 dir.setFile ("/usr/local/share/ispell"); 00664 if (!dir.exists() || !dir.isDir()) 00665 dir.setFile ("/usr/share/ispell"); 00666 if (!dir.exists() || !dir.isDir()) 00667 dir.setFile ("/usr/pkg/lib"); 00668 /* TODO get them all instead of just one of them. 00669 * If /usr/local/lib exists, it skips the rest 00670 if (!dir.exists() || !dir.isDir()) 00671 dir.setFile ("/usr/local/lib"); 00672 */ 00673 if (!dir.exists() || !dir.isDir()) return; 00674 00675 kdDebug(750) << "KSpellConfig::getAvailDictsIspell " 00676 << dir.filePath() << " " << dir.dirPath() << endl; 00677 00678 const QDir thedir (dir.filePath(),"*.hash"); 00679 const QStringList entryList = thedir.entryList(); 00680 00681 kdDebug(750) << "KSpellConfig" << thedir.path() << "\n" << endl; 00682 kdDebug(750) << "entryList().count()=" 00683 << entryList.count() << endl; 00684 00685 QStringList::const_iterator entryListItr = entryList.constBegin(); 00686 const QStringList::const_iterator entryListEnd = entryList.constEnd(); 00687 00688 for ( ; entryListItr != entryListEnd; ++entryListItr) 00689 { 00690 QString fname, lname, hname; 00691 fname = *entryListItr; 00692 00693 // remove .hash 00694 if (fname.endsWith(".hash")) fname.remove (fname.length()-5,5); 00695 00696 if (interpret (fname, lname, hname) && langfnames.first().isEmpty()) 00697 { // This one is the KDE default language 00698 // so place it first in the lists (overwrite "Default") 00699 00700 langfnames.remove ( langfnames.begin() ); 00701 langfnames.prepend ( fname ); 00702 00703 hname=i18n("default spelling dictionary" 00704 ,"Default - %1 [%2]").arg(hname).arg(fname); 00705 00706 box->changeItem (hname,0); 00707 } 00708 else 00709 { 00710 langfnames.append (fname); 00711 hname=hname+" ["+fname+"]"; 00712 00713 box->insertItem (hname); 00714 } 00715 } 00716 } else if ( iclient == KS_CLIENT_HSPELL ) { 00717 box->clear(); 00718 box->insertItem( i18n("Hebrew") ); 00719 langfnames.append(""); // Default 00720 sChangeEncoding( KS_E_CP1255 ); 00721 } else if ( iclient == KS_CLIENT_ZEMBEREK ) { 00722 box->clear(); 00723 box->insertItem( i18n("Turkish") ); 00724 langfnames.append(""); 00725 sChangeEncoding( KS_E_UTF8 ); 00726 } else if ( iclient == KS_CLIENT_ASPELL ) { 00727 box->clear(); 00728 langfnames.append(""); // Default 00729 box->insertItem (i18n("ASpell Default")); 00730 00731 // dictionary path 00732 // FIXME: use "aspell dump config" to find out the dict-dir 00733 QFileInfo dir ("/usr/lib" KDELIBSUFF "/aspell"); 00734 if (!dir.exists() || !dir.isDir()) 00735 dir.setFile ("/usr/lib" KDELIBSUFF "/aspell-0.60"); 00736 if (!dir.exists() || !dir.isDir()) 00737 dir.setFile ("/usr/local/lib" KDELIBSUFF "/aspell"); 00738 if (!dir.exists() || !dir.isDir()) 00739 dir.setFile ("/usr/share/aspell"); 00740 if (!dir.exists() || !dir.isDir()) 00741 dir.setFile ("/usr/local/share/aspell"); 00742 if (!dir.exists() || !dir.isDir()) 00743 dir.setFile ("/usr/pkg/lib/aspell"); 00744 if (!dir.exists() || !dir.isDir()) return; 00745 00746 kdDebug(750) << "KSpellConfig::getAvailDictsAspell " 00747 << dir.filePath() << " " << dir.dirPath() << endl; 00748 00749 const QDir thedir (dir.filePath(),"*"); 00750 const QStringList entryList = thedir.entryList(); 00751 00752 kdDebug(750) << "KSpellConfig" << thedir.path() << "\n" << endl; 00753 kdDebug(750) << "entryList().count()=" 00754 << entryList.count() << endl; 00755 00756 QStringList::const_iterator entryListItr = entryList.constBegin(); 00757 const QStringList::const_iterator entryListEnd = entryList.constEnd(); 00758 00759 for ( ; entryListItr != entryListEnd; ++entryListItr) 00760 { 00761 QString fname, lname, hname; 00762 fname = *entryListItr; 00763 00764 // consider only simple dicts without '-' in the name 00765 // FIXME: may be this is wrong an the list should contain 00766 // all *.multi files too, to allow using special dictionaries 00767 00768 // Well, KSpell2 has a better way to do this, but this code has to be 00769 // cleaned up somehow: since aspell 0.6 we have quite a lot of files in the 00770 // aspell dictionary that are not dictionaries. These must not be presented as "languages" 00771 // We only keep 00772 // *.rws: dictionary 00773 // *.multi: definition file to load several subdictionaries 00774 if ( !( fname.endsWith(".rws") || fname.endsWith(".multi") ) ) { 00775 // remove noise from the language list 00776 continue; 00777 } 00778 if (fname[0] != '.') 00779 { 00780 00781 // remove .multi 00782 if (fname.endsWith(".multi")) fname.remove (fname.length()-6,6); 00783 // remove .rws 00784 if (fname.endsWith(".rws")) fname.remove (fname.length()-4,4); 00785 00786 if (interpret (fname, lname, hname) && langfnames.first().isEmpty()) 00787 { // This one is the KDE default language 00788 // so place it first in the lists (overwrite "Default") 00789 00790 langfnames.remove ( langfnames.begin() ); 00791 langfnames.prepend ( fname ); 00792 00793 hname=i18n("default spelling dictionary" 00794 ,"Default - %1").arg(hname); 00795 00796 box->changeItem (hname,0); 00797 } 00798 else 00799 { 00800 langfnames.append (fname); 00801 box->insertItem (hname); 00802 } 00803 } 00804 } 00805 } else { 00806 box->clear(); 00807 langfnames.append(""); // Default 00808 box->insertItem( i18n("Hunspell Default") ); 00809 00810 // dictionary path 00811 QFileInfo dir ("/usr/share/myspell"); 00812 if (!dir.exists() || !dir.isDir()) 00813 dir.setFile ("/usr/share/hunspell"); 00814 if (!dir.exists() || !dir.isDir()) return; 00815 00816 kdDebug(750) << "KSpellConfig::getAvailDictsHunspell " 00817 << dir.filePath() << " " << dir.dirPath() << endl; 00818 00819 const QDir thedir (dir.filePath(),"*.dic"); 00820 const QStringList entryList = thedir.entryList(); 00821 00822 kdDebug(750) << "KSpellConfig" << thedir.path() << "\n" << endl; 00823 kdDebug(750) << "entryList().count()=" 00824 << entryList.count() << endl; 00825 00826 QStringList::const_iterator entryListItr = entryList.constBegin(); 00827 const QStringList::const_iterator entryListEnd = entryList.constEnd(); 00828 00829 for ( ; entryListItr != entryListEnd; ++entryListItr) 00830 { 00831 QString fname, lname, hname; 00832 fname = *entryListItr; 00833 00834 // remove .dic 00835 if (fname.endsWith(".dic")) fname.remove (fname.length()-4,4); 00836 00837 if (interpret (fname, lname, hname) && langfnames.first().isEmpty()) 00838 { // This one is the KDE default language 00839 // so place it first in the lists (overwrite "Default") 00840 00841 langfnames.remove ( langfnames.begin() ); 00842 langfnames.prepend ( fname ); 00843 00844 hname=i18n("default spelling dictionary" 00845 ,"Default - %1 [%2]").arg(hname).arg(fname); 00846 00847 box->changeItem (hname,0); 00848 } 00849 else 00850 { 00851 langfnames.append (fname); 00852 hname=hname+" ["+fname+"]"; 00853 00854 box->insertItem (hname); 00855 } 00856 } 00857 } 00858 int whichelement = langfnames.findIndex(qsdict); 00859 if ( whichelement >= 0 ) { 00860 box->setCurrentItem( whichelement ); 00861 } 00862 if ( dictionaries ) 00863 *dictionaries = langfnames; 00864 } 00865 } 00866 00867 /* 00868 * Options setting routines. 00869 */ 00870 00871 void 00872 KSpellConfig::setClient (int c) 00873 { 00874 iclient = c; 00875 00876 if (clientcombo) 00877 clientcombo->setCurrentItem(c); 00878 } 00879 00880 void 00881 KSpellConfig::setNoRootAffix (bool b) 00882 { 00883 bnorootaffix=b; 00884 00885 if(cb1) 00886 cb1->setChecked(b); 00887 } 00888 00889 void 00890 KSpellConfig::setRunTogether(bool b) 00891 { 00892 bruntogether=b; 00893 00894 if(cb2) 00895 cb2->setChecked(b); 00896 } 00897 00898 void 00899 KSpellConfig::setDictionary (const QString s) 00900 { 00901 qsdict=s; //.copy(); 00902 00903 if (qsdict.length()>5) 00904 if ((signed)qsdict.find(".hash")==(signed)qsdict.length()-5) 00905 qsdict.remove (qsdict.length()-5,5); 00906 00907 00908 if(dictcombo) 00909 { 00910 int whichelement=-1; 00911 if (dictFromList()) 00912 { 00913 whichelement = langfnames.findIndex(s); 00914 00915 if(whichelement >= 0) 00916 { 00917 dictcombo->setCurrentItem(whichelement); 00918 } 00919 } 00920 } 00921 00922 00923 } 00924 00925 void 00926 KSpellConfig::setDictFromList (bool dfl) 00927 { 00928 // kdebug (KDEBUG_INFO, 750, "sdfl = %d", dfl); 00929 dictfromlist=dfl; 00930 } 00931 00932 /* 00933 void KSpellConfig::setPersonalDict (const char *s) 00934 { 00935 qspdict=s; 00936 } 00937 */ 00938 00939 void 00940 KSpellConfig::setEncoding (int enctype) 00941 { 00942 enc=enctype; 00943 00944 if(encodingcombo) 00945 encodingcombo->setCurrentItem(enctype); 00946 } 00947 00948 /* 00949 Options reading routines. 00950 */ 00951 int 00952 KSpellConfig::client () const 00953 { 00954 return iclient; 00955 } 00956 00957 00958 bool 00959 KSpellConfig::noRootAffix () const 00960 { 00961 return bnorootaffix; 00962 } 00963 00964 bool 00965 KSpellConfig::runTogether() const 00966 { 00967 return bruntogether; 00968 } 00969 00970 const 00971 QString KSpellConfig::dictionary () const 00972 { 00973 return qsdict; 00974 } 00975 00976 /* 00977 const QString KSpellConfig::personalDict () const 00978 { 00979 return qspdict; 00980 } 00981 */ 00982 00983 int 00984 KSpellConfig::encoding () const 00985 { 00986 return enc; 00987 } 00988 00989 void 00990 KSpellConfig::sRunTogether(bool) 00991 { 00992 setRunTogether (cb2->isChecked()); 00993 emit configChanged(); 00994 } 00995 00996 void 00997 KSpellConfig::sNoAff(bool) 00998 { 00999 setNoRootAffix (cb1->isChecked()); 01000 emit configChanged(); 01001 } 01002 01003 /* 01004 void 01005 KSpellConfig::sBrowseDict() 01006 { 01007 return; 01008 01009 QString qs( KFileDialog::getOpenFileName ("/usr/local/lib","*.hash") ); 01010 if ( !qs.isNull() ) 01011 kle1->setText (qs); 01012 01013 } 01014 */ 01015 01016 /* 01017 void KSpellConfig::sBrowsePDict() 01018 { 01019 //how do I find home directory path?? 01020 QString qs( KFileDialog::getOpenFileName ("",".ispell_*") ); 01021 if ( !qs.isNull() ) 01022 kle2->setText (qs); 01023 01024 01025 } 01026 */ 01027 01028 void 01029 KSpellConfig::sSetDictionary (int i) 01030 { 01031 setDictionary (langfnames[i]); 01032 setDictFromList (true); 01033 QTimer::singleShot( 0, this, SIGNAL( configChanged() ) ); 01034 } 01035 01036 void 01037 KSpellConfig::sDictionary(bool on) 01038 { 01039 if (on) 01040 { 01041 dictcombo->setEnabled (true); 01042 setDictionary (langfnames[dictcombo->currentItem()] ); 01043 setDictFromList (true); 01044 } 01045 else 01046 { 01047 dictcombo->setEnabled (false); 01048 } 01049 emit configChanged(); 01050 } 01051 01052 void 01053 KSpellConfig::sPathDictionary(bool on) 01054 { 01055 return; //enough for now 01056 01057 01058 if (on) 01059 { 01060 //kle1->setEnabled (true); 01061 // browsebutton1->setEnabled (true); 01062 //setDictionary (kle1->text()); 01063 setDictFromList (false); 01064 } 01065 else 01066 { 01067 //kle1->setEnabled (false); 01068 //browsebutton1->setEnabled (false); 01069 } 01070 emit configChanged(); 01071 } 01072 01073 01074 void KSpellConfig::activateHelp( void ) 01075 { 01076 sHelp(); 01077 } 01078 01079 void KSpellConfig::sHelp( void ) 01080 { 01081 kapp->invokeHelp("configuration", "kspell"); 01082 } 01083 01084 /* 01085 void KSpellConfig::textChanged1 (const char *s) 01086 { 01087 setDictionary (s); 01088 } 01089 01090 void KSpellConfig::textChanged2 (const char *) 01091 { 01092 // setPersonalDict (s); 01093 } 01094 */ 01095 01096 void 01097 KSpellConfig::operator= (const KSpellConfig &ksc) 01098 { 01099 //We want to copy the data members, but not the 01100 //pointers to the child widgets 01101 setNoRootAffix (ksc.noRootAffix()); 01102 setRunTogether (ksc.runTogether()); 01103 setDictionary (ksc.dictionary()); 01104 setDictFromList (ksc.dictFromList()); 01105 // setPersonalDict (ksc.personalDict()); 01106 setEncoding (ksc.encoding()); 01107 setClient (ksc.client()); 01108 01109 fillInDialog(); 01110 } 01111 01112 // KDE 4: Make it const QStringList & 01113 void 01114 KSpellConfig::setIgnoreList (QStringList _ignorelist) 01115 { 01116 ignorelist=_ignorelist; 01117 } 01118 01119 QStringList 01120 KSpellConfig::ignoreList () const 01121 { 01122 return ignorelist; 01123 } 01124 01125 // KDE 4: Make it const QStringList & 01126 void 01127 KSpellConfig::setReplaceAllList (QStringList _replacelist) 01128 { 01129 d->replacelist=_replacelist; 01130 } 01131 01132 QStringList 01133 KSpellConfig::replaceAllList() const 01134 { 01135 return d->replacelist; 01136 } 01137 01138 #include "ksconfig.moc" 01139 01140 01141