~ubuntu-branches/ubuntu/raring/plasma-mobile/raring-proposed

« back to all changes in this revision

Viewing changes to components/metadatamodel/metadatacloudmodel.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2012-07-17 12:04:43 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20120717120443-q3ig9u2fnltx67yg
Tags: 2.0+git2012071701-0ubuntu1
* New upstream snapshot
* Remove build-dep on kde-runtime-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
#include <QDBusConnection>
23
23
#include <QDBusServiceWatcher>
24
 
#include <QTimer>
25
24
 
26
25
#include <KDebug>
27
26
#include <KMimeType>
42
41
#include <nepomuk/nfo.h>
43
42
#include <nepomuk/nie.h>
44
43
 
45
 
#include "kext.h"
 
44
#include <kao.h>
46
45
 
47
46
 
48
47
MetadataCloudModel::MetadataCloudModel(QObject *parent)
49
48
    : AbstractMetadataModel(parent),
50
 
      m_queryClient(0)
 
49
      m_queryClient(0),
 
50
      m_showEmptyCategories(false)
51
51
{
52
 
    m_queryTimer = new QTimer(this);
53
 
    m_queryTimer->setSingleShot(true);
54
 
    connect(m_queryTimer, SIGNAL(timeout()),
55
 
            this, SLOT(doQuery()));
56
 
 
57
 
 
58
52
    QHash<int, QByteArray> roleNames;
59
53
    roleNames[Label] = "label";
60
54
    roleNames[Count] = "count";
 
55
    roleNames[TotalCount] = "totalCount";
61
56
    setRoleNames(roleNames);
62
57
}
63
58
 
73
68
    }
74
69
 
75
70
    m_cloudCategory = category;
76
 
    m_queryTimer->start(0);
 
71
    askRefresh();
77
72
    emit cloudCategoryChanged();
78
73
}
79
74
 
96
91
    }
97
92
 
98
93
    m_allowedCategories = set;
 
94
    askRefresh();
99
95
    emit allowedCategoriesChanged();
100
96
}
101
97
 
104
100
    return stringToVariantList(m_allowedCategories.values());
105
101
}
106
102
 
 
103
void MetadataCloudModel::setShowEmptyCategories(bool show)
 
104
{
 
105
    if (show == m_showEmptyCategories) {
 
106
        return;
 
107
    }
 
108
 
 
109
    m_showEmptyCategories = show;
 
110
    askRefresh();
 
111
    emit showEmptyCategoriesChanged();
 
112
}
 
113
 
 
114
bool MetadataCloudModel::showEmptyCategories() const
 
115
{
 
116
    return m_showEmptyCategories;
 
117
}
 
118
 
107
119
 
108
120
void MetadataCloudModel::doQuery()
109
121
{
114
126
        return;
115
127
    }
116
128
 
117
 
    setStatus(Waiting);
118
 
    QString query = "select distinct ?label count(*) as ?count where { ";
