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-02 20:46:05 +02:00
|
|
|
#include "querymatch.h"
|
2007-12-10 19:04:45 +01:00
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
#include <QPointer>
|
2007-12-10 19:04:45 +01:00
|
|
|
#include <QVariant>
|
2008-05-09 05:49:11 +02:00
|
|
|
#include <QSharedData>
|
2007-12-10 19:04:45 +01:00
|
|
|
#include <QStringList>
|
|
|
|
#include <QIcon>
|
|
|
|
|
2008-01-22 05:38:03 +01:00
|
|
|
#include <KDebug>
|
|
|
|
|
2007-11-02 03:34:46 +01:00
|
|
|
#include "abstractrunner.h"
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
class QueryMatchPrivate : public QSharedData
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
|
|
|
public:
|
2008-07-01 20:56:43 +02:00
|
|
|
QueryMatchPrivate(AbstractRunner *r)
|
2008-05-09 05:49:11 +02:00
|
|
|
: QSharedData(),
|
|
|
|
runner(r),
|
2008-05-02 20:46:05 +02:00
|
|
|
type(QueryMatch::ExactMatch),
|
2007-12-02 08:11:50 +01:00
|
|
|
enabled(true),
|
2008-02-09 05:55:43 +01:00
|
|
|
relevance(.7)
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
|
|
|
}
|
2008-01-22 05:38:03 +01:00
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
QPointer<AbstractRunner> runner;
|
2008-05-02 20:46:05 +02:00
|
|
|
QueryMatch::Type type;
|
2008-05-06 20:43:55 +02:00
|
|
|
QString id;
|
2007-12-02 08:11:50 +01:00
|
|
|
QString text;
|
2008-02-19 12:24:42 +01:00
|
|
|
QString subtext;
|
2007-12-02 08:11:50 +01:00
|
|
|
QIcon icon;
|
|
|
|
QVariant data;
|
|
|
|
bool enabled;
|
2007-11-02 03:34:46 +01:00
|
|
|
qreal relevance;
|
|
|
|
};
|
|
|
|
|
2008-05-02 20:46:05 +02:00
|
|
|
QueryMatch::QueryMatch(AbstractRunner *runner)
|
2008-07-01 20:56:43 +02:00
|
|
|
: d(new QueryMatchPrivate(runner))
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2008-05-09 07:06:54 +02:00
|
|
|
if (runner) {
|
2008-05-09 06:32:54 +02:00
|
|
|
d->id = runner->id();
|
|
|
|
}
|
2008-05-01 09:51:59 +02:00
|
|
|
// kDebug() << "new match created";
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
2008-05-09 05:49:11 +02:00
|
|
|
QueryMatch::QueryMatch(const QueryMatch &other)
|
|
|
|
: d(other.d)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-05-02 20:46:05 +02:00
|
|
|
QueryMatch::~QueryMatch()
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-05-09 06:32:54 +02:00
|
|
|
bool QueryMatch::isValid() const
|
|
|
|
{
|
|
|
|
return d->runner != 0;
|
|
|
|
}
|
|
|
|
|
2008-05-06 20:43:55 +02:00
|
|
|
QString QueryMatch::id() const
|
|
|
|
{
|
|
|
|
return d->id;
|
|
|
|
}
|
|
|
|
|
2008-05-02 20:46:05 +02:00
|
|
|
void QueryMatch::setType(Type type)
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
|
|
|
d->type = type;
|
|
|
|
}
|
|
|
|
|
2008-05-02 20:46:05 +02:00
|
|
|
QueryMatch::Type QueryMatch::type() const
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
|
|
|
return d->type;
|
|
|
|
}
|
|
|
|
|
2008-05-02 20:46:05 +02:00
|
|
|
void QueryMatch::setRelevance(qreal relevance)
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2007-12-14 02:38:32 +01:00
|
|
|
d->relevance = qMax(qreal(0.0), qMin(qreal(1.0), relevance));
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
2008-05-02 20:46:05 +02:00
|
|
|
qreal QueryMatch::relevance() const
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
|
|
|
return d->relevance;
|
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
AbstractRunner *QueryMatch::runner() const
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
|
|
|
return d->runner;
|
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
void QueryMatch::setText(const QString &text)
|
2007-12-02 08:11:50 +01:00
|
|
|
{
|
|
|
|
d->text = text;
|
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
void QueryMatch::setSubtext(const QString &subtext)
|
2008-02-19 12:24:42 +01:00
|
|
|
{
|
|
|
|
d->subtext = subtext;
|
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
void QueryMatch::setData(const QVariant & data)
|
2007-12-02 08:11:50 +01:00
|
|
|
{
|
|
|
|
d->data = data;
|
2008-05-08 00:27:51 +02:00
|
|
|
setId(data.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
void QueryMatch::setId(const QString &id)
|
|
|
|
{
|
2008-05-09 06:32:54 +02:00
|
|
|
if (d->runner) {
|
|
|
|
d->id = d->runner->id();
|
|
|
|
}
|
|
|
|
|
2008-05-08 00:27:51 +02:00
|
|
|
if (!id.isEmpty()) {
|
|
|
|
d->id.append('_').append(id);
|
|
|
|
}
|
2007-12-02 08:11:50 +01:00
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
void QueryMatch::setIcon(const QIcon &icon)
|
2007-12-02 08:11:50 +01:00
|
|
|
{
|
|
|
|
d->icon = icon;
|
|
|
|
}
|
|
|
|
|
2008-05-02 20:46:05 +02:00
|
|
|
QVariant QueryMatch::data() const
|
2007-12-02 08:11:50 +01:00
|
|
|
{
|
|
|
|
return d->data;
|
|
|
|
}
|
|
|
|
|
2008-05-02 20:46:05 +02:00
|
|
|
QString QueryMatch::text() const
|
2007-12-02 08:11:50 +01:00
|
|
|
{
|
|
|
|
return d->text;
|
|
|
|
}
|
|
|
|
|
2008-05-02 20:46:05 +02:00
|
|
|
QString QueryMatch::subtext() const
|
2008-02-19 12:24:42 +01:00
|
|
|
{
|
|
|
|
return d->subtext;
|
|
|
|
}
|
|
|
|
|
2008-05-02 20:46:05 +02:00
|
|
|
QIcon QueryMatch::icon() const
|
2007-12-02 08:11:50 +01:00
|
|
|
{
|
|
|
|
return d->icon;
|
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
void QueryMatch::setEnabled(bool enabled)
|
2007-12-02 08:11:50 +01:00
|
|
|
{
|
|
|
|
d->enabled = enabled;
|
|
|
|
}
|
|
|
|
|
2008-05-02 20:46:05 +02:00
|
|
|
bool QueryMatch::isEnabled() const
|
2007-12-02 08:11:50 +01:00
|
|
|
{
|
2008-05-09 19:19:27 +02:00
|
|
|
return d->enabled && d->runner;
|
2007-12-02 08:11:50 +01:00
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
bool QueryMatch::operator<(const QueryMatch &other) const
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2008-05-20 04:47:13 +02:00
|
|
|
if (d->type == other.d->type) {
|
|
|
|
if (isEnabled() != other.isEnabled()) {
|
|
|
|
return other.isEnabled();
|
|
|
|
}
|
|
|
|
|
2008-05-21 07:18:30 +02:00
|
|
|
if (d->relevance != other.d->relevance) {
|
|
|
|
return d->relevance < other.d->relevance;
|
|
|
|
}
|
|
|
|
|
|
|
|
// when resorting to sort by alpha, we want the
|
|
|
|
// reverse sort order!
|
|
|
|
return d->text > other.d->text;
|
2008-05-20 04:47:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return d->type < other.d->type;
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
QueryMatch &QueryMatch::operator=(const QueryMatch &other)
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2008-05-09 07:06:54 +02:00
|
|
|
if (d != other.d) {
|
|
|
|
d = other.d;
|
|
|
|
}
|
|
|
|
|
2008-05-09 05:49:11 +02:00
|
|
|
return *this;
|
|
|
|
}
|
2008-01-22 05:38:03 +01:00
|
|
|
|
2008-05-09 05:49:11 +02:00
|
|
|
void QueryMatch::run(const RunnerContext &context) const
|
|
|
|
{
|
2008-05-02 20:17:09 +02:00
|
|
|
//kDebug() << "we run the term" << context->query() << "whose type is" << context->mimetype();
|
2007-12-03 18:18:28 +01:00
|
|
|
if (d->runner) {
|
2008-05-09 05:49:11 +02:00
|
|
|
d->runner->run(context, *this);
|
2007-12-02 08:11:50 +01:00
|
|
|
}
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
2008-01-22 05:38:03 +01:00
|
|
|
} // Plasma namespace
|
|
|
|
|