File: writer.cpp

package info (click to toggle)
sight 25.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,180 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (407 lines) | stat: -rw-r--r-- 13,622 bytes parent folder | download
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
/************************************************************************
 *
 * Copyright (C) 2009-2025 IRCAD France
 * Copyright (C) 2012-2020 IHU Strasbourg
 *
 * 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/>.
 *
 ***********************************************************************/
#include "io/__/service/writer.hpp"

#include <core/base.hpp>
#include <core/com/signal.hxx>
#include <core/com/slots.hpp>
#include <core/com/slots.hxx>

#include <service/macros.hpp>

#include <ui/__/preferences.hpp>

#include <boost/algorithm/string.hpp>

namespace sight::io::service
{

//-----------------------------------------------------------------------------

writer::writer(const std::string& _default_window_title) noexcept :
    has_monitors(m_signals),
    m_window_title(this, WINDOW_TITLE_KEY, _default_window_title)
{
    new_signal<signals::void_signal_t>(signals::FAILED);
    new_signal<signals::void_signal_t>(signals::SUCCEEDED);
    new_signal<signals::void_signal_t>(signals::PREFIX_SET);
    new_signal<signals::void_signal_t>(signals::BASE_FOLDER_SET);

    new_slot(slots::OPEN_LOCATION_DIALOG, &writer::open_location_dialog, this);
    new_slot(slots::SET_PREFIX, &writer::set_prefix, this);
    new_slot(slots::SET_BASE_FOLDER, &writer::set_base_folder, this);

    new_slot(slots::UPDATE_DEFAULT_LOCATIONS, &writer::update_default_locations, this);
}

//-----------------------------------------------------------------------------

std::filesystem::path writer::get_file() const
{
    // WARNING: Pay attention here that no canonical paths are processed when concatenating paths
    // In this case, the behavior is to replace the previous path.

    SIGHT_THROW_IF("This writer doesn't manage files", !(this->get_path_type() & io::service::file));

    // Make sure the base folder is in sync (also re-reads file/folder entries to respect the developer choices)
    std::string base_folder;
    this->update_base_folder(base_folder);

    // Pre-pend the base folder by default, whether it is unset and empty
    // Or pre-filled by the user
    std::filesystem::path location = std::filesystem::path(base_folder);

    // Only use the prefix if we have a base folder,
    // We don't want to modify an user defined path (set through the GUI)
    if(!base_folder.empty() && !m_current_prefix.empty())
    {
        location /= std::filesystem::path(m_current_prefix);
    }

    SIGHT_THROW_IF("Exactly one file must be defined in location", m_locations.size() != 1);

    location /= m_locations.front();

    return location;
}

//-----------------------------------------------------------------------------

void writer::set_file(const std::filesystem::path& _file)
{
    SIGHT_THROW_IF("This writer doesn't manage files", !(this->get_path_type() & io::service::file));
    m_locations.clear();
    m_locations.push_back(_file);
}

//-----------------------------------------------------------------------------

const io::service::locations_t& writer::get_files() const
{
    SIGHT_THROW_IF("This writer doesn't manage files", !(this->get_path_type() & io::service::files));
    SIGHT_THROW_IF("At least one file must be defined in location", m_locations.empty());
    return m_locations;
}

//-----------------------------------------------------------------------------

void writer::set_files(const io::service::locations_t& _files)
{
    SIGHT_THROW_IF("This writer doesn't manage files", !(this->get_path_type() & io::service::files));
    m_locations = _files;
}

//-----------------------------------------------------------------------------

std::filesystem::path writer::get_folder() const
{
    // WARNING: Pay attention here that no canonical paths are processed when concatenating paths
    // In this case, the behavior is to replace the previous path.

    SIGHT_THROW_IF("This writer doesn't manage folders", !(this->get_path_type() & io::service::folder));

    // Make sure the base folder is in sync (also re-reads file/folder entries to respect the developer choices)
    std::string base_folder;
    this->update_base_folder(base_folder);

    std::filesystem::path location = std::filesystem::path(base_folder);

    // Only use the prefix if we have a base folder,
    // We don't want to modify an user defined path (set through the GUI)
    if(!base_folder.empty() && !m_current_prefix.empty())
    {
        location /= std::filesystem::path(m_current_prefix);
    }

    location /= (m_locations.empty() ? "" : m_locations.front());

    return location;
}

//-----------------------------------------------------------------------------

void writer::set_folder(const std::filesystem::path& _folder)
{
    SIGHT_THROW_IF("This writer doesn't manage folders", !(this->get_path_type() & io::service::folder));
    m_locations.clear();
    m_locations.push_back(_folder);
}

//------------------------------------------------------------------------------

void writer::set_prefix(std::string _prefix)
{
    // Record the current prefix
    m_current_prefix = _prefix;

    this->async_emit(signals::PREFIX_SET);
}

//------------------------------------------------------------------------------

void writer::set_base_folder(std::string _path)
{
    // Record the current prefix
    m_base_folder = _path;

    this->async_emit(signals::BASE_FOLDER_SET);
}

//-----------------------------------------------------------------------------

const io::service::locations_t& writer::get_locations() const
{
    SIGHT_THROW_IF("At least one path must be defined in location", m_locations.empty());
    return m_locations;
}

//-----------------------------------------------------------------------------

void writer::clear_locations()
{
    m_locations.clear();
}

//-----------------------------------------------------------------------------

void writer::configuring()
{
    /// @todo check if this is still needed or usefull or not a static assert at build time...
    SIGHT_ASSERT(
        "Generic configuring method is only available for io service that use paths.",
        !(this->get_path_type() & io::service::type_not_defined)
    );

    SIGHT_ASSERT(
        "This writer cannot manages both io::service::files and io::service::file.",
        (this->get_path_type() & io::service::files) == 0 || (this->get_path_type() & io::service::file) == 0
    );

    // Check if we use properties or XML configuration
    const config_t config              = this->get_config();
    const bool use_file_config         = config.count(OLD_FILE_KEY) > 0;
    const bool use_folder_config       = config.count(FOLDER_KEY) > 0;
    const bool use_window_title_config = config.count(OLD_WINDOW_TITLE_KEY) > 0;

    if(use_file_config || use_folder_config || use_window_title_config)
    {
        // Use XML configuration
        /// @todo remove me once deprecated configuration is removed

        // Deprecation messages
        if(use_file_config)
        {
            FW_DEPRECATED_MSG(
                "<" + OLD_FILE_KEY + "> XML configuration is deprecated, use `" + FILES_KEY + "` property instead.",
                "26.0"
            );
        }

        if(use_folder_config)
        {
            FW_DEPRECATED_MSG(
                "<" + FOLDER_KEY + "> XML configuration is deprecated, use `" + FOLDER_KEY + "` property instead.",
                "26.0"
            );
        }

        // windowTitle
        if(use_window_title_config)
        {
            FW_DEPRECATED_MSG(
                "<" + OLD_WINDOW_TITLE_KEY + "> XML configuration is deprecated, use `" + WINDOW_TITLE_KEY
                + "` property instead.",
                "26.0"
            );

            auto window_title = m_window_title.lock();
            window_title->set_value(config.get<std::string>(OLD_WINDOW_TITLE_KEY));
        }

        // Files
        io::service::locations_t files;
        files.reserve(config.count(OLD_FILE_KEY));

        const auto files_cfg = config.equal_range(OLD_FILE_KEY);
        for(auto file_cfg = files_cfg.first ; file_cfg != files_cfg.second ; ++file_cfg)
        {
            files.emplace_back(file_cfg->second.get_value<std::filesystem::path>());
        }

        // Folder
        auto folder = config.get<std::filesystem::path>(FOLDER_KEY, "");

        // Perform validity checks and fill m_locations
        this->update_locations(files, folder);
    }
    else
    {
        // Use properties
        this->update_default_locations();
    }
}

//------------------------------------------------------------------------------

sight::service::base::connections_t writer::auto_connections() const
{
    return {
        {m_files, data::object::MODIFIED_SIG, slots::UPDATE_DEFAULT_LOCATIONS},
        {m_folder, data::object::MODIFIED_SIG, slots::UPDATE_DEFAULT_LOCATIONS}
    };
}

//------------------------------------------------------------------------------

void writer::update_default_locations()
{
    io::service::locations_t files;

    if(const auto files_property = *m_files; !files_property.empty())
    {
        boost::split(files, files_property, boost::is_any_of(";"), boost::token_compress_on);
    }

    this->update_locations(files, *m_folder);
}

//------------------------------------------------------------------------------

void writer::update_locations(
    const io::service::locations_t& _files,
    const std::filesystem::path& _folder
)
{
    // Assertion checks
    SIGHT_ASSERT(
        "This writer does not manage files and a file path is given in the configuration",
        (this->get_path_type() & io::service::file || this->get_path_type() & io::service::files) || (_files.empty())
    );

    // Populate m_locations with files
    if(!_files.empty())
    {
        if((this->get_path_type() & io::service::file) != 0)
        {
            // Single file
            SIGHT_ASSERT(
                "This writer manage single file, but there is more than one file.",
                _files.size() == 1
            );

            this->set_file(_files.front());
        }
        else if((this->get_path_type() & io::service::files) != 0)
        {
            // Multiple files
            this->set_files(_files);
        }
    }

    // Populate m_locations with folder
    if((this->get_path_type() & io::service::folder) != 0 && !_folder.empty())
    {
        this->set_folder(_folder);
    }
}

//------------------------------------------------------------------------------

void writer::update_base_folder(std::string& _out_base_folder) const
{
    const sight::service::config_t config = this->get_config();

    std::string base_folder_cfg = m_base_folder;

    if(base_folder_cfg.empty())
    {
        base_folder_cfg = config.get<std::string>("baseFolder", m_base_folder);
    }

    if(!base_folder_cfg.empty())
    {
        // We need to check for potential updates to the base folder
        // In case the user updates an associated preference during runtime
        // We also re-read the xml configuration for file and folder if baseFolder was set
        sight::ui::preferences preferences;

        // We need to provide the separator, otherwise the 2-args method will be selected
        auto [key, baseFolder] = preferences.parsed_get<std::string>(base_folder_cfg);

        // If the returned value is indeed a preference
        if(!key.empty())
        {
            if(!baseFolder.empty())
            {
                _out_base_folder = baseFolder;
            }
            else
            {
                SIGHT_INFO("No location defined for the preference " << base_folder_cfg);
            }
        }
        else
        {
            if(std::filesystem::exists(base_folder_cfg))
            {
                _out_base_folder = base_folder_cfg;
            }
            else
            {
                SIGHT_ERROR(base_folder_cfg << " is invalid. Reverting to classical selection.");
            }
        }
    }
}

//------------------------------------------------------------------------------

io::service::path_type_t writer::get_path_type() const
{
    return io::service::type_not_defined;
}

//-----------------------------------------------------------------------------

bool writer::has_location_defined() const
{
    std::string base_folder;
    update_base_folder(base_folder);

    // Either we have an absolution location set by the user
    // Or we are using the baseFolder value from the xml configuration
    return ((base_folder.empty()) && !m_locations.empty() && m_locations.front().is_absolute())
           || (!base_folder.empty() && ((this->get_path_type() & io::service::file) != 0)
               && !m_locations.empty())
           || (!base_folder.empty() && ((this->get_path_type() & io::service::folder) != 0));
}

//-----------------------------------------------------------------------------

bool writer::has_failed() const
{
    return m_write_failed;
}

//-----------------------------------------------------------------------------

} // namespace sight::io::service