]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/kloadmetadatathread.cpp
Don't call QThread::wait() in the destructor - in the worst case an issue in Nepomuk...
[dolphin.git] / src / panels / information / kloadmetadatathread.cpp
1 /*****************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
3 * *
4 * This library is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Library General Public *
6 * License version 2 as published by the Free Software Foundation. *
7 * *
8 * This library is distributed in the hope that it will be useful, *
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
11 * Library General Public License for more details. *
12 * *
13 * You should have received a copy of the GNU Library General Public License *
14 * along with this library; see the file COPYING.LIB. If not, write to *
15 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
16 * Boston, MA 02110-1301, USA. *
17 *****************************************************************************/
18
19 #include "kloadmetadatathread_p.h"
20
21 #include <kconfig.h>
22 #include <kconfiggroup.h>
23
24 #include <nepomuk/variant.h>
25
26 KLoadMetaDataThread::KLoadMetaDataThread() :
27 m_rating(0),
28 m_comment(),
29 m_tags(),
30 m_metaInfoLabels(),
31 m_metaInfoValues(),
32 m_files(),
33 m_urls(),
34 m_canceled(false)
35 {
36 }
37
38 KLoadMetaDataThread::~KLoadMetaDataThread()
39 {
40 }
41
42 void KLoadMetaDataThread::load(const KUrl::List& urls)
43 {
44 m_urls = urls;
45 m_canceled = false;
46 start();
47 }
48
49 void KLoadMetaDataThread::cancelAndDelete()
50 {
51 connect(this, SIGNAL(finished()), this, SLOT(slotFinished()));
52 m_canceled = true;
53 // Setting m_canceled to true will cancel KLoadMetaDataThread::run()
54 // as soon as possible. Afterwards the thread will delete itself
55 // asynchronously inside slotFinished().
56 }
57
58 void KLoadMetaDataThread::run()
59 {
60 KConfig config("kmetainformationrc", KConfig::NoGlobals);
61 KConfigGroup settings = config.group("Show");
62
63 bool first = true;
64 unsigned int rating = 0;
65 foreach (const KUrl& url, m_urls) {
66 if (m_canceled) {
67 return;
68 }
69
70 Nepomuk::Resource file(url);
71 m_files.insert(url, file);
72
73 if (!first && (rating != file.rating())) {
74 rating = 0; // reset rating
75 } else if (first) {
76 rating = file.rating();
77 }
78
79 if (!first && (m_comment != file.description())) {
80 m_comment.clear(); // reset comment
81 } else if (first) {
82 m_comment = file.description();
83 }
84
85 if (!first && (m_tags != file.tags())) {
86 m_tags.clear(); // reset tags
87 } else if (first) {
88 m_tags = file.tags();
89 }
90
91 if (first && (m_urls.count() == 1)) {
92 // TODO: show shared meta information instead
93 // of not showing anything on multiple selections
94 QHash<QUrl, Nepomuk::Variant> properties = file.properties();
95 QHash<QUrl, Nepomuk::Variant>::const_iterator it = properties.constBegin();
96 while (it != properties.constEnd()) {
97 Nepomuk::Types::Property prop(it.key());
98 if (settings.readEntry(prop.name(), true)) {
99 // TODO #1: use Nepomuk::formatValue(res, prop) if available
100 // instead of it.value().toString()
101 // TODO #2: using tunedLabel() is a workaround for KDE 4.3 (4.4?) until
102 // we get translated labels
103 m_metaInfoLabels.append(tunedLabel(prop.label()));
104 m_metaInfoValues.append(it.value().toString());
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<QString> KLoadMetaDataThread::metaInfoLabels() const
130 {
131 return m_metaInfoLabels;
132 }
133
134 QList<QString> KLoadMetaDataThread::metaInfoValues() const
135 {
136 return m_metaInfoValues;
137 }
138
139 QMap<KUrl, Nepomuk::Resource> KLoadMetaDataThread::files() const
140 {
141 return m_files;
142 }
143
144 void KLoadMetaDataThread::slotFinished()
145 {
146 deleteLater();
147 }
148
149 QString KLoadMetaDataThread::tunedLabel(const QString& label) const
150 {
151 QString tunedLabel;
152 const int labelLength = label.length();
153 if (labelLength > 0) {
154 tunedLabel.reserve(labelLength);
155 tunedLabel = label[0].toUpper();
156 for (int i = 1; i < labelLength; ++i) {
157 if (label[i].isUpper() && !label[i - 1].isSpace() && !label[i - 1].isUpper()) {
158 tunedLabel += ' ';
159 tunedLabel += label[i].toLower();
160 } else {
161 tunedLabel += label[i];
162 }
163 }
164 }
165 return tunedLabel + ':';
166 }
167
168 #include "kloadmetadatathread_p.moc"