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"
25 #include <KLocalizedString>
28 #include <KFileMetaData/PropertyInfo>
29 #include <KFileMetaData/UserMetaData>
34 struct KBalooRolesProviderSingleton
36 KBalooRolesProvider instance
;
38 Q_GLOBAL_STATIC(KBalooRolesProviderSingleton
, s_balooRolesProvider
)
41 KBalooRolesProvider
& KBalooRolesProvider::instance()
43 return s_balooRolesProvider
->instance
;
46 KBalooRolesProvider::~KBalooRolesProvider()
50 QSet
<QByteArray
> KBalooRolesProvider::roles() const
55 QHash
<QByteArray
, QVariant
> KBalooRolesProvider::roleValues(const Baloo::File
& file
,
56 const QSet
<QByteArray
>& roles
) const
58 QHash
<QByteArray
, QVariant
> values
;
63 QMapIterator
<KFileMetaData::Property::Property
, QVariant
> it(file
.properties());
64 while (it
.hasNext()) {
67 const KFileMetaData::PropertyInfo
pi(it
.key());
68 const QString property
= pi
.name();
69 const QByteArray role
= roleForProperty(property
);
70 if (role
.isEmpty() || !roles
.contains(role
)) {
74 const QVariant value
= it
.value();
76 if (role
== "imageSize") {
77 // Merge the two properties for width and height
78 // as one string into the "imageSize" role
79 if (property
== QLatin1String("width")) {
80 width
= value
.toInt();
82 else if (property
== QLatin1String("height")) {
83 height
= value
.toInt();
86 if (width
>= 0 && height
>= 0) {
87 QString widthAndHeight
= QString::number(width
);
88 widthAndHeight
+= QLatin1String(" x ");
89 widthAndHeight
+= QString::number(height
);
90 values
.insert(role
, widthAndHeight
);
92 } else if (role
== "orientation") {
93 const QString orientation
= orientationFromValue(value
.toInt());
94 values
.insert(role
, orientation
);
95 } else if (role
== "duration") {
96 const QString duration
= durationFromValue(value
.toInt());
97 values
.insert(role
, duration
);
99 values
.insert(role
, value
.toString());
103 KFileMetaData::UserMetaData
md(file
.path());
104 if (roles
.contains("tags")) {
105 values
.insert("tags", tagsFromValues(md
.tags()));
107 if (roles
.contains("rating")) {
108 values
.insert("rating", QString::number(md
.rating()));
110 if (roles
.contains("comment")) {
111 values
.insert("comment", md
.userComment());
117 QByteArray
KBalooRolesProvider::roleForProperty(const QString
& property
) const
119 return m_roleForProperty
.value(property
);
122 KBalooRolesProvider::KBalooRolesProvider() :
128 const char* const property
;
129 const char* const role
;
132 // Mapping from the URIs to the KFileItemModel roles. Note that this must not be
133 // a 1:1 mapping: One role may contain several URI-values (e.g. the URIs for height and
134 // width of an image are mapped to the role "imageSize")
135 static const PropertyInfo propertyInfoList
[] = {
136 { "rating", "rating" },
138 { "comment", "comment" },
139 { "wordCount", "wordCount" },
140 { "lineCount", "lineCount" },
141 { "width", "imageSize" },
142 { "height", "imageSize" },
143 { "nexif.orientation", "orientation", },
144 { "artist", "artist" },
145 { "album", "album" },
146 { "duration", "duration" },
147 { "trackNumber", "track" }
148 // { "http://www.semanticdesktop.org/ontologies/2010/04/30/ndo#copiedFrom", "copiedFrom" }
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 return values
.join(", ");
162 QString
KBalooRolesProvider::orientationFromValue(int value
) const
166 case 1: string
= i18nc("@item:intable Image orientation", "Unchanged"); break;
167 case 2: string
= i18nc("@item:intable Image orientation", "Horizontally flipped"); break;
168 case 3: string
= i18nc("@item:intable image orientation", "180° rotated"); break;
169 case 4: string
= i18nc("@item:intable image orientation", "Vertically flipped"); break;
170 case 5: string
= i18nc("@item:intable image orientation", "Transposed"); break;
171 case 6: string
= i18nc("@item:intable image orientation", "90° rotated"); break;
172 case 7: string
= i18nc("@item:intable image orientation", "Transversed"); break;
173 case 8: string
= i18nc("@item:intable image orientation", "270° rotated"); break;
180 QString
KBalooRolesProvider::durationFromValue(int value
) const
183 duration
= duration
.addSecs(value
);
184 return duration
.toString("hh:mm:ss");