From 80ba75d0b1fe8d964f1e4d24a8673bd3b7838081 Mon Sep 17 00:00:00 2001 From: Julien Rioux Date: Tue, 11 Oct 2011 17:57:33 +0000 Subject: [PATCH] Don't allow newline characters in document settings and preferences (#5840). Set a validator on QLineEdit widgets. The validator removes any \n and \r characters, thus preventing users from copy-pasting newline characters into these fields, and subsequently saving them, inadvertantly, to their lyx file or preferences file. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/branches/BRANCH_2_0_X@39828 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/frontends/qt4/GuiBranches.cpp | 2 ++ src/frontends/qt4/GuiDocument.cpp | 52 ++++++++++++++++++++++------ src/frontends/qt4/GuiIndices.cpp | 15 +++++---- src/frontends/qt4/GuiIndices.h | 2 +- src/frontends/qt4/GuiPrefs.cpp | 54 ++++++++++++++++++++++++++++++ src/frontends/qt4/Validator.cpp | 12 +++++++ src/frontends/qt4/Validator.h | 12 +++++++ src/frontends/qt4/ui/BiblioUi.ui | 4 +-- src/frontends/qt4/ui/IndicesUi.ui | 4 +-- src/frontends/qt4/ui/LanguageUi.ui | 2 +- status.20x | 3 ++ 11 files changed, 139 insertions(+), 23 deletions(-) diff --git a/src/frontends/qt4/GuiBranches.cpp b/src/frontends/qt4/GuiBranches.cpp index ebc1f01eb6..c79a42def2 100644 --- a/src/frontends/qt4/GuiBranches.cpp +++ b/src/frontends/qt4/GuiBranches.cpp @@ -70,6 +70,8 @@ GuiBranches::GuiBranches(QWidget * parent) undef_, SLOT(accept())); connect(undef_->cancelPB, SIGNAL(clicked()), undef_, SLOT(reject())); + + newBranchLE->setValidator(new NoNewLineValidator(newBranchLE)); } void GuiBranches::update(BufferParams const & params) diff --git a/src/frontends/qt4/GuiDocument.cpp b/src/frontends/qt4/GuiDocument.cpp index a836d65a95..efa0068942 100644 --- a/src/frontends/qt4/GuiDocument.cpp +++ b/src/frontends/qt4/GuiDocument.cpp @@ -744,6 +744,9 @@ GuiDocument::GuiDocument(GuiView & lv) outputModule->synccustomCB->addItem("\\synctex=-1"); outputModule->synccustomCB->addItem("\\usepackage[active]{srcltx}"); + outputModule->synccustomCB->setValidator(new NoNewLineValidator( + outputModule->synccustomCB)); + // fonts fontModule = new UiWidget; connect(fontModule->osFontsCB, SIGNAL(clicked()), @@ -783,6 +786,11 @@ GuiDocument::GuiDocument(GuiView & lv) connect(fontModule->fontOsfCB, SIGNAL(clicked()), this, SLOT(change_adaptor())); + fontModule->fontencLE->setValidator(new NoNewLineValidator( + fontModule->fontencLE)); + fontModule->cjkFontLE->setValidator(new NoNewLineValidator( + fontModule->cjkFontLE)); + updateFontlist(); fontModule->fontsizeCO->addItem(qt_("Default")); @@ -971,11 +979,14 @@ GuiDocument::GuiDocument(GuiView & lv) this, SLOT(change_adaptor())); connect(langModule->languagePackageCO, SIGNAL(activated(int)), this, SLOT(change_adaptor())); - connect(langModule->languagePackageED, SIGNAL(textChanged(QString)), + connect(langModule->languagePackageLE, SIGNAL(textChanged(QString)), this, SLOT(change_adaptor())); connect(langModule->languagePackageCO, SIGNAL(currentIndexChanged(int)), this, SLOT(languagePackageChanged(int))); + langModule->languagePackageLE->setValidator(new NoNewLineValidator( + langModule->languagePackageLE)); + QAbstractItemModel * language_model = guiApp->languageModel(); // FIXME: it would be nice if sorting was enabled/disabled via a checkbox. language_model->sort(0); @@ -1066,9 +1077,12 @@ GuiDocument::GuiDocument(GuiView & lv) this, SLOT(change_adaptor())); connect(biblioModule->bibtexCO, SIGNAL(activated(int)), this, SLOT(bibtexChanged(int))); - connect(biblioModule->bibtexOptionsED, SIGNAL(textChanged(QString)), + connect(biblioModule->bibtexOptionsLE, SIGNAL(textChanged(QString)), this, SLOT(change_adaptor())); + biblioModule->bibtexOptionsLE->setValidator(new NoNewLineValidator( + biblioModule->bibtexOptionsLE)); + biblioModule->citeStyleCO->addItem(qt_("Author-year")); biblioModule->citeStyleCO->addItem(qt_("Numerical")); biblioModule->citeStyleCO->setCurrentIndex(0); @@ -1145,6 +1159,11 @@ GuiDocument::GuiDocument(GuiView & lv) connect(latexModule->refstyleCB, SIGNAL(clicked()), this, SLOT(change_adaptor())); + latexModule->optionsLE->setValidator(new NoNewLineValidator( + latexModule->optionsLE)); + latexModule->childDocLE->setValidator(new NoNewLineValidator( + latexModule->childDocLE)); + // postscript drivers for (int n = 0; tex_graphics[n][0]; ++n) { QString enc = qt_(tex_graphics_gui[n]); @@ -1246,6 +1265,17 @@ GuiDocument::GuiDocument(GuiView & lv) connect(pdfSupportModule->optionsLE, SIGNAL(textChanged(QString)), this, SLOT(change_adaptor())); + pdfSupportModule->titleLE->setValidator(new NoNewLineValidator( + pdfSupportModule->titleLE)); + pdfSupportModule->authorLE->setValidator(new NoNewLineValidator( + pdfSupportModule->authorLE)); + pdfSupportModule->subjectLE->setValidator(new NoNewLineValidator( + pdfSupportModule->subjectLE)); + pdfSupportModule->keywordsLE->setValidator(new NoNewLineValidator( + pdfSupportModule->keywordsLE)); + pdfSupportModule->optionsLE->setValidator(new NoNewLineValidator( + pdfSupportModule->optionsLE)); + for (int i = 0; backref_opts[i][0]; ++i) pdfSupportModule->backrefCO->addItem(qt_(backref_opts_gui[i])); @@ -1903,14 +1933,14 @@ void GuiDocument::classChanged() void GuiDocument::languagePackageChanged(int i) { - langModule->languagePackageED->setEnabled( + langModule->languagePackageLE->setEnabled( langModule->languagePackageCO->itemData(i).toString() == "custom"); } void GuiDocument::bibtexChanged(int n) { - biblioModule->bibtexOptionsED->setEnabled( + biblioModule->bibtexOptionsLE->setEnabled( biblioModule->bibtexCO->itemData(n).toString() != "default"); changed(); } @@ -2168,7 +2198,7 @@ void GuiDocument::applyView() fromqstr(biblioModule->bibtexCO->itemData( biblioModule->bibtexCO->currentIndex()).toString()); string const bibtex_options = - fromqstr(biblioModule->bibtexOptionsED->text()); + fromqstr(biblioModule->bibtexOptionsLE->text()); if (bibtex_command == "default" || bibtex_options.empty()) bp_.bibtex_command = bibtex_command; else @@ -2236,7 +2266,7 @@ void GuiDocument::applyView() langModule->languagePackageCO->currentIndex()).toString(); if (pack == "custom") bp_.lang_package = - fromqstr(langModule->languagePackageED->text()); + fromqstr(langModule->languagePackageLE->text()); else bp_.lang_package = fromqstr(pack); @@ -2588,15 +2618,15 @@ void GuiDocument::paramsToDialog() int const bpos = biblioModule->bibtexCO->findData(toqstr(command)); if (bpos != -1) { biblioModule->bibtexCO->setCurrentIndex(bpos); - biblioModule->bibtexOptionsED->setText(toqstr(options).trimmed()); + biblioModule->bibtexOptionsLE->setText(toqstr(options).trimmed()); } else { // We reset to default if we do not know the specified compiler // This is for security reasons biblioModule->bibtexCO->setCurrentIndex( biblioModule->bibtexCO->findData(toqstr("default"))); - biblioModule->bibtexOptionsED->clear(); + biblioModule->bibtexOptionsLE->clear(); } - biblioModule->bibtexOptionsED->setEnabled( + biblioModule->bibtexOptionsLE->setEnabled( biblioModule->bibtexCO->currentIndex() != 0); // indices @@ -2641,10 +2671,10 @@ void GuiDocument::paramsToDialog() if (p == -1) { langModule->languagePackageCO->setCurrentIndex( langModule->languagePackageCO->findData("custom")); - langModule->languagePackageED->setText(toqstr(bp_.lang_package)); + langModule->languagePackageLE->setText(toqstr(bp_.lang_package)); } else { langModule->languagePackageCO->setCurrentIndex(p); - langModule->languagePackageED->clear(); + langModule->languagePackageLE->clear(); } //color diff --git a/src/frontends/qt4/GuiIndices.cpp b/src/frontends/qt4/GuiIndices.cpp index 7c21e70acb..8694b98cd7 100644 --- a/src/frontends/qt4/GuiIndices.cpp +++ b/src/frontends/qt4/GuiIndices.cpp @@ -60,6 +60,9 @@ GuiIndices::GuiIndices(QWidget * parent) QString const command = toqstr(*it).left(toqstr(*it).indexOf(" ")); indexCO->addItem(command, command); } + + indexOptionsLE->setValidator(new NoNewLineValidator(indexOptionsLE)); + newIndexLE->setValidator(new NoNewLineValidator(newIndexLE)); } void GuiIndices::update(BufferParams const & params) @@ -82,14 +85,14 @@ void GuiIndices::update(BufferParams const & params) int const pos = indexCO->findData(toqstr(command)); if (pos != -1) { indexCO->setCurrentIndex(pos); - indexOptionsED->setText(toqstr(options).trimmed()); + indexOptionsLE->setText(toqstr(options).trimmed()); } else { // We reset to default if we do not know the specified compiler // This is for security reasons indexCO->setCurrentIndex(indexCO->findData(toqstr("default"))); - indexOptionsED->clear(); + indexOptionsLE->clear(); } - indexOptionsED->setEnabled( + indexOptionsLE->setEnabled( indexCO->currentIndex() != 0); updateView(); @@ -145,7 +148,7 @@ void GuiIndices::apply(BufferParams & params) const string const index_command = fromqstr(indexCO->itemData( indexCO->currentIndex()).toString()); - string const index_options = fromqstr(indexOptionsED->text()); + string const index_options = fromqstr(indexOptionsLE->text()); if (index_command == "default" || index_options.empty()) params.index_command = index_command; else @@ -155,13 +158,13 @@ void GuiIndices::apply(BufferParams & params) const void GuiIndices::on_indexCO_activated(int n) { - indexOptionsED->setEnabled( + indexOptionsLE->setEnabled( indexCO->itemData(n).toString() != "default"); changed(); } -void GuiIndices::on_indexOptionsED_textChanged(QString) +void GuiIndices::on_indexOptionsLE_textChanged(QString) { changed(); } diff --git a/src/frontends/qt4/GuiIndices.h b/src/frontends/qt4/GuiIndices.h index 7e833edb42..a78d07e513 100644 --- a/src/frontends/qt4/GuiIndices.h +++ b/src/frontends/qt4/GuiIndices.h @@ -45,7 +45,7 @@ protected: protected Q_SLOTS: void on_indexCO_activated(int n); - void on_indexOptionsED_textChanged(QString); + void on_indexOptionsLE_textChanged(QString); void on_addIndexPB_pressed(); void on_renamePB_clicked(); void on_removePB_pressed(); diff --git a/src/frontends/qt4/GuiPrefs.cpp b/src/frontends/qt4/GuiPrefs.cpp index 9980897695..391f0c0023 100644 --- a/src/frontends/qt4/GuiPrefs.cpp +++ b/src/frontends/qt4/GuiPrefs.cpp @@ -20,6 +20,7 @@ #include "GuiFontLoader.h" #include "GuiKeySymbol.h" #include "qt_helpers.h" +#include "Validator.h" #include "Author.h" #include "BufferList.h" @@ -387,7 +388,11 @@ PrefOutput::PrefOutput(GuiPreferences * form) : PrefModule(qt_(catOutput), qt_("General"), form) { setupUi(this); + DateED->setValidator(new StrftimeValidator(DateED)); + dviCB->setValidator(new NoNewLineValidator(dviCB)); + pdfCB->setValidator(new NoNewLineValidator(pdfCB)); + connect(DateED, SIGNAL(textChanged(QString)), this, SIGNAL(changed())); connect(plaintextLinelengthSB, SIGNAL(valueChanged(int)), @@ -690,6 +695,16 @@ PrefLatex::PrefLatex(GuiPreferences * form) : PrefModule(qt_(catOutput), qt_("LaTeX"), form) { setupUi(this); + + latexEncodingED->setValidator(new NoNewLineValidator(latexEncodingED)); + latexDviPaperED->setValidator(new NoNewLineValidator(latexDviPaperED)); + latexBibtexED->setValidator(new NoNewLineValidator(latexBibtexED)); + latexJBibtexED->setValidator(new NoNewLineValidator(latexJBibtexED)); + latexIndexED->setValidator(new NoNewLineValidator(latexIndexED)); + latexJIndexED->setValidator(new NoNewLineValidator(latexJIndexED)); + latexNomenclED->setValidator(new NoNewLineValidator(latexNomenclED)); + latexChecktexED->setValidator(new NoNewLineValidator(latexChecktexED)); + connect(latexEncodingCB, SIGNAL(clicked()), this, SIGNAL(changed())); connect(latexEncodingED, SIGNAL(textChanged(QString)), @@ -1339,6 +1354,9 @@ PrefPaths::PrefPaths(GuiPreferences * form) connect(texinputsPrefixED, SIGNAL(textChanged(QString)), this, SIGNAL(changed())); + + pathPrefixED->setValidator(new NoNewLineValidator(pathPrefixED)); + texinputsPrefixED->setValidator(new NoNewLineValidator(texinputsPrefixED)); } @@ -1487,6 +1505,9 @@ PrefSpellchecker::PrefSpellchecker(GuiPreferences * form) this, SIGNAL(changed())); connect(spellcheckNotesCB, SIGNAL(clicked()), this, SIGNAL(changed())); + + altLanguageED->setValidator(new NoNewLineValidator(altLanguageED)); + escapeCharactersED->setValidator(new NoNewLineValidator(escapeCharactersED)); #else spellcheckerCB->setEnabled(false); altLanguageED->setEnabled(false); @@ -1570,6 +1591,8 @@ PrefConverters::PrefConverters(GuiPreferences * form) connect(maxAgeLE, SIGNAL(textEdited(QString)), this, SIGNAL(changed())); + converterED->setValidator(new NoNewLineValidator(converterED)); + converterFlagED->setValidator(new NoNewLineValidator(converterFlagED)); maxAgeLE->setValidator(new QDoubleValidator(maxAgeLE)); //converterDefGB->setFocusProxy(convertersLW); } @@ -1850,8 +1873,14 @@ PrefFileformats::PrefFileformats(GuiPreferences * form) : PrefModule(qt_(catFiles), qt_("File Formats"), form) { setupUi(this); + formatED->setValidator(new FormatNameValidator(formatsCB, form_->formats())); formatsCB->setValidator(new FormatPrettynameValidator(formatsCB, form_->formats())); + extensionED->setValidator(new NoNewLineValidator(extensionED)); + shortcutED->setValidator(new NoNewLineValidator(shortcutED)); + editorED->setValidator(new NoNewLineValidator(editorED)); + viewerED->setValidator(new NoNewLineValidator(viewerED)); + copierED->setValidator(new NoNewLineValidator(copierED)); connect(documentCB, SIGNAL(clicked()), this, SLOT(setFlags())); @@ -2231,6 +2260,10 @@ PrefLanguage::PrefLanguage(GuiPreferences * form) connect(defaultDecimalPointLE, SIGNAL(textChanged(QString)), this, SIGNAL(changed())); + languagePackageED->setValidator(new NoNewLineValidator(languagePackageED)); + startCommandED->setValidator(new NoNewLineValidator(startCommandED)); + endCommandED->setValidator(new NoNewLineValidator(endCommandED)); + uiLanguageCO->clear(); QAbstractItemModel * language_model = guiApp->languageModel(); @@ -2374,6 +2407,24 @@ PrefPrinter::PrefPrinter(GuiPreferences * form) this, SIGNAL(changed())); connect(printerPaperSizeED, SIGNAL(textChanged(QString)), this, SIGNAL(changed())); + + printerNameED->setValidator(new NoNewLineValidator(printerNameED)); + printerCommandED->setValidator(new NoNewLineValidator(printerCommandED)); + printerEvenED->setValidator(new NoNewLineValidator(printerEvenED)); + printerPageRangeED->setValidator(new NoNewLineValidator(printerPageRangeED)); + printerCopiesED->setValidator(new NoNewLineValidator(printerCopiesED)); + printerReverseED->setValidator(new NoNewLineValidator(printerReverseED)); + printerToFileED->setValidator(new NoNewLineValidator(printerToFileED)); + printerPaperTypeED->setValidator(new NoNewLineValidator(printerPaperTypeED)); + printerExtraED->setValidator(new NoNewLineValidator(printerExtraED)); + printerOddED->setValidator(new NoNewLineValidator(printerOddED)); + printerCollatedED->setValidator(new NoNewLineValidator(printerCollatedED)); + printerLandscapeED->setValidator(new NoNewLineValidator(printerLandscapeED)); + printerToPrinterED->setValidator(new NoNewLineValidator(printerToPrinterED)); + printerExtensionED->setValidator(new NoNewLineValidator(printerExtensionED)); + printerPaperSizeED->setValidator(new NoNewLineValidator(printerPaperSizeED)); + printerSpoolCommandED->setValidator(new NoNewLineValidator(printerSpoolCommandED)); + printerSpoolPrefixED->setValidator(new NoNewLineValidator(printerSpoolPrefixED)); } @@ -3114,6 +3165,9 @@ PrefIdentity::PrefIdentity(GuiPreferences * form) this, SIGNAL(changed())); connect(emailED, SIGNAL(textChanged(QString)), this, SIGNAL(changed())); + + nameED->setValidator(new NoNewLineValidator(nameED)); + emailED->setValidator(new NoNewLineValidator(emailED)); } diff --git a/src/frontends/qt4/Validator.cpp b/src/frontends/qt4/Validator.cpp index e156dcb099..7f5d5d45e2 100644 --- a/src/frontends/qt4/Validator.cpp +++ b/src/frontends/qt4/Validator.cpp @@ -138,6 +138,18 @@ QValidator::State DoubleAutoValidator::validate(QString & input, int & pos) cons } +NoNewLineValidator::NoNewLineValidator(QWidget * parent) + : QValidator(parent) +{} + + +QValidator::State NoNewLineValidator::validate(QString & qtext, int &) const +{ + qtext.remove(QRegExp("[\\n\\r]")); + return QValidator::Acceptable; +} + + PathValidator::PathValidator(bool acceptable_if_empty, QWidget * parent) : QValidator(parent), diff --git a/src/frontends/qt4/Validator.h b/src/frontends/qt4/Validator.h index 36250bbdca..e824659323 100644 --- a/src/frontends/qt4/Validator.h +++ b/src/frontends/qt4/Validator.h @@ -124,6 +124,18 @@ private: }; +// A class to ascertain that no newline characters are passed. +class NoNewLineValidator : public QValidator +{ + Q_OBJECT +public: + // Define a validator. + NoNewLineValidator(QWidget *); + // Remove newline characters from input. + QValidator::State validate(QString &, int &) const; +}; + + /** A class to ascertain whether the data passed to the @c validate() * member function is a valid file path. * The test is active only when the path is to be stored in a LaTeX diff --git a/src/frontends/qt4/ui/BiblioUi.ui b/src/frontends/qt4/ui/BiblioUi.ui index cc11229491..6c6d5b8cb0 100644 --- a/src/frontends/qt4/ui/BiblioUi.ui +++ b/src/frontends/qt4/ui/BiblioUi.ui @@ -168,12 +168,12 @@ &Options: - bibtexOptionsED + bibtexOptionsLE - + Define options such as --min-crossrefs (see the documentation of BibTeX) diff --git a/src/frontends/qt4/ui/IndicesUi.ui b/src/frontends/qt4/ui/IndicesUi.ui index ce398b17db..81d014538d 100644 --- a/src/frontends/qt4/ui/IndicesUi.ui +++ b/src/frontends/qt4/ui/IndicesUi.ui @@ -54,12 +54,12 @@ &Options: - indexOptionsED + indexOptionsLE - + Define program options of the selected processor. diff --git a/src/frontends/qt4/ui/LanguageUi.ui b/src/frontends/qt4/ui/LanguageUi.ui index 5741aada02..3dea1c27d6 100644 --- a/src/frontends/qt4/ui/LanguageUi.ui +++ b/src/frontends/qt4/ui/LanguageUi.ui @@ -137,7 +137,7 @@ - + Enter the command to load the language package (default: \usepackage{babel}) diff --git a/status.20x b/status.20x index 3e8809361a..dd9290a3b8 100644 --- a/status.20x +++ b/status.20x @@ -142,6 +142,9 @@ What's new - Fix instant preview when using Python version 2.4 or lower. +- Don't allow copy-pasting newline characters in Document->Settings + and Tools->Preferences (bug 5840). + * ADVANCED FIND AND REPLACE -- 2.39.5