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

« back to all changes in this revision

Viewing changes to plasma/generic/runners/nepomuksearch/nepomuksearchrunner.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 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 "nepomuksearchrunner.h"
 
20
#include "queryclientwrapper.h"
 
21
 
 
22
#include <QMenu>
 
23
 
 
24
#include <KIcon>
 
25
#include <KRun>
 
26
#include <KDebug>
 
27
#include <KUrl>
 
28
 
 
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>
 
36
 
 
37
#include <KFileItemActions>
 
38
#include <KFileItemList>
 
39
#include <KFileItemListProperties>
 
40
#include <KIO/NetAccess>
 
41
 
 
42
#include <KMimeType>
 
43
#include <KMimeTypeTrader>
 
44
#include <KService>
 
45
 
 
46
namespace {
 
47
    /**
 
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.
 
52
     */
 
53
    const int s_userActionTimeout = 400;
 
54
}
 
55
 
 
56
 
 
57
Nepomuk::SearchRunner::SearchRunner( QObject* parent, const QVariantList& args )
 
58
    : Plasma::AbstractRunner( parent, args )
 
59
{
 
60
}
 
61
 
 
62
 
 
63
Nepomuk::SearchRunner::SearchRunner( QObject* parent, const QString& serviceId )
 
64
    : Plasma::AbstractRunner( parent, serviceId )
 
65
{
 
66
}
 
67
 
 
68
 
 
69
void Nepomuk::SearchRunner::init()
 
70
{
 
71
    Nepomuk::ResourceManager::instance()->init();
 
72
 
 
73
    // we are pretty slow at times and use DBus calls
 
74
    setSpeed(SlowSpeed);
 
75
 
 
76
    // we are way less important than others, mostly because we are slow
 
77
    setPriority(LowPriority);
 
78
 
 
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.")));
 
81
}
 
82
 
 
83
 
 
84
Nepomuk::SearchRunner::~SearchRunner()
 
85
{
 
86
    qDeleteAll(m_konqActions);
 
87
}
 
88
 
 
89
 
 
90
void Nepomuk::SearchRunner::match( Plasma::RunnerContext& context )
 
91
{
 
92
    kDebug() << &context << context.query();
 
93
 
 
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.
 
97
        m_mutex.lock();
 
98
 
 
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
 
102
 
 
103
        m_waiter.wait(&m_mutex, s_userActionTimeout);
 
104
        m_mutex.unlock();
 
105
 
 
106
        if (!context.isValid()) {
 
107
            kDebug() << "deprecated search:" << context.query();
 
108
            // we are no longer the latest call
 
109
            return;
 
110
        }
 
111
 
 
112
        // no queries on very short strings
 
113
        if (Query::QueryServiceClient::serviceAvailable() && context.query().count() >= 3) {
 
114
            QueryClientWrapper queryWrapper(this, &context);
 
115
            queryWrapper.runQuery();
 
116
            m_waiter.wakeAll();
 
117
        }
 
118
    }
 
119
}
 
120
 
 
121
 
 
122
void Nepomuk::SearchRunner::run( const Plasma::RunnerContext&, const Plasma::QueryMatch& match )
 
123
{
 
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();
 
129
            return;
 
130
        }
 
131
    }
 
132
 
 
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();
 
137
    }
 
138
 
 
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());
 
143
    }
 
144
 
 
145
    if (preferredServicePtr.isNull() || !KRun::run(*preferredServicePtr.constData(), KUrl::List(url), 0)) {
 
146
        (void)new KRun(url, 0);
 
147
    }
 
148
}
 
149
 
 
150
QList<QAction*> Nepomuk::SearchRunner::actionsForMatch(const Plasma::QueryMatch &match)
 
151
{
 
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();
 
157
 
 
158
    QList<QAction*> ret;
 
159
    if (!action("open")) {
 
160
         addAction("open", KIcon("document-open"), i18n("Open"));
 
161
    }
 
162
    ret << action("open");
 
163
 
 
164
    Nepomuk::Resource res = match.data().value<Nepomuk::Resource>();
 
165
 
 
166
    KUrl url(res.resourceUri());
 
167
    KIO::UDSEntry entry;
 
168
    if (!KIO::NetAccess::stat(url.path(), entry, 0)) {
 
169
        return QList<QAction*>();
 
170
    }
 
171
 
 
172
    KFileItemList list;
 
173
    list << KFileItem(entry, url);
 
174
 
 
175
    KFileItemListProperties prop;
 
176
    prop.setItems(list);
 
177
 
 
178
    QMenu dummy;
 
179
    m_actions->setItemListProperties(prop);
 
180
    m_actions->addOpenWithActionsTo(&dummy, QString());
 
181
    //Add user defined actions
 
182
    m_actions->addServiceActionsTo(&dummy);
 
183
 
 
184
    m_konqActions = Plasma::actionsFromMenu(&dummy);
 
185
 
 
186
    ret << m_konqActions;
 
187
 
 
188
    return ret;
 
189
}
 
190
 
 
191
K_EXPORT_PLASMA_RUNNER(nepomuksearchrunner, Nepomuk::SearchRunner)
 
192
 
 
193
#include "nepomuksearchrunner.moc"