]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/kloadmetadatathread.cpp
Request the activation of the view, even if the column is marked as active already...
[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 <kfilemetainfo.h>
25 #include <kfilemetainfoitem.h>
26 #include <kglobal.h>
27 #include <klocale.h>
28 #include <kprotocolinfo.h>
29
30 #include <nepomuk/resource.h>
31
32 KLoadMetaDataThread::KLoadMetaDataThread() :
33 m_rating(0),
34 m_comment(),
35 m_tags(),
36 m_items(),
37 m_files(),
38 m_urls(),
39 m_canceled(false)
40 {
41 }
42
43 KLoadMetaDataThread::~KLoadMetaDataThread()
44 {
45 }
46
47 void KLoadMetaDataThread::load(const KUrl::List& urls)
48 {
49 m_urls = urls;
50 m_canceled = false;
51 start();
52 }
53
54 void KLoadMetaDataThread::cancelAndDelete()
55 {
56 connect(this, SIGNAL(finished()), this, SLOT(slotFinished()));
57 m_canceled = true;
58 // Setting m_canceled to true will cancel KLoadMetaDataThread::run()
59 // as soon as possible. Afterwards the thread will delete itself
60 // asynchronously inside slotFinished().
61 }
62
63 void KLoadMetaDataThread::run()
64 {
65 KConfig config("kmetainformationrc", KConfig::NoGlobals);
66 KConfigGroup settings = config.group("Show");
67
68 bool first = true;
69 foreach (const KUrl& url, m_urls) {
70 if (m_canceled) {
71 return;
72 }
73
74 Nepomuk::Resource file(url);
75 if (!file.isValid()) {
76 continue;
77 }
78
79 m_files.insert(url, file);
80
81 if (!first && (m_rating != file.rating())) {
82 m_rating = 0; // reset rating
83 } else if (first) {
84 m_rating = file.rating();
85 }
86
87 if (!first && (m_comment != file.description())) {
88 m_comment.clear(); // reset comment
89 } else if (first) {
90 m_comment = file.description();
91 }
92
93 if (!first && (m_tags != file.tags())) {
94 m_tags.clear(); // reset tags
95 } else if (first) {
96 m_tags = file.tags();
97 }
98
99 if (first && (m_urls.count() == 1)) {
100 // TODO: show shared meta information instead
101 // of not showing anything on multiple selections
102 QHash<QUrl, Nepomuk::Variant> variants = file.properties();
103 QHash<QUrl, Nepomuk::Variant>::const_iterator it = variants.constBegin();
104 while (it != variants.constEnd()) {
105 Nepomuk::Types::Property prop(it.key());
106 if (settings.readEntry(prop.name(), true)) {
107 Item item;
108 item.name = prop.name();
109 item.label = tunedLabel(prop.label());
110 item.value = formatValue(it.value());
111 m_items.append(item);
112 }
113 ++it;
114 }
115
116 if (variants.isEmpty()) {
117 // TODO: The following code is just meant as temporary fallback to show
118 // non-indexed meta data.
119 KFileMetaInfo metaInfo(m_urls.first());
120 const QHash<QString, KFileMetaInfoItem> metaInfoItems = metaInfo.items();
121 foreach (const KFileMetaInfoItem& metaInfoItem, metaInfoItems) {
122 Item item;
123 item.name = metaInfoItem.name();
124 item.label = metaInfoItem.name() + metaInfoItem.prefix() + metaInfoItem.suffix();
125 item.value = metaInfoItem.value().toString();
126 m_items.append(item);
127 }
128 }
129 }
130
131 first = false;
132 }
133 }
134
135 int KLoadMetaDataThread::rating() const
136 {
137 return m_rating;
138 }
139
140 QString KLoadMetaDataThread::comment() const
141 {
142 return m_comment;
143 }
144
145 QList<Nepomuk::Tag> KLoadMetaDataThread::tags() const
146 {
147 return m_tags;
148 }
149
150 QList<KLoadMetaDataThread::Item> KLoadMetaDataThread::items() const
151 {
152 return m_items;
153 }
154
155 QMap<KUrl, Nepomuk::Resource> KLoadMetaDataThread::files() const
156 {
157 return m_files;
158 }
159
160 void KLoadMetaDataThread::slotFinished()
161 {
162 deleteLater();
163 }
164
165 QString KLoadMetaDataThread::tunedLabel(const QString& label) const
166 {
167 QString tunedLabel;
168 const int labelLength = label.length();
169 if (labelLength > 0) {
170 tunedLabel.reserve(labelLength);
171 tunedLabel = label[0].toUpper();
172 for (int i = 1; i < labelLength; ++i) {
173 if (label[i].isUpper() && !label[i - 1].isSpace() && !label[i - 1].isUpper()) {
174 tunedLabel += ' ';
175 tunedLabel += label[i].toLower();
176 } else {
177 tunedLabel += label[i];
178 }
179 }
180 }
181 return tunedLabel + ':';
182 }
183
184 QString KLoadMetaDataThread::formatValue(const Nepomuk::Variant& value)
185 {
186 if (value.isDateTime()) {
187 return KGlobal::locale()->formatDateTime(value.toDateTime(), KLocale::FancyLongDate);
188 }
189
190 if (value.isResource() || value.isResourceList()) {
191 QStringList links;
192 foreach(const Nepomuk::Resource& res, value.toResourceList()) {
193 if (KProtocolInfo::isKnownProtocol(res.resourceUri())) {
194 links << QString::fromLatin1("<a href=\"%1\">%2</a>")
195 .arg(KUrl(res.resourceUri()).url())
196 .arg(res.genericLabel());
197 } else {
198 links << res.genericLabel();
199 }
200 }
201 return links.join(QLatin1String(";\n"));
202 }
203
204 return value.toString();
205 }
206
207 #include "kloadmetadatathread_p.moc"