500 lines
12 KiB
C++
Raw Normal View History

2017-09-19 20:14:56 +01:00
/*
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
* Copyright (C) 2017 Furrtek
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
2023-05-02 20:49:41 -07:00
/* TODO:
* - Paging menu items
* - Copy/Move
*/
#include <algorithm>
2017-09-19 20:14:56 +01:00
#include "ui_fileman.hpp"
#include "string_format.hpp"
#include "portapack.hpp"
#include "event_m0.hpp"
using namespace portapack;
namespace fs = std::filesystem;
2017-09-19 20:14:56 +01:00
namespace {
using namespace ui;
bool is_hidden_file(const fs::path& path) {
return !path.empty() && path.native()[0] == u'.';
}
// Gets a truncated name from a path for display.
std::string truncate(const fs::path& path, size_t max_length) {
auto name = path.string();
return name.length() <= max_length ? name : name.substr(0, max_length);
}
// Gets a human readable file size string.
2023-05-01 09:25:32 -07:00
std::string get_pretty_size(uint32_t file_size) {
static const std::string suffix[5] = { "B", "kB", "MB", "GB", "??" };
size_t suffix_index = 0;
while (file_size >= 1024) {
file_size /= 1024;
suffix_index++;
}
if (suffix_index > 4)
suffix_index = 4;
return to_string_dec_uint(file_size) + suffix[suffix_index];
}
// Case insensitive path equality on underlying "native" string.
bool iequal(
const fs::path& lhs,
const fs::path& rhs
) {
const auto& lhs_str = lhs.native();
const auto& rhs_str = rhs.native();
// NB: Not correct for Unicode/locales.
if (lhs_str.length() == rhs_str.length()) {
for (size_t i = 0; i < lhs_str.length(); ++i)
if (towupper(lhs_str[i]) != towupper(rhs_str[i]))
return false;
return true;
}
return false;
}
// Inserts the entry into the entry list sorted directories first then by file name.
void insert_sorted(std::vector<fileman_entry>& entries, fileman_entry&& entry) {
auto it = std::lower_bound(std::begin(entries), std::end(entries), entry,
[](const fileman_entry& lhs, const fileman_entry& rhs) {
if (lhs.is_directory && !rhs.is_directory)
return true;
2023-05-01 09:25:32 -07:00
else if (!lhs.is_directory && rhs.is_directory)
return false;
else
return lhs.path < rhs.path;
});
entries.insert(it, std::move(entry));
}
2023-05-01 09:25:32 -07:00
// Returns the partner file path or an empty path if no partner is found.
fs::path get_partner_file(fs::path path) {
2023-05-03 13:13:15 -07:00
if (fs::is_directory(path))
return { };
const fs::path txt_path{ u".TXT" };
const fs::path c16_path{ u".C16" };
2023-05-01 09:25:32 -07:00
auto ext = path.extension();
if (iequal(ext, txt_path))
ext = c16_path;
else if (iequal(ext, c16_path))
ext = txt_path;
else
return { };
path.replace_extension(ext);
2023-05-03 13:13:15 -07:00
return fs::file_exists(path) && !fs::is_directory(path) ? path : fs::path{ };
2023-05-01 09:25:32 -07:00
}
// Modal prompt to update the partner file if it exists.
// Runs continuation on_partner_action to update the partner file.
// Returns true is a partner is found, otherwise false.
2023-05-03 13:13:15 -07:00
// Path must be the full path to the file.
bool partner_file_prompt(
NavigationView& nav,
const fs::path& path,
std::string action_name,
std::function<void(const fs::path&, bool)> on_partner_action
) {
auto partner = get_partner_file(path);
2023-05-01 09:25:32 -07:00
if (partner.empty())
return false;
2023-05-01 09:25:32 -07:00
nav.push_under_current<ModalMessageView>(
"Partner File",
partner.filename().string() + "\n" + action_name + " this file too?",
2023-05-01 09:25:32 -07:00
YESNO,
[&nav, partner, on_partner_action](bool choice) {
if (on_partner_action)
on_partner_action(partner, choice);
}
2023-05-01 09:25:32 -07:00
);
return true;
2023-05-01 09:25:32 -07:00
}
}
2017-09-19 20:14:56 +01:00
namespace ui {
2023-05-03 13:13:15 -07:00
/* FileManBaseView ***********************************************************/
void FileManBaseView::load_directory_contents(const fs::path& dir_path) {
2017-09-19 20:14:56 +01:00
current_path = dir_path;
entry_list.clear();
auto filtering = !extension_filter.empty();
text_current.set(dir_path.empty() ? "(sd root)" : truncate(dir_path, 24));
for (const auto& entry : fs::directory_iterator(dir_path, u"*")) {
// Hide files starting with '.' (hidden / tmp).
if (is_hidden_file(entry.path()))
continue;
if (fs::is_regular_file(entry.status())) {
if (!filtering || iequal(entry.path().extension(), extension_filter))
insert_sorted(entry_list, { entry.path(), (uint32_t)entry.size(), false });
} else if (fs::is_directory(entry.status())) {
insert_sorted(entry_list, { entry.path(), 0, true });
2017-09-19 20:14:56 +01:00
}
}
// Add "parent" directory if not at the root.
if (!dir_path.empty())
2023-05-01 09:25:32 -07:00
entry_list.insert(entry_list.begin(), { parent_dir_path, 0, true });
2017-09-19 20:14:56 +01:00
}
fs::path FileManBaseView::get_selected_full_path() const {
2023-05-01 09:25:32 -07:00
if (get_selected_entry().path == parent_dir_path)
return current_path.parent_path();
return current_path / get_selected_entry().path;
2017-09-19 20:14:56 +01:00
}
const fileman_entry& FileManBaseView::get_selected_entry() const {
2023-05-03 13:13:15 -07:00
// TODO: return reference to an "empty" entry on OOB?
return entry_list[menu_view.highlighted_index()];
}
2017-09-19 20:14:56 +01:00
FileManBaseView::FileManBaseView(
NavigationView& nav,
std::string filter
) : nav_ (nav),
extension_filter { filter }
2017-09-19 20:14:56 +01:00
{
add_children({
&labels,
&text_current,
&button_exit
});
2017-09-19 20:14:56 +01:00
button_exit.on_select = [this, &nav](Button&) {
nav.pop();
};
if (!sdcIsCardInserted(&SDCD1)) {
empty_root = true;
text_current.set("NO SD CARD!");
return;
}
load_directory_contents(current_path);
if (!entry_list.size()) {
empty_root = true;
text_current.set("EMPTY SD CARD!");
} else {
menu_view.on_left = [this]() {
2023-05-03 13:13:15 -07:00
pop_dir();
};
}
}
2017-09-19 20:14:56 +01:00
void FileManBaseView::focus() {
if (empty_root) {
2017-09-19 20:14:56 +01:00
button_exit.focus();
} else {
menu_view.focus();
}
}
2023-05-03 13:13:15 -07:00
void FileManBaseView::push_dir(const fs::path& path) {
if (path == parent_dir_path) {
pop_dir();
} else {
current_path /= path;
saved_index_stack.push_back(menu_view.highlighted_index());
menu_view.set_highlighted(0);
reload_current();
}
}
void FileManBaseView::pop_dir() {
if (saved_index_stack.empty())
return;
current_path = current_path.parent_path();
reload_current();
menu_view.set_highlighted(saved_index_stack.back());
saved_index_stack.pop_back();
}
2017-09-19 20:14:56 +01:00
void FileManBaseView::refresh_list() {
if (on_refresh_widgets)
on_refresh_widgets(false);
2023-05-03 13:13:15 -07:00
auto prev_highlight = menu_view.highlighted_index();
menu_view.clear();
2017-09-19 20:14:56 +01:00
for (const auto& entry : entry_list) {
auto entry_name = truncate(entry.path, 20);
if (entry.is_directory) {
menu_view.add_item({
entry_name,
ui::Color::yellow(),
&bitmap_icon_dir,
[this](KeyEvent key) {
if (on_select_entry)
on_select_entry(key);
}
});
} else {
const auto& assoc = get_assoc(entry.path.extension());
2023-05-01 09:25:32 -07:00
auto size_str = get_pretty_size(entry.size);
menu_view.add_item({
entry_name + std::string(21 - entry_name.length(), ' ') + size_str,
assoc.color,
assoc.icon,
[this](KeyEvent key) {
if (on_select_entry)
on_select_entry(key);
}
});
2017-09-19 20:14:56 +01:00
}
}
2023-05-03 13:23:15 -07:00
menu_view.set_highlighted(prev_highlight);
}
void FileManBaseView::reload_current() {
load_directory_contents(current_path);
refresh_list();
}
const FileManBaseView::file_assoc_t& FileManBaseView::get_assoc(
const fs::path& ext) const
{
size_t index = 0;
for (; index < file_types.size() - 1; ++index)
if (iequal(ext, file_types[index].extension))
return file_types[index];
// Default to last entry in the list.
return file_types[index];
2017-09-19 20:14:56 +01:00
}
/*void FileSaveView::on_save_name() {
text_prompt(nav_, &filename_buffer, 8, [this](std::string * buffer) {
2017-09-19 20:14:56 +01:00
nav_.pop();
});
2017-09-19 20:14:56 +01:00
}
FileSaveView::FileSaveView(
NavigationView& nav
) : FileManBaseView(nav)
{
name_buffer.clear();
2017-09-19 20:14:56 +01:00
add_children({
&text_save,
&button_save_name,
&live_timestamp
2017-09-19 20:14:56 +01:00
});
button_save_name.on_select = [this, &nav](Button&) {
on_save_name();
};
}*/
2017-09-19 20:14:56 +01:00
2023-05-03 13:13:15 -07:00
/* FileLoadView **************************************************************/
void FileLoadView::refresh_widgets(const bool) {
2017-09-19 20:14:56 +01:00
set_dirty();
}
FileLoadView::FileLoadView(
NavigationView& nav,
std::string filter
) : FileManBaseView(nav, filter)
2017-09-19 20:14:56 +01:00
{
on_refresh_widgets = [this](bool v) {
refresh_widgets(v);
};
add_children({
&menu_view
2017-09-19 20:14:56 +01:00
});
// Resize menu view to fill screen
menu_view.set_parent_rect({ 0, 3 * 8, 240, 29 * 8 });
refresh_list();
on_select_entry = [this](KeyEvent) {
if (get_selected_entry().is_directory) {
2023-05-03 13:13:15 -07:00
push_dir(get_selected_entry().path);
} else {
2023-05-03 15:29:01 -07:00
nav_.pop();
if (on_changed)
on_changed(get_selected_full_path());
}
2017-09-19 20:14:56 +01:00
};
}
2023-05-03 13:13:15 -07:00
/* FileManagerView ***********************************************************/
2023-05-01 09:25:32 -07:00
void FileManagerView::on_rename() {
auto& entry = get_selected_entry();
name_buffer = entry.path.filename().string();
uint32_t cursor_pos = (uint32_t)name_buffer.length();
2023-05-03 13:13:15 -07:00
if (auto pos = name_buffer.find_last_of(".");
pos != name_buffer.npos && !entry.is_directory)
2023-05-01 09:25:32 -07:00
cursor_pos = pos;
text_prompt(nav_, name_buffer, cursor_pos, max_filename_length,
2023-05-03 13:13:15 -07:00
[this](std::string& renamed) {
auto renamed_path = fs::path{ renamed };
2023-05-01 09:25:32 -07:00
rename_file(get_selected_full_path(), current_path / renamed_path);
2023-05-03 13:13:15 -07:00
auto has_partner = partner_file_prompt(nav_, get_selected_full_path(), "Rename",
2023-05-02 20:49:41 -07:00
[this, renamed_path](const fs::path& partner, bool should_rename) mutable {
if (should_rename) {
auto new_name = renamed_path.replace_extension(partner.extension());
rename_file(current_path / partner, current_path / new_name);
}
2023-05-02 20:49:41 -07:00
reload_current();
}
);
if (!has_partner)
reload_current();
2023-05-01 09:25:32 -07:00
});
2017-09-19 20:14:56 +01:00
}
2023-05-01 09:25:32 -07:00
void FileManagerView::on_delete() {
2023-05-03 13:13:15 -07:00
auto name = get_selected_entry().path.filename().string();
2023-05-01 09:25:32 -07:00
nav_.push<ModalMessageView>("Delete", "Delete " + name + "\nAre you sure?", YESNO,
2023-05-03 13:13:15 -07:00
[this](bool choice) {
2023-05-01 09:25:32 -07:00
if (choice) {
delete_file(get_selected_full_path());
2023-04-26 12:54:07 +08:00
auto has_partner = partner_file_prompt(
2023-05-03 13:13:15 -07:00
nav_, get_selected_full_path(), "Delete",
[this](const fs::path& partner, bool should_delete) {
if (should_delete)
delete_file(current_path / partner);
reload_current();
}
);
if (!has_partner)
reload_current();
2023-05-01 09:25:32 -07:00
}
}
2023-05-01 09:25:32 -07:00
);
}
2023-05-01 09:25:32 -07:00
void FileManagerView::on_new_dir() {
name_buffer = "";
text_prompt(nav_, name_buffer, max_filename_length, [this](std::string& dir_name) {
2023-05-01 09:25:32 -07:00
make_new_directory(current_path / dir_name);
reload_current();
});
2017-09-19 20:14:56 +01:00
}
2023-05-03 13:13:15 -07:00
bool FileManagerView::selected_is_valid() const {
return !entry_list.empty() &&
get_selected_entry().path != parent_dir_path;
}
2017-09-19 20:14:56 +01:00
void FileManagerView::refresh_widgets(const bool v) {
button_rename.hidden(v);
button_delete.hidden(v);
button_new_dir.hidden(v);
2017-09-19 20:14:56 +01:00
set_dirty();
}
FileManagerView::~FileManagerView() {
}
FileManagerView::FileManagerView(
NavigationView& nav
) : FileManBaseView(nav, "")
2017-09-19 20:14:56 +01:00
{
if (!empty_root) {
on_refresh_widgets = [this](bool v) {
refresh_widgets(v);
};
2017-09-19 20:14:56 +01:00
add_children({
&menu_view,
&labels,
&text_date,
&button_rename,
&button_delete,
&button_new_dir,
2017-09-19 20:14:56 +01:00
});
menu_view.on_highlight = [this]() {
2023-05-03 13:13:15 -07:00
// TODO: enable/disable buttons.
if (selected_is_valid())
text_date.set(to_string_FAT_timestamp(file_created_date(get_selected_full_path())));
else
text_date.set("");
};
refresh_list();
2023-05-03 13:13:15 -07:00
on_select_entry = [this](KeyEvent key) {
if (key == KeyEvent::Select && get_selected_entry().is_directory) {
2023-05-03 13:13:15 -07:00
push_dir(get_selected_entry().path);
} else {
button_rename.focus();
}
};
2023-05-01 09:25:32 -07:00
button_rename.on_select = [this](Button&) {
2023-05-03 13:13:15 -07:00
if (selected_is_valid())
on_rename();
};
2023-04-22 17:10:43 +08:00
2023-05-01 09:25:32 -07:00
button_delete.on_select = [this](Button&) {
2023-05-03 13:13:15 -07:00
if (selected_is_valid())
on_delete();
};
2023-05-01 09:25:32 -07:00
button_new_dir.on_select = [this](Button&) {
on_new_dir();
};
}
2017-09-19 20:14:56 +01:00
}
2020-07-05 00:39:23 -03:00
}