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

Plasma

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.h>
00030 #include <kdebug.h>
00031 #include <kmimetype.h>
00032 #include <kshell.h>
00033 #include <kstandarddirs.h>
00034 #include <kurl.h>
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 = path.indexOf(' ');
00084             if (!KStandardDirs::findExe(path.left(space)).isEmpty()) {
00085                 // it's a shell command if there's a space because that implies
00086                 // that it has arguments!
00087                 type = (space > 0) ? RunnerContext::ShellCommand :
00088                                      RunnerContext::Executable;
00089             } else {
00090                 KUrl url(term);
00091                 if (!url.protocol().isEmpty() && !url.isLocalFile()) {
00092                     type = RunnerContext::NetworkLocation;
00093                 } else if (QFile::exists(path)) {
00094                     QFileInfo info(path);
00095                     if (info.isSymLink()) {
00096                         path = info.canonicalFilePath();
00097                         info = QFileInfo(path);
00098                     }
00099                     if (info.isDir()) {
00100                         type = RunnerContext::Directory;
00101                         mimeType = "inode/folder";
00102                     } else if (info.isFile()) {
00103                         type = RunnerContext::File;
00104                         KMimeType::Ptr mimeTypePtr = KMimeType::findByPath(path);
00105                         if (mimeTypePtr) {
00106                             mimeType = mimeTypePtr->name();
00107                         }
00108                     }
00109                 }
00110             }
00111         }
00112 
00113         QReadWriteLock lock;
00114         QList<QueryMatch> matches;
00115         QMap<QString, const QueryMatch*> matchesById;
00116         QString term;
00117         QString mimeType;
00118         RunnerContext::Type type;
00119         RunnerContext * q;
00120 };
00121 
00122 RunnerContext::RunnerContext(QObject *parent)
00123     : QObject(parent),
00124       d(new RunnerContextPrivate(this))
00125 {
00126 }
00127 
00128 //copy ctor
00129 RunnerContext::RunnerContext(RunnerContext &other, QObject *parent)
00130     : QObject(parent)
00131 {
00132     LOCK_FOR_READ((&other))
00133     d = other.d;
00134     UNLOCK((&other))
00135 }
00136 
00137 RunnerContext::~RunnerContext()
00138 {
00139 }
00140 
00141 void RunnerContext::reset()
00142 {
00143     // We will detach if we are a copy of someone. But we will reset
00144     // if we are the 'main' context others copied from. Resetting
00145     // one RunnerContext makes all the copies oneobsolete.
00146     d.detach();
00147 
00148     // we still have to remove all the matches, since if the
00149     // ref count was 1 (e.g. only the RunnerContext is using
00150     // the dptr) then we won't get a copy made
00151     if (!d->matches.isEmpty()) {
00152         d->matchesById.clear();
00153         d->matches.clear();
00154         emit d->q->matchesChanged();
00155     }
00156 
00157     d->term.clear();
00158     d->mimeType.clear();
00159     d->type = UnknownType;
00160     //kDebug() << "match count" << d->matches.count();
00161 }
00162 
00163 void RunnerContext::setQuery(const QString &term)
00164 {
00165     reset();
00166 
00167     if (term.isEmpty()) {
00168         return;
00169     }
00170 
00171     d->term = term;
00172     d->determineType();
00173 }
00174 
00175 QString RunnerContext::query() const
00176 {
00177     // the query term should never be set after
00178     // a search starts. in fact, reset() ensures this
00179     // and setQuery(QString) calls reset()
00180     return d->term;
00181 }
00182 
00183 RunnerContext::Type RunnerContext::type() const
00184 {
00185     return d->type;
00186 }
00187 
00188 QString RunnerContext::mimeType() const
00189 {
00190     return d->mimeType;
00191 }
00192 
00193 bool RunnerContext::addMatches(const QString &term, const QList<QueryMatch> &matches)
00194 {
00195     Q_UNUSED(term)
00196 
00197     if (matches.isEmpty()) {
00198         return false;
00199     }
00200 
00201     LOCK_FOR_WRITE(this)
00202     foreach (const QueryMatch &match, matches) {
00203         d->matches.append(match);
00204 #ifndef NDEBUG
00205         if (d->matchesById.contains(match.id())) {
00206                 kDebug() << "Duplicate match id " << match.id() << "from" << match.runner()->name();
00207         }
00208 #endif
00209         d->matchesById.insert(match.id(), &d->matches.at(d->matches.size() - 1));
00210     }
00211     UNLOCK(this);
00212     //kDebug()<< "add matches";
00213     // A copied searchContext may share the d pointer,
00214     // we always want to sent the signal of the object that created
00215     // the d pointer
00216     emit d->q->matchesChanged();
00217     return true;
00218 }
00219 
00220 bool RunnerContext::addMatch(const QString &term, const QueryMatch &match)
00221 {
00222     Q_UNUSED(term)
00223 
00224     LOCK_FOR_WRITE(this)
00225     d->matches.append(match);
00226     d->matchesById.insert(match.id(), &d->matches.at(d->matches.size() - 1));
00227     UNLOCK(this);
00228     //kDebug()<< "added match" << match->text();
00229     emit d->q->matchesChanged();
00230 
00231     return true;
00232 }
00233 
00234 QList<QueryMatch> RunnerContext::matches() const
00235 {
00236     LOCK_FOR_READ(this)
00237     QList<QueryMatch> matches = d->matches;
00238     UNLOCK(this);
00239     return matches;
00240 }
00241 
00242 QueryMatch RunnerContext::match(const QString &id) const
00243 {
00244     LOCK_FOR_READ(this)
00245     const QueryMatch *match = d->matchesById.value(id, 0);
00246     UNLOCK(this)
00247 
00248     if (match) {
00249         return *match;
00250     }
00251 
00252     return QueryMatch(0);
00253 }
00254 
00255 } // Plasma namespace
00256 
00257 #include "runnercontext.moc"

Plasma

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
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