Commit 358cf5a3 authored by Pablo Rauzy's avatar Pablo Rauzy Committed by Christoph Cullmann
Browse files

Warn user if kateproject file's JSON is malformed and failed to parse [proper]

Hello :),

Kate was silently failing when my `.kateproject` file contained malformed JSON and it took me a lot of time to figure out this was the problem.

Here I propose a simple way to fix that: show a warning dialog telling the user when it is the case, but only once for each `.kateproject` file's version (I keep a map from project files to their last modification time for that).

However, please note that this is my very first Kate (or KDE) contribution, and that I've never worked with the KDE Frameworks, Qt, or even C++ really (except for small university projects)…
So please do not hesitate to tell me what I did wrong!

In particular:

- I'm not sure my static variable approach is the right one, but I feel like it would be really a lot more work to use a class member (the `readJSONFile` method is static too);
- I'm not sure `QMap<QString, QDateTime>` is the best or the most KDE way to store the map;
- I'm not entirely sure to have respected your naming conventions;
- maybe the content of the error message could be improved.

While I'm here: it was a bit of a mess to get Kate master branch to compile on my Debian stable system, but I must say that I'm quite impressed with KDE's tooling (kdesrc-build is quite impressive).

<details><summary>Details about that</summary>
Here are the Debian packages I needed to install to be able to compile Kate + all 75 KDE packages it depends on when following the instructions from the [Built It](https://kate-editor.org/build-it/) page from Kate's website:
 
qtdeclarative5-dev qtbase5-private-dev qttools5-dev libpolkit-gobject-1-dev libpolkit-agent-1-dev libqt5x11extras5-dev libqt5waylandclient5-dev libwayland-dev libqt5svg5-dev libqca-qt5-2-dev libxcb-res0-dev libxcb-keysyms1-dev ibgcrypt20-dev libboost-dev libxss-dev libqt5scripttools5 wayland-protocols libattr1-dev qtquickcontrols2-5-dev qtscript5-dev
 
I also had to install all these, but some of them may very well be useless, I just didn't want to try them one by one, and the error message in the log was only about XCB without any more details (and libxcb1-dev was already installed…):
 
libxcb-event1-dev libxcb-dri2-0-dev libxcb-util-dev libxcb-shm0-dev libxcb-shape0-dev libxcb-render-util0-dev libxcb-xvmc0-dev libxcb-ewmh-dev libxcb-render0-dev libxcb-cursor-dev libxcb-randr0-dev libxcb-xinput-dev libxcb-dpms0-dev libxcb-record0-dev libxcb-xtest0-dev libxcb-icccm1-dev libxcb-reply1-dev libxcb-property1-dev libxcb-screensaver0-dev libxcb-icccm4-dev libxcb-aux0-dev libxcb-util0-dev libxcb-glx0-dev libxcb-xrm-dev libxcb-atom1-dev libxcb-sync-dev

Once curated, maybe this package list should be added in some form or another to the Build It page?
</details>

Anyway, I *really* love KDE and Kate in particular, and it was itching me for a long time to contribute something. I'm not sure this merge request will be accepted, but at least I have everything set up now :).
parent fd884c5f
Pipeline #208214 passed with stage
in 4 minutes and 58 seconds
......@@ -103,8 +103,13 @@ void KateProject::removeFile(const QString &file)
*
* In case of an error, the returned object verifies isNull() is true.
*/
QJsonDocument KateProject::readJSONFile(const QString &fileName)
QJsonDocument KateProject::readJSONFile(const QString &fileName) const
{
/**
* keep each project file last modification time to warn the user only once per malformed file.
*/
static QMap<QString, QDateTime> lastModifiedTimes;
if (fileName.isEmpty()) {
return QJsonDocument();
}
......@@ -122,6 +127,11 @@ QJsonDocument KateProject::readJSONFile(const QString &fileName)
QJsonDocument document(QJsonDocument::fromJson(jsonData, &parseError));
if (parseError.error != QJsonParseError::NoError) {
QDateTime lastModified = QFileInfo(fileName).lastModified();
if (lastModified > lastModifiedTimes.value(fileName, QDateTime())) {
lastModifiedTimes[fileName] = lastModified;
m_plugin->sendMessage(i18n("Malformed JSON file '%1': %2", fileName, parseError.errorString()), true);
}
return QJsonDocument();
}
......
......@@ -286,7 +286,7 @@ private:
*
* In case of an error, the returned object verifies isNull() is true.
*/
static QJsonDocument readJSONFile(const QString &fileName);
QJsonDocument readJSONFile(const QString &fileName) const;
private:
/**
......
......@@ -698,3 +698,13 @@ void KateProjectPlugin::writeSessionConfig(KConfigGroup &config)
}
config.writeEntry("projects", projectList);
}
void KateProjectPlugin::sendMessage(const QString &text, bool error)
{
QVariantMap genericMessage;
genericMessage.insert(QStringLiteral("type"), error ? QStringLiteral("Error") : QStringLiteral("Info"));
genericMessage.insert(QStringLiteral("category"), i18n("Project"));
genericMessage.insert(QStringLiteral("categoryIcon"), QIcon::fromTheme(QStringLiteral("project-open")));
genericMessage.insert(QStringLiteral("text"), text);
Q_EMIT message(genericMessage);
}
......@@ -165,6 +165,8 @@ public:
*/
KateProject *openProjectForDirectory(const QDir &dir);
void sendMessage(const QString &text, bool error);
Q_SIGNALS:
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment