Skip to content

Commit

Permalink
#5440: Add FileOverwriteConfirmation message. An unhandled message wi…
Browse files Browse the repository at this point in the history
…ll cancel the save process.
  • Loading branch information
codereader committed Dec 19, 2020
1 parent 04ae576 commit e836a29
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/imessagebus.h
Expand Up @@ -49,6 +49,7 @@ class IMessage
Notification,
TextureChanged,
ApplicationIsActiveQuery,
FileOverwriteConfirmation,

UserDefinedMessagesGoHigherThanThis = 999,
};
Expand Down
70 changes: 70 additions & 0 deletions libs/messages/FileOverwriteConfirmation.h
@@ -0,0 +1,70 @@
#pragma once

#include <stdexcept>
#include "imessagebus.h"
#include "iradiant.h"

namespace radiant
{

/**
* Message sent when the backend code wants the user to
* confirm overwriting a file.
* Senders: If this message stays unresponded no overwrite should occur.
*/
class FileOverwriteConfirmation :
public radiant::IMessage
{
private:
std::string _title;
std::string _message;

bool _confirmed;

public:
FileOverwriteConfirmation(const std::string& title, const std::string& message) :
_title(title),
_message(message)
{}

const std::string& getTitle() const
{
return _title;
}

bool hasTitle() const
{
return !_title.empty();
}

const std::string& getMessage() const
{
return _message;
}

std::size_t getId() const override
{
return IMessage::Type::FileOverwriteConfirmation;
}

void confirmOverwrite(bool confirmed = true)
{
_confirmed = confirmed;
}

bool overwriteConfirmed()
{
return isHandled() && _confirmed;
}

// Convenience method, creating an instance and dispatching it to the message bus, returning the answer
static bool SendAndReceiveAnswer(const std::string& message, const std::string& title = std::string())
{
FileOverwriteConfirmation msg(title, message);
GlobalRadiantCore().getMessageBus().sendMessage(msg);

return msg.overwriteConfirmed();
}
};

}
6 changes: 4 additions & 2 deletions radiantcore/map/Map.cpp
Expand Up @@ -43,6 +43,7 @@
#include "model/export/ModelScalePreserver.h"
#include "map/algorithm/Skins.h"
#include "messages/ScopedLongRunningOperation.h"
#include "messages/FileOverwriteConfirmation.h"
#include "selection/algorithm/Primitives.h"
#include "selection/algorithm/Group.h"
#include "scene/Group.h"
Expand Down Expand Up @@ -346,9 +347,10 @@ bool Map::save(const MapFormatPtr& mapFormat)
}

// Check if the map file has been modified in the meantime
if (_resource->fileHasBeenModifiedSinceLastSave())
if (_resource->fileHasBeenModifiedSinceLastSave() && !radiant::FileOverwriteConfirmation::SendAndReceiveAnswer(
fmt::format(_("The file {0} has been modified since it was last saved,\nperhaps by another application. "
"Do you really want to overwrite the file?"), _mapName), _("File modification detected")))
{
radiant::NotificationMessage::SendError("File has been modified in the meantime");
return false;
}

Expand Down
1 change: 1 addition & 0 deletions tools/msvc/libs.vcxproj
Expand Up @@ -163,6 +163,7 @@
<ClInclude Include="..\..\libs\messages\ApplicationShutdownRequest.h" />
<ClInclude Include="..\..\libs\messages\AutomaticMapSaveRequest.h" />
<ClInclude Include="..\..\libs\messages\CommandExecutionFailed.h" />
<ClInclude Include="..\..\libs\messages\FileOverwriteConfirmation.h" />
<ClInclude Include="..\..\libs\messages\FileSelectionRequest.h" />
<ClInclude Include="..\..\libs\messages\GameConfigNeededMessage.h" />
<ClInclude Include="..\..\libs\messages\LongRunningOperationMessage.h" />
Expand Down
3 changes: 3 additions & 0 deletions tools/msvc/libs.vcxproj.filters
Expand Up @@ -274,6 +274,9 @@
<ClInclude Include="..\..\libs\patch\PatchIterators.h">
<Filter>patch</Filter>
</ClInclude>
<ClInclude Include="..\..\libs\messages\FileOverwriteConfirmation.h">
<Filter>messages</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="util">
Expand Down

0 comments on commit e836a29

Please sign in to comment.