1
/* This file is part of the Nepomuk Project
2
Copyright (c) 2008-2009 Sebastian Trueg <trueg@kde.org>
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.
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.
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.
19
#include "queryclientwrapper.h"
20
#include "nepomuksearchrunner.h"
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>
36
#include <Plasma/QueryMatch>
37
#include <Plasma/RunnerContext>
38
#include <Plasma/AbstractRunner>
40
#include <QtCore/QTimer>
41
#include <QtCore/QMutex>
44
static const int s_maxResults = 10;
46
Nepomuk::QueryClientWrapper::QueryClientWrapper(SearchRunner* runner, Plasma::RunnerContext* context)
49
m_runnerContext(context)
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>&)));
58
Nepomuk::QueryClientWrapper::~QueryClientWrapper()
63
void Nepomuk::QueryClientWrapper::runQuery()
65
//kDebug() << m_runnerContext->query();
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()));
70
Query::Query q = Query::QueryParser::parseQuery(m_runnerContext->query());
71
q.setLimit(s_maxResults);
72
m_queryServiceClient->blockingQuery(q);
74
//kDebug() << m_runnerContext->query() << "done";
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);
90
void Nepomuk::QueryClientWrapper::slotNewEntries(const QList<Nepomuk::Query::Result>& results)
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()));
98
Nepomuk::Resource res = result.resource();
103
KMimeType::Ptr mimetype;
104
if (res.hasProperty(Nepomuk::Vocabulary::NIE::mimeType())) {
105
mimetype = KMimeType::mimeType(res.property(Nepomuk::Vocabulary::NIE::mimeType()).toString());
107
if (!mimetype && res.isFile() && res.toFile().url().isLocalFile()) {
108
const KUrl url(res.toFile().url());
109
mimetype = KMimeType::findByUrl(url);
113
type = mimetype->comment();
114
iconName = mimetype->iconName();
117
if (type.isEmpty() ) {
118
type = Nepomuk::Types::Class(res.resourceType()).label();
119
iconName = res.genericIcon();
122
match.setText(res.genericLabel());
123
match.setSubtext(type);
124
match.setIcon(KIcon(iconName.isEmpty() ? QString::fromLatin1("nepomuk") : iconName));
126
match.setData(qVariantFromValue(res));
127
match.setId(KUrl(res.resourceUri()).url());
130
m_runnerContext->addMatches(m_runnerContext->query(), matches);
133
#include "queryclientwrapper.moc"