File indexing completed on 2024-04-28 16:44:26

0001 /*  This file is part of the KDE project
0002     SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "kserviceselectdlg.h"
0008 #include "kservicelistwidget.h"
0009 
0010 #include <QDialogButtonBox>
0011 #include <QLabel>
0012 #include <QVBoxLayout>
0013 
0014 #include <KLocalizedString>
0015 #include <KMimeTypeTrader>
0016 #include <kplugininfo.h>
0017 
0018 #if KSERVICE_BUILD_DEPRECATED_SINCE(5, 102)
0019 KService::List allParts()
0020 {
0021     KService::List result;
0022     const KService::List allServices = KService::allServices();
0023     for (const auto &service : allServices) {
0024         if (service->hasServiceType(QStringLiteral("KParts/ReadOnlyPart"))) {
0025             result << service;
0026         }
0027     }
0028     return result;
0029 }
0030 #else
0031 QVector<KPluginMetaData> allParts()
0032 {
0033     return KPluginMetaData::findPlugins(QStringLiteral("kf" QT_STRINGIFY(QT_VERSION_MAJOR) "/parts"));
0034 }
0035 #endif
0036 
0037 KServiceSelectDlg::KServiceSelectDlg(const QString & /*serviceType*/, const QString & /*value*/, QWidget *parent)
0038     : QDialog(parent)
0039 {
0040     setObjectName(QLatin1String("serviceSelectDlg"));
0041     setModal(true);
0042     setWindowTitle(i18n("Add Service"));
0043 
0044     QVBoxLayout *layout = new QVBoxLayout(this);
0045 
0046     layout->addWidget(new QLabel(i18n("Select service:")));
0047     m_listbox = new QListWidget();
0048     m_buttonBox = new QDialogButtonBox;
0049     m_buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0050 
0051     const auto parts = allParts();
0052 
0053     for (const auto &part : parts) {
0054 #if KSERVICE_BUILD_DEPRECATED_SINCE(5, 102)
0055         m_listbox->addItem(new KServiceListItem(part, KServiceListWidget::SERVICELIST_SERVICES));
0056 #else
0057         m_listbox->addItem(new PluginListItem(part));
0058 #endif
0059     }
0060 
0061     m_listbox->model()->sort(0);
0062     m_listbox->setMinimumHeight(350);
0063     m_listbox->setMinimumWidth(400);
0064     layout->addWidget(m_listbox);
0065     layout->addWidget(m_buttonBox);
0066     connect(m_listbox, &QListWidget::itemDoubleClicked, this, &QDialog::accept);
0067     connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0068     connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0069 }
0070 
0071 KServiceSelectDlg::~KServiceSelectDlg()
0072 {
0073 }
0074 
0075 KService::Ptr KServiceSelectDlg::service()
0076 {
0077     int selIndex = m_listbox->currentRow();
0078     KServiceListItem *selItem = static_cast<KServiceListItem *>(m_listbox->item(selIndex));
0079     return KService::serviceByDesktopPath(selItem->desktopPath);
0080 }