* move SearchAction and SearchContext to their own files

* term -> searchTerm
* add a completion object to SearchContext

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=731848
This commit is contained in:
Aaron J. Seigo 2007-11-02 02:34:46 +00:00
parent 045ca46537
commit 0a6f2af7f3
7 changed files with 588 additions and 340 deletions

View File

@ -35,6 +35,8 @@ set(plasma_LIB_SRCS
plasma.cpp
plasma_export.h
scriptengine.cpp
searchaction.cpp
searchcontext.cpp
shadowitem.cpp
svg.cpp
theme.cpp
@ -106,6 +108,8 @@ set(plasma_LIB_INCLUDES
plasma.h
plasma_export.h
scriptengine.h
searchaction.h
searchcontext.h
shadowitem_p.h
svg.h
theme.h

View File

@ -20,61 +20,11 @@
#include "abstractrunner.h"
#include <KDebug>
#include <KMimeType>
#include <KServiceTypeTrader>
#include <KUriFilterData>
namespace Plasma
{
class SearchContext::Private
{
public:
Private()
: type(SearchContext::UnknownType)
{
}
void resetState()
{
qDeleteAll(info);
info.clear();
qDeleteAll(exact);
exact.clear();
qDeleteAll(possible);
possible.clear();
type = SearchContext::UnknownType;
term.clear();
mimetype.clear();
}
QList<SearchAction *> info;
QList<SearchAction *> exact;
QList<SearchAction *> possible;
QString term;
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;
};
class AbstractRunner::Private
{
public:
@ -82,183 +32,6 @@ class AbstractRunner::Private
bool hasConfig;
};
SearchContext::SearchContext(QObject *parent)
: QObject(parent),
d(new Private)
{
}
SearchContext::~SearchContext()
{
delete d;
}
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
{
return d->term;
}
SearchContext::Type SearchContext::type() const
{
return d->type;
}
QString SearchContext::mimetype() const
{
return d->mimetype;
}
SearchAction* SearchContext::addInformationalMatch(AbstractRunner *runner)
{
SearchAction *action = new SearchAction(this, runner);
action->setType(SearchAction::InformationalMatch);
d->info.append(action);
return action;
}
SearchAction* SearchContext::addExactMatch(AbstractRunner *runner)
{
SearchAction *action = new SearchAction(this, runner);
action->setType(SearchAction::ExactMatch);
d->exact.append(action);
return action;
}
SearchAction* SearchContext::addPossibleMatch(AbstractRunner *runner)
{
SearchAction *action = new SearchAction(this, runner);
action->setType(SearchAction::PossibleMatch);
d->possible.append(action);
return action;
}
QList<SearchAction *> SearchContext::informationalMatches() const
{
return d->info;
}
QList<SearchAction *> SearchContext::exactMatches() const
{
return d->exact;
}
QList<SearchAction *> SearchContext::possibleMatches() const
{
return d->possible;
}
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())

View File

@ -20,128 +20,19 @@
#ifndef RUNNER_H
#define RUNNER_H
#include <QtGui/QAction>
#include <QtCore/QList>
#include <QtCore/QObject>
#include <plasma/plasma_export.h>
#include <plasma/searchaction.h>
#include <plasma/searchcontext.h>
class KActionCollection;
class QAction;
class KCompletion;
namespace Plasma
{
class AbstractRunner;
class SearchAction;
class PLASMA_EXPORT SearchContext : public QObject
{
Q_OBJECT
public:
enum Type { UnknownType = 0,
Directory,
File,
NetworkLocation,
Executable,
ShellCommand,
Help
};
explicit SearchContext(QObject *parent = 0);
~SearchContext();
void setTerm(const QString&);
QString term() const;
Type type() const;
QString mimetype() const;
SearchAction* addInformationalMatch(AbstractRunner *runner);
SearchAction* addExactMatch(AbstractRunner *runner);
SearchAction* addPossibleMatch(AbstractRunner *runner);
QList<SearchAction *> informationalMatches() const;
QList<SearchAction *> exactMatches() const;
QList<SearchAction *> possibleMatches() const;
private:
class Private;
Private * const d;
};
class PLASMA_EXPORT SearchAction : public QAction
{
Q_OBJECT
public:
enum Type { InformationalMatch,
ExactMatch,
PossibleMatch };
SearchAction(SearchContext *search, AbstractRunner *runner);
~SearchAction();
/**
* Sets the type of match this action represents.
*/
void setType(Type type);
/**
* The type of action this is. Defaults to ExactMatch.
*/
Type type() const;
/**
* Sets the mimetype, if any, associated with this match
*
* @arg mimetype the mimetype
*/
void setMimetype(const QString &mimetype);
/**
* The mimetype associated with this action, if any
*/
QString mimetype() const;
/**
* The search term that triggered this action
*/
QString term() const;
/**
* Sets the relevance of this action for the search
* it was created for.
*
* @param relevance a number between 0 and 1.
*/
void setRelevance(qreal relevance);
/**
* The relevance of this action to the search. By default,
* the relevance is 1.
*
* @return a number between 0 and 1
*/
qreal relevance() const;
/**
* The runner associated with this action
*/
AbstractRunner* runner() const;
bool operator<(const SearchAction& other) const;
protected Q_SLOTS:
void exec();
private:
class Private;
Private * const d;
};
/**
* A abstract super-class for Plasma Runners
* An abstract base class for Plasma Runner plugins
*/
class PLASMA_EXPORT AbstractRunner : public QObject
{

111
searchaction.cpp Normal file
View File

@ -0,0 +1,111 @@
/*
* 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 "searchaction.h"
#include "abstractrunner.h"
namespace Plasma
{
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;
};
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::searchTerm() const
{
return d->search->searchTerm();
}
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);
}
}
#include "searchaction.moc"

105
searchaction.h Normal file
View File

@ -0,0 +1,105 @@
/*
* 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.
*/
#ifndef SEARCHACTION_H
#define SEARCHACTION_H
#include <QtGui/QAction>
#include <plasma/plasma_export.h>
namespace Plasma
{
class SearchContext;
class AbstractRunner;
class PLASMA_EXPORT SearchAction : public QAction
{
Q_OBJECT
public:
enum Type { InformationalMatch,
ExactMatch,
PossibleMatch };
SearchAction(SearchContext *search, AbstractRunner *runner);
~SearchAction();
/**
* Sets the type of match this action represents.
*/
void setType(Type type);
/**
* The type of action this is. Defaults to ExactMatch.
*/
Type type() const;
/**
* Sets the mimetype, if any, associated with this match
*
* @arg mimetype the mimetype
*/
void setMimetype(const QString &mimetype);
/**
* The mimetype associated with this action, if any
*/
QString mimetype() const;
/**
* The search term that triggered this action
*/
QString searchTerm() const;
/**
* Sets the relevance of this action for the search
* it was created for.
*
* @param relevance a number between 0 and 1.
*/
void setRelevance(qreal relevance);
/**
* The relevance of this action to the search. By default,
* the relevance is 1.
*
* @return a number between 0 and 1
*/
qreal relevance() const;
/**
* The runner associated with this action
*/
AbstractRunner* runner() const;
bool operator<(const SearchAction& other) const;
protected Q_SLOTS:
void exec();
private:
class Private;
Private * const d;
};
}
#endif

220
searchcontext.cpp Normal file
View File

@ -0,0 +1,220 @@
/*
* 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 "searchcontext.h"
#include <KCompletion>
#include <KDebug>
#include <KMimeType>
#include <KUriFilterData>
#include "searchaction.h"
namespace Plasma
{
class SearchContext::Private
{
public:
Private()
: type(SearchContext::UnknownType),
completer(0)
{
}
~Private()
{
delete completer;
}
void resetState()
{
qDeleteAll(info);
info.clear();
qDeleteAll(exact);
exact.clear();
qDeleteAll(possible);
possible.clear();
type = SearchContext::UnknownType;
term.clear();
mimetype.clear();
if (completer) {
completer->clear();
}
}
KCompletion* completionObject()
{
if (!completer) {
completer = new KCompletion;
}
return completer;
}
QList<SearchAction *> info;
QList<SearchAction *> exact;
QList<SearchAction *> possible;
QString term;
QString mimetype;
SearchContext::Type type;
KCompletion *completer;
};
SearchContext::SearchContext(QObject *parent)
: QObject(parent),
d(new Private)
{
}
SearchContext::~SearchContext()
{
delete d;
}
void SearchContext::setSearchTerm(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::searchTerm() const
{
return d->term;
}
SearchContext::Type SearchContext::type() const
{
return d->type;
}
QString SearchContext::mimetype() const
{
return d->mimetype;
}
KCompletion* SearchContext::completionObject() const
{
return d->completionObject();
}
void SearchContext::addStringCompletion(const QString &completion)
{
if (!d->completer) {
// if the completion object isn't actually used, don't bother
return;
}
d->completer->addItem(completion);
}
void SearchContext::addStringCompletions(const QStringList &completion)
{
if (!d->completer) {
// if the completion object isn't actually used, don't bother
return;
}
d->completer->insertItems(completion);
}
SearchAction* SearchContext::addInformationalMatch(AbstractRunner *runner)
{
SearchAction *action = new SearchAction(this, runner);
action->setType(SearchAction::InformationalMatch);
d->info.append(action);
return action;
}
SearchAction* SearchContext::addExactMatch(AbstractRunner *runner)
{
SearchAction *action = new SearchAction(this, runner);
action->setType(SearchAction::ExactMatch);
d->exact.append(action);
return action;
}
SearchAction* SearchContext::addPossibleMatch(AbstractRunner *runner)
{
SearchAction *action = new SearchAction(this, runner);
action->setType(SearchAction::PossibleMatch);
d->possible.append(action);
return action;
}
QList<SearchAction *> SearchContext::informationalMatches() const
{
return d->info;
}
QList<SearchAction *> SearchContext::exactMatches() const
{
return d->exact;
}
QList<SearchAction *> SearchContext::possibleMatches() const
{
return d->possible;
}
}
#include "searchcontext.moc"

144
searchcontext.h Normal file
View File

@ -0,0 +1,144 @@
/*
* 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.
*/
#ifndef SEARCHCONTEXT_H
#define SEARCHCONTEXT_H
#include <QtCore/QList>
#include <QtCore/QObject>
#include <plasma/plasma_export.h>
class KCompletion;
namespace Plasma
{
class SearchAction;
class AbstractRunner;
/**
* @short The SearchContext class provides information related to a search,
* including the search term, metadata on the search term and collected
* matches.
*/
class PLASMA_EXPORT SearchContext : public QObject
{
Q_OBJECT
public:
enum Type { UnknownType = 0,
Directory,
File,
NetworkLocation,
Executable,
ShellCommand,
Help
};
explicit SearchContext(QObject *parent = 0);
~SearchContext();
/**
* Sets the search term for this object. This clears all current
* matches in the process.
*/
void setSearchTerm(const QString&);
/**
* @return the current search term.
*/
QString searchTerm() const;
/**
* The type of item the search term might refer to.
* @see Type
*/
Type type() const;
/**
* The mimetype that the search term refers to, if discoverable.
*
* @return QString() if the mimetype can not be determined, otherwise
* the mimetype of the object being refered to by the search
* string.
*/
QString mimetype() const;
/**
* @return a completion object that can be used with UI elements
*/
KCompletion* completionObject() const;
/**
* Adds an item to the completion object.
*/
void addStringCompletion(const QString& completion);
/**
* Adds multiple items to the completion object.
*/
void addStringCompletions(const QStringList& completions);
/**
* Add an action that represents a match to the current search term.
* This action is informational in nature and does not represent and actionable
* match.
*
* If string data is added to the action using QAction::setData(), that
* string may be used in user interfaces when the item is selected.
*/
SearchAction* addInformationalMatch(AbstractRunner *runner);
/**
* Add an action that represents an exact match to the current search term.
*/
SearchAction* addExactMatch(AbstractRunner *runner);
/**
* Add an action that represents a possible match to the current search term.
*/
SearchAction* addPossibleMatch(AbstractRunner *runner);
/**
* Retrieves all available informational matches for the current
* search term.
*/
QList<SearchAction *> informationalMatches() const;
/**
* Retrieves all available exact matches for the current
* search term.
*/
QList<SearchAction *> exactMatches() const;
/**
* Retrieves all available possible matches for the current
* search term.
*/
QList<SearchAction *> possibleMatches() const;
private:
class Private;
Private * const d;
};
}
#endif