File indexing completed on 2024-04-28 11:45:25

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 }
0025 
0026 namespace Kate
0027 {
0028 /**
0029  * Class for tracking editing actions.
0030  * In case Kate crashes, this can be used to replay all edit actions to
0031  * recover the lost data.
0032  */
0033 class SwapFile : public QObject
0034 {
0035     Q_OBJECT
0036 
0037 public:
0038     explicit SwapFile(KTextEditor::DocumentPrivate *document);
0039     ~SwapFile() override;
0040     bool shouldRecover() const;
0041 
0042     void fileClosed();
0043     QString fileName();
0044 
0045     KTextEditor::DocumentPrivate *document();
0046 
0047 private:
0048     void setTrackingEnabled(bool trackingEnabled);
0049     void removeSwapFile();
0050     bool updateFileName();
0051     bool isValidSwapFile(QDataStream &stream, bool checkDigest) const;
0052 
0053 private:
0054     KTextEditor::DocumentPrivate *m_document;
0055     bool m_trackingEnabled;
0056 
0057 protected Q_SLOTS:
0058     void fileSaved(const QString &filename);
0059     void fileLoaded(const QString &filename);
0060     void modifiedChanged();
0061 
0062     void startEditing();
0063     void finishEditing();
0064 
0065     void wrapLine(const KTextEditor::Cursor position);
0066     void unwrapLine(int line);
0067     void insertText(const KTextEditor::Cursor position, const QString &text);
0068     void removeText(KTextEditor::Range range);
0069 
0070 public Q_SLOTS:
0071     void discard();
0072     void recover();
0073     bool recover(QDataStream &, bool checkDigest = true);
0074     void configChanged();
0075 
0076 private:
0077     QDataStream m_stream;
0078     QFile m_swapfile;
0079     bool m_recovered;
0080     bool m_needSync;
0081     static QTimer *s_timer;
0082 
0083 protected Q_SLOTS:
0084     void writeFileToDisk();
0085 
0086 private:
0087     static QTimer *syncTimer();
0088 
0089 public Q_SLOTS:
0090     void showSwapFileMessage();
0091     void showDiff();
0092 
0093 private:
0094     QPointer<KTextEditor::Message> m_swapMessage;
0095 };
0096 
0097 }
0098 
0099 #endif // KATE_SWAPFILE_H