~ubuntu-branches/ubuntu/maverick/kdebase/maverick-updates

« back to all changes in this revision

Viewing changes to apps/dolphin/src/panels/information/kloadmetadatathread.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Debian Qt/KDE Maintainers, Martin Alfke, Modestas Vainius
  • Date: 2010-05-01 23:37:50 UTC
  • mfrom: (0.7.4 upstream) (0.4.4 experimental)
  • mto: This revision was merged to the branch mainline in revision 285.
  • Revision ID: james.westby@ubuntu.com-20100501233750-maq4i4sh8ymjbneb
Tags: 4:4.4.3-1
* New upstream release:
  - Konsole does not crash when closing a broken restored session.
    (Closes: #555831)
  - Konqueror does not crash when closing fast a tab. (Closes: #441298)

[Martin Alfke]
* Update of debian/copyright for kde 4.4

[ Modestas Vainius ]
* Bump kde-sc-dev-latest build dependency to 4.4.3.
* Confirm symbol files for 4.4.2 on hurd-i386 i386 ia64 kfreebsd-amd64
  kfreebsd-i386 mips powerpc s390 sparc.
* Release KDE SC 4.4.3 to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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::cancel()
 
54
{
 
55
    // Setting m_canceled to true will cancel KLoadMetaDataThread::run()
 
56
    // as soon as run() gets the chance to check m_cancel.
 
57
    m_canceled = true;
 
58
}
 
59
 
 
60
void KLoadMetaDataThread::cancelAndDelete()
 
61
{
 
62
    if (isFinished()) {
 
63
        Q_ASSERT(!isRunning());
 
64
        deleteLater();
 
65
    } else {
 
66
        connect(this, SIGNAL(finished()), this, SLOT(slotFinished()));
 
67
        // Setting m_canceled to true will cancel KLoadMetaDataThread::run()
 
68
        // as soon as run() gets the chance to check m_cancel.
 
69
        m_canceled = true;
 
70
        // Afterwards the thread will delete itself
 
71
        // asynchronously inside slotFinished().
 
72
    }
 
73
}
 
74
void KLoadMetaDataThread::run()
 
75
{
 
76
    KConfig config("kmetainformationrc", KConfig::NoGlobals);
 
77
    KConfigGroup settings = config.group("Show");
 
78
 
 
79
    bool first = true;
 
80
    foreach (const KUrl& url, m_urls) {
 
81
        if (m_canceled) {
 
82
            return;
 
83
        }
 
84
 
 
85
        Nepomuk::Resource file(url);
 
86
        if (!file.isValid()) {
 
87
            continue;
 
88
        }
 
89
 
 
90
        m_files.insert(url, file);
 
91
 
 
92
        if (!first && (m_rating != file.rating())) {
 
93
            m_rating = 0; // reset rating
 
94
        } else if (first) {
 
95
            m_rating = file.rating();
 
96
        }
 
97
 
 
98
        if (!first && (m_comment != file.description())) {
 
99
            m_comment.clear(); // reset comment
 
100
        } else if (first) {
 
101
            m_comment = file.description();
 
102
        }
 
103
 
 
104
        if (!first && (m_tags != file.tags())) {
 
105
            m_tags.clear(); // reset tags
 
106
        } else if (first) {
 
107
            m_tags = file.tags();
 
108
        }
 
109
 
 
110
        if (first && (m_urls.count() == 1)) {
 
111
            // TODO: show shared meta information instead
 
112
            // of not showing anything on multiple selections
 
113
            QHash<QUrl, Nepomuk::Variant> variants = file.properties();
 
114
            QHash<QUrl, Nepomuk::Variant>::const_iterator it = variants.constBegin();
 
115
            while (it != variants.constEnd()) {
 
116
                Nepomuk::Types::Property prop(it.key());
 
117
                if (settings.readEntry(prop.name(), true)) {
 
118
                    Item item;
 
119
                    item.name = prop.name();
 
120
                    item.label = tunedLabel(prop.label());
 
121
                    item.value = formatValue(it.value());
 
122
                    m_items.append(item);
 
123
                }
 
124
                ++it;
 
125
            }
 
126
        }
 
127
 
 
128
        first = false;
 
129
    }
 
130
}
 
131
 
 
132
int KLoadMetaDataThread::rating() const
 
133
{
 
134
    return m_rating;
 
135
}
 
136
 
 
137
QString KLoadMetaDataThread::comment() const
 
138
{
 
139
    return m_comment;
 
140
}
 
141
 
 
142
QList<Nepomuk::Tag> KLoadMetaDataThread::tags() const
 
143
{
 
144
    return m_tags;
 
145
}
 
146
 
 
147
QList<KLoadMetaDataThread::Item> KLoadMetaDataThread::items() const
 
148
{
 
149
    return m_items;
 
150
}
 
151
 
 
152
QMap<KUrl, Nepomuk::Resource> KLoadMetaDataThread::files() const
 
153
{
 
154
    return m_files;
 
155
}
 
156
 
 
157
void KLoadMetaDataThread::slotFinished()
 
158
{
 
159
    deleteLater();
 
160
}
 
161
 
 
162
QString  KLoadMetaDataThread::tunedLabel(const QString& label) const
 
163
{
 
164
    QString tunedLabel;
 
165
    const int labelLength = label.length();
 
166
    if (labelLength > 0) {
 
167
        tunedLabel.reserve(labelLength);
 
168
        tunedLabel = label[0].toUpper();
 
169
        for (int i = 1; i < labelLength; ++i) {
 
170
            if (label[i].isUpper() && !label[i - 1].isSpace() && !label[i - 1].isUpper()) {
 
171
                tunedLabel += ' ';
 
172
                tunedLabel += label[i].toLower();
 
173
            } else {
 
174
                tunedLabel += label[i];
 
175
            }
 
176
        }
 
177
    }
 
178
    return tunedLabel + ':';
 
179
}
 
180
 
 
181
QString  KLoadMetaDataThread::formatValue(const Nepomuk::Variant& value)
 
182
{
 
183
    if (value.isDateTime()) {
 
184
        return KGlobal::locale()->formatDateTime(value.toDateTime(), KLocale::FancyLongDate);
 
185
    }
 
186
 
 
187
    if (value.isResource() || value.isResourceList()) {
 
188
        QStringList links;
 
189
        foreach(const Nepomuk::Resource& res, value.toResourceList()) {
 
190
            if (KProtocolInfo::isKnownProtocol(res.resourceUri())) {
 
191
                links << QString::fromLatin1("<a href=\"%1\">%2</a>")
 
192
                         .arg(KUrl(res.resourceUri()).url())
 
193
                         .arg(res.genericLabel());
 
194
            } else {
 
195
                links << res.genericLabel();
 
196
            }
 
197
        }
 
198
        return links.join(QLatin1String(";\n"));
 
199
    }
 
200
 
 
201
    return value.toString();
 
202
}
 
203
 
 
204
#include "kloadmetadatathread_p.moc"