File indexing completed on 2024-06-23 05:49:22

0001 /*
0002     This file is part of the Kasten Framework, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2006-2007, 2009, 2011, 2019 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "documentmanager_p.hpp"
0010 
0011 // lib
0012 #include <abstractdocument.hpp>
0013 // Qt
0014 #include <QUrl>
0015 #include <QMutableVectorIterator>
0016 #include <QStringList>
0017 // Std
0018 #include <utility>
0019 
0020 // temporary
0021 #include "documentcreatemanager.hpp"
0022 #include "documentsyncmanager.hpp"
0023 #include "modelcodecmanager.hpp"
0024 
0025 namespace Kasten {
0026 
0027 static int lastDocumentId = 0;
0028 
0029 DocumentManagerPrivate::DocumentManagerPrivate(DocumentManager* q)
0030     : q_ptr(q)
0031     , mCreateManager(new DocumentCreateManager(q))
0032     , mSyncManager(new DocumentSyncManager(q))
0033     , mCodecManager(new ModelCodecManager())
0034 {
0035 }
0036 
0037 DocumentManagerPrivate::~DocumentManagerPrivate()
0038 {
0039     // TODO: emit signal here, too?
0040     qDeleteAll(mList);
0041 
0042     delete mCreateManager;
0043     delete mSyncManager;
0044     delete mCodecManager;
0045 } // TODO: destroy all documents?
0046 
0047 void DocumentManagerPrivate::addDocument(AbstractDocument* document)
0048 {
0049     Q_Q(DocumentManager);
0050 
0051     // TODO: check for double insert
0052     document->setId(QString::number(++lastDocumentId));
0053     mList.append(document);
0054     // TODO: only emit if document was not included before
0055     const QVector<AbstractDocument*> addedDocuments { document };
0056     Q_EMIT q->added(addedDocuments);
0057 }
0058 
0059 void DocumentManagerPrivate::closeDocument(AbstractDocument* document)
0060 {
0061     Q_Q(DocumentManager);
0062 
0063     QMutableVectorIterator<AbstractDocument*> iterator(mList);
0064 
0065     if (iterator.findNext(document)) {
0066         // TODO: first check if unsaved and ask, only then close
0067 
0068         iterator.remove();
0069 
0070         const QVector<AbstractDocument*> closedDocuments { document };
0071         Q_EMIT q->closing(closedDocuments);
0072 
0073         delete document;
0074     }
0075 }
0076 
0077 void DocumentManagerPrivate::closeDocuments(const QVector<AbstractDocument*>& documents)
0078 {
0079     Q_Q(DocumentManager);
0080 
0081     // TODO: optimize
0082     for (AbstractDocument* document : documents) {
0083         mList.removeOne(document);
0084     }
0085 
0086     Q_EMIT q->closing(documents);
0087 
0088     for (AbstractDocument* document : documents) {
0089         delete document;
0090     }
0091 }
0092 
0093 void DocumentManagerPrivate::closeAll()
0094 {
0095     Q_Q(DocumentManager);
0096 
0097     // TODO: is it better for remove the document from the list before emitting closing(document)?
0098     // TODO: or better emit close(documentList)? who would use this?
0099     const QVector<AbstractDocument*> closedDocuments = mList;
0100     mList.clear();
0101 
0102     Q_EMIT q->closing(closedDocuments);
0103 
0104     for (AbstractDocument* document : closedDocuments) {
0105         delete document;
0106     }
0107 }
0108 
0109 void DocumentManagerPrivate::closeAllOther(AbstractDocument* keptDocument)
0110 {
0111     Q_Q(DocumentManager);
0112 
0113     // TODO: is it better for remove the document from the list before emitting closing(document)?
0114     // TODO: or better emit close(documentList)? who would use this?
0115     QVector<AbstractDocument*> closedDocuments = mList;
0116     closedDocuments.removeOne(keptDocument);
0117 
0118     mList.clear();
0119     mList.append(keptDocument);
0120 
0121     Q_EMIT q->closing(closedDocuments);
0122 
0123     for (AbstractDocument* document : std::as_const(closedDocuments)) {
0124         delete document;
0125     }
0126 }
0127 
0128 bool DocumentManagerPrivate::canClose(AbstractDocument* document) const
0129 {
0130     return mSyncManager->canClose(document);
0131 }
0132 
0133 bool DocumentManagerPrivate::canClose(const QVector<AbstractDocument*>& documents) const
0134 {
0135     bool canClose = true;
0136 
0137     for (AbstractDocument* document : documents) {
0138         if (!mSyncManager->canClose(document)) {
0139             canClose = false;
0140             break;
0141         }
0142     }
0143 
0144     return canClose;
0145 }
0146 
0147 bool DocumentManagerPrivate::canCloseAll() const
0148 {
0149     bool canCloseAll = true;
0150 
0151     for (AbstractDocument* document : std::as_const(mList)) {
0152         if (!mSyncManager->canClose(document)) {
0153             canCloseAll = false;
0154             break;
0155         }
0156     }
0157 
0158     return canCloseAll;
0159 }
0160 
0161 bool DocumentManagerPrivate::canCloseAllOther(AbstractDocument* keptDocument) const
0162 {
0163     bool canCloseAll = true;
0164 
0165     for (AbstractDocument* document : std::as_const(mList)) {
0166         if ((document != keptDocument) &&
0167             !mSyncManager->canClose(document)) {
0168             canCloseAll = false;
0169             break;
0170         }
0171     }
0172 
0173     return canCloseAll;
0174 }
0175 
0176 void DocumentManagerPrivate::requestFocus(AbstractDocument* document)
0177 {
0178     Q_Q(DocumentManager);
0179 
0180     Q_EMIT q->focusRequested(document);
0181 }
0182 
0183 }