Akonadi

mimetypechecker.cpp
1 /*
2  Copyright (c) 2009 Kevin Krammer <[email protected]>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "mimetypechecker.h"
21 
22 #include "mimetypechecker_p.h"
23 
24 #include "collection.h"
25 #include "item.h"
26 
27 using namespace Akonadi;
28 
30 {
31  d = new MimeTypeCheckerPrivate();
32 }
33 
35  : d(other.d)
36 {
37 }
38 
40 {
41 }
42 
44 {
45  if (&other != this) {
46  d = other.d;
47  }
48 
49  return *this;
50 }
51 
53 {
54  return d->mWantedMimeTypes.values();
55 }
56 
58 {
59  return !d->mWantedMimeTypes.isEmpty();
60 }
61 
63 {
64  d->mWantedMimeTypes = QSet<QString>::fromList(mimeTypes);
65 }
66 
68 {
69  d->mWantedMimeTypes.insert(mimeType);
70 }
71 
73 {
74  d->mWantedMimeTypes.remove(mimeType);
75 }
76 
77 bool MimeTypeChecker::isWantedItem(const Item &item) const
78 {
79  if (d->mWantedMimeTypes.isEmpty() || !item.isValid()) {
80  return false;
81  }
82 
83  const QString mimeType = item.mimeType();
84  if (mimeType.isEmpty()) {
85  return false;
86  }
87 
88  return d->isWantedMimeType(mimeType);
89 }
90 
91 bool MimeTypeChecker::isWantedCollection(const Collection &collection) const
92 {
93  if (d->mWantedMimeTypes.isEmpty() || !collection.isValid()) {
94  return false;
95  }
96 
97  const QStringList contentMimeTypes = collection.contentMimeTypes();
98  if (contentMimeTypes.isEmpty()) {
99  return false;
100  }
101 
102  for (const QString &mimeType : contentMimeTypes) {
103  if (mimeType.isEmpty()) {
104  continue;
105  }
106 
107  if (d->isWantedMimeType(mimeType)) {
108  return true;
109  }
110  }
111 
112  return false;
113 }
114 
115 bool MimeTypeChecker::isWantedItem(const Item &item, const QString &wantedMimeType)
116 {
117  if (wantedMimeType.isEmpty() || !item.isValid()) {
118  return false;
119  }
120 
121  const QString mimeType = item.mimeType();
122  if (mimeType.isEmpty()) {
123  return false;
124  }
125 
126  if (mimeType == wantedMimeType) {
127  return true;
128  }
129 
130  QMimeDatabase db;
131  const QMimeType mt = db.mimeTypeForName(mimeType);
132  if (!mt.isValid()) {
133  return false;
134  }
135 
136  return mt.inherits(wantedMimeType);
137 }
138 
139 bool MimeTypeChecker::isWantedCollection(const Collection &collection, const QString &wantedMimeType)
140 {
141  if (wantedMimeType.isEmpty() || !collection.isValid()) {
142  return false;
143  }
144 
145  const QStringList contentMimeTypes = collection.contentMimeTypes();
146  if (contentMimeTypes.isEmpty()) {
147  return false;
148  }
149 
150  for (const QString &mimeType : contentMimeTypes) {
151  if (mimeType.isEmpty()) {
152  continue;
153  }
154 
155  if (mimeType == wantedMimeType) {
156  return true;
157  }
158 
159  QMimeDatabase db;
160  const QMimeType mt = db.mimeTypeForName(mimeType);
161  if (!mt.isValid()) {
162  continue;
163  }
164 
165  if (mt.inherits(wantedMimeType)) {
166  return true;
167  }
168  }
169 
170  return false;
171 }
172 
173 bool MimeTypeChecker::isWantedMimeType(const QString &mimeType) const
174 {
175  return d->isWantedMimeType(mimeType);
176 }
177 
179 {
180  for (const QString &mt : mimeTypes) {
181  if (d->isWantedMimeType(mt)) {
182  return true;
183  }
184  }
185  return false;
186 }
187 
bool isValid() const
Returns whether the collection is valid.
Definition: collection.cpp:137
Represents a collection of PIM items.
Definition: collection.h:76
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.
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:256
void removeWantedMimeType(const QString &mimeType)
Removes a MIME type from the list of wanted MIME types this instance checks against.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed May 27 2020 22:43:39 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.