2019-06-07 12:48:03 +03:00
|
|
|
#include "DirectoryView.h"
|
2019-09-10 16:21:58 +03:00
|
|
|
#include "FileUtils.h"
|
2019-11-20 23:52:15 +03:00
|
|
|
#include "PropertiesDialog.h"
|
2019-06-07 12:48:03 +03:00
|
|
|
#include <AK/FileSystemPath.h>
|
2019-09-10 16:21:58 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2019-11-04 19:27:56 +03:00
|
|
|
#include <LibCore/CConfigFile.h>
|
2019-06-07 12:48:03 +03:00
|
|
|
#include <LibCore/CUserInfo.h>
|
2019-07-28 11:18:49 +03:00
|
|
|
#include <LibDraw/PNGLoader.h>
|
2019-12-31 01:12:52 +03:00
|
|
|
#include <LibGUI/GAboutDialog.h>
|
2019-02-14 10:52:12 +03:00
|
|
|
#include <LibGUI/GAction.h>
|
2019-07-09 23:10:03 +03:00
|
|
|
#include <LibGUI/GActionGroup.h>
|
2019-06-07 12:48:03 +03:00
|
|
|
#include <LibGUI/GApplication.h>
|
|
|
|
#include <LibGUI/GBoxLayout.h>
|
2019-09-10 16:21:58 +03:00
|
|
|
#include <LibGUI/GClipboard.h>
|
2019-06-07 12:48:03 +03:00
|
|
|
#include <LibGUI/GFileSystemModel.h>
|
2019-03-21 00:31:21 +03:00
|
|
|
#include <LibGUI/GInputBox.h>
|
2019-06-07 12:48:03 +03:00
|
|
|
#include <LibGUI/GLabel.h>
|
|
|
|
#include <LibGUI/GMenuBar.h>
|
2019-03-21 00:31:21 +03:00
|
|
|
#include <LibGUI/GMessageBox.h>
|
2019-03-25 06:25:25 +03:00
|
|
|
#include <LibGUI/GProgressBar.h>
|
2019-03-30 15:53:30 +03:00
|
|
|
#include <LibGUI/GSplitter.h>
|
2019-06-07 12:48:03 +03:00
|
|
|
#include <LibGUI/GStatusBar.h>
|
|
|
|
#include <LibGUI/GTextEditor.h>
|
|
|
|
#include <LibGUI/GToolBar.h>
|
|
|
|
#include <LibGUI/GTreeView.h>
|
|
|
|
#include <LibGUI/GWidget.h>
|
|
|
|
#include <LibGUI/GWindow.h>
|
2019-03-01 17:47:07 +03:00
|
|
|
#include <signal.h>
|
2019-02-09 11:22:04 +03:00
|
|
|
#include <stdio.h>
|
2019-06-07 12:48:03 +03:00
|
|
|
#include <unistd.h>
|
2019-02-09 11:22:04 +03:00
|
|
|
|
2019-02-11 16:56:23 +03:00
|
|
|
int main(int argc, char** argv)
|
2019-02-09 11:22:04 +03:00
|
|
|
{
|
2019-03-01 17:47:07 +03:00
|
|
|
struct sigaction act;
|
|
|
|
memset(&act, 0, sizeof(act));
|
|
|
|
act.sa_flags = SA_NOCLDWAIT;
|
|
|
|
act.sa_handler = SIG_IGN;
|
|
|
|
int rc = sigaction(SIGCHLD, &act, nullptr);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("sigaction");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-11-04 19:27:56 +03:00
|
|
|
RefPtr<CConfigFile> config = CConfigFile::get_for_app("FileManager");
|
|
|
|
|
2019-02-11 16:56:23 +03:00
|
|
|
GApplication app(argc, argv);
|
2019-02-09 11:22:04 +03:00
|
|
|
|
2019-09-21 19:34:06 +03:00
|
|
|
auto window = GWindow::construct();
|
2019-05-27 14:52:28 +03:00
|
|
|
window->set_title("File Manager");
|
2019-11-20 23:52:15 +03:00
|
|
|
|
2019-11-09 12:29:03 +03:00
|
|
|
auto left = config->read_num_entry("Window", "Left", 150);
|
|
|
|
auto top = config->read_num_entry("Window", "Top", 75);
|
|
|
|
auto width = config->read_num_entry("Window", "Width", 640);
|
|
|
|
auto heigth = config->read_num_entry("Window", "Heigth", 480);
|
2019-11-20 23:52:15 +03:00
|
|
|
window->set_rect({ left, top, width, heigth });
|
|
|
|
|
2019-09-21 18:05:35 +03:00
|
|
|
auto widget = GWidget::construct();
|
2019-03-02 11:16:57 +03:00
|
|
|
widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
|
2019-05-11 03:35:03 +03:00
|
|
|
widget->layout()->set_spacing(0);
|
2019-03-02 11:16:57 +03:00
|
|
|
|
2019-09-21 17:27:54 +03:00
|
|
|
auto main_toolbar = GToolBar::construct(widget);
|
|
|
|
auto location_toolbar = GToolBar::construct(widget);
|
2019-03-27 22:48:23 +03:00
|
|
|
location_toolbar->layout()->set_margins({ 6, 3, 6, 3 });
|
2019-07-20 23:39:24 +03:00
|
|
|
location_toolbar->set_preferred_size(0, 25);
|
2019-03-21 00:01:02 +03:00
|
|
|
|
2019-09-21 15:19:05 +03:00
|
|
|
auto location_label = GLabel::construct("Location: ", location_toolbar);
|
2019-03-21 00:01:02 +03:00
|
|
|
location_label->size_to_fit();
|
|
|
|
|
2019-09-21 16:43:52 +03:00
|
|
|
auto location_textbox = GTextEditor::construct(GTextEditor::SingleLine, location_toolbar);
|
2019-03-03 02:34:40 +03:00
|
|
|
|
2019-09-21 17:11:02 +03:00
|
|
|
auto splitter = GSplitter::construct(Orientation::Horizontal, widget);
|
2019-09-21 17:06:43 +03:00
|
|
|
auto tree_view = GTreeView::construct(splitter);
|
2020-01-10 18:58:00 +03:00
|
|
|
auto directories_model = GFileSystemModel::create("/", GFileSystemModel::Mode::DirectoriesOnly);
|
|
|
|
tree_view->set_model(directories_model);
|
|
|
|
tree_view->set_column_hidden(GFileSystemModel::Column::Icon, true);
|
|
|
|
tree_view->set_column_hidden(GFileSystemModel::Column::Size, true);
|
|
|
|
tree_view->set_column_hidden(GFileSystemModel::Column::Owner, true);
|
|
|
|
tree_view->set_column_hidden(GFileSystemModel::Column::Group, true);
|
|
|
|
tree_view->set_column_hidden(GFileSystemModel::Column::Permissions, true);
|
|
|
|
tree_view->set_column_hidden(GFileSystemModel::Column::ModificationTime, true);
|
|
|
|
tree_view->set_column_hidden(GFileSystemModel::Column::Inode, true);
|
2019-03-29 16:46:53 +03:00
|
|
|
tree_view->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
|
2020-01-10 18:58:00 +03:00
|
|
|
tree_view->set_preferred_size(150, 0);
|
2019-09-21 21:04:00 +03:00
|
|
|
auto directory_view = DirectoryView::construct(splitter);
|
2019-03-29 06:58:15 +03:00
|
|
|
|
2019-09-21 17:29:47 +03:00
|
|
|
auto statusbar = GStatusBar::construct(widget);
|
2019-03-02 11:16:57 +03:00
|
|
|
|
2019-09-21 17:31:12 +03:00
|
|
|
auto progressbar = GProgressBar::construct(statusbar);
|
2019-03-25 06:25:25 +03:00
|
|
|
progressbar->set_caption("Generating thumbnails: ");
|
|
|
|
progressbar->set_format(GProgressBar::Format::ValueSlashMax);
|
|
|
|
progressbar->set_visible(false);
|
2019-04-10 04:43:46 +03:00
|
|
|
progressbar->set_frame_shape(FrameShape::Panel);
|
|
|
|
progressbar->set_frame_shadow(FrameShadow::Sunken);
|
2019-03-30 15:12:59 +03:00
|
|
|
progressbar->set_frame_thickness(1);
|
2019-03-25 06:25:25 +03:00
|
|
|
|
2019-09-21 21:04:00 +03:00
|
|
|
location_textbox->on_return_pressed = [&] {
|
2019-04-10 04:08:29 +03:00
|
|
|
directory_view->open(location_textbox->text());
|
2019-03-03 02:34:40 +03:00
|
|
|
};
|
|
|
|
|
2019-12-03 03:54:29 +03:00
|
|
|
auto refresh_tree_view = [&] {
|
2020-01-10 18:58:00 +03:00
|
|
|
directories_model->update();
|
2019-12-03 03:54:29 +03:00
|
|
|
|
|
|
|
auto current_path = directory_view->path();
|
|
|
|
|
2019-12-12 12:49:17 +03:00
|
|
|
struct stat st;
|
|
|
|
// If the directory no longer exists, we find a parent that does.
|
|
|
|
while (lstat(current_path.characters(), &st) != 0) {
|
|
|
|
directory_view->open_parent_directory();
|
|
|
|
current_path = directory_view->path();
|
2020-01-10 18:58:00 +03:00
|
|
|
if (current_path == directories_model->root_path()) {
|
2019-12-12 12:49:17 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:58:00 +03:00
|
|
|
// Reselect the existing folder in the tree.
|
|
|
|
auto new_index = directories_model->index(current_path, GFileSystemModel::Column::Name);
|
2019-12-03 03:54:29 +03:00
|
|
|
tree_view->selection().set(new_index);
|
|
|
|
tree_view->scroll_into_view(new_index, Orientation::Vertical);
|
|
|
|
tree_view->update();
|
|
|
|
|
|
|
|
directory_view->refresh();
|
|
|
|
};
|
|
|
|
|
2019-12-12 12:49:17 +03:00
|
|
|
auto directory_context_menu = GMenu::construct("Directory View Directory");
|
|
|
|
auto file_context_menu = GMenu::construct("Directory View File");
|
|
|
|
auto directory_view_context_menu = GMenu::construct("Directory View");
|
|
|
|
auto tree_view_directory_context_menu = GMenu::construct("Tree View Directory");
|
|
|
|
auto tree_view_context_menu = GMenu::construct("Tree View");
|
|
|
|
|
2019-09-21 21:04:00 +03:00
|
|
|
auto open_parent_directory_action = GAction::create("Open parent directory", { Mod_Alt, Key_Up }, GraphicsBitmap::load_from_file("/res/icons/16x16/open-parent-directory.png"), [&](const GAction&) {
|
2019-03-23 05:53:51 +03:00
|
|
|
directory_view->open_parent_directory();
|
2019-03-02 04:20:11 +03:00
|
|
|
});
|
|
|
|
|
2020-01-01 02:55:36 +03:00
|
|
|
auto mkdir_action = GAction::create("New directory...", { Mod_Ctrl | Mod_Shift, Key_N }, GraphicsBitmap::load_from_file("/res/icons/16x16/mkdir.png"), [&](const GAction&) {
|
2019-09-21 21:32:31 +03:00
|
|
|
auto input_box = GInputBox::construct("Enter name:", "New directory", window);
|
|
|
|
if (input_box->exec() == GInputBox::ExecOK && !input_box->text_value().is_empty()) {
|
2019-07-15 07:49:28 +03:00
|
|
|
auto new_dir_path = canonicalized_path(
|
|
|
|
String::format("%s/%s",
|
|
|
|
directory_view->path().characters(),
|
2019-09-21 21:32:31 +03:00
|
|
|
input_box->text_value().characters()));
|
2019-03-21 00:31:21 +03:00
|
|
|
int rc = mkdir(new_dir_path.characters(), 0777);
|
|
|
|
if (rc < 0) {
|
2019-07-16 22:32:10 +03:00
|
|
|
GMessageBox::show(String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window);
|
2019-03-21 00:31:21 +03:00
|
|
|
} else {
|
2019-12-03 03:54:29 +03:00
|
|
|
refresh_tree_view();
|
2019-03-21 00:31:21 +03:00
|
|
|
}
|
|
|
|
}
|
2019-02-20 04:39:46 +03:00
|
|
|
});
|
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr<GAction> view_as_table_action;
|
|
|
|
RefPtr<GAction> view_as_icons_action;
|
2019-04-26 22:09:56 +03:00
|
|
|
|
2019-06-07 12:48:03 +03:00
|
|
|
view_as_table_action = GAction::create("Table view", { Mod_Ctrl, KeyCode::Key_L }, GraphicsBitmap::load_from_file("/res/icons/16x16/table-view.png"), [&](const GAction&) {
|
2019-03-23 05:53:51 +03:00
|
|
|
directory_view->set_view_mode(DirectoryView::ViewMode::List);
|
2019-04-26 22:09:56 +03:00
|
|
|
view_as_table_action->set_checked(true);
|
2019-11-04 19:27:56 +03:00
|
|
|
|
|
|
|
config->write_entry("DirectoryView", "ViewMode", "List");
|
|
|
|
config->sync();
|
2019-03-23 05:53:51 +03:00
|
|
|
});
|
2019-04-26 22:09:56 +03:00
|
|
|
view_as_table_action->set_checkable(true);
|
2019-03-23 05:53:51 +03:00
|
|
|
|
2019-06-07 12:48:03 +03:00
|
|
|
view_as_icons_action = GAction::create("Icon view", { Mod_Ctrl, KeyCode::Key_I }, GraphicsBitmap::load_from_file("/res/icons/16x16/icon-view.png"), [&](const GAction&) {
|
2019-03-23 05:53:51 +03:00
|
|
|
directory_view->set_view_mode(DirectoryView::ViewMode::Icon);
|
2019-04-26 22:09:56 +03:00
|
|
|
view_as_icons_action->set_checked(true);
|
2019-11-04 19:27:56 +03:00
|
|
|
|
|
|
|
config->write_entry("DirectoryView", "ViewMode", "Icon");
|
|
|
|
config->sync();
|
2019-03-23 05:53:51 +03:00
|
|
|
});
|
2019-04-26 22:09:56 +03:00
|
|
|
view_as_icons_action->set_checkable(true);
|
2019-07-09 23:10:03 +03:00
|
|
|
|
|
|
|
auto view_type_action_group = make<GActionGroup>();
|
|
|
|
view_type_action_group->set_exclusive(true);
|
|
|
|
view_type_action_group->add_action(*view_as_table_action);
|
|
|
|
view_type_action_group->add_action(*view_as_icons_action);
|
2019-11-20 23:52:15 +03:00
|
|
|
|
2019-09-18 22:46:14 +03:00
|
|
|
auto selected_file_paths = [&] {
|
|
|
|
Vector<String> paths;
|
|
|
|
auto& view = directory_view->current_view();
|
|
|
|
auto& model = *view.model();
|
|
|
|
view.selection().for_each_index([&](const GModelIndex& index) {
|
2020-01-10 18:58:00 +03:00
|
|
|
auto parent_index = model.parent_index(index);
|
|
|
|
auto name_index = model.index(index.row(), GFileSystemModel::Column::Name, parent_index);
|
2019-09-18 22:46:14 +03:00
|
|
|
auto path = model.data(name_index, GModel::Role::Custom).to_string();
|
|
|
|
paths.append(path);
|
|
|
|
});
|
|
|
|
return paths;
|
|
|
|
};
|
|
|
|
|
2019-12-12 12:49:17 +03:00
|
|
|
auto tree_view_selected_file_paths = [&] {
|
|
|
|
Vector<String> paths;
|
|
|
|
auto& view = tree_view;
|
|
|
|
view->selection().for_each_index([&](const GModelIndex& index) {
|
2020-01-10 18:58:00 +03:00
|
|
|
paths.append(directories_model->full_path(index));
|
2019-12-12 12:49:17 +03:00
|
|
|
});
|
|
|
|
return paths;
|
|
|
|
};
|
|
|
|
|
2020-01-07 12:29:21 +03:00
|
|
|
auto select_all_action = GAction::create("Select all", { Mod_Ctrl, KeyCode::Key_A }, [&](const GAction&) {
|
2020-01-07 13:12:33 +03:00
|
|
|
directory_view->current_view().select_all();
|
2020-01-07 12:29:21 +03:00
|
|
|
});
|
|
|
|
|
2019-12-12 12:49:17 +03:00
|
|
|
auto copy_action = GCommonActions::make_copy_action([&](const GAction& action) {
|
|
|
|
Vector<String> paths;
|
2019-12-13 01:15:17 +03:00
|
|
|
if (action.activator() == directory_context_menu || directory_view->active_widget()->is_focused()) {
|
2019-12-12 12:49:17 +03:00
|
|
|
paths = selected_file_paths();
|
|
|
|
} else {
|
|
|
|
paths = tree_view_selected_file_paths();
|
|
|
|
}
|
2019-09-18 22:46:14 +03:00
|
|
|
if (paths.is_empty())
|
2019-09-10 16:21:58 +03:00
|
|
|
return;
|
|
|
|
StringBuilder copy_text;
|
2019-09-18 22:46:14 +03:00
|
|
|
for (auto& path : paths) {
|
2019-09-10 16:21:58 +03:00
|
|
|
copy_text.appendf("%s\n", path.characters());
|
|
|
|
}
|
2019-09-14 10:20:20 +03:00
|
|
|
GClipboard::the().set_data(copy_text.build(), "file-list");
|
2019-09-10 16:21:58 +03:00
|
|
|
});
|
2019-09-13 23:09:01 +03:00
|
|
|
copy_action->set_enabled(false);
|
2019-09-10 16:21:58 +03:00
|
|
|
|
2019-09-14 23:10:44 +03:00
|
|
|
auto paste_action = GCommonActions::make_paste_action([&](const GAction&) {
|
2019-09-14 10:20:20 +03:00
|
|
|
auto data_and_type = GClipboard::the().data_and_type();
|
|
|
|
if (data_and_type.type != "file-list") {
|
|
|
|
dbg() << "Cannot paste clipboard type " << data_and_type.type;
|
2019-09-10 16:21:58 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-09-14 10:20:20 +03:00
|
|
|
auto copied_lines = data_and_type.data.split('\n');
|
|
|
|
if (copied_lines.is_empty()) {
|
|
|
|
dbg() << "No files to paste";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (auto& current_path : copied_lines) {
|
2019-09-10 16:21:58 +03:00
|
|
|
if (current_path.is_empty())
|
|
|
|
continue;
|
|
|
|
auto current_directory = directory_view->path();
|
|
|
|
auto new_path = String::format("%s/%s",
|
|
|
|
current_directory.characters(),
|
|
|
|
FileSystemPath(current_path).basename().characters());
|
|
|
|
if (!FileUtils::copy_file_or_directory(current_path, new_path)) {
|
|
|
|
auto error_message = String::format("Could not paste %s.",
|
|
|
|
current_path.characters());
|
|
|
|
GMessageBox::show(error_message, "File Manager", GMessageBox::Type::Error);
|
2019-12-12 12:49:17 +03:00
|
|
|
} else {
|
|
|
|
refresh_tree_view();
|
2019-09-10 16:21:58 +03:00
|
|
|
}
|
|
|
|
}
|
2019-02-20 04:39:46 +03:00
|
|
|
});
|
2019-09-14 10:20:20 +03:00
|
|
|
paste_action->set_enabled(GClipboard::the().type() == "file-list");
|
|
|
|
|
|
|
|
GClipboard::the().on_content_change = [&](const String& data_type) {
|
|
|
|
paste_action->set_enabled(data_type == "file-list");
|
|
|
|
};
|
2019-02-20 04:39:46 +03:00
|
|
|
|
2019-09-14 10:20:20 +03:00
|
|
|
auto properties_action
|
2019-12-12 12:49:17 +03:00
|
|
|
= GAction::create("Properties...", { Mod_Alt, Key_Return }, GraphicsBitmap::load_from_file("/res/icons/16x16/properties.png"), [&](const GAction& action) {
|
2019-11-20 23:52:15 +03:00
|
|
|
auto& model = directory_view->model();
|
2019-12-12 12:49:17 +03:00
|
|
|
String path;
|
|
|
|
Vector<String> selected;
|
2019-12-13 01:15:17 +03:00
|
|
|
if (action.activator() == directory_context_menu || directory_view->active_widget()->is_focused()) {
|
2019-12-12 12:49:17 +03:00
|
|
|
path = directory_view->path();
|
|
|
|
selected = selected_file_paths();
|
|
|
|
} else {
|
2020-01-10 18:58:00 +03:00
|
|
|
path = directories_model->full_path(tree_view->selection().first());
|
2019-12-12 12:49:17 +03:00
|
|
|
selected = tree_view_selected_file_paths();
|
|
|
|
}
|
2020-01-10 18:58:00 +03:00
|
|
|
|
2019-11-20 23:52:15 +03:00
|
|
|
RefPtr<PropertiesDialog> properties;
|
|
|
|
if (selected.is_empty()) {
|
2019-12-12 12:49:17 +03:00
|
|
|
properties = PropertiesDialog::construct(model, path, true, window);
|
2019-11-20 23:52:15 +03:00
|
|
|
} else {
|
|
|
|
properties = PropertiesDialog::construct(model, selected.first(), false, window);
|
|
|
|
}
|
|
|
|
|
|
|
|
properties->exec();
|
|
|
|
});
|
|
|
|
|
2020-01-10 19:06:00 +03:00
|
|
|
enum class ConfirmBeforeDelete {
|
|
|
|
No,
|
|
|
|
Yes
|
|
|
|
};
|
2019-09-18 22:53:47 +03:00
|
|
|
|
2019-12-12 12:49:17 +03:00
|
|
|
auto do_delete = [&](ConfirmBeforeDelete confirm, const GAction& action) {
|
|
|
|
Vector<String> paths;
|
2019-12-13 01:15:17 +03:00
|
|
|
if (action.activator() == directory_context_menu || directory_view->active_widget()->is_focused()) {
|
2019-12-12 12:49:17 +03:00
|
|
|
paths = selected_file_paths();
|
|
|
|
} else {
|
|
|
|
paths = tree_view_selected_file_paths();
|
|
|
|
}
|
2019-09-18 22:46:14 +03:00
|
|
|
if (paths.is_empty())
|
|
|
|
return;
|
|
|
|
{
|
|
|
|
String message;
|
|
|
|
if (paths.size() == 1) {
|
|
|
|
message = String::format("Really delete %s?", FileSystemPath(paths[0]).basename().characters());
|
|
|
|
} else {
|
|
|
|
message = String::format("Really delete %d files?", paths.size());
|
|
|
|
}
|
|
|
|
|
2019-09-18 22:53:47 +03:00
|
|
|
if (confirm == ConfirmBeforeDelete::Yes) {
|
2019-09-21 21:32:31 +03:00
|
|
|
auto result = GMessageBox::show(
|
2019-09-18 22:53:47 +03:00
|
|
|
message,
|
|
|
|
"Confirm deletion",
|
|
|
|
GMessageBox::Type::Warning,
|
|
|
|
GMessageBox::InputType::OKCancel,
|
|
|
|
window);
|
|
|
|
if (result == GMessageBox::ExecCancel)
|
|
|
|
return;
|
|
|
|
}
|
2019-09-18 22:46:14 +03:00
|
|
|
}
|
2019-11-21 23:43:02 +03:00
|
|
|
|
2019-09-18 22:46:14 +03:00
|
|
|
for (auto& path : paths) {
|
2019-11-21 23:43:02 +03:00
|
|
|
struct stat st;
|
|
|
|
if (lstat(path.characters(), &st)) {
|
|
|
|
GMessageBox::show(
|
|
|
|
String::format("lstat(%s) failed: %s", path.characters(), strerror(errno)),
|
|
|
|
"Delete failed",
|
|
|
|
GMessageBox::Type::Error,
|
|
|
|
GMessageBox::InputType::OK,
|
|
|
|
window);
|
|
|
|
break;
|
2019-12-03 03:54:29 +03:00
|
|
|
} else {
|
|
|
|
refresh_tree_view();
|
2019-11-21 23:43:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
String error_path;
|
|
|
|
int error = FileUtils::delete_directory(path, error_path);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
GMessageBox::show(
|
|
|
|
String::format("Failed to delete directory \"%s\": %s", error_path.characters(), strerror(error)),
|
|
|
|
"Delete failed",
|
|
|
|
GMessageBox::Type::Error,
|
|
|
|
GMessageBox::InputType::OK,
|
|
|
|
window);
|
|
|
|
break;
|
2019-12-03 03:54:29 +03:00
|
|
|
} else {
|
|
|
|
refresh_tree_view();
|
2019-11-21 23:43:02 +03:00
|
|
|
}
|
|
|
|
} else if (unlink(path.characters()) < 0) {
|
2019-09-18 22:46:14 +03:00
|
|
|
int saved_errno = errno;
|
|
|
|
GMessageBox::show(
|
|
|
|
String::format("unlink(%s) failed: %s", path.characters(), strerror(saved_errno)),
|
|
|
|
"Delete failed",
|
|
|
|
GMessageBox::Type::Error,
|
|
|
|
GMessageBox::InputType::OK,
|
|
|
|
window);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 22:53:47 +03:00
|
|
|
};
|
|
|
|
|
2019-12-12 12:49:17 +03:00
|
|
|
auto force_delete_action = GAction::create("Delete without confirmation", { Mod_Shift, Key_Delete }, [&](const GAction& action) {
|
|
|
|
do_delete(ConfirmBeforeDelete::No, action);
|
2019-09-18 22:53:47 +03:00
|
|
|
});
|
|
|
|
|
2019-12-12 12:49:17 +03:00
|
|
|
auto delete_action = GCommonActions::make_delete_action([&](const GAction& action) {
|
|
|
|
do_delete(ConfirmBeforeDelete::Yes, action);
|
2019-02-20 04:39:46 +03:00
|
|
|
});
|
2019-09-13 23:09:01 +03:00
|
|
|
delete_action->set_enabled(false);
|
2019-02-20 04:39:46 +03:00
|
|
|
|
2019-10-05 10:21:55 +03:00
|
|
|
auto go_back_action = GCommonActions::make_go_back_action([&](auto&) {
|
2019-05-24 00:15:57 +03:00
|
|
|
directory_view->open_previous_directory();
|
2019-03-28 05:38:23 +03:00
|
|
|
});
|
|
|
|
|
2019-10-05 10:21:55 +03:00
|
|
|
auto go_forward_action = GCommonActions::make_go_forward_action([&](auto&) {
|
2019-05-24 00:15:57 +03:00
|
|
|
directory_view->open_next_directory();
|
2019-03-28 05:38:23 +03:00
|
|
|
});
|
|
|
|
|
2019-10-06 22:59:46 +03:00
|
|
|
auto go_home_action = GCommonActions::make_go_home_action([&](auto&) {
|
2019-07-21 10:19:09 +03:00
|
|
|
directory_view->open(get_current_user_home_path());
|
|
|
|
});
|
|
|
|
|
2019-02-14 10:52:12 +03:00
|
|
|
auto menubar = make<GMenuBar>();
|
|
|
|
|
2019-12-09 23:05:28 +03:00
|
|
|
auto app_menu = GMenu::construct("File Manager");
|
2019-08-26 20:18:54 +03:00
|
|
|
app_menu->add_action(mkdir_action);
|
|
|
|
app_menu->add_action(copy_action);
|
2019-09-10 16:21:58 +03:00
|
|
|
app_menu->add_action(paste_action);
|
2019-08-26 20:18:54 +03:00
|
|
|
app_menu->add_action(delete_action);
|
|
|
|
app_menu->add_separator();
|
2019-09-14 23:10:44 +03:00
|
|
|
app_menu->add_action(GCommonActions::make_quit_action([](auto&) {
|
2019-02-17 11:58:35 +03:00
|
|
|
GApplication::the().quit(0);
|
2019-02-14 10:52:12 +03:00
|
|
|
}));
|
|
|
|
menubar->add_menu(move(app_menu));
|
|
|
|
|
2019-12-09 23:05:28 +03:00
|
|
|
auto view_menu = GMenu::construct("View");
|
2019-04-26 22:09:56 +03:00
|
|
|
view_menu->add_action(*view_as_icons_action);
|
|
|
|
view_menu->add_action(*view_as_table_action);
|
2019-03-23 05:53:51 +03:00
|
|
|
menubar->add_menu(move(view_menu));
|
|
|
|
|
2019-12-09 23:05:28 +03:00
|
|
|
auto go_menu = GMenu::construct("Go");
|
2019-07-11 16:59:06 +03:00
|
|
|
go_menu->add_action(go_back_action);
|
|
|
|
go_menu->add_action(go_forward_action);
|
|
|
|
go_menu->add_action(open_parent_directory_action);
|
2019-08-26 22:20:39 +03:00
|
|
|
go_menu->add_action(go_home_action);
|
2019-05-24 00:15:57 +03:00
|
|
|
menubar->add_menu(move(go_menu));
|
2019-03-28 05:38:23 +03:00
|
|
|
|
2019-12-09 23:05:28 +03:00
|
|
|
auto help_menu = GMenu::construct("Help");
|
2019-12-31 01:12:52 +03:00
|
|
|
help_menu->add_action(GAction::create("About", [&](const GAction&) {
|
|
|
|
GAboutDialog::show("File Manager", load_png("/res/icons/32x32/filetype-folder.png"), window);
|
2019-02-14 10:52:12 +03:00
|
|
|
}));
|
|
|
|
menubar->add_menu(move(help_menu));
|
|
|
|
|
|
|
|
app.set_menubar(move(menubar));
|
|
|
|
|
2019-07-11 16:59:06 +03:00
|
|
|
main_toolbar->add_action(go_back_action);
|
|
|
|
main_toolbar->add_action(go_forward_action);
|
|
|
|
main_toolbar->add_action(open_parent_directory_action);
|
2019-07-21 10:19:09 +03:00
|
|
|
main_toolbar->add_action(go_home_action);
|
2019-03-28 05:38:23 +03:00
|
|
|
|
|
|
|
main_toolbar->add_separator();
|
2019-07-11 16:59:06 +03:00
|
|
|
main_toolbar->add_action(mkdir_action);
|
|
|
|
main_toolbar->add_action(copy_action);
|
2019-09-10 16:21:58 +03:00
|
|
|
main_toolbar->add_action(paste_action);
|
2019-07-11 16:59:06 +03:00
|
|
|
main_toolbar->add_action(delete_action);
|
2019-02-20 04:39:46 +03:00
|
|
|
|
2019-03-25 03:29:45 +03:00
|
|
|
main_toolbar->add_separator();
|
2019-04-26 22:09:56 +03:00
|
|
|
main_toolbar->add_action(*view_as_icons_action);
|
|
|
|
main_toolbar->add_action(*view_as_table_action);
|
2019-03-25 03:29:45 +03:00
|
|
|
|
2019-09-21 16:43:52 +03:00
|
|
|
directory_view->on_path_change = [&](const String& new_path) {
|
2019-05-27 14:52:28 +03:00
|
|
|
window->set_title(String::format("File Manager: %s", new_path.characters()));
|
2019-03-03 02:34:40 +03:00
|
|
|
location_textbox->set_text(new_path);
|
2020-01-10 18:58:00 +03:00
|
|
|
auto new_index = directories_model->index(new_path, GFileSystemModel::Column::Name);
|
2019-12-12 12:49:17 +03:00
|
|
|
if (new_index.is_valid()) {
|
|
|
|
tree_view->selection().set(new_index);
|
|
|
|
tree_view->scroll_into_view(new_index, Orientation::Vertical);
|
|
|
|
tree_view->update();
|
|
|
|
}
|
2019-05-24 00:15:57 +03:00
|
|
|
|
|
|
|
go_forward_action->set_enabled(directory_view->path_history_position()
|
2019-06-07 12:48:03 +03:00
|
|
|
< directory_view->path_history_size() - 1);
|
2019-05-24 00:15:57 +03:00
|
|
|
go_back_action->set_enabled(directory_view->path_history_position() > 0);
|
2019-02-09 11:22:04 +03:00
|
|
|
};
|
|
|
|
|
2019-09-21 17:29:47 +03:00
|
|
|
directory_view->on_status_message = [&](const StringView& message) {
|
2019-06-02 13:26:28 +03:00
|
|
|
statusbar->set_text(message);
|
2019-02-10 13:07:13 +03:00
|
|
|
};
|
|
|
|
|
2019-06-07 12:48:03 +03:00
|
|
|
directory_view->on_thumbnail_progress = [&](int done, int total) {
|
2019-03-25 06:25:25 +03:00
|
|
|
if (done == total) {
|
|
|
|
progressbar->set_visible(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
progressbar->set_range(0, total);
|
|
|
|
progressbar->set_value(done);
|
|
|
|
progressbar->set_visible(true);
|
|
|
|
};
|
|
|
|
|
2019-09-13 23:09:01 +03:00
|
|
|
directory_view->on_selection_change = [&](GAbstractView& view) {
|
|
|
|
// FIXME: Figure out how we can enable/disable the paste action, based on clipboard contents.
|
|
|
|
copy_action->set_enabled(!view.selection().is_empty());
|
|
|
|
delete_action->set_enabled(!view.selection().is_empty());
|
|
|
|
};
|
|
|
|
|
2019-10-07 21:03:19 +03:00
|
|
|
auto open_in_text_editor_action = GAction::create("Open in TextEditor...", GraphicsBitmap::load_from_file("/res/icons/TextEditor16.png"), [&](auto&) {
|
|
|
|
for (auto& path : selected_file_paths()) {
|
|
|
|
if (!fork()) {
|
|
|
|
int rc = execl("/bin/TextEditor", "TextEditor", path.characters(), nullptr);
|
|
|
|
if (rc < 0)
|
|
|
|
perror("execl");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-12-08 08:39:45 +03:00
|
|
|
directory_context_menu->add_action(copy_action);
|
|
|
|
directory_context_menu->add_action(paste_action);
|
|
|
|
directory_context_menu->add_action(delete_action);
|
|
|
|
directory_context_menu->add_separator();
|
|
|
|
directory_context_menu->add_action(properties_action);
|
|
|
|
|
|
|
|
file_context_menu->add_action(copy_action);
|
|
|
|
file_context_menu->add_action(paste_action);
|
|
|
|
file_context_menu->add_action(delete_action);
|
|
|
|
file_context_menu->add_separator();
|
|
|
|
file_context_menu->add_action(open_in_text_editor_action);
|
|
|
|
file_context_menu->add_separator();
|
|
|
|
file_context_menu->add_action(properties_action);
|
|
|
|
|
|
|
|
directory_view_context_menu->add_action(mkdir_action);
|
|
|
|
|
2019-12-12 12:49:17 +03:00
|
|
|
tree_view_directory_context_menu->add_action(copy_action);
|
|
|
|
tree_view_directory_context_menu->add_action(paste_action);
|
|
|
|
tree_view_directory_context_menu->add_action(delete_action);
|
|
|
|
tree_view_directory_context_menu->add_separator();
|
|
|
|
tree_view_directory_context_menu->add_action(properties_action);
|
|
|
|
tree_view_directory_context_menu->add_separator();
|
|
|
|
tree_view_directory_context_menu->add_action(mkdir_action);
|
|
|
|
|
2019-12-08 08:39:45 +03:00
|
|
|
directory_view->on_context_menu_request = [&](const GAbstractView&, const GModelIndex& index, const GContextMenuEvent& event) {
|
|
|
|
if (index.is_valid()) {
|
2020-01-10 18:58:00 +03:00
|
|
|
auto& node = directory_view->model().node(index);
|
2019-12-08 08:39:45 +03:00
|
|
|
|
2020-01-10 18:58:00 +03:00
|
|
|
if (node.is_directory())
|
2019-12-08 08:39:45 +03:00
|
|
|
directory_context_menu->popup(event.screen_position());
|
2020-01-10 18:58:00 +03:00
|
|
|
else
|
2019-12-08 08:39:45 +03:00
|
|
|
file_context_menu->popup(event.screen_position());
|
|
|
|
} else {
|
|
|
|
directory_view_context_menu->popup(event.screen_position());
|
|
|
|
}
|
2019-09-13 23:00:47 +03:00
|
|
|
};
|
|
|
|
|
2019-12-12 12:49:17 +03:00
|
|
|
tree_view->on_selection_change = [&] {
|
2020-01-10 18:58:00 +03:00
|
|
|
auto path = directories_model->full_path(tree_view->selection().first());
|
2019-12-12 12:49:17 +03:00
|
|
|
if (directory_view->path() == path)
|
|
|
|
return;
|
|
|
|
directory_view->open(path);
|
|
|
|
copy_action->set_enabled(!tree_view->selection().is_empty());
|
|
|
|
delete_action->set_enabled(!tree_view->selection().is_empty());
|
|
|
|
};
|
|
|
|
|
|
|
|
tree_view->on_context_menu_request = [&](const GModelIndex& index, const GContextMenuEvent& event) {
|
|
|
|
if (index.is_valid()) {
|
|
|
|
tree_view_directory_context_menu->popup(event.screen_position());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-31 17:23:53 +03:00
|
|
|
// our initial location is defined as, in order of precedence:
|
|
|
|
// 1. the first command-line argument (e.g. FileManager /bin)
|
|
|
|
// 2. the user's home directory
|
|
|
|
// 3. the root directory
|
|
|
|
|
|
|
|
String initial_location;
|
|
|
|
|
|
|
|
if (argc >= 2)
|
|
|
|
initial_location = argv[1];
|
|
|
|
|
|
|
|
if (initial_location.is_empty())
|
|
|
|
initial_location = get_current_user_home_path();
|
|
|
|
|
|
|
|
if (initial_location.is_empty())
|
|
|
|
initial_location = "/";
|
|
|
|
|
|
|
|
directory_view->open(initial_location);
|
2019-03-23 05:53:51 +03:00
|
|
|
directory_view->set_focus(true);
|
2019-02-09 11:22:04 +03:00
|
|
|
|
2019-03-02 11:16:57 +03:00
|
|
|
window->set_main_widget(widget);
|
2019-02-20 04:39:46 +03:00
|
|
|
window->show();
|
2019-02-09 11:22:04 +03:00
|
|
|
|
2019-07-28 11:18:49 +03:00
|
|
|
window->set_icon(load_png("/res/icons/16x16/filetype-folder.png"));
|
2019-04-23 21:42:47 +03:00
|
|
|
|
2019-11-04 19:27:56 +03:00
|
|
|
// Read direcory read mode from config.
|
|
|
|
auto dir_view_mode = config->read_entry("DirectoryView", "ViewMode", "Icon");
|
|
|
|
|
|
|
|
if (dir_view_mode.contains("List")) {
|
|
|
|
directory_view->set_view_mode(DirectoryView::ViewMode::List);
|
|
|
|
view_as_table_action->set_checked(true);
|
|
|
|
} else {
|
|
|
|
directory_view->set_view_mode(DirectoryView::ViewMode::Icon);
|
|
|
|
view_as_icons_action->set_checked(true);
|
|
|
|
}
|
|
|
|
|
2019-11-09 12:29:03 +03:00
|
|
|
// Write window position to config file on close request.
|
|
|
|
window->on_close_request = [&] {
|
|
|
|
config->write_num_entry("Window", "Left", window->x());
|
|
|
|
config->write_num_entry("Window", "Top", window->y());
|
|
|
|
config->write_num_entry("Window", "Width", window->width());
|
|
|
|
config->write_num_entry("Window", "Heigth", window->height());
|
|
|
|
config->sync();
|
|
|
|
|
|
|
|
return GWindow::CloseRequestDecision::Close;
|
|
|
|
};
|
2019-11-20 23:52:15 +03:00
|
|
|
|
2019-02-20 04:39:46 +03:00
|
|
|
return app.exec();
|
|
|
|
}
|