File size: 15,208 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 | /***************************************************************************
* Copyright (c) 2015 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include <QApplication>
#include <QFile>
#include <QDir>
#include <QRunnable>
#include <QTextStream>
#include <QThreadPool>
#include <App/Application.h>
#include <App/Document.h>
#include <App/DocumentObject.h>
#include <Base/Console.h>
#include <Base/FileInfo.h>
#include <Base/Stream.h>
#include <Base/TimeInfo.h>
#include <Base/Tools.h>
#include <Base/Writer.h>
#include "AutoSaver.h"
#include "Document.h"
#include "MainWindow.h"
#include "ViewProvider.h"
#include "WaitCursor.h"
FC_LOG_LEVEL_INIT("App", true, true)
using namespace Gui;
namespace sp = std::placeholders;
AutoSaver* AutoSaver::self = nullptr;
const int AutoSaveTimeout = 900000;
AutoSaver::AutoSaver(QObject* parent)
: QObject(parent)
, timeout(AutoSaveTimeout)
, compressed(true)
{
// NOLINTBEGIN
App::GetApplication().signalNewDocument.connect(
std::bind(&AutoSaver::slotCreateDocument, this, sp::_1)
);
App::GetApplication().signalDeleteDocument.connect(
std::bind(&AutoSaver::slotDeleteDocument, this, sp::_1)
);
// NOLINTEND
}
AutoSaver::~AutoSaver() = default;
AutoSaver* AutoSaver::instance()
{
if (!self) {
self = new AutoSaver(QApplication::instance());
}
return self;
}
void AutoSaver::renameFile(QString dirName, QString file, QString tmpFile)
{
FC_LOG("auto saver rename " << tmpFile.toUtf8().constData() << " -> " << file.toUtf8().constData());
QDir dir(dirName);
dir.remove(file);
if (!dir.rename(tmpFile, file)) {
FC_ERR(
"Failed to rename autosave file " << tmpFile.toStdString() << " to "
<< file.toStdString() << "\n"
);
}
}
void AutoSaver::setTimeout(int ms)
{
timeout = Base::clamp<int>(ms, 0, 3600000); // between 0 and 60 min
// go through the attached documents and apply the new timeout
for (auto& it : saverMap) {
if (it.second->timerId > 0) {
killTimer(it.second->timerId);
}
int id = timeout > 0 ? startTimer(timeout) : 0;
it.second->timerId = id;
}
}
void AutoSaver::setCompressed(bool on)
{
this->compressed = on;
}
void AutoSaver::slotCreateDocument(const App::Document& Doc)
{
std::string name = Doc.getName();
int id = timeout > 0 ? startTimer(timeout) : 0;
AutoSaveProperty* as = new AutoSaveProperty(&Doc);
as->timerId = id;
if (!this->compressed) {
std::string dirName = Doc.TransientDir.getValue();
dirName += "/fc_recovery_files";
Base::FileInfo fi(dirName);
fi.createDirectory();
as->dirName = dirName;
}
saverMap.insert(std::make_pair(name, as));
}
void AutoSaver::slotDeleteDocument(const App::Document& Doc)
{
std::string name = Doc.getName();
std::map<std::string, AutoSaveProperty*>::iterator it = saverMap.find(name);
if (it != saverMap.end()) {
if (it->second->timerId > 0) {
killTimer(it->second->timerId);
}
delete it->second;
saverMap.erase(it);
}
}
void AutoSaver::saveDocument(const std::string& name, AutoSaveProperty& saver)
{
Gui::WaitCursor wc;
App::Document* doc = App::GetApplication().getDocument(name.c_str());
if (doc && !doc->testStatus(App::Document::PartialDoc)
&& !doc->testStatus(App::Document::TempDoc)) {
// Set the document's current transient directory
std::string dirName = doc->TransientDir.getValue();
dirName += "/fc_recovery_files";
saver.dirName = dirName;
// Write recovery meta file
QFile file(QStringLiteral("%1/fc_recovery_file.xml")
.arg(QString::fromUtf8(doc->TransientDir.getValue())));
if (file.open(QFile::WriteOnly)) {
QTextStream str(&file);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
str.setCodec("UTF-8");
#endif
str << "<?xml version='1.0' encoding='utf-8'?>\n"
<< "<AutoRecovery SchemaVersion=\"1\">\n";
str << " <Status>Created</Status>\n";
str << " <Label>" << QString::fromUtf8(doc->Label.getValue())
<< "</Label>\n"; // store the document's current label
str << " <FileName>" << QString::fromUtf8(doc->FileName.getValue())
<< "</FileName>\n"; // store the document's current filename
str << "</AutoRecovery>\n";
file.close();
}
// make sure to tmp. disable saving thumbnails because this causes trouble if the
// associated 3d view is not active
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetParameterGroupByPath(
"User parameter:BaseApp/Preferences/Document"
);
bool save = hGrp->GetBool("SaveThumbnail", true);
hGrp->SetBool("SaveThumbnail", false);
getMainWindow()->showMessage(tr("Wait until the auto-recovery file has been saved…"), 5000);
// qApp->processEvents();
Base::TimeElapsed startTime;
// open extra scope to close ZipWriter properly
{
if (!this->compressed) {
RecoveryWriter writer(saver);
// We will be using thread pool if not compressed.
// So, always force binary format because ASCII
// is not reentrant. See PropertyPartShape::SaveDocFile
writer.setMode("BinaryBrep");
writer.putNextEntry("Document.xml");
doc->Save(writer);
// Special handling for Gui document.
doc->signalSaveDocument(writer);
// write additional files
writer.writeFiles();
}
// only create the file if something has changed
else if (!saver.touched.empty()) {
std::string fn = doc->TransientDir.getValue();
fn += "/fc_recovery_file.fcstd";
Base::FileInfo tmp(fn);
Base::ofstream file(tmp, std::ios::out | std::ios::binary);
if (file.is_open()) {
Base::ZipWriter writer(file);
if (hGrp->GetBool("SaveBinaryBrep", true)) {
writer.setMode("BinaryBrep");
}
writer.setComment("AutoRecovery file");
writer.setLevel(1); // apparently the fastest compression
writer.putNextEntry("Document.xml");
doc->Save(writer);
// Special handling for Gui document.
doc->signalSaveDocument(writer);
// write additional files
writer.writeFiles();
}
}
}
Base::Console().log(
"Save auto-recovery file in %fs\n",
Base::TimeElapsed::diffTimeF(startTime, Base::TimeElapsed())
);
hGrp->SetBool("SaveThumbnail", save);
}
}
void AutoSaver::timerEvent(QTimerEvent* event)
{
int id = event->timerId();
for (auto& it : saverMap) {
if (it.second->timerId == id) {
try {
saveDocument(it.first, *it.second);
it.second->touched.clear();
break;
}
catch (...) {
Base::Console().error("Failed to auto-save document '%s'\n", it.first.c_str());
}
}
}
}
// ----------------------------------------------------------------------------
AutoSaveProperty::AutoSaveProperty(const App::Document* doc)
: timerId(-1)
{
// NOLINTBEGIN
documentNew = const_cast<App::Document*>(doc)->signalNewObject.connect(
std::bind(&AutoSaveProperty::slotNewObject, this, sp::_1)
);
documentMod = const_cast<App::Document*>(doc)->signalChangedObject.connect(
std::bind(&AutoSaveProperty::slotChangePropertyData, this, sp::_2)
);
// NOLINTEND
}
AutoSaveProperty::~AutoSaveProperty()
{
documentNew.disconnect();
documentMod.disconnect();
}
void AutoSaveProperty::slotNewObject(const App::DocumentObject& obj)
{
std::vector<App::Property*> props;
obj.getPropertyList(props);
// if an object was deleted and then restored by an undo then add all properties
// because this might be the data files which we may want to re-write
for (const auto& prop : props) {
slotChangePropertyData(*prop);
}
}
void AutoSaveProperty::slotChangePropertyData(const App::Property& prop)
{
std::stringstream str;
str << static_cast<const void*>(&prop) << std::ends;
std::string address = str.str();
this->touched.insert(address);
}
// ----------------------------------------------------------------------------
RecoveryWriter::RecoveryWriter(AutoSaveProperty& saver)
: Base::FileWriter(saver.dirName.c_str())
, saver(saver)
{}
RecoveryWriter::~RecoveryWriter() = default;
bool RecoveryWriter::shouldWrite(const std::string& name, const Base::Persistence* object) const
{
// Property files of a view provider can always be written because
// these are rather small files.
if (object->isDerivedFrom<App::Property>()) {
const auto* prop = static_cast<const App::Property*>(object);
const App::PropertyContainer* parent = prop->getContainer();
if (parent && parent->isDerivedFrom<Gui::ViewProvider>()) {
return true;
}
}
else if (object->isDerivedFrom<Gui::Document>()) {
return true;
}
// These are the addresses of touched properties of a document object.
std::stringstream str;
str << static_cast<const void*>(object) << std::ends;
std::string address = str.str();
// Check if the property will be exported to the same file. If the file has changed or if the
// property hasn't been yet exported then (re-)write the file.
std::map<std::string, std::string>::iterator it = saver.fileMap.find(address);
if (it == saver.fileMap.end() || it->second != name) {
saver.fileMap[address] = name;
return true;
}
std::set<std::string>::const_iterator jt = saver.touched.find(address);
return (jt != saver.touched.end());
}
namespace Gui
{
class RecoveryRunnable: public QRunnable
{
public:
RecoveryRunnable(
const std::set<std::string>& modes,
const char* dir,
const char* file,
const App::Property* p
)
: prop(p->Copy())
, writer(dir)
{
writer.setModes(modes);
dirName = QString::fromUtf8(dir);
fileName = QString::fromUtf8(file);
tmpName = QStringLiteral("%1.tmp%2").arg(fileName).arg(rand());
writer.putNextEntry(tmpName.toUtf8().constData());
}
~RecoveryRunnable() override
{
delete prop;
}
void run() override
{
try {
prop->SaveDocFile(writer);
writer.close();
// We could have renamed the file in this thread. However, there is
// still chance of crash when we deleted the original and before rename
// the new file. So we ask the main thread to do it. There is still
// possibility of crash caused by thread other than the main, but
// that's the best we can do for now.
QMetaObject::invokeMethod(
AutoSaver::instance(),
"renameFile",
Qt::QueuedConnection,
Q_ARG(QString, dirName),
Q_ARG(QString, fileName),
Q_ARG(QString, tmpName)
);
}
catch (const Base::Exception& e) {
Base::Console().warning("Exception in auto-saving: %s\n", e.what());
}
catch (const std::exception& e) {
Base::Console().warning("C++ exception in auto-saving: %s\n", e.what());
}
catch (...) {
Base::Console().warning("Unknown exception in auto-saving\n");
}
}
private:
App::Property* prop;
Base::FileWriter writer;
QString dirName;
QString fileName;
QString tmpName;
};
} // namespace Gui
void RecoveryWriter::writeFiles()
{
// use a while loop because it is possible that while
// processing the files new ones can be added
size_t index = 0;
this->FileStream.close();
while (index < FileList.size()) {
FileEntry entry = FileList.begin()[index];
if (shouldWrite(entry.FileName, entry.Object)) {
std::string filePath = entry.FileName;
std::string::size_type pos = 0;
while ((pos = filePath.find('/', pos)) != std::string::npos) {
std::string dirName = DirName + "/" + filePath.substr(0, pos);
pos++;
Base::FileInfo fi(dirName);
fi.createDirectory();
}
// For properties a copy can be created and then this can be written to disk in a thread
if (entry.Object->isDerivedFrom<App::Property>()) {
const auto* prop = static_cast<const App::Property*>(entry.Object);
QThreadPool::globalInstance()->start(
new RecoveryRunnable(getModes(), DirName.c_str(), entry.FileName.c_str(), prop)
);
}
else {
std::string fileName = DirName + "/" + entry.FileName;
this->FileStream.open(fileName.c_str(), std::ios::out | std::ios::binary);
entry.Object->SaveDocFile(*this);
this->FileStream.close();
}
}
index++;
}
}
#include "moc_AutoSaver.cpp"
|