File indexing completed on 2024-04-28 05:49:29

0001 /* This file is part of the KDE project
0002    SPDX-FileCopyrightText: 2001 Christoph Cullmann <cullmann@kde.org>
0003    SPDX-FileCopyrightText: 2002 Joseph Wenninger <jowenn@kde.org>
0004 
0005    SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #pragma once
0009 
0010 #include <ktexteditor/document.h>
0011 
0012 #include <QList>
0013 #include <QObject>
0014 
0015 #include <KConfig>
0016 
0017 #include <unordered_map>
0018 
0019 class KateMainWindow;
0020 
0021 class KateDocumentInfo
0022 {
0023 public:
0024     enum CustomRoles { RestoreOpeningFailedRole };
0025 
0026 public:
0027     KateDocumentInfo() = default;
0028 
0029     bool modifiedOnDisc = false;
0030     KTextEditor::Document::ModifiedOnDiskReason modifiedOnDiscReason = KTextEditor::Document::OnDiskUnmodified;
0031 
0032     bool openedByUser = false;
0033     bool openSuccess = true;
0034     KTextEditor::Cursor startCursor = KTextEditor::Cursor::invalid(); // initial cursor position. This can be specified in the command line or as a url query
0035     bool wasDocumentEverModified = false;
0036 
0037     QUrl normalizedUrl;
0038 };
0039 
0040 class KateDocManager : public QObject
0041 {
0042     Q_OBJECT
0043 
0044 public:
0045     explicit KateDocManager(QObject *parent);
0046     ~KateDocManager() override;
0047 
0048     KTextEditor::Document *createDoc(const KateDocumentInfo &docInfo = KateDocumentInfo());
0049 
0050     KateDocumentInfo *documentInfo(KTextEditor::Document *doc);
0051 
0052     /** Returns the documentNumber of the doc with url URL or -1 if no such doc is found */
0053     KTextEditor::Document *findDocument(const QUrl &url) const;
0054 
0055     const QList<KTextEditor::Document *> &documentList() const
0056     {
0057         return m_docList;
0058     }
0059 
0060     KTextEditor::Document *openUrl(const QUrl &, const QString &encoding = QString(), const KateDocumentInfo &docInfo = KateDocumentInfo());
0061 
0062     std::vector<KTextEditor::Document *>
0063     openUrls(const QList<QUrl> &, const QString &encoding = QString(), const KateDocumentInfo &docInfo = KateDocumentInfo());
0064 
0065     bool closeDocument(KTextEditor::Document *, bool closeUrl = true);
0066     bool closeDocuments(const QList<KTextEditor::Document *> documents, bool closeUrl = true);
0067     bool closeDocumentList(const QList<KTextEditor::Document *> &documents, KateMainWindow *window);
0068     bool closeAllDocuments(bool closeUrl = true);
0069     bool closeOtherDocuments(KTextEditor::Document *);
0070 
0071     std::vector<KTextEditor::Document *> modifiedDocumentList();
0072     bool queryCloseDocuments(KateMainWindow *w);
0073 
0074     void saveDocumentList(KConfig *config);
0075     void restoreDocumentList(KConfig *config);
0076 
0077     inline bool getSaveMetaInfos()
0078     {
0079         return m_saveMetaInfos;
0080     }
0081     inline void setSaveMetaInfos(bool b)
0082     {
0083         m_saveMetaInfos = b;
0084     }
0085 
0086     inline int getDaysMetaInfos()
0087     {
0088         return m_daysMetaInfos;
0089     }
0090     inline void setDaysMetaInfos(int i)
0091     {
0092         m_daysMetaInfos = i;
0093     }
0094 
0095 public Q_SLOTS:
0096     /**
0097      * saves all documents that has at least one view.
0098      * documents with no views are ignored :P
0099      */
0100     void saveAll();
0101 
0102     /**
0103      * reloads all documents that has at least one view.
0104      * documents with no views are ignored :P
0105      */
0106     void reloadAll();
0107 
0108     /**
0109      * close all documents, which could not be reopened
0110      */
0111     void closeOrphaned();
0112 
0113     /**
0114      * save selected documents from the File List
0115      */
0116     static void saveSelected(const QList<KTextEditor::Document *> &);
0117 
0118 Q_SIGNALS:
0119     /**
0120      * This signal is emitted when the \p document was created.
0121      */
0122     void documentCreated(KTextEditor::Document *document);
0123 
0124     /**
0125      * This signal is emitted before a \p document which should be closed is deleted
0126      * The document is still accessible and usable, but it will be deleted
0127      * after this signal was send.
0128      *
0129      * @param document document that will be deleted
0130      */
0131     void documentWillBeDeleted(KTextEditor::Document *document);
0132 
0133     /**
0134      * This signal is emitted when the \p document has been deleted.
0135      *
0136      *  Warning !!! DO NOT ACCESS THE DATA REFERENCED BY THE POINTER, IT IS ALREADY INVALID
0137      *  Use the pointer only to remove mappings in hash or maps
0138      */
0139     void documentDeleted(KTextEditor::Document *document);
0140 
0141     /**
0142      * This signal is emitted before the documents batch is going to be deleted
0143      *
0144      * note that the batch can be interrupted in the middle and only some
0145      * of the documents may be actually deleted. See documentsDeleted() signal.
0146      */
0147     void aboutToDeleteDocuments(const QList<KTextEditor::Document *> &);
0148 
0149     /**
0150      * This signal is emitted after the documents batch was deleted
0151      *
0152      * This is the batch closing signal for aboutToDeleteDocuments
0153      * @param documents the documents that weren't deleted after all
0154      */
0155     void documentsDeleted(const QList<KTextEditor::Document *> &documents);
0156 
0157 private Q_SLOTS:
0158     void slotModifiedOnDisc(KTextEditor::Document *doc, bool b, KTextEditor::Document::ModifiedOnDiskReason reason);
0159     void slotModChanged(KTextEditor::Document *doc);
0160     void slotModChanged1(KTextEditor::Document *doc);
0161     void slotUrlChanged(const QUrl &newUrl);
0162 
0163 private:
0164     bool loadMetaInfos(KTextEditor::Document *doc, const QUrl &url);
0165     void saveMetaInfos(const QList<KTextEditor::Document *> &docs);
0166 
0167     QList<KTextEditor::Document *> m_docList;
0168     std::unordered_map<KTextEditor::Document *, KateDocumentInfo> m_docInfos;
0169 
0170     KConfig m_metaInfos;
0171     bool m_saveMetaInfos;
0172     int m_daysMetaInfos;
0173 
0174 private Q_SLOTS:
0175     void documentOpened();
0176 };