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 "querymatch.h"
|
|
|
|
|
|
|
|
#include <QAction>
|
2009-10-13 23:42:22 +02:00
|
|
|
#include <QIcon>
|
2011-01-16 22:46:55 +01:00
|
|
|
#include <QReadWriteLock>
|
2008-11-04 00:08:39 +01:00
|
|
|
#include <QSharedData>
|
|
|
|
#include <QStringList>
|
2009-10-13 23:42:22 +02:00
|
|
|
#include <QVariant>
|
|
|
|
#include <QWeakPointer>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2008-11-04 03:04:34 +01:00
|
|
|
#include <kdebug.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
#include "abstractrunner.h"
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class QueryMatchPrivate : public QSharedData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
QueryMatchPrivate(AbstractRunner *r)
|
|
|
|
: QSharedData(),
|
2011-01-16 22:46:55 +01:00
|
|
|
lock(new QReadWriteLock(QReadWriteLock::Recursive)),
|
2008-11-04 00:08:39 +01:00
|
|
|
runner(r),
|
|
|
|
type(QueryMatch::ExactMatch),
|
|
|
|
relevance(.7),
|
2010-01-11 21:08:03 +01:00
|
|
|
selAction(0),
|
|
|
|
enabled(true),
|
|
|
|
idSetByData(false)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-01-16 22:46:55 +01:00
|
|
|
QueryMatchPrivate(const QueryMatchPrivate &other)
|
|
|
|
: QSharedData(other),
|
|
|
|
lock(new QReadWriteLock(QReadWriteLock::Recursive))
|
|
|
|
{
|
|
|
|
QReadLocker lock(other.lock);
|
|
|
|
runner = other.runner;
|
|
|
|
type = other.type;
|
|
|
|
relevance = other.relevance;
|
|
|
|
selAction = other.selAction;
|
|
|
|
enabled = other.enabled;
|
|
|
|
idSetByData = other.idSetByData;
|
|
|
|
id = other.id;
|
|
|
|
text = other.text;
|
|
|
|
subtext = other.subtext;
|
|
|
|
icon = other.icon;
|
|
|
|
data = other.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
~QueryMatchPrivate()
|
|
|
|
{
|
|
|
|
delete lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
QReadWriteLock *lock;
|
2009-10-13 23:42:22 +02:00
|
|
|
QWeakPointer<AbstractRunner> runner;
|
2008-11-04 00:08:39 +01:00
|
|
|
QueryMatch::Type type;
|
|
|
|
QString id;
|
|
|
|
QString text;
|
|
|
|
QString subtext;
|
|
|
|
QIcon icon;
|
|
|
|
QVariant data;
|
|
|
|
qreal relevance;
|
|
|
|
QAction *selAction;
|
2010-01-11 21:08:03 +01:00
|
|
|
bool enabled : 1;
|
|
|
|
bool idSetByData : 1;
|
2008-11-04 00:08:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
QueryMatch::QueryMatch(AbstractRunner *runner)
|
|
|
|
: d(new QueryMatchPrivate(runner))
|
|
|
|
{
|
|
|
|
// kDebug() << "new match created";
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryMatch::QueryMatch(const QueryMatch &other)
|
|
|
|
: d(other.d)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryMatch::~QueryMatch()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QueryMatch::isValid() const
|
|
|
|
{
|
|
|
|
return d->runner != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString QueryMatch::id() const
|
|
|
|
{
|
2009-10-13 23:42:22 +02:00
|
|
|
if (d->id.isEmpty() && d->runner) {
|
|
|
|
return d->runner.data()->id();
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
return d->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QueryMatch::setType(Type type)
|
|
|
|
{
|
|
|
|
d->type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryMatch::Type QueryMatch::type() const
|
|
|
|
{
|
|
|
|
return d->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QueryMatch::setRelevance(qreal relevance)
|
|
|
|
{
|
2009-05-13 21:18:30 +02:00
|
|
|
d->relevance = qMax(qreal(0.0), relevance);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
qreal QueryMatch::relevance() const
|
|
|
|
{
|
|
|
|
return d->relevance;
|
|
|
|
}
|
|
|
|
|
|
|
|
AbstractRunner* QueryMatch::runner() const
|
|
|
|
{
|
2009-10-13 23:42:22 +02:00
|
|
|
return d->runner.data();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void QueryMatch::setText(const QString &text)
|
|
|
|
{
|
2011-01-16 22:46:55 +01:00
|
|
|
QWriteLocker locker(d->lock);
|
2008-11-04 00:08:39 +01:00
|
|
|
d->text = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QueryMatch::setSubtext(const QString &subtext)
|
|
|
|
{
|
2011-01-16 22:46:55 +01:00
|
|
|
QWriteLocker locker(d->lock);
|
2008-11-04 00:08:39 +01:00
|
|
|
d->subtext = subtext;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QueryMatch::setData(const QVariant & data)
|
|
|
|
{
|
2011-01-16 22:46:55 +01:00
|
|
|
QWriteLocker locker(d->lock);
|
2008-11-04 00:08:39 +01:00
|
|
|
d->data = data;
|
2010-01-11 21:08:03 +01:00
|
|
|
|
|
|
|
if (d->id.isEmpty() || d->idSetByData) {
|
|
|
|
const QString id = data.toString();
|
|
|
|
if (!id.isEmpty()) {
|
|
|
|
setId(data.toString());
|
|
|
|
d->idSetByData = true;
|
|
|
|
}
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void QueryMatch::setId(const QString &id)
|
|
|
|
{
|
2011-01-16 22:46:55 +01:00
|
|
|
QWriteLocker locker(d->lock);
|
2008-11-04 00:08:39 +01:00
|
|
|
if (d->runner) {
|
2009-10-13 23:42:22 +02:00
|
|
|
d->id = d->runner.data()->id();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!id.isEmpty()) {
|
|
|
|
d->id.append('_').append(id);
|
|
|
|
}
|
2010-01-11 21:08:03 +01:00
|
|
|
|
|
|
|
d->idSetByData = false;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void QueryMatch::setIcon(const QIcon &icon)
|
|
|
|
{
|
2011-01-16 22:46:55 +01:00
|
|
|
QWriteLocker locker(d->lock);
|
2008-11-04 00:08:39 +01:00
|
|
|
d->icon = icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant QueryMatch::data() const
|
|
|
|
{
|
2011-01-16 22:46:55 +01:00
|
|
|
QReadLocker locker(d->lock);
|
2008-11-04 00:08:39 +01:00
|
|
|
return d->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString QueryMatch::text() const
|
|
|
|
{
|
2011-01-16 22:46:55 +01:00
|
|
|
QReadLocker locker(d->lock);
|
2008-11-04 00:08:39 +01:00
|
|
|
return d->text;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString QueryMatch::subtext() const
|
|
|
|
{
|
2011-01-16 22:46:55 +01:00
|
|
|
QReadLocker locker(d->lock);
|
2008-11-04 00:08:39 +01:00
|
|
|
return d->subtext;
|
|
|
|
}
|
|
|
|
|
|
|
|
QIcon QueryMatch::icon() const
|
|
|
|
{
|
2011-01-16 22:46:55 +01:00
|
|
|
QReadLocker locker(d->lock);
|
2008-11-04 00:08:39 +01:00
|
|
|
return d->icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QueryMatch::setEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
d->enabled = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QueryMatch::isEnabled() const
|
|
|
|
{
|
|
|
|
return d->enabled && d->runner;
|
|
|
|
}
|
|
|
|
|
|
|
|
QAction* QueryMatch::selectedAction() const
|
|
|
|
{
|
|
|
|
return d->selAction;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QueryMatch::setSelectedAction(QAction *action)
|
|
|
|
{
|
|
|
|
d->selAction = action;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QueryMatch::operator<(const QueryMatch &other) const
|
|
|
|
{
|
|
|
|
if (d->type == other.d->type) {
|
|
|
|
if (isEnabled() != other.isEnabled()) {
|
|
|
|
return other.isEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d->relevance != other.d->relevance) {
|
|
|
|
return d->relevance < other.d->relevance;
|
|
|
|
}
|
|
|
|
|
2011-01-16 22:46:55 +01:00
|
|
|
QReadLocker locker(d->lock);
|
|
|
|
QReadLocker otherLocker(other.d->lock);
|
2008-11-04 00:08:39 +01:00
|
|
|
// when resorting to sort by alpha, we want the
|
|
|
|
// reverse sort order!
|
|
|
|
return d->text > other.d->text;
|
|
|
|
}
|
|
|
|
|
|
|
|
return d->type < other.d->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryMatch &QueryMatch::operator=(const QueryMatch &other)
|
|
|
|
{
|
|
|
|
if (d != other.d) {
|
|
|
|
d = other.d;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2009-11-24 13:05:46 +01:00
|
|
|
bool QueryMatch::operator==(const QueryMatch &other) const
|
|
|
|
{
|
|
|
|
return (d == other.d);
|
|
|
|
}
|
|
|
|
|
2012-05-11 15:46:29 +02:00
|
|
|
bool QueryMatch::operator!=(const QueryMatch &other) const
|
|
|
|
{
|
|
|
|
return (d != other.d);
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void QueryMatch::run(const RunnerContext &context) const
|
|
|
|
{
|
|
|
|
//kDebug() << "we run the term" << context->query() << "whose type is" << context->mimetype();
|
|
|
|
if (d->runner) {
|
2009-10-13 23:42:22 +02:00
|
|
|
d->runner.data()->run(context, *this);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-05 08:22:53 +02:00
|
|
|
bool QueryMatch::hasConfigurationInterface() const
|
|
|
|
{
|
2009-10-13 23:42:22 +02:00
|
|
|
return d->runner && d->runner.data()->hasRunOptions();
|
2009-04-05 08:22:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void QueryMatch::createConfigurationInterface(QWidget *parent)
|
|
|
|
{
|
|
|
|
if (hasConfigurationInterface()) {
|
2009-10-13 23:42:22 +02:00
|
|
|
d->runner.data()->createRunOptions(parent);
|
2009-04-05 08:22:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
} // Plasma namespace
|
|
|
|
|