MauiKit File Browsing

openwithmodel.cpp
1#include "openwithmodel.h"
2
3#ifdef KIO_AVAILABLE
4#include <KApplicationTrader>
5#include <KFileItem>
6#include <KIO/ApplicationLauncherJob>
7#include <KService>
8#endif
9
10OpenWithModel::OpenWithModel(QObject* parent) : MauiList(parent)
11{
12}
13
14void OpenWithModel::componentComplete()
15{
16 this->setList();
17 connect(this, &OpenWithModel::urlsChanged, this, &OpenWithModel::setList);
18}
19
20const FMH::MODEL_LIST &OpenWithModel::items() const
21{
22 return m_list;
23}
24
25void OpenWithModel::setUrls(const QStringList& urls)
26{
27 m_urls = urls;
28 Q_EMIT urlsChanged();
29}
30
32{
33 return m_urls;
34}
35
36void OpenWithModel::openWith(const int& index)
37{
38 if(index < 0 && index >= m_list.count())
39 {
40 return;
41 }
42#ifdef KIO_AVAILABLE
43 KService::Ptr service(new KService(this->m_list[index][FMH::MODEL_KEY::EXECUTABLE]));
44 auto *job = new KIO::ApplicationLauncherJob(service, this);
45 job->setUrls(QUrl::fromStringList(m_urls));
46 job->start();
47#endif
48}
49
50#ifdef KIO_AVAILABLE
51static FMH::MODEL createActionItem(const QExplicitlySharedDataPointer< KService > &service)
52{
53 FMH::MODEL map
54 {
55 {FMH::MODEL_KEY::LABEL, service->name().replace(QStringLiteral("&"), QStringLiteral("&&"))},
56 {FMH::MODEL_KEY::ICON, service->icon()},
57 {FMH::MODEL_KEY::COMMENT, service->comment()},
58 {FMH::MODEL_KEY::ID, QStringLiteral("_kicker_fileItem_openWith")},
59 {FMH::MODEL_KEY::EXECUTABLE, service->entryPath()}
60 // item["serviceExec"] = service->exec();
61 };
62
63 return map;
64}
65#endif
66
67void OpenWithModel::setList()
68{
69 if(m_urls.isEmpty())
70 {
71 return;
72 }
73
74 QUrl url = QUrl::fromUserInput(m_urls.first());
75 if(url.isEmpty() || !url.isValid())
76 {
77 return;
78 }
79
80 m_list.clear();
81 Q_EMIT this->preListChanged();
82
83#ifdef KIO_AVAILABLE
84 KFileItem fileItem(url);
85
86 const auto services = KApplicationTrader::queryByMimeType(fileItem.mimetype());
87 for (const auto &service : services) {
88 m_list << createActionItem(service);
89 }
90
92#endif
93}
void preListChanged()
void postListChanged()
void openWith(const int &index)
Launch a service at the given index with the URLs provided.
QStringList urls
The list of file URLs.
KSERVICE_EXPORT KService::List queryByMimeType(const QString &mimeType, FilterFunc filterFunc={})
void clear()
qsizetype count() const const
T & first()
bool isEmpty() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
QList< QUrl > fromStringList(const QStringList &urls, ParsingMode mode)
QUrl fromUserInput(const QString &userInput, const QString &workingDirectory, UserInputResolutionOptions options)
bool isEmpty() const const
bool isValid() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:49:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.