1 | /* |
2 | This file is part of the KDE project |
3 | SPDX-FileCopyrightText: 2007 David Faure <faure@kde.org> |
4 | |
5 | SPDX-License-Identifier: LGPL-2.0-or-later |
6 | */ |
7 | |
8 | #include "kserviceaction.h" |
9 | #include "kservice.h" |
10 | #include "ksycoca.h" |
11 | #include "ksycoca_p.h" |
12 | |
13 | #include <QDataStream> |
14 | #include <QVariant> |
15 | |
16 | #include <KConfigGroup> |
17 | |
18 | class KServiceActionPrivate : public QSharedData |
19 | { |
20 | public: |
21 | KServiceActionPrivate(const QString &name, const QString &text, const QString &icon, const QString &exec, bool noDisplay) |
22 | : m_name(name) |
23 | , m_text(text) |
24 | , m_icon(icon) |
25 | , m_exec(exec) |
26 | , m_noDisplay(noDisplay) |
27 | { |
28 | } |
29 | QString m_name; |
30 | QString m_text; |
31 | QString m_icon; |
32 | QString m_exec; |
33 | QVariant m_data; |
34 | bool m_noDisplay; |
35 | KServicePtr m_service; |
36 | // warning keep QDataStream operators in sync if adding data here |
37 | }; |
38 | |
39 | KServiceAction::KServiceAction() |
40 | : d(new KServiceActionPrivate(QString(), QString(), QString(), QString(), false)) |
41 | { |
42 | } |
43 | |
44 | KServiceAction::KServiceAction(const QString &name, const QString &text, const QString &icon, const QString &exec, bool noDisplay, const KServicePtr &service) |
45 | : d(new KServiceActionPrivate(name, text, icon, exec, noDisplay)) |
46 | { |
47 | d->m_service = service; |
48 | } |
49 | |
50 | KServiceAction::~KServiceAction() |
51 | { |
52 | } |
53 | |
54 | KServiceAction::KServiceAction(const KServiceAction &other) |
55 | : d(other.d) |
56 | { |
57 | } |
58 | |
59 | KServiceAction &KServiceAction::operator=(const KServiceAction &other) |
60 | { |
61 | d = other.d; |
62 | return *this; |
63 | } |
64 | |
65 | QVariant KServiceAction::data() const |
66 | { |
67 | return d->m_data; |
68 | } |
69 | |
70 | void KServiceAction::setData(const QVariant &data) |
71 | { |
72 | d->m_data = data; |
73 | } |
74 | |
75 | QString KServiceAction::name() const |
76 | { |
77 | return d->m_name; |
78 | } |
79 | |
80 | QString KServiceAction::text() const |
81 | { |
82 | return d->m_text; |
83 | } |
84 | |
85 | QString KServiceAction::icon() const |
86 | { |
87 | return d->m_icon; |
88 | } |
89 | |
90 | QString KServiceAction::exec() const |
91 | { |
92 | return d->m_exec; |
93 | } |
94 | |
95 | bool KServiceAction::noDisplay() const |
96 | { |
97 | return d->m_noDisplay; |
98 | } |
99 | |
100 | bool KServiceAction::isSeparator() const |
101 | { |
102 | return d->m_name == QLatin1String("_SEPARATOR_" ); |
103 | } |
104 | |
105 | KServicePtr KServiceAction::service() const |
106 | { |
107 | return d->m_service; |
108 | } |
109 | |
110 | void KServiceAction::setService(const KServicePtr &service) |
111 | { |
112 | d->m_service = service; |
113 | } |
114 | |
115 | QDataStream &operator>>(QDataStream &str, KServiceAction &act) |
116 | { |
117 | KServiceActionPrivate *d = act.d; |
118 | str >> d->m_name; |
119 | str >> d->m_text; |
120 | str >> d->m_icon; |
121 | str >> d->m_exec; |
122 | str >> d->m_data; |
123 | str >> d->m_noDisplay; |
124 | return str; |
125 | } |
126 | |
127 | QDataStream &operator<<(QDataStream &str, const KServiceAction &act) |
128 | { |
129 | const KServiceActionPrivate *d = act.d; |
130 | str << d->m_name; |
131 | str << d->m_text; |
132 | str << d->m_icon; |
133 | str << d->m_exec; |
134 | str << d->m_data; |
135 | str << d->m_noDisplay; |
136 | return str; |
137 | } |
138 | |
139 | QVariant KServiceAction::property(const QString &_name, QMetaType::Type type) const |
140 | { |
141 | const auto dataMap = d->m_data.toMap(); |
142 | auto it = dataMap.constFind(key: _name); |
143 | if (it == dataMap.cend() || !it.value().isValid()) { |
144 | return QVariant(); // No property set. |
145 | } |
146 | |
147 | if (type == QMetaType::QString) { |
148 | return it.value(); // no conversion necessary |
149 | } else { |
150 | // All others |
151 | // For instance properties defined as StringList, like MimeTypes. |
152 | // XXX This API is accessible only through a friend declaration. |
153 | return KConfigGroup::convertToQVariant(pKey: _name.toUtf8().constData(), value: it.value().toString().toUtf8(), aDefault: QVariant(QMetaType(type))); |
154 | } |
155 | } |
156 | |