Warning, file /utilities/keditbookmarks/src/bookmarkiterator.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002    Copyright (C) 2000, 2010 David Faure <faure@kde.org>
0003    Copyright (C) 2002-2003 Alexander Kellett <lypanov@kde.org>
0004 
0005    This program is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU General Public
0007    License version 2 or at your option version 3 as published by
0008    the Free Software Foundation.
0009 
0010    This program is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    General Public License for more details.
0014 
0015    You should have received a copy of the GNU General Public License
0016    along with this program; see the file COPYING.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "bookmarkiterator.h"
0022 #include "kbookmarkmodel/model.h"
0023 #include <KBookmarkManager>
0024 
0025 #include "keditbookmarks_debug.h"
0026 #include <QTimer>
0027 
0028 BookmarkIterator::BookmarkIterator(BookmarkIteratorHolder *holder, const QList<KBookmark> &bks)
0029     : QObject(holder)
0030     , m_bookmarkList(bks)
0031     , m_holder(holder)
0032 {
0033     delayedEmitNextOne();
0034 }
0035 
0036 BookmarkIterator::~BookmarkIterator()
0037 {
0038 }
0039 
0040 void BookmarkIterator::delayedEmitNextOne()
0041 {
0042     QTimer::singleShot(1, this, &BookmarkIterator::nextOne);
0043 }
0044 
0045 KBookmark BookmarkIterator::currentBookmark()
0046 {
0047     return m_bk;
0048 }
0049 
0050 void BookmarkIterator::nextOne()
0051 {
0052     // //qCDebug(KEDITBOOKMARKS_LOG) << "BookmarkIterator::nextOne";
0053 
0054     // Look for an interesting bookmark
0055     while (!m_bookmarkList.isEmpty()) {
0056         KBookmark bk = m_bookmarkList.takeFirst();
0057         if (bk.hasParent() && isApplicable(bk)) {
0058             m_bk = bk;
0059             doAction();
0060             // Async action started, we'll have to come back later
0061             return;
0062         }
0063     }
0064     if (m_bookmarkList.isEmpty()) {
0065         holder()->removeIterator(this); // deletes "this"
0066         return;
0067     }
0068 }
0069 
0070 KBookmarkModel *BookmarkIterator::model()
0071 {
0072     return m_holder->model();
0073 }
0074 
0075 /* --------------------------- */
0076 
0077 BookmarkIteratorHolder::BookmarkIteratorHolder(QObject *parent, KBookmarkModel *model)
0078     : QObject(parent)
0079     , m_model(model)
0080 {
0081     Q_ASSERT(m_model);
0082 }
0083 
0084 void BookmarkIteratorHolder::insertIterator(BookmarkIterator *itr)
0085 {
0086     m_iterators.prepend(itr);
0087     doIteratorListChanged();
0088 }
0089 
0090 void BookmarkIteratorHolder::removeIterator(BookmarkIterator *itr)
0091 {
0092     m_iterators.removeAll(itr);
0093     itr->deleteLater();
0094     doIteratorListChanged();
0095 }
0096 
0097 void BookmarkIteratorHolder::cancelAllItrs()
0098 {
0099     const auto iterList = m_iterators;
0100     for (BookmarkIterator *iterator : iterList) {
0101         iterator->cancel();
0102     }
0103     qDeleteAll(m_iterators);
0104     m_iterators.clear();
0105     doIteratorListChanged();
0106 }
0107 
0108 void BookmarkIteratorHolder::addAffectedBookmark(const QString &address)
0109 {
0110     // qCDebug(KEDITBOOKMARKS_LOG) << address;
0111     if (m_affectedBookmark.isNull())
0112         m_affectedBookmark = address;
0113     else
0114         m_affectedBookmark = KBookmark::commonParent(m_affectedBookmark, address);
0115     // qCDebug(KEDITBOOKMARKS_LOG) << "m_affectedBookmark is now" << m_affectedBookmark;
0116 }
0117 
0118 void BookmarkIteratorHolder::doIteratorListChanged()
0119 {
0120     // qCDebug(KEDITBOOKMARKS_LOG) << count() << "iterators";
0121     Q_EMIT setCancelEnabled(count() > 0);
0122     if (count() == 0) {
0123         // qCDebug(KEDITBOOKMARKS_LOG) << "Notifying managers" << m_affectedBookmark;
0124         KBookmarkManager *mgr = m_model->bookmarkManager();
0125         model()->notifyManagers(mgr->findByAddress(m_affectedBookmark).toGroup());
0126         m_affectedBookmark.clear();
0127     }
0128 }