]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kbaloorolesprovider.cpp
f2473de3af2f6e886f48656488e42e42242f273c
[dolphin.git] / src / kitemviews / private / kbaloorolesprovider.cpp
1 /*
2 * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2013 Vishesh Handa <me@vhanda.in>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8 #include "kbaloorolesprovider.h"
9
10 #include <Baloo/File>
11 #include <KFileMetaData/PropertyInfo>
12 #include <KFileMetaData/UserMetaData>
13
14 #include <QCollator>
15 #include <QDebug>
16 #include <QTime>
17
18 struct KBalooRolesProviderSingleton
19 {
20 KBalooRolesProvider instance;
21 };
22 Q_GLOBAL_STATIC(KBalooRolesProviderSingleton, s_balooRolesProvider)
23
24
25 KBalooRolesProvider& KBalooRolesProvider::instance()
26 {
27 return s_balooRolesProvider->instance;
28 }
29
30 KBalooRolesProvider::~KBalooRolesProvider()
31 {
32 }
33
34 QSet<QByteArray> KBalooRolesProvider::roles() const
35 {
36 return m_roles;
37 }
38
39 QHash<QByteArray, QVariant> KBalooRolesProvider::roleValues(const Baloo::File& file,
40 const QSet<QByteArray>& roles) const
41 {
42 QHash<QByteArray, QVariant> values;
43
44 using entry = std::pair<const KFileMetaData::Property::Property&, const QVariant&>;
45
46 const auto& propMap = file.properties();
47 auto rangeBegin = propMap.constKeyValueBegin();
48
49 while (rangeBegin != propMap.constKeyValueEnd()) {
50 auto key = (*rangeBegin).first;
51 const KFileMetaData::PropertyInfo propertyInfo(key);
52 const QByteArray role = roleForProperty(propertyInfo.name());
53
54 auto rangeEnd = std::find_if(rangeBegin, propMap.constKeyValueEnd(),
55 [key](const entry& e) { return e.first != key; });
56
57 if (role.isEmpty() || !roles.contains(role)) {
58 rangeBegin = rangeEnd;
59 continue;
60 }
61
62 auto distance = std::distance(rangeBegin, rangeEnd);
63 if (distance > 1) {
64 QVariantList list;
65 list.reserve(static_cast<int>(distance));
66 std::for_each(rangeBegin, rangeEnd, [&list](const entry& s) { list.append(s.second); });
67 values.insert(role, propertyInfo.formatAsDisplayString(list));
68 } else {
69 if (propertyInfo.valueType() == QVariant::DateTime) {
70 // Let dolphin format later Dates
71 values.insert(role, (*rangeBegin).second);
72 } else {
73 values.insert(role, propertyInfo.formatAsDisplayString((*rangeBegin).second));
74 }
75 }
76 rangeBegin = rangeEnd;
77 }
78
79 KFileMetaData::UserMetaData md(file.path());
80 if (roles.contains("tags")) {
81 values.insert("tags", tagsFromValues(md.tags()));
82 }
83 if (roles.contains("rating")) {
84 values.insert("rating", QString::number(md.rating()));
85 }
86 if (roles.contains("comment")) {
87 values.insert("comment", md.userComment());
88 }
89 if (roles.contains("originUrl")) {
90 values.insert("originUrl", md.originUrl());
91 }
92
93 return values;
94 }
95
96 QByteArray KBalooRolesProvider::roleForProperty(const QString& property) const
97 {
98 return m_roleForProperty.value(property);
99 }
100
101 KBalooRolesProvider::KBalooRolesProvider() :
102 m_roles(),
103 m_roleForProperty()
104 {
105 struct PropertyInfo
106 {
107 const char* const property;
108 const char* const role;
109 };
110
111 // Mapping from the URIs to the KFileItemModel roles. Note that this must not be
112 // a 1:1 mapping: One role may contain several URI-values
113 static const PropertyInfo propertyInfoList[] = {
114 { "rating", "rating" },
115 { "tag", "tags" },
116 { "comment", "comment" },
117 { "title", "title" },
118 { "wordCount", "wordCount" },
119 { "lineCount", "lineCount" },
120 { "width", "width" },
121 { "height", "height" },
122 { "imageDateTime", "imageDateTime"},
123 { "imageOrientation", "orientation", },
124 { "artist", "artist" },
125 { "genre", "genre" },
126 { "album", "album" },
127 { "duration", "duration" },
128 { "bitRate", "bitrate" },
129 { "aspectRatio", "aspectRatio" },
130 { "frameRate", "frameRate" },
131 { "releaseYear", "releaseYear" },
132 { "trackNumber", "track" },
133 { "originUrl", "originUrl" }
134 };
135
136 for (unsigned int i = 0; i < sizeof(propertyInfoList) / sizeof(PropertyInfo); ++i) {
137 m_roleForProperty.insert(propertyInfoList[i].property, propertyInfoList[i].role);
138 m_roles.insert(propertyInfoList[i].role);
139 }
140 }
141
142 QString KBalooRolesProvider::tagsFromValues(const QStringList& values) const
143 {
144 QStringList alphabeticalOrderTags = values;
145 QCollator coll;
146 coll.setNumericMode(true);
147 std::sort(alphabeticalOrderTags.begin(), alphabeticalOrderTags.end(), [&](const QString& s1, const QString& s2){ return coll.compare(s1, s2) < 0; });
148 return alphabeticalOrderTags.join(QLatin1String(", "));
149 }