Port away from QRegEx, drop support of Qt versions below 5.14, add an option to build with Qt6

This commit is contained in:
blaze 2024-08-04 14:29:02 +03:00
parent 33085ca533
commit 35838e405f
No known key found for this signature in database
GPG Key ID: C4996C546950EAE4
12 changed files with 106 additions and 64 deletions

View File

@ -1,5 +1,5 @@
cmake_minimum_required (VERSION 3.5)
project(zeit)
cmake_minimum_required (VERSION 3.1.0)
option(BUILD_TESTS "Build tests" OFF)
@ -11,21 +11,26 @@ set(CRONTAB_V "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
file(READ rev ZEIT_REVISION LIMIT 9)
set(QT_MIN_VERSION "5.7.1")
find_package (Qt5 ${QT_MIN_VERSION} CONFIG REQUIRED COMPONENTS
Core
Gui
Widgets
Test
)
# root actions support
find_package(KF5Auth)
if(KF5Auth_FOUND)
find_package(KF5CoreAddons REQUIRED)
if(KF5CoreAddons_FOUND)
set(BUILD_HELPER TRUE)
endif()
if(WITH_QT6)
find_package (Qt6 6.2.0 CONFIG REQUIRED COMPONENTS Core Gui Widgets Test)
# root actions support KF6
find_package(KF6Auth)
if(KF6Auth_FOUND)
find_package(KF6CoreAddons REQUIRED)
if(KF6CoreAddons_FOUND)
set(BUILD_HELPER TRUE)
endif()
endif()
else()
find_package (Qt5 5.14.0 CONFIG REQUIRED COMPONENTS Core Gui Widgets Test)
# root actions support KF5
find_package(KF5Auth)
if(KF5Auth_FOUND)
find_package(KF5CoreAddons REQUIRED)
if(KF5CoreAddons_FOUND)
set(BUILD_HELPER TRUE)
endif()
endif()
endif()
set(CMAKE_AUTOMOC ON)

View File

@ -36,13 +36,24 @@ set(crontablib_HDRS
add_library(crontab SHARED ${crontablib_SRCS})
target_link_libraries(crontab Qt5::Core Qt5::Gui)
target_include_directories(crontab PRIVATE Qt5::Core Qt5::Gui
${CMAKE_CURRENT_BINARY_DIR}/../src)
if(BUILD_HELPER)
target_link_libraries(crontab KF5::AuthCore)
target_include_directories(crontab PRIVATE KF5::Auth KF5::CoreAddons)
if(WITH_QT6)
target_link_libraries(crontab Qt6::Core Qt6::Gui)
target_include_directories(crontab PRIVATE Qt6::Core Qt6::Gui
${CMAKE_CURRENT_BINARY_DIR}/../src)
if(BUILD_HELPER)
target_link_libraries(crontab KF6::AuthCore)
target_include_directories(crontab PRIVATE KF6::Auth KF6::CoreAddons)
endif()
else()
target_link_libraries(crontab Qt5::Core Qt5::Gui)
target_include_directories(crontab PRIVATE Qt5::Core Qt5::Gui
${CMAKE_CURRENT_BINARY_DIR}/../src)
if(BUILD_HELPER)
target_link_libraries(crontab KF5::AuthCore)
target_include_directories(crontab PRIVATE KF5::Auth KF5::CoreAddons)
endif()
endif()
target_compile_features(crontab PRIVATE cxx_lambdas cxx_nullptr cxx_unicode_literals)
set_target_properties(crontab PROPERTIES VERSION ${CRONTAB_V} SOVERSION ${VERSION_MAJOR})

View File

@ -12,7 +12,7 @@
#include "config.h"
#include "ctcron.h"
#include <QRegExp>
#include <QRegularExpression>
#include <QFile>
#include <QTemporaryFile>
#include <QTextStream>
@ -184,7 +184,8 @@ void CTCron::parseFile(const QString& fileName) {
continue;
leadingComment = false;
// If the first 10 characters don't contain a character, it's probably a disabled entry.
int firstText = line.indexOf(QRegExp(QLatin1String("\\w")));
static const QRegularExpression firstTextRx(QStringLiteral("\\w"));
int firstText = line.indexOf(firstTextRx);
if (firstText < 0) {
comment.clear();
continue;
@ -205,7 +206,8 @@ void CTCron::parseFile(const QString& fileName) {
}
// either a task or a variable
int firstWhiteSpace = line.indexOf(QRegExp(QLatin1String("[ \t]")));
static const QRegularExpression firstWhitespaceRx(QStringLiteral("[ \t]"));
int firstWhiteSpace = line.indexOf(firstWhitespaceRx);
int firstEquals = line.indexOf(QChar::fromLatin1('='));
// if there is an equals sign and either there is no

View File

@ -11,6 +11,7 @@
#include "cttask.h"
#include <QRegularExpression>
#include <QMimeDatabase>
#include <QUrl>
@ -56,7 +57,8 @@ CTTask::CTTask(const QString& tokenString, const QString& _comment,
}
}
int spacePos(tokStr.indexOf(QRegExp(QStringLiteral("[ \t]"))));
static const QRegularExpression spacePosRx(QStringLiteral("[ \t]"));
int spacePos(tokStr.indexOf(spacePosRx));
// If reboot bypass initialize functions so no keys selected in modify task
if (reboot == false) {
@ -66,25 +68,25 @@ CTTask::CTTask(const QString& tokenString, const QString& _comment,
while (isSpace(tokStr, spacePos+1))
spacePos++;
tokStr = tokStr.mid(spacePos+1, tokStr.length()-1);
spacePos = tokStr.indexOf(QRegExp(QStringLiteral("[ \t]")));
spacePos = tokStr.indexOf(spacePosRx);
hour.initialize(tokStr.mid(0, spacePos));
while (isSpace(tokStr, spacePos+1))
spacePos++;
tokStr = tokStr.mid(spacePos+1, tokStr.length()-1);
spacePos = tokStr.indexOf(QRegExp(QStringLiteral("[ \t]")));
spacePos = tokStr.indexOf(spacePosRx);
dayOfMonth.initialize(tokStr.mid(0, spacePos));
while (isSpace(tokStr, spacePos+1))
spacePos++;
tokStr = tokStr.mid(spacePos+1, tokStr.length()-1);
spacePos = tokStr.indexOf(QRegExp(QStringLiteral("[ \t]")));
spacePos = tokStr.indexOf(spacePosRx);
month.initialize(tokStr.mid(0, spacePos));
while (isSpace(tokStr, spacePos+1))
spacePos++;
tokStr = tokStr.mid(spacePos+1, tokStr.length()-1);
spacePos = tokStr.indexOf(QRegExp(QStringLiteral("[ \t]")));
spacePos = tokStr.indexOf(spacePosRx);
dayOfWeek.initialize(tokStr.mid(0, spacePos));
}
@ -92,7 +94,7 @@ CTTask::CTTask(const QString& tokenString, const QString& _comment,
while (isSpace(tokStr, spacePos+1))
spacePos++;
tokStr = tokStr.mid(spacePos+1, tokStr.length()-1);
spacePos = tokStr.indexOf(QRegExp(QStringLiteral("[ \t]")));
spacePos = tokStr.indexOf(spacePosRx);
userLogin = tokStr.mid(0, spacePos);
}
else {
@ -101,7 +103,7 @@ CTTask::CTTask(const QString& tokenString, const QString& _comment,
command = tokStr.mid(spacePos+1, tokStr.length()-1);
// remove leading whitespace
while (command.indexOf(QRegExp(QStringLiteral("[ \t]"))) == 0)
while (command.indexOf(spacePosRx) == 0)
command = command.mid(1, command.length()-1);
comment = _comment;

View File

@ -11,7 +11,7 @@
#include "ctvariable.h"
#include <QRegExp>
#include <QRegularExpression>
#include "ctHelper.h"
@ -27,7 +27,8 @@ CTVariable::CTVariable(const QString& tokenString, const QString& _comment, cons
int spacepos(0);
spacepos = tokStr.indexOf(QRegExp(QLatin1String( "[ =]" )));
static const QRegularExpression spacePosRx(QStringLiteral("[ =]"));
spacepos = tokStr.indexOf(spacePosRx);
variable = tokStr.mid(0, spacepos);
value = tokStr.mid(spacepos+1, tokStr.length()-spacepos-1);

View File

@ -36,17 +36,33 @@ SET(TRANSLATION
../translations/crontablib_ru_RU.ts
../translations/crontablib_sv_SE.ts
)
find_package(Qt5LinguistTools REQUIRED)
qt5_add_translation(QM_FILES ${TRANSLATION})
set(qt_LIBS Qt5::Core Qt5::Gui Qt5::Widgets)
if(WITH_QT6)
find_package(Qt6LinguistTools REQUIRED)
qt_add_translation(QM_FILES ${TRANSLATION})
set(qt_LIBS Qt6::Core Qt6::Gui Qt6::Widgets)
qt_wrap_ui(zeit_FORMS_HEADERS ${zeit_FRMS})
qt_add_resources(zeit_RESOURCES_RCC ${zeit_RSRCS})
set(QT_INCLUDE_DIRECTORIES ${Qt6Core_INCLUDE_DIRS}
${Qt6Widgets_INCLUDE_DIRS})
else()
find_package(Qt5LinguistTools REQUIRED)
qt5_add_translation(QM_FILES ${TRANSLATION})
set(qt_LIBS Qt5::Core Qt5::Gui Qt5::Widgets)
qt5_wrap_ui(zeit_FORMS_HEADERS ${zeit_FRMS})
qt5_add_resources(zeit_RESOURCES_RCC ${zeit_RSRCS})
set(QT_INCLUDE_DIRECTORIES ${Qt5Core_INCLUDE_DIRS}
${Qt5Widgets_INCLUDE_DIRS})
endif()
qt5_wrap_ui(zeit_FORMS_HEADERS ${zeit_FRMS})
qt5_add_resources(zeit_RESOURCES_RCC ${zeit_RSRCS})
add_executable(zeit ${zeit_SRCS} ${zeit_HEADERS_MOC} ${zeit_FORMS_HEADERS}
${QM_FILES} ${zeit_RESOURCES_RCC})
target_include_directories(zeit PRIVATE ${Qt5Core_INCLUDE_DIRS}
${Qt5Widgets_INCLUDE_DIRS}
target_include_directories(zeit PRIVATE ${QT_INCLUDE_DIRECTORIES}
${CMAKE_CURRENT_SOURCE_DIR}/../crontablib
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR})

View File

@ -46,7 +46,7 @@ AlarmDialog::AlarmDialog(CTTask* _ctTask, QWidget* parent) :
QStringList{QStringLiteral("mpv"), QStringLiteral("mplayer")});
proc.waitForFinished();
QStringList players = QString::fromUtf8(proc.readAllStandardOutput())
.split(QRegExp(QStringLiteral("\n")));
.split(QChar::fromLatin1('\n'));
if(players.length() > 0)
ui->lineEditPlayer->setText(players.at(0));
/* file dialog actions */

View File

@ -18,7 +18,7 @@
* ======================================================================== */
#include <QProcess>
#include <QRegExp>
#include <QRegularExpression>
#include "commands.h"
@ -30,10 +30,10 @@ void Commands::addCommand(const QByteArray& command, const QString& time) {
p.closeWriteChannel();
p.waitForFinished();
QString output = QString::fromUtf8(p.readAllStandardError());
QRegExp match(QStringLiteral("job\\s(\\d+)\\s(.*)"));
match.setMinimal(true);
if(match.indexIn(output) > -1) {
uint id = match.cap(1).toUInt();
static const QRegularExpression jobRx(QStringLiteral("job\\s(\\d+?)\\s(.*?)"));
const QRegularExpressionMatch match = jobRx.match(output);
if(match.hasMatch()) {
const uint id = match.captured(1).toUInt();
map.insert(id, command);
}
}
@ -55,19 +55,14 @@ const QVector<Command>& Commands::getCommands() {
QString output = QString::fromUtf8(p.readAllStandardOutput());
QStringList entries;
if(!output.isEmpty())
entries = output
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
.split(QChar::fromLatin1('\n'), Qt::SkipEmptyParts);
#else
.split(QChar::fromLatin1('\n'), QString::SkipEmptyParts);
#endif
entries = output.split(QChar::fromLatin1('\n'), Qt::SkipEmptyParts);
for(QString& entry : entries) {
QRegExp match(QStringLiteral("^(\\d+)\\s+(.*)$"));
match.setMinimal(true);
match.indexIn(entry);
QRegularExpression entryRx(QStringLiteral("^(\\d+?)\\s+?(.*?)$"),
QRegularExpression::MultilineOption);
QRegularExpressionMatch match = entryRx.match(entry);
Command command;
command.id = match.cap(1).toUInt();
command.description = match.cap(2);
command.id = match.captured(1).toUInt();
command.description = match.captured(2);
command.command = map.value(command.id, QStringLiteral("n/a"));
commands.append(command);
}

View File

@ -24,6 +24,7 @@
#include <QProcess>
#include <QSettings>
#include <QSystemTrayIcon>
#include <QActionGroup>
#ifdef BUILD_HELPER
#define ROOT_ACTIONS cron->isSystemCron()

View File

@ -17,6 +17,8 @@
* along with Zeit. If not, see <http://www.gnu.org/licenses/>.
* ======================================================================== */
#include <QRegularExpression>
#include "cttask.h"
#include "taskdialog.h"
#include "ui_taskdialog.h"
@ -84,8 +86,9 @@ void TaskDialog::init() {
setText("*", "*", "*", "*", "*");
}
else {
static const QRegularExpression whitespaceRx(QStringLiteral("\\s+"));
QStringList tokenList = task->schedulingCronFormat()
.split(QRegExp(QStringLiteral("\\s")));
.split(whitespaceRx);
setText(tokenList.at(0), tokenList.at(1),
tokenList.at(2), tokenList.at(4), tokenList.at(3));
}
@ -198,13 +201,16 @@ void TaskDialog::validate() {
ui->commandEdit->setToolTip(tr("Command field should not be empty"));
isInputValid = false;
}
QRegExp rx(QStringLiteral("\\*|\\d{1,2}(,\\d{1,2}|-\\d{1,2}(/\\d{1,2})?)*"));
const QVector<QLineEdit*> leVector {
ui->editMinute,ui->editHour,ui->editDay,ui->editWeekday,ui->editMonth };
for(QLineEdit* le : leVector) {
le->setStyleSheet(QString());
le->setToolTip(helpToolTip);
if(!rx.exactMatch(le->text())) {
static const QRegularExpression rx(
QRegularExpression::anchoredPattern(
QStringLiteral("\\*|\\d{1,2}(,\\d{1,2}|-\\d{1,2}(/\\d{1,2})?)*")));
const QRegularExpressionMatch match = rx.match(le->text());
if(!match.hasMatch()) {
le->setStyleSheet(errorStyleSheet);
le->setToolTip(tr("<b>Invalid input</b><br />") + helpToolTip);
isInputValid = false;

View File

@ -41,8 +41,8 @@ TimerDialog::TimerDialog(Commands* commands_, QWidget* parent) :
proc.start(QStringLiteral("which"),
QStringList{QStringLiteral("mpv"), QStringLiteral("mplayer")});
proc.waitForFinished(-1);
QStringList players = QString::fromUtf8(proc.readAllStandardOutput()).split(
QRegExp(QStringLiteral("\n")));
QStringList players = QString::fromUtf8(proc.readAllStandardOutput())
.split(QChar::fromLatin1('\n'));
if(players.length() > 0)
ui->lineEditPlayer->setText(players.at(0));
/* get system time with a positive offset of 5 minutes */

View File

@ -17,6 +17,8 @@
* along with Zeit. If not, see <http://www.gnu.org/licenses/>.
* ======================================================================== */
#include <QRegularExpression>
#include "ctvariable.h"
#include "variabledialog.h"
#include "ui_variabledialog.h"
@ -45,7 +47,8 @@ VariableDialog::VariableDialog(CTVariable* _ctVar,
ui->varEdit->setToolTip(tr("Variable field should not be empty"));
isInputValid = false;
}
if(ui->varEdit->text().contains(QRegExp(QStringLiteral("\\W")))) {
static const QRegularExpression nonWordRx(QStringLiteral("\\W+"));
if(ui->varEdit->text().contains(nonWordRx)) {
ui->varEdit->setToolTip(tr("Invalid variable name"));
isInputValid = false;
}