[education/rkward/kf5] rkward/agents: Port okular part handling to Qt6

Thomas Friedrichsmeier null at kde.org
Wed Apr 10 16:12:08 BST 2024


Git commit eca8e450433367131cfdb08a48eed6a305a99526 by Thomas Friedrichsmeier, on behalf of Carl Schwan.
Committed on 01/04/2024 at 11:45.
Pushed by tfry into branch 'kf5'.

Port okular part handling to Qt6

M  +27   -46   rkward/agents/rkprintagent.cpp

https://invent.kde.org/education/rkward/-/commit/eca8e450433367131cfdb08a48eed6a305a99526

diff --git a/rkward/agents/rkprintagent.cpp b/rkward/agents/rkprintagent.cpp
index 764d595d1..50ea72c42 100644
--- a/rkward/agents/rkprintagent.cpp
+++ b/rkward/agents/rkprintagent.cpp
@@ -12,24 +12,21 @@ SPDX-License-Identifier: GPL-2.0-or-later
 #include <QElapsedTimer>
 #include <QUrl>
 
-#include <kservice.h>
-#include <kmessagebox.h>
+#include <KMessageBox>
 #include <kio_version.h>
-#if KIO_VERSION >= QT_VERSION_CHECK(5, 77, 0)
-#	include <KIO/OpenUrlJob>
-#	include <KIO/JobUiDelegate>
-#else
-#	include <KRun>
-#endif
+#include <KIO/OpenUrlJob>
+#include <KIO/JobUiDelegate>
+#include <KIO/JobUiDelegateFactory>
 #include <KLocalizedString>
 #include <KPluginFactory>
-#include <KPluginLoader>
 #include <kcoreaddons_version.h>
 
 #include "../rkward.h"
 
 #include "../debug.h"
 
+#define OKULAR_LIBRARY_NAME "kf6/parts/okularpart"
+
 RKPrintAgent::RKPrintAgent(const QString &file, KParts::ReadOnlyPart *provider, bool delete_file) : QObject(), file(file), provider(provider), delete_file(delete_file) {
 	RK_TRACE (APP);
 	//provider->widget()->show(); // not very helpful as a preview, unfortunately
@@ -45,53 +42,37 @@ RKPrintAgent::~RKPrintAgent () {
 void RKPrintAgent::printPostscript (const QString &file, bool delete_file) {
 	RK_TRACE (APP)
 
-	KParts::ReadOnlyPart *provider = nullptr;
-	KService::Ptr service = KService::serviceByDesktopPath("okular_part.desktop");
-	if (!service) service = KService::serviceByDesktopPath("kpdf_part.desktop");
-	if (service) {
-#if KCOREADDONS_VERSION < QT_VERSION_CHECK(5,86,0)
-		auto factory = KPluginLoader(service->library()).factory();
-		if (factory) {
-			provider = factory->create<KParts::ReadOnlyPart>(nullptr);
-		}
-#else
-		provider = KPluginFactory::instantiatePlugin<KParts::ReadOnlyPart>(KPluginMetaData(QPluginLoader(service->library())), nullptr).plugin;
-#endif
-	} else {
-		RK_DEBUG (APP, DL_WARNING, "No KDE service found for postscript printing");
-	}
-
-	QAction *printaction = 0;
-	if (provider) {
-		printaction = provider->action ("print");
-		if (!printaction) printaction = provider->action ("file_print");
-		if (!printaction) {
-			QAction *a = new QAction (provider);
-			bool ok = connect (a, SIGNAL (triggered()), provider, SLOT (slotPrint()));
-			if (ok) printaction = a;
-		}
-		if (!(printaction && provider->openUrl (QUrl::fromLocalFile (file)))) {
-			RK_DEBUG (APP, DL_WARNING, "No print action in postscript provider");
-			delete provider;
-			provider = 0;
-		}
-	}
+    const KPluginMetaData okularPart(QStringLiteral(OKULAR_LIBRARY_NAME));
+    const QVariantList args {"ViewerWidget"};
+    auto result = KPluginFactory::instantiatePlugin<KParts::ReadOnlyPart>(okularPart, nullptr, args);
 
-	if (!provider) {
+    if(!result) {
 		RK_DEBUG (APP, DL_WARNING, "No valid postscript provider was found");
 		KMessageBox::error(RKWardMainWindow::getMain(), i18n("No service was found to provide a KDE print dialog for PostScript files. We will try to open a generic PostScript viewer (if any), instead.<br><br>Consider installing 'okular', or configure RKWard not to attempt to print using a KDE print dialog."), i18n("Unable to open KDE print dialog"));
+
 		// fallback: If we can't find a proper part, try to invoke a standalone PS reader, instead
-#if KIO_VERSION < QT_VERSION_CHECK(5, 77, 0)
-		KRun::runUrl (QUrl::fromLocalFile(file), "application/postscript", RKWardMainWindow::getMain(), KRun::RunFlags());
-#else
 		auto *job = new KIO::OpenUrlJob(QUrl::fromLocalFile(file), "application/postscript");
-		job->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, RKWardMainWindow::getMain()));
+		job->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, RKWardMainWindow::getMain()));
 		job->setDeleteTemporaryFile(delete_file);
 		job->start();
-#endif
 		return;
 	}
 
+	auto provider = result.plugin;
+	QAction *printaction = printaction = provider->action ("print");
+	if (!printaction) printaction = provider->action ("file_print");
+	if (!printaction) {
+		QAction *a = new QAction (provider);
+		bool ok = connect (a, SIGNAL (triggered()), provider, SLOT (slotPrint()));
+		if (ok) printaction = a;
+	}
+	if (!(printaction && provider->openUrl (QUrl::fromLocalFile (file)))) {
+		RK_DEBUG (APP, DL_WARNING, "No print action in postscript provider");
+		delete provider;
+		provider = nullptr;
+	}
+	
+
 	RKPrintAgent *agent = new RKPrintAgent(file, provider, delete_file);
 
 	// very hacky heuristic to try to find out, whether the print action is synchronous or asynchronous. If the latter, delete after half an hour. If the former delete after printing.



More information about the rkward-tracker mailing list