feat: add missing KF6 framework recipes

This commit is contained in:
2026-05-07 07:53:26 +01:00
parent d8d498f831
commit a69f479b52
2374 changed files with 2610246 additions and 0 deletions
@@ -0,0 +1,21 @@
find_package(Qt6Test ${REQUIRED_QT_VERSION} REQUIRED)
set_package_properties(Qt6Test PROPERTIES PURPOSE "Required for tests")
include(ECMAddTests)
########### a KParts ###############
# don't use kcoreaddons_add_plugin here since we don't want to install it
add_library(notepadpart MODULE)
target_sources(notepadpart PRIVATE notepad.cpp notepad.qrc)
# so we have to do the INSTALL_NAMESPACE thing by hand:
set_target_properties(notepadpart PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/kf6/parts")
target_link_libraries(notepadpart KF6::Parts KF6::I18n)
########### tests ###############
ecm_add_tests(
parttest.cpp
partloadertest.cpp
LINK_LIBRARIES KF6::Parts Qt6::Test KF6::XmlGui
)
@@ -0,0 +1,91 @@
/*
SPDX-FileCopyrightText: 1999, 2000 David Faure <faure@kde.org>
SPDX-FileCopyrightText: 1999, 2000 Simon Hausmann <hausmann@kde.org>
SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "notepad.h"
#include <kparts/mainwindow.h>
#include <kparts/partmanager.h>
#include <QAction>
#include <QDebug>
#include <QFile>
#include <QTextEdit>
#include <QTextStream>
#include <KAboutData>
#include <KActionCollection>
#include <KLocalizedString>
#include <KPluginFactory>
K_PLUGIN_CLASS_WITH_JSON(NotepadPart, "notepad.json")
NotepadPart::NotepadPart(QWidget *parentWidget, QObject *parent, const KPluginMetaData &metaData)
: KParts::ReadWritePart(parent, metaData)
{
m_edit = new QTextEdit(parentWidget);
m_edit->setPlainText(QStringLiteral("NotepadPart's multiline edit"));
setWidget(m_edit);
QAction *searchReplace = new QAction(QStringLiteral("Search and replace"), this);
actionCollection()->addAction(QStringLiteral("searchreplace"), searchReplace);
setXMLFile(QStringLiteral("notepadpart.rc")); // will be found in the qrc resource
setReadWrite(true);
}
NotepadPart::~NotepadPart()
{
}
void NotepadPart::setReadWrite(bool rw)
{
m_edit->setReadOnly(!rw);
if (rw) {
connect(m_edit, &QTextEdit::textChanged, this, qOverload<>(&KParts::ReadWritePart::setModified));
} else {
disconnect(m_edit, &QTextEdit::textChanged, this, qOverload<>(&KParts::ReadWritePart::setModified));
}
ReadWritePart::setReadWrite(rw);
}
bool NotepadPart::openFile()
{
// qDebug() << "NotepadPart: opening " << localFilePath();
QFile f(localFilePath());
QString s;
if (f.open(QIODevice::ReadOnly)) {
QTextStream t(&f);
// The default with Qt6 is UTF-8
s = t.readAll();
f.close();
}
m_edit->setPlainText(s);
Q_EMIT setStatusBarText(url().toString());
return true;
}
bool NotepadPart::saveFile()
{
if (!isReadWrite()) {
return false;
}
QFile f(localFilePath());
if (f.open(QIODevice::WriteOnly)) {
QTextStream t(&f);
t << m_edit->toPlainText();
f.close();
return true;
} else {
return false;
}
}
#include "moc_notepad.cpp"
#include "notepad.moc"
@@ -0,0 +1,3 @@
[Desktop Entry]
Name=Notepad (example)
Type=Service
@@ -0,0 +1,35 @@
/*
SPDX-FileCopyrightText: 1999, 2000 David Faure <faure@kde.org>
SPDX-FileCopyrightText: 1999, 2000 Simon Hausmann <hausmann@kde.org>
SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#pragma once
#include <kparts/readwritepart.h>
class KPluginMetaData;
class QTextEdit;
/**
* Who said writing a part should be complex ? :-)
* Here is a very simple kedit-like part
* @internal
*/
class NotepadPart : public KParts::ReadWritePart
{
Q_OBJECT
public:
NotepadPart(QWidget *parentWidget, QObject *parent, const KPluginMetaData &metaData);
~NotepadPart() override;
void setReadWrite(bool rw) override;
protected:
bool openFile() override;
bool saveFile() override;
protected:
QTextEdit *m_edit;
};
@@ -0,0 +1,29 @@
{
"KPlugin": {
"MimeTypes": [
"text/english",
"text/plain",
"text/x-makefile",
"text/x-c++hdr",
"text/x-c++src",
"text/x-chdr",
"text/x-csrc",
"text/x-java",
"text/x-moc",
"text/x-pascal",
"text/x-tcl",
"text/x-tex",
"application/x-shellscript",
"text/x-c",
"text/x-c++"
],
"Name": "Notepad (example)"
},
"KParts": {
"InitialPreference": 9,
"Capabilities": [
"BrowserView",
"ReadWrite"
]
}
}
@@ -0,0 +1,7 @@
<!DOCTYPE RCC>
<RCC version="1.0">
<qresource prefix="/kxmlgui5/notepadpart">
<file>notepadpart.rc</file>
</qresource>
</RCC>
@@ -0,0 +1,13 @@
<!DOCTYPE gui SYSTEM "kpartgui.dtd">
<gui name="NotepadPart" version="2">
<MenuBar>
<Menu name="file"><text>&amp;File</text>
<Menu name="NotepadSubMenu"><text>Notepad Stuff</text>
<Action name="searchreplace"/>
</Menu>
</Menu>
<Menu name="edit"><text>&amp;Edit</text>
<Action name="searchreplace"/>
</Menu>
</MenuBar>
</gui>
@@ -0,0 +1,112 @@
/*
SPDX-FileCopyrightText: 2020 David Faure <faure@kde.org>
SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "partloader.h"
#include <KParts/PartLoader>
#include <KParts/ReadOnlyPart>
#include <QTest>
#include <KPluginMetaData>
#include <QDebug>
#include <QJsonArray>
#include <QStandardPaths>
class PartLoaderTest : public QObject
{
Q_OBJECT
private:
const QString m_plainTextMimetype = QStringLiteral("text/plain");
private Q_SLOTS:
void initTestCase()
{
QStandardPaths::setTestModeEnabled(true);
// Ensure notepadpart is preferred over other installed parts.
// This also tests the mimeapps.list parsing in PartLoader
const QByteArray contents =
"[Added KDE Service Associations]\n"
"text/plain=notepad.desktop;\n";
const QString configDir = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
QDir().mkpath(configDir);
const QString mimeAppsPath = configDir + QLatin1String("/mimeapps.list");
QFile mimeAppsFile(mimeAppsPath);
QVERIFY(mimeAppsFile.open(QIODevice::WriteOnly));
mimeAppsFile.write(contents);
}
void shouldListParts()
{
const QList<KPluginMetaData> plugins = KParts::PartLoader::partsForMimeType(m_plainTextMimetype);
QVERIFY(!plugins.isEmpty());
QCOMPARE(plugins.at(0).pluginId(), QStringLiteral("notepadpart")); // basename of plugin
const QString fileName = plugins.at(0).fileName();
QVERIFY2(fileName.contains(QLatin1String("notepadpart")), qPrintable(fileName));
}
void shouldLoadPlainTextPart()
{
const QString testFile = QFINDTESTDATA("partloadertest.cpp");
QVERIFY(!testFile.isEmpty());
QWidget parentWidget;
auto res = KParts::PartLoader::instantiatePartForMimeType<KParts::ReadOnlyPart>(m_plainTextMimetype, &parentWidget, this);
QVERIFY(res);
QCOMPARE(res.errorString, QString());
QCOMPARE(res.plugin->metaObject()->className(), "NotepadPart");
QVERIFY(res.plugin->openUrl(QUrl::fromLocalFile(testFile)));
}
void shouldHandleNoPartError()
{
// can't use an unlikely mimetype here, okteta is associated with application/octet-stream :-)
const QString mimeType = QStringLiteral("does/not/exist");
QWidget parentWidget;
const KPluginFactory::Result result = KParts::PartLoader::instantiatePartForMimeType<KParts::ReadOnlyPart>(mimeType, &parentWidget, this);
QVERIFY2(!result, result ? result.plugin->metaObject()->className() : nullptr);
QCOMPARE(result.errorString, QStringLiteral("No part was found for mimeType does/not/exist"));
}
void shouldInstantiatePart()
{
const KPluginMetaData md(QStringLiteral("kf6/parts/notepadpart"));
QVERIFY(md.isValid());
QWidget parentWidget;
const KPluginFactory::Result result = KParts::PartLoader::instantiatePart<KParts::ReadOnlyPart>(md, &parentWidget, this);
QVERIFY(result);
QCOMPARE(result.plugin->metaObject()->className(), "NotepadPart");
}
void testPartCapabilities()
{
const KPluginMetaData md(QStringLiteral("kf6/parts/notepadpart"));
QVERIFY(md.isValid());
QCOMPARE(KParts::PartLoader::partCapabilities(md), KParts::PartCapability::BrowserView | KParts::PartCapability::ReadWrite);
}
void testPartCapabilitiesCompat()
{
const KPluginMetaData md(QStringLiteral("kf6/parts/notepadpart"));
QJsonObject obj = md.rawData();
QJsonObject kplugin = obj[QLatin1String("KPlugin")].toObject();
kplugin[QLatin1String("ServiceTypes")] = QJsonValue::fromVariant(QStringList{QStringLiteral("Browser/View")});
obj[QLatin1String("KPlugin")] = kplugin;
obj.remove(QLatin1String("KParts"));
const auto caps = KParts::PartLoader::partCapabilities(KPluginMetaData(obj, md.fileName()));
QCOMPARE(caps, QFlags(KParts::PartCapability::BrowserView));
}
};
QTEST_MAIN(PartLoaderTest)
#include "partloadertest.moc"
@@ -0,0 +1,278 @@
/*
SPDX-FileCopyrightText: 2007 David Faure <faure@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "parttest.h"
#include <qtest_widgets.h>
#include <KSharedConfig>
#include <QSignalSpy>
#include <QTest>
#include <QWidget>
#include <kparts/guiactivateevent.h>
#include <kparts/openurlarguments.h>
#include <kparts/readonlypart.h>
QTEST_MAIN(PartTest)
class TestPart : public KParts::ReadOnlyPart
{
public:
TestPart(QObject *parent, QWidget *parentWidget)
: KParts::ReadOnlyPart(parent)
, m_openFileCalled(false)
{
setWidget(new QWidget(parentWidget));
connect(this, &KParts::ReadOnlyPart::urlChanged, this, &TestPart::logUrlChanged);
}
bool openFileCalled() const
{
return m_openFileCalled;
}
void logUrlChanged(const QUrl &url)
{
qDebug() << "url changed: " << url;
}
bool m_guiActivationEventTriggered = false;
protected:
bool openFile() override
{
m_openFileCalled = true;
return true;
}
void guiActivateEvent(KParts::GUIActivateEvent * /*event*/) override
{
m_guiActivationEventTriggered = true;
}
private:
bool m_openFileCalled;
};
void PartTest::testAutoDeletePart()
{
KParts::Part *part = new TestPart(nullptr, nullptr);
QPointer<KParts::Part> partPointer(part);
delete part->widget();
QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
QVERIFY(partPointer.isNull());
}
void PartTest::testAutoDeleteWidget()
{
KParts::Part *part = new TestPart(nullptr, nullptr);
QPointer<KParts::Part> partPointer(part);
QPointer<QWidget> widgetPointer(part->widget());
delete part;
QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
QVERIFY(widgetPointer.isNull());
}
void PartTest::testNoAutoDeletePart()
{
KParts::Part *part = new TestPart(nullptr, nullptr);
part->setAutoDeletePart(false);
QPointer<KParts::Part> partPointer(part);
delete part->widget();
QVERIFY(part->widget() == nullptr);
QCOMPARE(static_cast<KParts::Part *>(partPointer), part);
delete part;
}
void PartTest::testNoAutoDeleteWidget()
{
KParts::Part *part = new TestPart(nullptr, nullptr);
part->setAutoDeleteWidget(false);
QWidget *widget = part->widget();
QVERIFY(widget);
QPointer<QWidget> widgetPointer(part->widget());
delete part;
QCOMPARE(static_cast<QWidget *>(widgetPointer), widget);
delete widget;
}
// There is no operator== in OpenUrlArguments because it's only useful in unit tests
static bool compareArgs(const KParts::OpenUrlArguments &arg1, const KParts::OpenUrlArguments &arg2)
{
return arg1.mimeType() == arg2.mimeType() && //
arg1.xOffset() == arg2.xOffset() && //
arg1.yOffset() == arg2.yOffset() && //
arg1.reload() == arg2.reload();
}
void PartTest::testOpenUrlArguments()
{
TestPart *part = new TestPart(nullptr, nullptr);
QVERIFY(part->closeUrl()); // nothing to do, no error
QVERIFY(part->arguments().mimeType().isEmpty());
KParts::OpenUrlArguments args;
args.setMimeType(QStringLiteral("application/xml"));
args.setXOffset(50);
args.setYOffset(10);
args.setReload(true);
part->setArguments(args);
QVERIFY(compareArgs(args, part->arguments()));
part->openUrl(QUrl::fromLocalFile(QFINDTESTDATA("parttest.cpp")));
QVERIFY(part->openFileCalled());
QVERIFY(compareArgs(args, part->arguments()));
// Explicit call to closeUrl: arguments are cleared
part->closeUrl();
QVERIFY(part->arguments().mimeType().isEmpty());
// Calling openUrl with local file: mimetype is determined
part->openUrl(QUrl::fromLocalFile(QFINDTESTDATA("parttest.cpp")));
QCOMPARE(part->arguments().mimeType(), QStringLiteral("text/x-c++src"));
// (for a remote url it would be determined during downloading)
delete part;
}
void PartTest::testAutomaticMimeType()
{
TestPart *part = new TestPart(nullptr, nullptr);
QVERIFY(part->closeUrl()); // nothing to do, no error
QVERIFY(part->arguments().mimeType().isEmpty());
// open a file, and test the detected mimetype
part->openUrl(QUrl::fromLocalFile(QFINDTESTDATA("notepad.desktop")));
QCOMPARE(part->arguments().mimeType(), QString::fromLatin1("application/x-desktop"));
// manually closing, no mimetype should be stored now
part->closeUrl();
QVERIFY(part->arguments().mimeType().isEmpty());
// open a new file, and test again its (autdetected) mimetype
part->openUrl(QUrl::fromLocalFile(QFINDTESTDATA("parttest.cpp")));
QCOMPARE(part->arguments().mimeType(), QStringLiteral("text/x-c++src"));
// open a new file, but without explicitly close the first
part->openUrl(QUrl::fromLocalFile(QFINDTESTDATA("notepad.desktop")));
// test again its (autdetected) mimetype
QCOMPARE(part->arguments().mimeType(), QString::fromLatin1("application/x-desktop"));
// open a new file, but manually forcing a mimetype
KParts::OpenUrlArguments args;
args.setMimeType(QStringLiteral("application/xml"));
part->setArguments(args);
QVERIFY(compareArgs(args, part->arguments()));
part->openUrl(QUrl::fromLocalFile(QFINDTESTDATA("parttest.cpp")));
QCOMPARE(part->arguments().mimeType(), QString::fromLatin1("application/xml"));
// clear the args and open a new file, reactivating the automatic mimetype detection again
part->setArguments(KParts::OpenUrlArguments());
part->openUrl(QUrl::fromLocalFile(QFINDTESTDATA("notepad.desktop")));
// test again its (autdetected) mimetype
QCOMPARE(part->arguments().mimeType(), QString::fromLatin1("application/x-desktop"));
delete part;
}
void PartTest::testEmptyUrlAfterCloseUrl()
{
TestPart *part = new TestPart(nullptr, nullptr);
QVERIFY(part->openUrl(QUrl::fromLocalFile(QFINDTESTDATA("notepad.desktop"))));
QSignalSpy spy(part, &KParts::ReadOnlyPart::urlChanged);
QVERIFY(part->openUrl(QUrl::fromLocalFile(QFINDTESTDATA("parttest.cpp"))));
QVERIFY(!part->url().isEmpty());
QCOMPARE(spy.count(), 1);
spy.clear();
QVERIFY(part->closeUrl());
QVERIFY(part->url().isEmpty());
QCOMPARE(spy.count(), 1);
delete part;
}
#include <KConfigGroup>
#include <KToggleToolBarAction>
#include <KToolBar>
#include <kparts/mainwindow.h>
class MyMainWindow : public KParts::MainWindow
{
public:
MyMainWindow()
: KParts::MainWindow()
{
tb = new KToolBar(this);
tb->setObjectName(QStringLiteral("testtbvisibility"));
}
// createGUI and saveAutoSaveSettings are protected, so the whole test is here:
void testToolbarVisibility()
{
QVERIFY(tb->isVisible());
TestPart *part = new TestPart(nullptr, nullptr);
// TODO define xml with a toolbar for the part
// and put some saved settings into qttestrc in order to test
// r347935+r348051, i.e. the fact that KParts::MainWindow::createGUI
// will apply the toolbar settings (and that they won't have been
// erased by the previous call to saveMainWindowSettings...)
this->createGUI(part);
QVERIFY(tb->isVisible());
this->saveAutoSaveSettings();
// Hide the toolbar using the action (so that setSettingsDirty is called, too)
KToggleToolBarAction action(tb, QString(), nullptr);
action.trigger();
QVERIFY(!tb->isVisible());
// Switch the active part, and check that
// the toolbar doesn't magically reappear,
// as it did when createGUI was calling applyMainWindowSettings
this->createGUI(nullptr);
QVERIFY(!tb->isVisible());
this->createGUI(part);
QVERIFY(!tb->isVisible());
// All ok, show it again so that test can be run again :)
action.trigger();
QVERIFY(tb->isVisible());
close();
}
private:
KToolBar *tb;
};
// A KParts::MainWindow unit test
void PartTest::testToolbarVisibility()
{
// The bug was: hide a toolbar in konqueror,
// then switch tabs -> the toolbar shows again
// (unless you waited for the autosave timer to kick in)
MyMainWindow window;
KConfigGroup cg(KSharedConfig::openConfig(), QStringLiteral("kxmlgui_unittest"));
window.setAutoSaveSettings(cg.name());
window.show();
window.testToolbarVisibility();
}
void PartTest::testShouldNotCrashAfterDelete()
{
TestPart *part = new TestPart(nullptr, nullptr);
QVERIFY(part->openUrl(QUrl::fromLocalFile(QFINDTESTDATA("notepad.desktop"))));
QVERIFY(part->openFileCalled());
delete part;
}
void PartTest::testActivationEvent()
{
TestPart *part = new TestPart(nullptr, nullptr);
QVERIFY(!part->m_guiActivationEventTriggered);
part->event(new QEvent(QEvent::MouseButtonPress));
QVERIFY(!part->m_guiActivationEventTriggered);
part->event(new KParts::GUIActivateEvent(true));
QVERIFY(part->m_guiActivationEventTriggered);
delete part;
}
#include "moc_parttest.cpp"
@@ -0,0 +1,30 @@
/*
SPDX-FileCopyrightText: 2007 David Faure <faure@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef PARTTEST_H
#define PARTTEST_H
#include <QObject>
class PartTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testAutoDeletePart();
void testAutoDeleteWidget();
void testNoAutoDeletePart();
void testNoAutoDeleteWidget();
void testOpenUrlArguments();
void testAutomaticMimeType();
void testEmptyUrlAfterCloseUrl();
void testToolbarVisibility();
void testShouldNotCrashAfterDelete();
void testActivationEvent();
};
#endif /* PARTTEST_H */