21 _latestDirectory = QString::fromStdString(
"/tmp/MemoryExport");
23 _loadFromDiskButton =
new QPushButton(
" Load Query from Disk into WM",
this);
24 _loadFromDiskButton->setIcon(QIcon(
":/icons/document-open.svg"));
25 _storeOnDiskButton =
new QPushButton(
" Store shown Data on Disk",
this);
26 _storeOnDiskButton->setIcon(QIcon(
":/icons/document-save.svg"));
29 std::vector<QPushButton*> buttons{_storeOnDiskButton, _loadFromDiskButton};
30 for (QPushButton* button : buttons)
32 button->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
35 this->setSizePolicy(QSizePolicy::Policy::Preferred, QSizePolicy::Policy::Fixed);
37 QHBoxLayout* layout =
new QHBoxLayout();
38 this->setLayout(layout);
41 layout->setContentsMargins(margin, margin, margin, margin);
43 layout->addWidget(_loadFromDiskButton);
44 layout->addWidget(_storeOnDiskButton);
49 connect(_loadFromDiskButton,
50 &QPushButton::pressed,
53 QString directory = chooseDirectoryDialog();
54 if (directory.size() > 0)
59 connect(_storeOnDiskButton,
60 &QPushButton::pressed,
63 QString directory = chooseDirectoryDialog();
64 if (directory.size() > 0)
71 static const std::string&
72 handleSingular(
int num,
const std::string& singular,
const std::string& plural)
74 return num == 1 ? singular : plural;
79 const std::vector<wm::Memory> memoryData,
80 std::string* outStatus)
82 std::filesystem::path path(directory.toUtf8().constData());
86 if (std::filesystem::is_regular_file(path))
88 status <<
"Could not export memories contents to " << path
89 <<
": Cannot overwrite existing file.";
94 for (
const auto&
data : memoryData)
96 std::string name =
data.id().memoryName;
97 if (std::filesystem::is_regular_file(path / name))
99 status <<
"Could not export memory '" << name <<
"' to " << path
100 <<
": Cannot overwrite existing file.\n";
110 status <<
"Exported " << numStored <<
" "
111 << handleSingular(numStored,
"memory",
"memories") <<
" to " << path <<
".";
116 *outStatus =
status.str();
120 std::map<std::filesystem::path, wm::Memory>
123 std::string* outStatus)
125 std::filesystem::path path(directory.toUtf8().constData());
127 std::map<std::filesystem::path, wm::Memory> memoryData;
129 auto setStatus = [&](
const std::string&
s)
137 if (not std::filesystem::is_directory(path))
139 setStatus(
"Could not import a memory from " + path.string() +
140 ". It is not a directory. Skipping import.");
146 bool isSingleMemory =
false;
147 for (
auto i = std::filesystem::recursive_directory_iterator(path);
148 i != std::filesystem::recursive_directory_iterator();
154 setStatus(
"Could not import a memory from " + path.string() +
155 ". Data files were not found until max-depth 7. Skipping import.");
162 if (std::filesystem::is_regular_file(dir.path()) &&
177 auto loadMemory = [&](
const std::filesystem::path& p)
179 if (std::filesystem::is_directory(p))
184 memoryData[p] = std::move(
memory);
197 for (
const auto& dir : std::filesystem::directory_iterator(path))
199 loadMemory(dir.path());
204 handleSingular(numLoaded,
"memory",
"memories") +
" from " + path.string() +
".");
209 ControlWidget::chooseDirectoryDialog()
211 _latestDirectory = QFileDialog::getExistingDirectory(
this,
214 QFileDialog::ShowDirsOnly |
215 QFileDialog::DontResolveSymlinks);
217 return _latestDirectory;