Commit b319a07f authored by David Faure's avatar David Faure
Browse files

KXmlGui: when upgrading a local .rc file, keep new app toolbars

Prompted by graphics/okular!197
parent a9235b34
......@@ -76,6 +76,9 @@ static void createXmlFile(QFile &file, int version, int flags, const QByteArray
"<ToolBar name=\"bookmarkToolBar\">\n"
" <text>Bookmark Toolbar</text>\n"
"</ToolBar>\n"
"<ToolBar name=\"newToolBar\">\n"
" <text>New Toolbar</text>\n"
"</ToolBar>\n"
);
}
if (flags & AddModifiedToolBars) {
......@@ -180,6 +183,8 @@ void KXmlGui_UnitTest::testVersionHandlerSameVersion()
QVERIFY(finalDoc.contains(QLatin1String("sidebartng")));
// Check that the toolbars modified by the user were kept
QVERIFY(finalDoc.contains(QLatin1String("<Action name=\"home\"")));
// and that the toolbar that isn't in the local file, isn't there in the GUI
QVERIFY(!finalDoc.contains(QLatin1String("<ToolBar name=\"newToolBar\"")));
QVERIFY(userFile.open());
const QString userFileContents = QString::fromUtf8(userFile.readAll());
......@@ -245,7 +250,7 @@ void KXmlGui_UnitTest::testVersionHandlerNewVersionUserChanges()
createXmlFile(fileV2, 2, AddActionProperties | AddModifiedToolBars);
fileToVersionMap.insert(fileV2.fileName(), 2);
// more-global file
// more-global (application) file
QTemporaryFile fileV5;
QVERIFY(fileV5.open());
createXmlFile(fileV5, 5, AddToolBars | AddModifiedMenus, "kpartgui");
......@@ -281,6 +286,8 @@ void KXmlGui_UnitTest::testVersionHandlerNewVersionUserChanges()
QVERIFY(finalDoc.contains(QLatin1String("<Action name=\"file_open\"")));
// Check that the toolbars modified by the user were kept
QVERIFY(finalDoc.contains(QLatin1String("<Action name=\"home\"")));
// Check that the toolbars added by the application were kept (https://invent.kde.org/graphics/okular/-/merge_requests/197)
QVERIFY(finalDoc.contains(QLatin1String("<ToolBar name=\"newToolBar\"")));
}
static QStringList collectMenuNames(KXMLGUIFactory &factory)
......
......@@ -46,12 +46,24 @@ static QList<QDomElement> extractToolBars(const QDomDocument &doc)
return toolbars;
}
static void removeAllToolBars(QDomDocument &doc)
static QStringList toolBarNames(const QList<QDomElement> &toolBars)
{
QStringList names;
names.reserve(toolBars.count());
for (const QDomElement &e : toolBars) {
names.append(e.attribute(QStringLiteral("name")));
}
return names;
}
static void removeToolBars(QDomDocument &doc, const QStringList &toolBarNames)
{
QDomElement parent = doc.documentElement();
const QList<QDomElement> toolBars = extractToolBars(doc);
for (const QDomElement &e : toolBars) {
parent.removeChild(e);
if (toolBarNames.contains(e.attribute(QStringLiteral("name")))) {
parent.removeChild(e);
}
}
}
......@@ -316,8 +328,9 @@ KXmlGuiVersionHandler::KXmlGuiVersionHandler(const QStringList &files)
// and store the properties in there
storeActionProperties(document, properties);
if (!toolbars.isEmpty()) {
// remove application toolbars
removeAllToolBars(document);
// remove application toolbars present in the user file
// (not others, that the app might have added since)
removeToolBars(document, toolBarNames(toolbars));
// add user toolbars
insertToolBars(document, toolbars);
}
......
Markdown is supported
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