MauiKit File Browsing

tagging.h
1/*
2 * Copyright 2018 Camilo Higuita <milo.h@aol.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20#pragma once
21
22#include <QObject>
23#include <QQmlEngine>
24
25#include "filebrowsing_export.h"
26
27#include <MauiKit4/Core/fmh.h>
28
29#define MAX_LIMIT 9999
30
31class TAGDB;
32
33/**
34 * @brief The Tagging class provides quick methods to access and modify the tags associated to the files.
35 *
36 * @note This class follows a singleton pattern and it is thread safe, by creating a new instance for each new thread that request access to the singleton. All of the internal instances are self-handled and destroyed when the application quits.
37 *
38 * Graphical interfaces are provided which implement most of this class functionality and can be quickly used:
39 * - TagsBar
40 * - TagsDialog
41 */
42class FILEBROWSING_EXPORT Tagging : public QObject
43{
44 Q_OBJECT
45 QML_ELEMENT
46 QML_SINGLETON
47 Q_DISABLE_COPY_MOVE(Tagging)
48
49public:
50 /**
51 * @brief Returns an instance to the tagging object.
52 * @return
53 */
54 static Tagging *getInstance();
55 static QObject * qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine) {
56 Q_UNUSED(scriptEngine)
57 auto obj = Tagging::getInstance();
59 return obj;
60 }
61
62 /**
63 * @private
64 */
65 Tagging();
66
67 /**
68 * @private
69 */
70 ~Tagging();
71
72public Q_SLOTS:
73
74 /**
75 * @brief Retrieve the information into a model, optionally you can pass a modifier callback function to manipulate or discard items in the model
76 * @param query the query to be retrieved
77 * @param modifier a callback function that sends as an argument a reference to the current item being retrieved, which can be modified in-place, and expects a boolean value to be returned to decide if such item should de added to the model or not
78 * @return the resulting model
79 */
80 const QVariantList get(const QString &query, std::function<bool(QVariantMap &item)> modifier = nullptr);
81
82 /**
83 * @brief Checks if a given tag exists, it can be strictly enforced, meaning it is checked if the tag was created by the application making the request
84 * @param tag the tag to search
85 * @param strict whether the search should be strictly enforced. If strict is true then the tag should have been created by the app making the request, otherwise checks if the tag exists and could have been created by any other application.
86 * @return
87 */
88 bool tagExists(const QString &tag, const bool &strict = false);
89
90 /**
91 * @brief Checks if a given tag is associated to a give file URL. The check can be strictly enforced, meaning the given URL was tagged by the application making the request
92 * @param url the file URL
93 * @param tag the tag to perform the check
94 * @param strict strictly enforced check
95 * @return
96 */
97 bool urlTagExists(const QString &url, const QString &tag);
98
99 // INSERTIONS
100 /**
101 * @brief Adds a new tag, the newly created tag gets associated to the app making the call. If the tag already exists nothing is changed. If the tag exists the app making the request will get associated to the tag too
102 * @param tag the name of the tag
103 * @param color optional color for the tag
104 * @param comment optional comment for the tag @deprecated
105 * @return whether the operation was successful, meaning the tag was created
106 */
107 bool tag(const QString &tag, const QString &color = QString(), const QString &comment = QString());
108
109 /**
110 * @brief Adds a tag to a given file URL, if the given tag doesn't exists then it gets created
111 * @param url the file URL to be tagged
112 * @param tag the tag to be added to the file URL
113 * @param color @deprecated Optional color
114 * @param comment optional comment
115 * @return whether the operation was successful
116 */
117 bool tagUrl(const QString &url, const QString &tag, const QString &color = QString(), const QString &comment = QString());
118
119 // UPDATES
120 /**
121 * @brief Updates the tags associated to a file URL. If any of the given tags doesn't exists then they get created, if a tag associated to the current file URL is missing in the new passed tags then those get removed
122 * @param url the file URL
123 * @param tags the new set of tags to be associated to the file URL
124 * @return whether the operation was successful
125 */
126 bool updateUrlTags(const QString &url, const QStringList &tags, const bool &strict = false);
127
128 /**
129 * @brief Updates a file URL to a new URL, preserving all associated tags. This is useful if a file is rename or moved to a new location
130 * @param url previous file URL
131 * @param newUrl new file URL
132 * @return whether the operation was successful
133 */
134 bool updateUrl(const QString &url, const QString &newUrl);
135
136 // QUERIES
137
138 /**
139 * @brief Give a list of all tags associated to files @deprecated
140 * @param strict
141 * @return whether the operation was successful
142 */
143 QVariantList getUrlsTags(const bool &strict = false);
144
145 /**
146 * @brief Returns a list model of all the tags. The model can be strictly enforced to only tags that were created by the application making the call
147 * @param strict if true returns only tags created by the application making the request
148 * @return the model with the info of all the requested tags
149 */
150 QVariantList getAllTags(const bool &strict = false);
151
152 /**
153 * @brief Returns a model of all the file URLs associated to a tag, the result can be strictly enforced to only file URLs associated to a tag created by the application making the request, restrict it to a maximum limit, filter by the mime-type or just add a modifier function
154 * @param tag the tag name to perform the search
155 * @param strict strictly enforced to only file URLs associated to the given tag created by the application making the request
156 * @param limit maximum limit of results
157 * @param mimeType filter by mime-type, for example: `"image/\*"` or `"image/png"`
158 * @param modifier a callback function that sends as an argument a reference to the current item being retrieved, which can be modified, and expects a boolean value to be returned to decide if such item should be added to the model or not
159 * @return the result model
160 */
161 QVariantList getUrls(const QString &tag, const bool &strict = false, const int &limit = MAX_LIMIT, const QString &mimeType = QStringLiteral(""), std::function<bool(QVariantMap &item)> modifier = nullptr);
162
163 /**
164 * @brief Returns a model list of all the tags associated to a file URL. The result can be strictly enforced to only tags created by the application making the call
165 * @param url the file URL
166 * @param strict strictly enforced to only tags created by the application making the request
167 * @return the result model
168 */
169 QVariantList getUrlTags(const QString &url, const bool &strict = false);
170
171 // DELETE
172 /**
173 * @brief Given a file URL remove all the tags associated to it
174 * @param url the file URL
175 * @return whether the operation was successful
176 */
177 bool removeUrlTags(const QString &url, const bool &strict = false);
178
179 /**
180 * @brief Removes a given tag associated to a given file URL
181 * @param url file URL
182 * @param tag tag associated to file URL to be removed
183 * @return whether the operation was successful
184 */
185 bool removeUrlTag(const QString &url, const QString &tag);
186
187 /**
188 * @brief Removes a URL with its associated tags
189 * @param url the file URL
190 * @return whether the operation was successful
191 */
192 bool removeUrl(const QString &url);
193
194 /**
195 * @brief Remove a tag
196 */
197 bool removeTag(const QString &tag, const bool &strict = false);
198
199 /**
200 * @brief Checks if a file URL has been marked as favorite. This works if the tagging component has been enabled, otherwise returns false as default.
201 * @param url the file URL to be checked
202 * @param strict strictly check if the file has been marked as favorite by the app making the request or not
203 * @return
204 */
205 bool isFav(const QUrl &url, const bool &strict = false);
206
207 /**
208 * @brief If the given file has been marked as favorite then the tag is removed. This works if the tagging component has been enabled, otherwise returns false as default.
209 * @param url the file URL
210 * @return whether the operation has been successful
211 */
212 bool unFav(const QUrl &url);
213
214 /**
215 * @brief Marks a file URL as favorite. This works if the tagging component has been enabled, otherwise returns false as default.
216 * @param url the file URL
217 * @return whether the operation has been successful
218 */
219 bool fav(const QUrl &url);
220
221 /**
222 * @brief Toggle the fav tag of a given file, meaning, if a file is marked as fav then the tag gets removed and if it is not marked then the fav tag gets added.
223 * @param url the file URL
224 * @return whether the operation has been successful
225 */
226 bool toggleFav(const QUrl &url);
227
228 /**
229 * @brief Shortcut for getting a list of file URLs associated to a tag, the resulting list of URLs can be filtered by regular expression or by mime-type and limited
230 * @param tag the tag to look up
231 * @param filters the regular expressions list
232 * @param strict if strict then the URLs returned are only associated to the application making the call, meaning, that such tag was added by such application only.
233 * @param limit the maximum limit number of URLs to be returned
234 * @param mime the mime-type filtering, for example, `"image/\*"` or `"image/png"`, `"audio/mp4"`
235 * @return the list of file URLs
236 */
237 QList<QUrl> getTagUrls(const QString &tag, const QStringList &filters, const bool &strict = false, const int &limit = 9999, const QString &mime = QStringLiteral(""));
238
239 /**
240 * @brief Get all the tags available with detailed information packaged as a FMH::MODEL_LIST
241 * @param limit the maximum numbers of tags
242 * @return the model of tags
243 */
244 FMH::MODEL_LIST getTags(const int &limit = 5);
245
246 /**
247 * @brief Returns a model of tags associated to a file URL
248 * @param url the file URL
249 * @return model of the tags
250 */
251 FMH::MODEL_LIST getUrlTags(const QUrl &url);
252
253 /**
254 * @brief Adds a tag to a given file URL
255 * @param tag the wanted tag, if the tag doesn't exists it is created
256 * @param url the file URL
257 * @return whether the operation has been successful
258 */
259 bool addTagToUrl(const QString tag, const QUrl &url);
260
261 /**
262 * @brief Removes a tag from a file URL if the tags exists
263 * @param tag the lookup tag
264 * @param url the file URL
265 * @return whether the operation has been successful
266 */
267 bool removeTagToUrl(const QString tag, const QUrl &url);
268
269private:
270 void setApp();
271
272 QString appName;
273 QString appComment;
274 QString appOrg;
275
276 //register the app to the db
277 bool app();
278
280 TAGDB *db();
281
282protected:
283 static bool setTagIconName(QVariantMap &item);
284
286 /**
287 * @brief Emitted when a tag has been assigened to a file URL
288 * @param url The file URL tagged
289 * @param tag the tag name
290 */
291 void urlTagged(QString url, QString tag);
292
293 /**
294 * @brief Emitted when a new tag has been created.
295 * @param tag contains information about the new tag, such as color, add date and comment
296 */
297 void tagged(QVariantMap tag);
298
299 /**
300 * @brief Emitted when a tag has been removed
301 * @param tag
302 */
304
305 /**
306 * @brief Emitted when a tag has been dissociated from a file URL
307 * @param tag
308 * @param url
309 */
311
312 /**
313 * @brief Emitted when a file has been removed a thus all associations to any tag
314 * @param url
315 */
317};
318
The TAGDB class exposes methods to add, remove and modify tags in the MauiKit FileBrowsing Tagging sy...
Definition tagdb.h:56
The Tagging class provides quick methods to access and modify the tags associated to the files.
Definition tagging.h:43
void urlTagged(QString url, QString tag)
Emitted when a tag has been assigened to a file URL.
void tagRemoved(QString tag)
Emitted when a tag has been removed.
void tagged(QVariantMap tag)
Emitted when a new tag has been created.
static Tagging * getInstance()
Returns an instance to the tagging object.
Definition tagging.cpp:70
void urlTagRemoved(QString tag, QString url)
Emitted when a tag has been dissociated from a file URL.
void urlRemoved(QString url)
Emitted when a file has been removed a thus all associations to any tag.
void setObjectOwnership(QObject *object, ObjectOwnership ownership)
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:49:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.