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 <KLocalizedString>
27 #include <KFileMetaData/PropertyInfo>
28 #include <KFileMetaData/UserMetaData>
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
;
62 QMapIterator
<KFileMetaData::Property::Property
, QVariant
> it(file
.properties());
63 while (it
.hasNext()) {
66 const KFileMetaData::PropertyInfo
pi(it
.key());
67 const QString property
= pi
.name();
68 const QByteArray role
= roleForProperty(property
);
69 if (role
.isEmpty() || !roles
.contains(role
)) {
73 const QVariant value
= it
.value();
75 if (role
== "imageSize") {
76 // Merge the two properties for width and height
77 // as one string into the "imageSize" role
78 if (property
== QLatin1String("width")) {
79 width
= value
.toInt();
81 else if (property
== QLatin1String("height")) {
82 height
= value
.toInt();
85 if (width
>= 0 && height
>= 0) {
86 QString widthAndHeight
= QString::number(width
);
87 widthAndHeight
+= QLatin1String(" x ");
88 widthAndHeight
+= QString::number(height
);
89 values
.insert(role
, widthAndHeight
);
91 } else if (role
== "orientation") {
92 const QString orientation
= orientationFromValue(value
.toInt());
93 values
.insert(role
, orientation
);
94 } else if (role
== "duration") {
95 const QString duration
= durationFromValue(value
.toInt());
96 values
.insert(role
, duration
);
98 values
.insert(role
, value
.toString());
102 KFileMetaData::UserMetaData
md(file
.path());
103 if (roles
.contains("tags")) {
104 values
.insert("tags", tagsFromValues(md
.tags()));
106 if (roles
.contains("rating")) {
107 values
.insert("rating", QString::number(md
.rating()));
109 if (roles
.contains("comment")) {
110 values
.insert("comment", md
.userComment());
112 if (roles
.contains("originUrl")) {
113 values
.insert("originUrl", md
.originUrl());
119 QByteArray
KBalooRolesProvider::roleForProperty(const QString
& property
) const
121 return m_roleForProperty
.value(property
);
124 KBalooRolesProvider::KBalooRolesProvider() :
130 const char* const property
;
131 const char* const role
;
134 // Mapping from the URIs to the KFileItemModel roles. Note that this must not be
135 // a 1:1 mapping: One role may contain several URI-values (e.g. the URIs for height and
136 // width of an image are mapped to the role "imageSize")
137 static const PropertyInfo propertyInfoList
[] = {
138 { "rating", "rating" },
140 { "comment", "comment" },
141 { "title", "title" },
142 { "wordCount", "wordCount" },
143 { "lineCount", "lineCount" },
144 { "width", "imageSize" },
145 { "height", "imageSize" },
146 { "nexif.orientation", "orientation", },
147 { "artist", "artist" },
148 { "album", "album" },
149 { "duration", "duration" },
150 { "trackNumber", "track" },
151 { "originUrl", "originUrl" }
154 for (unsigned int i
= 0; i
< sizeof(propertyInfoList
) / sizeof(PropertyInfo
); ++i
) {
155 m_roleForProperty
.insert(propertyInfoList
[i
].property
, propertyInfoList
[i
].role
);
156 m_roles
.insert(propertyInfoList
[i
].role
);
160 QString
KBalooRolesProvider::tagsFromValues(const QStringList
& values
) const
162 return values
.join(QStringLiteral(", "));
165 QString
KBalooRolesProvider::orientationFromValue(int value
) const
169 case 1: string
= i18nc("@item:intable Image orientation", "Unchanged"); break;
170 case 2: string
= i18nc("@item:intable Image orientation", "Horizontally flipped"); break;
171 case 3: string
= i18nc("@item:intable image orientation", "180° rotated"); break;
172 case 4: string
= i18nc("@item:intable image orientation", "Vertically flipped"); break;
173 case 5: string
= i18nc("@item:intable image orientation", "Transposed"); break;
174 case 6: string
= i18nc("@item:intable image orientation", "90° rotated"); break;
175 case 7: string
= i18nc("@item:intable image orientation", "Transversed"); break;
176 case 8: string
= i18nc("@item:intable image orientation", "270° rotated"); break;
183 QString
KBalooRolesProvider::durationFromValue(int value
) const
185 QTime
duration(0, 0, 0);
186 duration
= duration
.addSecs(value
);
187 return duration
.toString(QStringLiteral("hh:mm:ss"));