From 353ddc87a41d08f275a11da2fef62a5efef595ac Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Sat, 17 Feb 2024 12:29:00 +0100 Subject: [PATCH] DockManager: Introduce read/writeAttribute() helpers They are going to be reused in reading / writing display name and mcus enabled. Change-Id: I679de8858c37fb8629774d0166a7dbc56e5dab76 Reviewed-by: Tim Jenssen --- .../advanceddockingsystem/dockmanager.cpp | 42 +++++++++++++++++++ src/libs/advanceddockingsystem/dockmanager.h | 5 ++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/libs/advanceddockingsystem/dockmanager.cpp b/src/libs/advanceddockingsystem/dockmanager.cpp index b718302f4cc..beb396bc663 100644 --- a/src/libs/advanceddockingsystem/dockmanager.cpp +++ b/src/libs/advanceddockingsystem/dockmanager.cpp @@ -1627,6 +1627,48 @@ bool DockManager::writeMcusEnabled(const FilePath &filePath, const QString &mcus return true; } +QString DockManager::readAttribute(const FilePath &filePath, QStringView key) +{ + auto data = loadFile(filePath); + if (data.isEmpty()) + return {}; + + auto tmp = data.startsWith(" content = filePath.fileContents(); + QTC_ASSERT_EXPECTED(content, return false); + + QDomDocument doc; + QString error_msg; + int error_line, error_col; + if (!doc.setContent(*content, &error_msg, &error_line, &error_col)) { + qWarning() << QString("XML error on line %1, col %2: %3") + .arg(error_line).arg(error_col).arg(error_msg); + return false; + } + + QDomElement docElem = doc.documentElement(); + docElem.setAttribute(key.toString(), value); + + const expected_str result = write(filePath, doc.toByteArray(workspaceXmlFormattingIndent)); + if (result) + return true; + + qWarning() << "Could not write" << key << value << "to" << filePath << ":" << result.error(); + return false; +} + expected_str DockManager::write(const FilePath &filePath, const QByteArray &data) { qCInfo(adsLog) << "Write" << filePath; diff --git a/src/libs/advanceddockingsystem/dockmanager.h b/src/libs/advanceddockingsystem/dockmanager.h index 23aa21a5781..b092eedef09 100644 --- a/src/libs/advanceddockingsystem/dockmanager.h +++ b/src/libs/advanceddockingsystem/dockmanager.h @@ -458,7 +458,7 @@ public: /** * This function sets the tool button style for the given dock widget state. It is possible to - * switch the tool button style depending on the state. If a dock widget is floating, then here + * switch the tool button style depending on the state. If a dock widget is floating, then here * are more space and it is possible to select a style that requires more space like * Qt::ToolButtonTextUnderIcon. For the docked state Qt::ToolButtonIconOnly might be better. */ @@ -783,6 +783,9 @@ signals: void lockWorkspaceChanged(); private: + static QString readAttribute(const Utils::FilePath &filePath, QStringView key); + static bool writeAttribute(const Utils::FilePath &filePath, QStringView key, + const QString &value); static Utils::expected_str write(const Utils::FilePath &filePath, const QByteArray &data); Utils::expected_str loadWorkspace(const Workspace &workspace) const;