1 /* This file is part of the KDE project
2    SPDX-FileCopyrightText: 2001 Christoph Cullmann <cullmann@kde.org>
3    SPDX-FileCopyrightText: 2002 Joseph Wenninger <jowenn@kde.org>
4 
5    SPDX-License-Identifier: LGPL-2.0-only
6 */
7 
8 #ifndef __KATE_DOCMANAGER_H__
9 #define __KATE_DOCMANAGER_H__
10 
11 #include <ktexteditor/document.h>
12 #include <ktexteditor/modificationinterface.h>
13 
14 #include <QDateTime>
15 #include <QList>
16 #include <QObject>
17 
18 #include <KConfig>
19 
20 #include <unordered_map>
21 
22 class KateMainWindow;
23 
24 class KateDocumentInfo
25 {
26 public:
27     enum CustomRoles { RestoreOpeningFailedRole };
28 
29 public:
30     KateDocumentInfo() = default;
31 
32     bool modifiedOnDisc = false;
33     KTextEditor::ModificationInterface::ModifiedOnDiskReason modifiedOnDiscReason = KTextEditor::ModificationInterface::OnDiskUnmodified;
34 
35     bool openedByUser = false;
36     bool openSuccess = true;
37     bool doPostLoadOperations = false;
38 };
39 
40 class KateDocManager : public QObject
41 {
42     Q_OBJECT
43 
44 public:
45     KateDocManager(QObject *parent);
46     ~KateDocManager() override;
47 
48     KTextEditor::Document *createDoc(const KateDocumentInfo &docInfo = KateDocumentInfo());
49 
50     KateDocumentInfo *documentInfo(KTextEditor::Document *doc);
51 
52     /** Returns the documentNumber of the doc with url URL or -1 if no such doc is found */
53     KTextEditor::Document *findDocument(const QUrl &url) const;
54 
documentList()55     const QList<KTextEditor::Document *> &documentList() const
56     {
57         return m_docList;
58     }
59 
60     KTextEditor::Document *
61     openUrl(const QUrl &, const QString &encoding = QString(), bool isTempFile = false, const KateDocumentInfo &docInfo = KateDocumentInfo());
62 
63     std::vector<KTextEditor::Document *>
64     openUrls(const QList<QUrl> &, const QString &encoding = QString(), bool isTempFile = false, const KateDocumentInfo &docInfo = KateDocumentInfo());
65 
66     bool closeDocument(KTextEditor::Document *, bool closeUrl = true);
67     bool closeDocuments(const QList<KTextEditor::Document *> &documents, bool closeUrl = true);
68     bool closeDocumentList(const QList<KTextEditor::Document *> &documents);
69     bool closeAllDocuments(bool closeUrl = true);
70     bool closeOtherDocuments(KTextEditor::Document *);
71 
72     std::vector<KTextEditor::Document *> modifiedDocumentList();
73     bool queryCloseDocuments(KateMainWindow *w);
74 
75     void saveDocumentList(KConfig *config);
76     void restoreDocumentList(KConfig *config);
77 
getSaveMetaInfos()78     inline bool getSaveMetaInfos()
79     {
80         return m_saveMetaInfos;
81     }
setSaveMetaInfos(bool b)82     inline void setSaveMetaInfos(bool b)
83     {
84         m_saveMetaInfos = b;
85     }
86 
getDaysMetaInfos()87     inline int getDaysMetaInfos()
88     {
89         return m_daysMetaInfos;
90     }
setDaysMetaInfos(int i)91     inline void setDaysMetaInfos(int i)
92     {
93         m_daysMetaInfos = i;
94     }
95 
96 public Q_SLOTS:
97     /**
98      * saves all documents that has at least one view.
99      * documents with no views are ignored :P
100      */
101     void saveAll();
102 
103     /**
104      * reloads all documents that has at least one view.
105      * documents with no views are ignored :P
106      */
107     void reloadAll();
108 
109     /**
110      * close all documents, which could not be reopened
111      */
112     void closeOrphaned();
113 
114     /**
115      * save selected documents from the File List
116      */
117     void saveSelected(const QList<KTextEditor::Document *> &);
118 
119 Q_SIGNALS:
120     /**
121      * This signal is emitted when the \p document was created.
122      */
123     void documentCreated(KTextEditor::Document *document);
124 
125     /**
126      * This signal is emitted when the \p document was created.
127      * This is emitted after the initial documentCreated for internal use in view manager
128      */
129     void documentCreatedViewManager(KTextEditor::Document *document);
130 
131     /**
132      * This signal is emitted before a \p document which should be closed is deleted
133      * The document is still accessible and usable, but it will be deleted
134      * after this signal was send.
135      *
136      * @param document document that will be deleted
137      */
138     void documentWillBeDeleted(KTextEditor::Document *document);
139 
140     /**
141      * This signal is emitted when the \p document has been deleted.
142      *
143      *  Warning !!! DO NOT ACCESS THE DATA REFERENCED BY THE POINTER, IT IS ALREADY INVALID
144      *  Use the pointer only to remove mappings in hash or maps
145      */
146     void documentDeleted(KTextEditor::Document *document);
147 
148     /**
149      * This signal is emitted before the documents batch is going to be deleted
150      *
151      * note that the batch can be interrupted in the middle and only some
152      * of the documents may be actually deleted. See documentsDeleted() signal.
153      */
154     void aboutToDeleteDocuments(const QList<KTextEditor::Document *> &);
155 
156     /**
157      * This signal is emitted after the documents batch was deleted
158      *
159      * This is the batch closing signal for aboutToDeleteDocuments
160      * @param documents the documents that weren't deleted after all
161      */
162     void documentsDeleted(const QList<KTextEditor::Document *> &documents);
163 
164 private Q_SLOTS:
165     void slotModifiedOnDisc(KTextEditor::Document *doc, bool b, KTextEditor::ModificationInterface::ModifiedOnDiskReason reason);
166     void slotModChanged(KTextEditor::Document *doc);
167     void slotModChanged1(KTextEditor::Document *doc);
168 
169 private:
170     bool loadMetaInfos(KTextEditor::Document *doc, const QUrl &url);
171     void saveMetaInfos(const QList<KTextEditor::Document *> &docs);
172 
173     QList<KTextEditor::Document *> m_docList;
174     std::unordered_map<KTextEditor::Document *, KateDocumentInfo> m_docInfos;
175 
176     KConfig m_metaInfos;
177     bool m_saveMetaInfos;
178     int m_daysMetaInfos;
179 
180     typedef std::pair<QUrl, QDateTime> TPair;
181     std::unordered_map<KTextEditor::Document *, TPair> m_tempFiles;
182 
183 private Q_SLOTS:
184     void documentOpened();
185 };
186 
187 #endif
188