26 #include "stringreplacerconf.moc"
29 #include <QtCore/QFile>
30 #include <QtCore/QFileInfo>
31 #include <QtXml/QDomDocument>
32 #include <QtGui/QRadioButton>
33 #include <QtCore/QTextStream>
34 #include <QtGui/QTableWidget>
35 #include <QtGui/QHeaderView>
40 #include <klineedit.h>
42 #include <kpushbutton.h>
44 #include <kstandarddirs.h>
45 #include <kregexpeditorinterface.h>
46 #include <kfiledialog.h>
47 #include <kmessagebox.h>
49 #include <kservicetypetrader.h>
67 substLView->setSortingEnabled(
false);
68 substLView->verticalHeader()->hide();
69 substLView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
71 connect(nameLineEdit, SIGNAL(textChanged(QString)),
73 connect(languageBrowseButton, SIGNAL(clicked()),
74 this, SLOT(slotLanguageBrowseButton_clicked()));
75 connect(addButton, SIGNAL(clicked()),
76 this, SLOT(slotAddButton_clicked()));
77 connect(upButton, SIGNAL(clicked()),
78 this, SLOT(slotUpButton_clicked()));
79 connect(downButton, SIGNAL(clicked()),
80 this, SLOT(slotDownButton_clicked()));
81 connect(editButton, SIGNAL(clicked()),
82 this, SLOT(slotEditButton_clicked()));
83 connect(removeButton, SIGNAL(clicked()),
84 this, SLOT(slotRemoveButton_clicked()));
85 connect(loadButton, SIGNAL(clicked()),
86 this, SLOT(slotLoadButton_clicked()));
87 connect(saveButton, SIGNAL(clicked()),
88 this, SLOT(slotSaveButton_clicked()));
89 connect(clearButton, SIGNAL(clicked()),
90 this, SLOT(slotClearButton_clicked()));
91 connect(substLView, SIGNAL(currentItemChanged(QTableWidgetItem*,QTableWidgetItem*)),
92 this, SLOT(enableDisableButtons()));
93 connect(appIdLineEdit, SIGNAL(textChanged(QString)),
97 m_reEditorInstalled = !KServiceTypeTrader::self()->query(QLatin1String(
"KRegExpEditor/KRegExpEditor" )).isEmpty();
110 KConfigGroup config( c, configGroup );
111 QString wordsFilename = config.readEntry(
"WordListFile" );
112 if ( !wordsFilename.isEmpty() )
114 QString errMsg = loadFromFile( wordsFilename,
true );
115 if ( !errMsg.isEmpty() )
116 kDebug() <<
"StringReplacerConf::load: " << errMsg;
117 enableDisableButtons();
122 QString StringReplacerConf::loadFromFile(
const QString& filename,
bool clear)
125 QFile file( filename );
126 if ( !file.open( QIODevice::ReadOnly ) )
128 return i18n(
"Unable to open file.") + filename;
131 QDomDocument doc( QLatin1String(
"" ) );
132 if ( !doc.setContent( &file ) ) {
134 return i18n(
"File not in proper XML format.");
140 if ( clear ) substLView->setRowCount(0);
143 QDomNodeList nameList = doc.elementsByTagName( QLatin1String(
"name" ) );
144 QDomNode nameNode = nameList.item( 0 );
145 nameLineEdit->setText( nameNode.toElement().text() );
150 QString languageCodes;
151 QDomNodeList languageList = doc.elementsByTagName( QLatin1String(
"language-code" ) );
152 for (
int ndx=0; ndx < languageList.count(); ++ndx )
154 QDomNode languageNode = languageList.item( ndx );
155 if (!languageCodes.isEmpty()) languageCodes += QLatin1Char(
',' );
156 languageCodes += languageNode.toElement().text();
159 m_languageCodeList = languageCodes.split(QLatin1Char(
',' ), QString::SkipEmptyParts);
161 m_languageCodeList += languageCodes.split(QLatin1Char(
',' ), QString::SkipEmptyParts);
163 m_languageCodeList.sort();
166 int ndx = m_languageCodeList.count() - 2;
168 if ( m_languageCodeList[ndx] == m_languageCodeList[ndx+1] )
169 m_languageCodeList.removeAt(ndx+1);
173 for (
int ndx=0; ndx < m_languageCodeList.count(); ++ndx )
175 if (!language.isEmpty()) language += QLatin1Char(
',' );
176 language += KGlobal::locale()->languageCodeToName(m_languageCodeList[ndx]);
178 languageLineEdit->setText(language);
183 QDomNodeList appIdList = doc.elementsByTagName( QLatin1String(
"appid" ) );
185 for (
int ndx=0; ndx < appIdList.count(); ++ndx )
187 QDomNode appIdNode = appIdList.item( ndx );
188 if (!appIds.isEmpty()) appIds += QLatin1Char(
',' );
189 appIds += appIdNode.toElement().text();
191 if ( !clear ) appIds = appIdLineEdit->text() + appIds;
192 appIdLineEdit->setText( appIds );
195 QDomNodeList wordList = doc.elementsByTagName( QLatin1String(
"word" ) );
196 const int wordListCount = wordList.count();
197 for (
int wordIndex = 0; wordIndex < wordListCount; ++wordIndex )
200 QDomNode wordNode = wordList.item(wordIndex);
201 QDomNodeList propList = wordNode.childNodes();
203 QString matchCase = QLatin1String(
"No" );
206 const int propListCount = propList.count();
207 for (
int propIndex = 0; propIndex < propListCount; ++propIndex )
209 QDomNode propNode = propList.item(propIndex);
210 QDomElement prop = propNode.toElement();
211 if (prop.tagName() == QLatin1String(
"type" )) wordType = prop.text();
212 if (prop.tagName() == QLatin1String(
"case" )) matchCase = prop.text();
213 if (prop.tagName() == QLatin1String(
"match" ))
219 if (prop.tagName() == QLatin1String(
"subst" ))
225 QString wordTypeStr =
226 (wordType==QLatin1String(
"RegExp" )?i18nc(
"Abbreviation for 'Regular Expression'",
"RegExp"):i18n(
"Word"));
227 int tableRow = substLView->rowCount();
228 QString matchCaseStr =
229 (matchCase==QLatin1String(
"Yes" )?i18nc(
"Yes or no",
"Yes"):i18nc(
"Yes or no",
"No"));
230 substLView->setRowCount( tableRow + 1 );
231 substLView->setItem( tableRow, 0,
new QTableWidgetItem( wordTypeStr ) );
232 substLView->setItem( tableRow, 1,
new QTableWidgetItem( matchCaseStr ) );
233 substLView->setItem( tableRow, 2,
new QTableWidgetItem( match ) );
234 substLView->setItem( tableRow, 3,
new QTableWidgetItem( subst ) );
242 QString wordsFilename =
243 KGlobal::dirs()->saveLocation(
"data" ,QLatin1String(
"jovie/stringreplacer/" ),
true );
244 if ( wordsFilename.isEmpty() )
246 kDebug() <<
"StringReplacerConf::save: no save location";
249 wordsFilename += configGroup;
250 QString errMsg = saveToFile( wordsFilename );
251 if ( errMsg.isEmpty() )
253 KConfigGroup config( c, configGroup );
254 config.writeEntry(
"WordListFile",
realFilePath(wordsFilename) );
257 kDebug() <<
"StringReplacerConf::save: " << errMsg;
261 QString StringReplacerConf::saveToFile(
const QString& filename)
265 QFile file( filename );
266 if ( !file.open( QIODevice::WriteOnly ) )
267 return i18n(
"Unable to open file ") + filename;
270 QDomDocument doc( QLatin1String(
"" ) );
272 QDomElement root = doc.createElement( QLatin1String(
"wordlist" ) );
273 doc.appendChild( root );
276 QDomElement name = doc.createElement( QLatin1String(
"name" ) );
277 root.appendChild( name );
278 QDomText t = doc.createTextNode( nameLineEdit->text() );
279 name.appendChild( t );
282 for (
int ndx=0; ndx < m_languageCodeList.count(); ++ndx )
284 QDomElement languageCode = doc.createElement( QLatin1String(
"language-code" ) );
285 root.appendChild( languageCode );
286 t = doc.createTextNode( m_languageCodeList[ndx] );
287 languageCode.appendChild( t );
291 QString appId = appIdLineEdit->text().remove(QLatin1Char(
' ' ));
292 if ( !appId.isEmpty() )
294 QStringList appIdList = appId.split( QLatin1Char(
',' ), QString::SkipEmptyParts );
295 for (
int ndx=0; ndx < appIdList.count(); ++ndx )
297 QDomElement appIdElem = doc.createElement( QLatin1String(
"appid" ) );
298 root.appendChild( appIdElem );
299 t = doc.createTextNode( appIdList[ndx] );
300 appIdElem.appendChild( t );
305 for (
int row = 0; row < substLView->rowCount(); ++row )
307 QDomElement wordTag = doc.createElement( QLatin1String(
"word" ) );
308 root.appendChild( wordTag );
309 QDomElement propTag = doc.createElement( QLatin1String(
"type" ) );
310 wordTag.appendChild( propTag);
311 QDomText t = doc.createTextNode(
312 substLView->item(row, 0)->text()==i18n(
"Word")?QLatin1String(
"Word" ):QLatin1String(
"RegExp" ) );
313 propTag.appendChild( t );
315 propTag = doc.createElement( QLatin1String(
"case" ) );
316 wordTag.appendChild( propTag);
317 t = doc.createTextNode(
318 substLView->item(row, 1)->text()==i18nc(
"Yes or no",
"Yes")?QLatin1String(
"Yes" ):QLatin1String(
"No" ) );
319 propTag.appendChild( t );
321 propTag = doc.createElement( QLatin1String(
"match" ) );
322 wordTag.appendChild( propTag);
323 QString s = substLView->item(row, 2)->text();
325 t = doc.createCDATASection( s );
326 propTag.appendChild( t );
328 propTag = doc.createElement( QLatin1String(
"subst" ) );
329 wordTag.appendChild( propTag);
330 s = substLView->item(row, 3)->text();
332 t = doc.createCDATASection( s );
334 propTag.appendChild( t );
338 QTextStream ts( &file );
339 ts.setCodec(
"UTF-8" );
340 ts <<
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
341 ts << doc.toString();
351 m_languageCodeList.clear();
352 languageLineEdit->setText( QLatin1String(
"" ) );
354 nameLineEdit->setText( i18n(
"String Replacer") );
355 substLView->setRowCount(0);
357 appIdLineEdit->setText(QLatin1String(
"" ) );
358 enableDisableButtons();
366 if ( substLView->rowCount() == 0 )
return QString();
367 QString instName = nameLineEdit->text();
368 if ( instName.isEmpty() )
371 if (m_languageCodeList.count() == 1)
372 language = KGlobal::locale()->languageCodeToName(m_languageCodeList[0]);
373 if (m_languageCodeList.count() > 1)
374 language = i18n(
"Multiple Languages");
375 if (!language.isEmpty())
376 instName = i18n(
"String Replacer") + QLatin1String(
" (" ) + language + QLatin1Char(
')' );
382 QString StringReplacerConf::substitutionTypeToString(
const int substitutionType)
384 switch (substitutionType)
386 case stWord:
return i18n(
"Word");
387 case stRegExp:
return i18nc(
"Abbreviation for 'Regular Expresion'",
"RegExp");
389 return i18n(
"Error");
392 void StringReplacerConf::slotLanguageBrowseButton_clicked()
396 i18n(
"Select Languages"),
397 QStringList(m_languageCodeList),
400 int dlgResult = dlg->exec();
401 if (dlgResult == QDialog::Accepted)
402 m_languageCodeList = dlg->selectedLanguageCodes();
404 if (dlgResult != QDialog::Accepted)
return;
406 for (
int ndx=0; ndx < m_languageCodeList.count(); ++ndx)
408 if (!language.isEmpty()) language += QLatin1Char(
',' );
409 language += KGlobal::locale()->languageCodeToName(m_languageCodeList[ndx]);
411 QString s1 = languageLineEdit->text();
412 languageLineEdit->setText(language);
414 QString s2 = nameLineEdit->text();
415 if (m_languageCodeList.count() > 1) language = i18n(
"Multiple Languages");
418 s2.replace( s1, language );
419 s2.replace( i18n(
"Multiple Languages"), language );
421 s2.remove(QLatin1String(
" ()" ));
422 if ( !s2.contains(QLatin1Char(
'(' )) && !language.isEmpty() ) s2 += QLatin1String(
" (" ) + language + QLatin1Char(
')' );
423 nameLineEdit->setText(s2);
427 void StringReplacerConf::enableDisableButtons()
429 int row = substLView->currentRow();
430 bool enableBtn = (row >= 0 && row < substLView->rowCount());
433 upButton->setEnabled(row > 0);
434 downButton->setEnabled(row < (substLView->rowCount() - 1));
436 upButton->setEnabled(
false);
437 downButton->setEnabled(
false);
439 editButton->setEnabled(enableBtn);
440 removeButton->setEnabled(enableBtn);
441 clearButton->setEnabled(substLView->rowCount() > 0);
442 saveButton->setEnabled(substLView->rowCount() > 0);
445 void StringReplacerConf::slotUpButton_clicked()
447 int row = substLView->currentRow();
448 if (row < 1 || row >= substLView->rowCount())
return;
450 QTableWidgetItem *itemAbove = substLView->item(row - 1, 0);
451 QTableWidgetItem *item = substLView->item(row, 0);
452 QString t = itemAbove->text();
453 itemAbove->setText(item->text());
456 itemAbove = substLView->item(row - 1, 1);
457 item = substLView->item(row, 1);
458 t = itemAbove->text();
459 itemAbove->setText(item->text());
462 itemAbove = substLView->item(row - 1, 2);
463 item = substLView->item(row, 2);
464 t = itemAbove->text();
465 itemAbove->setText(item->text());
468 itemAbove = substLView->item(row - 1, 3);
469 item = substLView->item(row, 3);
470 t = itemAbove->text();
471 itemAbove->setText(item->text());
474 substLView->setCurrentItem(substLView->item(row - 1, substLView->currentColumn()));
476 enableDisableButtons();
480 void StringReplacerConf::slotDownButton_clicked()
482 int row = substLView->currentRow();
483 if (row < 0 || row >= substLView->rowCount() - 1)
return;
485 QTableWidgetItem *itemBelow = substLView->item(row + 1, 0);
486 QTableWidgetItem *item = substLView->item(row, 0);
487 QString t = itemBelow->text();
488 itemBelow->setText(item->text());
491 itemBelow = substLView->item(row + 1, 1);
492 item = substLView->item(row, 1);
493 t = itemBelow->text();
494 itemBelow->setText(item->text());
497 itemBelow = substLView->item(row + 1, 2);
498 item = substLView->item(row, 2);
499 t = itemBelow->text();
500 itemBelow->setText(item->text());
503 itemBelow = substLView->item(row + 1, 3);
504 item = substLView->item(row, 3);
505 t = itemBelow->text();
506 itemBelow->setText(item->text());
509 substLView->setCurrentItem(substLView->item(row + 1, substLView->currentColumn()));
511 enableDisableButtons();
515 void StringReplacerConf::slotAddButton_clicked()
517 addOrEditSubstitution(
true );
520 void StringReplacerConf::slotEditButton_clicked()
522 addOrEditSubstitution(
false );
526 void StringReplacerConf::addOrEditSubstitution(
bool isAdd)
530 row = substLView->rowCount() - 1;
532 row = substLView->currentRow();
535 m_editWidget =
new Ui::EditReplacementWidget();
536 m_editWidget->setupUi( w );
538 m_editWidget->matchButton->setEnabled(
false );
541 if ( substLView->item(row, 0)->text() != i18n(
"Word") )
543 m_editWidget->regexpRadioButton->setChecked(
true );
544 m_editWidget->matchButton->setEnabled( m_reEditorInstalled );
546 if ( substLView->item(row, 1)->text() == i18nc(
"Yes or no",
"Yes") )
547 m_editWidget->matchCaseCheckBox->setChecked(
true );
548 m_editWidget->matchLineEdit->setText( substLView->item(row, 2)->text() );
549 m_editWidget->substLineEdit->setText( substLView->item(row, 3)->text() );
552 connect( m_editWidget->matchLineEdit, SIGNAL(textChanged(QString)),
553 this, SLOT(slotMatchLineEdit_textChanged(QString)) );
554 connect( m_editWidget->regexpRadioButton, SIGNAL(clicked()),
555 this, SLOT(slotTypeButtonGroup_clicked()) );
556 connect( m_editWidget->wordRadioButton, SIGNAL(clicked()),
557 this, SLOT(slotTypeButtonGroup_clicked()) );
558 connect( m_editWidget->matchButton, SIGNAL(clicked()),
559 this, SLOT(slotMatchButton_clicked()) );
562 m_editDlg->setCaption(i18n(
"Edit String Replacement"));
563 m_editDlg->setButtons(KDialog::Help|KDialog::Ok|KDialog::Cancel);
565 m_editDlg->setDefaultButton(KDialog::Cancel);
567 m_editDlg->setMainWidget( w );
568 m_editDlg->setHelp( QLatin1String(
"" ), QLatin1String(
"jovie" ) );
569 m_editDlg->enableButton( KDialog::Ok, !m_editWidget->matchLineEdit->text().isEmpty() );
570 int dlgResult = m_editDlg->exec();
571 QString substType = i18n(
"Word" );
572 if ( m_editWidget->regexpRadioButton->isChecked() )
573 substType = i18nc(
"Abbreviation for 'Regular Expression'",
"RegExp");
574 QString matchCase = m_editWidget->matchCaseCheckBox->isChecked()?i18nc(
"Yes or no",
"Yes"):i18nc(
"Yes or no",
"No");
575 QString match = m_editWidget->matchLineEdit->text();
576 QString subst = m_editWidget->substLineEdit->text();
581 if (dlgResult != QDialog::Accepted)
return;
583 if ( match.isEmpty() )
return;
586 row = substLView->rowCount();
587 substLView->setRowCount(row + 1);
588 substLView->setCurrentItem(substLView->item(row, 0));
589 for (
int i = 0; i <= 4; ++i)
590 substLView->setItem(row, i,
new QTableWidgetItem(QLatin1String(
"" )));
592 substLView->item(row, 0)->setText(substType);
593 substLView->item(row, 1)->setText(matchCase);
594 substLView->item(row, 2)->setText(match);
595 substLView->item(row, 3)->setText(subst);
597 enableDisableButtons();
601 void StringReplacerConf::slotMatchLineEdit_textChanged(
const QString& text)
604 if ( !m_editDlg )
return;
605 m_editDlg->enableButton( KDialog::Ok, !text.isEmpty() );
608 void StringReplacerConf::slotRemoveButton_clicked()
610 int row = substLView->currentRow();
611 if (row <= 0 || row >= substLView->rowCount())
return;
612 delete substLView->takeItem(row, 0);
613 delete substLView->takeItem(row, 1);
614 delete substLView->takeItem(row, 2);
615 delete substLView->takeItem(row, 3);
616 substLView->removeRow(row);
617 enableDisableButtons();
621 void StringReplacerConf::slotTypeButtonGroup_clicked()
624 if ( !m_editWidget )
return;
625 m_editWidget->matchButton->setEnabled( m_editWidget->regexpRadioButton->isChecked() && m_reEditorInstalled );
628 void StringReplacerConf::slotMatchButton_clicked()
631 if ( !m_editWidget )
return;
632 if ( !m_editDlg )
return;
633 if ( !m_reEditorInstalled )
return;
635 KServiceTypeTrader::createInstanceFromQuery<KDialog>( QLatin1String(
"KRegExpEditor/KRegExpEditor" ) );
640 KRegExpEditorInterface *reEditor = qobject_cast<KRegExpEditorInterface*>( editorDialog );
641 Q_ASSERT( reEditor );
642 reEditor->setRegExp( m_editWidget->matchLineEdit->text() );
643 int dlgResult = editorDialog->exec();
644 if ( dlgResult == QDialog::Accepted )
646 QString re = reEditor->regExp();
647 m_editWidget->matchLineEdit->setText( re );
648 m_editDlg->enableButton( KDialog::Ok, !re.isEmpty() );
654 void StringReplacerConf::slotLoadButton_clicked()
656 QStringList dataDirs = KGlobal::dirs()->findAllResources(
"data", QLatin1String(
"jovie/stringreplacer/" ));
658 if (!dataDirs.isEmpty()) dataDir = dataDirs.last();
659 QString filename = KFileDialog::getOpenFileName(
661 QLatin1String(
"*.xml|" ) + i18n(
"String Replacer Word List (*.xml)" ),
663 QLatin1String(
"stringreplacer_loadfile" ));
664 if ( filename.isEmpty() )
return;
665 QString errMsg = loadFromFile( filename,
false );
666 enableDisableButtons();
667 if ( !errMsg.isEmpty() )
668 KMessageBox::sorry(
this, errMsg, i18n(
"Error Opening File") );
673 void StringReplacerConf::slotSaveButton_clicked()
675 QString filename = KFileDialog::getSaveFileName(
676 KGlobal::dirs()->saveLocation(
"data" ,QLatin1String(
"kttsd/stringreplacer/" ),
false ),
677 QLatin1String(
"*.xml|" ) + i18n(
"String Replacer Word List (*.xml)" ),
679 QLatin1String(
"stringreplacer_savefile" ));
680 if ( filename.isEmpty() )
return;
681 QString errMsg = saveToFile( filename );
682 enableDisableButtons();
683 if ( !errMsg.isEmpty() )
684 KMessageBox::sorry(
this, errMsg, i18n(
"Error Opening File") );
687 void StringReplacerConf::slotClearButton_clicked()
689 substLView->setRowCount(0);
690 enableDisableButtons();
void cdataEscape(QString *s)
virtual ~StringReplacerConf()
Destructor.
static QString realFilePath(const QString &filename)
StringReplacerConf(QWidget *parent, const QVariantList &args=QVariantList())
Constructor.
void configChanged()
This slot is used internally when the configuration is changed.
virtual void defaults()
This function is called to set the settings in the module to sensible default values.
virtual bool supportsMultiInstance()
Indicates whether the plugin supports multiple instances.
virtual void save(KConfig *c, const QString &configGroup)
This function gets called when the user wants to save the settings in the user interface, updating the config files or wherever the configuration is stored.
virtual void load(KConfig *c, const QString &configGroup)
This method is invoked whenever the module should read its configuration (most of the times from a co...
virtual QString userPlugInName()
Returns the name of the plugin.
void cdataUnescape(QString *s)