Rev 17554 | Rev 17922 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4430 | cbradney | 1 | /* |
2 | For general Scribus (>=1.3.2) copyright and licensing information please refer |
||
3 | to the COPYING file provided with the program. Following this notice may exist |
||
4 | a copyright and/or license notice that predates the release of Scribus 1.3.2 |
||
5 | for which a new license (GPL+exception) is in place. |
||
6 | */ |
||
17542 | craig | 7 | #include "fileunzip.h" |
3 | paul | 8 | #include "hysettings.h" |
10170 | fschmid | 9 | #include "hyphenator.h" |
12787 | pierre | 10 | #include "langmgr.h" |
17542 | craig | 11 | #include "scribusapp.h" |
6279 | fschmid | 12 | #include "scribusdoc.h" |
10200 | cbradney | 13 | #include "util_icon.h" |
10648 | fschmid | 14 | #include "util.h" |
17554 | craig | 15 | #include "util_file.h" |
17542 | craig | 16 | #include "scpaths.h" |
17 | #include <QDomDocument> |
||
18 | #include <QHeaderView> |
||
10170 | fschmid | 19 | #include <QInputDialog> |
17820 | craig | 20 | #include <QLabel> |
21 | #include <QProgressBar> |
||
17542 | craig | 22 | #include <QTableWidgetItem> |
161 | Franz | 23 | |
17542 | craig | 24 | extern ScribusQApp* ScQApp; |
25 | |||
26 | HySettings::HySettings( QWidget* parent) : QWidget( parent ) |
||
3 | paul | 27 | { |
17542 | craig | 28 | |
10170 | fschmid | 29 | setupUi(this); |
17542 | craig | 30 | |
31 | downloadLocation=ScPaths::downloadDir(); |
||
12787 | pierre | 32 | LanguageManager *lmg(LanguageManager::instance()); |
33 | language->setInsertPolicy(QComboBox::InsertAlphabetically); |
||
34 | foreach(QString hlang, lmg->hyphLangs()) |
||
35 | { |
||
36 | language->addItem( lmg->getLangFromAbbrev(hlang), lmg->getLangFromAbbrev(hlang,false) ); |
||
37 | } |
||
17542 | craig | 38 | updateDictList(); |
39 | setAvailDictsXMLFile(downloadLocation + "scribus_spell_dicts.xml"); |
||
17820 | craig | 40 | downloadProgressBar->setVisible(false); |
41 | dlLabel->setVisible(false); |
||
10170 | fschmid | 42 | buttonExceptAdd->setIcon(QIcon(loadIcon("16/list-add.png"))); |
43 | buttonExceptEdit->setEnabled(false); |
||
44 | buttonExceptRemove->setIcon(QIcon(loadIcon("16/list-remove.png"))); |
||
45 | buttonExceptRemove->setEnabled(false); |
||
46 | buttonIgnoreAdd->setIcon(QIcon(loadIcon("16/list-add.png"))); |
||
47 | buttonIgnoreEdit->setEnabled(false); |
||
48 | buttonIgnoreRemove->setIcon(QIcon(loadIcon("16/list-remove.png"))); |
||
49 | buttonIgnoreRemove->setEnabled(false); |
||
50 | connect(buttonIgnoreAdd, SIGNAL(clicked()), this, SLOT(addToIgnoreList())); |
||
51 | connect(buttonIgnoreEdit, SIGNAL(clicked()), this, SLOT(editIgnoreListEntry())); |
||
52 | connect(buttonIgnoreRemove, SIGNAL(clicked()), this, SLOT(removeIgnoreListEntry())); |
||
53 | connect(ignoreList, SIGNAL(itemSelectionChanged()), this, SLOT(enableIgnoreButtons())); |
||
54 | connect(buttonExceptAdd, SIGNAL(clicked()), this, SLOT(addToExceptList())); |
||
55 | connect(buttonExceptEdit, SIGNAL(clicked()), this, SLOT(editExceptListEntry())); |
||
56 | connect(buttonExceptRemove, SIGNAL(clicked()), this, SLOT(removeExceptListEntry())); |
||
57 | connect(exceptList, SIGNAL(itemSelectionChanged()), this, SLOT(enableExceptButtons())); |
||
17542 | craig | 58 | connect(spellDownloadButton, SIGNAL(clicked()), this, SLOT(downloadSpellDicts())); |
59 | connect(availListDownloadButton, SIGNAL(clicked()), this, SLOT(updateAvailDictList())); |
||
3 | paul | 60 | } |
61 | |||
5851 | subik | 62 | void HySettings::restoreDefaults(struct ApplicationPrefs *prefsData) |
63 | { |
||
64 | verbose->setChecked(!prefsData->Automatic); |
||
65 | input->setChecked(prefsData->AutoCheck); |
||
12787 | pierre | 66 | setCurrentComboItem(language, LanguageManager::instance()->getTransLangFromLang(prefsData->Language)); |
5851 | subik | 67 | wordLen->setValue(prefsData->MinWordLen); |
68 | maxCount->setValue(prefsData->HyCount); |
||
10170 | fschmid | 69 | ignoreList->addItems(prefsData->ignoredWords.toList()); |
70 | ignoreList->sortItems(); |
||
71 | exceptList->addItems(prefsData->specialWords.values()); |
||
72 | exceptList->sortItems(); |
||
5851 | subik | 73 | } |
6279 | fschmid | 74 | |
75 | void HySettings::restoreDefaults(ScribusDoc *prefsData) |
||
76 | { |
||
77 | verbose->setChecked(!prefsData->Automatic); |
||
78 | input->setChecked(prefsData->AutoCheck); |
||
12787 | pierre | 79 | setCurrentComboItem(language, LanguageManager::instance()->getTransLangFromLang(prefsData->Language)); |
6279 | fschmid | 80 | wordLen->setValue(prefsData->MinWordLen); |
81 | maxCount->setValue(prefsData->HyCount); |
||
10170 | fschmid | 82 | ignoreList->addItems(prefsData->docHyphenator->ignoredWords.toList()); |
83 | ignoreList->sortItems(); |
||
84 | exceptList->addItems(prefsData->docHyphenator->specialWords.values()); |
||
85 | exceptList->sortItems(); |
||
6279 | fschmid | 86 | } |
10170 | fschmid | 87 | |
88 | void HySettings::addToIgnoreList() |
||
89 | { |
||
90 | bool ok; |
||
91 | QString text = QInputDialog::getText(this, tr("Ignore List"), tr("Add a new Entry"), QLineEdit::Normal, "", &ok); |
||
92 | if ((ok) && (!text.isEmpty())) |
||
93 | { |
||
94 | if (ignoreList->findItems(text, Qt::MatchExactly).count() == 0) |
||
95 | ignoreList->addItem(text); |
||
96 | ignoreList->sortItems(); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | void HySettings::editIgnoreListEntry() |
||
101 | { |
||
102 | bool ok; |
||
103 | QString text = QInputDialog::getText(this, tr("Ignore List"), tr("Edit Entry"), QLineEdit::Normal, ignoreList->currentItem()->text(), &ok); |
||
104 | if ((ok) && (!text.isEmpty())) |
||
105 | { |
||
106 | if (ignoreList->findItems(text, Qt::MatchExactly).count() == 0) |
||
107 | ignoreList->currentItem()->setText(text); |
||
108 | ignoreList->sortItems(); |
||
109 | } |
||
110 | } |
||
111 | |||
112 | void HySettings::removeIgnoreListEntry() |
||
113 | { |
||
114 | QListWidgetItem *item = ignoreList->takeItem(ignoreList->row(ignoreList->currentItem())); |
||
115 | delete item; |
||
116 | if (ignoreList->count() == 0) |
||
117 | { |
||
118 | buttonIgnoreEdit->setEnabled(false); |
||
119 | buttonIgnoreRemove->setEnabled(false); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | void HySettings::enableIgnoreButtons() |
||
124 | { |
||
125 | buttonIgnoreEdit->setEnabled(true); |
||
126 | buttonIgnoreRemove->setEnabled(true); |
||
127 | } |
||
128 | |||
129 | void HySettings::addToExceptList() |
||
130 | { |
||
131 | bool ok; |
||
132 | QString text = QInputDialog::getText(this, tr("Exception List"), tr("Add a new Entry"), QLineEdit::Normal, "", &ok); |
||
133 | if ((ok) && (!text.isEmpty())) |
||
134 | { |
||
135 | if (exceptList->findItems(text, Qt::MatchExactly).count() == 0) |
||
136 | exceptList->addItem(text); |
||
137 | exceptList->sortItems(); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | void HySettings::editExceptListEntry() |
||
142 | { |
||
143 | bool ok; |
||
144 | QString text = QInputDialog::getText(this, tr("Exception List"), tr("Edit Entry"), QLineEdit::Normal, exceptList->currentItem()->text(), &ok); |
||
145 | if ((ok) && (!text.isEmpty())) |
||
146 | { |
||
147 | if (exceptList->findItems(text, Qt::MatchExactly).count() == 0) |
||
148 | exceptList->currentItem()->setText(text); |
||
149 | exceptList->sortItems(); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | void HySettings::removeExceptListEntry() |
||
154 | { |
||
155 | QListWidgetItem *item = exceptList->takeItem(exceptList->row(exceptList->currentItem())); |
||
156 | delete item; |
||
157 | if (exceptList->count() == 0) |
||
158 | { |
||
159 | buttonExceptEdit->setEnabled(false); |
||
160 | buttonExceptRemove->setEnabled(false); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | void HySettings::enableExceptButtons() |
||
165 | { |
||
166 | buttonExceptEdit->setEnabled(true); |
||
167 | buttonExceptRemove->setEnabled(true); |
||
168 | } |
||
169 | |||
17542 | craig | 170 | void HySettings::downloadSpellDicts() |
171 | { |
||
172 | qDebug()<<"Now attempting downloads"; |
||
173 | int rows=availDictTableWidget->rowCount(); |
||
174 | QStringList dlLangs; |
||
175 | for (int i=0; i<rows; ++i) |
||
176 | { |
||
177 | QTableWidgetItem *dlItem=availDictTableWidget->item(i,3); |
||
178 | if (dlItem->checkState()==Qt::Checked) |
||
179 | dlLangs<<availDictTableWidget->item(i,1)->text(); |
||
180 | } |
||
181 | qDebug()<<dlLangs; |
||
17820 | craig | 182 | |
183 | downloadList.clear(); |
||
184 | downloadProgressBar->setValue(0); |
||
185 | downloadProgressBar->setVisible(true); |
||
186 | dlLabel->setVisible(true); |
||
17542 | craig | 187 | int i=0; |
188 | foreach(DictData d, dictList) |
||
189 | { |
||
190 | if (dlLangs.contains(d.lang)) |
||
191 | { |
||
17554 | craig | 192 | if (d.filetype=="zip") |
193 | { |
||
194 | ScQApp->dlManager()->addURL(d.url, true, downloadLocation); |
||
195 | downloadList.append(d); |
||
196 | ++i; |
||
197 | } |
||
198 | if (d.filetype=="plain") |
||
199 | { |
||
200 | //qDebug()<<d.url<<d.files; |
||
201 | QStringList plainURLs(d.files.split(";", QString::SkipEmptyParts)); |
||
202 | foreach (QString s, plainURLs) |
||
17820 | craig | 203 | { |
17554 | craig | 204 | ScQApp->dlManager()->addURL(d.url+"/"+s, true, downloadLocation); |
17820 | craig | 205 | ++i; |
206 | } |
||
17554 | craig | 207 | downloadList.append(d); |
208 | } |
||
17542 | craig | 209 | } |
210 | } |
||
211 | if (i>0) |
||
212 | { |
||
17820 | craig | 213 | qDebug()<<"Files to download:"<<i; |
214 | downloadProgressBar->setRange(0, i); |
||
17542 | craig | 215 | connect(ScQApp->dlManager(), SIGNAL(finished()), this, SLOT(downloadSpellDictsFinished())); |
17820 | craig | 216 | connect(ScQApp->dlManager(), SIGNAL(fileReceived(const QString&)), this, SLOT(updateProgressBar())); |
17542 | craig | 217 | ScQApp->dlManager()->startDownloads(); |
218 | } |
||
219 | } |
||
220 | |||
221 | void HySettings::updateDictList() |
||
222 | { |
||
223 | bool dictsFound=LanguageManager::instance()->findDictionaries(dictionaryPaths); |
||
224 | if (!dictsFound) |
||
225 | return; |
||
226 | dictionaryMap.clear(); |
||
227 | LanguageManager::instance()->findDictionarySets(dictionaryPaths, dictionaryMap); |
||
228 | |||
229 | dictTableWidget->clear(); |
||
230 | dictTableWidget->setRowCount(dictionaryMap.count()); |
||
231 | dictTableWidget->setColumnCount(3); |
||
232 | QMapIterator<QString, QString> i(dictionaryMap); |
||
233 | int row=0; |
||
234 | while (i.hasNext()) |
||
235 | { |
||
236 | i.next(); |
||
237 | int column=0; |
||
238 | //qDebug()<<i.key()<<i.value(); |
||
239 | QTableWidgetItem *newItem1 = new QTableWidgetItem(LanguageManager::instance()->getLangFromAbbrev(i.key())); |
||
240 | newItem1->setFlags(newItem1->flags() & ~Qt::ItemIsEditable); |
||
241 | dictTableWidget->setItem(row, column++, newItem1); |
||
242 | QTableWidgetItem *newItem2 = new QTableWidgetItem(i.key()); |
||
243 | newItem2->setFlags(newItem1->flags()); |
||
244 | dictTableWidget->setItem(row, column++, newItem2); |
||
245 | QTableWidgetItem *newItem3 = new QTableWidgetItem(i.value()); |
||
246 | newItem3->setFlags(newItem1->flags()); |
||
247 | newItem3->setToolTip(i.value()); |
||
248 | dictTableWidget->setItem(row, column++, newItem3); |
||
249 | ++row; |
||
250 | } |
||
251 | QStringList headers; |
||
252 | headers << tr("Language") << tr("Code") << tr("Location"); |
||
253 | dictTableWidget->setHorizontalHeaderLabels(headers); |
||
254 | dictTableWidget->resizeColumnsToContents(); |
||
255 | } |
||
256 | |||
257 | void HySettings::updateAvailDictList() |
||
258 | { |
||
259 | ScQApp->dlManager()->addURL("http://services.scribus.net/scribus_spell_dicts.xml", true, downloadLocation); |
||
260 | connect(ScQApp->dlManager(), SIGNAL(finished()), this, SLOT(downloadDictListFinished())); |
||
261 | ScQApp->dlManager()->startDownloads(); |
||
262 | } |
||
263 | |||
264 | void HySettings::downloadDictListFinished() |
||
265 | { |
||
266 | disconnect(ScQApp->dlManager(), SIGNAL(finished()), this, SLOT(downloadDictListFinished())); |
||
267 | setAvailDictsXMLFile(downloadLocation + "scribus_spell_dicts.xml"); |
||
268 | } |
||
269 | |||
270 | void HySettings::downloadSpellDictsFinished() |
||
271 | { |
||
272 | disconnect(ScQApp->dlManager(), SIGNAL(finished()), this, SLOT(downloadDictListFinished())); |
||
273 | //qDebug()<<"Downloads All Finished"; |
||
274 | QString userDictDir(ScPaths::getUserDictDir(true)); |
||
275 | foreach(DictData d, downloadList) |
||
276 | { |
||
277 | QString basename = QFileInfo(d.url).fileName(); |
||
17554 | craig | 278 | QString filename=downloadLocation+basename; |
17542 | craig | 279 | //qDebug()<<filename; |
17554 | craig | 280 | if (d.filetype=="zip") |
17542 | craig | 281 | { |
17554 | craig | 282 | //qDebug()<<"zip data found"<<filename; |
283 | FileUnzip* fun = new FileUnzip(filename); |
||
284 | QStringList files=d.files.split(";", QString::SkipEmptyParts); |
||
285 | foreach (QString s, files) |
||
286 | { |
||
287 | //qDebug()<<"Unzipping"<<userDictDir+s; |
||
288 | QString data=fun->getFile(s, userDictDir); |
||
289 | } |
||
290 | delete fun; |
||
17542 | craig | 291 | } |
17554 | craig | 292 | if (d.filetype=="plain") |
293 | { |
||
294 | QStringList files=d.files.split(";", QString::SkipEmptyParts); |
||
295 | foreach (QString s, files) |
||
296 | { |
||
297 | //qDebug()<<"plain data found"<<downloadLocation<<userDictDir<<s; |
||
298 | moveFile(downloadLocation+s, userDictDir+s); |
||
299 | } |
||
300 | } |
||
17542 | craig | 301 | } |
302 | updateDictList(); |
||
17820 | craig | 303 | downloadProgressBar->setValue(0); |
304 | downloadProgressBar->setVisible(false); |
||
305 | dlLabel->setVisible(false); |
||
17542 | craig | 306 | } |
307 | |||
17820 | craig | 308 | void HySettings::updateProgressBar() |
309 | { |
||
310 | downloadProgressBar->setValue(downloadProgressBar->value()+1); |
||
311 | } |
||
312 | |||
17542 | craig | 313 | void HySettings::setAvailDictsXMLFile(QString availDictsXMLDataFile) |
314 | { |
||
315 | QFile dataFile(availDictsXMLDataFile); |
||
316 | dataFile.open(QIODevice::ReadOnly); |
||
317 | QTextStream ts(&dataFile); |
||
318 | ts.setCodec(QTextCodec::codecForName("UTF-8")); |
||
319 | QString errorMsg; |
||
320 | int eline; |
||
321 | int ecol; |
||
322 | QDomDocument doc( "scribus_spell_dicts" ); |
||
323 | QString data(ts.readAll()); |
||
324 | dataFile.close(); |
||
325 | if ( !doc.setContent( data, &errorMsg, &eline, &ecol )) |
||
326 | { |
||
327 | if (data.toLower().contains("404 not found")) |
||
328 | qDebug()<<"File not found on server"; |
||
329 | else |
||
330 | qDebug()<<"Could not open file"<<availDictsXMLDataFile; |
||
331 | return; |
||
332 | } |
||
333 | dictList.clear(); |
||
334 | QDomElement docElem = doc.documentElement(); |
||
335 | QDomNode n = docElem.firstChild(); |
||
336 | while( !n.isNull() ) { |
||
337 | QDomElement e = n.toElement(); |
||
338 | if( !e.isNull() ) { |
||
339 | if (e.tagName()=="dictionary") |
||
340 | { |
||
17554 | craig | 341 | if (e.hasAttribute("type") && e.hasAttribute("filetype")) |
17542 | craig | 342 | { |
343 | if (e.attribute("type")=="spell") |
||
344 | { |
||
345 | struct DictData d; |
||
346 | d.desc=e.attribute("description"); |
||
347 | d.download=false; |
||
348 | d.files=e.attribute("files"); |
||
349 | d.url=e.attribute("URL"); |
||
350 | d.version=e.attribute("version"); |
||
351 | d.lang=e.attribute("language"); |
||
352 | d.license=e.attribute("license"); |
||
17554 | craig | 353 | d.filetype=e.attribute("filetype"); |
17542 | craig | 354 | QUrl url(d.url); |
355 | if (url.isValid() && !url.isEmpty() && !url.host().isEmpty()) |
||
356 | dictList.append(d); |
||
357 | else |
||
358 | qDebug()<<"hysettings : availDicts : invalid URL"<<d.url; |
||
359 | } |
||
360 | } |
||
361 | } |
||
362 | } |
||
363 | n = n.nextSibling(); |
||
364 | } |
||
365 | availDictTableWidget->clear(); |
||
366 | availDictTableWidget->setRowCount(dictList.count()); |
||
367 | availDictTableWidget->setColumnCount(4); |
||
368 | int row=0; |
||
369 | foreach(DictData d, dictList) |
||
370 | { |
||
371 | int column=0; |
||
372 | //qDebug()<<d.version<<d.files<<d.url<<d.desc<<d.license; |
||
373 | QTableWidgetItem *newItem1 = new QTableWidgetItem(d.desc); |
||
374 | newItem1->setFlags(newItem1->flags() & ~Qt::ItemIsEditable); |
||
375 | availDictTableWidget->setItem(row, column++, newItem1); |
||
376 | QTableWidgetItem *newItem2 = new QTableWidgetItem(d.lang); |
||
377 | newItem2->setFlags(newItem1->flags()); |
||
378 | availDictTableWidget->setItem(row, column++, newItem2); |
||
379 | QTableWidgetItem *newItem3 = new QTableWidgetItem(); |
||
380 | newItem3->setCheckState(dictionaryMap.contains(d.lang) ? Qt::Checked : Qt::Unchecked); |
||
381 | newItem3->setFlags(newItem1->flags() & ~Qt::ItemIsUserCheckable); |
||
382 | availDictTableWidget->setItem(row, column++, newItem3); |
||
383 | QTableWidgetItem *newItem4 = new QTableWidgetItem(); |
||
384 | newItem4->setCheckState(d.download ? Qt::Checked : Qt::Unchecked); |
||
385 | availDictTableWidget->setItem(row, column++, newItem4); |
||
386 | ++row; |
||
387 | } |
||
388 | QStringList headers; |
||
389 | headers << tr("Language") << tr("Code") << tr("Installed") << tr("Download"); |
||
390 | availDictTableWidget->setHorizontalHeaderLabels(headers); |
||
391 | availDictTableWidget->resizeColumnsToContents(); |
||
392 | } |
||
393 | |||
10170 | fschmid | 394 | int HySettings::getWordLen() |
395 | { |
||
396 | return wordLen->value(); |
||
397 | } |
||
398 | |||
399 | int HySettings::getMaxCount() |
||
400 | { |
||
401 | return maxCount->value(); |
||
402 | } |
||
403 | |||
404 | bool HySettings::getVerbose() |
||
405 | { |
||
406 | return verbose->isChecked(); |
||
407 | } |
||
408 | |||
409 | bool HySettings::getInput() |
||
410 | { |
||
411 | return input->isChecked(); |
||
412 | } |
||
413 | |||
414 | QString HySettings::getLanguage() |
||
415 | { |
||
12787 | pierre | 416 | return language->itemData(language->currentIndex()).toString(); |
10170 | fschmid | 417 | } |
418 | |||
419 | QSet<QString> HySettings::getIgnoreList() |
||
420 | { |
||
421 | QSet<QString> ret; |
||
422 | for (int row = 0; row < ignoreList->count(); row++) |
||
423 | { |
||
424 | ret.insert(ignoreList->item(row)->text()); |
||
425 | } |
||
426 | return ret; |
||
427 | } |
||
428 | |||
429 | QHash<QString, QString> HySettings::getExceptionList() |
||
430 | { |
||
431 | QHash<QString, QString> ret; |
||
432 | for (int row = 0; row < exceptList->count(); row++) |
||
433 | { |
||
434 | ret.insert(exceptList->item(row)->text().remove("-"), exceptList->item(row)->text()); |
||
435 | } |
||
436 | return ret; |
||
437 | } |
||
17542 | craig | 438 | |
439 | void HySettings::setSpellingInvisible() |
||
440 | { |
||
441 | tabWidget->removeTab(1); |
||
442 | } |