Akonadi

mimetypechecker.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Kevin Krammer <[email protected]>
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 
14 using namespace Akonadi;
15 
17 {
18  d = new MimeTypeCheckerPrivate();
19 }
20 
22  : d(other.d)
23 {
24 }
25 
27 {
28 }
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 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
52  d->mWantedMimeTypes = QSet<QString>::fromList(mimeTypes);
53 #else
54  d->mWantedMimeTypes = QSet<QString>(mimeTypes.begin(), mimeTypes.end());
55 #endif
56 }
57 
59 {
60  d->mWantedMimeTypes.insert(mimeType);
61 }
62 
64 {
65  d->mWantedMimeTypes.remove(mimeType);
66 }
67 
68 bool MimeTypeChecker::isWantedItem(const Item &item) const
69 {
70  if (d->mWantedMimeTypes.isEmpty() || !item.isValid()) {
71  return false;
72  }
73 
74  const QString mimeType = item.mimeType();
75  if (mimeType.isEmpty()) {
76  return false;
77  }
78 
79  return d->isWantedMimeType(mimeType);
80 }
81 
82 bool MimeTypeChecker::isWantedCollection(const Collection &collection) const
83 {
84  if (d->mWantedMimeTypes.isEmpty() || !collection.isValid()) {
85  return false;
86  }
87 
88  const QStringList contentMimeTypes = collection.contentMimeTypes();
89  if (contentMimeTypes.isEmpty()) {
90  return false;
91  }
92 
93  for (const QString &mimeType : contentMimeTypes) {
94  if (mimeType.isEmpty()) {
95  continue;
96  }
97 
98  if (d->isWantedMimeType(mimeType)) {
99  return true;
100  }
101  }
102 
103  return false;
104 }
105 
106 bool MimeTypeChecker::isWantedItem(const Item &item, const QString &wantedMimeType)
107 {
108  if (wantedMimeType.isEmpty() || !item.isValid()) {
109  return false;
110  }
111 
112  const QString mimeType = item.mimeType();
113  if (mimeType.isEmpty()) {
114  return false;
115  }
116 
117  if (mimeType == wantedMimeType) {
118  return true;
119  }
120 
121  QMimeDatabase db;
122  const QMimeType mt = db.mimeTypeForName(mimeType);
123  if (!mt.isValid()) {
124  return false;
125  }
126 
127  return mt.inherits(wantedMimeType);
128 }
129 
130 bool MimeTypeChecker::isWantedCollection(const Collection &collection, const QString &wantedMimeType)
131 {
132  if (wantedMimeType.isEmpty() || !collection.isValid()) {
133  return false;
134  }
135 
136  const QStringList contentMimeTypes = collection.contentMimeTypes();
137  if (contentMimeTypes.isEmpty()) {
138  return false;
139  }
140 
141  for (const QString &mimeType : contentMimeTypes) {
142  if (mimeType.isEmpty()) {
143  continue;
144  }
145 
146  if (mimeType == wantedMimeType) {
147  return true;
148  }
149 
150  QMimeDatabase db;
151  const QMimeType mt = db.mimeTypeForName(mimeType);
152  if (!mt.isValid()) {
153  continue;
154  }
155 
156  if (mt.inherits(wantedMimeType)) {
157  return true;
158  }
159  }
160 
161  return false;
162 }
163 
164 bool MimeTypeChecker::isWantedMimeType(const QString &mimeType) const
165 {
166  return d->isWantedMimeType(mimeType);
167 }
168 
170 {
171  for (const QString &mt : mimeTypes) {
172  if (d->isWantedMimeType(mt)) {
173  return true;
174  }
175  }
176  return false;
177 }
178 
bool isValid() const
Returns whether the collection is valid.
Definition: collection.cpp:124
Represents a collection of PIM items.
Definition: collection.h:63
bool inherits(const QString &mimeTypeName) const const
Helper for checking MIME types of Collections and Items.
MimeTypeChecker()
Creates an empty MIME type checker.
void addWantedMimeType(const QString &mimeType)
Adds another MIME type to 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.
~MimeTypeChecker()
Destroys the MIME type checker.
bool isEmpty() const const
bool isEmpty() const const
QStringList wantedMimeTypes() const
Returns the list of wanted MIME types this instance checks against.
QMimeType mimeTypeForName(const QString &nameOrAlias) const const
bool isWantedCollection(const Collection &collection) const
Checks whether a given collection has one of the wanted MIME types.
QList::iterator end()
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 isValid() const const
void setWantedMimeTypes(const QStringList &mimeTypes)
Sets the list of wanted MIME types this instance checks against.
bool isWantedItem(const Item &item) const
Checks whether a given item has one of the wanted MIME types.
MimeTypeChecker & operator=(const MimeTypeChecker &other)
Assigns the other to this checker and returns a reference to this checker.
Helper integration between Akonadi and Qt.
QSet< T > fromList(const QList< T > &list)
QStringList contentMimeTypes() const
Returns a list of possible content mimetypes, e.g.
Definition: collection.cpp:243
void removeWantedMimeType(const QString &mimeType)
Removes a MIME type from the list of wanted MIME types this instance checks against.
QList::iterator begin()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Oct 22 2020 23:20:55 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.