┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/panels/information/kmetadatamodel.cpp
blob: 6a1898b1f65c024bb55db3d5994a5d326341b4b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*****************************************************************************
 * Copyright (C) 2010 by Peter Penz <[email protected]>                      *
 *                                                                           *
 * This library is free software; you can redistribute it and/or             *
 * modify it under the terms of the GNU Library General Public               *
 * License version 2 as published by the Free Software Foundation.           *
 *                                                                           *
 * This library is distributed in the hope that it will be useful,           *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
 * Library General Public License for more details.                          *
 *                                                                           *
 * You should have received a copy of the GNU Library General Public License *
 * along with this library; see the file COPYING.LIB.  If not, write to      *
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,      *
 * Boston, MA 02110-1301, USA.                                               *
 *****************************************************************************/

#include "kmetadatamodel.h"

#include <kfileitem.h>
#include "kloadmetadatathread_p.h"
#include <kurl.h>

class KMetaDataModel::Private
{

public:
    Private(KMetaDataModel* parent);
    ~Private();

    void slotLoadingFinished();

    QList<KFileItem> m_fileItems;
#ifdef HAVE_NEPOMUK
    QHash<KUrl, Nepomuk::Variant> m_data;

    QList<KLoadMetaDataThread*> m_metaDataThreads;
    KLoadMetaDataThread* m_latestMetaDataThread;
#endif

private:
    KMetaDataModel* const q;
};

KMetaDataModel::Private::Private(KMetaDataModel* parent) :
    m_fileItems(),
#ifdef HAVE_NEPOMUK
    m_data(),
    m_metaDataThreads(),
    m_latestMetaDataThread(0),
#endif
    q(parent)
{
}

KMetaDataModel::Private::~Private()
{
}

KMetaDataModel::KMetaDataModel(QObject* parent) :
    QObject(parent),
    d(new Private(this))
{
}

KMetaDataModel::~KMetaDataModel()
{
    delete d;
}

void KMetaDataModel::Private::slotLoadingFinished()
{
#ifdef HAVE_NEPOMUK
    // The thread that has emitted the finished() signal
    // will get deleted and removed from m_metaDataThreads.
    const int threadsCount = m_metaDataThreads.count();
    for (int i = 0; i < threadsCount; ++i) {
        KLoadMetaDataThread* thread = m_metaDataThreads[i];
        if (thread == q->sender()) {
            m_metaDataThreads.removeAt(i);
            if (thread != m_latestMetaDataThread) {
                // Ignore data of older threads, as the data got
                // obsolete by m_latestMetaDataThread.
                thread->deleteLater();
                return;
            }
        }
    }

    m_data = m_latestMetaDataThread->data();
    m_latestMetaDataThread->deleteLater();
#endif

    emit q->loadingFinished();
}

void KMetaDataModel::setItems(const KFileItemList& items)
{
    d->m_fileItems = items;

#ifdef HAVE_NEPOMUK
    QList<KUrl> urls;
    foreach (const KFileItem& item, items) {
        const KUrl url = item.nepomukUri();
        if (url.isValid()) {
            urls.append(url);
        }
    }

    // Cancel all threads that have not emitted a finished() signal.
    // The deleting of those threads is done in slotLoadingFinished().
    foreach (KLoadMetaDataThread* thread, d->m_metaDataThreads) {
        thread->cancel();
    }

    // create a new thread that will provide the meeta data for the items
    d->m_latestMetaDataThread = new KLoadMetaDataThread(this);
    connect(d->m_latestMetaDataThread, SIGNAL(finished()), this, SLOT(slotLoadingFinished()));
    d->m_latestMetaDataThread->load(urls);
    d->m_metaDataThreads.append(d->m_latestMetaDataThread);
#endif
}

QString KMetaDataModel::group(const KUrl& metaDataUri) const
{
    QString group; // return value

    const QString uri = metaDataUri.url();
    if (uri == QLatin1String("http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#width")) {
        group = QLatin1String("0sizeA");
    } else if (uri == QLatin1String("http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#height")) {
        group = QLatin1String("0sizeB");
    }

    return group;
}

KFileItemList KMetaDataModel::items() const
{
    return d->m_fileItems;
}

#ifdef HAVE_NEPOMUK
QHash<KUrl, Nepomuk::Variant> KMetaDataModel::data() const
{
    return d->m_data;
}

QHash<KUrl, Nepomuk::Variant> KMetaDataModel::loadData() const
{
    return QHash<KUrl, Nepomuk::Variant>();
}
#endif

#include "kmetadatamodel.moc"