| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2015 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
#ifndef GUI_AUTOSAVER_H |
| 25 |
#define GUI_AUTOSAVER_H |
| 26 |
|
| 27 |
#include <QObject> |
| 28 |
|
| 29 |
#include <map> |
| 30 |
#include <set> |
| 31 |
#include <string> |
| 32 |
#include <boost/signals2.hpp> |
| 33 |
#include <Base/Writer.h> |
| 34 |
|
| 35 |
namespace App |
| 36 |
{ |
| 37 |
class Document; |
| 38 |
class DocumentObject; |
| 39 |
class Property; |
| 40 |
} // namespace App |
| 41 |
|
| 42 |
namespace Gui |
| 43 |
{ |
| 44 |
class ViewProvider; |
| 45 |
|
| 46 |
class AutoSaveProperty |
| 47 |
{ |
| 48 |
public: |
| 49 |
AutoSaveProperty(const App::Document* doc); |
| 50 |
~AutoSaveProperty(); |
| 51 |
int timerId; |
| 52 |
std::set<std::string> touched; |
| 53 |
std::string dirName; |
| 54 |
std::map<std::string, std::string> fileMap; |
| 55 |
|
| 56 |
private: |
| 57 |
void slotNewObject(const App::DocumentObject&); |
| 58 |
void slotChangePropertyData(const App::Property&); |
| 59 |
using Connection = boost::signals2::connection; |
| 60 |
Connection documentNew; |
| 61 |
Connection documentMod; |
| 62 |
}; |
| 63 |
|
| 64 |
/*! |
| 65 |
The class AutoSaver is used to automatically save a document to a temporary file. |
| 66 |
@author Werner Mayer |
| 67 |
*/ |
| 68 |
class AutoSaver: public QObject |
| 69 |
{ |
| 70 |
Q_OBJECT |
| 71 |
|
| 72 |
private: |
| 73 |
static AutoSaver* self; |
| 74 |
AutoSaver(QObject* parent); |
| 75 |
~AutoSaver() override; |
| 76 |
|
| 77 |
public: |
| 78 |
static AutoSaver* instance(); |
| 79 |
/*! |
| 80 |
Sets the timeout in milliseconds. A value of 0 means that no timer is used. |
| 81 |
*/ |
| 82 |
void setTimeout(int ms); |
| 83 |
/*! |
| 84 |
Enables or disables to create compreesed recovery files. |
| 85 |
*/ |
| 86 |
void setCompressed(bool on); |
| 87 |
|
| 88 |
protected: |
| 89 |
void slotCreateDocument(const App::Document& Doc); |
| 90 |
void slotDeleteDocument(const App::Document& Doc); |
| 91 |
void timerEvent(QTimerEvent* event) override; |
| 92 |
void saveDocument(const std::string&, AutoSaveProperty&); |
| 93 |
|
| 94 |
public Q_SLOTS: |
| 95 |
void renameFile(QString dirName, QString file, QString tmpFile); |
| 96 |
|
| 97 |
private: |
| 98 |
int timeout; /*!< Timeout in milliseconds */ |
| 99 |
bool compressed; |
| 100 |
std::map<std::string, AutoSaveProperty*> saverMap; |
| 101 |
}; |
| 102 |
|
| 103 |
class RecoveryWriter: public Base::FileWriter |
| 104 |
{ |
| 105 |
public: |
| 106 |
RecoveryWriter(AutoSaveProperty&); |
| 107 |
~RecoveryWriter() override; |
| 108 |
|
| 109 |
/*! |
| 110 |
This method can be re-implemented in sub-classes to avoid |
| 111 |
to write out certain objects. The default implementation |
| 112 |
always returns true. |
| 113 |
*/ |
| 114 |
bool shouldWrite(const std::string&, const Base::Persistence*) const override; |
| 115 |
void writeFiles() override; |
| 116 |
|
| 117 |
private: |
| 118 |
AutoSaveProperty& saver; |
| 119 |
}; |
| 120 |
|
| 121 |
} // namespace Gui |
| 122 |
|
| 123 |
|
| 124 |
#endif // GUI_AUTOSAVER_H |