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 QMapIterator
<KFileMetaData::Property::Property
, QVariant
> it(file
.properties());
60 while (it
.hasNext()) {
63 const KFileMetaData::PropertyInfo
pi(it
.key());
64 const QString property
= pi
.name();
65 const QByteArray role
= roleForProperty(property
);
66 if (role
.isEmpty() || !roles
.contains(role
)) {
70 const QVariant value
= it
.value();
72 if (role
== "orientation") {
73 const QString orientation
= orientationFromValue(value
.toInt());
74 values
.insert(role
, orientation
);
75 } else if (role
== "duration") {
76 const QString duration
= durationFromValue(value
.toInt());
77 values
.insert(role
, duration
);
78 } else if (role
== "bitrate") {
79 const QString bitrate
= bitrateFromValue(value
.toInt());
80 values
.insert(role
, bitrate
);
81 } else if (pi
.valueType() == QVariant::StringList
) {
82 values
.insert(role
, value
.toStringList().join(QStringLiteral(", ")));
84 values
.insert(role
, value
.toString());
88 KFileMetaData::UserMetaData
md(file
.path());
89 if (roles
.contains("tags")) {
90 values
.insert("tags", tagsFromValues(md
.tags()));
92 if (roles
.contains("rating")) {
93 values
.insert("rating", QString::number(md
.rating()));
95 if (roles
.contains("comment")) {
96 values
.insert("comment", md
.userComment());
98 if (roles
.contains("originUrl")) {
99 values
.insert("originUrl", md
.originUrl());
105 QByteArray
KBalooRolesProvider::roleForProperty(const QString
& property
) const
107 return m_roleForProperty
.value(property
);
110 KBalooRolesProvider::KBalooRolesProvider() :
116 const char* const property
;
117 const char* const role
;
120 // Mapping from the URIs to the KFileItemModel roles. Note that this must not be
121 // a 1:1 mapping: One role may contain several URI-values
122 static const PropertyInfo propertyInfoList
[] = {
123 { "rating", "rating" },
125 { "comment", "comment" },
126 { "title", "title" },
127 { "wordCount", "wordCount" },
128 { "lineCount", "lineCount" },
129 { "width", "width" },
130 { "height", "height" },
131 { "imageDateTime", "imageDateTime"},
132 { "nexif.orientation", "orientation", },
133 { "artist", "artist" },
134 { "genre", "genre" },
135 { "album", "album" },
136 { "duration", "duration" },
137 { "bitRate", "bitrate" },
138 { "releaseYear", "releaseYear" },
139 { "trackNumber", "track" },
140 { "originUrl", "originUrl" }
143 for (unsigned int i
= 0; i
< sizeof(propertyInfoList
) / sizeof(PropertyInfo
); ++i
) {
144 m_roleForProperty
.insert(propertyInfoList
[i
].property
, propertyInfoList
[i
].role
);
145 m_roles
.insert(propertyInfoList
[i
].role
);
149 QString
KBalooRolesProvider::tagsFromValues(const QStringList
& values
) const
151 QStringList alphabeticalOrderTags
= values
;
153 coll
.setNumericMode(true);
154 std::sort(alphabeticalOrderTags
.begin(), alphabeticalOrderTags
.end(), [&](const QString
& s1
, const QString
& s2
){ return coll
.compare(s1
, s2
) < 0; });
155 return alphabeticalOrderTags
.join(QStringLiteral(", "));
158 QString
KBalooRolesProvider::orientationFromValue(int value
) const
162 case 1: string
= i18nc("@item:intable Image orientation", "Unchanged"); break;
163 case 2: string
= i18nc("@item:intable Image orientation", "Horizontally flipped"); break;
164 case 3: string
= i18nc("@item:intable image orientation", "180° rotated"); break;
165 case 4: string
= i18nc("@item:intable image orientation", "Vertically flipped"); break;
166 case 5: string
= i18nc("@item:intable image orientation", "Transposed"); break;
167 case 6: string
= i18nc("@item:intable image orientation", "90° rotated"); break;
168 case 7: string
= i18nc("@item:intable image orientation", "Transversed"); break;
169 case 8: string
= i18nc("@item:intable image orientation", "270° rotated"); break;
176 QString
KBalooRolesProvider::durationFromValue(int value
) const
178 QTime
duration(0, 0, 0);
179 duration
= duration
.addSecs(value
);
180 return duration
.toString(QStringLiteral("hh:mm:ss"));
184 QString
KBalooRolesProvider::bitrateFromValue(int value
) const
187 QString bitrate
= i18nc("@label bitrate (per second)", "%1/s", form
.formatByteSize(value
, 1, KFormat::MetricBinaryDialect
));