File indexing completed on 2024-05-05 04:40:10

0001 /*
0002     SPDX-FileCopyrightText: 2009 Andreas Pakulat <apaku@gmx.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KDEVPLATFORM_PLUGIN_OPENWITHPLUGIN_H
0008 #define KDEVPLATFORM_PLUGIN_OPENWITHPLUGIN_H
0009 
0010 #include <interfaces/iplugin.h>
0011 #include <QVariantList>
0012 
0013 #include <KService>
0014 
0015 #include "iopenwith.h"
0016 
0017 class QMimeType;
0018 
0019 namespace OpenWithUtils {
0020 class FileOpener
0021 {
0022 public:
0023     FileOpener() = default;
0024     FileOpener(const KService::Ptr& service);
0025     static FileOpener fromPartId(const QString& partId);
0026 
0027     static FileOpener fromConfigEntryValue(const QString& value);
0028     QString toConfigEntryValue() const;
0029 
0030     /**
0031      * @return whether this opener object is valid
0032      * @note Most member functions and operators require valid opener(s) as a precondition.
0033      */
0034     bool isValid() const;
0035 
0036     bool isPart() const;
0037     const QString& id() const;
0038 
0039     /**
0040      * @return non-null service pointer for a valid application opener
0041      * @pre !isPart()
0042      */
0043     const KService::Ptr& service() const;
0044 
0045 private:
0046     explicit FileOpener(bool isPart, const QString& id);
0047 
0048     bool m_isPart = false;
0049     QString m_id;
0050     KService::Ptr m_service; ///< a cached service pointer
0051 };
0052 
0053 bool operator==(const FileOpener&, const FileOpener&);
0054 } // namespace OpenWithUtils
0055 
0056 class OpenWithPlugin : public KDevelop::IPlugin, public KDevelop::IOpenWith
0057 {
0058     Q_OBJECT
0059     Q_INTERFACES( KDevelop::IOpenWith )
0060 public:
0061     OpenWithPlugin( QObject* parent, const QVariantList& args );
0062     ~OpenWithPlugin() override;
0063     KDevelop::ContextMenuExtension contextMenuExtension(KDevelop::Context* context, QWidget* parent) override;
0064 
0065 protected:
0066     void openFilesInternal( const QList<QUrl>& files ) override;
0067 
0068 private:
0069     void openApplication(const KService::Ptr& service);
0070     void openPart(const QString& pluginId, const QString& name);
0071     bool canOpenDefault() const;
0072     void openDefault() const;
0073     void delegateToParts(const QString& pluginId) const;
0074     void delegateToExternalApplication(const KService::Ptr& service) const;
0075     void rememberDefaultChoice(const OpenWithUtils::FileOpener& opener, const QString& name);
0076 
0077     /**
0078      * Update @a m_mimeType and @a m_defaultOpener based on @a m_urls.
0079      *
0080      * @return the updated MIME type
0081      */
0082     QMimeType updateMimeTypeForUrls();
0083 
0084     QList<QAction*> actionsForParts(QWidget* parent);
0085     QList<QAction*> actionsForApplications(QWidget* parent);
0086     QList<QUrl> m_urls;
0087     QString m_mimeType;
0088     OpenWithUtils::FileOpener m_defaultOpener;
0089 };
0090 
0091 #endif // KDEVPLATFORM_PLUGIN_OPENWITHPLUGIN_H