File indexing completed on 2022-11-29 20:24:09

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 <QTimer>
0026 
0027 BookmarkIterator::BookmarkIterator(BookmarkIteratorHolder *holder, const QList<KBookmark> &bks)
0028     : QObject(holder)
0029     , m_bookmarkList(bks)
0030     , m_holder(holder)
0031 {
0032     delayedEmitNextOne();
0033 }
0034 
0035 BookmarkIterator::~BookmarkIterator()
0036 {
0037 }
0038 
0039 void BookmarkIterator::delayedEmitNextOne()
0040 {
0041     QTimer::singleShot(1, this, &BookmarkIterator::nextOne);
0042 }
0043 
0044 KBookmark BookmarkIterator::currentBookmark()
0045 {
0046     return m_bk;
0047 }
0048 
0049 void BookmarkIterator::nextOne()
0050 {
0051     // //qCDebug(KEDITBOOKMARKS_LOG) << "BookmarkIterator::nextOne";
0052 
0053     // Look for an interesting bookmark
0054     while (!m_bookmarkList.isEmpty()) {
0055         KBookmark bk = m_bookmarkList.takeFirst();
0056         if (bk.hasParent() && isApplicable(bk)) {
0057             m_bk = bk;
0058             doAction();
0059             // Async action started, we'll have to come back later
0060             return;
0061         }
0062     }
0063     if (m_bookmarkList.isEmpty()) {
0064         holder()->removeIterator(this); // deletes "this"
0065         return;
0066     }
0067 }
0068 
0069 KBookmarkModel *BookmarkIterator::model()
0070 {
0071     return m_holder->model();
0072 }
0073 
0074 /* --------------------------- */
0075 
0076 BookmarkIteratorHolder::BookmarkIteratorHolder(QObject *parent, KBookmarkModel *model)
0077     : QObject(parent)
0078     , m_model(model)
0079 {
0080     Q_ASSERT(m_model);
0081 }
0082 
0083 void BookmarkIteratorHolder::insertIterator(BookmarkIterator *itr)
0084 {
0085     m_iterators.prepend(itr);
0086     doIteratorListChanged();
0087 }
0088 
0089 void BookmarkIteratorHolder::removeIterator(BookmarkIterator *itr)
0090 {
0091     m_iterators.removeAll(itr);
0092     itr->deleteLater();
0093     doIteratorListChanged();
0094 }
0095 
0096 void BookmarkIteratorHolder::cancelAllItrs()
0097 {
0098     const auto iterList = m_iterators;
0099     for (BookmarkIterator *iterator : iterList) {
0100         iterator->cancel();
0101     }
0102     qDeleteAll(m_iterators);
0103     m_iterators.clear();
0104     doIteratorListChanged();
0105 }
0106 
0107 void BookmarkIteratorHolder::addAffectedBookmark(const QString &address)
0108 {
0109     // qCDebug(KEDITBOOKMARKS_LOG) << address;
0110     if (m_affectedBookmark.isNull())
0111         m_affectedBookmark = address;
0112     else
0113         m_affectedBookmark = KBookmark::commonParent(m_affectedBookmark, address);
0114     // qCDebug(KEDITBOOKMARKS_LOG) << "m_affectedBookmark is now" << m_affectedBookmark;
0115 }
0116 
0117 void BookmarkIteratorHolder::doIteratorListChanged()
0118 {
0119     // qCDebug(KEDITBOOKMARKS_LOG) << count() << "iterators";
0120     Q_EMIT setCancelEnabled(count() > 0);
0121     if (count() == 0) {
0122         // qCDebug(KEDITBOOKMARKS_LOG) << "Notifying managers" << m_affectedBookmark;
0123         KBookmarkManager *mgr = m_model->bookmarkManager();
0124         model()->notifyManagers(mgr->findByAddress(m_affectedBookmark).toGroup());
0125         m_affectedBookmark.clear();
0126     }
0127 }