From 4eb77ee25e4afa3d7d702eb9cd3138e4c09d652b Mon Sep 17 00:00:00 2001 From: Igor Malinovskiy Date: Fri, 3 Jan 2014 12:01:24 +0200 Subject: [PATCH] Fixed issue #343 --- .../value-view-formatters/AbstractFormatter.h | 6 +++- .../value-view-formatters/JsonFormatter.h | 13 +++++++ .../PHPSerializeFormatter.h | 4 +++ .../value-view-formatters/PlainFormatter.h | 4 +++ .../AbstractFormatter.cpp | 2 +- .../value-view-formatters/JsonFormatter.cpp | 35 ++++++++++++++++--- .../PHPSerializeFormatter.cpp | 10 ++++++ .../value-view-formatters/PlainFormatter.cpp | 9 +++++ .../source/widgets/ValueTabView.cpp | 16 +++++++-- 9 files changed, 90 insertions(+), 9 deletions(-) diff --git a/redis-desktop-manager/include/models/value-view-formatters/AbstractFormatter.h b/redis-desktop-manager/include/models/value-view-formatters/AbstractFormatter.h index dcd940b1..a8ab9068 100644 --- a/redis-desktop-manager/include/models/value-view-formatters/AbstractFormatter.h +++ b/redis-desktop-manager/include/models/value-view-formatters/AbstractFormatter.h @@ -8,10 +8,14 @@ public: virtual ~AbstractFormatter() {}; - virtual void setRawValue(const QString&); + virtual void setSource(const QString&); virtual QString getFormatted() = 0; + virtual bool isValid() = 0; + + virtual QString getRaw() = 0; + enum FormatterType { Plain = 0, Json = 1, PHPSerializer = 2 diff --git a/redis-desktop-manager/include/models/value-view-formatters/JsonFormatter.h b/redis-desktop-manager/include/models/value-view-formatters/JsonFormatter.h index e4cdc703..be7f2712 100644 --- a/redis-desktop-manager/include/models/value-view-formatters/JsonFormatter.h +++ b/redis-desktop-manager/include/models/value-view-formatters/JsonFormatter.h @@ -1,9 +1,22 @@ #pragma once +#include #include "AbstractFormatter.h" class JsonFormatter : public AbstractFormatter { public: + JsonFormatter(); + + void setSource(const QString&); + QString getFormatted(); + + bool isValid(); + + QString getRaw(); + +protected: + bool valid; + QJsonDocument document; }; diff --git a/redis-desktop-manager/include/models/value-view-formatters/PHPSerializeFormatter.h b/redis-desktop-manager/include/models/value-view-formatters/PHPSerializeFormatter.h index 8c4a1225..1a1d6d08 100644 --- a/redis-desktop-manager/include/models/value-view-formatters/PHPSerializeFormatter.h +++ b/redis-desktop-manager/include/models/value-view-formatters/PHPSerializeFormatter.h @@ -12,6 +12,10 @@ public: virtual QString getFormatted(); + bool isValid(); + + QString getRaw(); + private: enum ValueType { diff --git a/redis-desktop-manager/include/models/value-view-formatters/PlainFormatter.h b/redis-desktop-manager/include/models/value-view-formatters/PlainFormatter.h index 65aba19f..fcc21616 100644 --- a/redis-desktop-manager/include/models/value-view-formatters/PlainFormatter.h +++ b/redis-desktop-manager/include/models/value-view-formatters/PlainFormatter.h @@ -6,5 +6,9 @@ class PlainFormatter : public AbstractFormatter { public: QString getFormatted(); + + bool isValid(); + + QString getRaw(); }; diff --git a/redis-desktop-manager/source/models/value-view-formatters/AbstractFormatter.cpp b/redis-desktop-manager/source/models/value-view-formatters/AbstractFormatter.cpp index 8100211b..cbcfdc99 100644 --- a/redis-desktop-manager/source/models/value-view-formatters/AbstractFormatter.cpp +++ b/redis-desktop-manager/source/models/value-view-formatters/AbstractFormatter.cpp @@ -3,7 +3,7 @@ #include "PHPSerializeFormatter.h" #include "JsonFormatter.h" -void AbstractFormatter::setRawValue(const QString&r) +void AbstractFormatter::setSource(const QString&r) { rawValue = r; } diff --git a/redis-desktop-manager/source/models/value-view-formatters/JsonFormatter.cpp b/redis-desktop-manager/source/models/value-view-formatters/JsonFormatter.cpp index 59f7789b..68ac763e 100644 --- a/redis-desktop-manager/source/models/value-view-formatters/JsonFormatter.cpp +++ b/redis-desktop-manager/source/models/value-view-formatters/JsonFormatter.cpp @@ -1,16 +1,43 @@ #include "JsonFormatter.h" +#include #include +JsonFormatter::JsonFormatter() + : valid(false) +{ +} + +void JsonFormatter::setSource(const QString &src) +{ + rawValue = src; + + QScopedPointer parsingError(new QJsonParseError); + document = QJsonDocument::fromJson(rawValue.toStdString().c_str(), parsingError.data()); + valid = (parsingError->error == QJsonParseError::NoError); +} + QString JsonFormatter::getFormatted() { - QJsonParseError * parsingError = new QJsonParseError; - QJsonDocument document = QJsonDocument::fromJson(rawValue.toStdString().c_str(), parsingError); - - if (parsingError->error != QJsonParseError::NoError) + if (!isValid()) { return QString("Invalid JSON"); } return QString(document.toJson(QJsonDocument::Indented)); } + +bool JsonFormatter::isValid() +{ + return valid; +} + +QString JsonFormatter::getRaw() +{ + if (!isValid()) + { + return QString("Invalid JSON"); + } + + return QString(document.toJson(QJsonDocument::Compact)); +} \ No newline at end of file diff --git a/redis-desktop-manager/source/models/value-view-formatters/PHPSerializeFormatter.cpp b/redis-desktop-manager/source/models/value-view-formatters/PHPSerializeFormatter.cpp index aef3a6ff..251c7242 100644 --- a/redis-desktop-manager/source/models/value-view-formatters/PHPSerializeFormatter.cpp +++ b/redis-desktop-manager/source/models/value-view-formatters/PHPSerializeFormatter.cpp @@ -3,6 +3,16 @@ #include #include +bool PHPSerializeFormatter::isValid() +{ + return true; +} + +QString PHPSerializeFormatter::getRaw() +{ + return rawValue; +} + PHPSerializeFormatter::PHPSerializeFormatter() { arrayItemWithValueRegex.setPatternSyntax(QRegExp::RegExp2); diff --git a/redis-desktop-manager/source/models/value-view-formatters/PlainFormatter.cpp b/redis-desktop-manager/source/models/value-view-formatters/PlainFormatter.cpp index 7699d4c8..902f42ca 100644 --- a/redis-desktop-manager/source/models/value-view-formatters/PlainFormatter.cpp +++ b/redis-desktop-manager/source/models/value-view-formatters/PlainFormatter.cpp @@ -6,3 +6,12 @@ QString PlainFormatter::getFormatted() return rawValue; } +bool PlainFormatter::isValid() +{ + return true; +} + +QString PlainFormatter::getRaw() +{ + return rawValue; +} \ No newline at end of file diff --git a/redis-desktop-manager/source/widgets/ValueTabView.cpp b/redis-desktop-manager/source/widgets/ValueTabView.cpp index 1d818252..df7226b9 100644 --- a/redis-desktop-manager/source/widgets/ValueTabView.cpp +++ b/redis-desktop-manager/source/widgets/ValueTabView.cpp @@ -4,6 +4,7 @@ #include "StringKeyModel.h" #include "PaginatedModel.h" #include "AbstractFormatter.h" +#include ValueTabView::ValueTabView(const QString& name, QWidget * parent) : controller(parent), formatter(AbstractFormatter::getFormatter()), @@ -246,7 +247,7 @@ void ValueTabView::onSelectedItemChanged(const QModelIndex & current, const QMod singleValue->clear(); - formatter->setRawValue(model->itemFromIndex(current)->text()); + formatter->setSource(model->itemFromIndex(current)->text()); singleValue->appendPlainText(formatter->getFormatted()); @@ -263,7 +264,7 @@ void ValueTabView::currentFormatterChanged(int index) if (model->getKeyModelType() == StringKeyModel::KEY_MODEL_TYPE) { singleValue->clear(); - formatter->setRawValue(((StringKeyModel*)model)->getValue()); + formatter->setSource(((StringKeyModel*)model)->getValue()); singleValue->appendPlainText(formatter->getFormatted()); } else if (currentCell != nullptr) { onSelectedItemChanged(*currentCell, *currentCell); @@ -275,7 +276,16 @@ void ValueTabView::valueUpdate() if (currentCell == nullptr && model->getKeyModelType() != StringKeyModel::KEY_MODEL_TYPE) return; - emit saveChangedValue(singleValue->toPlainText(), currentCell); + QString value = singleValue->toPlainText(); + + formatter->setSource(value); + + if (!formatter->isValid()) { + QMessageBox::warning(controller, "Invalid value", "Check value format"); + return; + } + + emit saveChangedValue(formatter->getRaw(), currentCell); } const QModelIndex * ValueTabView::getCurrentCell()