2007-02-28 01:41:09 +01:00
|
|
|
/*
|
2007-08-06 13:20:02 +02:00
|
|
|
* Copyright 2006-2007 Aaron Seigo <aseigo@kde.org>
|
2007-02-28 01:41:09 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-08-06 13:20:02 +02:00
|
|
|
* 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.
|
2007-02-28 01:41:09 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2007-05-21 16:28:03 +02:00
|
|
|
#include "abstractrunner.h"
|
|
|
|
|
2007-10-06 00:21:25 +02:00
|
|
|
#include <KDebug>
|
2007-10-31 05:44:09 +01:00
|
|
|
#include <KMimeType>
|
|
|
|
#include <KServiceTypeTrader>
|
|
|
|
#include <KUriFilterData>
|
|
|
|
|
2007-02-28 01:41:09 +01:00
|
|
|
|
2007-03-02 06:27:33 +01:00
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2007-10-31 05:44:09 +01:00
|
|
|
class SearchContext::Private
|
2007-02-28 01:41:09 +01:00
|
|
|
{
|
|
|
|
public:
|
2007-10-31 05:44:09 +01:00
|
|
|
Private()
|
|
|
|
: type(SearchContext::UnknownType)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void resetState()
|
2007-02-28 01:41:09 +01:00
|
|
|
{
|
2007-10-31 05:44:09 +01:00
|
|
|
qDeleteAll(info);
|
|
|
|
info.clear();
|
|
|
|
qDeleteAll(exact);
|
|
|
|
exact.clear();
|
|
|
|
qDeleteAll(possible);
|
|
|
|
possible.clear();
|
|
|
|
type = SearchContext::UnknownType;
|
|
|
|
term.clear();
|
|
|
|
mimetype.clear();
|
2007-02-28 01:41:09 +01:00
|
|
|
}
|
|
|
|
|
2007-10-31 05:44:09 +01:00
|
|
|
QList<SearchAction *> info;
|
|
|
|
QList<SearchAction *> exact;
|
|
|
|
QList<SearchAction *> possible;
|
2007-02-28 04:55:22 +01:00
|
|
|
QString term;
|
2007-10-31 05:44:09 +01:00
|
|
|
QString mimetype;
|
|
|
|
SearchContext::Type type;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SearchAction::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Private(SearchContext* s, AbstractRunner *r)
|
|
|
|
: search(s),
|
|
|
|
runner(r),
|
|
|
|
type(SearchAction::ExactMatch),
|
|
|
|
relevance(1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SearchContext *search;
|
|
|
|
AbstractRunner *runner;
|
|
|
|
SearchAction::Type type;
|
|
|
|
QString mimetype;
|
|
|
|
qreal relevance;
|
2007-02-28 01:41:09 +01:00
|
|
|
};
|
|
|
|
|
2007-10-31 05:44:09 +01:00
|
|
|
class AbstractRunner::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool hasMatchOptions;
|
|
|
|
bool hasConfig;
|
|
|
|
};
|
|
|
|
|
|
|
|
SearchContext::SearchContext(QObject *parent)
|
|
|
|
: QObject(parent),
|
|
|
|
d(new Private)
|
2007-02-28 01:41:09 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-10-31 05:44:09 +01:00
|
|
|
SearchContext::~SearchContext()
|
2007-02-28 01:41:09 +01:00
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2007-10-31 05:44:09 +01:00
|
|
|
void SearchContext::setTerm(const QString &term)
|
|
|
|
{
|
|
|
|
d->resetState();
|
|
|
|
|
|
|
|
if (term.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->term = term;
|
|
|
|
//FIXME: this is insanely slow =/
|
|
|
|
KUriFilterData filter(term);
|
|
|
|
bool filtered = KUriFilter::self()->filterUri(filter);
|
|
|
|
|
|
|
|
if (filtered) {
|
|
|
|
switch (filter.uriType()) {
|
|
|
|
case KUriFilterData::LocalDir:
|
|
|
|
d->type = Directory;
|
|
|
|
d->mimetype = "inode/folder";
|
|
|
|
break;
|
|
|
|
case KUriFilterData::LocalFile: {
|
|
|
|
d->type = File;
|
|
|
|
KMimeType::Ptr mimetype = KMimeType::findByPath(filter.uri().path());
|
|
|
|
if (mimetype) {
|
|
|
|
d->mimetype = mimetype->name();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case KUriFilterData::NetProtocol:
|
|
|
|
kDebug() << "term is a network protocol?" << term << filter.uriType();
|
|
|
|
d->type = NetworkLocation;
|
|
|
|
break;
|
|
|
|
case KUriFilterData::Executable:
|
|
|
|
d->type = Executable;
|
|
|
|
break;
|
|
|
|
case KUriFilterData::Shell:
|
|
|
|
d->type = ShellCommand;
|
|
|
|
break;
|
|
|
|
case KUriFilterData::Help:
|
|
|
|
d->type = Help;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SearchContext::term() const
|
2007-02-28 01:41:09 +01:00
|
|
|
{
|
2007-10-31 05:44:09 +01:00
|
|
|
return d->term;
|
2007-02-28 01:41:09 +01:00
|
|
|
}
|
|
|
|
|
2007-10-31 05:44:09 +01:00
|
|
|
SearchContext::Type SearchContext::type() const
|
2007-02-28 01:41:09 +01:00
|
|
|
{
|
2007-10-31 05:44:09 +01:00
|
|
|
return d->type;
|
2007-02-28 01:41:09 +01:00
|
|
|
}
|
|
|
|
|
2007-10-31 05:44:09 +01:00
|
|
|
QString SearchContext::mimetype() const
|
2007-03-05 01:07:21 +01:00
|
|
|
{
|
2007-10-31 05:44:09 +01:00
|
|
|
return d->mimetype;
|
2007-03-05 01:07:21 +01:00
|
|
|
}
|
|
|
|
|
2007-10-31 05:44:09 +01:00
|
|
|
SearchAction* SearchContext::addInformationalMatch(AbstractRunner *runner)
|
2007-02-28 04:55:22 +01:00
|
|
|
{
|
2007-10-31 05:44:09 +01:00
|
|
|
SearchAction *action = new SearchAction(this, runner);
|
|
|
|
action->setType(SearchAction::InformationalMatch);
|
|
|
|
d->info.append(action);
|
|
|
|
return action;
|
|
|
|
}
|
2007-02-28 04:55:22 +01:00
|
|
|
|
2007-10-31 05:44:09 +01:00
|
|
|
SearchAction* SearchContext::addExactMatch(AbstractRunner *runner)
|
|
|
|
{
|
|
|
|
SearchAction *action = new SearchAction(this, runner);
|
|
|
|
action->setType(SearchAction::ExactMatch);
|
|
|
|
d->exact.append(action);
|
|
|
|
return action;
|
|
|
|
}
|
2007-02-28 04:55:22 +01:00
|
|
|
|
2007-10-31 05:44:09 +01:00
|
|
|
SearchAction* SearchContext::addPossibleMatch(AbstractRunner *runner)
|
|
|
|
{
|
|
|
|
SearchAction *action = new SearchAction(this, runner);
|
|
|
|
action->setType(SearchAction::PossibleMatch);
|
|
|
|
d->possible.append(action);
|
|
|
|
return action;
|
2007-02-28 04:55:22 +01:00
|
|
|
}
|
|
|
|
|
2007-10-31 05:44:09 +01:00
|
|
|
QList<SearchAction *> SearchContext::informationalMatches() const
|
2007-02-28 01:41:09 +01:00
|
|
|
{
|
2007-10-31 05:44:09 +01:00
|
|
|
return d->info;
|
2007-02-28 01:41:09 +01:00
|
|
|
}
|
|
|
|
|
2007-10-31 05:44:09 +01:00
|
|
|
QList<SearchAction *> SearchContext::exactMatches() const
|
2007-02-28 01:41:09 +01:00
|
|
|
{
|
2007-10-31 05:44:09 +01:00
|
|
|
return d->exact;
|
2007-02-28 01:41:09 +01:00
|
|
|
}
|
|
|
|
|
2007-10-31 05:44:09 +01:00
|
|
|
QList<SearchAction *> SearchContext::possibleMatches() const
|
2007-02-28 04:55:22 +01:00
|
|
|
{
|
2007-10-31 05:44:09 +01:00
|
|
|
return d->possible;
|
|
|
|
}
|
2007-07-18 21:09:41 +02:00
|
|
|
|
2007-10-31 05:44:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
SearchAction::SearchAction(SearchContext *search, AbstractRunner *runner)
|
|
|
|
: QAction(search),
|
|
|
|
d(new Private(search, runner))
|
|
|
|
{
|
|
|
|
connect(this, SIGNAL(triggered(bool)), this, SLOT(exec()));
|
|
|
|
}
|
|
|
|
|
|
|
|
SearchAction::~SearchAction()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchAction::setType(Type type)
|
|
|
|
{
|
|
|
|
d->type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
SearchAction::Type SearchAction::type() const
|
|
|
|
{
|
|
|
|
return d->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchAction::setMimetype(const QString &mimetype)
|
|
|
|
{
|
|
|
|
d->mimetype = mimetype;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SearchAction::mimetype() const
|
|
|
|
{
|
|
|
|
return d->mimetype.isEmpty() ? d->search->mimetype() : d->mimetype;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SearchAction::term() const
|
|
|
|
{
|
|
|
|
return d->search->term();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchAction::setRelevance(qreal relevance)
|
|
|
|
{
|
|
|
|
d->relevance = qMax(0.0, qMin(1.0, relevance));
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal SearchAction::relevance() const
|
|
|
|
{
|
|
|
|
return d->relevance;
|
|
|
|
}
|
|
|
|
|
|
|
|
AbstractRunner* SearchAction::runner() const
|
|
|
|
{
|
|
|
|
return d->runner;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SearchAction::operator<(const SearchAction& other) const
|
|
|
|
{
|
|
|
|
return d->relevance < other.d->relevance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchAction::exec()
|
|
|
|
{
|
|
|
|
//TODO: this could be dangerous if the runner is deleted behind our backs.
|
|
|
|
d->runner->exec(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AbstractRunner::AbstractRunner(QObject* parent)
|
|
|
|
: QObject(parent),
|
|
|
|
d(new Private())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AbstractRunner::~AbstractRunner()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AbstractRunner::hasMatchOptions()
|
|
|
|
{
|
|
|
|
return d->hasMatchOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractRunner::setHasMatchOptions(bool hasMatchOptions)
|
|
|
|
{
|
|
|
|
d->hasMatchOptions = hasMatchOptions;
|
|
|
|
}
|
|
|
|
|
2007-10-31 06:25:07 +01:00
|
|
|
void AbstractRunner::createMatchOptions(QWidget *parent)
|
2007-10-31 05:44:09 +01:00
|
|
|
{
|
|
|
|
Q_UNUSED(parent)
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AbstractRunner::canBeConfigured()
|
|
|
|
{
|
|
|
|
return d->hasConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractRunner::setCanBeConfigured(bool hasConfig)
|
|
|
|
{
|
|
|
|
d->hasConfig = hasConfig;
|
|
|
|
}
|
|
|
|
|
2007-10-31 06:25:07 +01:00
|
|
|
void AbstractRunner::createConfigurationInterface(QWidget *widget)
|
|
|
|
{
|
|
|
|
Q_UNUSED(widget)
|
|
|
|
}
|
|
|
|
|
2007-10-31 05:44:09 +01:00
|
|
|
void AbstractRunner::exec(Plasma::SearchAction *action)
|
|
|
|
{
|
|
|
|
Q_UNUSED(action)
|
2007-02-28 04:55:22 +01:00
|
|
|
}
|
|
|
|
|
2007-10-31 05:44:09 +01:00
|
|
|
AbstractRunner::List AbstractRunner::loadRunners(QObject* parent)
|
2007-03-02 01:06:52 +01:00
|
|
|
{
|
2007-10-09 05:20:02 +02:00
|
|
|
List firstRunners;
|
2007-03-02 01:06:52 +01:00
|
|
|
List runners;
|
2007-10-09 05:20:02 +02:00
|
|
|
List lastRunners;
|
|
|
|
|
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("Plasma/Runner");
|
2007-08-29 05:06:48 +02:00
|
|
|
QString error;
|
|
|
|
foreach (KService::Ptr service, offers) {
|
2007-09-27 01:06:17 +02:00
|
|
|
AbstractRunner* runner = service->createInstance<AbstractRunner>(parent, QVariantList(), &error);
|
2007-10-09 05:20:02 +02:00
|
|
|
if (runner) {
|
2007-10-31 05:44:09 +01:00
|
|
|
//kDebug() << "loaded runner : " << service->name();
|
2007-10-09 05:20:02 +02:00
|
|
|
QString phase = service->property("X-Plasma-RunnerPhase").toString();
|
|
|
|
if (phase == "last") {
|
|
|
|
lastRunners.append(runner);
|
|
|
|
} else if (phase == "first") {
|
|
|
|
firstRunners.append(runner);
|
|
|
|
} else {
|
|
|
|
runners.append(runner);
|
|
|
|
}
|
2007-03-02 01:06:52 +01:00
|
|
|
}
|
|
|
|
else {
|
2007-08-29 05:06:48 +02:00
|
|
|
kDebug() << "failed to load runner : " << service->name() << ". error reported: " << error;
|
2007-03-02 01:06:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-09 05:20:02 +02:00
|
|
|
firstRunners << runners << lastRunners;
|
|
|
|
return firstRunners;
|
2007-03-02 01:06:52 +01:00
|
|
|
}
|
|
|
|
|
2007-03-02 06:27:33 +01:00
|
|
|
} // Plasma namespace
|
|
|
|
|
2007-03-20 18:37:44 +01:00
|
|
|
#include "abstractrunner.moc"
|