1/*
2 SPDX-FileCopyrightText: 2010-2018 Dominik Haumann <dhaumann@kde.org>
3 SPDX-FileCopyrightText: 2010 Diana-Victoria Tiriplica <diana.tiriplica@gmail.com>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#ifndef KATE_SWAPFILE_H
9#define KATE_SWAPFILE_H
10
11#include <QDataStream>
12#include <QFile>
13#include <QObject>
14#include <QPointer>
15
16class QTimer;
17namespace KTextEditor
18{
19class ViewPrivate;
20class DocumentPrivate;
21class Message;
22class Range;
23class Cursor;
24class Document;
25}
26
27namespace Kate
28{
29/**
30 * Class for tracking editing actions.
31 * In case Kate crashes, this can be used to replay all edit actions to
32 * recover the lost data.
33 */
34class SwapFile : public QObject
35{
36public:
37 explicit SwapFile(KTextEditor::DocumentPrivate *document);
38 ~SwapFile() override;
39 bool shouldRecover() const;
40
41 void fileClosed();
42 QString fileName();
43
44 KTextEditor::DocumentPrivate *document();
45
46private:
47 void setTrackingEnabled(bool trackingEnabled);
48 void removeSwapFile();
49 bool updateFileName();
50 bool isValidSwapFile(QDataStream &stream, bool checkDigest) const;
51
52private:
53 KTextEditor::DocumentPrivate *m_document;
54 bool m_trackingEnabled;
55
56protected:
57 void fileSaved(const QString &filename);
58 void fileLoaded(const QString &filename);
59 void modifiedChanged();
60
61 void startEditing();
62 void finishEditing();
63
64 void wrapLine(KTextEditor::Document *, const KTextEditor::Cursor position);
65 void unwrapLine(KTextEditor::Document *, int line);
66 void insertText(KTextEditor::Document *, const KTextEditor::Cursor position, const QString &text);
67 void removeText(KTextEditor::Document *, KTextEditor::Range range, const QString &);
68
69public:
70 void discard();
71 void recover();
72 bool recover(QDataStream &, bool checkDigest = true);
73 void configChanged();
74
75private:
76 QDataStream m_stream;
77 QFile m_swapfile;
78 bool m_recovered;
79 bool m_needSync;
80 static QTimer *s_timer;
81
82protected:
83 void writeFileToDisk();
84
85private:
86 static QTimer *syncTimer();
87
88public:
89 void showSwapFileMessage();
90 void showDiff();
91
92private:
93 QPointer<KTextEditor::Message> m_swapMessage;
94};
95}
96
97#endif // KATE_SWAPFILE_H
98

source code of ktexteditor/src/swapfile/kateswapfile.h