Commit 6c61d701 authored by Nicolas Fella's avatar Nicolas Fella
Browse files

Port away from KMimeTypeTrader

It is about to be deprecated

See https://phabricator.kde.org/T12177
parent 2a6aa0a6
Pipeline #254042 passed with stage
in 2 minutes and 20 seconds
......@@ -23,6 +23,8 @@
#include <KPluginMetaData>
#include <KXMLGUIFactory>
#include <kparts_version.h>
#include <QFile>
#include <QMimeDatabase>
#include <QProgressDialog>
......@@ -85,7 +87,7 @@ void ArkViewer::openExternalViewer(const KService::Ptr viewer, const QString& fi
job->start();
}
void ArkViewer::openInternalViewer(const KService::Ptr viewer, const QString& fileName, const QString& entryPath, const QMimeType& mimeType)
void ArkViewer::openInternalViewer(const KPluginMetaData& viewer, const QString& fileName, const QString& entryPath, const QMimeType& mimeType)
{
qCDebug(ARK) << "Opening internal viewer";
......@@ -143,10 +145,10 @@ void ArkViewer::view(const QString& fileName, const QString& entryPath)
QMimeType mimeType = db.mimeTypeForFile(fileName);
qCDebug(ARK) << "viewing" << fileName << "from" << entryPath << "with mime type:" << mimeType.name();
const KService::Ptr internalViewer = ArkViewer::getInternalViewer(mimeType.name());
const std::optional<KPluginMetaData> internalViewer = ArkViewer::getInternalViewer(mimeType.name());
if (internalViewer) {
openInternalViewer(internalViewer, fileName, entryPath, mimeType);
openInternalViewer(*internalViewer, fileName, entryPath, mimeType);
return;
}
......@@ -160,22 +162,60 @@ void ArkViewer::view(const QString& fileName, const QString& entryPath)
// No internal or external viewer available for the file. Ask the user if it
// should be previewed as text/plain.
if (askViewAsPlainText(mimeType)) {
const KService::Ptr textViewer = ArkViewer::getInternalViewer(QStringLiteral("text/plain"));
openInternalViewer(textViewer, fileName, entryPath, db.mimeTypeForName(QStringLiteral("text/plain")));
const std::optional<KPluginMetaData> textViewer = ArkViewer::getInternalViewer(QStringLiteral("text/plain"));
if (textViewer) {
openInternalViewer(*textViewer, fileName, entryPath, db.mimeTypeForName(QStringLiteral("text/plain")));
return;
}
}
qCDebug(ARK) << "Removing temporary file:" << fileName;
QFile::remove(fileName);
}
#if KPARTS_VERSION < QT_VERSION_CHECK(5, 100, 0)
// Copied from kparts/partloader.h
namespace KParts::PartLoader {
template<typename T>
static KPluginFactory::Result<T>
instantiatePart(const KPluginMetaData &data, QWidget *parentWidget = nullptr, QObject *parent = nullptr, const QVariantList &args = {})
{
KPluginFactory::Result<T> result;
KPluginFactory::Result<KPluginFactory> factoryResult = KPluginFactory::loadFactory(data);
if (!factoryResult.plugin) {
result.errorString = factoryResult.errorString;
result.errorReason = factoryResult.errorReason;
return result;
}
T *instance = factoryResult.plugin->create<T>(parentWidget, parent, args);
if (!instance) {
const QString fileName = data.fileName();
result.errorString = QObject::tr("KPluginFactory could not load the plugin: %1").arg(fileName);
result.errorText = QStringLiteral("KPluginFactory could not load the plugin: %1").arg(fileName);
result.errorReason = KPluginFactory::INVALID_KPLUGINFACTORY_INSTANTIATION;
} else {
qCDebug(ARK) << "Removing temporary file:" << fileName;
QFile::remove(fileName);
result.plugin = instance;
}
return result;
}
}
#endif
bool ArkViewer::viewInInternalViewer(const KService::Ptr viewer, const QString& fileName, const QString& entryPath, const QMimeType &mimeType)
bool ArkViewer::viewInInternalViewer(const KPluginMetaData& viewer, const QString& fileName, const QString& entryPath, const QMimeType &mimeType)
{
// Set icon and comment for the mimetype.
m_iconLabel->setPixmap(QIcon::fromTheme(mimeType.iconName()).pixmap(style()->pixelMetric(QStyle::PixelMetric::PM_SmallIconSize)));
m_commentLabel->setText(mimeType.comment());
// Create the ReadOnlyPart instance.
m_part = viewer->createInstance<KParts::ReadOnlyPart>(this, this);
const auto result = KParts::PartLoader::instantiatePart<KParts::ReadOnlyPart>(viewer, this, this);
if (!result) {
qCDebug(ARK) << "Failed to create internal viewer" << result.errorString;
return false;
}
m_part = result.plugin;
if (!m_part.data()) {
return false;
......@@ -209,27 +249,21 @@ bool ArkViewer::viewInInternalViewer(const KService::Ptr viewer, const QString&
KService::Ptr ArkViewer::getExternalViewer(const QString &mimeType)
{
KService::List offers = KApplicationTrader::queryByMimeType(mimeType);
if (!offers.isEmpty()) {
return offers.first();
} else {
return KService::Ptr();
}
return KApplicationTrader::preferredService(mimeType);
}
KService::Ptr ArkViewer::getInternalViewer(const QString& mimeType)
std::optional<KPluginMetaData> ArkViewer::getInternalViewer(const QString& mimeType)
{
// No point in even trying to find anything for application/octet-stream
if (mimeType == QLatin1String("application/octet-stream")) {
return KService::Ptr();
return {};
}
// Try to get a read-only kpart for the internal viewer
KService::List offers = KMimeTypeTrader::self()->query(mimeType, QStringLiteral("KParts/ReadOnlyPart"));
QVector<KPluginMetaData> offers = KParts::PartLoader::partsForMimeType(mimeType);
auto arkPartIt = std::find_if(offers.begin(), offers.end(), [](KService::Ptr service) {
return service->storageId() == QLatin1String("ark_part.desktop");
auto arkPartIt = std::find_if(offers.begin(), offers.end(), [](const KPluginMetaData& service) {
return service.pluginId() == QLatin1String("arkpart");
});
// Use the Ark part only when the mime type matches an archive type directly.
......@@ -237,14 +271,14 @@ KService::Ptr ArkViewer::getInternalViewer(const QString& mimeType)
// but browsing their internals is typically not what the user wants.
if (arkPartIt != offers.end()) {
// Not using hasMimeType() as we're explicitly not interested in inheritance.
if (!(*arkPartIt)->mimeTypes().contains(mimeType)) {
if (!arkPartIt->mimeTypes().contains(mimeType)) {
offers.erase(arkPartIt);
}
}
// Skip the KHTML part
auto khtmlPart = std::find_if(offers.begin(), offers.end(), [](KService::Ptr service) {
return service->desktopEntryName() == QLatin1String("khtml");
auto khtmlPart = std::find_if(offers.begin(), offers.end(), [](const KPluginMetaData &part) {
return part.pluginId() == QLatin1String("khtmlpart");
});
if (khtmlPart != offers.end()) {
......@@ -254,8 +288,8 @@ KService::Ptr ArkViewer::getInternalViewer(const QString& mimeType)
// The oktetapart can open any file, but a hex viewer isn't really useful here
// Skip it so we prefer an external viewer instead
auto oktetaPart = std::find_if(offers.begin(), offers.end(), [](KService::Ptr service) {
return service->desktopEntryName() == QLatin1String("oktetapart");
auto oktetaPart = std::find_if(offers.begin(), offers.end(), [](const KPluginMetaData &part) {
return part.pluginId() == QLatin1String("oktetapart");
});
if (oktetaPart != offers.end()) {
......@@ -263,9 +297,9 @@ KService::Ptr ArkViewer::getInternalViewer(const QString& mimeType)
}
if (!offers.isEmpty()) {
return offers.first();
return offers.at(0);
} else {
return KService::Ptr();
return {};
}
}
......
......@@ -18,6 +18,8 @@
#include <QMimeType>
#include <QPointer>
#include <optional>
class ArkViewer : public KParts::MainWindow, public Ui::ArkViewer
{
Q_OBJECT
......@@ -31,14 +33,15 @@ private:
explicit ArkViewer();
static KService::Ptr getExternalViewer(const QString& mimeType);
static KService::Ptr getInternalViewer(const QString& mimeType);
static std::optional<KPluginMetaData> getInternalViewer(const QString& mimeType);
static void openExternalViewer(const KService::Ptr viewer, const QString& fileName);
static void openInternalViewer(const KService::Ptr viewer, const QString& fileName, const QString& entryPath, const QMimeType& mimeType);
static void openInternalViewer(const KPluginMetaData& viewer, const QString& fileName, const QString& entryPath, const QMimeType& mimeType);
static bool askViewAsPlainText(const QMimeType& mimeType);
bool viewInInternalViewer(const KService::Ptr viewer, const QString& fileName, const QString &entryPath, const QMimeType& mimeType);
bool viewInInternalViewer(const KPluginMetaData& viewer, const QString& fileName, const QString& entryPath, const QMimeType& mimeType);
private Q_SLOTS:
void aboutKPart();
......
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