Skip to content

Commit

Permalink
(fix, rename) multiple files storage, configurable option to use for …
Browse files Browse the repository at this point in the history
…the embedded files a constant name (#2354)
  • Loading branch information
giuspen committed Jan 19, 2025
1 parent 895faaa commit e16edcb
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/ct/ct_actions_edit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ void CtActions::embfile_insert_path(const std::string& filepath)
_curr_buffer()->get_insert()->get_iter().get_offset(),
"",
CtImageEmbFile::get_next_unique_id(),
embfilePath.empty() ? embfilePath : embfilePath.parent_path()};
embfilePath};
pAnchoredWidget->insertInTextBuffer(_curr_buffer());

_pCtMainWin->get_tree_store().addAnchoredWidgets(_pCtMainWin->curr_tree_iter(),
Expand Down
8 changes: 4 additions & 4 deletions src/ct/ct_actions_others.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ void CtActions::embfile_save()

_pCtConfig->pickDirFile = Glib::path_get_dirname(filepath);

if (curr_file_anchor->get_raw_blob().empty() and not curr_file_anchor->get_dirLastMultiFile().empty()) {
const fs::path embfilePathLast = curr_file_anchor->get_dirLastMultiFile() / curr_file_anchor->get_file_name();
if (curr_file_anchor->get_raw_blob().empty()) {
const fs::path& embfilePathLast = curr_file_anchor->get_pathLastMultiFile();
if (fs::exists(embfilePathLast) and fs::copy_file(embfilePathLast, filepath.c_str())) {
return;
}
Expand All @@ -159,8 +159,8 @@ void CtActions::embfile_save()

void CtActions::embfile_open()
{
if (curr_file_anchor->get_raw_blob().empty() and not curr_file_anchor->get_dirLastMultiFile().empty()) {
const fs::path embfilePathLast = curr_file_anchor->get_dirLastMultiFile() / curr_file_anchor->get_file_name();
if (curr_file_anchor->get_raw_blob().empty()) {
const fs::path& embfilePathLast = curr_file_anchor->get_pathLastMultiFile();
if (fs::exists(embfilePathLast)) {
fs::open_filepath(embfilePathLast, false/*open_folder_if_file_not_exists*/, _pCtConfig);
return;
Expand Down
20 changes: 7 additions & 13 deletions src/ct/ct_image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -593,13 +593,13 @@ CtImageEmbFile::CtImageEmbFile(CtMainWin* pCtMainWin,
const int charOffset,
const std::string& justification,
const size_t uniqueId,
const fs::path& dirLastMultiFile)
const fs::path& pathLastMultiFile)
: CtImage{pCtMainWin, _get_file_icon(pCtMainWin, fileName), charOffset, justification}
, _fileName{fileName}
, _rawBlob{rawBlob}
, _timeSeconds{timeSeconds}
, _uniqueId{uniqueId}
, _dirLastMultiFile{dirLastMultiFile}
, _pathLastMultiFile{pathLastMultiFile}
{
signal_button_press_event().connect(sigc::mem_fun(*this, &CtImageEmbFile::_on_button_press_event), false);
update_tooltip();
Expand Down Expand Up @@ -627,18 +627,12 @@ void CtImageEmbFile::_checkNonEmptyRawBlob(const char* multifile_dir)
return;
}
// let's check also if the embedded file was copied/moved and the original file is still in the old directory
if (not _dirLastMultiFile.empty()) {
const fs::path embfilePathLast = _dirLastMultiFile / _fileName;
if (fs::exists(embfilePathLast)) {
_rawBlob = Glib::file_get_contents(embfilePathLast.string());
spdlog::debug("{} FROM multifile constant foreign {}", __FUNCTION__, embfilePathLast.string());
}
else {
spdlog::warn("?? missing foreign {}", __FUNCTION__, embfilePathLast.c_str());
}
if (fs::exists(_pathLastMultiFile)) {
_rawBlob = Glib::file_get_contents(_pathLastMultiFile.string());
spdlog::debug("{} FROM multifile constant last {}", __FUNCTION__, _pathLastMultiFile.string());
}
else {
spdlog::warn("?? {} {} empty _dirLastMultiFile", __FUNCTION__, _fileName.c_str());
spdlog::warn("?? missing multifile constant last {}", __FUNCTION__, _pathLastMultiFile.c_str());
}
}

Expand Down Expand Up @@ -668,7 +662,7 @@ void CtImageEmbFile::to_xml(xmlpp::Element* p_node_parent,
Glib::file_set_contents(embfilePath.string(), _rawBlob);
if (fs::exists(embfilePath)) {
spdlog::debug("{} written multifile constant name {}, cleared _rawBlob", __FUNCTION__, embfilePath.c_str());
_dirLastMultiFile = multifile_dir;
_pathLastMultiFile = embfilePath;
_rawBlob.clear();
}
else {
Expand Down
6 changes: 3 additions & 3 deletions src/ct/ct_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class CtImageEmbFile : public CtImage
const int charOffset,
const std::string& justification,
const size_t uniqueId,
const fs::path& dirLastMultiFile);
const fs::path& pathLastMultiFile);
~CtImageEmbFile() override {}

void to_xml(xmlpp::Element* p_node_parent, const int offset_adjustment, CtStorageCache* cache, const std::string& multifile_dir) override;
Expand All @@ -191,7 +191,7 @@ class CtImageEmbFile : public CtImage
time_t get_time() { return _timeSeconds; }
void set_time(const time_t time) { _timeSeconds = time; }
size_t get_unique_id() { return _uniqueId; }
const fs::path& get_dirLastMultiFile() { return _dirLastMultiFile; }
const fs::path& get_pathLastMultiFile() { return _pathLastMultiFile; }

static size_t get_next_unique_id();

Expand All @@ -210,5 +210,5 @@ class CtImageEmbFile : public CtImage
std::string _rawBlob; // raw data, not a string
time_t _timeSeconds;
const size_t _uniqueId;
fs::path _dirLastMultiFile;
fs::path _pathLastMultiFile;
};
6 changes: 3 additions & 3 deletions src/ct/ct_state_machine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ CtAnchoredWidgetState_EmbFile::CtAnchoredWidgetState_EmbFile(CtImageEmbFile* emb
, rawBlob{embFile->get_raw_blob()}
, timeSeconds{embFile->get_time()}
, uniqueId{embFile->get_unique_id()}
, dirLastMultiFile{embFile->get_dirLastMultiFile()}
, pathLastMultiFile{embFile->get_pathLastMultiFile()}
{
}

Expand All @@ -116,12 +116,12 @@ bool CtAnchoredWidgetState_EmbFile::equal(std::shared_ptr<CtAnchoredWidgetState>
rawBlob == other_state->rawBlob and
timeSeconds == other_state->timeSeconds and
uniqueId == other_state->uniqueId and
dirLastMultiFile == other_state->dirLastMultiFile;
pathLastMultiFile == other_state->pathLastMultiFile;
}

CtAnchoredWidget* CtAnchoredWidgetState_EmbFile::to_widget(CtMainWin* pCtMainWin)
{
return new CtImageEmbFile{pCtMainWin, fileName, rawBlob, timeSeconds, charOffset, justification, uniqueId, dirLastMultiFile};
return new CtImageEmbFile{pCtMainWin, fileName, rawBlob, timeSeconds, charOffset, justification, uniqueId, pathLastMultiFile};
}

// Codebox
Expand Down
2 changes: 1 addition & 1 deletion src/ct/ct_state_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class CtAnchoredWidgetState_EmbFile : public CtAnchoredWidgetState
std::string rawBlob; // raw data, not a string
time_t timeSeconds;
const size_t uniqueId;
fs::path dirLastMultiFile;
fs::path pathLastMultiFile;
};

class CtAnchoredWidgetState_Codebox : public CtAnchoredWidgetState
Expand Down
9 changes: 8 additions & 1 deletion src/ct/ct_storage_xml.cc
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,14 @@ CtAnchoredWidget* CtStorageXmlHelper::_create_image_from_xml(xmlpp::Element* xml
timeStr = "0";
}
const time_t timeInt = std::stoll(timeStr);
return new CtImageEmbFile{_pCtMainWin, file_name, rawBlob, timeInt, charOffset, justification, CtImageEmbFile::get_next_unique_id(), multifile_dir};
return new CtImageEmbFile{_pCtMainWin,
file_name,
rawBlob,
timeInt,
charOffset,
justification,
CtImageEmbFile::get_next_unique_id(),
fs::path{multifile_dir} / file_name};
}
const Glib::ustring link = xml_element->get_attribute_value("link");
return new CtImagePng{_pCtMainWin, rawBlob, link, charOffset, justification};
Expand Down
3 changes: 1 addition & 2 deletions tests/tests_read_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,7 @@ void TestCtApp::_assert_tree_data(CtMainWin* pWin, const bool after_mods)
static const std::string embedded_file = Glib::Base64::decode("0LnRhtGD0LrQtdC90LPRiNGJ0LcK");
const auto rawBlobSize = pImageEmbFile->get_raw_blob().size();
if (0 == rawBlobSize) {
const fs::path& dirLastMultiFile = pImageEmbFile->get_dirLastMultiFile();
const fs::path embFilePath = dirLastMultiFile / pImageEmbFile->get_file_name();
const fs::path& embFilePath = pImageEmbFile->get_pathLastMultiFile();
ASSERT_TRUE(fs::is_regular_file(embFilePath));
std::string embFileContent = Glib::file_get_contents(embFilePath.string());
ASSERT_EQ(embedded_file.size(), embFileContent.size());
Expand Down

0 comments on commit e16edcb

Please sign in to comment.