From 4cf1c980c504b3ac0a1ae5401e4c09dfab4ad21b Mon Sep 17 00:00:00 2001 From: ArsenArsen Date: Wed, 5 Jul 2017 12:05:55 +0200 Subject: [PATCH] Add a filename resolution method Closes #11 --- KShare.pro | 6 ++++-- filenamevalidator.cpp | 12 ++++++++++++ filenamevalidator.hpp | 12 ++++++++++++ platformbackend.hpp | 7 ++++--- platformspecifics/mac/macbackend.cpp | 4 ++++ platformspecifics/mac/macbackend.hpp | 1 + platformspecifics/u32/u32backend.cpp | 4 ++++ platformspecifics/u32/u32backend.hpp | 1 + platformspecifics/x11/x11backend.cpp | 4 ++++ platformspecifics/x11/x11backend.hpp | 1 + settingsdialog.cpp | 2 ++ 11 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 filenamevalidator.cpp create mode 100644 filenamevalidator.hpp diff --git a/KShare.pro b/KShare.pro index 283fe23..59c9675 100644 --- a/KShare.pro +++ b/KShare.pro @@ -65,7 +65,8 @@ SOURCES += main.cpp\ hotkeyinputdialog.cpp \ cropeditor/drawing/arrowitem.cpp \ uploaders/default/imgursettingsdialog.cpp \ - uploaders/default/imgplusuploader.cpp + uploaders/default/imgplusuploader.cpp \ + filenamevalidator.cpp HEADERS += mainwindow.hpp \ cropeditor/cropeditor.hpp \ @@ -109,7 +110,8 @@ HEADERS += mainwindow.hpp \ hotkeyinputdialog.hpp \ cropeditor/drawing/arrowitem.hpp \ uploaders/default/imgursettingsdialog.hpp \ - uploaders/default/imgplusuploader.hpp + uploaders/default/imgplusuploader.hpp \ + filenamevalidator.hpp CONFIG += link_pkgconfig PKGCONFIG += libavformat libavcodec libswscale libavutil diff --git a/filenamevalidator.cpp b/filenamevalidator.cpp new file mode 100644 index 0000000..928f1d0 --- /dev/null +++ b/filenamevalidator.cpp @@ -0,0 +1,12 @@ +#include "filenamevalidator.hpp" + +#include +#include + +FilenameValidator::FilenameValidator(QObject *parent) : QValidator(parent) { +} + +QValidator::State FilenameValidator::validate(QString &input, int &) const { + QString name = formatter::format(input, "lol"); + return PlatformBackend::inst().filenameValid(name) ? State::Acceptable : State::Invalid; +} diff --git a/filenamevalidator.hpp b/filenamevalidator.hpp new file mode 100644 index 0000000..66a46b7 --- /dev/null +++ b/filenamevalidator.hpp @@ -0,0 +1,12 @@ +#ifndef FILENAMEVALIDATOR_HPP +#define FILENAMEVALIDATOR_HPP + +#include + +class FilenameValidator : public QValidator { +public: + FilenameValidator(QObject *parent = nullptr); + QValidator::State validate(QString &input, int &) const override; +}; + +#endif // FILENAMEVALIDATOR_HPP diff --git a/platformbackend.hpp b/platformbackend.hpp index 8133ebc..28a666f 100644 --- a/platformbackend.hpp +++ b/platformbackend.hpp @@ -1,13 +1,14 @@ #ifndef PLATFORMBACKEND_HPP #define PLATFORMBACKEND_HPP +#include -#ifdef __APPLE__ +#ifdef Q_OS_MACOS #include #endif -#ifdef _WIN32 +#ifdef Q_OS_WIN #include #endif -#ifdef __unix__ +#ifdef Q_OS_UNIX #include #endif diff --git a/platformspecifics/mac/macbackend.cpp b/platformspecifics/mac/macbackend.cpp index c750eb9..675cc63 100644 --- a/platformspecifics/mac/macbackend.cpp +++ b/platformspecifics/mac/macbackend.cpp @@ -11,3 +11,7 @@ QPixmap PlatformBackend::getCursor() { pid_t PlatformBackend::pid() { return getpid(); } + +bool PlatformBackend::filenameValid(QString name) { + return !name.contains('/'); +} diff --git a/platformspecifics/mac/macbackend.hpp b/platformspecifics/mac/macbackend.hpp index 4016511..ea15166 100644 --- a/platformspecifics/mac/macbackend.hpp +++ b/platformspecifics/mac/macbackend.hpp @@ -13,6 +13,7 @@ public: static PlatformBackend inst; return inst; } + bool filenameValid(QString name); }; #endif // MACBACKEND_HPP diff --git a/platformspecifics/u32/u32backend.cpp b/platformspecifics/u32/u32backend.cpp index 344ba68..d084928 100644 --- a/platformspecifics/u32/u32backend.cpp +++ b/platformspecifics/u32/u32backend.cpp @@ -28,3 +28,7 @@ DWORD PlatformBackend::pid() { WId PlatformBackend::getActiveWID() { return (WId)GetForegroundWindow(); } + +bool PlatformBackend::filenamValid(QString name) { + return IsValidFileName(name.toLocal8Bit().constData()) == 0; +} diff --git a/platformspecifics/u32/u32backend.hpp b/platformspecifics/u32/u32backend.hpp index 8cb9075..891a808 100644 --- a/platformspecifics/u32/u32backend.hpp +++ b/platformspecifics/u32/u32backend.hpp @@ -17,6 +17,7 @@ public: return inst; } WId getActiveWID(); + bool filenamValid(QString name); }; #endif // U32BACKEND_HPP diff --git a/platformspecifics/x11/x11backend.cpp b/platformspecifics/x11/x11backend.cpp index 8bf841b..fe4fc94 100644 --- a/platformspecifics/x11/x11backend.cpp +++ b/platformspecifics/x11/x11backend.cpp @@ -54,3 +54,7 @@ WId PlatformBackend::getActiveWID() { delete treeReply; return window; } + +bool PlatformBackend::filenameValid(QString name) { + return !name.contains('/'); +} diff --git a/platformspecifics/x11/x11backend.hpp b/platformspecifics/x11/x11backend.hpp index fc1f1c5..63e8d2b 100644 --- a/platformspecifics/x11/x11backend.hpp +++ b/platformspecifics/x11/x11backend.hpp @@ -16,6 +16,7 @@ public: return inst; } WId getActiveWID(); + bool filenameValid(QString name); }; #endif // X11BACKEND_HPP diff --git a/settingsdialog.cpp b/settingsdialog.cpp index eb67258..3a39548 100644 --- a/settingsdialog.cpp +++ b/settingsdialog.cpp @@ -1,4 +1,5 @@ #include "settingsdialog.hpp" +#include "filenamevalidator.hpp" #include "hotkeyinputdialog.hpp" #include "mainwindow.hpp" #include "ui_settingsdialog.h" @@ -77,6 +78,7 @@ SettingsDialog::SettingsDialog(QWidget *parent) : QDialog(parent), ui(new Ui::Se ui->cropX->setValue(settings::settings().value("cropx", 0).toInt()); ui->cropY->setValue(settings::settings().value("cropy", 0).toInt()); setWindowTitle("Settings"); + ui->nameScheme->setValidator(new FilenameValidator(ui->nameScheme)); #ifndef PLATFORM_CAPABILITY_CURSOR ui->captureCursor->setEnabled(false); ui->captureCursor->setText("Capture cursor (disabled: implementation missing)");