Changeset View
Changeset View
Standalone View
Standalone View
addons/close-except-like/close_except_plugin.cpp
Show First 20 Lines • Show All 220 Lines • ▼ Show 20 Line(s) | 201 | { | |||
---|---|---|---|---|---|
221 | menu->addAction(m_show_confirmation_action); | 221 | menu->addAction(m_show_confirmation_action); | ||
222 | } | 222 | } | ||
223 | 223 | | |||
224 | void CloseExceptPluginView::updateMenu() | 224 | void CloseExceptPluginView::updateMenu() | ||
225 | { | 225 | { | ||
226 | const QList<KTextEditor::Document*>& docs = KTextEditor::Editor::instance()->application()->documents(); | 226 | const QList<KTextEditor::Document*>& docs = KTextEditor::Editor::instance()->application()->documents(); | ||
227 | if (docs.size() < 2) | 227 | if (docs.size() < 2) | ||
228 | { | 228 | { | ||
229 | qDebug() << "No docs r (or the only) opened right now --> disable menu"; | 229 | //qDebug() << "No docs r (or the only) opened right now --> disable menu"; | ||
230 | m_except_menu->setEnabled(false); | 230 | m_except_menu->setEnabled(false); | ||
231 | m_except_menu->addSeparator(); | 231 | m_except_menu->addSeparator(); | ||
232 | m_like_menu->setEnabled(false); | 232 | m_like_menu->setEnabled(false); | ||
233 | m_like_menu->addSeparator(); | 233 | m_like_menu->addSeparator(); | ||
234 | /// \note It seems there is always a document present... it named \em 'Untitled' | 234 | /// \note It seems there is always a document present... it named \em 'Untitled' | ||
235 | } | 235 | } | ||
236 | else | 236 | else | ||
237 | { | 237 | { | ||
238 | // Iterate over documents and form a set of candidates | 238 | // Iterate over documents and form a set of candidates | ||
239 | typedef std::set<QUrl> paths_set_type; | 239 | typedef std::set<QUrl> paths_set_type; | ||
240 | typedef std::set<QString> paths_set_type_masks; | 240 | typedef std::set<QString> paths_set_type_masks; | ||
241 | paths_set_type doc_paths; | 241 | paths_set_type doc_paths; | ||
242 | paths_set_type_masks masks; | 242 | paths_set_type_masks masks; | ||
243 | Q_FOREACH(KTextEditor::Document* document, docs) | 243 | Q_FOREACH(KTextEditor::Document* document, docs) | ||
244 | { | 244 | { | ||
245 | const QString& ext = QFileInfo(document->url().path()).completeSuffix(); | 245 | const QString& ext = QFileInfo(document->url().path()).completeSuffix(); | ||
246 | if (!ext.isEmpty()) | 246 | if (!ext.isEmpty()) | ||
247 | masks.insert(QStringLiteral("*.") + ext); | 247 | masks.insert(QStringLiteral("*.") + ext); | ||
248 | doc_paths.insert(KIO::upUrl(document->url())); | 248 | doc_paths.insert(KIO::upUrl(document->url())); | ||
249 | } | 249 | } | ||
250 | paths_set_type paths = doc_paths; | 250 | paths_set_type paths = doc_paths; | ||
251 | qDebug() << "stage #1: Collected" << paths.size() << "paths and" << masks.size() << "masks"; | 251 | //qDebug() << "stage #1: Collected" << paths.size() << "paths and" << masks.size() << "masks"; | ||
252 | // Add common paths to the collection | 252 | // Add common paths to the collection | ||
253 | for (paths_set_type::iterator it = doc_paths.begin(), last = doc_paths.end(); it != last; ++it) | 253 | for (paths_set_type::iterator it = doc_paths.begin(), last = doc_paths.end(); it != last; ++it) | ||
254 | { | 254 | { | ||
255 | for ( | 255 | for ( | ||
256 | QUrl url = *it | 256 | QUrl url = *it | ||
257 | ; (!url.path().isEmpty()) && url.path() != QStringLiteral("/") | 257 | ; (!url.path().isEmpty()) && url.path() != QStringLiteral("/") | ||
258 | ; url = KIO::upUrl(url) | 258 | ; url = KIO::upUrl(url) | ||
259 | ) | 259 | ) | ||
260 | { | 260 | { | ||
261 | paths_set_type::iterator not_it = it; | 261 | paths_set_type::iterator not_it = it; | ||
262 | for (++not_it; not_it != last; ++not_it) | 262 | for (++not_it; not_it != last; ++not_it) | ||
263 | if (!not_it->path().startsWith(url.path())) | 263 | if (!not_it->path().startsWith(url.path())) | ||
264 | break; | 264 | break; | ||
265 | if (not_it == last) | 265 | if (not_it == last) | ||
266 | { | 266 | { | ||
267 | paths.insert(url); | 267 | paths.insert(url); | ||
268 | break; | 268 | break; | ||
269 | } | 269 | } | ||
270 | } | 270 | } | ||
271 | } | 271 | } | ||
272 | qDebug() << "stage #2: Collected" << paths.size() << "paths and" << masks.size() << "masks"; | 272 | //qDebug() << "stage #2: Collected" << paths.size() << "paths and" << masks.size() << "masks"; | ||
273 | // | 273 | // | ||
274 | updateMenu(paths, masks, m_except_actions, m_except_menu, &CloseExceptPluginView::closeExcept); | 274 | updateMenu(paths, masks, m_except_actions, m_except_menu, &CloseExceptPluginView::closeExcept); | ||
275 | updateMenu(paths, masks, m_like_actions, m_like_menu, &CloseExceptPluginView::closeLike); | 275 | updateMenu(paths, masks, m_like_actions, m_like_menu, &CloseExceptPluginView::closeLike); | ||
276 | } | 276 | } | ||
277 | } | 277 | } | ||
278 | 278 | | |||
279 | void CloseExceptPluginView::close(const QString& item, const bool close_if_match) | 279 | void CloseExceptPluginView::close(const QString& item, const bool close_if_match) | ||
280 | { | 280 | { | ||
281 | QChar asterisk=QLatin1Char('*'); | 281 | QChar asterisk=QLatin1Char('*'); | ||
282 | assert( | 282 | assert( | ||
283 | "Parameter seems invalid! Is smth has changed in the code?" | 283 | "Parameter seems invalid! Is smth has changed in the code?" | ||
284 | && !item.isEmpty() && (item[0] == asterisk || item[item.size() - 1] == asterisk) | 284 | && !item.isEmpty() && (item[0] == asterisk || item[item.size() - 1] == asterisk) | ||
285 | ); | 285 | ); | ||
286 | 286 | | |||
287 | const bool is_path = item[0] != asterisk; | 287 | const bool is_path = item[0] != asterisk; | ||
288 | const QString mask = is_path ? item.left(item.size() - 1) : item; | 288 | const QString mask = is_path ? item.left(item.size() - 1) : item; | ||
289 | qDebug() << "Going to close items [" << close_if_match << "/" << is_path << "]: " << mask; | 289 | //qDebug() << "Going to close items [" << close_if_match << "/" << is_path << "]: " << mask; | ||
290 | 290 | | |||
291 | QList<KTextEditor::Document*> docs2close; | 291 | QList<KTextEditor::Document*> docs2close; | ||
292 | const QList<KTextEditor::Document*>& docs = KTextEditor::Editor::instance()->application()->documents(); | 292 | const QList<KTextEditor::Document*>& docs = KTextEditor::Editor::instance()->application()->documents(); | ||
293 | Q_FOREACH(KTextEditor::Document* document, docs) | 293 | Q_FOREACH(KTextEditor::Document* document, docs) | ||
294 | { | 294 | { | ||
295 | const QString& path = KIO::upUrl(document->url()).path(); | 295 | const QString& path = KIO::upUrl(document->url()).path(); | ||
296 | /// \note Take a dot in account, so \c *.c would not match for \c blah.kcfgc | 296 | /// \note Take a dot in account, so \c *.c would not match for \c blah.kcfgc | ||
297 | const QString& ext = QLatin1Char('.') + QFileInfo(document->url().fileName()).completeSuffix(); | 297 | const QString& ext = QLatin1Char('.') + QFileInfo(document->url().fileName()).completeSuffix(); | ||
298 | const bool match = (!is_path && mask.endsWith(ext)) | 298 | const bool match = (!is_path && mask.endsWith(ext)) | ||
299 | || (is_path && path.startsWith(mask)) | 299 | || (is_path && path.startsWith(mask)) | ||
300 | ; | 300 | ; | ||
301 | if (match == close_if_match) | 301 | if (match == close_if_match) | ||
302 | { | 302 | { | ||
303 | qDebug() << "*** Will close: " << document->url(); | 303 | //qDebug() << "*** Will close: " << document->url(); | ||
304 | docs2close.push_back(document); | 304 | docs2close.push_back(document); | ||
305 | } | 305 | } | ||
306 | } | 306 | } | ||
307 | if (docs2close.isEmpty()) | 307 | if (docs2close.isEmpty()) | ||
308 | { | 308 | { | ||
309 | displayMessage( | 309 | displayMessage( | ||
310 | i18nc("@title:window", "Error") | 310 | i18nc("@title:window", "Error") | ||
311 | , i18nc("@info:tooltip", "No files to close ...") | 311 | , i18nc("@info:tooltip", "No files to close ...") | ||
▲ Show 20 Lines • Show All 52 Lines • Show Last 20 Lines |