2019-02-09 11:22:04 +03:00
|
|
|
#include <LibGUI/GWindow.h>
|
|
|
|
#include <LibGUI/GWidget.h>
|
2019-02-10 13:07:13 +03:00
|
|
|
#include <LibGUI/GBoxLayout.h>
|
2019-02-11 16:56:23 +03:00
|
|
|
#include <LibGUI/GApplication.h>
|
2019-02-10 13:07:13 +03:00
|
|
|
#include <LibGUI/GStatusBar.h>
|
2019-03-20 20:12:56 +03:00
|
|
|
#include <LibGUI/GTextEditor.h>
|
2019-02-20 04:39:46 +03:00
|
|
|
#include <LibGUI/GToolBar.h>
|
2019-02-14 10:52:12 +03:00
|
|
|
#include <LibGUI/GMenuBar.h>
|
|
|
|
#include <LibGUI/GAction.h>
|
2019-03-21 00:01:02 +03:00
|
|
|
#include <LibGUI/GLabel.h>
|
2019-03-21 00:31:21 +03:00
|
|
|
#include <LibGUI/GInputBox.h>
|
|
|
|
#include <LibGUI/GMessageBox.h>
|
2019-03-25 06:25:25 +03:00
|
|
|
#include <LibGUI/GProgressBar.h>
|
2019-03-29 06:58:15 +03:00
|
|
|
#include <LibGUI/GTreeView.h>
|
2019-03-29 19:03:30 +03:00
|
|
|
#include <LibGUI/GFileSystemModel.h>
|
2019-03-30 15:53:30 +03:00
|
|
|
#include <LibGUI/GSplitter.h>
|
2019-05-08 23:41:19 +03:00
|
|
|
#include <AK/FileSystemPath.h>
|
2019-02-09 11:22:04 +03:00
|
|
|
#include <unistd.h>
|
2019-03-01 17:47:07 +03:00
|
|
|
#include <signal.h>
|
2019-02-09 11:22:04 +03:00
|
|
|
#include <stdio.h>
|
2019-04-02 15:43:56 +03:00
|
|
|
#include "DirectoryView.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-02-11 16:56:23 +03:00
|
|
|
GApplication app(argc, argv);
|
2019-02-09 11:22:04 +03:00
|
|
|
|
2019-03-02 11:16:57 +03:00
|
|
|
auto* window = new GWindow;
|
|
|
|
window->set_title("FileManager");
|
|
|
|
window->set_rect(20, 200, 640, 480);
|
2019-03-19 02:01:02 +03:00
|
|
|
window->set_should_exit_event_loop_on_close(true);
|
2019-03-02 11:16:57 +03:00
|
|
|
|
|
|
|
auto* widget = new GWidget;
|
|
|
|
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-03-03 02:34:40 +03:00
|
|
|
auto* main_toolbar = new GToolBar(widget);
|
|
|
|
auto* location_toolbar = new GToolBar(widget);
|
2019-03-27 22:48:23 +03:00
|
|
|
location_toolbar->layout()->set_margins({ 6, 3, 6, 3 });
|
2019-03-29 03:57:29 +03:00
|
|
|
location_toolbar->set_preferred_size({ 0, 25 });
|
2019-03-21 00:01:02 +03:00
|
|
|
|
|
|
|
auto* location_label = new GLabel("Location: ", location_toolbar);
|
|
|
|
location_label->size_to_fit();
|
|
|
|
|
2019-03-20 20:12:56 +03:00
|
|
|
auto* location_textbox = new GTextEditor(GTextEditor::SingleLine, location_toolbar);
|
2019-03-03 02:34:40 +03:00
|
|
|
|
2019-03-30 15:53:30 +03:00
|
|
|
auto* splitter = new GSplitter(Orientation::Horizontal, widget);
|
2019-03-29 06:58:15 +03:00
|
|
|
auto* tree_view = new GTreeView(splitter);
|
2019-03-30 04:22:38 +03:00
|
|
|
auto file_system_model = GFileSystemModel::create("/", GFileSystemModel::Mode::DirectoriesOnly);
|
|
|
|
tree_view->set_model(file_system_model.copy_ref());
|
2019-03-29 16:46:53 +03:00
|
|
|
tree_view->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
|
|
|
|
tree_view->set_preferred_size({ 200, 0 });
|
2019-03-29 06:58:15 +03:00
|
|
|
auto* directory_view = new DirectoryView(splitter);
|
|
|
|
|
2019-03-02 11:16:57 +03:00
|
|
|
auto* statusbar = new GStatusBar(widget);
|
|
|
|
|
2019-03-25 06:25:25 +03:00
|
|
|
auto* progressbar = new GProgressBar(statusbar);
|
|
|
|
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-04-10 04:08:29 +03:00
|
|
|
location_textbox->on_return_pressed = [directory_view, location_textbox] {
|
|
|
|
directory_view->open(location_textbox->text());
|
2019-03-03 02:34:40 +03:00
|
|
|
};
|
|
|
|
|
2019-03-30 04:22:38 +03:00
|
|
|
file_system_model->on_selection_changed = [&] (auto& index) {
|
|
|
|
directory_view->open(file_system_model->path(index));
|
|
|
|
};
|
|
|
|
|
2019-03-27 22:48:23 +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"), [directory_view] (const GAction&) {
|
2019-03-23 05:53:51 +03:00
|
|
|
directory_view->open_parent_directory();
|
2019-03-02 04:20:11 +03:00
|
|
|
});
|
|
|
|
|
2019-03-22 02:19:53 +03:00
|
|
|
auto mkdir_action = GAction::create("New directory...", GraphicsBitmap::load_from_file("/res/icons/16x16/mkdir.png"), [&] (const GAction&) {
|
2019-03-21 00:31:21 +03:00
|
|
|
GInputBox input_box("Enter name:", "New directory", window);
|
|
|
|
if (input_box.exec() == GInputBox::ExecOK && !input_box.text_value().is_empty()) {
|
2019-05-08 23:41:19 +03:00
|
|
|
auto new_dir_path = FileSystemPath(String::format("%s/%s",
|
2019-03-23 05:53:51 +03:00
|
|
|
directory_view->path().characters(),
|
2019-03-21 00:31:21 +03:00
|
|
|
input_box.text_value().characters()
|
2019-05-08 23:41:19 +03:00
|
|
|
)).string();
|
2019-03-21 00:31:21 +03:00
|
|
|
int rc = mkdir(new_dir_path.characters(), 0777);
|
|
|
|
if (rc < 0) {
|
2019-05-08 23:41:19 +03:00
|
|
|
GMessageBox::show(String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, window);
|
2019-03-21 00:31:21 +03:00
|
|
|
} else {
|
2019-03-23 05:53:51 +03:00
|
|
|
directory_view->refresh();
|
2019-03-21 00:31:21 +03:00
|
|
|
}
|
|
|
|
}
|
2019-02-20 04:39:46 +03:00
|
|
|
});
|
|
|
|
|
2019-04-26 22:09:56 +03:00
|
|
|
RetainPtr<GAction> view_as_table_action;
|
|
|
|
RetainPtr<GAction> view_as_icons_action;
|
|
|
|
|
|
|
|
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_icons_action->set_checked(false);
|
|
|
|
view_as_table_action->set_checked(true);
|
2019-03-23 05:53:51 +03:00
|
|
|
});
|
2019-04-26 22:09:56 +03:00
|
|
|
view_as_table_action->set_checkable(true);
|
|
|
|
view_as_table_action->set_checked(false);
|
2019-03-23 05:53:51 +03:00
|
|
|
|
2019-04-26 22:09:56 +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_table_action->set_checked(false);
|
|
|
|
view_as_icons_action->set_checked(true);
|
2019-03-23 05:53:51 +03:00
|
|
|
});
|
2019-04-26 22:09:56 +03:00
|
|
|
view_as_icons_action->set_checkable(true);
|
|
|
|
view_as_icons_action->set_checked(true);
|
2019-03-23 05:53:51 +03:00
|
|
|
|
2019-03-27 22:48:23 +03:00
|
|
|
auto copy_action = GAction::create("Copy", GraphicsBitmap::load_from_file("/res/icons/16x16/edit-copy.png"), [] (const GAction&) {
|
2019-02-20 04:39:46 +03:00
|
|
|
dbgprintf("'Copy' action activated!\n");
|
|
|
|
});
|
|
|
|
|
2019-03-22 02:19:53 +03:00
|
|
|
auto delete_action = GAction::create("Delete", GraphicsBitmap::load_from_file("/res/icons/16x16/delete.png"), [] (const GAction&) {
|
2019-02-20 04:39:46 +03:00
|
|
|
dbgprintf("'Delete' action activated!\n");
|
|
|
|
});
|
|
|
|
|
2019-03-28 05:38:23 +03:00
|
|
|
auto go_back_action = GAction::create("Go Back", GraphicsBitmap::load_from_file("/res/icons/16x16/go-back.png"), [] (const GAction&) {
|
|
|
|
dbgprintf("'Go Back' action activated!\n");
|
|
|
|
});
|
|
|
|
|
|
|
|
auto go_forward_action = GAction::create("Go Forward", GraphicsBitmap::load_from_file("/res/icons/16x16/go-forward.png"), [] (const GAction&) {
|
|
|
|
dbgprintf("'Go Forward' action activated!\n");
|
|
|
|
});
|
|
|
|
|
2019-02-14 10:52:12 +03:00
|
|
|
auto menubar = make<GMenuBar>();
|
|
|
|
|
|
|
|
auto app_menu = make<GMenu>("FileManager");
|
2019-03-03 04:52:22 +03:00
|
|
|
app_menu->add_action(GAction::create("Quit", { Mod_Alt, Key_F4 }, [] (const GAction&) {
|
2019-02-17 11:58:35 +03:00
|
|
|
GApplication::the().quit(0);
|
2019-02-14 10:52:12 +03:00
|
|
|
return;
|
|
|
|
}));
|
|
|
|
menubar->add_menu(move(app_menu));
|
|
|
|
|
|
|
|
auto file_menu = make<GMenu>("File");
|
2019-02-20 04:39:46 +03:00
|
|
|
file_menu->add_action(mkdir_action.copy_ref());
|
|
|
|
file_menu->add_action(copy_action.copy_ref());
|
|
|
|
file_menu->add_action(delete_action.copy_ref());
|
2019-02-14 10:52:12 +03:00
|
|
|
menubar->add_menu(move(file_menu));
|
|
|
|
|
2019-03-23 05:53:51 +03:00
|
|
|
auto view_menu = make<GMenu>("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-03-28 05:38:23 +03:00
|
|
|
auto go_menu = make<GMenu>("Go");
|
|
|
|
go_menu->add_action(go_back_action.copy_ref());
|
|
|
|
go_menu->add_action(go_forward_action.copy_ref());
|
|
|
|
go_menu->add_action(open_parent_directory_action.copy_ref());
|
|
|
|
|
2019-02-14 10:52:12 +03:00
|
|
|
auto help_menu = make<GMenu>("Help");
|
2019-02-20 04:39:46 +03:00
|
|
|
help_menu->add_action(GAction::create("About", [] (const GAction&) {
|
2019-02-14 10:52:12 +03:00
|
|
|
dbgprintf("FIXME: Implement Help/About\n");
|
|
|
|
}));
|
|
|
|
menubar->add_menu(move(help_menu));
|
|
|
|
|
|
|
|
app.set_menubar(move(menubar));
|
|
|
|
|
2019-03-28 05:38:23 +03:00
|
|
|
main_toolbar->add_action(go_back_action.copy_ref());
|
|
|
|
main_toolbar->add_action(go_forward_action.copy_ref());
|
2019-03-03 02:34:40 +03:00
|
|
|
main_toolbar->add_action(open_parent_directory_action.copy_ref());
|
2019-03-28 05:38:23 +03:00
|
|
|
|
|
|
|
main_toolbar->add_separator();
|
2019-03-03 02:34:40 +03:00
|
|
|
main_toolbar->add_action(mkdir_action.copy_ref());
|
|
|
|
main_toolbar->add_action(copy_action.copy_ref());
|
|
|
|
main_toolbar->add_action(delete_action.copy_ref());
|
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-03-30 05:27:25 +03:00
|
|
|
directory_view->on_path_change = [window, location_textbox, &file_system_model, tree_view] (const String& new_path) {
|
2019-02-09 11:22:04 +03:00
|
|
|
window->set_title(String::format("FileManager: %s", new_path.characters()));
|
2019-03-03 02:34:40 +03:00
|
|
|
location_textbox->set_text(new_path);
|
2019-03-30 05:27:25 +03:00
|
|
|
file_system_model->set_selected_index(file_system_model->index(new_path));
|
|
|
|
tree_view->scroll_into_view(file_system_model->selected_index(), Orientation::Vertical);
|
|
|
|
tree_view->update();
|
2019-02-09 11:22:04 +03:00
|
|
|
};
|
|
|
|
|
2019-03-23 05:53:51 +03:00
|
|
|
directory_view->on_status_message = [statusbar] (String message) {
|
2019-02-10 13:07:13 +03:00
|
|
|
statusbar->set_text(move(message));
|
|
|
|
};
|
|
|
|
|
2019-03-25 06:25:25 +03:00
|
|
|
directory_view->on_thumbnail_progress = [&] (int done, int total) {
|
|
|
|
if (done == total) {
|
|
|
|
progressbar->set_visible(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
progressbar->set_range(0, total);
|
|
|
|
progressbar->set_value(done);
|
|
|
|
progressbar->set_visible(true);
|
|
|
|
};
|
|
|
|
|
2019-03-23 05:53:51 +03:00
|
|
|
directory_view->open("/");
|
|
|
|
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-04-23 21:42:47 +03:00
|
|
|
window->set_icon_path("/res/icons/16x16/filetype-folder.png");
|
|
|
|
|
2019-02-20 04:39:46 +03:00
|
|
|
return app.exec();
|
|
|
|
}
|