summaryrefslogtreecommitdiff
path: root/src/file_dialog.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/file_dialog.cpp')
-rw-r--r--src/file_dialog.cpp129
1 files changed, 129 insertions, 0 deletions
diff --git a/src/file_dialog.cpp b/src/file_dialog.cpp
new file mode 100644
index 0000000..faab3e7
--- /dev/null
+++ b/src/file_dialog.cpp
@@ -0,0 +1,129 @@
+#include "file_dialog.hpp"
+#include "imgui_context.hpp"
+
+#include <imgui/imgui.h>
+
+#include <spdlog/spdlog.h>
+
+namespace bookmouse {
+
+using fud::FudStatus;
+
+DialogEntry::DialogEntry(fud::DirectoryEntry&& entry) :
+ fud::DirectoryEntry(std::move(entry)), timeInfo{}, niceTime{}, selected{false}, valid{false}, gotTime{false}
+{
+ auto timeInfoPtr = localtime_r(&this->modificationTime, &timeInfo);
+ if (timeInfoPtr == nullptr) {
+ return;
+ }
+
+ valid = true;
+}
+
+FudStatus DialogEntry::formatTime(TimeFormat& format)
+{
+ if (!valid) {
+ return FudStatus::ObjectInvalid;
+ }
+
+ auto result = format.format(this->timeInfo);
+ if (result.isError())
+ {
+ return result.getError();
+ }
+
+ this->niceTime = result.getOkay();
+
+ return FudStatus::Success;
+}
+
+FileDialog::FileDialog(
+ const fud::String& directoryName,
+ TimeFormat& timeFormat) : m_directoryName{directoryName},
+ m_directory{directoryName},
+ m_timeFormat{timeFormat}
+{
+ auto status = getDirectoryContents();
+ if (status != FudStatus::Success) {
+ return;
+ }
+}
+
+FudStatus FileDialog::getDirectoryContents() {
+ auto directoryStatsResult = m_directory.info();
+ if (directoryStatsResult.isError()) {
+ return directoryStatsResult.getError();
+ }
+ auto directoryStats = directoryStatsResult.getOkay();
+
+ size_t count{0};
+ m_directoryContents.reserve(directoryStats.links);
+
+ while (true && count < SIZE_MAX) {
+ auto dirEntryResult = m_directory.getNextEntry();
+ if (dirEntryResult.isError()) {
+ break;
+ }
+
+ auto dirEntryOpt = dirEntryResult.getOkay();
+ if (dirEntryOpt == std::nullopt) {
+ break;
+ }
+
+ if (dirEntryOpt->inode == directoryStats.inode) {
+ continue;
+ }
+
+ m_directoryContents.emplace_back(std::move(*dirEntryOpt));
+ auto status = m_directoryContents.back().formatTime(m_timeFormat);
+ if (status != FudStatus::Success) {
+ spdlog::error("Could not convert time: {}", fud::FudStatusToString(status));
+ }
+ }
+
+ return FudStatus::Success;
+}
+
+FilePickerResult FileDialog::pickFiles()
+{
+ ImText("%s %zu", m_directory.name().c_str(), m_directoryContents.size());
+ if (ImTable table{"Entries", 4, ImGuiTableFlags_Borders}) {
+ auto colFlags = ImGuiSelectableFlags_NoAutoClosePopups;
+ IM_ASSERT(table);
+ ImNextColumn();
+ ImSelectable("T", &m_typeSelected, colFlags);
+ ImNextColumn();
+ ImSelectable("Name", &m_nameSelectede, colFlags);
+ ImNextColumn();
+ ImSelectable("Size", &m_sizeSelected, colFlags);
+ ImNextColumn();
+ ImSelectable("Date", &m_dateSelected, colFlags);
+ for (auto& entry : m_directoryContents) {
+ ImNextRow();
+ const char entryLetter = DirEntryTypeToChar(entry.entryType);
+ ImNextColumn();
+ ImText("%c", entryLetter);
+ ImNextColumn();
+ ImSelectable(entry.name.c_str(), &entry.selected, colFlags | ImGuiSelectableFlags_SpanAllColumns);
+ ImNextColumn();
+ ImText("%zu\n", entry.isDirectory() ? entry.links : entry.size);
+ ImNextColumn();
+ ImText("%s", entry.niceTime.c_str());
+ }
+ }
+
+ if (ImButton("Accept")) {
+ return FilePickerResult::error(FudStatus::NotImplemented);
+ }
+
+ ImSameLine();
+
+ if (ImButton("Cancel")) {
+ return FilePickerResult::error(FudStatus::NotImplemented);
+ }
+
+
+ return FilePickerResult::error(FudStatus::NotImplemented);
+}
+
+} // namespace bookmouse