Warning, file /maui/mauikit-filebrowsing/src/code/openwithmodel.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 #include "openwithmodel.h"
0002 
0003 #ifdef KIO_AVAILABLE
0004 #include <KApplicationTrader>
0005 #include <KFileItem>
0006 #include <KIO/ApplicationLauncherJob>
0007 #include <KService>
0008 #endif
0009 
0010 OpenWithModel::OpenWithModel(QObject* parent) : MauiList(parent)
0011 {
0012 }
0013 
0014 void OpenWithModel::componentComplete()
0015 {
0016     this->setList();
0017     connect(this, &OpenWithModel::urlsChanged, this, &OpenWithModel::setList);
0018 }
0019 
0020 const FMH::MODEL_LIST &OpenWithModel::items() const
0021 {
0022     return m_list;
0023 }
0024 
0025 void OpenWithModel::setUrls(const QStringList& urls)
0026 {
0027     m_urls = urls;
0028     Q_EMIT urlsChanged();
0029 }
0030 
0031 QStringList OpenWithModel::urls() const
0032 {
0033     return m_urls;
0034 }
0035 
0036 void OpenWithModel::openWith(const int& index)
0037 {
0038     if(index < 0 && index >= m_list.count())
0039     {
0040         return;
0041     }
0042 #ifdef KIO_AVAILABLE
0043     KService::Ptr service(new KService(this->m_list[index][FMH::MODEL_KEY::EXECUTABLE]));
0044     auto *job = new KIO::ApplicationLauncherJob(service, this);
0045     job->setUrls(QUrl::fromStringList(m_urls));
0046     job->start();
0047 #endif
0048 }
0049 
0050 #ifdef KIO_AVAILABLE
0051 static FMH::MODEL createActionItem(const QExplicitlySharedDataPointer< KService > &service)
0052 {
0053     FMH::MODEL map
0054     {
0055         {FMH::MODEL_KEY::LABEL, service->name().replace(QStringLiteral("&"), QStringLiteral("&&"))},
0056         {FMH::MODEL_KEY::ICON, service->icon()},
0057         {FMH::MODEL_KEY::COMMENT, service->comment()},
0058         {FMH::MODEL_KEY::ID, QStringLiteral("_kicker_fileItem_openWith")},
0059         {FMH::MODEL_KEY::EXECUTABLE, service->entryPath()}
0060         //         item["serviceExec"] = service->exec();
0061     };
0062     
0063     return map;
0064 }
0065 #endif
0066 
0067 void OpenWithModel::setList()
0068 {
0069     if(m_urls.isEmpty())
0070     {
0071         return;
0072     }
0073     
0074     QUrl url = QUrl::fromUserInput(m_urls.first());
0075     if(url.isEmpty() || !url.isValid())
0076     {
0077         return;
0078     }
0079     
0080     m_list.clear();
0081     Q_EMIT this->preListChanged();
0082 
0083 #ifdef KIO_AVAILABLE
0084     KFileItem fileItem(url);
0085     
0086     const auto services = KApplicationTrader::queryByMimeType(fileItem.mimetype());
0087     for (const auto &service : services) {
0088         m_list << createActionItem(service);
0089     }
0090     
0091     Q_EMIT postListChanged();
0092 #endif
0093 }