From 5be56b517a913ef7fa51548c3b13df1160305c03 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=BCrgen=20Spitzm=C3=BCller?= Date: Fri, 27 Mar 2009 11:22:54 +0000 Subject: [PATCH] Fix bug 5827 (validate date-insert argument): * src/support/os*.{cpp,h}: - new function is_valid_strftime that validates strftime arguments, OS dependant (win32 differs here) * src/Text3.cpp: - use is_valid_strftime in LFUN_DATE_INSERT status check. * src/frontends/qt4/GuiPrefs.{cpp, h}: - new GUI validator for strftime. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@28932 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Text3.cpp | 9 ++++++- src/frontends/qt4/GuiPrefs.cpp | 46 +++++++++++++++++++++++++++++++++- src/frontends/qt4/GuiPrefs.h | 3 +++ src/support/os.h | 3 +++ src/support/os_cygwin.cpp | 17 +++++++++++++ src/support/os_unix.cpp | 18 +++++++++++++ src/support/os_win32.cpp | 17 +++++++++++++ 7 files changed, 111 insertions(+), 2 deletions(-) diff --git a/src/Text3.cpp b/src/Text3.cpp index cfd8c98e9e..fdd6e9c406 100644 --- a/src/Text3.cpp +++ b/src/Text3.cpp @@ -68,6 +68,7 @@ #include "support/gettext.h" #include "support/lstrings.h" #include "support/lyxtime.h" +#include "support/os.h" #include "mathed/InsetMathHull.h" #include "mathed/MathMacroTemplate.h" @@ -2318,6 +2319,13 @@ bool Text::getStatus(Cursor & cur, FuncRequest const & cmd, enable = cur.inset().insetAllowed(MATH_CODE); break; + case LFUN_DATE_INSERT: { + string const format = cmd.argument().empty() + ? lyxrc.date_insert_format : to_utf8(cmd.argument()); + enable = support::os::is_valid_strftime(format); + break; + } + case LFUN_WORD_DELETE_FORWARD: case LFUN_WORD_DELETE_BACKWARD: case LFUN_LINE_DELETE: @@ -2363,7 +2371,6 @@ bool Text::getStatus(Cursor & cur, FuncRequest const & cmd, case LFUN_SERVER_SET_XY: case LFUN_SERVER_GET_LAYOUT: case LFUN_LAYOUT: - case LFUN_DATE_INSERT: case LFUN_SELF_INSERT: case LFUN_LINE_INSERT: case LFUN_MATH_DISPLAY: diff --git a/src/frontends/qt4/GuiPrefs.cpp b/src/frontends/qt4/GuiPrefs.cpp index 4b0da985d3..1af5c049e6 100644 --- a/src/frontends/qt4/GuiPrefs.cpp +++ b/src/frontends/qt4/GuiPrefs.cpp @@ -351,6 +351,35 @@ void PrefPlaintext::update(LyXRC const & rc) } +///////////////////////////////////////////////////////////////////// +// +// StrftimeValidator +// +///////////////////////////////////////////////////////////////////// + +class StrftimeValidator : public QValidator +{ +public: + StrftimeValidator(QWidget *); + QValidator::State validate(QString & input, int & pos) const; +}; + + +StrftimeValidator::StrftimeValidator(QWidget * parent) + : QValidator(parent) +{ +} + + +QValidator::State StrftimeValidator::validate(QString & input, int & /*pos*/) const +{ + if (is_valid_strftime(fromqstr(input))) + return QValidator::Acceptable; + else + return QValidator::Intermediate; +} + + ///////////////////////////////////////////////////////////////////// // // PrefDate @@ -361,11 +390,22 @@ PrefDate::PrefDate(GuiPreferences * form) : PrefModule(qt_(catOutput), qt_("Date format"), form) { setupUi(this); + DateED->setValidator(new StrftimeValidator(DateED)); connect(DateED, SIGNAL(textChanged(QString)), this, SIGNAL(changed())); } +void PrefDate::on_DateED_textChanged(const QString &) +{ + QString t = DateED->text(); + int p = 0; + bool valid = DateED->validator()->validate(t, p) + == QValidator::Acceptable; + setValid(DateLA, valid); +} + + void PrefDate::apply(LyXRC & rc) const { rc.date_insert_format = fromqstr(DateED->text()); @@ -2525,7 +2565,8 @@ GuiPreferences::GuiPreferences(GuiView & lv) addModule(new PrefSpellchecker(this)); addModule(new PrefPrinter(this)); - addModule(new PrefDate(this)); + PrefDate * dateFormat = new PrefDate(this); + addModule(dateFormat); addModule(new PrefPlaintext(this)); addModule(new PrefLatex(this)); @@ -2548,6 +2589,9 @@ GuiPreferences::GuiPreferences(GuiView & lv) bc().setApply(applyPB); bc().setCancel(closePB); bc().setRestore(restorePB); + + // initialize the strftime validator + bc().addCheckedLineEdit(dateFormat->DateED); } diff --git a/src/frontends/qt4/GuiPrefs.h b/src/frontends/qt4/GuiPrefs.h index 8ad157e51a..a9c7e310d2 100644 --- a/src/frontends/qt4/GuiPrefs.h +++ b/src/frontends/qt4/GuiPrefs.h @@ -177,6 +177,9 @@ public: virtual void apply(LyXRC & rc) const; virtual void update(LyXRC const & rc); + +private Q_SLOTS: + void on_DateED_textChanged(const QString &); }; diff --git a/src/support/os.h b/src/support/os.h index 35fa2aa613..c4277ef4fe 100644 --- a/src/support/os.h +++ b/src/support/os.h @@ -74,6 +74,9 @@ std::string internal_path_list(std::string const & p); */ std::string latex_path(std::string const & p); +/// Checks if the format string is suitable on the OS +bool is_valid_strftime(std::string const & p); + /** Returns a string suitable to be passed to popen when * reading a file. */ diff --git a/src/support/os_cygwin.cpp b/src/support/os_cygwin.cpp index 762fdb0c2e..0e741a70c4 100644 --- a/src/support/os_cygwin.cpp +++ b/src/support/os_cygwin.cpp @@ -206,6 +206,23 @@ string latex_path(string const & p) } +bool is_valid_strftime(string const & p) +{ + string::size_type pos = p.find_first_of('%'); + while (pos != string::npos) { + if (pos + 1 == string::npos) + break; + if (!containsOnly(p.substr(pos + 1, 1), + "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+")) + return false; + if (pos + 2 == string::npos) + break; + pos = p.find_first_of('%', pos + 2); + } + return true; +} + + // returns a string suitable to be passed to popen when // reading a pipe char const * popen_read_mode() diff --git a/src/support/os_unix.cpp b/src/support/os_unix.cpp index b817a11fe8..2c3f3262fd 100644 --- a/src/support/os_unix.cpp +++ b/src/support/os_unix.cpp @@ -15,6 +15,7 @@ #include "support/os.h" #include "support/docstring.h" #include "support/FileName.h" +#include "support/lstrings.h" #ifdef __APPLE__ #include @@ -96,6 +97,23 @@ string latex_path(string const & p) } +bool is_valid_strftime(string const & p) +{ + string::size_type pos = p.find_first_of('%'); + while (pos != string::npos) { + if (pos + 1 == string::npos) + break; + if (!containsOnly(p.substr(pos + 1, 1), + "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+")) + return false; + if (pos + 2 == string::npos) + break; + pos = p.find_first_of('%', pos + 2); + } + return true; +} + + char const * popen_read_mode() { return "r"; diff --git a/src/support/os_win32.cpp b/src/support/os_win32.cpp index 5d927c6a6a..fd1b6ad397 100644 --- a/src/support/os_win32.cpp +++ b/src/support/os_win32.cpp @@ -265,6 +265,23 @@ string latex_path(string const & p) } +bool is_valid_strftime(string const & p) +{ + string::size_type pos = p.find_first_of('%'); + while (pos != string::npos) { + if (pos + 1 == string::npos) + break; + if (!containsOnly(p.substr(pos + 1, 1), + "aAbBcdfHIjmMpSUwWxXyYzZ%")) + return false; + if (pos + 2 == string::npos) + break; + pos = p.find_first_of('%', pos + 2); + } + return true; +} + + // returns a string suitable to be passed to popen when // reading a pipe char const * popen_read_mode() -- 2.39.5