libplasma
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>
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
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 = 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
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
00146
00147
00148 d.detach();
00149
00150
00151
00152
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
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
00181
00182
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
00216
00217
00218
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
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 }
00259
00260 #include "runnercontext.moc"