]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/kloadmetadatathread.cpp
* Do not hide the parent folder since we can now browse into it by simply clicking...
[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 #include <kprotocolinfo.h>
28
29 #include <nepomuk/resource.h>
30
31 KLoadMetaDataThread::KLoadMetaDataThread() :
32 m_rating(0),
33 m_comment(),
34 m_tags(),
35 m_items(),
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 foreach (const KUrl& url, m_urls) {
69 if (m_canceled) {
70 return;
71 }
72
73 Nepomuk::Resource file(url);
74 if (!file.isValid()) {
75 continue;
76 }
77
78 m_files.insert(url, file);
79
80 if (!first && (m_rating != file.rating())) {
81 m_rating = 0; // reset rating
82 } else if (first) {
83 m_rating = file.rating();
84 }
85
86 if (!first && (m_comment != file.description())) {
87 m_comment.clear(); // reset comment
88 } else if (first) {
89 m_comment = file.description();
90 }
91
92 if (!first && (m_tags != file.tags())) {
93 m_tags.clear(); // reset tags
94 } else if (first) {
95 m_tags = file.tags();
96 }
97
98 if (first && (m_urls.count() == 1)) {
99 // TODO: show shared meta information instead
100 // of not showing anything on multiple selections
101 QHash<QUrl, Nepomuk::Variant> variants = file.properties();
102 QHash<QUrl, Nepomuk::Variant>::const_iterator it = variants.constBegin();
103 while (it != variants.constEnd()) {
104 Nepomuk::Types::Property prop(it.key());
105 if (settings.readEntry(prop.name(), true)) {
106 Item item;
107 item.name = prop.name();
108 item.label = tunedLabel(prop.label());
109 item.value = formatValue(it.value());
110 m_items.append(item);
111 }
112 ++it;
113 }
114 }
115
116 first = false;
117 }
118 }
119
120 int KLoadMetaDataThread::rating() const
121 {
122 return m_rating;
123 }
124
125 QString KLoadMetaDataThread::comment() const
126 {
127 return m_comment;
128 }
129
130 QList<Nepomuk::Tag> KLoadMetaDataThread::tags() const
131 {
132 return m_tags;
133 }
134
135 QList<KLoadMetaDataThread::Item> KLoadMetaDataThread::items() const
136 {
137 return m_items;
138 }
139
140 QMap<KUrl, Nepomuk::Resource> KLoadMetaDataThread::files() const
141 {
142 return m_files;
143 }
144
145 void KLoadMetaDataThread::slotFinished()
146 {
147 deleteLater();
148 }
149
150 QString KLoadMetaDataThread::tunedLabel(const QString& label) const
151 {
152 QString tunedLabel;
153 const int labelLength = label.length();
154 if (labelLength > 0) {
155 tunedLabel.reserve(labelLength);
156 tunedLabel = label[0].toUpper();
157 for (int i = 1; i < labelLength; ++i) {
158 if (label[i].isUpper() && !label[i - 1].isSpace() && !label[i - 1].isUpper()) {
159 tunedLabel += ' ';
160 tunedLabel += label[i].toLower();
161 } else {
162 tunedLabel += label[i];
163 }
164 }
165 }
166 return tunedLabel + ':';
167 }
168
169 QString KLoadMetaDataThread::formatValue(const Nepomuk::Variant& value)
170 {
171 if (value.isDateTime()) {
172 return KGlobal::locale()->formatDateTime(value.toDateTime(), KLocale::FancyLongDate);
173 }
174
175 if (value.isResource() || value.isResourceList()) {
176 QStringList links;
177 foreach(const Nepomuk::Resource& res, value.toResourceList()) {
178 if (KProtocolInfo::isKnownProtocol(res.resourceUri())) {
179 links << QString::fromLatin1("<a href=\"%1\">%2</a>")
180 .arg(KUrl(res.resourceUri()).url())
181 .arg(res.genericLabel());
182 } else {
183 links << res.genericLabel();
184 }
185 }
186 return links.join(QLatin1String(";\n"));
187 }
188
189 return value.toString();
190 }
191
192 #include "kloadmetadatathread_p.moc"