2007-11-02 03:34:46 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2006-2007 Aaron Seigo <aseigo@kde.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2008-05-01 20:40:10 +02:00
|
|
|
#include "runnercontext.h"
|
2007-11-02 03:34:46 +01:00
|
|
|
|
2007-12-02 08:11:50 +01:00
|
|
|
#include <QReadWriteLock>
|
|
|
|
|
2008-06-10 03:08:33 +02:00
|
|
|
#include <QDir>
|
2007-11-09 20:14:15 +01:00
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
2008-05-01 09:51:59 +02:00
|
|
|
#include <QSharedData>
|
2007-11-09 20:14:15 +01:00
|
|
|
|
2007-11-02 03:34:46 +01:00
|
|
|
#include <KCompletion>
|
|
|
|
#include <KDebug>
|
|
|
|
#include <KMimeType>
|
2007-12-19 08:01:16 +01:00
|
|
|
#include <KShell>
|
2007-11-09 20:14:15 +01:00
|
|
|
#include <KStandardDirs>
|
|
|
|
#include <KUrl>
|
2007-11-02 03:34:46 +01:00
|
|
|
|
2008-05-12 06:53:42 +02:00
|
|
|
#include "abstractrunner.h"
|
2008-05-02 20:46:05 +02:00
|
|
|
#include "querymatch.h"
|
2007-11-02 03:34:46 +01:00
|
|
|
|
2008-05-09 05:49:11 +02:00
|
|
|
//#define LOCK_FOR_READ(context) if (context->d->policy == Shared) { context->d->lock.lockForRead(); }
|
|
|
|
//#define LOCK_FOR_WRITE(context) if (context->d->policy == Shared) { context->d->lock.lockForWrite(); }
|
|
|
|
//#define UNLOCK(context) if (context->d->policy == Shared) { context->d->lock.unlock(); }
|
|
|
|
|
2008-05-01 21:43:22 +02:00
|
|
|
#define LOCK_FOR_READ(context) context->d->lock.lockForRead();
|
|
|
|
#define LOCK_FOR_WRITE(context) context->d->lock.lockForWrite();
|
|
|
|
#define UNLOCK(context) context->d->lock.unlock();
|
2008-05-09 05:49:11 +02:00
|
|
|
|
2007-11-02 03:34:46 +01:00
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
class RunnerContextPrivate : public QSharedData
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
|
|
|
public:
|
2008-07-01 20:56:43 +02:00
|
|
|
RunnerContextPrivate(RunnerContext *context)
|
2008-05-01 20:26:38 +02:00
|
|
|
: QSharedData(),
|
2008-05-01 20:40:10 +02:00
|
|
|
type(RunnerContext::UnknownType),
|
2008-05-09 05:49:11 +02:00
|
|
|
q(context)
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
RunnerContextPrivate(const RunnerContextPrivate& p)
|
2008-05-01 20:26:38 +02:00
|
|
|
: QSharedData(),
|
2008-05-09 05:49:11 +02:00
|
|
|
type(RunnerContext::None),
|
|
|
|
q(p.q)
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2008-05-09 05:49:11 +02:00
|
|
|
//kDebug() << "¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿boo yeah" << type;
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
~RunnerContextPrivate()
|
2007-12-02 08:11:50 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
/**
|
|
|
|
* Determines type of query
|
|
|
|
*/
|
|
|
|
void determineType()
|
|
|
|
{
|
2008-05-09 05:49:11 +02:00
|
|
|
// NOTE! this method must NEVER be called from
|
|
|
|
// code that may be running in multiple threads
|
|
|
|
// with the same data.
|
2008-07-01 20:56:43 +02:00
|
|
|
type = RunnerContext::UnknownType;
|
2008-06-10 03:08:33 +02:00
|
|
|
QString path = QDir::cleanPath(KShell::tildeExpand(term));
|
2008-05-01 21:43:22 +02:00
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
int space = term.indexOf(' ');
|
|
|
|
if (space > 0) {
|
|
|
|
if (!KStandardDirs::findExe(path.left(space)).isEmpty()) {
|
2008-07-01 20:56:43 +02:00
|
|
|
type = RunnerContext::ShellCommand;
|
2008-05-01 09:51:59 +02:00
|
|
|
}
|
|
|
|
} else if (!KStandardDirs::findExe(path.left(space)).isEmpty()) {
|
2008-07-01 20:56:43 +02:00
|
|
|
type = RunnerContext::Executable;
|
2008-05-01 09:51:59 +02:00
|
|
|
} else {
|
|
|
|
KUrl url(term);
|
2008-06-10 03:08:33 +02:00
|
|
|
if (!url.protocol().isEmpty() && !url.isLocalFile()) {
|
2008-07-01 20:56:43 +02:00
|
|
|
type = RunnerContext::NetworkLocation;
|
2008-06-10 03:08:33 +02:00
|
|
|
} else if (QFile::exists(path)) {
|
2008-05-01 09:51:59 +02:00
|
|
|
QFileInfo info(path);
|
2008-06-11 19:26:36 +02:00
|
|
|
if (info.isSymLink()) {
|
|
|
|
path = info.canonicalFilePath();
|
|
|
|
info = QFileInfo(path);
|
|
|
|
}
|
2008-05-01 09:51:59 +02:00
|
|
|
if (info.isDir()) {
|
2008-07-01 20:56:43 +02:00
|
|
|
type = RunnerContext::Directory;
|
2008-05-01 09:51:59 +02:00
|
|
|
mimeType = "inode/folder";
|
2008-06-11 19:26:36 +02:00
|
|
|
} else if (info.isFile()) {
|
2008-07-01 20:56:43 +02:00
|
|
|
type = RunnerContext::File;
|
2008-05-01 09:51:59 +02:00
|
|
|
KMimeType::Ptr mimeTypePtr = KMimeType::findByPath(path);
|
|
|
|
if (mimeTypePtr) {
|
|
|
|
mimeType = mimeTypePtr->name();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-05-01 21:43:22 +02:00
|
|
|
|
2007-12-02 08:11:50 +01:00
|
|
|
QReadWriteLock lock;
|
2008-05-09 05:49:11 +02:00
|
|
|
QList<QueryMatch> matches;
|
2008-05-12 06:53:42 +02:00
|
|
|
QMap<QString, const QueryMatch*> matchesById;
|
2007-11-02 03:34:46 +01:00
|
|
|
QString term;
|
2008-04-24 17:15:38 +02:00
|
|
|
QString mimeType;
|
2008-05-01 20:40:10 +02:00
|
|
|
RunnerContext::Type type;
|
|
|
|
RunnerContext * q;
|
2007-11-02 03:34:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-05-09 05:49:11 +02:00
|
|
|
RunnerContext::RunnerContext(QObject *parent)
|
2007-11-02 03:34:46 +01:00
|
|
|
: QObject(parent),
|
2008-07-01 20:56:43 +02:00
|
|
|
d(new RunnerContextPrivate(this))
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
//copy ctor
|
2008-05-01 20:40:10 +02:00
|
|
|
RunnerContext::RunnerContext(RunnerContext &other, QObject *parent)
|
2008-05-01 09:51:59 +02:00
|
|
|
: QObject(parent)
|
2007-12-02 08:11:50 +01:00
|
|
|
{
|
2008-05-01 21:43:22 +02:00
|
|
|
LOCK_FOR_READ((&other))
|
|
|
|
d = other.d;
|
|
|
|
UNLOCK((&other))
|
2007-12-02 08:11:50 +01:00
|
|
|
}
|
|
|
|
|
2008-05-01 20:40:10 +02:00
|
|
|
RunnerContext::~RunnerContext()
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-05-01 20:40:10 +02:00
|
|
|
void RunnerContext::reset()
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2008-05-01 09:51:59 +02:00
|
|
|
// We will detach if we are a copy of someone. But we will reset
|
|
|
|
// if we are the 'main' context others copied from. Resetting
|
2008-05-01 20:40:10 +02:00
|
|
|
// one RunnerContext makes all the copies oneobsolete.
|
2008-05-01 09:51:59 +02:00
|
|
|
d.detach();
|
2008-02-19 04:59:21 +01:00
|
|
|
|
2008-05-01 20:26:38 +02:00
|
|
|
// we still have to remove all the matches, since if the
|
2008-05-01 20:40:10 +02:00
|
|
|
// ref count was 1 (e.g. only the RunnerContext is using
|
2008-05-01 20:26:38 +02:00
|
|
|
// the dptr) then we won't get a copy made
|
2008-05-09 05:49:11 +02:00
|
|
|
if (!d->matches.isEmpty()) {
|
2008-05-12 06:53:42 +02:00
|
|
|
d->matchesById.clear();
|
2008-05-09 05:49:11 +02:00
|
|
|
d->matches.clear();
|
|
|
|
emit d->q->matchesChanged();
|
|
|
|
}
|
2008-05-01 20:26:38 +02:00
|
|
|
|
2008-05-09 05:49:11 +02:00
|
|
|
d->term.clear();
|
|
|
|
d->mimeType.clear();
|
|
|
|
d->type = UnknownType;
|
2008-05-01 20:26:38 +02:00
|
|
|
//kDebug() << "match count" << d->matches.count();
|
2008-01-22 05:38:03 +01:00
|
|
|
}
|
2007-11-02 03:34:46 +01:00
|
|
|
|
2008-05-02 20:17:09 +02:00
|
|
|
void RunnerContext::setQuery(const QString &term)
|
2008-01-22 05:38:03 +01:00
|
|
|
{
|
2008-05-01 20:26:38 +02:00
|
|
|
reset();
|
|
|
|
|
2007-11-02 03:34:46 +01:00
|
|
|
if (term.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->term = term;
|
2008-05-01 09:51:59 +02:00
|
|
|
d->determineType();
|
2007-12-02 08:11:50 +01:00
|
|
|
}
|
|
|
|
|
2007-11-02 03:34:46 +01:00
|
|
|
|
2008-05-02 20:17:09 +02:00
|
|
|
QString RunnerContext::query() const
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2008-05-09 05:49:11 +02:00
|
|
|
// the query term should never be set after
|
|
|
|
// a search starts. in fact, reset() ensures this
|
|
|
|
// and setQuery(QString) calls reset()
|
|
|
|
return d->term;
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
2008-05-01 20:40:10 +02:00
|
|
|
RunnerContext::Type RunnerContext::type() const
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
|
|
|
return d->type;
|
|
|
|
}
|
|
|
|
|
2008-05-01 20:40:10 +02:00
|
|
|
QString RunnerContext::mimeType() const
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2008-04-24 17:15:38 +02:00
|
|
|
return d->mimeType;
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
2008-05-09 05:49:11 +02:00
|
|
|
bool RunnerContext::addMatches(const QString& term, const QList<QueryMatch> &matches)
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2008-05-09 05:49:11 +02:00
|
|
|
Q_UNUSED(term)
|
|
|
|
|
|
|
|
if (matches.isEmpty()) {
|
2008-02-19 04:59:21 +01:00
|
|
|
return false;
|
|
|
|
}
|
2007-11-02 03:34:46 +01:00
|
|
|
|
2008-05-01 21:43:22 +02:00
|
|
|
LOCK_FOR_WRITE(this)
|
2008-05-12 06:53:42 +02:00
|
|
|
foreach (const QueryMatch &match, matches) {
|
|
|
|
d->matches.append(match);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (d->matchesById.contains(match.id())) {
|
|
|
|
kDebug() << "Duplicate match id " << match.id() << "from" << match.runner()->name();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
d->matchesById.insert(match.id(), &d->matches.at(d->matches.size() - 1));
|
|
|
|
}
|
2008-05-01 21:43:22 +02:00
|
|
|
UNLOCK(this);
|
2008-05-01 09:51:59 +02:00
|
|
|
//kDebug()<< "add matches";
|
|
|
|
// A copied searchContext may share the d pointer,
|
|
|
|
// we always want to sent the signal of the object that created
|
|
|
|
// the d pointer
|
|
|
|
emit d->q->matchesChanged();
|
2008-02-19 04:59:21 +01:00
|
|
|
return true;
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
2008-05-09 05:49:11 +02:00
|
|
|
bool RunnerContext::addMatch(const QString &term, const QueryMatch &match)
|
2007-12-02 08:11:50 +01:00
|
|
|
{
|
2008-05-09 05:49:11 +02:00
|
|
|
Q_UNUSED(term)
|
2008-02-08 20:10:20 +01:00
|
|
|
|
2008-05-01 21:43:22 +02:00
|
|
|
LOCK_FOR_WRITE(this)
|
2008-05-12 06:53:42 +02:00
|
|
|
d->matches.append(match);
|
|
|
|
d->matchesById.insert(match.id(), &d->matches.at(d->matches.size() - 1));
|
2008-05-01 21:43:22 +02:00
|
|
|
UNLOCK(this);
|
2008-05-01 09:51:59 +02:00
|
|
|
//kDebug()<< "added match" << match->text();
|
|
|
|
emit d->q->matchesChanged();
|
2008-02-19 04:59:21 +01:00
|
|
|
|
2007-12-02 08:11:50 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-05-09 05:49:11 +02:00
|
|
|
QList<QueryMatch> RunnerContext::matches() const
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2008-05-01 21:43:22 +02:00
|
|
|
LOCK_FOR_READ(this)
|
2008-05-09 05:49:11 +02:00
|
|
|
QList<QueryMatch> matches = d->matches;
|
2008-05-01 21:43:22 +02:00
|
|
|
UNLOCK(this);
|
2007-12-02 08:11:50 +01:00
|
|
|
return matches;
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
2008-05-12 06:53:42 +02:00
|
|
|
QueryMatch RunnerContext::match(const QString &id) const
|
|
|
|
{
|
|
|
|
LOCK_FOR_READ(this)
|
|
|
|
if (d->matchesById.contains(id)) {
|
2008-05-19 21:58:06 +02:00
|
|
|
const QueryMatch *match = d->matchesById.value(id);
|
|
|
|
UNLOCK(this)
|
|
|
|
return *match;
|
2008-05-12 06:53:42 +02:00
|
|
|
}
|
|
|
|
UNLOCK(this)
|
|
|
|
|
|
|
|
return QueryMatch(0);
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
2008-05-12 06:53:42 +02:00
|
|
|
} // Plasma namespace
|
|
|
|
|
2008-05-01 20:40:10 +02:00
|
|
|
#include "runnercontext.moc"
|