2008-11-04 00:08:39 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "runnercontext.h"
|
|
|
|
|
2010-05-19 11:51:21 +02:00
|
|
|
#include <cmath>
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
#include <QReadWriteLock>
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QSharedData>
|
|
|
|
|
2008-11-04 03:04:34 +01:00
|
|
|
#include <kcompletion.h>
|
2009-05-13 21:18:30 +02:00
|
|
|
#include <kconfiggroup.h>
|
2008-11-04 03:04:34 +01:00
|
|
|
#include <kdebug.h>
|
|
|
|
#include <kmimetype.h>
|
|
|
|
#include <kshell.h>
|
|
|
|
#include <kstandarddirs.h>
|
|
|
|
#include <kurl.h>
|
2010-01-12 05:14:32 +01:00
|
|
|
#include <kprotocolinfo.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
#include "abstractrunner.h"
|
|
|
|
#include "querymatch.h"
|
|
|
|
|
2009-11-12 20:52:41 +01:00
|
|
|
//#define LOCK_FOR_READ(d) if (d->policy == Shared) { d->lock.lockForRead(); }
|
|
|
|
//#define LOCK_FOR_WRITE(d) if (d->policy == Shared) { d->lock.lockForWrite(); }
|
|
|
|
//#define UNLOCK(d) if (d->policy == Shared) { d->lock.unlock(); }
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-11-12 20:52:41 +01:00
|
|
|
#define LOCK_FOR_READ(d) d->lock.lockForRead();
|
|
|
|
#define LOCK_FOR_WRITE(d) d->lock.lockForWrite();
|
|
|
|
#define UNLOCK(d) d->lock.unlock();
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2009-01-28 15:09:36 +01:00
|
|
|
/*
|
|
|
|
Corrects the case of the last component in a path (e.g. /usr/liB -> /usr/lib)
|
|
|
|
path: The path to be processed.
|
|
|
|
correctCasePath: The corrected-case path
|
|
|
|
mustBeDir: Tells whether the last component is a folder or doesn't matter
|
|
|
|
Returns true on success and false on error, in case of error, correctCasePath is not modified
|
|
|
|
*/
|
|
|
|
bool correctLastComponentCase(const QString &path, QString &correctCasePath, const bool mustBeDir)
|
|
|
|
{
|
|
|
|
//kDebug() << "Correcting " << path;
|
|
|
|
|
|
|
|
// If the file already exists then no need to search for it.
|
|
|
|
if (QFile::exists(path)) {
|
|
|
|
correctCasePath = path;
|
|
|
|
//kDebug() << "Correct path is" << correctCasePath;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QFileInfo pathInfo(path);
|
|
|
|
|
|
|
|
const QDir fileDir = pathInfo.dir();
|
|
|
|
//kDebug() << "Directory is" << fileDir;
|
|
|
|
|
|
|
|
const QString filename = pathInfo.fileName();
|
|
|
|
//kDebug() << "Filename is" << filename;
|
|
|
|
|
|
|
|
//kDebug() << "searching for a" << (mustBeDir ? "directory" : "directory/file");
|
|
|
|
|
|
|
|
const QStringList matchingFilenames = fileDir.entryList(QStringList(filename),
|
|
|
|
mustBeDir ? QDir::Dirs : QDir::NoFilter);
|
|
|
|
|
|
|
|
if (matchingFilenames.empty()) {
|
|
|
|
//kDebug() << "No matches found!!\n";
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
/*if (matchingFilenames.size() > 1) {
|
|
|
|
kDebug() << "Found multiple matches!!\n";
|
|
|
|
}*/
|
|
|
|
|
|
|
|
if (fileDir.path().endsWith(QDir::separator())) {
|
|
|
|
correctCasePath = fileDir.path() + matchingFilenames[0];
|
|
|
|
} else {
|
|
|
|
correctCasePath = fileDir.path() + QDir::separator() + matchingFilenames[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
//kDebug() << "Correct path is" << correctCasePath;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Corrects the case of a path (e.g. /uSr/loCAL/bIN -> /usr/local/bin)
|
|
|
|
path: The path to be processed.
|
|
|
|
corrected: The corrected-case path
|
|
|
|
Returns true on success and false on error, in case of error, corrected is not modified
|
|
|
|
*/
|
2009-03-20 09:25:20 +01:00
|
|
|
bool correctPathCase(const QString& path, QString &corrected)
|
2009-01-28 15:09:36 +01:00
|
|
|
{
|
|
|
|
// early exit check
|
|
|
|
if (QFile::exists(path)) {
|
|
|
|
corrected = path;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// path components
|
|
|
|
QStringList components = QString(path).split(QDir::separator());
|
|
|
|
|
2010-01-31 23:26:58 +01:00
|
|
|
if (components.size() < 1) {
|
2009-01-28 15:09:36 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool mustBeDir = components.back().isEmpty();
|
|
|
|
|
|
|
|
//kDebug() << "Components are" << components;
|
|
|
|
|
2010-01-31 23:26:58 +01:00
|
|
|
if (mustBeDir) {
|
2009-01-28 15:09:36 +01:00
|
|
|
components.pop_back();
|
|
|
|
}
|
|
|
|
|
2010-02-16 03:36:28 +01:00
|
|
|
if (components.isEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-01-31 23:26:58 +01:00
|
|
|
QString correctPath;
|
2009-01-28 15:09:36 +01:00
|
|
|
const unsigned initialComponents = components.size();
|
|
|
|
for (unsigned i = 0; i < initialComponents - 1; i ++) {
|
|
|
|
const QString tmp = components[0] + QDir::separator() + components[1];
|
|
|
|
|
|
|
|
if (!correctLastComponentCase(tmp, correctPath, components.size() > 2 || mustBeDir)) {
|
2009-12-10 10:16:30 +01:00
|
|
|
//kDebug() << "search was not successful";
|
2009-01-28 15:09:36 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
components.removeFirst();
|
|
|
|
components[0] = correctPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
corrected = correctPath;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
class RunnerContextPrivate : public QSharedData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RunnerContextPrivate(RunnerContext *context)
|
|
|
|
: QSharedData(),
|
|
|
|
type(RunnerContext::UnknownType),
|
2009-11-25 10:37:47 +01:00
|
|
|
q(context),
|
|
|
|
singleRunnerQueryMode(false)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
RunnerContextPrivate(const RunnerContextPrivate &p)
|
|
|
|
: QSharedData(),
|
2009-05-13 21:18:30 +02:00
|
|
|
launchCounts(p.launchCounts),
|
2008-11-04 00:08:39 +01:00
|
|
|
type(RunnerContext::None),
|
2009-11-25 10:37:47 +01:00
|
|
|
q(p.q),
|
|
|
|
singleRunnerQueryMode(false)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
//kDebug() << "¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿boo yeah" << type;
|
|
|
|
}
|
|
|
|
|
|
|
|
~RunnerContextPrivate()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines type of query
|
2010-01-31 23:26:58 +01:00
|
|
|
&&
|
2008-11-04 00:08:39 +01:00
|
|
|
*/
|
|
|
|
void determineType()
|
|
|
|
{
|
|
|
|
// NOTE! this method must NEVER be called from
|
|
|
|
// code that may be running in multiple threads
|
|
|
|
// with the same data.
|
|
|
|
type = RunnerContext::UnknownType;
|
|
|
|
QString path = QDir::cleanPath(KShell::tildeExpand(term));
|
|
|
|
|
2008-12-10 19:15:47 +01:00
|
|
|
int space = path.indexOf(' ');
|
|
|
|
if (!KStandardDirs::findExe(path.left(space)).isEmpty()) {
|
|
|
|
// it's a shell command if there's a space because that implies
|
|
|
|
// that it has arguments!
|
|
|
|
type = (space > 0) ? RunnerContext::ShellCommand :
|
|
|
|
RunnerContext::Executable;
|
2008-11-04 00:08:39 +01:00
|
|
|
} else {
|
|
|
|
KUrl url(term);
|
2010-01-31 22:22:13 +01:00
|
|
|
// check for a normal URL first
|
2010-01-31 23:26:58 +01:00
|
|
|
//kDebug() << url << KProtocolInfo::protocolClass(url.protocol()) << url.hasHost() <<
|
|
|
|
// url.host() << url.isLocalFile() << path << path.indexOf('/');
|
|
|
|
const bool hasProtocol = !url.protocol().isEmpty();
|
|
|
|
const bool isLocalProtocol = KProtocolInfo::protocolClass(url.protocol()) == ":local";
|
|
|
|
if (hasProtocol &&
|
|
|
|
((!isLocalProtocol && url.hasHost()) ||
|
|
|
|
(isLocalProtocol && url.protocol() != "file"))) {
|
|
|
|
// we either have a network protocol with a host, so we can show matches for it
|
|
|
|
// or we have a non-file url that may be local so a host isn't required
|
2010-01-31 22:22:13 +01:00
|
|
|
type = RunnerContext::NetworkLocation;
|
2010-01-31 23:26:58 +01:00
|
|
|
} else if (isLocalProtocol) {
|
|
|
|
// at this point in the game, we assume we have a path,
|
|
|
|
// but if a path doesn't have any slashes
|
|
|
|
// it's too ambiguous to be sure we're in a filesystem context
|
|
|
|
path = QDir::cleanPath(url.toLocalFile());
|
|
|
|
//kDebug( )<< "slash check" << path;
|
|
|
|
if (hasProtocol || ((path.indexOf('/') != -1 || path.indexOf('\\') != -1))) {
|
|
|
|
QString correctCasePath;
|
|
|
|
if (correctPathCase(path, correctCasePath)) {
|
|
|
|
path = correctCasePath;
|
|
|
|
QFileInfo info(path);
|
|
|
|
//kDebug( )<< "correct cas epath is" << correctCasePath << info.isSymLink() <<
|
|
|
|
// info.isDir() << info.isFile();
|
|
|
|
|
|
|
|
if (info.isSymLink()) {
|
|
|
|
path = info.canonicalFilePath();
|
|
|
|
info = QFileInfo(path);
|
|
|
|
}
|
|
|
|
if (info.isDir()) {
|
|
|
|
type = RunnerContext::Directory;
|
|
|
|
mimeType = "inode/folder";
|
|
|
|
} else if (info.isFile()) {
|
|
|
|
type = RunnerContext::File;
|
|
|
|
KMimeType::Ptr mimeTypePtr = KMimeType::findByPath(path);
|
|
|
|
if (mimeTypePtr) {
|
|
|
|
mimeType = mimeTypePtr->name();
|
|
|
|
}
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-01-31 23:26:58 +01:00
|
|
|
|
|
|
|
//kDebug() << "term2type" << term << type;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-04-23 00:50:20 +02:00
|
|
|
void invalidate()
|
|
|
|
{
|
|
|
|
q = &s_dummyContext;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
QReadWriteLock lock;
|
|
|
|
QList<QueryMatch> matches;
|
|
|
|
QMap<QString, const QueryMatch*> matchesById;
|
2009-05-13 21:18:30 +02:00
|
|
|
QHash<QString, int> launchCounts;
|
2008-11-04 00:08:39 +01:00
|
|
|
QString term;
|
|
|
|
QString mimeType;
|
|
|
|
RunnerContext::Type type;
|
|
|
|
RunnerContext * q;
|
2009-04-23 00:50:20 +02:00
|
|
|
static RunnerContext s_dummyContext;
|
2009-11-25 10:37:47 +01:00
|
|
|
bool singleRunnerQueryMode;
|
2008-11-04 00:08:39 +01:00
|
|
|
};
|
|
|
|
|
2009-04-23 00:50:20 +02:00
|
|
|
RunnerContext RunnerContextPrivate::s_dummyContext;
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
RunnerContext::RunnerContext(QObject *parent)
|
|
|
|
: QObject(parent),
|
|
|
|
d(new RunnerContextPrivate(this))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//copy ctor
|
|
|
|
RunnerContext::RunnerContext(RunnerContext &other, QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
{
|
2009-11-12 20:52:41 +01:00
|
|
|
LOCK_FOR_READ(other.d)
|
2008-11-04 00:08:39 +01:00
|
|
|
d = other.d;
|
2009-11-12 20:52:41 +01:00
|
|
|
UNLOCK(other.d)
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
RunnerContext::~RunnerContext()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-11-04 01:21:55 +01:00
|
|
|
RunnerContext &RunnerContext::operator=(const RunnerContext &other)
|
|
|
|
{
|
2009-11-04 20:01:43 +01:00
|
|
|
if (this->d == other.d) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2009-11-12 20:52:41 +01:00
|
|
|
QExplicitlySharedDataPointer<Plasma::RunnerContextPrivate> oldD = d;
|
|
|
|
LOCK_FOR_WRITE(d)
|
|
|
|
LOCK_FOR_READ(other.d)
|
2009-11-04 01:21:55 +01:00
|
|
|
d = other.d;
|
2009-11-12 20:52:41 +01:00
|
|
|
UNLOCK(other.d)
|
|
|
|
UNLOCK(oldD)
|
2009-11-04 01:21:55 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void RunnerContext::reset()
|
|
|
|
{
|
|
|
|
// We will detach if we are a copy of someone. But we will reset
|
|
|
|
// if we are the 'main' context others copied from. Resetting
|
2009-03-03 09:02:13 +01:00
|
|
|
// one RunnerContext makes all the copies obsolete.
|
2009-03-03 17:44:35 +01:00
|
|
|
|
2009-04-23 00:50:20 +02:00
|
|
|
// We need to mark the q pointer of the detached RunnerContextPrivate
|
|
|
|
// as dirty on detach to avoid receiving results for old queries
|
|
|
|
d->invalidate();
|
2009-03-03 09:02:13 +01:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
d.detach();
|
2009-03-03 17:44:35 +01:00
|
|
|
|
2009-04-23 00:50:20 +02:00
|
|
|
// Now that we detached the d pointer we need to reset its q pointer
|
|
|
|
|
2009-03-03 17:44:35 +01:00
|
|
|
d->q = this;
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
// we still have to remove all the matches, since if the
|
|
|
|
// ref count was 1 (e.g. only the RunnerContext is using
|
|
|
|
// the dptr) then we won't get a copy made
|
|
|
|
if (!d->matches.isEmpty()) {
|
|
|
|
d->matchesById.clear();
|
|
|
|
d->matches.clear();
|
2009-03-03 09:02:13 +01:00
|
|
|
emit matchesChanged();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d->term.clear();
|
|
|
|
d->mimeType.clear();
|
|
|
|
d->type = UnknownType;
|
2009-11-25 10:37:47 +01:00
|
|
|
d->singleRunnerQueryMode = false;
|
2008-11-04 00:08:39 +01:00
|
|
|
//kDebug() << "match count" << d->matches.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RunnerContext::setQuery(const QString &term)
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
|
|
|
|
if (term.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->term = term;
|
|
|
|
d->determineType();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString RunnerContext::query() const
|
|
|
|
{
|
|
|
|
// the query term should never be set after
|
|
|
|
// a search starts. in fact, reset() ensures this
|
|
|
|
// and setQuery(QString) calls reset()
|
|
|
|
return d->term;
|
|
|
|
}
|
|
|
|
|
|
|
|
RunnerContext::Type RunnerContext::type() const
|
|
|
|
{
|
|
|
|
return d->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString RunnerContext::mimeType() const
|
|
|
|
{
|
|
|
|
return d->mimeType;
|
|
|
|
}
|
|
|
|
|
2009-05-04 01:13:52 +02:00
|
|
|
bool RunnerContext::isValid() const
|
2009-03-03 18:27:28 +01:00
|
|
|
{
|
2009-04-23 00:50:20 +02:00
|
|
|
// if our qptr is dirty, we aren't useful anymore
|
|
|
|
return (d->q != &(d->s_dummyContext));
|
2009-03-03 18:27:28 +01:00
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
bool RunnerContext::addMatches(const QString &term, const QList<QueryMatch> &matches)
|
|
|
|
{
|
|
|
|
Q_UNUSED(term)
|
|
|
|
|
2009-04-23 00:50:20 +02:00
|
|
|
if (matches.isEmpty() || !isValid()) {
|
2009-03-03 17:44:35 +01:00
|
|
|
//Bail out if the query is empty or the qptr is dirty
|
2008-11-04 00:08:39 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-12 20:52:41 +01:00
|
|
|
LOCK_FOR_WRITE(d)
|
2009-05-13 21:18:30 +02:00
|
|
|
foreach (QueryMatch match, matches) {
|
|
|
|
// Give previously launched matches a slight boost in relevance
|
2010-05-19 11:51:21 +02:00
|
|
|
// The boost smoothly saturates to 0.5;
|
2009-05-13 21:18:30 +02:00
|
|
|
if (int count = d->launchCounts.value(match.id())) {
|
2010-05-19 11:51:21 +02:00
|
|
|
match.setRelevance(match.relevance() + 0.5 * (1-exp(-count*0.3)));
|
2009-05-13 21:18:30 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
d->matches.append(match);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (d->matchesById.contains(match.id())) {
|
2010-03-04 23:27:03 +01:00
|
|
|
kDebug() << "Duplicate match id " << match.id() << "from" << match.runner()->name();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
d->matchesById.insert(match.id(), &d->matches.at(d->matches.size() - 1));
|
|
|
|
}
|
2009-11-12 20:52:41 +01:00
|
|
|
UNLOCK(d);
|
2008-11-04 00:08:39 +01: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();
|
2009-03-03 17:44:35 +01:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RunnerContext::addMatch(const QString &term, const QueryMatch &match)
|
|
|
|
{
|
|
|
|
Q_UNUSED(term)
|
|
|
|
|
2009-04-23 00:50:20 +02:00
|
|
|
if (!isValid()) {
|
2009-03-03 17:44:35 +01:00
|
|
|
// Bail out if the qptr is dirty
|
2009-03-03 09:02:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
2009-03-03 17:44:35 +01:00
|
|
|
|
2009-05-13 21:18:30 +02:00
|
|
|
QueryMatch m(match); // match must be non-const to modify relevance
|
|
|
|
|
2009-11-12 20:52:41 +01:00
|
|
|
LOCK_FOR_WRITE(d)
|
2009-05-13 21:18:30 +02:00
|
|
|
|
|
|
|
if (int count = d->launchCounts.value(m.id())) {
|
|
|
|
m.setRelevance(m.relevance() + 0.05 * count);
|
|
|
|
}
|
|
|
|
|
|
|
|
d->matches.append(m);
|
|
|
|
d->matchesById.insert(m.id(), &d->matches.at(d->matches.size() - 1));
|
2009-11-12 20:52:41 +01:00
|
|
|
UNLOCK(d);
|
2008-11-04 00:08:39 +01:00
|
|
|
//kDebug()<< "added match" << match->text();
|
2009-04-23 00:50:20 +02:00
|
|
|
emit d->q->matchesChanged();
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-11-24 13:05:46 +01:00
|
|
|
bool RunnerContext::removeMatches(const QStringList matchIdList)
|
|
|
|
{
|
|
|
|
if (!isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList presentMatchIdList;
|
|
|
|
QList<const QueryMatch*> presentMatchList;
|
|
|
|
|
|
|
|
LOCK_FOR_READ(d)
|
2009-12-27 23:16:11 +01:00
|
|
|
foreach(const QString &matchId, matchIdList) {
|
2009-11-24 13:05:46 +01:00
|
|
|
const QueryMatch* match = d->matchesById.value(matchId, 0);
|
|
|
|
if (match) {
|
|
|
|
presentMatchList << match;
|
|
|
|
presentMatchIdList << matchId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
UNLOCK(d)
|
|
|
|
|
|
|
|
if (presentMatchIdList.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOCK_FOR_WRITE(d)
|
|
|
|
foreach(const QueryMatch *match, presentMatchList) {
|
|
|
|
d->matches.removeAll(*match);
|
|
|
|
}
|
2009-12-27 23:16:11 +01:00
|
|
|
foreach(const QString &matchId, presentMatchIdList) {
|
2009-11-24 13:05:46 +01:00
|
|
|
d->matchesById.remove(matchId);
|
|
|
|
}
|
|
|
|
UNLOCK(d)
|
|
|
|
|
|
|
|
emit d->q->matchesChanged();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RunnerContext::removeMatch(const QString matchId)
|
|
|
|
{
|
|
|
|
if (!isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
LOCK_FOR_READ(d)
|
|
|
|
const QueryMatch* match = d->matchesById.value(matchId, 0);
|
|
|
|
UNLOCK(d)
|
|
|
|
if (!match) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
LOCK_FOR_WRITE(d)
|
|
|
|
d->matches.removeAll(*match);
|
|
|
|
d->matchesById.remove(matchId);
|
|
|
|
UNLOCK(d)
|
|
|
|
emit d->q->matchesChanged();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-02 13:46:55 +01:00
|
|
|
bool RunnerContext::removeMatches(Plasma::AbstractRunner *runner)
|
|
|
|
{
|
|
|
|
if (!isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QueryMatch> presentMatchList;
|
|
|
|
|
|
|
|
LOCK_FOR_READ(d)
|
|
|
|
foreach(const QueryMatch &match, d->matches) {
|
|
|
|
if (match.runner() == runner) {
|
|
|
|
presentMatchList << match;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
UNLOCK(d)
|
|
|
|
|
|
|
|
if (presentMatchList.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOCK_FOR_WRITE(d)
|
|
|
|
foreach (const QueryMatch &match, presentMatchList) {
|
|
|
|
d->matchesById.remove(match.id());
|
|
|
|
d->matches.removeAll(match);
|
|
|
|
}
|
|
|
|
UNLOCK(d)
|
|
|
|
|
|
|
|
emit d->q->matchesChanged();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
QList<QueryMatch> RunnerContext::matches() const
|
|
|
|
{
|
2009-11-12 20:52:41 +01:00
|
|
|
LOCK_FOR_READ(d)
|
2008-11-04 00:08:39 +01:00
|
|
|
QList<QueryMatch> matches = d->matches;
|
2009-11-12 20:52:41 +01:00
|
|
|
UNLOCK(d);
|
2008-11-04 00:08:39 +01:00
|
|
|
return matches;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryMatch RunnerContext::match(const QString &id) const
|
|
|
|
{
|
2009-11-12 20:52:41 +01:00
|
|
|
LOCK_FOR_READ(d)
|
2008-12-11 03:52:06 +01:00
|
|
|
const QueryMatch *match = d->matchesById.value(id, 0);
|
2009-11-12 20:52:41 +01:00
|
|
|
UNLOCK(d)
|
2008-12-11 03:52:06 +01:00
|
|
|
|
|
|
|
if (match) {
|
2008-11-04 00:08:39 +01:00
|
|
|
return *match;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QueryMatch(0);
|
|
|
|
}
|
|
|
|
|
2009-11-25 10:37:47 +01:00
|
|
|
void RunnerContext::setSingleRunnerQueryMode(bool enabled)
|
|
|
|
{
|
|
|
|
d->singleRunnerQueryMode = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RunnerContext::singleRunnerQueryMode() const
|
|
|
|
{
|
|
|
|
return d->singleRunnerQueryMode;
|
|
|
|
}
|
|
|
|
|
2009-05-13 21:18:30 +02:00
|
|
|
void RunnerContext::restore(const KConfigGroup &config)
|
|
|
|
{
|
2009-07-06 13:37:02 +02:00
|
|
|
const QStringList cfgList = config.readEntry("LaunchCounts", QStringList());
|
2009-05-13 21:18:30 +02:00
|
|
|
|
2009-07-06 13:37:02 +02:00
|
|
|
const QRegExp r("(\\d*) (.*)");
|
|
|
|
foreach (const QString& entry, cfgList) {
|
2009-05-13 21:18:30 +02:00
|
|
|
r.indexIn(entry);
|
|
|
|
int count = r.cap(1).toInt();
|
|
|
|
QString id = r.cap(2);
|
|
|
|
d->launchCounts[id] = count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RunnerContext::save(KConfigGroup &config)
|
|
|
|
{
|
|
|
|
QStringList countList;
|
|
|
|
|
|
|
|
typedef QHash<QString, int>::const_iterator Iterator;
|
|
|
|
Iterator end = d->launchCounts.constEnd();
|
|
|
|
for (Iterator i = d->launchCounts.constBegin(); i != end; ++i) {
|
|
|
|
countList << QString("%2 %1").arg(i.key()).arg(i.value());
|
|
|
|
}
|
|
|
|
|
|
|
|
config.writeEntry("LaunchCounts", countList);
|
|
|
|
config.sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RunnerContext::run(const QueryMatch &match)
|
|
|
|
{
|
|
|
|
++d->launchCounts[match.id()];
|
|
|
|
match.run(*this);
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
} // Plasma namespace
|
|
|
|
|
|
|
|
#include "runnercontext.moc"
|