1/* This file is part of the KDE project
2 Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
3 Copyright (C) 2002 Joseph Wenninger <jowenn@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#ifndef __KATE_DOCMANAGER_H__
21#define __KATE_DOCMANAGER_H__
22
23#include "katemain.h"
24#include <kate/documentmanager.h>
25
26#include <KTextEditor/Document>
27#include <KTextEditor/Editor>
28#include <KTextEditor/ModificationInterface>
29
30#include <QList>
31#include <QObject>
32#include <QByteArray>
33#include <QHash>
34#include <QMap>
35#include <QPair>
36#include <QDateTime>
37
38namespace KParts
39{
40 class Factory;
41}
42
43class KConfig;
44class KateMainWindow;
45
46class KateDocumentInfo
47{
48 public:
49 enum CustomRoles {RestoreOpeningFailedRole };
50
51 public:
52 KateDocumentInfo ()
53 : modifiedOnDisc (false)
54 , modifiedOnDiscReason (KTextEditor::ModificationInterface::OnDiskUnmodified)
55 , openedByUser(false)
56 , openSuccess(true)
57 {}
58
59 bool modifiedOnDisc;
60 KTextEditor::ModificationInterface::ModifiedOnDiskReason modifiedOnDiscReason;
61
62 bool openedByUser;
63 bool openSuccess;
64};
65
66class KateDocManager : public QObject
67{
68 Q_OBJECT
69
70 public:
71 KateDocManager (QObject *parent);
72 ~KateDocManager ();
73
74 /**
75 * should the document manager suppress all opening error dialogs on openUrl?
76 + @param suppress suppress dialogs?
77 */
78 void setSuppressOpeningErrorDialogs (bool suppress);
79
80 static KateDocManager *self ();
81
82 Kate::DocumentManager *documentManager ()
83 {
84 return m_documentManager;
85 }
86 KTextEditor::Editor *editor()
87 {
88 return m_editor;
89 }
90
91 KTextEditor::Document *createDoc (const KateDocumentInfo& docInfo = KateDocumentInfo());
92
93 void deleteDoc (KTextEditor::Document *doc);
94
95 KTextEditor::Document *document (uint n);
96
97 KateDocumentInfo *documentInfo (KTextEditor::Document *doc);
98
99 int findDocument (KTextEditor::Document *doc);
100 /** Returns the documentNumber of the doc with url URL or -1 if no such doc is found */
101 KTextEditor::Document *findDocument (const KUrl &url) const;
102
103 bool isOpen(KUrl url);
104
105 uint documents ();
106
107 const QList<KTextEditor::Document*> &documentList () const
108 {
109 return m_docList;
110 }
111
112 KTextEditor::Document *openUrl(const KUrl&,
113 const QString &encoding = QString(),
114 bool isTempFile = false,
115 const KateDocumentInfo& docInfo = KateDocumentInfo());
116
117 QList<KTextEditor::Document *> openUrls(const QList<KUrl>&,
118 const QString &encoding = QString(),
119 bool isTempFile = false,
120 const KateDocumentInfo& docInfo = KateDocumentInfo());
121
122 bool closeDocument(KTextEditor::Document *, bool closeUrl = true);
123 bool closeDocuments(const QList<KTextEditor::Document *> &documents, bool closeUrl = true);
124 bool closeDocumentList(QList<KTextEditor::Document*> documents);
125 bool closeAllDocuments(bool closeUrl = true);
126 bool closeOtherDocuments(KTextEditor::Document*);
127 bool closeOtherDocuments(uint);
128
129 QList<KTextEditor::Document*> modifiedDocumentList();
130 bool queryCloseDocuments(KateMainWindow *w);
131
132 void saveDocumentList (KConfig *config);
133 void restoreDocumentList (KConfig *config);
134
135 inline bool getSaveMetaInfos()
136 {
137 return m_saveMetaInfos;
138 }
139 inline void setSaveMetaInfos(bool b)
140 {
141 m_saveMetaInfos = b;
142 }
143
144 inline int getDaysMetaInfos()
145 {
146 return m_daysMetaInfos;
147 }
148 inline void setDaysMetaInfos(int i)
149 {
150 m_daysMetaInfos = i;
151 }
152
153 public Q_SLOTS:
154 /**
155 * saves all documents that has at least one view.
156 * documents with no views are ignored :P
157 */
158 void saveAll();
159
160 /**
161 * reloads all documents that has at least one view.
162 * documents with no views are ignored :P
163 */
164 void reloadAll();
165
166 /**
167 * close all documents, which could not be reopened
168 */
169 void closeOrphaned();
170
171 /**
172 * save selected documents from the File List
173 */
174 void saveSelected(const QList<KTextEditor::Document*>&);
175
176 Q_SIGNALS:
177
178 /**
179 * This signal is emitted when the \p document was created.
180 */
181 void documentCreated (KTextEditor::Document *document);
182
183 /**
184 * This signal is emitted when the \p document has been deleted.
185 *
186 * Warning !!! DO NOT ACCESS THE DATA REFERENCED BY THE POINTER, IT IS ALREADY INVALID
187 * Use the pointer only to remove mappings in hash or maps
188 */
189 void documentDeleted (KTextEditor::Document *document);
190
191 void initialDocumentReplaced ();
192
193 private Q_SLOTS:
194 void slotModifiedOnDisc (KTextEditor::Document *doc, bool b, KTextEditor::ModificationInterface::ModifiedOnDiskReason reason);
195 void slotModChanged(KTextEditor::Document *doc);
196 void slotModChanged1(KTextEditor::Document *doc);
197
198 void showRestoreErrors ();
199 private:
200 bool loadMetaInfos(KTextEditor::Document *doc, const KUrl &url);
201 void saveMetaInfos(const QList<KTextEditor::Document *> &docs);
202 bool computeUrlMD5(const KUrl &url, QByteArray &result);
203
204 Kate::DocumentManager *m_documentManager;
205 QList<KTextEditor::Document*> m_docList;
206 QHash<KTextEditor::Document*, KateDocumentInfo*> m_docInfos;
207
208 KConfig *m_metaInfos;
209 bool m_saveMetaInfos;
210 int m_daysMetaInfos;
211
212
213 //KParts::Factory *m_factory;
214 KTextEditor::Editor *m_editor;
215
216 typedef QPair<KUrl, QDateTime> TPair;
217 QMap<KTextEditor::Document *, TPair> m_tempFiles;
218 QString m_openingErrors;
219 int m_documentStillToRestore;
220
221 // suppress error dialogs while opening?
222 bool m_suppressOpeningErrorDialogs;
223
224 private Q_SLOTS:
225 void documentOpened();
226};
227
228#endif
229// kate: space-indent on; indent-width 2; replace-tabs on;
230