1 | /* |
2 | Copyright (c) 2009 Kevin Krammer <kevin.krammer@gmx.at> |
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 | |
29 | MimeTypeChecker::MimeTypeChecker() |
30 | { |
31 | d = new MimeTypeCheckerPrivate(); |
32 | } |
33 | |
34 | MimeTypeChecker::MimeTypeChecker(const MimeTypeChecker &other) |
35 | : d(other.d) |
36 | { |
37 | } |
38 | |
39 | MimeTypeChecker::~MimeTypeChecker() |
40 | { |
41 | } |
42 | |
43 | MimeTypeChecker &MimeTypeChecker::operator=(const MimeTypeChecker &other) |
44 | { |
45 | if (&other != this) { |
46 | d = other.d; |
47 | } |
48 | |
49 | return *this; |
50 | } |
51 | |
52 | QStringList MimeTypeChecker::wantedMimeTypes() const |
53 | { |
54 | return d->mWantedMimeTypes.values(); |
55 | } |
56 | |
57 | void MimeTypeChecker::setWantedMimeTypes(const QStringList &mimeTypes) |
58 | { |
59 | d->mWantedMimeTypes = QSet<QString>::fromList(mimeTypes); |
60 | } |
61 | |
62 | void MimeTypeChecker::addWantedMimeType(const QString &mimeType) |
63 | { |
64 | d->mWantedMimeTypes.insert(mimeType); |
65 | } |
66 | |
67 | void MimeTypeChecker::removeWantedMimeType(const QString &mimeType) |
68 | { |
69 | d->mWantedMimeTypes.remove(mimeType); |
70 | } |
71 | |
72 | bool MimeTypeChecker::isWantedItem(const Item &item) const |
73 | { |
74 | if (d->mWantedMimeTypes.isEmpty() || !item.isValid()) { |
75 | return false; |
76 | } |
77 | |
78 | const QString mimeType = item.mimeType(); |
79 | if (mimeType.isEmpty()) { |
80 | return false; |
81 | } |
82 | |
83 | return d->isWantedMimeType(mimeType); |
84 | } |
85 | |
86 | bool MimeTypeChecker::isWantedCollection(const Collection &collection) const |
87 | { |
88 | if (d->mWantedMimeTypes.isEmpty() || !collection.isValid()) { |
89 | return false; |
90 | } |
91 | |
92 | const QStringList contentMimeTypes = collection.contentMimeTypes(); |
93 | if (contentMimeTypes.isEmpty()) { |
94 | return false; |
95 | } |
96 | |
97 | foreach (const QString &mimeType, contentMimeTypes) { |
98 | if (mimeType.isEmpty()) { |
99 | continue; |
100 | } |
101 | |
102 | if (d->isWantedMimeType(mimeType)) { |
103 | return true; |
104 | } |
105 | } |
106 | |
107 | return false; |
108 | } |
109 | |
110 | bool MimeTypeChecker::isWantedItem(const Item &item, const QString &wantedMimeType) |
111 | { |
112 | if (wantedMimeType.isEmpty() || !item.isValid()) { |
113 | return false; |
114 | } |
115 | |
116 | const QString mimeType = item.mimeType(); |
117 | if (mimeType.isEmpty()) { |
118 | return false; |
119 | } |
120 | |
121 | if (mimeType == wantedMimeType) { |
122 | return true; |
123 | } |
124 | |
125 | KMimeType::Ptr mimeTypePtr = KMimeType::mimeType(mimeType, KMimeType::ResolveAliases); |
126 | if (mimeTypePtr.isNull()) { |
127 | return false; |
128 | } |
129 | |
130 | return mimeTypePtr->is(wantedMimeType); |
131 | } |
132 | |
133 | bool MimeTypeChecker::isWantedCollection(const Collection &collection, const QString &wantedMimeType) |
134 | { |
135 | if (wantedMimeType.isEmpty() || !collection.isValid()) { |
136 | return false; |
137 | } |
138 | |
139 | const QStringList contentMimeTypes = collection.contentMimeTypes(); |
140 | if (contentMimeTypes.isEmpty()) { |
141 | return false; |
142 | } |
143 | |
144 | foreach (const QString &mimeType, contentMimeTypes) { |
145 | if (mimeType.isEmpty()) { |
146 | continue; |
147 | } |
148 | |
149 | if (mimeType == wantedMimeType) { |
150 | return true; |
151 | } |
152 | |
153 | KMimeType::Ptr mimeTypePtr = KMimeType::mimeType(mimeType, KMimeType::ResolveAliases); |
154 | if (mimeTypePtr.isNull()) { |
155 | continue; |
156 | } |
157 | |
158 | if (mimeTypePtr->is(wantedMimeType)) { |
159 | return true; |
160 | } |
161 | } |
162 | |
163 | return false; |
164 | } |
165 | |
166 | bool MimeTypeChecker::isWantedMimeType(const QString &mimeType) const |
167 | { |
168 | return d->isWantedMimeType(mimeType); |
169 | } |
170 | |
171 | bool MimeTypeChecker::containsWantedMimeType(const QStringList &mimeTypes) const |
172 | { |
173 | foreach (const QString &mt, mimeTypes) { |
174 | if (d->isWantedMimeType(mt)) { |
175 | return true; |
176 | } |
177 | } |
178 | return false; |
179 | } |
180 | |
181 | // kate: space-indent on; indent-width 2; replace-tabs on; |
182 | |