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 if (propertyInfo
.valueType() == QVariant::DateTime
) {
85 // Let dolphin format later Dates
86 values
.insert(role
, (*rangeBegin
).second
);
88 values
.insert(role
, propertyInfo
.formatAsDisplayString((*rangeBegin
).second
));
91 rangeBegin
= rangeEnd
;
94 KFileMetaData::UserMetaData
md(file
.path());
95 if (roles
.contains("tags")) {
96 values
.insert("tags", tagsFromValues(md
.tags()));
98 if (roles
.contains("rating")) {
99 values
.insert("rating", QString::number(md
.rating()));
101 if (roles
.contains("comment")) {
102 values
.insert("comment", md
.userComment());
104 if (roles
.contains("originUrl")) {
105 values
.insert("originUrl", md
.originUrl());
111 QByteArray
KBalooRolesProvider::roleForProperty(const QString
& property
) const
113 return m_roleForProperty
.value(property
);
116 KBalooRolesProvider::KBalooRolesProvider() :
122 const char* const property
;
123 const char* const role
;
126 // Mapping from the URIs to the KFileItemModel roles. Note that this must not be
127 // a 1:1 mapping: One role may contain several URI-values
128 static const PropertyInfo propertyInfoList
[] = {
129 { "rating", "rating" },
131 { "comment", "comment" },
132 { "title", "title" },
133 { "wordCount", "wordCount" },
134 { "lineCount", "lineCount" },
135 { "width", "width" },
136 { "height", "height" },
137 { "imageDateTime", "imageDateTime"},
138 { "imageOrientation", "orientation", },
139 { "artist", "artist" },
140 { "genre", "genre" },
141 { "album", "album" },
142 { "duration", "duration" },
143 { "bitRate", "bitrate" },
144 { "aspectRatio", "aspectRatio" },
145 { "frameRate", "frameRate" },
146 { "releaseYear", "releaseYear" },
147 { "trackNumber", "track" },
148 { "originUrl", "originUrl" }
151 for (unsigned int i
= 0; i
< sizeof(propertyInfoList
) / sizeof(PropertyInfo
); ++i
) {
152 m_roleForProperty
.insert(propertyInfoList
[i
].property
, propertyInfoList
[i
].role
);
153 m_roles
.insert(propertyInfoList
[i
].role
);
157 QString
KBalooRolesProvider::tagsFromValues(const QStringList
& values
) const
159 QStringList alphabeticalOrderTags
= values
;
161 coll
.setNumericMode(true);
162 std::sort(alphabeticalOrderTags
.begin(), alphabeticalOrderTags
.end(), [&](const QString
& s1
, const QString
& s2
){ return coll
.compare(s1
, s2
) < 0; });
163 return alphabeticalOrderTags
.join(QLatin1String(", "));