Changeset View
Changeset View
Standalone View
Standalone View
runners/places/placesrunner.cpp
Show First 20 Lines • Show All 68 Lines • ▼ Show 20 Line(s) | |||||
69 | 69 | | |||
70 | PlacesRunnerHelper::PlacesRunnerHelper(PlacesRunner *runner) | 70 | PlacesRunnerHelper::PlacesRunnerHelper(PlacesRunner *runner) | ||
71 | : QObject(runner) | 71 | : QObject(runner) | ||
72 | { | 72 | { | ||
73 | Q_ASSERT(QThread::currentThread() == QCoreApplication::instance()->thread()); | 73 | Q_ASSERT(QThread::currentThread() == QCoreApplication::instance()->thread()); | ||
74 | connect(runner, &PlacesRunner::doMatch, | 74 | connect(runner, &PlacesRunner::doMatch, | ||
75 | this, &PlacesRunnerHelper::match, | 75 | this, &PlacesRunnerHelper::match, | ||
76 | Qt::BlockingQueuedConnection); | 76 | Qt::BlockingQueuedConnection); | ||
77 | | ||||
78 | connect(&m_places, &KFilePlacesModel::setupDone, this, [this](const QModelIndex &index, bool success) { | ||||
79 | if (success && m_pendingUdi == m_places.deviceForIndex(index).udi()) { | ||||
80 | new KRun(m_places.url(index), nullptr); | ||||
81 | } | ||||
82 | m_pendingUdi.clear(); | ||||
83 | }); | ||||
77 | } | 84 | } | ||
78 | 85 | | |||
79 | void PlacesRunnerHelper::match(Plasma::RunnerContext *c) | 86 | void PlacesRunnerHelper::match(Plasma::RunnerContext *c) | ||
80 | { | 87 | { | ||
81 | Plasma::RunnerContext &context = *c; | 88 | Plasma::RunnerContext &context = *c; | ||
82 | if (!context.isValid()) { | 89 | if (!context.isValid()) { | ||
83 | return; | 90 | return; | ||
84 | } | 91 | } | ||
Show All 23 Lines | 101 | for (int i = 0; i <= m_places.rowCount(); i++) { | |||
108 | if (type != Plasma::QueryMatch::NoMatch) { | 115 | if (type != Plasma::QueryMatch::NoMatch) { | ||
109 | Plasma::QueryMatch match(static_cast<PlacesRunner *>(parent())); | 116 | Plasma::QueryMatch match(static_cast<PlacesRunner *>(parent())); | ||
110 | match.setType(type); | 117 | match.setType(type); | ||
111 | match.setRelevance(relevance); | 118 | match.setRelevance(relevance); | ||
112 | match.setIcon(m_places.icon(current_index)); | 119 | match.setIcon(m_places.icon(current_index)); | ||
113 | match.setText(text); | 120 | match.setText(text); | ||
114 | 121 | | |||
115 | //if we have to mount it set the device udi instead of the URL, as we can't open it directly | 122 | //if we have to mount it set the device udi instead of the URL, as we can't open it directly | ||
116 | QUrl url; | | |||
117 | if (m_places.isDevice(current_index) && m_places.setupNeeded(current_index)) { | 123 | if (m_places.isDevice(current_index) && m_places.setupNeeded(current_index)) { | ||
118 | url = QUrl(m_places.deviceForIndex(current_index).udi()); | 124 | const QString udi = m_places.deviceForIndex(current_index).udi(); | ||
125 | match.setId(udi); | ||||
126 | match.setData(udi); | ||||
119 | } else { | 127 | } else { | ||
120 | url = m_places.url(current_index); | 128 | const QUrl url = m_places.url(current_index); | ||
121 | } | | |||
122 | | ||||
123 | match.setData(url); | 129 | match.setData(url); | ||
124 | match.setId(url.toDisplayString()); | 130 | match.setId(url.toDisplayString()); | ||
131 | } | ||||
132 | | ||||
125 | matches << match; | 133 | matches << match; | ||
126 | } | 134 | } | ||
127 | } | 135 | } | ||
128 | 136 | | |||
129 | context.addMatches(matches); | 137 | context.addMatches(matches); | ||
130 | } | 138 | } | ||
131 | 139 | | |||
140 | void PlacesRunnerHelper::openDevice(const QString &udi) | ||||
141 | { | ||||
142 | m_pendingUdi.clear(); | ||||
143 | | ||||
144 | for (int i = 0; i < m_places.rowCount(); ++i) { | ||||
145 | const QModelIndex idx = m_places.index(i, 0); | ||||
146 | if (m_places.isDevice(idx) && m_places.deviceForIndex(idx).udi() == udi) { | ||||
147 | m_pendingUdi = udi; | ||||
148 | m_places.requestSetup(idx); | ||||
149 | break; | ||||
150 | } | ||||
151 | } | ||||
152 | } | ||||
132 | 153 | | |||
133 | void PlacesRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &action) | 154 | void PlacesRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &action) | ||
134 | { | 155 | { | ||
135 | Q_UNUSED(context); | 156 | Q_UNUSED(context); | ||
136 | //I don't just pass the model index because the list could change before the user clicks on it, which would make everything go wrong. Ideally we don't want things to go wrong. | 157 | //I don't just pass the model index because the list could change before the user clicks on it, which would make everything go wrong. Ideally we don't want things to go wrong. | ||
137 | if (action.data().type() == QVariant::Url) { | 158 | if (action.data().type() == QVariant::Url) { | ||
138 | new KRun(action.data().toUrl(), 0); | 159 | new KRun(action.data().toUrl(), 0); | ||
139 | } else if (action.data().canConvert<QString>()) { | 160 | } else if (action.data().canConvert<QString>()) { | ||
140 | //search our list for the device with the same udi, then set it up (mount it). | 161 | m_helper->openDevice(action.data().toString()); | ||
141 | QString deviceUdi = action.data().toString(); | | |||
142 | | ||||
143 | // gets deleted in setupComplete | | |||
144 | KFilePlacesModel *places = new KFilePlacesModel(this); | | |||
145 | connect(places, SIGNAL(setupDone(QModelIndex,bool)), SLOT(setupComplete(QModelIndex,bool))); | | |||
146 | bool found = false; | | |||
147 | | ||||
148 | for (int i = 0; i <= places->rowCount();i++) { | | |||
149 | QModelIndex current_index = places->index(i, 0); | | |||
150 | if (places->isDevice(current_index) && places->deviceForIndex(current_index).udi() == deviceUdi) { | | |||
151 | places->requestSetup(current_index); | | |||
152 | found = true; | | |||
153 | break; | | |||
154 | } | | |||
155 | } | | |||
156 | | ||||
157 | if (!found) { | | |||
158 | delete places; | | |||
159 | } | | |||
160 | } | 162 | } | ||
161 | } | 163 | } | ||
162 | 164 | | |||
163 | QMimeData *PlacesRunner::mimeDataForMatch(const Plasma::QueryMatch &match) | 165 | QMimeData *PlacesRunner::mimeDataForMatch(const Plasma::QueryMatch &match) | ||
164 | { | 166 | { | ||
165 | if (match.data().type() == QVariant::Url) { | 167 | if (match.data().type() == QVariant::Url) { | ||
166 | QMimeData *result = new QMimeData(); | 168 | QMimeData *result = new QMimeData(); | ||
167 | result->setUrls({match.data().toUrl()}); | 169 | result->setUrls({match.data().toUrl()}); | ||
168 | return result; | 170 | return result; | ||
169 | } | 171 | } | ||
170 | 172 | | |||
171 | return nullptr; | 173 | return nullptr; | ||
172 | } | 174 | } | ||
173 | 175 | | |||
174 | //if a device needed mounting, this slot gets called when it's finished. | | |||
175 | void PlacesRunner::setupComplete(QModelIndex index, bool success) | | |||
176 | { | | |||
177 | KFilePlacesModel *places = qobject_cast<KFilePlacesModel*>(sender()); | | |||
178 | //qDebug() << "setup complete" << places << sender(); | | |||
179 | if (success && places) { | | |||
180 | new KRun(places->url(index), 0); | | |||
181 | places->deleteLater(); | | |||
182 | } | | |||
183 | } | | |||
184 | | ||||
185 | #include "placesrunner.moc" | 176 | #include "placesrunner.moc" |