From e3045b513c8743caddf93c74619a9813698c9301 Mon Sep 17 00:00:00 2001 From: Ashod Nakashian Date: Sat, 2 Apr 2022 17:21:09 -0400 Subject: wsd: add canSaveToDisk helper Change-Id: I8ad6c91ac05d60e29726a20d67799aa11714226a Signed-off-by: Ashod Nakashian --- wsd/DocumentBroker.cpp | 13 ++++++++----- wsd/DocumentBroker.hpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/wsd/DocumentBroker.cpp b/wsd/DocumentBroker.cpp index 596a66e77c..e77ba63bc6 100644 --- a/wsd/DocumentBroker.cpp +++ b/wsd/DocumentBroker.cpp @@ -1833,16 +1833,18 @@ bool DocumentBroker::autoSave(const bool force, const bool dontSaveIfUnmodified) LOG_TRC("autoSave(): forceful? " << force << ", dontSaveIfUnmodified: " << dontSaveIfUnmodified); - if (_sessions.empty() || !isLoaded() || (!isModified() && !force)) + + const CanSave canSave = canSaveToDisk(); + if (canSave != CanSave::Yes) { - // Nothing to do. - LOG_TRC("Nothing to autosave [" << _docKey << "]."); + LOG_DBG("Cannot save to disk: " << name(canSave)); return false; } - if (_docState.isDisconnected()) + if (!isModified() && !force) { - LOG_DBG("Cannot autosave when disconnected from Kit."); + // Nothing to do. + LOG_TRC("Nothing to autosave [" << _docKey << "]."); return false; } @@ -3515,6 +3517,7 @@ void DocumentBroker::dumpState(std::ostream& os) os << "\n thread start: " << Util::getTimeForLog(now, _threadStart); os << "\n modified?: " << isModified(); os << "\n possibly-modified: " << isPossiblyModified(); + os << "\n canSave: " << name(canSaveToDisk()); os << "\n canUpload: " << name(canUploadToStorage()); os << "\n needToUpload: " << name(needToUploadToStorage()); os << "\n haveActivityAfterSaveRequest: " << haveActivityAfterSaveRequest(); diff --git a/wsd/DocumentBroker.hpp b/wsd/DocumentBroker.hpp index f27dac4d75..544ab2f25c 100644 --- a/wsd/DocumentBroker.hpp +++ b/wsd/DocumentBroker.hpp @@ -514,6 +514,38 @@ private: /// with the child and cleans up ChildProcess etc. void terminateChild(const std::string& closeReason); + /// Encodes whether or not saving is possible + /// (regardless of whether we need to or not). + STATE_ENUM( + CanSave, + Yes, //< Saving is possible. + NoKit, //< There is no Kit. + NotLoaded, //< No document is loaded. + NoWriteSession, //< No available session can write. + ); + + /// Returns the state of whether saving is possible. + /// (regardless of whether we need to or not). + CanSave canSaveToDisk() const + { + if (_docState.isDisconnected() || getPid() <= 0) + { + return CanSave::NoKit; + } + + if (!isLoaded()) + { + return CanSave::NotLoaded; + } + + if (_sessions.empty() || getWriteableSessionId().empty()) + { + return CanSave::NoWriteSession; + } + + return CanSave::Yes; + } + /// Encodes whether or not uploading is possible. /// (regardless of whether we need to or not). STATE_ENUM( -- cgit