]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/kloadmetadatathread.cpp
SVN_SILENT made messages (.desktop file)
[dolphin.git] / src / panels / information / kloadmetadatathread.cpp
1 /*****************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2009 by Sebastian Trueg <trueg@kde.org> *
4 * *
5 * This library is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU Library General Public *
7 * License version 2 as published by the Free Software Foundation. *
8 * *
9 * This library is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Library General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Library General Public License *
15 * along with this library; see the file COPYING.LIB. If not, write to *
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301, USA. *
18 *****************************************************************************/
19
20 #include "kloadmetadatathread_p.h"
21
22 #include <kconfig.h>
23 #include <kconfiggroup.h>
24 #include <kglobal.h>
25 #include <klocale.h>
26
27 #include <nepomuk/variant.h>
28 #include <nepomuk/resource.h>
29
30 KLoadMetaDataThread::KLoadMetaDataThread() :
31 m_rating(0),
32 m_comment(),
33 m_tags(),
34 m_metaInfoLabels(),
35 m_metaInfoValues(),
36 m_files(),
37 m_urls(),
38 m_canceled(false)
39 {
40 }
41
42 KLoadMetaDataThread::~KLoadMetaDataThread()
43 {
44 }
45
46 void KLoadMetaDataThread::load(const KUrl::List& urls)
47 {
48 m_urls = urls;
49 m_canceled = false;
50 start();
51 }
52
53 void KLoadMetaDataThread::cancelAndDelete()
54 {
55 connect(this, SIGNAL(finished()), this, SLOT(slotFinished()));
56 m_canceled = true;
57 // Setting m_canceled to true will cancel KLoadMetaDataThread::run()
58 // as soon as possible. Afterwards the thread will delete itself
59 // asynchronously inside slotFinished().
60 }
61
62 void KLoadMetaDataThread::run()
63 {
64 KConfig config("kmetainformationrc", KConfig::NoGlobals);
65 KConfigGroup settings = config.group("Show");
66
67 bool first = true;
68 unsigned int rating = 0;
69 foreach (const KUrl& url, m_urls) {
70 if (m_canceled) {
71 return;
72 }
73
74 Nepomuk::Resource file(url);
75 m_files.insert(url, file);
76
77 if (!first && (rating != file.rating())) {
78 rating = 0; // reset rating
79 } else if (first) {
80 rating = file.rating();
81 }
82
83 if (!first && (m_comment != file.description())) {
84 m_comment.clear(); // reset comment
85 } else if (first) {
86 m_comment = file.description();
87 }
88
89 if (!first && (m_tags != file.tags())) {
90 m_tags.clear(); // reset tags
91 } else if (first) {
92 m_tags = file.tags();
93 }
94
95 if (first && (m_urls.count() == 1)) {
96 // TODO: show shared meta information instead
97 // of not showing anything on multiple selections
98 QHash<QUrl, Nepomuk::Variant> properties = file.properties();
99 QHash<QUrl, Nepomuk::Variant>::const_iterator it = properties.constBegin();
100 while (it != properties.constEnd()) {
101 Nepomuk::Types::Property prop(it.key());
102 if (settings.readEntry(prop.name(), true)) {
103 // TODO #1: use Nepomuk::formatValue(res, prop) if available
104 // instead of it.value().toString()
105 // TODO #2: using tunedLabel() is a workaround for KDE 4.3 (4.4?) until
106 // we get translated labels
107 m_metaInfoLabels.append(tunedLabel(prop.label()));
108 m_metaInfoValues.append(formatValue(it.value()));
109 }
110 ++it;
111 }
112 }
113
114 first = false;
115 }
116 }
117
118 int KLoadMetaDataThread::rating() const
119 {
120 return m_rating;
121 }
122
123 QString KLoadMetaDataThread::comment() const
124 {
125 return m_comment;
126 }
127
128 QList<Nepomuk::Tag> KLoadMetaDataThread::tags() const
129 {
130 return m_tags;
131 }
132
133 QList<QString> KLoadMetaDataThread::metaInfoLabels() const
134 {
135 return m_metaInfoLabels;
136 }
137
138 QList<QString> KLoadMetaDataThread::metaInfoValues() const
139 {
140 return m_metaInfoValues;
141 }
142
143 QMap<KUrl, Nepomuk::Resource> KLoadMetaDataThread::files() const
144 {
145 return m_files;
146 }
147
148 void KLoadMetaDataThread::slotFinished()
149 {
150 deleteLater();
151 }
152
153 QString KLoadMetaDataThread::tunedLabel(const QString& label) const
154 {
155 QString tunedLabel;
156 const int labelLength = label.length();
157 if (labelLength > 0) {
158 tunedLabel.reserve(labelLength);
159 tunedLabel = label[0].toUpper();
160 for (int i = 1; i < labelLength; ++i) {
161 if (label[i].isUpper() && !label[i - 1].isSpace() && !label[i - 1].isUpper()) {
162 tunedLabel += ' ';
163 tunedLabel += label[i].toLower();
164 } else {
165 tunedLabel += label[i];
166 }
167 }
168 }
169 return tunedLabel + ':';
170 }
171
172
173 // This is a short hack until we have a proper formatting facility in Nepomuk
174 // here we simply handle the most common formatting situations that do not look nice
175 // when using Nepomuk::Variant::toString()
176 QString KLoadMetaDataThread::formatValue(const Nepomuk::Variant& value)
177 {
178 if (value.isDateTime()) {
179 return KGlobal::locale()->formatDateTime( value.toDateTime(), KLocale::FancyLongDate );
180 }
181 else if (value.isResource()) {
182 return value.toResource().genericLabel();
183 }
184 else if (value.isResourceList()) {
185 QStringList ll;
186 foreach(const Nepomuk::Resource& res, value.toResourceList()) {
187 ll << res.genericLabel();
188 }
189 return ll.join(QLatin1String(";\n"));
190 }
191 else {
192 return value.toString();
193 }
194 }
195
196 #include "kloadmetadatathread_p.moc"