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
|
/************************************************************************
*
* Copyright (C) 2021-2025 IRCAD France
*
* This file is part of Sight.
*
* Sight is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sight 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sight. If not, see <https://www.gnu.org/licenses/>.
*
***********************************************************************/
// cspell:ignore NOLINT
#include "module/io/session/writer.hpp"
#include <core/com/signal.hxx>
#include <core/crypto/password_keeper.hpp>
#include <core/crypto/secure_string.hpp>
#include <core/jobs/aggregator.hpp>
#include <core/jobs/job.hpp>
#include <core/location/single_folder.hpp>
#include <core/os/temp_path.hpp>
#include <core/tools/system.hpp>
#include <io/session/session_writer.hpp>
#include <ui/__/cursor.hpp>
#include <ui/__/dialog/input.hpp>
#include <ui/__/dialog/location.hpp>
#include <ui/__/dialog/message.hpp>
namespace sight::module::io::session
{
using core::crypto::password_keeper;
using core::crypto::secure_string;
using sight::io::zip::archive;
/// Private writer implementation
class writer::writer_impl
{
public:
/// Delete default constructors and assignment operators
writer_impl(const writer_impl&) = delete;
writer_impl(writer_impl&&) = delete;
writer_impl& operator=(const writer_impl&) = delete;
writer_impl& operator=(writer_impl&&) = delete;
/// Constructor
explicit writer_impl(writer* const _writer) noexcept :
m_writer(_writer),
m_job_created_signal(_writer->new_signal<job_created_signal_t>("job_created"))
{
}
/// Default destructor
~writer_impl() noexcept = default;
/// Pointer to the public interface
writer* const m_writer;
/// Extension name to use for session file
std::string m_extension_name {".zip"};
/// Extension description to use for file save dialog
std::string m_extension_description {"Sight session"};
/// Dialog policy to use for the file location
dialog_policy m_dialog_policy = {dialog_policy::never};
/// Password policy to use
password_keeper::password_policy m_password_policy {password_keeper::password_policy::never};
/// Encryption policy to use
password_keeper::encryption_policy m_encryption_policy {password_keeper::encryption_policy::password};
/// Archive format to use
archive::archive_format m_archive_format {archive::archive_format::DEFAULT};
/// Signal emitted when job created.
job_created_signal_t::sptr m_job_created_signal;
};
writer::writer() noexcept :
sight::io::service::writer("Choose a file to save a session"),
m_pimpl(std::make_unique<writer_impl>(this))
{
}
// Defining the destructor here, allows us to use PImpl with a unique_ptr
writer::~writer() noexcept = default;
//-----------------------------------------------------------------------------
void writer::starting()
{
}
//-----------------------------------------------------------------------------
void writer::stopping()
{
clear_locations();
}
//-----------------------------------------------------------------------------
void writer::configuring()
{
sight::io::service::writer::configuring();
const auto& tree = this->get_config();
// Dialog configuration
const auto& dialog = tree.get_child_optional("dialog.<xmlattr>");
if(dialog.is_initialized())
{
m_pimpl->m_extension_name = dialog->get<std::string>("extension");
m_pimpl->m_extension_description = dialog->get<std::string>("description");
m_pimpl->m_dialog_policy = string_to_dialog_policy(dialog->get<std::string>("policy", "default"));
SIGHT_THROW_IF(
"Cannot read dialog policy.",
m_pimpl->m_dialog_policy == dialog_policy::invalid
);
}
// Password configuration
const auto& password = tree.get_child_optional("password.<xmlattr>");
if(password.is_initialized())
{
// Password policy
m_pimpl->m_password_policy = password_keeper::string_to_password_policy(
password->get<std::string>("policy", "default")
);
SIGHT_THROW_IF(
"Cannot read password policy.",
m_pimpl->m_password_policy == password_keeper::password_policy::invalid
);
// Encryption policy
m_pimpl->m_encryption_policy = password_keeper::string_to_encryption_policy(
password->get<std::string>("encryption", "default")
);
SIGHT_THROW_IF(
"Cannot read encryption policy.",
m_pimpl->m_encryption_policy == password_keeper::encryption_policy::invalid
);
}
// Format configuration
const auto& archive = tree.get_child_optional("archive.<xmlattr>");
if(archive.is_initialized())
{
m_pimpl->m_archive_format = archive::string_to_archive_format(archive->get<std::string>("format", "default"));
SIGHT_THROW_IF(
"Cannot read archive format.",
m_pimpl->m_archive_format == archive::archive_format::invalid
);
}
}
//-----------------------------------------------------------------------------
void writer::updating()
{
// Set to failed until successful
m_write_failed = true;
// Show the save dialog if the path is empty
if((!has_location_defined() && m_pimpl->m_dialog_policy != dialog_policy::never)
|| m_pimpl->m_dialog_policy == dialog_policy::always)
{
open_location_dialog();
}
// If the user did not choose a file, we stop here
if(!has_location_defined())
{
return;
}
// Check if we must add an extension or not
auto filepath = get_file();
if(!filepath.has_extension())
{
filepath += m_pimpl->m_extension_name;
}
// In case the path is computed instead of user-selected, make sure it exists
if(filepath.has_parent_path() && !std::filesystem::exists(filepath.parent_path()))
{
std::filesystem::create_directories(filepath.parent_path());
}
SIGHT_THROW_IF("The file '" << filepath << "' is an existing folder.", std::filesystem::is_directory(filepath));
// Generate temporary file
const core::os::temp_file temp_file;
// Ask password if needed
const secure_string& password =
[&]
{
if(m_pimpl->m_password_policy == password_keeper::password_policy::never)
{
// No password management
return secure_string();
}
const secure_string& global_password = password_keeper::get_global_password();
if((m_pimpl->m_password_policy == password_keeper::password_policy::always)
|| (m_pimpl->m_password_policy == password_keeper::password_policy::global
&& global_password.empty()))
{
const auto& [newPassword, ok] =
sight::ui::dialog::input::show_input_dialog(
"Enter Password",
"Password:",
global_password.c_str(), // NOLINT(readability-redundant-string-cstr)
sight::ui::dialog::input::echo_mode::password
);
return secure_string(newPassword);
}
return global_password;
}();
const auto write_job = std::make_shared<core::jobs::job>(
"Writing " + temp_file.string() + " file",
[&](core::jobs::job& _running_job)
{
_running_job.done_work(10);
// Create the session writer
auto writer = std::make_shared<sight::io::session::session_writer>();
{
// The object must be unlocked since it will be locked again when writing
auto data = m_data.lock();
writer->set_object(data.get_shared());
writer->set_file(temp_file);
writer->set_password(password);
writer->set_encryption_policy(m_pimpl->m_encryption_policy);
writer->set_archive_format(m_pimpl->m_archive_format);
}
// Set cursor to busy state. It will be reset to default even if exception occurs
const sight::ui::busy_cursor busy_cursor;
// Write the file
writer->write();
_running_job.done();
},
this->worker()
);
const auto rename_job = std::make_shared<core::jobs::job>(
"Rename file" + temp_file.string() + " to " + filepath.string() + ".",
[&](core::jobs::job& _running_job)
{
_running_job.done_work(80);
// Robust rename
core::tools::system::robust_rename(temp_file, filepath, true);
_running_job.done();
},
this->worker()
);
core::jobs::aggregator::sptr jobs = std::make_shared<core::jobs::aggregator>(filepath.string() + " writer");
jobs->add(write_job);
jobs->add(rename_job);
jobs->set_cancelable(false);
m_pimpl->m_job_created_signal->emit(jobs);
try
{
jobs->run().get();
m_write_failed = false;
}
catch(const std::exception& e)
{
// Handle the error.
SIGHT_ERROR(e.what());
sight::ui::dialog::message::show(
"Session writer failed",
e.what(),
sight::ui::dialog::message::critical
);
}
catch(...)
{
// Handle the error.
sight::ui::dialog::message::show(
"Session writer aborted",
"Writing process aborted",
sight::ui::dialog::message::warning
);
}
}
//-----------------------------------------------------------------------------
void writer::open_location_dialog()
{
static auto default_location = std::make_shared<core::location::single_folder>();
sight::ui::dialog::location location_dialog;
location_dialog.set_title(*m_window_title);
location_dialog.set_default_location(default_location);
location_dialog.set_option(ui::dialog::location::write);
location_dialog.set_type(ui::dialog::location::single_file);
location_dialog.add_filter(m_pimpl->m_extension_description, "*" + m_pimpl->m_extension_name);
// Show the dialog
const auto result = std::dynamic_pointer_cast<core::location::single_file>(location_dialog.show());
if(result)
{
const auto& filepath = result->get_file();
set_file(filepath);
m_pimpl->m_extension_name = location_dialog.get_selected_extensions().front();
// Save default location for later use
default_location->set_folder(filepath.parent_path());
location_dialog.save_default_location(default_location);
}
else
{
clear_locations();
}
}
//-----------------------------------------------------------------------------
} // namespace sight::module::io::session
|