| 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 | |
| 16 | class QTimer; |
| 17 | namespace KTextEditor |
| 18 | { |
| 19 | class ViewPrivate; |
| 20 | class DocumentPrivate; |
| 21 | class Message; |
| 22 | class Range; |
| 23 | class Cursor; |
| 24 | class Document; |
| 25 | } |
| 26 | |
| 27 | namespace 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 | */ |
| 34 | class SwapFile : public QObject |
| 35 | { |
| 36 | public: |
| 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 | |
| 46 | private: |
| 47 | void setTrackingEnabled(bool trackingEnabled); |
| 48 | void removeSwapFile(); |
| 49 | bool updateFileName(); |
| 50 | bool isValidSwapFile(QDataStream &stream, bool checkDigest) const; |
| 51 | |
| 52 | private: |
| 53 | KTextEditor::DocumentPrivate *m_document; |
| 54 | bool m_trackingEnabled; |
| 55 | |
| 56 | protected: |
| 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 | |
| 69 | public: |
| 70 | void discard(); |
| 71 | void recover(); |
| 72 | bool recover(QDataStream &, bool checkDigest = true); |
| 73 | void configChanged(); |
| 74 | |
| 75 | private: |
| 76 | QDataStream m_stream; |
| 77 | QFile m_swapfile; |
| 78 | bool m_recovered; |
| 79 | bool m_needSync; |
| 80 | static QTimer *s_timer; |
| 81 | |
| 82 | protected: |
| 83 | void writeFileToDisk(); |
| 84 | |
| 85 | private: |
| 86 | static QTimer *syncTimer(); |
| 87 | |
| 88 | public: |
| 89 | void showSwapFileMessage(); |
| 90 | void showDiff(); |
| 91 | |
| 92 | private: |
| 93 | QPointer<KTextEditor::Message> m_swapMessage; |
| 94 | }; |
| 95 | } |
| 96 | |
| 97 | #endif // KATE_SWAPFILE_H |
| 98 | |