Plasma
runnercontext.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00040
00041
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
00066 }
00067
00068 ~RunnerContextPrivate()
00069 {
00070 }
00071
00075 void determineType()
00076 {
00077
00078
00079
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
00086
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
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
00144
00145
00146 d.detach();
00147
00148
00149
00150
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
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
00178
00179
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
00213
00214
00215
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
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 }
00256
00257 #include "runnercontext.moc"