Akonadi

mimetypechecker.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Kevin Krammer <kevin.krammer@gmx.at>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "mimetypechecker.h"
8
9#include "mimetypechecker_p.h"
10
11#include "collection.h"
12#include "item.h"
13
14using namespace Akonadi;
15
17 : d(new MimeTypeCheckerPrivate())
18{
19}
20
22 : d(other.d)
23{
24}
25
29
31{
32 if (&other != this) {
33 d = other.d;
34 }
35
36 return *this;
37}
38
40{
41 return d->mWantedMimeTypes.values();
42}
43
45{
46 return !d->mWantedMimeTypes.isEmpty();
47}
48
50{
51 d->mWantedMimeTypes = QSet<QString>(mimeTypes.begin(), mimeTypes.end());
52}
53
55{
56 d->mWantedMimeTypes.insert(mimeType);
57}
58
60{
61 d->mWantedMimeTypes.remove(mimeType);
62}
63
64bool MimeTypeChecker::isWantedItem(const Item &item) const
65{
66 if (d->mWantedMimeTypes.isEmpty() || !item.isValid()) {
67 return false;
68 }
69
70 const QString mimeType = item.mimeType();
71 if (mimeType.isEmpty()) {
72 return false;
73 }
74
75 return d->isWantedMimeType(mimeType);
76}
77
79{
80 if (d->mWantedMimeTypes.isEmpty() || !collection.isValid()) {
81 return false;
82 }
83
84 const QStringList contentMimeTypes = collection.contentMimeTypes();
85 if (contentMimeTypes.isEmpty()) {
86 return false;
87 }
88
89 for (const QString &mimeType : contentMimeTypes) {
90 if (mimeType.isEmpty()) {
91 continue;
92 }
93
94 if (d->isWantedMimeType(mimeType)) {
95 return true;
96 }
97 }
98
99 return false;
100}
101
102bool MimeTypeChecker::isWantedItem(const Item &item, const QString &wantedMimeType)
103{
104 if (wantedMimeType.isEmpty() || !item.isValid()) {
105 return false;
106 }
107
108 const QString mimeType = item.mimeType();
109 if (mimeType.isEmpty()) {
110 return false;
111 }
112
113 if (mimeType == wantedMimeType) {
114 return true;
115 }
116
117 QMimeDatabase db;
118 const QMimeType mt = db.mimeTypeForName(mimeType);
119 if (!mt.isValid()) {
120 return false;
121 }
122
123 return mt.inherits(wantedMimeType);
124}
125
126bool MimeTypeChecker::isWantedCollection(const Collection &collection, const QString &wantedMimeType)
127{
128 if (wantedMimeType.isEmpty() || !collection.isValid()) {
129 return false;
130 }
131
132 const QStringList contentMimeTypes = collection.contentMimeTypes();
133 if (contentMimeTypes.isEmpty()) {
134 return false;
135 }
136
137 for (const QString &mimeType : contentMimeTypes) {
138 if (mimeType.isEmpty()) {
139 continue;
140 }
141
142 if (mimeType == wantedMimeType) {
143 return true;
144 }
145
146 QMimeDatabase db;
147 const QMimeType mt = db.mimeTypeForName(mimeType);
148 if (!mt.isValid()) {
149 continue;
150 }
151
152 if (mt.inherits(wantedMimeType)) {
153 return true;
154 }
155 }
156
157 return false;
158}
159
161{
162 return d->isWantedMimeType(mimeType);
163}
164
166{
167 for (const QString &mt : mimeTypes) {
168 if (d->isWantedMimeType(mt)) {
169 return true;
170 }
171 }
172 return false;
173}
Represents a collection of PIM items.
Definition collection.h:62
Represents a PIM item stored in Akonadi storage.
Definition item.h:100
QString mimeType() const
Returns the mime type of the item.
Definition item.cpp:326
bool isValid() const
Returns whether the item is valid.
Definition item.cpp:88
Helper for checking MIME types of Collections and Items.
void addWantedMimeType(const QString &mimeType)
Adds another MIME type to the list of wanted MIME types this instance checks against.
bool isWantedMimeType(const QString &mimeType) const
Checks whether a given mime type is covered by one of the wanted MIME types.
bool hasWantedMimeTypes() const
Checks whether any wanted MIME types are set.
bool isWantedItem(const Item &item) const
Checks whether a given item has one of the wanted MIME types.
MimeTypeChecker()
Creates an empty MIME type checker.
bool isWantedCollection(const Collection &collection) const
Checks whether a given collection has one of the wanted MIME types.
~MimeTypeChecker()
Destroys the MIME type checker.
QStringList wantedMimeTypes() const
Returns the list of wanted MIME types this instance checks against.
MimeTypeChecker & operator=(const MimeTypeChecker &other)
Assigns the other to this checker and returns a reference to this checker.
void removeWantedMimeType(const QString &mimeType)
Removes a MIME type from the list of wanted MIME types this instance checks against.
bool containsWantedMimeType(const QStringList &mimeTypes) const
Checks whether any of the given MIME types is covered by one of the wanted MIME types.
void setWantedMimeTypes(const QStringList &mimeTypes)
Sets the list of wanted MIME types this instance checks against.
Helper integration between Akonadi and Qt.
iterator begin()
iterator end()
bool isEmpty() const const
QMimeType mimeTypeForName(const QString &nameOrAlias) const const
bool inherits(const QString &mimeTypeName) const const
bool isValid() const const
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Sep 27 2024 11:50:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.