File indexing completed on 2024-12-08 03:40:45

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 1998-2009 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #ifndef KFILEITEMACTIONS_P_H
0009 #define KFILEITEMACTIONS_P_H
0010 
0011 #include "kabstractfileitemactionplugin.h"
0012 #include <KConfig>
0013 #include <KDesktopFileAction>
0014 #include <KService>
0015 #include <kfileitem.h>
0016 #include <kfileitemlistproperties.h>
0017 
0018 #include <QActionGroup>
0019 #include <QObject>
0020 
0021 class KFileItemActions;
0022 
0023 typedef QList<KDesktopFileAction> ServiceList;
0024 
0025 class KFileItemActionsPrivate : public QObject
0026 {
0027     Q_OBJECT
0028     friend class KFileItemActions;
0029 
0030 public:
0031     explicit KFileItemActionsPrivate(KFileItemActions *qq);
0032     ~KFileItemActionsPrivate() override;
0033 
0034     int insertServicesSubmenus(const QMap<QString, ServiceList> &list, QMenu *menu);
0035     int insertServices(const ServiceList &list, QMenu *menu);
0036 
0037     // For "open with"
0038     KService::List associatedApplications(const QString &traderConstraint);
0039     QAction *createAppAction(const KService::Ptr &service, bool singleOffer);
0040 
0041     struct ServiceRank {
0042         int score;
0043         KService::Ptr service;
0044     };
0045 
0046     // Inline function for sorting lists of ServiceRank
0047     static bool lessRank(const ServiceRank &id1, const ServiceRank &id2)
0048     {
0049         return id1.score < id2.score;
0050     }
0051 
0052     QStringList listMimeTypes(const KFileItemList &items);
0053     QStringList listPreferredServiceIds(const QStringList &mimeTypeList, const QStringList &excludedDesktopEntryNames, const QString &traderConstraint);
0054 
0055     struct ServiceActionInfo {
0056         int userItemCount = 0;
0057         QMenu *menu = nullptr;
0058     };
0059     ServiceActionInfo addServiceActionsTo(QMenu *mainMenu, const QList<QAction *> &additionalActions, const QStringList &excludeList);
0060     int addPluginActionsTo(QMenu *mainMenu, QMenu *actionsMenu, const QStringList &excludeList);
0061     void insertOpenWithActionsTo(QAction *before, QMenu *topMenu, const QStringList &excludedDesktopEntryNames, const QString &traderConstraint);
0062     static KService::List
0063     associatedApplications(const QStringList &mimeTypeList, const QString &traderConstraint, const QStringList &excludedDesktopEntryNames);
0064 
0065     QStringList serviceMenuFilePaths();
0066 
0067 public Q_SLOTS:
0068     void slotRunPreferredApplications();
0069 
0070 private:
0071     void openWithByMime(const KFileItemList &fileItems);
0072 
0073     // Utility function which returns true if the service menu should be displayed
0074     bool shouldDisplayServiceMenu(const KConfigGroup &cfg, const QString &protocol) const;
0075     // Utility functions which returns true if the types for the service are set and the exclude types are not contained
0076     bool checkTypesMatch(const KConfigGroup &cfg) const;
0077 
0078 private Q_SLOTS:
0079     // For servicemenus
0080     void slotExecuteService(QAction *act);
0081     // For "open with" applications
0082     void slotRunApplication(QAction *act);
0083     void slotOpenWithDialog();
0084 
0085 public:
0086     KFileItemActions *const q;
0087     KFileItemListProperties m_props;
0088     QStringList m_mimeTypeList;
0089     QString m_traderConstraint;
0090     KFileItemList m_fileOpenList;
0091     QActionGroup m_executeServiceActionGroup;
0092     QActionGroup m_runApplicationActionGroup;
0093     QWidget *m_parentWidget;
0094     KConfig m_config;
0095     QHash<QString, KAbstractFileItemActionPlugin *> m_loadedPlugins;
0096 };
0097 
0098 Q_DECLARE_METATYPE(KService::Ptr)
0099 Q_DECLARE_METATYPE(KServiceAction)
0100 
0101 #endif /* KFILEITEMACTIONS_P_H */