]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kbaloorolesprovider.cpp
f3671540dd89c4e6d49fbfdfe18404d978d44c4b
[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 values.insert(role, propertyInfo.formatAsDisplayString((*rangeBegin).second));
85 }
86 rangeBegin = rangeEnd;
87 }
88
89 KFileMetaData::UserMetaData md(file.path());
90 if (roles.contains("tags")) {
91 values.insert("tags", tagsFromValues(md.tags()));
92 }
93 if (roles.contains("rating")) {
94 values.insert("rating", QString::number(md.rating()));
95 }
96 if (roles.contains("comment")) {
97 values.insert("comment", md.userComment());
98 }
99 if (roles.contains("originUrl")) {
100 values.insert("originUrl", md.originUrl());
101 }
102
103 return values;
104 }
105
106 QByteArray KBalooRolesProvider::roleForProperty(const QString& property) const
107 {
108 return m_roleForProperty.value(property);
109 }
110
111 KBalooRolesProvider::KBalooRolesProvider() :
112 m_roles(),
113 m_roleForProperty()
114 {
115 struct PropertyInfo
116 {
117 const char* const property;
118 const char* const role;
119 };
120
121 // Mapping from the URIs to the KFileItemModel roles. Note that this must not be
122 // a 1:1 mapping: One role may contain several URI-values
123 static const PropertyInfo propertyInfoList[] = {
124 { "rating", "rating" },
125 { "tag", "tags" },
126 { "comment", "comment" },
127 { "title", "title" },
128 { "wordCount", "wordCount" },
129 { "lineCount", "lineCount" },
130 { "width", "width" },
131 { "height", "height" },
132 { "imageDateTime", "imageDateTime"},
133 { "imageOrientation", "orientation", },
134 { "artist", "artist" },
135 { "genre", "genre" },
136 { "album", "album" },
137 { "duration", "duration" },
138 { "bitRate", "bitrate" },
139 { "aspectRatio", "aspectRatio" },
140 { "frameRate", "frameRate" },
141 { "releaseYear", "releaseYear" },
142 { "trackNumber", "track" },
143 { "originUrl", "originUrl" }
144 };
145
146 for (unsigned int i = 0; i < sizeof(propertyInfoList) / sizeof(PropertyInfo); ++i) {
147 m_roleForProperty.insert(propertyInfoList[i].property, propertyInfoList[i].role);
148 m_roles.insert(propertyInfoList[i].role);
149 }
150 }
151
152 QString KBalooRolesProvider::tagsFromValues(const QStringList& values) const
153 {
154 QStringList alphabeticalOrderTags = values;
155 QCollator coll;
156 coll.setNumericMode(true);
157 std::sort(alphabeticalOrderTags.begin(), alphabeticalOrderTags.end(), [&](const QString& s1, const QString& s2){ return coll.compare(s1, s2) < 0; });
158 return alphabeticalOrderTags.join(QStringLiteral(", "));
159 }