Skip to content

Commit

Permalink
FileManager+LibGUI: Fix crash when opening a folder, as well as when …
Browse files Browse the repository at this point in the history
…trying to open a newly created folder.
  • Loading branch information
xeons committed Sep 16, 2019
1 parent d754ac5 commit d74a55a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
16 changes: 16 additions & 0 deletions Applications/FileManager/main.cpp
Expand Up @@ -104,6 +104,22 @@ int main(int argc, char** argv)
if (rc < 0) {
GMessageBox::show(String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window);
} else {
file_system_model->update();

auto current_path = directory_view->path();

// not exactly sure why i have to reselect the root node first, but the index() fails if I dont
auto root_index = file_system_model->index(file_system_model->root_path());
tree_view->selection().set(root_index);
tree_view->scroll_into_view(root_index, Orientation::Vertical);
tree_view->update();

// reselect the existing folder in the tree
auto new_index = file_system_model->index(current_path);
tree_view->selection().set(new_index);
tree_view->scroll_into_view(new_index, Orientation::Vertical);
tree_view->update();

directory_view->refresh();
}
}
Expand Down
22 changes: 19 additions & 3 deletions Libraries/LibGUI/GFileSystemModel.cpp
Expand Up @@ -31,6 +31,15 @@ struct GFileSystemModel::Node {
ASSERT_NOT_REACHED();
}

void cleanup()
{
for(auto &child: children) {
child->cleanup();
delete child;
}
children.clear();
}

void traverse_if_needed(const GFileSystemModel& model)
{
if (type != Node::Directory || has_traversed)
Expand Down Expand Up @@ -146,15 +155,22 @@ GFileSystemModel::~GFileSystemModel()

void GFileSystemModel::update()
{
// FIXME: Support refreshing the model!
if (m_root)
return;
cleanup();

m_root = new Node;
m_root->name = m_root_path;
m_root->reify_if_needed(*this);
}

void GFileSystemModel::cleanup()
{
if (m_root) {
m_root->cleanup();
delete m_root;
m_root = nullptr;
}
}

int GFileSystemModel::row_count(const GModelIndex& index) const
{
if (!index.is_valid())
Expand Down
1 change: 1 addition & 0 deletions Libraries/LibGUI/GFileSystemModel.h
Expand Up @@ -37,6 +37,7 @@ class GFileSystemModel : public GModel {

struct Node;
Node* m_root { nullptr };
void cleanup();

GIcon m_open_folder_icon;
GIcon m_closed_folder_icon;
Expand Down
14 changes: 12 additions & 2 deletions Libraries/LibGUI/GModelSelection.h
Expand Up @@ -33,17 +33,27 @@ class GModelSelection {
template<typename Callback>
void for_each_index(Callback callback)
{
for (auto& index : m_indexes)
for (auto& index : indexes())
callback(index);
}

template<typename Callback>
void for_each_index(Callback callback) const
{
for (auto& index : m_indexes)
for (auto& index : indexes())
callback(index);
}

Vector<GModelIndex> indexes() const
{
Vector<GModelIndex> selected_indexes;

for (auto& index : m_indexes)
selected_indexes.append(index);

return selected_indexes;
}

// FIXME: This doesn't guarantee that what you get is the lowest or "first" index selected..
GModelIndex first() const
{
Expand Down

0 comments on commit d74a55a

Please sign in to comment.