]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kbaloorolesprovider.cpp
Fix untranslated spinbox suffix strings
[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 using entry = std::pair<const KFileMetaData::Property::Property&, const QVariant&>;
60
61 const auto& propMap = file.properties();
62 auto rangeBegin = propMap.constKeyValueBegin();
63
64 while (rangeBegin != propMap.constKeyValueEnd()) {
65 auto key = (*rangeBegin).first;
66 const KFileMetaData::PropertyInfo propertyInfo(key);
67 const QByteArray role = roleForProperty(propertyInfo.name());
68
69 auto rangeEnd = std::find_if(rangeBegin, propMap.constKeyValueEnd(),
70 [key](const entry& e) { return e.first != key; });
71
72 if (role.isEmpty() || !roles.contains(role)) {
73 rangeBegin = rangeEnd;
74 continue;
75 }
76
77 auto distance = std::distance(rangeBegin, rangeEnd);
78 if (distance > 1) {
79 QVariantList list;
80 list.reserve(static_cast<int>(distance));
81 std::for_each(rangeBegin, rangeEnd, [&list](const entry& s) { list.append(s.second); });
82 values.insert(role, propertyInfo.formatAsDisplayString(list));
83 } else {
84 if (propertyInfo.valueType() == QVariant::DateTime) {
85 // Let dolphin format later Dates
86 values.insert(role, (*rangeBegin).second);
87 } else {
88 values.insert(role, propertyInfo.formatAsDisplayString((*rangeBegin).second));
89 }
90 }
91 rangeBegin = rangeEnd;
92 }
93
94 KFileMetaData::UserMetaData md(file.path());
95 if (roles.contains("tags")) {
96 values.insert("tags", tagsFromValues(md.tags()));
97 }
98 if (roles.contains("rating")) {
99 values.insert("rating", QString::number(md.rating()));
100 }
101 if (roles.contains("comment")) {
102 values.insert("comment", md.userComment());
103 }
104 if (roles.contains("originUrl")) {
105 values.insert("originUrl", md.originUrl());
106 }
107
108 return values;
109 }
110
111 QByteArray KBalooRolesProvider::roleForProperty(const QString& property) const
112 {
113 return m_roleForProperty.value(property);
114 }
115
116 KBalooRolesProvider::KBalooRolesProvider() :
117 m_roles(),
118 m_roleForProperty()
119 {
120 struct PropertyInfo
121 {
122 const char* const property;
123 const char* const role;
124 };
125
126 // Mapping from the URIs to the KFileItemModel roles. Note that this must not be
127 // a 1:1 mapping: One role may contain several URI-values
128 static const PropertyInfo propertyInfoList[] = {
129 { "rating", "rating" },
130 { "tag", "tags" },
131 { "comment", "comment" },
132 { "title", "title" },
133 { "wordCount", "wordCount" },
134 { "lineCount", "lineCount" },
135 { "width", "width" },
136 { "height", "height" },
137 { "imageDateTime", "imageDateTime"},
138 { "imageOrientation", "orientation", },
139 { "artist", "artist" },
140 { "genre", "genre" },
141 { "album", "album" },
142 { "duration", "duration" },
143 { "bitRate", "bitrate" },
144 { "aspectRatio", "aspectRatio" },
145 { "frameRate", "frameRate" },
146 { "releaseYear", "releaseYear" },
147 { "trackNumber", "track" },
148 { "originUrl", "originUrl" }
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 QStringList alphabeticalOrderTags = values;
160 QCollator coll;
161 coll.setNumericMode(true);
162 std::sort(alphabeticalOrderTags.begin(), alphabeticalOrderTags.end(), [&](const QString& s1, const QString& s2){ return coll.compare(s1, s2) < 0; });
163 return alphabeticalOrderTags.join(QLatin1String(", "));
164 }