Debian Codegraph @

lxqt-config-file-associations/mimetypeitemmodel.cpp

raw
1 /*
2 * Copyright (c) 2015 Christian Surlykke
3 *
4 * This file is part of the LXQt project. <https://lxqt-project.org>
5 * It is distributed under the LGPL 2.1 or later license.
6 * Please refer to the LICENSE file for a copy of the license, and
7 * the AUTHORS file for copyright and authorship information.
8 */
9
10 #include <QObject>
11 #include <QDebug>
12
13 #include "mimetypeitemmodel.h"
14 #include <XdgMime>
15
16 /*
17 * Implementation note:
18 *
19 * MimetypeItemModel is an implementation of QAbstractItemModel with XdgMimeInfoCache as backing store.
20 *
21 * There are 2 levels of items in this model: mediatype ('application', 'audio', 'image' etc.) and
22 * subtype ('application/pdf' which is a child of 'application' or 'image/jpeg' which is a child of 'image'.
23 *
24 * A QModelIndex for a mediatype has a zero internal pointer.
25 *
26 * A QModelIndex for a subtype has an internal pointer to the XdgMimeInfo for that subtype - so the QModelIndex for
27 * 'image/jpeg' has an internal pointer that points to the XdgMineInfo for 'image/jpeg' which is held in XdgMimeInfoCache.
28 *
29 * QModelIndexes for mediatypes have no parents, and QModelIndexes for subtypes have no children.
30 */
31
32
33 MimetypeItemModel::MimetypeItemModel(QObject *parent) :
34 QAbstractItemModel(parent)
35 {
36 }
37
38 MimetypeItemModel::~MimetypeItemModel()
39 {
40 }
41
42 QVariant MimetypeItemModel::data(const QModelIndex& index, int role) const
43 {
44 if (!index.isValid())
45 {
46 return QVariant();
47 }
48
49 XdgMimeInfo *mimeInfo = 0;
50 QString text;
51
52 if (index.parent().isValid())
53 {
54 mimeInfo = static_cast<XdgMimeInfo*>(index.internalPointer());
55 text = mimeInfo->subType();
56 }
57 else
58 {
59 text = XdgMimeInfoCache::mediatypes().value(index.row());
60 }
61
62 switch (role)
63 {
64 case Qt::DisplayRole : return QVariant(text);
65 case MimeInfoRole : return QVariant::fromValue<XdgMimeInfo*>(mimeInfo);
66 default : return QVariant();
67 }
68 }
69
70 QModelIndex MimetypeItemModel::index(int row, int column, const QModelIndex& parent) const
71 {
72 if (column != 0)
73 {
74 return QModelIndex();
75 }
76
77 if (parent.isValid())
78 {
79 if (parent.row() >= XdgMimeInfoCache::mediatypes().size())
80 {
81 return QModelIndex();
82 }
83
84 QString mediatype = XdgMimeInfoCache::mediatypes().value(parent.row());
85
86 if (row >= XdgMimeInfoCache::subtypes(mediatype).size())
87 {
88 return QModelIndex();
89 }
90
91 QString subtype = XdgMimeInfoCache::subtypes(mediatype).value(row);
92
93 XdgMimeInfo* mimeInfo = XdgMimeInfoCache::xdgMimeInfo(mediatype, subtype);
94 return createIndex(row, 0, mimeInfo);
95 }
96 else
97 {
98 if (row >= XdgMimeInfoCache::mediatypes().size())
99 {
100 return QModelIndex();
101 }
102
103 return createIndex(row, 0);
104 }
105 }
106
107 QModelIndex MimetypeItemModel::parent(const QModelIndex& index) const
108 {
109 if (index.isValid() && index.internalPointer())
110 {
111 XdgMimeInfo* mimeInfo = static_cast<XdgMimeInfo*>(index.internalPointer());
112
113 int row = XdgMimeInfoCache::mediatypes().indexOf(mimeInfo->mediaType());
114 return createIndex(row, 0);
115 }
116 else
117 {
118 return QModelIndex();
119 }
120 }
121
122
123 int MimetypeItemModel::rowCount(const QModelIndex& parent) const
124 {
125 if (parent.column() > 0)
126 {
127 return 0;
128 }
129
130 if (parent.parent().isValid())
131 {
132 return 0;
133 }
134 else if (parent.isValid())
135 {
136 QString media = XdgMimeInfoCache::mediatypes().value(parent.row());
137 return XdgMimeInfoCache::subtypes(media).size();
138 }
139 else
140 {
141 return XdgMimeInfoCache::mediatypes().size();
142 }
143 }
144
145
146 MimetypeFilterItemModel::MimetypeFilterItemModel(QObject* parent) :
147 QSortFilterProxyModel(parent)
148 {
149 }
150
151 bool MimetypeFilterItemModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
152 {
153 QModelIndex index = sourceModel()->index(source_row, 0, source_parent);
154
155 if (source_parent.isValid())
156 {
157 return filterHelper(index);
158 }
159 else
160 {
161 for (int i = 0; i < sourceModel()->rowCount(index); i++)
162 {
163 if (filterAcceptsRow(i, index))
164 {
165 return true;
166 }
167 }
168 }
169
170 return false;
171 }
172
173 bool MimetypeFilterItemModel::filterHelper(QModelIndex& source_index) const
174 {
175 XdgMimeInfo* mimeInfo = source_index.data(MimeInfoRole).value<XdgMimeInfo*>();
176 if (!mimeInfo)
177 {
178 return false;
179 }
180
181 if (mimeInfo->mimeType().contains(filterRegExp()))
182 {
183 return true;
184 }
185
186 if (mimeInfo->comment().contains(filterRegExp()))
187 {
188 return true;
189 }
190
191 const auto patterns = mimeInfo->patterns();
192 for (const QString &pattern : patterns)
193 {
194 if (pattern.contains(filterRegExp()))
195 {
196 return true;
197 }
198 }
199
200 return false;
201 }