]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/kloadmetadatathread.cpp
SVN_SILENT: removed unnecessary include
[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 unsigned int rating = 0;
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 && (rating != file.rating())) {
76 rating = 0; // reset rating
77 } else if (first) {
78 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()) {
169 return value.toResource().genericLabel();
170 } else if (value.isResourceList()) {
171 QStringList list;
172 foreach(const Nepomuk::Resource& res, value.toResourceList()) {
173 list << res.genericLabel();
174 }
175 return list.join(QLatin1String(";\n"));
176 } else {
177 return value.toString();
178 }
179 }
180
181 #include "kloadmetadatathread_p.moc"