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:
|
2008-02-11 07:04:20 +01:00
|
|
|
Private(SearchContext::DataPolicy p)
|
2007-11-02 03:34:46 +01:00
|
|
|
: type(SearchContext::UnknownType),
|
2008-02-11 07:04:20 +01:00
|
|
|
completer(0),
|
|
|
|
policy(p)
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~Private()
|
|
|
|
{
|
2008-02-19 04:59:21 +01:00
|
|
|
qDeleteAll(matches);
|
|
|
|
matches.clear();
|
2007-11-02 03:34:46 +01:00
|
|
|
delete completer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void resetState()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
KCompletion* completionObject()
|
|
|
|
{
|
|
|
|
if (!completer) {
|
|
|
|
completer = new KCompletion;
|
|
|
|
}
|
|
|
|
|
|
|
|
return completer;
|
|
|
|
}
|
|
|
|
|
2007-12-02 08:11:50 +01:00
|
|
|
void lockForRead()
|
|
|
|
{
|
2008-02-11 07:04:20 +01:00
|
|
|
if (policy == Shared) {
|
|
|
|
lock.lockForRead();
|
|
|
|
}
|
2007-12-02 08:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void lockForWrite()
|
|
|
|
{
|
2008-02-11 07:04:20 +01:00
|
|
|
if (policy == Shared) {
|
|
|
|
lock.lockForWrite();
|
|
|
|
}
|
2007-12-02 08:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void unlock()
|
|
|
|
{
|
2008-02-11 07:04:20 +01:00
|
|
|
if (policy == Shared) {
|
|
|
|
lock.unlock();
|
|
|
|
}
|
2007-12-02 08:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QReadWriteLock lock;
|
2008-02-19 04:59:21 +01:00
|
|
|
QList<SearchMatch *> matches;
|
2007-11-02 03:34:46 +01:00
|
|
|
QString term;
|
|
|
|
QString mimetype;
|
|
|
|
SearchContext::Type type;
|
|
|
|
KCompletion *completer;
|
2008-02-11 07:04:20 +01:00
|
|
|
const SearchContext::DataPolicy policy;
|
2007-11-02 03:34:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-02-11 07:04:20 +01:00
|
|
|
SearchContext::SearchContext(QObject *parent, DataPolicy policy)
|
2007-11-02 03:34:46 +01:00
|
|
|
: QObject(parent),
|
2008-02-11 07:04:20 +01:00
|
|
|
d(new Private(policy))
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-12-02 08:11:50 +01:00
|
|
|
SearchContext::SearchContext(QObject *parent, const SearchContext &other)
|
2008-02-11 07:04:20 +01:00
|
|
|
: QObject(parent),
|
|
|
|
d(new Private(SingleConsumer))
|
2007-12-02 08:11:50 +01:00
|
|
|
{
|
|
|
|
other.d->lockForRead();
|
|
|
|
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
|
|
|
{
|
2008-02-19 04:59:21 +01:00
|
|
|
d->lockForWrite();
|
|
|
|
QList<SearchMatch*> matches = d->matches;
|
|
|
|
|
|
|
|
d->matches.clear();
|
|
|
|
d->type = SearchContext::UnknownType;
|
|
|
|
d->term.clear();
|
|
|
|
d->mimetype.clear();
|
|
|
|
|
|
|
|
if (d->completer) {
|
|
|
|
d->completer->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
d->unlock();
|
|
|
|
|
2008-02-15 04:57:54 +01:00
|
|
|
emit matchesChanged();
|
2008-02-19 04:59:21 +01:00
|
|
|
|
|
|
|
// in case someone is still holding on to the Matches
|
|
|
|
// when we emit the matchesChanged() signal, we don't
|
|
|
|
// delete the matches until after the signal is handled.
|
|
|
|
// a bit safer.
|
|
|
|
qDeleteAll(matches);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-02-19 04:59:21 +01:00
|
|
|
bool SearchContext::addMatches(const QString& term, const QList<SearchMatch*> &matches)
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2008-02-19 04:59:21 +01:00
|
|
|
if (searchTerm() != term || matches.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-11-02 03:34:46 +01:00
|
|
|
|
2008-02-19 04:59:21 +01:00
|
|
|
d->lockForWrite();
|
|
|
|
d->matches << matches;
|
|
|
|
d->unlock();
|
2007-11-02 03:34:46 +01:00
|
|
|
|
2008-02-19 04:59:21 +01:00
|
|
|
// TODO: perhaps queue this signal so that it is only emitted after a small delay?
|
|
|
|
// currently we do this in krunner's Interface class, but it would probably
|
|
|
|
// be better to move that detail to SearchContext so that other apps that
|
|
|
|
// use SearchContext can also benefit from it
|
|
|
|
emit matchesChanged();
|
|
|
|
return true;
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
2008-02-19 04:59:21 +01:00
|
|
|
bool SearchContext::addMatch(const QString &term, SearchMatch *match)
|
2007-12-02 08:11:50 +01:00
|
|
|
{
|
|
|
|
if (searchTerm() != term) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-02-08 20:10:20 +01:00
|
|
|
|
2007-12-02 08:11:50 +01:00
|
|
|
d->lockForWrite();
|
2008-02-19 04:59:21 +01:00
|
|
|
d->matches << match;
|
2007-12-02 08:11:50 +01:00
|
|
|
d->unlock();
|
|
|
|
emit matchesChanged();
|
2008-02-19 04:59:21 +01:00
|
|
|
|
2007-12-02 08:11:50 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-02-11 07:04:20 +01:00
|
|
|
bool SearchContext::addMatchesTo(SearchContext &other)
|
|
|
|
{
|
2008-02-19 04:59:21 +01:00
|
|
|
//NOTE: we have addMatchesTo instead of the more 'natural' addMatches
|
|
|
|
// because we can get away with one write lock on the local object
|
|
|
|
// this way, otherwise we'd need to lock once for searchTerm, once
|
|
|
|
// for matches() and again for clearMatches() (2 read, one write)
|
|
|
|
d->lockForWrite();
|
|
|
|
|
|
|
|
const bool success = other.addMatches(d->term, d->matches);
|
|
|
|
|
|
|
|
if (success) {
|
2008-02-11 07:04:20 +01:00
|
|
|
// the matches no longer belong to this SearchContext,
|
|
|
|
// so remove them from the data
|
2008-02-19 04:59:21 +01:00
|
|
|
d->matches.clear();
|
2008-02-11 07:04:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d->unlock();
|
2008-02-19 04:59:21 +01:00
|
|
|
return success;
|
2008-02-11 07:04:20 +01:00
|
|
|
}
|
|
|
|
|
2008-02-19 04:59:21 +01:00
|
|
|
QList<SearchMatch *> SearchContext::matches() const
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2007-12-02 08:11:50 +01:00
|
|
|
d->lockForRead();
|
2008-02-19 04:59:21 +01:00
|
|
|
QList<SearchMatch *> matches = d->matches;
|
2007-12-02 08:11:50 +01:00
|
|
|
d->unlock();
|
|
|
|
return matches;
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
2008-02-19 04:59:21 +01:00
|
|
|
void SearchContext::clearMatches()
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2008-02-19 04:59:21 +01:00
|
|
|
d->lockForWrite();
|
2007-11-02 03:34:46 +01:00
|
|
|
|
2008-02-19 04:59:21 +01:00
|
|
|
QList<SearchMatch*> matches = d->matches;
|
|
|
|
d->matches.clear();
|
2007-12-02 08:11:50 +01:00
|
|
|
d->unlock();
|
|
|
|
|
2008-02-15 04:57:54 +01:00
|
|
|
emit matchesChanged();
|
2008-02-19 04:59:21 +01:00
|
|
|
|
|
|
|
// in case someone is still holding on to the Matches
|
|
|
|
// when we emit the matchesChanged() signal, we don't
|
|
|
|
// delete the matches until after the signal is handled.
|
|
|
|
// a bit safer.
|
|
|
|
qDeleteAll(matches);
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "searchcontext.moc"
|