summaryrefslogtreecommitdiff
path: root/Libraries/LibGUI
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-09-24 11:30:14 +0200
committerAndreas Kling <kling@serenityos.org>2020-09-24 11:30:14 +0200
commit701787b9067e017c76e01889cf71c62586ff5a4b (patch)
tree84ba2b08e9d06a439a5066a0ad811703e841ee06 /Libraries/LibGUI
parent7f8e18b86afe2fb7d7fc1e474b3005929eef6678 (diff)
downloadserenity-701787b9067e017c76e01889cf71c62586ff5a4b.zip
LibGUI: Support editing filenames through FileSystemModel :^)
Diffstat (limited to 'Libraries/LibGUI')
-rw-r--r--Libraries/LibGUI/FileSystemModel.cpp21
-rw-r--r--Libraries/LibGUI/FileSystemModel.h2
2 files changed, 23 insertions, 0 deletions
diff --git a/Libraries/LibGUI/FileSystemModel.cpp b/Libraries/LibGUI/FileSystemModel.cpp
index 10df60a4ef..f4ed772bd6 100644
--- a/Libraries/LibGUI/FileSystemModel.cpp
+++ b/Libraries/LibGUI/FileSystemModel.cpp
@@ -39,6 +39,7 @@
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
+#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
@@ -606,4 +607,24 @@ void FileSystemModel::set_should_show_dotfiles(bool show)
update();
}
+bool FileSystemModel::is_editable(const ModelIndex& index) const
+{
+ if (!index.is_valid())
+ return false;
+ return index.column() == Column::Name;
+}
+
+void FileSystemModel::set_data(const ModelIndex& index, const Variant& data)
+{
+ ASSERT(is_editable(index));
+ Node& node = const_cast<Node&>(this->node(index));
+ auto dirname = LexicalPath(node.full_path()).dirname();
+ auto new_full_path = String::formatted("{}/{}", dirname, data.to_string());
+ int rc = rename(node.full_path().characters(), new_full_path.characters());
+ if (rc < 0) {
+ if (on_error)
+ on_error(errno, strerror(errno));
+ }
+}
+
}
diff --git a/Libraries/LibGUI/FileSystemModel.h b/Libraries/LibGUI/FileSystemModel.h
index 693c41d7d2..2009db5873 100644
--- a/Libraries/LibGUI/FileSystemModel.h
+++ b/Libraries/LibGUI/FileSystemModel.h
@@ -149,6 +149,8 @@ public:
virtual StringView drag_data_type() const override { return "text/uri-list"; }
virtual bool accepts_drag(const ModelIndex&, const StringView& data_type) override;
virtual bool is_column_sortable(int column_index) const override { return column_index != Column::Icon; }
+ virtual bool is_editable(const ModelIndex&) const override;
+ virtual void set_data(const ModelIndex&, const Variant&) override;
static String timestamp_string(time_t timestamp)
{