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