119
 
 
120
 
    if (m_cloudCategory == "kext:Activity") {
121
 
        query += " ?activity nao:isRelated ?r . ?activity rdf:type kext:Activity . ?activity kext:activityIdentifier ?label ";
 
129
    setRunning(true);
 
130
    QString query;
 
131
 
 
132
    if (!m_showEmptyCategories) {
 
133
        query += "select * where { filter(?count != 0) { ";
 
134
    }
 
135
    query += "select distinct ?label "
 
136
          "sum(?localWeight) as ?count "
 
137
          "sum(?globalWeight) as ?totalCount "
 
138
        "where {  ?r nie:url ?h . "
 
139
        "{ select distinct ?r ?label "
 
140
           "1 as ?localWeight "
 
141
           "0 as ?globalWeight "
 
142
          "where {";
 
143
 
 
144
    if (m_cloudCategory == "kao:Activity") {
 
145
        query += " ?activity nao:isRelated ?r . ?activity rdf:type kao:Activity . ?activity kao:activityIdentifier ?label ";
122
146
    } else {
123
147
        query += " ?r " + m_cloudCategory + " ?label";
124
148
    }
125
149
 
126
150
 
127
 
    if (!resourceType().isEmpty() && m_cloudCategory != "rdf:type") {
 
151
    if (!resourceType().isEmpty()) {
128
152
        QString type = resourceType();
129
153
        bool negation = false;
130
 
        if (type.startsWith("!")) {
 
154
        if (type.startsWith('!')) {
131
155
            type = type.remove(0, 1);
132
156
            negation = true;
133
157
        }
143
167
        }
144
168
    }
145
169
 
146
 
    if (!resourceType().isEmpty() && m_cloudCategory != "nie:mimeType") {
147
 
        QString type = mimeType();
148
 
        bool negation = false;
149
 
        if (type.startsWith("!")) {
150
 
            type = type.remove(0, 1);
151
 
            negation = true;
152
 
        }
153
 
        if (negation) {
154
 
            query += " . FILTER(!bif:exists((select (1) where { ?r nie:mimeType ?mimeType . FILTER(bif:contains(?mimeType, \"'" + type + "'\")) . }))) ";
155
 
        } else {
156
 
            query += " . ?r nie:mimeType ?mimeType . FILTER(bif:contains(?mimeType, \"'" + type + "'\")) ";
157
 
        }
 
170
    if (!mimeTypeStrings().isEmpty()) {
 
171
        query += " { ";
 
172
        bool first = true;
 
173
        foreach (QString type, mimeTypeStrings()) {
 
174
            bool negation = false;
 
175
            if (!first) {
 
176
                query += " UNION ";
 
177
            }
 
178
            first = false;
 
179
            if (type.startsWith('!')) {
 
180
                type = type.remove(0, 1);
 
181
                negation = true;
 
182
            }
 
183
            if (negation) {
 
184
                query += " { . FILTER(!bif:exists((select (1) where { ?r nie:mimeType \"" + type + "\"^^<http://www.w3.org/2001/XMLSchema#string> . }))) } ";
 
185
            } else {
 
186
                query += " { ?r nie:mimeType \"" + type + "\"^^<http://www.w3.org/2001/XMLSchema#string> . } ";
 
187
            }
 
188
        }
 
189
        query += " } ";
158
190
    }
159
191
 
160
192
    if (parameters && parameters->size() > 0) {
161
193
        foreach (const QString &key, parameters->keys()) {
162
194
            QString parameter = parameters->value(key).toString();
163
195
            bool negation = false;
164
 
            if (parameter.startsWith("!")) {
 
196
            if (parameter.startsWith('!')) {
165
197
                parameter = parameter.remove(0, 1);
166
198
                negation = true;
167
199
            }
174
206
        }
175
207
    }
176
208
 
177
 
    if (!activityId().isEmpty() && m_cloudCategory != "kext:Activity") {
 
209
    if (!activityId().isEmpty()) {
178
210
        QString activity = activityId();
179
211
        bool negation = false;
180
 
        if (activity.startsWith("!")) {
 
212
        if (activity.startsWith('!')) {
181
213
            activity = activity.remove(0, 1);
182
214
            negation = true;
183
215
        }
184
 
        Nepomuk::Resource acRes(activity, Nepomuk::Vocabulary::KEXT::Activity());
 
216
        Nepomuk::Resource acRes(activity, Nepomuk::Vocabulary::KAO::Activity());
185
217
 
186
218
        if (negation) {
187
219
            query +=  ". FILTER(!bif:exists((select (1) where { <" + acRes.resourceUri().toString() + "> <http://www.semanticdesktop.org/ontologies/2007/08/15/nao#isRelated> ?r . }))) ";
195
227
        QString individualTag = tag;
196
228
        bool negation = false;
197
229
 
198
 
        if (individualTag.startsWith("!")) {
 
230
        if (individualTag.startsWith('!')) {
199
231
            individualTag = individualTag.remove(0, 1);
200
232
            negation = true;
201
233
        }
244
276
        query += " . ?r nao:numericRating ?rating filter (?rating <=" + QString::number(maximumRating()) + ") ";
245
277
    }
246
278
 
247
 
    query +=  " . ?r <http://www.semanticdesktop.org/ontologies/2007/08/15/nao#userVisible> ?v1 . FILTER(?v1>0) .  } group by ?label order by ?label";
 
279
    //Exclude who doesn't have url
 
280
    query += " . ?r nie:url ?h . ";
 
281
 
 
282
    //User visibility filter doesn't seem to have an acceptable speed
 
283
    //query +=  " . FILTER(bif:exists((select (1) where { ?r a [ <http://www.semanticdesktop.org/ontologies/2007/08/15/nao#userVisible> \"true\"^^<http://www.w3.org/2001/XMLSchema#boolean> ] . }))) } group by ?label order by ?label";
 
284
 
 
285
    query += "}} UNION { "
 
286
              "select distinct ?r ?label "
 
287
                "0 as ?localWeight "
 
288
                "1 as ?globalWeight "
 
289
              "where { "
 
290
                "?r " + m_cloudCategory + " ?label . "
 
291
                "?r nie:url ?h . }}";
 
292
 
 
293
    query +=  " } group by ?label order by ?label";
 
294
 
 
295
    if (!m_showEmptyCategories) {
 
296
        query +=  " }} ";
 
297
    }
248
298
 
249
299
    kDebug() << "Performing the Sparql query" << query;
250
300
 
256
306
    delete m_queryClient;
257
307
    m_queryClient = new Nepomuk::Query::QueryServiceClient(this);
258
308
 
259
 
    connect(m_queryClient, SIGNAL(newEntries(const QList<Nepomuk::Query::Result> &)),
260
 
            this, SLOT(newEntries(const QList<Nepomuk::Query::Result> &)));
261
 
    connect(m_queryClient, SIGNAL(entriesRemoved(const QList<QUrl> &)),
262
 
            this, SLOT(entriesRemoved(const QList<QUrl> &)));
 
309
    connect(m_queryClient, SIGNAL(newEntries(QList<Nepomuk::Query::Result>)),
 
310
            this, SLOT(newEntries(QList<Nepomuk::Query::Result>)));
 
311
    connect(m_queryClient, SIGNAL(entriesRemoved(QList<QUrl>)),
 
312
            this, SLOT(entriesRemoved(QList<QUrl>)));
263
313
    connect(m_queryClient, SIGNAL(finishedListing()), this, SLOT(finishedListing()));
264
314
 
265
315
    m_queryClient->sparqlQuery(query);
267
317
 
268
318
void MetadataCloudModel::newEntries(const QList< Nepomuk::Query::Result > &entries)
269
319
{
270
 
    setStatus(Running);
271
 
    QVector<QPair<QString, int> > results;
 
320
    QVector<QHash<int, QVariant> > results;
272
321
    QVariantList categories;
273
322
 
274
 
    foreach (Nepomuk::Query::Result res, entries) {
 
323
    foreach (const Nepomuk::Query::Result &res, entries) {
275
324
        QString label;
276
325
        int count = res.additionalBinding(QLatin1String("count")).variant().toInt();
 
326
        int totalCount = res.additionalBinding(QLatin1String("totalCount")).variant().toInt();
277
327
        QVariant rawLabel = res.additionalBinding(QLatin1String("label")).variant();
278
328
 
279
329
        if (rawLabel.canConvert<Nepomuk::Resource>()) {
298
348
            !(m_allowedCategories.isEmpty() || m_allowedCategories.contains(label))) {
299
349
            continue;
300
350
        }
301
 
        results << QPair<QString, int>(label, count);
 
351
        QHash<int, QVariant> result;
 
352
        result[Label] = label;
 
353
        result[Count] = count;
 
354
        result[TotalCount] = totalCount;
 
355
        results << result;
302
356
        categories << label;
303
357
    }
304
358
    if (results.count() > 0) {
305
 
        beginInsertRows(QModelIndex(), m_results.count(), m_results.count()+results.count());
 
359
        beginInsertRows(QModelIndex(), m_results.count(), m_results.count()+results.count()-1);
306
360
        m_results << results;
307
361
        m_categories << categories;
308
362
        endInsertRows();
338
392
 
339
393
void MetadataCloudModel::finishedListing()
340
394
{
341
 
    setStatus(Idle);
 
395
    setRunning(false);
342
396
}
343
397
 
344
398
 
350
404
        return QVariant();
351
405
    }
352
406
 
353
 
    const QPair<QString, int> row = m_results[index.row()];
 
407
    return m_results[index.row()].value(role);
354
408
 
355
 
    switch (role) {
356
 
    case Label:
357
 
        return row.first;
358
 
    case Count:
359
 
        return row.second;
360
 
    default:
361
 
        return QVariant();
362
 
    }
363
409
}
364
410
 
365
411
#include "metadatacloudmodel.moc"