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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "searchcontext.h"
|
|
|
|
|
2007-12-02 08:11:50 +01:00
|
|
|
#include <QReadWriteLock>
|
|
|
|
|
2007-11-09 20:14:15 +01:00
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
2007-11-02 03:34:46 +01:00
|
|
|
#include <KCompletion>
|
|
|
|
#include <KDebug>
|
|
|
|
#include <KMimeType>
|
2007-12-19 08:01:16 +01:00
|
|
|
#include <KShell>
|
2007-11-09 20:14:15 +01:00
|
|
|
#include <KStandardDirs>
|
|
|
|
#include <KUrl>
|
2007-11-02 03:34:46 +01:00
|
|
|
|
2007-12-03 18:18:28 +01:00
|
|
|
#include "searchmatch.h"
|
2007-11-02 03:34:46 +01:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class SearchContext::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Private()
|
|
|
|
: type(SearchContext::UnknownType),
|
|
|
|
completer(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~Private()
|
|
|
|
{
|
|
|
|
delete completer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void resetState()
|
|
|
|
{
|
2007-12-02 08:11:50 +01:00
|
|
|
lock.lockForWrite();
|
2007-11-02 03:34:46 +01:00
|
|
|
qDeleteAll(info);
|
|
|
|
info.clear();
|
|
|
|
qDeleteAll(exact);
|
|
|
|
exact.clear();
|
|
|
|
qDeleteAll(possible);
|
|
|
|
possible.clear();
|
|
|
|
type = SearchContext::UnknownType;
|
|
|
|
term.clear();
|
|
|
|
mimetype.clear();
|
|
|
|
|
|
|
|
if (completer) {
|
|
|
|
completer->clear();
|
|
|
|
}
|
2007-12-02 08:11:50 +01:00
|
|
|
lock.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void clearMatches()
|
|
|
|
{
|
|
|
|
lock.lockForWrite();
|
|
|
|
qDeleteAll(info);
|
|
|
|
info.clear();
|
|
|
|
qDeleteAll(exact);
|
|
|
|
exact.clear();
|
|
|
|
qDeleteAll(possible);
|
|
|
|
possible.clear();
|
|
|
|
lock.unlock();
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
KCompletion* completionObject()
|
|
|
|
{
|
|
|
|
if (!completer) {
|
|
|
|
completer = new KCompletion;
|
|
|
|
}
|
|
|
|
|
|
|
|
return completer;
|
|
|
|
}
|
|
|
|
|
2007-12-02 08:11:50 +01:00
|
|
|
void lockForRead()
|
|
|
|
{
|
|
|
|
lock.lockForRead();
|
|
|
|
}
|
|
|
|
|
|
|
|
void lockForWrite()
|
|
|
|
{
|
|
|
|
lock.lockForWrite();
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock()
|
|
|
|
{
|
|
|
|
lock.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
QReadWriteLock lock;
|
2007-12-03 18:18:28 +01:00
|
|
|
QList<SearchMatch *> info;
|
|
|
|
QList<SearchMatch *> exact;
|
|
|
|
QList<SearchMatch *> possible;
|
2007-11-02 03:34:46 +01:00
|
|
|
QString term;
|
|
|
|
QString mimetype;
|
|
|
|
SearchContext::Type type;
|
|
|
|
KCompletion *completer;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
SearchContext::SearchContext(QObject *parent)
|
|
|
|
: QObject(parent),
|
|
|
|
d(new Private)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-12-02 08:11:50 +01:00
|
|
|
SearchContext::SearchContext(QObject *parent, const SearchContext &other)
|
|
|
|
: QObject( parent ),
|
|
|
|
d( new Private )
|
|
|
|
{
|
|
|
|
other.d->lockForRead();
|
|
|
|
d->info = other.d->info;
|
|
|
|
d->possible = other.d->possible;
|
|
|
|
d->exact = other.d->exact;
|
|
|
|
d->term = other.d->term;
|
|
|
|
d->mimetype = other.d->mimetype;
|
|
|
|
d->type = other.d->type;
|
|
|
|
other.d->unlock();
|
|
|
|
}
|
|
|
|
|
2007-11-02 03:34:46 +01:00
|
|
|
SearchContext::~SearchContext()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2008-01-22 05:38:03 +01:00
|
|
|
void SearchContext::resetSearchTerm(const QString &term)
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
|
|
|
d->resetState();
|
2008-01-22 05:38:03 +01:00
|
|
|
setSearchTerm(term);
|
|
|
|
}
|
2007-11-02 03:34:46 +01:00
|
|
|
|
2008-01-22 05:38:03 +01:00
|
|
|
void SearchContext::setSearchTerm(const QString &term)
|
|
|
|
{
|
2007-11-02 03:34:46 +01:00
|
|
|
if (term.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-12-02 08:11:50 +01:00
|
|
|
d->lockForWrite();
|
2007-11-02 03:34:46 +01:00
|
|
|
d->term = term;
|
2007-12-02 08:11:50 +01:00
|
|
|
d->unlock();
|
|
|
|
determineType();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchContext::determineType()
|
|
|
|
{
|
|
|
|
d->lockForWrite();
|
|
|
|
QString term = d->term;
|
2007-12-19 08:01:16 +01:00
|
|
|
QString path = KShell::tildeExpand(term);
|
2007-11-09 20:14:15 +01:00
|
|
|
|
|
|
|
int space = term.indexOf(' ');
|
|
|
|
if (space > 0) {
|
2007-12-19 08:01:16 +01:00
|
|
|
if (!KStandardDirs::findExe(path.left(space)).isEmpty()) {
|
2007-11-09 20:14:15 +01:00
|
|
|
d->type = ShellCommand;
|
|
|
|
}
|
2007-12-19 08:01:16 +01:00
|
|
|
} else if (!KStandardDirs::findExe(path.left(space)).isEmpty()) {
|
2007-11-09 20:14:15 +01:00
|
|
|
d->type = Executable;
|
|
|
|
} else {
|
|
|
|
KUrl url(term);
|
|
|
|
|
|
|
|
if (!url.protocol().isEmpty() && !url.host().isEmpty()) {
|
|
|
|
d->type = NetworkLocation;
|
2007-12-19 08:01:16 +01:00
|
|
|
} else if (QFile::exists(path)) {
|
|
|
|
QFileInfo info(path);
|
2007-11-09 20:14:15 +01:00
|
|
|
if (info.isDir()) {
|
|
|
|
d->type = Directory;
|
|
|
|
d->mimetype = "inode/folder";
|
|
|
|
} else {
|
|
|
|
d->type = File;
|
2007-12-19 08:01:16 +01:00
|
|
|
KMimeType::Ptr mimetype = KMimeType::findByPath(path);
|
2007-11-09 20:14:15 +01:00
|
|
|
if (mimetype) {
|
|
|
|
d->mimetype = mimetype->name();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (term.contains('.')) {
|
|
|
|
// default to a network location so we can can do things like www.kde.org
|
|
|
|
d->type = NetworkLocation;
|
|
|
|
}
|
|
|
|
}
|
2007-12-02 08:11:50 +01:00
|
|
|
d->unlock();
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString SearchContext::searchTerm() const
|
|
|
|
{
|
2007-12-02 08:11:50 +01:00
|
|
|
d->lockForRead();
|
|
|
|
QString term = d->term;
|
|
|
|
d->unlock();
|
|
|
|
return term;
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SearchContext::Type SearchContext::type() const
|
|
|
|
{
|
|
|
|
return d->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SearchContext::mimetype() const
|
|
|
|
{
|
|
|
|
return d->mimetype;
|
|
|
|
}
|
|
|
|
|
|
|
|
KCompletion* SearchContext::completionObject() const
|
|
|
|
{
|
2007-12-02 08:11:50 +01:00
|
|
|
d->lockForRead();
|
|
|
|
KCompletion* comp = d->completionObject();
|
|
|
|
d->unlock();
|
|
|
|
return comp;
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchContext::addStringCompletion(const QString &completion)
|
|
|
|
{
|
2007-12-02 08:11:50 +01:00
|
|
|
d->lockForWrite();
|
2007-11-02 03:34:46 +01:00
|
|
|
if (!d->completer) {
|
2007-12-02 08:11:50 +01:00
|
|
|
d->unlock();
|
2007-11-02 03:34:46 +01:00
|
|
|
// if the completion object isn't actually used, don't bother
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->completer->addItem(completion);
|
2007-12-02 08:11:50 +01:00
|
|
|
d->unlock();
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchContext::addStringCompletions(const QStringList &completion)
|
|
|
|
{
|
2007-12-02 08:11:50 +01:00
|
|
|
d->lockForWrite();
|
2007-11-02 03:34:46 +01:00
|
|
|
if (!d->completer) {
|
2007-12-02 08:11:50 +01:00
|
|
|
d->unlock();
|
2007-11-02 03:34:46 +01:00
|
|
|
// if the completion object isn't actually used, don't bother
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->completer->insertItems(completion);
|
2007-12-02 08:11:50 +01:00
|
|
|
d->unlock();
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
2007-12-03 18:18:28 +01:00
|
|
|
SearchMatch* SearchContext::addInformationalMatch(AbstractRunner *runner)
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2007-12-03 18:18:28 +01:00
|
|
|
SearchMatch *action = new SearchMatch(this, runner);
|
|
|
|
action->setType(SearchMatch::InformationalMatch);
|
2007-11-02 03:34:46 +01:00
|
|
|
d->info.append(action);
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
2007-12-03 18:18:28 +01:00
|
|
|
SearchMatch* SearchContext::addExactMatch(AbstractRunner *runner)
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2007-12-03 18:18:28 +01:00
|
|
|
SearchMatch *action = new SearchMatch(this, runner);
|
|
|
|
action->setType(SearchMatch::ExactMatch);
|
2007-11-02 03:34:46 +01:00
|
|
|
d->exact.append(action);
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
2007-12-03 18:18:28 +01:00
|
|
|
SearchMatch* SearchContext::addPossibleMatch(AbstractRunner *runner)
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2007-12-03 18:18:28 +01:00
|
|
|
SearchMatch *action = new SearchMatch(this, runner);
|
|
|
|
action->setType(SearchMatch::PossibleMatch);
|
2007-11-02 03:34:46 +01:00
|
|
|
d->possible.append(action);
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
2007-12-03 18:18:28 +01:00
|
|
|
bool SearchContext::addMatches( const QString& term, const QList<SearchMatch *> &exactMatches,
|
|
|
|
const QList<SearchMatch *> &possibleMatches,
|
|
|
|
const QList<SearchMatch *> &informationalMatches )
|
2007-12-02 08:11:50 +01:00
|
|
|
{
|
|
|
|
if (searchTerm() != term) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-02-08 20:10:20 +01:00
|
|
|
|
|
|
|
if (exactMatches.isEmpty() &&
|
|
|
|
possibleMatches.isEmpty() &&
|
|
|
|
informationalMatches.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-12-02 08:11:50 +01:00
|
|
|
d->lockForWrite();
|
|
|
|
d->exact << exactMatches;
|
|
|
|
d->possible << possibleMatches;
|
|
|
|
d->info << informationalMatches;
|
|
|
|
d->unlock();
|
|
|
|
emit matchesChanged();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-12-03 18:18:28 +01:00
|
|
|
QList<SearchMatch *> SearchContext::informationalMatches() const
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2007-12-02 08:11:50 +01:00
|
|
|
d->lockForRead();
|
2007-12-03 18:18:28 +01:00
|
|
|
QList<SearchMatch *> matches = d->info;
|
2007-12-02 08:11:50 +01:00
|
|
|
d->unlock();
|
|
|
|
return matches;
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
2007-12-03 18:18:28 +01:00
|
|
|
QList<SearchMatch *> SearchContext::exactMatches() const
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2007-12-02 08:11:50 +01:00
|
|
|
d->lockForRead();
|
2007-12-03 18:18:28 +01:00
|
|
|
QList<SearchMatch *> matches = d->exact;
|
2007-12-02 08:11:50 +01:00
|
|
|
d->unlock();
|
|
|
|
return matches;
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
2007-12-03 18:18:28 +01:00
|
|
|
QList<SearchMatch *> SearchContext::possibleMatches() const
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2007-12-02 08:11:50 +01:00
|
|
|
d->lockForRead();
|
2007-12-03 18:18:28 +01:00
|
|
|
QList<SearchMatch *> matches = d->possible;
|
2007-12-02 08:11:50 +01:00
|
|
|
d->unlock();
|
|
|
|
return matches;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchContext::clearMatches()
|
|
|
|
{
|
|
|
|
d->clearMatches();
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "searchcontext.moc"
|