]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kbaloorolesprovider.cpp
e7d18a81ad6e4b8bc76d3e52a79ff2e7427fcea1
[dolphin.git] / src / kitemviews / private / kbaloorolesprovider.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2013 by Vishesh Handa <me@vhanda.in> *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21 #include "kbaloorolesprovider.h"
22
23 #include <QDebug>
24 #include <KGlobal>
25 #include <KLocalizedString>
26
27 #include <Baloo/File>
28 #include <KFileMetaData/PropertyInfo>
29 #include <KFileMetaData/UserMetaData>
30
31 #include <QTime>
32 #include <QMap>
33
34 struct KBalooRolesProviderSingleton
35 {
36 KBalooRolesProvider instance;
37 };
38 Q_GLOBAL_STATIC(KBalooRolesProviderSingleton, s_balooRolesProvider)
39
40
41 KBalooRolesProvider& KBalooRolesProvider::instance()
42 {
43 return s_balooRolesProvider->instance;
44 }
45
46 KBalooRolesProvider::~KBalooRolesProvider()
47 {
48 }
49
50 QSet<QByteArray> KBalooRolesProvider::roles() const
51 {
52 return m_roles;
53 }
54
55 QHash<QByteArray, QVariant> KBalooRolesProvider::roleValues(const Baloo::File& file,
56 const QSet<QByteArray>& roles) const
57 {
58 QHash<QByteArray, QVariant> values;
59
60 int width = -1;
61 int height = -1;
62
63 QMapIterator<KFileMetaData::Property::Property, QVariant> it(file.properties());
64 while (it.hasNext()) {
65 it.next();
66
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)) {
71 continue;
72 }
73
74 const QVariant value = it.value();
75
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();
81 }
82 else if (property == QLatin1String("height")) {
83 height = value.toInt();
84 }
85
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);
91 }
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);
98 } else {
99 values.insert(role, value.toString());
100 }
101 }
102
103 KFileMetaData::UserMetaData md(file.path());
104 if (roles.contains("tags")) {
105 values.insert("tags", tagsFromValues(md.tags()));
106 }
107 if (roles.contains("rating")) {
108 values.insert("rating", QString::number(md.rating()));
109 }
110 if (roles.contains("comment")) {
111 values.insert("comment", md.userComment());
112 }
113
114 return values;
115 }
116
117 QByteArray KBalooRolesProvider::roleForProperty(const QString& property) const
118 {
119 return m_roleForProperty.value(property);
120 }
121
122 KBalooRolesProvider::KBalooRolesProvider() :
123 m_roles(),
124 m_roleForProperty()
125 {
126 struct PropertyInfo
127 {
128 const char* const property;
129 const char* const role;
130 };
131
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" },
137 { "tag", "tags" },
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" }
149 };
150
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);
154 }
155 }
156
157 QString KBalooRolesProvider::tagsFromValues(const QStringList& values) const
158 {
159 return values.join(", ");
160 }
161
162 QString KBalooRolesProvider::orientationFromValue(int value) const
163 {
164 QString string;
165 switch (value) {
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;
174 default:
175 break;
176 }
177 return string;
178 }
179
180 QString KBalooRolesProvider::durationFromValue(int value) const
181 {
182 QTime duration;
183 duration = duration.addSecs(value);
184 return duration.toString("hh:mm:ss");
185 }
186