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