mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 01:07:34 +00:00
TextEditor+LibGUI: Use unveil and FileSystemAccessServer
Making use of the new FileSystemAccessServer we are able to use unveil without restricting our ability to open and save files. A file argument will be unveiled automatically however all other files require user action via the FileSystemAccessServer to gain access.
This commit is contained in:
parent
41ce2debda
commit
73ae5200a9
6 changed files with 109 additions and 18 deletions
|
@ -44,6 +44,7 @@
|
|||
namespace TextEditor {
|
||||
|
||||
MainWidget::MainWidget()
|
||||
: m_file_system_access_client(FileSystemAccessClient::construct())
|
||||
{
|
||||
load_from_gml(text_editor_window_gml);
|
||||
|
||||
|
@ -259,9 +260,9 @@ MainWidget::MainWidget()
|
|||
});
|
||||
|
||||
m_open_action = GUI::CommonActions::make_open_action([this](auto&) {
|
||||
Optional<String> open_path = GUI::FilePicker::get_open_filepath(window());
|
||||
auto fd_response = m_file_system_access_client->prompt_open_file(Core::StandardPaths::home_directory(), Core::OpenMode::ReadWrite);
|
||||
|
||||
if (!open_path.has_value())
|
||||
if (fd_response.error() != 0)
|
||||
return;
|
||||
|
||||
if (editor().document().is_modified()) {
|
||||
|
@ -272,28 +273,36 @@ MainWidget::MainWidget()
|
|||
return;
|
||||
}
|
||||
|
||||
open_file(open_path.value());
|
||||
read_file_and_close(fd_response.fd()->take_fd(), fd_response.chosen_file().value());
|
||||
});
|
||||
|
||||
m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
|
||||
Optional<String> save_path = GUI::FilePicker::get_save_filepath(window(), m_name.is_null() ? "Untitled" : m_name, m_extension.is_null() ? "txt" : m_extension);
|
||||
if (!save_path.has_value())
|
||||
auto response = m_file_system_access_client->prompt_save_file(m_name.is_null() ? "Untitled" : m_name, m_extension.is_null() ? "txt" : m_extension, Core::StandardPaths::home_directory(), Core::OpenMode::Truncate | Core::OpenMode::WriteOnly);
|
||||
|
||||
if (response.error() != 0)
|
||||
return;
|
||||
|
||||
if (!m_editor->write_to_file(save_path.value())) {
|
||||
if (!m_editor->write_to_file_and_close(response.fd()->take_fd())) {
|
||||
GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
|
||||
return;
|
||||
}
|
||||
// FIXME: It would be cool if this would propagate from GUI::TextDocument somehow.
|
||||
window()->set_modified(false);
|
||||
|
||||
set_path(save_path.value());
|
||||
dbgln("Wrote document to {}", save_path.value());
|
||||
set_path(response.chosen_file().value());
|
||||
dbgln("Wrote document to {}", response.chosen_file().value());
|
||||
});
|
||||
|
||||
m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
|
||||
if (!m_path.is_empty()) {
|
||||
if (!m_editor->write_to_file(m_path)) {
|
||||
auto response = m_file_system_access_client->request_file(m_path, Core::OpenMode::Truncate | Core::OpenMode::WriteOnly);
|
||||
|
||||
if (response.error() != 0)
|
||||
return;
|
||||
|
||||
int fd = response.fd()->take_fd();
|
||||
|
||||
if (!m_editor->write_to_file_and_close(fd)) {
|
||||
GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
|
||||
} else {
|
||||
// FIXME: It would be cool if this would propagate from GUI::TextDocument somehow.
|
||||
|
@ -647,10 +656,12 @@ void MainWidget::update_title()
|
|||
window()->set_title(builder.to_string());
|
||||
}
|
||||
|
||||
bool MainWidget::open_file(const String& path)
|
||||
bool MainWidget::read_file_and_close(int fd, String const& path)
|
||||
{
|
||||
auto file = Core::File::construct(path);
|
||||
if (!file->open(Core::OpenMode::ReadOnly) && file->error() != ENOENT) {
|
||||
VERIFY(path.starts_with("/"sv));
|
||||
auto file = Core::File::construct();
|
||||
|
||||
if (!file->open(fd, Core::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes) && file->error() != ENOENT) {
|
||||
GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: {}", path, strerror(errno)), "Error", GUI::MessageBox::Type::Error);
|
||||
return false;
|
||||
}
|
||||
|
@ -706,7 +717,12 @@ void MainWidget::drop_event(GUI::DropEvent& event)
|
|||
GUI::MessageBox::show(window(), "TextEditor can only open one file at a time!", "One at a time please!", GUI::MessageBox::Type::Error);
|
||||
return;
|
||||
}
|
||||
open_file(urls.first().path());
|
||||
auto file_response = m_file_system_access_client->request_file(urls.first().path(), Core::OpenMode::ReadOnly);
|
||||
|
||||
if (file_response.error() != 0)
|
||||
return;
|
||||
|
||||
read_file_and_close(file_response.fd()->take_fd(), urls.first().path());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue