1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2013 by Vishesh Handa <me@vhanda.in> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include "kbaloorolesprovider.h"
24 #include <KFileMetaData/PropertyInfo>
25 #include <KFileMetaData/UserMetaData>
27 #include <KLocalizedString>
33 struct KBalooRolesProviderSingleton
35 KBalooRolesProvider instance
;
37 Q_GLOBAL_STATIC(KBalooRolesProviderSingleton
, s_balooRolesProvider
)
40 KBalooRolesProvider
& KBalooRolesProvider::instance()
42 return s_balooRolesProvider
->instance
;
45 KBalooRolesProvider::~KBalooRolesProvider()
49 QSet
<QByteArray
> KBalooRolesProvider::roles() const
54 QHash
<QByteArray
, QVariant
> KBalooRolesProvider::roleValues(const Baloo::File
& file
,
55 const QSet
<QByteArray
>& roles
) const
57 QHash
<QByteArray
, QVariant
> values
;
59 using entry
= std::pair
<const KFileMetaData::Property::Property
&, const QVariant
&>;
61 const auto& propMap
= file
.properties();
62 auto rangeBegin
= propMap
.constKeyValueBegin();
64 while (rangeBegin
!= propMap
.constKeyValueEnd()) {
65 auto key
= (*rangeBegin
).first
;
66 const KFileMetaData::PropertyInfo
propertyInfo(key
);
67 const QByteArray role
= roleForProperty(propertyInfo
.name());
69 auto rangeEnd
= std::find_if(rangeBegin
, propMap
.constKeyValueEnd(),
70 [key
](const entry
& e
) { return e
.first
!= key
; });
72 if (role
.isEmpty() || !roles
.contains(role
)) {
73 rangeBegin
= rangeEnd
;
77 auto distance
= std::distance(rangeBegin
, rangeEnd
);
80 list
.reserve(static_cast<int>(distance
));
81 std::for_each(rangeBegin
, rangeEnd
, [&list
](const entry
& s
) { list
.append(s
.second
); });
82 values
.insert(role
, propertyInfo
.formatAsDisplayString(list
));
84 values
.insert(role
, propertyInfo
.formatAsDisplayString((*rangeBegin
).second
));
86 rangeBegin
= rangeEnd
;
89 KFileMetaData::UserMetaData
md(file
.path());
90 if (roles
.contains("tags")) {
91 values
.insert("tags", tagsFromValues(md
.tags()));
93 if (roles
.contains("rating")) {
94 values
.insert("rating", QString::number(md
.rating()));
96 if (roles
.contains("comment")) {
97 values
.insert("comment", md
.userComment());
99 if (roles
.contains("originUrl")) {
100 values
.insert("originUrl", md
.originUrl());
106 QByteArray
KBalooRolesProvider::roleForProperty(const QString
& property
) const
108 return m_roleForProperty
.value(property
);
111 KBalooRolesProvider::KBalooRolesProvider() :
117 const char* const property
;
118 const char* const role
;
121 // Mapping from the URIs to the KFileItemModel roles. Note that this must not be
122 // a 1:1 mapping: One role may contain several URI-values
123 static const PropertyInfo propertyInfoList
[] = {
124 { "rating", "rating" },
126 { "comment", "comment" },
127 { "title", "title" },
128 { "wordCount", "wordCount" },
129 { "lineCount", "lineCount" },
130 { "width", "width" },
131 { "height", "height" },
132 { "imageDateTime", "imageDateTime"},
133 { "imageOrientation", "orientation", },
134 { "artist", "artist" },
135 { "genre", "genre" },
136 { "album", "album" },
137 { "duration", "duration" },
138 { "bitRate", "bitrate" },
139 { "aspectRatio", "aspectRatio" },
140 { "frameRate", "frameRate" },
141 { "releaseYear", "releaseYear" },
142 { "trackNumber", "track" },
143 { "originUrl", "originUrl" }
146 for (unsigned int i
= 0; i
< sizeof(propertyInfoList
) / sizeof(PropertyInfo
); ++i
) {
147 m_roleForProperty
.insert(propertyInfoList
[i
].property
, propertyInfoList
[i
].role
);
148 m_roles
.insert(propertyInfoList
[i
].role
);
152 QString
KBalooRolesProvider::tagsFromValues(const QStringList
& values
) const
154 QStringList alphabeticalOrderTags
= values
;
156 coll
.setNumericMode(true);
157 std::sort(alphabeticalOrderTags
.begin(), alphabeticalOrderTags
.end(), [&](const QString
& s1
, const QString
& s2
){ return coll
.compare(s1
, s2
) < 0; });
158 return alphabeticalOrderTags
.join(QStringLiteral(", "));