basic saving (no data is written to file yet)

This commit is contained in:
Marc Zinnschlag 2013-09-15 15:00:41 +02:00
parent bcd36bd378
commit fa25a068a8
5 changed files with 149 additions and 5 deletions

View file

@ -8,7 +8,10 @@
CSMDoc::Saving::Saving (Document& document) CSMDoc::Saving::Saving (Document& document)
: Operation (State_Saving, true, true), mDocument (document), mState (*this) : Operation (State_Saving, true, true), mDocument (document), mState (*this)
{ {
appendStage (new OpenSaveStage (mDocument, mState));
appendStage (new CloseSaveStage (mState));
appendStage (new FinalSavingStage (mDocument, mState)); appendStage (new FinalSavingStage (mDocument, mState));
} }

View file

@ -1,11 +1,53 @@
#include "savingstages.hpp" #include "savingstages.hpp"
#include <fstream>
#include <boost/filesystem.hpp>
#include <QUndoStack> #include <QUndoStack>
#include "document.hpp" #include "document.hpp"
#include "savingstate.hpp" #include "savingstate.hpp"
CSMDoc::OpenSaveStage::OpenSaveStage (Document& document, SavingState& state)
: mDocument (document), mState (state)
{}
int CSMDoc::OpenSaveStage::setup()
{
return 1;
}
void CSMDoc::OpenSaveStage::perform (int stage, std::vector<std::string>& messages)
{
mState.start (mDocument);
mState.getStream().open (mState.getTmpPath().string().c_str());
if (!mState.getStream().is_open())
throw std::runtime_error ("failed to open stream for saving");
}
CSMDoc::CloseSaveStage::CloseSaveStage (SavingState& state)
: mState (state)
{}
int CSMDoc::CloseSaveStage::setup()
{
return 1;
}
void CSMDoc::CloseSaveStage::perform (int stage, std::vector<std::string>& messages)
{
mState.getStream().close();
if (!mState.getStream())
throw std::runtime_error ("saving failed");
}
CSMDoc::FinalSavingStage::FinalSavingStage (Document& document, SavingState& state) CSMDoc::FinalSavingStage::FinalSavingStage (Document& document, SavingState& state)
: mDocument (document), mState (state) : mDocument (document), mState (state)
{} {}
@ -19,12 +61,17 @@ void CSMDoc::FinalSavingStage::perform (int stage, std::vector<std::string>& mes
{ {
if (mState.hasError()) if (mState.hasError())
{ {
/// \todo close stream mState.getWriter().close();
/// \todo delete tmp file mState.getStream().close();
if (boost::filesystem::exists (mState.getTmpPath()))
boost::filesystem::remove (mState.getTmpPath());
} }
else else
{ {
/// \todo delete file, rename tmp file boost::filesystem::remove (mState.getPath());
boost::filesystem::rename (mState.getTmpPath(), mState.getPath());
mDocument.getUndoStack().setClean(); mDocument.getUndoStack().setClean();
} }
} }

View file

@ -8,6 +8,37 @@ namespace CSMDoc
class Document; class Document;
class SavingState; class SavingState;
class OpenSaveStage : public Stage
{
Document& mDocument;
SavingState& mState;
public:
OpenSaveStage (Document& document, SavingState& state);
virtual int setup();
///< \return number of steps
virtual void perform (int stage, std::vector<std::string>& messages);
///< Messages resulting from this stage will be appended to \a messages.
};
class CloseSaveStage : public Stage
{
SavingState& mState;
public:
CloseSaveStage (SavingState& state);
virtual int setup();
///< \return number of steps
virtual void perform (int stage, std::vector<std::string>& messages);
///< Messages resulting from this stage will be appended to \a messages.
};
class FinalSavingStage : public Stage class FinalSavingStage : public Stage
{ {
Document& mDocument; Document& mDocument;

View file

@ -2,12 +2,53 @@
#include "savingstate.hpp" #include "savingstate.hpp"
#include "operation.hpp" #include "operation.hpp"
#include "document.hpp"
CSMDoc::SavingState::SavingState (Operation& operation) CSMDoc::SavingState::SavingState (Operation& operation)
: mOperation (operation) : mOperation (operation),
{} /// \todo set encoding properly, once config implementation has been fixed.
mEncoder (ToUTF8::calculateEncoding ("win1252"))
{
mWriter.setEncoder (&mEncoder);
}
bool CSMDoc::SavingState::hasError() const bool CSMDoc::SavingState::hasError() const
{ {
return mOperation.hasError(); return mOperation.hasError();
} }
void CSMDoc::SavingState::start (Document& document)
{
if (mStream.is_open())
mStream.close();
mStream.clear();
mPath = document.getSavePath();
boost::filesystem::path file (mPath.filename().string() + ".tmp");
mTmpPath = mPath.parent_path();
mTmpPath /= file;
}
const boost::filesystem::path& CSMDoc::SavingState::getPath() const
{
return mPath;
}
const boost::filesystem::path& CSMDoc::SavingState::getTmpPath() const
{
return mTmpPath;
}
std::ofstream& CSMDoc::SavingState::getStream()
{
return mStream;
}
ESM::ESMWriter& CSMDoc::SavingState::getWriter()
{
return mWriter;
}

View file

@ -1,19 +1,41 @@
#ifndef CSM_DOC_SAVINGSTATE_H #ifndef CSM_DOC_SAVINGSTATE_H
#define CSM_DOC_SAVINGSTATE_H #define CSM_DOC_SAVINGSTATE_H
#include <fstream>
#include <boost/filesystem/path.hpp>
#include <components/esm/esmwriter.hpp>
namespace CSMDoc namespace CSMDoc
{ {
class Operation; class Operation;
class Document;
class SavingState class SavingState
{ {
Operation& mOperation; Operation& mOperation;
boost::filesystem::path mPath;
boost::filesystem::path mTmpPath;
ToUTF8::Utf8Encoder mEncoder;
std::ofstream mStream;
ESM::ESMWriter mWriter;
public: public:
SavingState (Operation& operation); SavingState (Operation& operation);
bool hasError() const; bool hasError() const;
void start (Document& document);
const boost::filesystem::path& getPath() const;
const boost::filesystem::path& getTmpPath() const;
std::ofstream& getStream();
ESM::ESMWriter& getWriter();
}; };