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>
|
|
|
|
|
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-02 20:46:05 +02:00
|
|
|
#include "querymatch.h"
|
2007-11-02 03:34:46 +01:00
|
|
|
|
2008-05-01 21:43:22 +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(); }
|
|
|
|
/*
|
|
|
|
#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();
|
|
|
|
*/
|
2007-11-02 03:34:46 +01:00
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2008-05-01 20:40:10 +02:00
|
|
|
class RunnerContext::Private : public QSharedData
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
|
|
|
public:
|
2008-05-01 20:40:10 +02:00
|
|
|
Private(RunnerContext *context, RunnerContext::DataPolicy p)
|
2008-05-01 20:26:38 +02:00
|
|
|
: QSharedData(),
|
2008-05-01 20:40:10 +02:00
|
|
|
type(RunnerContext::UnknownType),
|
2008-05-01 20:26:38 +02:00
|
|
|
q(context),
|
2008-02-11 07:04:20 +01:00
|
|
|
policy(p)
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-05-01 20:40:10 +02:00
|
|
|
Private(const RunnerContext::Private& p)
|
2008-05-01 20:26:38 +02:00
|
|
|
: QSharedData(),
|
|
|
|
term(p.term),
|
2008-05-01 09:51:59 +02:00
|
|
|
mimeType(p.mimeType),
|
|
|
|
type(p.type),
|
|
|
|
q(p.q),
|
|
|
|
policy(p.policy)
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2008-05-01 20:26:38 +02:00
|
|
|
//kDebug() << "¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿boo yeah";
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
~Private()
|
2007-12-02 08:11:50 +01:00
|
|
|
{
|
2008-02-11 07:04:20 +01:00
|
|
|
if (policy == Shared) {
|
|
|
|
lock.lockForWrite();
|
|
|
|
}
|
2008-05-01 21:43:22 +02:00
|
|
|
qDeleteAll(matches);
|
|
|
|
matches.clear();
|
2008-02-11 07:04:20 +01:00
|
|
|
if (policy == Shared) {
|
|
|
|
lock.unlock();
|
|
|
|
}
|
2007-12-02 08:11:50 +01:00
|
|
|
}
|
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
/**
|
|
|
|
* Determines type of query
|
|
|
|
*/
|
|
|
|
void determineType()
|
|
|
|
{
|
2008-05-01 21:43:22 +02:00
|
|
|
if (policy == Shared) {
|
|
|
|
lock.lockForWrite();
|
|
|
|
}
|
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
QString path = 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()) {
|
|
|
|
type = ShellCommand;
|
|
|
|
}
|
|
|
|
} else if (!KStandardDirs::findExe(path.left(space)).isEmpty()) {
|
|
|
|
type = Executable;
|
|
|
|
} else {
|
|
|
|
KUrl url(term);
|
|
|
|
if (!url.protocol().isEmpty() && !url.host().isEmpty()) {
|
|
|
|
type = NetworkLocation;
|
|
|
|
} else if (QFile::exists(path)) {
|
|
|
|
QFileInfo info(path);
|
|
|
|
if (info.isDir()) {
|
|
|
|
type = Directory;
|
|
|
|
mimeType = "inode/folder";
|
|
|
|
} else {
|
|
|
|
type = File;
|
|
|
|
KMimeType::Ptr mimeTypePtr = KMimeType::findByPath(path);
|
|
|
|
if (mimeTypePtr) {
|
|
|
|
mimeType = mimeTypePtr->name();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (term.contains('.')) {
|
|
|
|
// default to a network location so we can can do things like www.kde.org
|
|
|
|
type = NetworkLocation;
|
|
|
|
}
|
|
|
|
}
|
2008-05-01 21:43:22 +02:00
|
|
|
|
|
|
|
if (policy == Shared) {
|
|
|
|
lock.unlock();
|
|
|
|
}
|
2008-05-01 09:51:59 +02:00
|
|
|
}
|
2008-05-01 21:43:22 +02:00
|
|
|
|
2007-12-02 08:11:50 +01:00
|
|
|
QReadWriteLock lock;
|
2008-05-02 20:46:05 +02:00
|
|
|
QList<QueryMatch*> matches;
|
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;
|
|
|
|
const RunnerContext::DataPolicy policy;
|
2007-11-02 03:34:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-05-01 20:40:10 +02:00
|
|
|
RunnerContext::RunnerContext(QObject *parent, DataPolicy policy)
|
2007-11-02 03:34:46 +01:00
|
|
|
: QObject(parent),
|
2008-05-01 09:51:59 +02:00
|
|
|
d(new Private(this, policy))
|
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
|
|
|
// Locks are needed as other contexts can be copied of this one
|
|
|
|
|
|
|
|
// 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-02 07:31:24 +02:00
|
|
|
LOCK_FOR_WRITE(this)
|
2008-05-01 09:51:59 +02:00
|
|
|
//kDebug() << "reset searchContext";
|
2008-05-01 20:40:10 +02:00
|
|
|
d->type = RunnerContext::UnknownType;
|
2008-02-19 04:59:21 +01:00
|
|
|
d->term.clear();
|
2008-04-24 17:15:38 +02:00
|
|
|
d->mimeType.clear();
|
2008-05-01 21:43:22 +02:00
|
|
|
UNLOCK(this);
|
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-01 09:51:59 +02:00
|
|
|
removeAllMatches();
|
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;
|
|
|
|
}
|
|
|
|
|
2008-05-01 21:43:22 +02:00
|
|
|
LOCK_FOR_WRITE(this)
|
2007-11-02 03:34:46 +01:00
|
|
|
d->term = term;
|
2008-05-01 21:43:22 +02:00
|
|
|
UNLOCK(this);
|
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-01 21:43:22 +02:00
|
|
|
LOCK_FOR_READ(this)
|
2007-12-02 08:11:50 +01:00
|
|
|
QString term = d->term;
|
2008-05-01 21:43:22 +02:00
|
|
|
UNLOCK(this);
|
2007-12-02 08:11:50 +01:00
|
|
|
return 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-02 20:46:05 +02:00
|
|
|
bool RunnerContext::addMatches(const QString& term, const QList<QueryMatch*> &matches)
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2008-05-02 20:17:09 +02:00
|
|
|
if (query() != term || 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-02-19 04:59:21 +01:00
|
|
|
d->matches << matches;
|
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-02 20:46:05 +02:00
|
|
|
bool RunnerContext::addMatch(const QString &term, QueryMatch *match)
|
2007-12-02 08:11:50 +01:00
|
|
|
{
|
2008-05-02 20:17:09 +02:00
|
|
|
if (query() != term) {
|
2007-12-02 08:11:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
2008-02-08 20:10:20 +01:00
|
|
|
|
2008-05-01 21:43:22 +02:00
|
|
|
LOCK_FOR_WRITE(this)
|
2008-02-19 04:59:21 +01:00
|
|
|
d->matches << match;
|
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-02-11 07:04:20 +01:00
|
|
|
|
2008-05-02 20:46:05 +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-02 20:46:05 +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-01 20:40:10 +02:00
|
|
|
void RunnerContext::removeAllMatches()
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2008-05-01 21:43:22 +02:00
|
|
|
LOCK_FOR_WRITE(this)
|
2008-05-01 20:26:38 +02:00
|
|
|
if (!d->matches.isEmpty()) {
|
2008-05-02 20:46:05 +02:00
|
|
|
QList<QueryMatch*> matches = d->matches;
|
2008-05-01 09:51:59 +02:00
|
|
|
d->matches.clear();
|
2008-05-01 21:43:22 +02:00
|
|
|
UNLOCK(this);
|
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
// in case someone is still holding on to the Matches
|
|
|
|
// when we emit the matchesChanged() signal, we don't
|
|
|
|
// delete the matches until after the signal is handled.
|
|
|
|
// a bit safer.
|
2008-05-01 21:43:22 +02:00
|
|
|
emit d->q->matchesChanged();
|
|
|
|
|
|
|
|
qDeleteAll(matches);
|
|
|
|
} else {
|
|
|
|
UNLOCK(this);
|
2008-05-01 09:51:59 +02:00
|
|
|
}
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-05-01 20:40:10 +02:00
|
|
|
#include "runnercontext.moc"
|