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#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
28#include <MauiKit3/Core/fmh.h>
29#else
30#include <MauiKit4/Core/fmh.h>
31#endif
32
33#define MAX_LIMIT 9999
34
35class TAGDB;
36
37/**
38 * @brief The Tagging class provides quick methods to access and modify the tags associated to the files.
39 *
40 * @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.
41 *
42 * Graphical interfaces are provided which implement most of this class functionality and can be quickly used:
43 * - TagsBar
44 * - TagsDialog
45 */
46class FILEBROWSING_EXPORT Tagging : public QObject
47{
48 Q_OBJECT
49 QML_ELEMENT
50 QML_SINGLETON
51 Q_DISABLE_COPY_MOVE(Tagging)
52
53public:
54 /**
55 * @brief Returns an instance to the tagging object.
56 * @return
57 */
58 static Tagging *getInstance();
59 static QObject * qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine) {
60 Q_UNUSED(scriptEngine)
61 auto obj = Tagging::getInstance();
63 return obj;
64 }
65
66 /**
67 * @private
68 */
69 Tagging();
70
71 /**
72 * @private
73 */
74 ~Tagging();
75
76public Q_SLOTS:
77
78 /**
79 * @brief Retrieve the information into a model, optionally you can pass a modifier callback function to manipulate or discard items in the model
80 * @param query the query to be retrieved
81 * @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
82 * @return the resulting model
83 */
84 const QVariantList get(const QString &query, std::function<bool(QVariantMap &item)> modifier = nullptr);
85
86 /**
87 * @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
88 * @param tag the tag to search
89 * @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.
90 * @return
91 */
92 bool tagExists(const QString &tag, const bool &strict = false);
93
94 /**
95 * @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
96 * @param url the file URL
97 * @param tag the tag to perform the check
98 * @param strict strictly enforced check
99 * @return
100 */
101 bool urlTagExists(const QString &url, const QString &tag);
102
103 // INSERTIONS
104 /**
105 * @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
106 * @param tag the name of the tag
107 * @param color optional color for the tag
108 * @param comment optional comment for the tag @deprecated
109 * @return whether the operation was successful, meaning the tag was created
110 */
111 bool tag(const QString &tag, const QString &color = QString(), const QString &comment = QString());
112
113 /**
114 * @brief Adds a tag to a given file URL, if the given tag doesn't exists then it gets created
115 * @param url the file URL to be tagged
116 * @param tag the tag to be added to the file URL
117 * @param color @deprecated Optional color
118 * @param comment optional comment
119 * @return whether the operation was successful
120 */
121 bool tagUrl(const QString &url, const QString &tag, const QString &color = QString(), const QString &comment = QString());
122
123 // UPDATES
124 /**
125 * @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
126 * @param url the file URL
127 * @param tags the new set of tags to be associated to the file URL
128 * @return whether the operation was successful
129 */
130 bool updateUrlTags(const QString &url, const QStringList &tags, const bool &strict = false);
131
132 /**
133 * @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
134 * @param url previous file URL
135 * @param newUrl new file URL
136 * @return whether the operation was successful
137 */
138 bool updateUrl(const QString &url, const QString &newUrl);
139
140 // QUERIES
141
142 /**
143 * @brief Give a list of all tags associated to files @deprecated
144 * @param strict
145 * @return whether the operation was successful
146 */
147 QVariantList getUrlsTags(const bool &strict = false);
148
149 /**
150 * @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
151 * @param strict if true returns only tags created by the application making the request
152 * @return the model with the info of all the requested tags
153 */
154 QVariantList getAllTags(const bool &strict = false);
155
156 /**
157 * @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
158 * @param tag the tag name to perform the search
159 * @param strict strictly enforced to only file URLs associated to the given tag created by the application making the request
160 * @param limit maximum limit of results
161 * @param mimeType filter by mime-type, for example: `"image/\*"` or `"image/png"`
162 * @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
163 * @return the result model
164 */
165 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);
166
167 /**
168 * @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
169 * @param url the file URL
170 * @param strict strictly enforced to only tags created by the application making the request
171 * @return the result model
172 */
173 QVariantList getUrlTags(const QString &url, const bool &strict = false);
174
175 // DELETE
176 /**
177 * @brief Given a file URL remove all the tags associated to it
178 * @param url the file URL
179 * @return whether the operation was successful
180 */
181 bool removeUrlTags(const QString &url, const bool &strict = false);
182
183 /**
184 * @brief Removes a given tag associated to a given file URL
185 * @param url file URL
186 * @param tag tag associated to file URL to be removed
187 * @return whether the operation was successful
188 */
189 bool removeUrlTag(const QString &url, const QString &tag);
190
191 /**
192 * @brief Removes a URL with its associated tags
193 * @param url the file URL
194 * @return whether the operation was successful
195 */
196 bool removeUrl(const QString &url);
197
198 /**
199 * @brief Remove a tag
200 */
201 bool removeTag(const QString &tag, const bool &strict = false);
202
203 /**
204 * @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.
205 * @param url the file URL to be checked
206 * @param strict strictly check if the file has been marked as favorite by the app making the request or not
207 * @return
208 */
209 bool isFav(const QUrl &url, const bool &strict = false);
210
211 /**
212 * @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.
213 * @param url the file URL
214 * @return whether the operation has been successful
215 */
216 bool unFav(const QUrl &url);
217
218 /**
219 * @brief Marks a file URL as favorite. This works if the tagging component has been enabled, otherwise returns false as default.
220 * @param url the file URL
221 * @return whether the operation has been successful
222 */
223 bool fav(const QUrl &url);
224
225 /**
226 * @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.
227 * @param url the file URL
228 * @return whether the operation has been successful
229 */
230 bool toggleFav(const QUrl &url);
231
232 /**
233 * @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
234 * @param tag the tag to look up
235 * @param filters the regular expressions list
236 * @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.
237 * @param limit the maximum limit number of URLs to be returned
238 * @param mime the mime-type filtering, for example, `"image/\*"` or `"image/png"`, `"audio/mp4"`
239 * @return the list of file URLs
240 */
241 QList<QUrl> getTagUrls(const QString &tag, const QStringList &filters, const bool &strict = false, const int &limit = 9999, const QString &mime = QStringLiteral(""));
242
243 /**
244 * @brief Get all the tags available with detailed information packaged as a FMH::MODEL_LIST
245 * @param limit the maximum numbers of tags
246 * @return the model of tags
247 */
248 FMH::MODEL_LIST getTags(const int &limit = 5);
249
250 /**
251 * @brief Returns a model of tags associated to a file URL
252 * @param url the file URL
253 * @return model of the tags
254 */
255 FMH::MODEL_LIST getUrlTags(const QUrl &url);
256
257 /**
258 * @brief Adds a tag to a given file URL
259 * @param tag the wanted tag, if the tag doesn't exists it is created
260 * @param url the file URL
261 * @return whether the operation has been successful
262 */
263 bool addTagToUrl(const QString tag, const QUrl &url);
264
265 /**
266 * @brief Removes a tag from a file URL if the tags exists
267 * @param tag the lookup tag
268 * @param url the file URL
269 * @return whether the operation has been successful
270 */
271 bool removeTagToUrl(const QString tag, const QUrl &url);
272
273private:
274 void setApp();
275
276 QString appName;
277 QString appComment;
278 QString appOrg;
279
280 //register the app to the db
281 bool app();
282
284 TAGDB *db();
285
286protected:
287 static bool setTagIconName(QVariantMap &item);
288
290 void urlTagged(QString url, QString tag);
291 void tagged(QVariantMap tag);
292 void tagRemoved(QString tag);
293 void urlTagRemoved(QString tag, QString url);
294 void urlRemoved(QString url);
295};
296
The TAGDB class exposes methods to add, remove and modify tags in the MauiKit FileBrowsing Tagging sy...
Definition tagdb.h:60
The Tagging class provides quick methods to access and modify the tags associated to the files.
Definition tagging.h:47
static Tagging * getInstance()
Returns an instance to the tagging object.
Definition tagging.cpp:70
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 May 17 2024 11:51:27 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.