1 /* This file is part of the KDE project
2    Copyright (C) 1998, 1999, 2000 Torben Weis <weis@kde.org>
3    Copyright (C) 2001 David Faure <faure@kde.org>
4 
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public
7    License as published by the Free Software Foundation; either
8    version 2 of the License, or (at your option) any later version.
9 
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14 
15    You should have received a copy of the GNU Library General Public License
16    along with this library; see the file COPYING.LIB.  If not, write to
17    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18    Boston, MA 02110-1301, USA.
19 */
20 
21 #ifndef KDATATOOL_H
22 #define KDATATOOL_H
23 
24 #include <kdelibs4support_export.h>
25 #include <QObject>
26 #include <QString>
27 
28 #include <QAction>
29 
30 #include <kservice.h>
31 
32 class KDataTool;
33 class QPixmap;
34 class QStringList;
35 class KActionCollection;
36 
37 // If you're only looking at implementing a data-tool, skip directly to the last
38 // class definition, KDataTool.
39 
40 /**
41  * This is a convenience class for KService. You can use it if you have
42  * a KService describing a KDataTool. In this case the KDataToolInfo class
43  * is more convenient to work with.
44  *
45  * Especially useful is the method createTool which creates the datatool
46  * described by the service.
47  * @see KDataTool
48  */
49 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KDataToolInfo
50 {
51 public:
52     /**
53      * Create an invalid KDataToolInfo.
54      */
55     KDataToolInfo();
56     /**
57      * Create a valid KDataToolInfo.
58      * @param service the corresponding service
59      * @param componentName the name of the component to use
60      */
61     KDataToolInfo(const KService::Ptr &service, const QString &componentName);
62     /**
63      * Destructor
64      */
65     ~KDataToolInfo();
66     /**
67      * Copy constructor.
68      */
69     KDataToolInfo(const KDataToolInfo &info);
70     /**
71      * Assignment operator.
72      */
73     KDataToolInfo &operator= (const KDataToolInfo &info);
74 
75     /**
76      * Returns the data type that the DataTool can accept.
77      * @return the C++ data type that this DataTool accepts.
78      *         For example "QString" or "QImage" or something more
79      *         complicated.
80      */
81     QString dataType() const;
82     /**
83      * Returns a list of mime type that will be accepted by the DataTool.
84      * The mimetypes are only used if the dataType can be used to store
85      * different mimetypes. For example in a "QString" you could save "text/plain"
86      * or "text/html" or "text/xml".
87      *
88      * @return the mime types accepted by this DataTool. For example
89      *         "image/gif" or "text/plain". In some cases the dataType
90      *         determines the accepted type of data perfectly. In this cases
91      *         this list may be empty.
92      */
93     QStringList mimeTypes() const;
94 
95     /**
96      * Checks whether the DataTool is read-only.
97      * @return true if the DataTool does not modify the data passed to it by KDataTool::run.
98      */
99     bool isReadOnly() const;
100 
101     /**
102      * Returns the icon name for this DataTool.
103      * @return the name of the icon for the DataTool
104      */
105     QString iconName() const;
106     /**
107      * Returns a list of strings that you can put in a QPopupMenu item, for example to
108      * offer the DataTools services to the user. The returned value
109      * is usually something like "Spell checking", "Shrink Image", "Rotate Image"
110      * or something like that.
111      * This list comes from the Comment field of the tool's desktop file
112      * (so that it can be translated).
113      *
114      * Each of the strings returned corresponds to a string in the list returned by
115      * commands.
116      *
117      * @return a list of strings that you can put in a QPopupMenu item
118      */
119     QStringList userCommands() const;
120     /**
121      * Returns the list of commands the DataTool can execute. The application
122      * passes the command to the KDataTool::run method.
123      *
124      * This list comes from the Commands field of the tool's desktop file.
125      *
126      * Each of the strings returned corresponds to a string in the list returned by
127      * userCommands.
128      * @return the list of commands the DataTool can execute, suitable for
129      *         the KDataTool::run method.
130      */
131     QStringList commands() const;
132 
133     /**
134      * Creates the data tool described by this KDataToolInfo.
135      * @param parent the parent of the QObject (or 0 for parent-less KDataTools)
136      * @return a pointer to the created data tool or 0 on error.
137      */
138     KDataTool *createTool(QObject *parent = nullptr) const;
139 
140     /**
141      * The KDataToolInfo's service that is represented by this class.
142      * @return the service
143      */
144     KService::Ptr service() const;
145 
146     /**
147      * The instance of the service.
148      * @return the component name
149      */
150     QString componentName() const;
151 
152     /**
153      * A DataToolInfo may be invalid if the KService passed to its constructor does
154      * not feature the service type "KDataTool".
155      * @return true if valid, false otherwise
156      */
157     bool isValid() const;
158 
159     /**
160      * Queries the KServiceTypeTrader about installed KDataTool implementations.
161      * @param datatype a type that the application can 'export' to the tools (e.g. QString)
162      * @param mimetype the mimetype of the data (e.g. text/plain)
163      * @param componentName the application (or the part)'s instance (to check if a tool is excluded from this part,
164      * and also used if the tool wants to read its configuration in the app's config file).
165      * @return the list of results
166      */
167     static QList<KDataToolInfo> query(const QString &datatype, const QString &mimetype, const QString &componentName);
168 
169 private:
170     class KDataToolInfoPrivate;
171     KDataToolInfoPrivate *const d;
172 };
173 
174 /**
175  * This class helps applications implement support for KDataTool.
176  * The steps to follow are simple:
177  * @li query for the available tools using KDataToolInfo::query
178  * @li pass the result to KDataToolAction::dataToolActionList (with a slot)
179  * @li plug the resulting actions, either using KXMLGUIClient::plugActionList, or by hand.
180  *
181  * The slot defined for step 2 is called when the action is activated, and
182  * that's where the tool should be created and run.
183  */
184 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KDataToolAction : public QAction
185 {
186     Q_OBJECT
187 public:
188     /**
189      * Constructs a new KDataToolAction.
190      *
191      * @param text The text that will be displayed
192      * @param info The corresponding KDataToolInfo
193      * @param command The command of the action
194      * @param parent This action's parent
195      * @param name The name of the action
196      */
197     KDataToolAction(const QString &text, const KDataToolInfo &info, const QString &command, QObject *parent);
198 
199     /**
200      * Destructor
201      */
202     ~KDataToolAction() override;
203 
204     /**
205      * Creates a list of actions from a list of information about data-tools.
206      * The slot must have a signature corresponding to the toolActivated signal.
207      *
208      * Note that it's the caller's responsibility to delete the actions when they're not needed anymore.
209      * @param tools the list of data tool descriptions
210      * @param receiver the receiver for toolActivated() signals
211      * @param slot the slot that will receive the toolActivated() signals
212      * @param parent the parent action collection for the actions to be created
213      * @return the KActions
214      */
215     static QList<QAction *> dataToolActionList(const QList<KDataToolInfo> &tools, const QObject *receiver, const char *slot, KActionCollection *parent);
216 
217 Q_SIGNALS:
218     /**
219      * Emitted when a tool has been activated.
220      * @param info a description of the activated tools
221      * @param command the command for the tool
222      */
223     void toolActivated(const KDataToolInfo &info, const QString &command);
224 
225 protected:
226     virtual void slotActivated();
227 
228 private:
229     class KDataToolActionPrivate;
230     KDataToolActionPrivate *const d;
231 
232 };
233 
234 /**
235  * A generic tool that processes data.
236  *
237  * A data-tool is a "plugin" for an application, that acts (reads/modifies)
238  * on a portion of the data present in the document (e.g. a text document,
239  * a single word or paragraph, a KSpread cell, an image, etc.)
240  *
241  * The application has some generic code for presenting the tools in a popupmenu
242  * @see KDataToolAction, and for activating a tool, passing it the data
243  * (and possibly getting modified data from it).
244  */
245 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KDataTool : public QObject
246 {
247     Q_OBJECT
248 public:
249     /**
250      * Constructor
251      * The data-tool is only created when a menu-item, that relates to it, is activated.
252      * @param parent the parent of the QObject (or 0 for parent-less KDataTools)
253      */
254     KDataTool(QObject *parent = nullptr);
255 
256     /**
257      * Destructor
258      */
259     ~KDataTool() override;
260 
261     /**
262      * @internal. Do not use under any circumstance (including bad weather).
263      */
264     void setComponentName(const QString &componentName);
265 
266     /**
267      * Returns the instance of the part that created this tool.
268      * Usually used if the tool wants to read its configuration in the app's config file.
269      * @return the instance of the part that created this tool.
270      */
271     QString componentName() const;
272 
273     /**
274      * Interface for 'running' this tool.
275      * This is the method that the data-tool must implement.
276      *
277      * @param command is the command that was selected (see KDataToolInfo::commands())
278      * @param data the data provided by the application, on which to run the tool.
279      *             The application is responsible for setting that data before running the tool,
280      *             and for getting it back and updating itself with it, after the tool ran.
281      * @param datatype defines the type of @p data.
282      * @param mimetype defines the mimetype of the data (for instance datatype may be
283      *                 QString, but the mimetype can be text/plain, text/html etc.)
284      * @return true if successful, false otherwise
285      */
286     virtual bool run(const QString &command, void *data, const QString &datatype, const QString &mimetype) = 0;
287 
288 private:
289     class KDataToolPrivate;
290     KDataToolPrivate *const d;
291 };
292 
293 #endif
294