1
/* This file is part of the Nepomuk Project
2
Copyright (c) 2008 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 "nepomuksearchrunner.h"
20
#include "queryclientwrapper.h"
29
#include <nepomuk/nie.h>
30
#include <nepomuk/nfo.h>
31
#include <Nepomuk/File>
32
#include <Nepomuk/Resource>
33
#include <Nepomuk/Variant>
34
#include <Nepomuk/ResourceManager>
35
#include <Nepomuk/Query/QueryServiceClient>
37
#include <KFileItemActions>
38
#include <KFileItemList>
39
#include <KFileItemListProperties>
40
#include <KIO/NetAccess>
43
#include <KMimeTypeTrader>
48
* Milliseconds to wait before issuing the next query.
49
* This timeout is intended to prevent us from starting
50
* a query after each key press by the user. So it should
51
* roughly equal the time between key presses of an average user.
53
const int s_userActionTimeout = 400;
57
Nepomuk::SearchRunner::SearchRunner( QObject* parent, const QVariantList& args )
58
: Plasma::AbstractRunner( parent, args )
63
Nepomuk::SearchRunner::SearchRunner( QObject* parent, const QString& serviceId )
64
: Plasma::AbstractRunner( parent, serviceId )
69
void Nepomuk::SearchRunner::init()
71
Nepomuk::ResourceManager::instance()->init();
73
// we are pretty slow at times and use DBus calls
76
// we are way less important than others, mostly because we are slow
77
setPriority(LowPriority);
79
m_actions = new KFileItemActions(this);
80
addSyntax(Plasma::RunnerSyntax(":q:", i18n("Finds files, documents and other content that matches :q: using the desktop search system.")));
84
Nepomuk::SearchRunner::~SearchRunner()
86
qDeleteAll(m_konqActions);
90
void Nepomuk::SearchRunner::match( Plasma::RunnerContext& context )
92
kDebug() << &context << context.query();
94
if (Nepomuk::ResourceManager::instance()->initialized()) {
95
// This method needs to be thread-safe since KRunner does simply start new threads whenever
96
// the query term changes.
99
// we do not want to restart a query on each key-press. That would result
100
// in way too many queries for the rather sluggy Nepomuk query service
101
// Thus, we use a little timeout to make sure we do not query too often
103
m_waiter.wait(&m_mutex, s_userActionTimeout);
106
if (!context.isValid()) {
107
kDebug() << "deprecated search:" << context.query();
108
// we are no longer the latest call
112
// no queries on very short strings
113
if (Query::QueryServiceClient::serviceAvailable() && context.query().count() >= 3) {
114
QueryClientWrapper queryWrapper(this, &context);
115
queryWrapper.runQuery();
122
void Nepomuk::SearchRunner::run( const Plasma::RunnerContext&, const Plasma::QueryMatch& match )
124
// If no action was selected, the interface doesn't support multiple
125
// actions so we simply open the file
126
if (QAction *a = match.selectedAction()) {
127
if (a != action("open")) {
128
match.selectedAction()->trigger();
133
Nepomuk::Resource res = match.data().value<Nepomuk::Resource>();
134
KUrl url = res.resourceUri();
135
if (res.isFile() && res.toFile().url().isLocalFile()) {
136
url = res.toFile().url();
139
KService::Ptr preferredServicePtr;
140
if (res.hasProperty(Nepomuk::Vocabulary::NIE::mimeType()) &&
141
KUrl(res.property(Nepomuk::Vocabulary::NIE::url()).toUrl()).isLocalFile()) {
142
preferredServicePtr = KMimeTypeTrader::self()->preferredService(res.property(Nepomuk::Vocabulary::NIE::mimeType()).toString());
145
if (preferredServicePtr.isNull() || !KRun::run(*preferredServicePtr.constData(), KUrl::List(url), 0)) {
146
(void)new KRun(url, 0);
150
QList<QAction*> Nepomuk::SearchRunner::actionsForMatch(const Plasma::QueryMatch &match)
152
//Unlike other runners, the actions generated here are likely to see
153
//little reuse. Hence, we will clear the actions then generate new
154
//ones per iteration to avoid excessive memory consumption.
155
qDeleteAll(m_konqActions);
156
m_konqActions.clear();
159
if (!action("open")) {
160
addAction("open", KIcon("document-open"), i18n("Open"));
162
ret << action("open");
164
Nepomuk::Resource res = match.data().value<Nepomuk::Resource>();
166
KUrl url(res.resourceUri());
168
if (!KIO::NetAccess::stat(url.path(), entry, 0)) {
169
return QList<QAction*>();
173
list << KFileItem(entry, url);
175
KFileItemListProperties prop;
179
m_actions->setItemListProperties(prop);
180
m_actions->addOpenWithActionsTo(&dummy, QString());
181
//Add user defined actions
182
m_actions->addServiceActionsTo(&dummy);
184
m_konqActions = Plasma::actionsFromMenu(&dummy);
186
ret << m_konqActions;
191
K_EXPORT_PLASMA_RUNNER(nepomuksearchrunner, Nepomuk::SearchRunner)
193
#include "nepomuksearchrunner.moc"