~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/runners/nepomuksearch/queryclientwrapper.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the Nepomuk Project
 
2
   Copyright (c) 2008-2009 Sebastian Trueg <trueg@kde.org>
 
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 "queryclientwrapper.h"
 
20
#include "nepomuksearchrunner.h"
 
21
 
 
22
#include <nepomuk/nie.h>
 
23
#include <nepomuk/nfo.h>
 
24
#include <Nepomuk/File>
 
25
#include <Nepomuk/Variant>
 
26
#include <Nepomuk/Types/Class>
 
27
#include <Nepomuk/Query/QueryServiceClient>
 
28
#include <Nepomuk/Query/Result>
 
29
#include <Nepomuk/Query/Query>
 
30
#include <Nepomuk/Query/QueryParser>
 
31
 
 
32
#include <KIcon>
 
33
#include <KDebug>
 
34
#include <KMimeType>
 
35
 
 
36
#include <Plasma/QueryMatch>
 
37
#include <Plasma/RunnerContext>
 
38
#include <Plasma/AbstractRunner>
 
39
 
 
40
#include <QtCore/QTimer>
 
41
#include <QtCore/QMutex>
 
42
 
 
43
 
 
44
static const int s_maxResults = 10;
 
45
 
 
46
Nepomuk::QueryClientWrapper::QueryClientWrapper(SearchRunner* runner, Plasma::RunnerContext* context)
 
47
    : QObject(),
 
48
      m_runner(runner),
 
49
      m_runnerContext(context)
 
50
{
 
51
    // initialize the query client
 
52
    m_queryServiceClient = new Nepomuk::Query::QueryServiceClient(this);
 
53
    connect(m_queryServiceClient, SIGNAL(newEntries(const QList<Nepomuk::Query::Result>&)),
 
54
             this, SLOT(slotNewEntries(const QList<Nepomuk::Query::Result>&)));
 
55
}
 
56
 
 
57
 
 
58
Nepomuk::QueryClientWrapper::~QueryClientWrapper()
 
59
{
 
60
}
 
61
 
 
62
 
 
63
void Nepomuk::QueryClientWrapper::runQuery()
 
64
{
 
65
    //kDebug() << m_runnerContext->query();
 
66
 
 
67
    // add a timeout in case something goes wrong (no user wants to wait more than 30 seconds)
 
68
    QTimer::singleShot(30000, m_queryServiceClient, SLOT(close()));
 
69
 
 
70
    Query::Query q = Query::QueryParser::parseQuery(m_runnerContext->query());
 
71
    q.setLimit(s_maxResults);
 
72
    m_queryServiceClient->blockingQuery(q);
 
73
 
 
74
    //kDebug() << m_runnerContext->query() << "done";
 
75
}
 
76
 
 
77
 
 
78
namespace {
 
79
qreal normalizeScore(double score) {
 
80
    // no search result is ever a perfect match, NEVER. And mostly, when typing a URL
 
81
    // the users wants to open that url instead of using the search result. Thus, all
 
82
    // search results need to have a lower score than URLs which can drop to 0.5
 
83
    // And in the end, for 10 results, the score is not that important at the moment.
 
84
    // This can be improved in the future.
 
85
    // We go the easy way here and simply cut the score at 0.4
 
86
    return qMin(0.4, score);
 
87
}
 
88
}
 
89
 
 
90
void Nepomuk::QueryClientWrapper::slotNewEntries(const QList<Nepomuk::Query::Result>& results)
 
91
{
 
92
    QList<Plasma::QueryMatch> matches;
 
93
    foreach(const Query::Result& result, results) {
 
94
        Plasma::QueryMatch match(m_runner);
 
95
        match.setType(Plasma::QueryMatch::PossibleMatch);
 
96
        match.setRelevance(normalizeScore(result.score()));
 
97
 
 
98
        Nepomuk::Resource res = result.resource();
 
99
 
 
100
        QString type;
 
101
        QString iconName;
 
102
 
 
103
        KMimeType::Ptr mimetype;
 
104
        if (res.hasProperty(Nepomuk::Vocabulary::NIE::mimeType())) {
 
105
            mimetype = KMimeType::mimeType(res.property(Nepomuk::Vocabulary::NIE::mimeType()).toString());
 
106
        }
 
107
        if (!mimetype && res.isFile() && res.toFile().url().isLocalFile()) {
 
108
            const KUrl url(res.toFile().url());
 
109
            mimetype = KMimeType::findByUrl(url);
 
110
        }
 
111
 
 
112
        if (mimetype) {
 
113
            type = mimetype->comment();
 
114
            iconName = mimetype->iconName();
 
115
        }
 
116
 
 
117
        if (type.isEmpty() ) {
 
118
            type = Nepomuk::Types::Class(res.resourceType()).label();
 
119
            iconName = res.genericIcon();
 
120
        }
 
121
 
 
122
        match.setText(res.genericLabel());
 
123
        match.setSubtext(type);
 
124
        match.setIcon(KIcon(iconName.isEmpty() ? QString::fromLatin1("nepomuk") : iconName));
 
125
 
 
126
        match.setData(qVariantFromValue(res));
 
127
        match.setId(KUrl(res.resourceUri()).url());
 
128
        matches << match;
 
129
    }
 
130
    m_runnerContext->addMatches(m_runnerContext->query(), matches);
 
131
}
 
132
 
 
133
#include "queryclientwrapper.moc"