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