• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libplasma

runnercontext.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright 2006-2007 Aaron Seigo <aseigo@kde.org>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library General Public License as
00006  *   published by the Free Software Foundation; either version 2, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details
00013  *
00014  *   You should have received a copy of the GNU Library General Public
00015  *   License along with this program; if not, write to the
00016  *   Free Software Foundation, Inc.,
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018  */
00019 
00020 #include "runnercontext.h"
00021 
00022 #include <QReadWriteLock>
00023 
00024 #include <QDir>
00025 #include <QFile>
00026 #include <QFileInfo>
00027 #include <QSharedData>
00028 
00029 #include <KCompletion>
00030 #include <KDebug>
00031 #include <KMimeType>
00032 #include <KShell>
00033 #include <KStandardDirs>
00034 #include <KUrl>
00035 
00036 #include "abstractrunner.h"
00037 #include "querymatch.h"
00038 
00039 //#define LOCK_FOR_READ(context) if (context->d->policy == Shared) { context->d->lock.lockForRead(); }
00040 //#define LOCK_FOR_WRITE(context) if (context->d->policy == Shared) { context->d->lock.lockForWrite(); }
00041 //#define UNLOCK(context) if (context->d->policy == Shared) { context->d->lock.unlock(); }
00042 
00043 #define LOCK_FOR_READ(context) context->d->lock.lockForRead();
00044 #define LOCK_FOR_WRITE(context) context->d->lock.lockForWrite();
00045 #define UNLOCK(context) context->d->lock.unlock();
00046 
00047 namespace Plasma
00048 {
00049 
00050 class RunnerContextPrivate : public QSharedData
00051 {
00052     public:
00053         RunnerContextPrivate(RunnerContext *context)
00054             : QSharedData(),
00055               type(RunnerContext::UnknownType),
00056               q(context)
00057         {
00058         }
00059 
00060         RunnerContextPrivate(const RunnerContextPrivate& p)
00061             : QSharedData(),
00062               type(RunnerContext::None),
00063               q(p.q)
00064         {
00065             //kDebug() << "¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿boo yeah" << type;
00066         }
00067 
00068         ~RunnerContextPrivate()
00069         {
00070         }
00071 
00075         void determineType()
00076         {
00077             // NOTE! this method must NEVER be called from
00078             // code that may be running in multiple threads
00079             // with the same data.
00080             type = RunnerContext::UnknownType;
00081             QString path = QDir::cleanPath(KShell::tildeExpand(term));
00082 
00083             int space = term.indexOf(' ');
00084             if (space > 0) {
00085                 if (!KStandardDirs::findExe(path.left(space)).isEmpty()) {
00086                     type = RunnerContext::ShellCommand;
00087                 }
00088             } else if (!KStandardDirs::findExe(path.left(space)).isEmpty()) {
00089                 type = RunnerContext::Executable;
00090             } else {
00091                 KUrl url(term);
00092                 if (!url.protocol().isEmpty() && !url.isLocalFile()) {
00093                     type = RunnerContext::NetworkLocation;
00094                 } else if (QFile::exists(path)) {
00095                     QFileInfo info(path);
00096                     if (info.isSymLink()) {
00097                         path = info.canonicalFilePath();
00098                         info = QFileInfo(path);
00099                     }
00100                     if (info.isDir()) {
00101                         type = RunnerContext::Directory;
00102                         mimeType = "inode/folder";
00103                     } else if (info.isFile()) {
00104                         type = RunnerContext::File;
00105                         KMimeType::Ptr mimeTypePtr = KMimeType::findByPath(path);
00106                         if (mimeTypePtr) {
00107                             mimeType = mimeTypePtr->name();
00108                         }
00109                     }
00110                 }
00111             }
00112         }
00113 
00114         QReadWriteLock lock;
00115         QList<QueryMatch> matches;
00116         QMap<QString, const QueryMatch*> matchesById;
00117         QString term;
00118         QString mimeType;
00119         RunnerContext::Type type;
00120         RunnerContext * q;
00121 };
00122 
00123 
00124 RunnerContext::RunnerContext(QObject *parent)
00125     : QObject(parent),
00126       d(new RunnerContextPrivate(this))
00127 {
00128 }
00129 
00130 //copy ctor
00131 RunnerContext::RunnerContext(RunnerContext &other, QObject *parent)
00132      : QObject(parent)
00133 {
00134     LOCK_FOR_READ((&other))
00135     d = other.d;
00136     UNLOCK((&other))
00137 }
00138 
00139 RunnerContext::~RunnerContext()
00140 {
00141 }
00142 
00143 void RunnerContext::reset()
00144 {
00145     // We will detach if we are a copy of someone. But we will reset 
00146     // if we are the 'main' context others copied from. Resetting 
00147     // one RunnerContext makes all the copies oneobsolete.  
00148     d.detach();
00149 
00150     // we still have to remove all the matches, since if the
00151     // ref count was 1 (e.g. only the RunnerContext is using
00152     // the dptr) then we won't get a copy made
00153     if (!d->matches.isEmpty()) {
00154         d->matchesById.clear();
00155         d->matches.clear();
00156         emit d->q->matchesChanged();
00157     }
00158 
00159     d->term.clear();
00160     d->mimeType.clear();
00161     d->type = UnknownType;
00162     //kDebug() << "match count" << d->matches.count();
00163 }
00164 
00165 void RunnerContext::setQuery(const QString &term)
00166 {
00167     reset();
00168 
00169     if (term.isEmpty()) {
00170         return;
00171     }
00172 
00173     d->term = term;
00174     d->determineType();
00175 }
00176 
00177 
00178 QString RunnerContext::query() const
00179 {
00180     // the query term should never be set after
00181     // a search starts. in fact, reset() ensures this
00182     // and setQuery(QString) calls reset()
00183     return d->term;
00184 }
00185 
00186 RunnerContext::Type RunnerContext::type() const
00187 {
00188     return d->type;
00189 }
00190 
00191 QString RunnerContext::mimeType() const
00192 {
00193     return d->mimeType;
00194 }
00195 
00196 bool RunnerContext::addMatches(const QString& term, const QList<QueryMatch> &matches)
00197 {
00198     Q_UNUSED(term)
00199 
00200     if (matches.isEmpty()) {
00201         return false;
00202     }
00203 
00204     LOCK_FOR_WRITE(this)
00205     foreach (const QueryMatch &match, matches) {
00206         d->matches.append(match);
00207 #ifndef NDEBUG
00208         if (d->matchesById.contains(match.id())) {
00209                 kDebug() << "Duplicate match id " << match.id() << "from" << match.runner()->name();
00210         }
00211 #endif
00212         d->matchesById.insert(match.id(), &d->matches.at(d->matches.size() - 1));
00213     }
00214     UNLOCK(this);
00215     //kDebug()<< "add matches";
00216     // A copied searchContext may share the d pointer, 
00217     // we always want to sent the signal of the object that created
00218     // the d pointer 
00219     emit d->q->matchesChanged();
00220     return true;
00221 }
00222 
00223 bool RunnerContext::addMatch(const QString &term, const QueryMatch &match)
00224 {
00225     Q_UNUSED(term)
00226 
00227     LOCK_FOR_WRITE(this)
00228     d->matches.append(match);
00229     d->matchesById.insert(match.id(), &d->matches.at(d->matches.size() - 1));
00230     UNLOCK(this);
00231     //kDebug()<< "added match" << match->text();
00232     emit d->q->matchesChanged();
00233 
00234     return true;
00235 }
00236 
00237 QList<QueryMatch> RunnerContext::matches() const
00238 {
00239     LOCK_FOR_READ(this)
00240     QList<QueryMatch> matches = d->matches;
00241     UNLOCK(this);
00242     return matches;
00243 }
00244 
00245 QueryMatch RunnerContext::match(const QString &id) const
00246 {
00247     LOCK_FOR_READ(this)
00248     if (d->matchesById.contains(id)) {
00249         const QueryMatch *match = d->matchesById.value(id);
00250         UNLOCK(this)
00251         return *match;
00252     }
00253     UNLOCK(this)
00254 
00255     return QueryMatch(0);
00256 }
00257 
00258 } // Plasma namespace
00259 
00260 #include "runnercontext.moc"

libplasma

Skip menu "libplasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal