File indexing completed on 2024-04-14 03:55:24

0001 /*
0002     SPDX-FileCopyrightText: 2010-2018 Dominik Haumann <dhaumann@kde.org>
0003     SPDX-FileCopyrightText: 2010 Diana-Victoria Tiriplica <diana.tiriplica@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KATE_SWAPFILE_H
0009 #define KATE_SWAPFILE_H
0010 
0011 #include <QDataStream>
0012 #include <QFile>
0013 #include <QObject>
0014 #include <QPointer>
0015 
0016 class QTimer;
0017 namespace KTextEditor
0018 {
0019 class ViewPrivate;
0020 class DocumentPrivate;
0021 class Message;
0022 class Range;
0023 class Cursor;
0024 class Document;
0025 }
0026 
0027 namespace Kate
0028 {
0029 /**
0030  * Class for tracking editing actions.
0031  * In case Kate crashes, this can be used to replay all edit actions to
0032  * recover the lost data.
0033  */
0034 class SwapFile : public QObject
0035 {
0036 public:
0037     explicit SwapFile(KTextEditor::DocumentPrivate *document);
0038     ~SwapFile() override;
0039     bool shouldRecover() const;
0040 
0041     void fileClosed();
0042     QString fileName();
0043 
0044     KTextEditor::DocumentPrivate *document();
0045 
0046 private:
0047     void setTrackingEnabled(bool trackingEnabled);
0048     void removeSwapFile();
0049     bool updateFileName();
0050     bool isValidSwapFile(QDataStream &stream, bool checkDigest) const;
0051 
0052 private:
0053     KTextEditor::DocumentPrivate *m_document;
0054     bool m_trackingEnabled;
0055 
0056 protected:
0057     void fileSaved(const QString &filename);
0058     void fileLoaded(const QString &filename);
0059     void modifiedChanged();
0060 
0061     void startEditing();
0062     void finishEditing();
0063 
0064     void wrapLine(KTextEditor::Document *, const KTextEditor::Cursor position);
0065     void unwrapLine(KTextEditor::Document *, int line);
0066     void insertText(KTextEditor::Document *, const KTextEditor::Cursor position, const QString &text);
0067     void removeText(KTextEditor::Document *, KTextEditor::Range range, const QString &);
0068 
0069 public:
0070     void discard();
0071     void recover();
0072     bool recover(QDataStream &, bool checkDigest = true);
0073     void configChanged();
0074 
0075 private:
0076     QDataStream m_stream;
0077     QFile m_swapfile;
0078     bool m_recovered;
0079     bool m_needSync;
0080     static QTimer *s_timer;
0081 
0082 protected:
0083     void writeFileToDisk();
0084 
0085 private:
0086     static QTimer *syncTimer();
0087 
0088 public:
0089     void showSwapFileMessage();
0090     void showDiff();
0091 
0092 private:
0093     QPointer<KTextEditor::Message> m_swapMessage;
0094 };
0095 }
0096 
0097 #endif // KATE_SWAPFILE_H