]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kbaloorolesprovider.cpp
Split Image Size into Width/Height
[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 <Baloo/File>
24 #include <KFileMetaData/PropertyInfo>
25 #include <KFileMetaData/UserMetaData>
26 #include <KFormat>
27 #include <KLocalizedString>
28
29 #include <QCollator>
30 #include <QDebug>
31 #include <QTime>
32
33 struct KBalooRolesProviderSingleton
34 {
35 KBalooRolesProvider instance;
36 };
37 Q_GLOBAL_STATIC(KBalooRolesProviderSingleton, s_balooRolesProvider)
38
39
40 KBalooRolesProvider& KBalooRolesProvider::instance()
41 {
42 return s_balooRolesProvider->instance;
43 }
44
45 KBalooRolesProvider::~KBalooRolesProvider()
46 {
47 }
48
49 QSet<QByteArray> KBalooRolesProvider::roles() const
50 {
51 return m_roles;
52 }
53
54 QHash<QByteArray, QVariant> KBalooRolesProvider::roleValues(const Baloo::File& file,
55 const QSet<QByteArray>& roles) const
56 {
57 QHash<QByteArray, QVariant> values;
58
59 QMapIterator<KFileMetaData::Property::Property, QVariant> it(file.properties());
60 while (it.hasNext()) {
61 it.next();
62
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)) {
67 continue;
68 }
69
70 const QVariant value = it.value();
71
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 {
82 values.insert(role, value.toString());
83 }
84 }
85
86 KFileMetaData::UserMetaData md(file.path());
87 if (roles.contains("tags")) {
88 values.insert("tags", tagsFromValues(md.tags()));
89 }
90 if (roles.contains("rating")) {
91 values.insert("rating", QString::number(md.rating()));
92 }
93 if (roles.contains("comment")) {
94 values.insert("comment", md.userComment());
95 }
96 if (roles.contains("originUrl")) {
97 values.insert("originUrl", md.originUrl());
98 }
99
100 return values;
101 }
102
103 QByteArray KBalooRolesProvider::roleForProperty(const QString& property) const
104 {
105 return m_roleForProperty.value(property);
106 }
107
108 KBalooRolesProvider::KBalooRolesProvider() :
109 m_roles(),
110 m_roleForProperty()
111 {
112 struct PropertyInfo
113 {
114 const char* const property;
115 const char* const role;
116 };
117
118 // Mapping from the URIs to the KFileItemModel roles. Note that this must not be
119 // a 1:1 mapping: One role may contain several URI-values
120 static const PropertyInfo propertyInfoList[] = {
121 { "rating", "rating" },
122 { "tag", "tags" },
123 { "comment", "comment" },
124 { "title", "title" },
125 { "wordCount", "wordCount" },
126 { "lineCount", "lineCount" },
127 { "width", "width" },
128 { "height", "height" },
129 { "imageDateTime", "imageDateTime"},
130 { "nexif.orientation", "orientation", },
131 { "artist", "artist" },
132 { "genre", "genre" },
133 { "album", "album" },
134 { "duration", "duration" },
135 { "bitRate", "bitrate" },
136 { "releaseYear", "releaseYear" },
137 { "trackNumber", "track" },
138 { "originUrl", "originUrl" }
139 };
140
141 for (unsigned int i = 0; i < sizeof(propertyInfoList) / sizeof(PropertyInfo); ++i) {
142 m_roleForProperty.insert(propertyInfoList[i].property, propertyInfoList[i].role);
143 m_roles.insert(propertyInfoList[i].role);
144 }
145 }
146
147 QString KBalooRolesProvider::tagsFromValues(const QStringList& values) const
148 {
149 QStringList alphabeticalOrderTags = values;
150 QCollator coll;
151 coll.setNumericMode(true);
152 std::sort(alphabeticalOrderTags.begin(), alphabeticalOrderTags.end(), [&](const QString& s1, const QString& s2){ return coll.compare(s1, s2) < 0; });
153 return alphabeticalOrderTags.join(QStringLiteral(", "));
154 }
155
156 QString KBalooRolesProvider::orientationFromValue(int value) const
157 {
158 QString string;
159 switch (value) {
160 case 1: string = i18nc("@item:intable Image orientation", "Unchanged"); break;
161 case 2: string = i18nc("@item:intable Image orientation", "Horizontally flipped"); break;
162 case 3: string = i18nc("@item:intable image orientation", "180° rotated"); break;
163 case 4: string = i18nc("@item:intable image orientation", "Vertically flipped"); break;
164 case 5: string = i18nc("@item:intable image orientation", "Transposed"); break;
165 case 6: string = i18nc("@item:intable image orientation", "90° rotated"); break;
166 case 7: string = i18nc("@item:intable image orientation", "Transversed"); break;
167 case 8: string = i18nc("@item:intable image orientation", "270° rotated"); break;
168 default:
169 break;
170 }
171 return string;
172 }
173
174 QString KBalooRolesProvider::durationFromValue(int value) const
175 {
176 QTime duration(0, 0, 0);
177 duration = duration.addSecs(value);
178 return duration.toString(QStringLiteral("hh:mm:ss"));
179 }
180
181
182 QString KBalooRolesProvider::bitrateFromValue(int value) const
183 {
184 KFormat form;
185 QString bitrate = i18nc("@label bitrate (per second)", "%1/s", form.formatByteSize(value, 1, KFormat::MetricBinaryDialect));
186 return bitrate;
187 }
188