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>
|
2008-05-01 09:51:59 +02:00
|
|
|
#include <QSharedData>
|
2007-11-09 20:14:15 +01:00
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
class SearchContext::Private : public QSharedData
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
|
|
|
public:
|
2008-05-01 09:51:59 +02:00
|
|
|
Private(SearchContext *context, SearchContext::DataPolicy p)
|
2007-11-02 03:34:46 +01:00
|
|
|
: type(SearchContext::UnknownType),
|
2008-05-01 09:51:59 +02:00
|
|
|
q(context),
|
2008-02-11 07:04:20 +01:00
|
|
|
policy(p)
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
Private(const SearchContext::Private& p)
|
|
|
|
: term(p.term),
|
|
|
|
mimeType(p.mimeType),
|
|
|
|
type(p.type),
|
|
|
|
q(p.q),
|
|
|
|
policy(p.policy)
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
~Private()
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2008-05-01 09:51:59 +02:00
|
|
|
lockForWrite();
|
|
|
|
qDeleteAll(matches);
|
|
|
|
matches.clear();
|
|
|
|
unlock();
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
/**
|
|
|
|
* Determines type of query
|
|
|
|
*/
|
|
|
|
void determineType()
|
|
|
|
{
|
|
|
|
lockForWrite();
|
|
|
|
QString path = KShell::tildeExpand(term);
|
|
|
|
|
|
|
|
int space = term.indexOf(' ');
|
|
|
|
if (space > 0) {
|
|
|
|
if (!KStandardDirs::findExe(path.left(space)).isEmpty()) {
|
|
|
|
type = ShellCommand;
|
|
|
|
}
|
|
|
|
} else if (!KStandardDirs::findExe(path.left(space)).isEmpty()) {
|
|
|
|
type = Executable;
|
|
|
|
} else {
|
|
|
|
KUrl url(term);
|
|
|
|
if (!url.protocol().isEmpty() && !url.host().isEmpty()) {
|
|
|
|
type = NetworkLocation;
|
|
|
|
} else if (QFile::exists(path)) {
|
|
|
|
QFileInfo info(path);
|
|
|
|
if (info.isDir()) {
|
|
|
|
type = Directory;
|
|
|
|
mimeType = "inode/folder";
|
|
|
|
} else {
|
|
|
|
type = File;
|
|
|
|
KMimeType::Ptr mimeTypePtr = KMimeType::findByPath(path);
|
|
|
|
if (mimeTypePtr) {
|
|
|
|
mimeType = mimeTypePtr->name();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (term.contains('.')) {
|
|
|
|
// default to a network location so we can can do things like www.kde.org
|
|
|
|
type = NetworkLocation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unlock();
|
|
|
|
}
|
|
|
|
|
2007-12-02 08:11:50 +01:00
|
|
|
QReadWriteLock lock;
|
2008-05-01 09:51:59 +02:00
|
|
|
QList<SearchMatch*> matches;
|
2007-11-02 03:34:46 +01:00
|
|
|
QString term;
|
2008-04-24 17:15:38 +02:00
|
|
|
QString mimeType;
|
2007-11-02 03:34:46 +01:00
|
|
|
SearchContext::Type type;
|
2008-05-01 09:51:59 +02:00
|
|
|
SearchContext * q;
|
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-05-01 09:51:59 +02:00
|
|
|
d(new Private(this, policy))
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
//copy ctor
|
|
|
|
SearchContext::SearchContext(SearchContext &other, QObject *parent)
|
|
|
|
: QObject(parent)
|
2007-12-02 08:11:50 +01:00
|
|
|
{
|
|
|
|
other.d->lockForRead();
|
2008-05-01 09:51:59 +02:00
|
|
|
d=other.d;
|
2007-12-02 08:11:50 +01:00
|
|
|
other.d->unlock();
|
|
|
|
}
|
|
|
|
|
2007-11-02 03:34:46 +01:00
|
|
|
SearchContext::~SearchContext()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-04-24 18:25:36 +02:00
|
|
|
void SearchContext::reset()
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2008-05-01 09:51:59 +02:00
|
|
|
// Locks are needed as other contexts can be copied of this one
|
|
|
|
|
|
|
|
// We will detach if we are a copy of someone. But we will reset
|
|
|
|
// if we are the 'main' context others copied from. Resetting
|
|
|
|
// one SearchContext makes all the copies oneobsolete.
|
|
|
|
d.detach();
|
2008-02-19 04:59:21 +01:00
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
//kDebug() << "reset searchContext";
|
|
|
|
d->lockForWrite();
|
2008-02-19 04:59:21 +01:00
|
|
|
d->type = SearchContext::UnknownType;
|
|
|
|
d->term.clear();
|
2008-04-24 17:15:38 +02:00
|
|
|
d->mimeType.clear();
|
2008-02-19 04:59:21 +01:00
|
|
|
d->unlock();
|
2008-05-01 09:51:59 +02:00
|
|
|
removeAllMatches();
|
2008-01-22 05:38:03 +01:00
|
|
|
}
|
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();
|
2008-05-01 09:51:59 +02:00
|
|
|
d->determineType();
|
2007-12-02 08:11:50 +01:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-04-24 17:15:38 +02:00
|
|
|
QString SearchContext::mimeType() const
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2008-04-24 17:15:38 +02:00
|
|
|
return d->mimeType;
|
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();
|
2008-05-01 09:51:59 +02:00
|
|
|
//kDebug()<< "add matches";
|
|
|
|
// A copied searchContext may share the d pointer,
|
|
|
|
// we always want to sent the signal of the object that created
|
|
|
|
// the d pointer
|
|
|
|
emit d->q->matchesChanged();
|
2008-02-19 04:59:21 +01:00
|
|
|
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();
|
2008-05-01 09:51:59 +02:00
|
|
|
//kDebug()<< "added match" << match->text();
|
|
|
|
emit d->q->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
|
|
|
|
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-04-24 18:04:50 +02:00
|
|
|
void SearchContext::removeAllMatches()
|
2007-11-02 03:34:46 +01:00
|
|
|
{
|
2008-02-19 04:59:21 +01:00
|
|
|
d->lockForWrite();
|
2008-05-01 09:51:59 +02:00
|
|
|
bool emptyMatches = d->matches.isEmpty();
|
|
|
|
if (!emptyMatches) {
|
|
|
|
d->matches.clear();
|
|
|
|
d->unlock();
|
|
|
|
emit d->q->matchesChanged();
|
|
|
|
// 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.
|
|
|
|
d->lockForWrite();
|
|
|
|
qDeleteAll(d->matches);
|
|
|
|
}
|
2007-12-02 08:11:50 +01:00
|
|
|
d->unlock();
|
2007-11-02 03:34:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "searchcontext.moc"
|