1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:47:35 +00:00

Applications: Move to Userland/Applications/

This commit is contained in:
Andreas Kling 2021-01-12 12:05:23 +01:00
parent aa939c4b4b
commit dc28c07fa5
287 changed files with 1 additions and 1 deletions

View file

@ -0,0 +1,10 @@
set(SOURCES
History.cpp
main.cpp
ManualModel.cpp
ManualPageNode.cpp
ManualSectionNode.cpp
)
serenity_app(Help ICON app-help)
target_link_libraries(Help LibWeb LibMarkdown LibGUI LibDesktop)

View file

@ -0,0 +1,59 @@
/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "History.h"
void History::push(const StringView& history_item)
{
m_items.shrink(m_current_history_item + 1);
m_items.append(history_item);
m_current_history_item++;
}
String History::current()
{
if (m_current_history_item == -1)
return {};
return m_items[m_current_history_item];
}
void History::go_back()
{
ASSERT(can_go_back());
m_current_history_item--;
}
void History::go_forward()
{
ASSERT(can_go_forward());
m_current_history_item++;
}
void History::clear()
{
m_items = {};
m_current_history_item = -1;
}

View file

@ -0,0 +1,48 @@
/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/String.h>
#include <AK/Vector.h>
class History final {
public:
void push(const StringView& history_item);
String current();
void go_back();
void go_forward();
bool can_go_back() { return m_current_history_item > 0; }
bool can_go_forward() { return m_current_history_item + 1 < static_cast<int>(m_items.size()); }
void clear();
private:
Vector<String> m_items;
int m_current_history_item { -1 };
};

View file

@ -0,0 +1,199 @@
/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ManualModel.h"
#include "ManualNode.h"
#include "ManualPageNode.h"
#include "ManualSectionNode.h"
#include <AK/ByteBuffer.h>
#include <LibCore/File.h>
#include <LibGUI/FilteringProxyModel.h>
static ManualSectionNode s_sections[] = {
{ "1", "User programs" },
{ "2", "System calls" },
{ "3", "Libraries" },
{ "4", "Special files" },
{ "5", "File formats" },
{ "6", "Games" },
{ "7", "Miscellanea" },
{ "8", "Sysadmin tools" }
};
ManualModel::ManualModel()
{
m_section_open_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/book-open.png"));
m_section_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/book.png"));
m_page_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-unknown.png"));
}
Optional<GUI::ModelIndex> ManualModel::index_from_path(const StringView& path) const
{
for (int section = 0; section < row_count(); ++section) {
auto parent_index = index(section, 0);
for (int row = 0; row < row_count(parent_index); ++row) {
auto child_index = index(row, 0, parent_index);
auto* node = static_cast<const ManualNode*>(child_index.internal_data());
if (!node->is_page())
continue;
auto* page = static_cast<const ManualPageNode*>(node);
if (page->path() != path)
continue;
return child_index;
}
}
return {};
}
String ManualModel::page_path(const GUI::ModelIndex& index) const
{
if (!index.is_valid())
return {};
auto* node = static_cast<const ManualNode*>(index.internal_data());
if (!node->is_page())
return {};
auto* page = static_cast<const ManualPageNode*>(node);
return page->path();
}
Result<StringView, OSError> ManualModel::page_view(const String& path) const
{
if (path.is_empty())
return StringView {};
{
// Check if we've got it cached already.
auto mapped_file = m_mapped_files.get(path);
if (mapped_file.has_value())
return StringView { mapped_file.value()->bytes() };
}
auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error())
return file_or_error.error();
StringView view { file_or_error.value()->bytes() };
m_mapped_files.set(path, file_or_error.release_value());
return view;
}
String ManualModel::page_and_section(const GUI::ModelIndex& index) const
{
if (!index.is_valid())
return {};
auto* node = static_cast<const ManualNode*>(index.internal_data());
if (!node->is_page())
return {};
auto* page = static_cast<const ManualPageNode*>(node);
auto* section = static_cast<const ManualSectionNode*>(page->parent());
return String::formatted("{}({})", page->name(), section->section_name());
}
GUI::ModelIndex ManualModel::index(int row, int column, const GUI::ModelIndex& parent_index) const
{
if (!parent_index.is_valid())
return create_index(row, column, &s_sections[row]);
auto* parent = static_cast<const ManualNode*>(parent_index.internal_data());
auto* child = &parent->children()[row];
return create_index(row, column, child);
}
GUI::ModelIndex ManualModel::parent_index(const GUI::ModelIndex& index) const
{
if (!index.is_valid())
return {};
auto* child = static_cast<const ManualNode*>(index.internal_data());
auto* parent = child->parent();
if (parent == nullptr)
return {};
if (parent->parent() == nullptr) {
for (size_t row = 0; row < sizeof(s_sections) / sizeof(s_sections[0]); row++)
if (&s_sections[row] == parent)
return create_index(row, 0, parent);
ASSERT_NOT_REACHED();
}
for (size_t row = 0; row < parent->parent()->children().size(); row++) {
ManualNode* child_at_row = &parent->parent()->children()[row];
if (child_at_row == parent)
return create_index(row, 0, parent);
}
ASSERT_NOT_REACHED();
}
int ManualModel::row_count(const GUI::ModelIndex& index) const
{
if (!index.is_valid())
return sizeof(s_sections) / sizeof(s_sections[0]);
auto* node = static_cast<const ManualNode*>(index.internal_data());
return node->children().size();
}
int ManualModel::column_count(const GUI::ModelIndex&) const
{
return 1;
}
GUI::Variant ManualModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
{
auto* node = static_cast<const ManualNode*>(index.internal_data());
switch (role) {
case GUI::ModelRole::Search:
if (!node->is_page())
return {};
return String(page_view(page_path(index)).value());
case GUI::ModelRole::Display:
return node->name();
case GUI::ModelRole::Icon:
if (node->is_page())
return m_page_icon;
if (node->is_open())
return m_section_open_icon;
return m_section_icon;
default:
return {};
}
}
void ManualModel::update_section_node_on_toggle(const GUI::ModelIndex& index, const bool open)
{
auto* node = static_cast<ManualSectionNode*>(index.internal_data());
node->set_open(open);
}
TriState ManualModel::data_matches(const GUI::ModelIndex& index, GUI::Variant term) const
{
auto view_result = page_view(page_path(index));
if (view_result.is_error() || view_result.value().is_empty())
return TriState::False;
return view_result.value().contains(term.as_string()) ? TriState::True : TriState::False;
}
void ManualModel::update()
{
did_update();
}

View file

@ -0,0 +1,66 @@
/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/NonnullRefPtr.h>
#include <AK/Optional.h>
#include <AK/Result.h>
#include <AK/String.h>
#include <LibGUI/Model.h>
class ManualModel final : public GUI::Model {
public:
static NonnullRefPtr<ManualModel> create()
{
return adopt(*new ManualModel);
}
virtual ~ManualModel() override {};
Optional<GUI::ModelIndex> index_from_path(const StringView&) const;
String page_path(const GUI::ModelIndex&) const;
String page_and_section(const GUI::ModelIndex&) const;
Result<StringView, OSError> page_view(const String& path) const;
void update_section_node_on_toggle(const GUI::ModelIndex&, const bool);
virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
virtual TriState data_matches(const GUI::ModelIndex&, GUI::Variant) const override;
virtual void update() override;
virtual GUI::ModelIndex parent_index(const GUI::ModelIndex&) const override;
virtual GUI::ModelIndex index(int row, int column = 0, const GUI::ModelIndex& parent = GUI::ModelIndex()) const override;
private:
ManualModel();
GUI::Icon m_section_open_icon;
GUI::Icon m_section_icon;
GUI::Icon m_page_icon;
mutable HashMap<String, NonnullRefPtr<MappedFile>> m_mapped_files;
};

View file

@ -0,0 +1,41 @@
/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/NonnullOwnPtrVector.h>
#include <AK/String.h>
class ManualNode {
public:
virtual ~ManualNode() { }
virtual NonnullOwnPtrVector<ManualNode>& children() const = 0;
virtual const ManualNode* parent() const = 0;
virtual String name() const = 0;
virtual bool is_page() const { return false; }
virtual bool is_open() const { return false; }
};

View file

@ -0,0 +1,44 @@
/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ManualPageNode.h"
#include "ManualSectionNode.h"
const ManualNode* ManualPageNode::parent() const
{
return &m_section;
}
NonnullOwnPtrVector<ManualNode>& ManualPageNode::children() const
{
static NonnullOwnPtrVector<ManualNode> empty_vector;
return empty_vector;
}
String ManualPageNode::path() const
{
return String::formatted("{}/{}.md", m_section.path(), m_page);
}

View file

@ -0,0 +1,53 @@
/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "ManualNode.h"
class ManualSectionNode;
class ManualPageNode : public ManualNode {
public:
virtual ~ManualPageNode() override { }
ManualPageNode(const ManualSectionNode& section, const StringView& page)
: m_section(section)
, m_page(page)
{
}
virtual NonnullOwnPtrVector<ManualNode>& children() const override;
virtual const ManualNode* parent() const override;
virtual String name() const override { return m_page; };
virtual bool is_page() const override { return true; }
String path() const;
private:
const ManualSectionNode& m_section;
String m_page;
};

View file

@ -0,0 +1,66 @@
/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ManualSectionNode.h"
#include "ManualPageNode.h"
#include <AK/LexicalPath.h>
#include <AK/QuickSort.h>
#include <AK/String.h>
#include <LibCore/DirIterator.h>
String ManualSectionNode::path() const
{
return String::formatted("/usr/share/man/man{}", m_section);
}
void ManualSectionNode::reify_if_needed() const
{
if (m_reified)
return;
m_reified = true;
Core::DirIterator dir_iter { path(), Core::DirIterator::Flags::SkipDots };
Vector<String> page_names;
while (dir_iter.has_next()) {
LexicalPath lexical_path(dir_iter.next_path());
if (lexical_path.extension() != "md")
continue;
page_names.append(lexical_path.title());
}
quick_sort(page_names);
for (auto& page_name : page_names)
m_children.append(make<ManualPageNode>(*this, move(page_name)));
}
void ManualSectionNode::set_open(bool open)
{
if (m_open == open)
return;
m_open = open;
}

View file

@ -0,0 +1,63 @@
/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "ManualNode.h"
class ManualSectionNode : public ManualNode {
public:
virtual ~ManualSectionNode() override { }
ManualSectionNode(String section, String name)
: m_section(section)
, m_full_name(String::formatted("{}. {}", section, name))
{
}
virtual NonnullOwnPtrVector<ManualNode>& children() const override
{
reify_if_needed();
return m_children;
}
virtual const ManualNode* parent() const override { return nullptr; }
virtual String name() const override { return m_full_name; }
virtual bool is_open() const override { return m_open; }
void set_open(bool open);
const String& section_name() const { return m_section; }
String path() const;
private:
void reify_if_needed() const;
String m_section;
String m_full_name;
mutable NonnullOwnPtrVector<ManualNode> m_children;
mutable bool m_reified { false };
bool m_open { false };
};

View file

@ -0,0 +1,312 @@
/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "History.h"
#include "ManualModel.h"
#include <AK/URL.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <LibDesktop/Launcher.h>
#include <LibGUI/Action.h>
#include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/FilteringProxyModel.h>
#include <LibGUI/ListView.h>
#include <LibGUI/Menu.h>
#include <LibGUI/MenuBar.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/Splitter.h>
#include <LibGUI/TabWidget.h>
#include <LibGUI/TextBox.h>
#include <LibGUI/ToolBar.h>
#include <LibGUI/ToolBarContainer.h>
#include <LibGUI/TreeView.h>
#include <LibGUI/Window.h>
#include <LibMarkdown/Document.h>
#include <LibWeb/OutOfProcessWebView.h>
#include <libgen.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[])
{
if (pledge("stdio shared_buffer accept rpath unix cpath fattr", nullptr) < 0) {
perror("pledge");
return 1;
}
auto app = GUI::Application::construct(argc, argv);
if (pledge("stdio shared_buffer accept rpath unix", nullptr) < 0) {
perror("pledge");
return 1;
}
if (unveil("/res", "r") < 0) {
perror("unveil");
return 1;
}
if (unveil("/usr/share/man", "r") < 0) {
perror("unveil");
return 1;
}
if (unveil("/tmp/portal/launch", "rw") < 0) {
perror("unveil");
return 1;
}
if (unveil("/tmp/portal/webcontent", "rw") < 0) {
perror("unveil");
return 1;
}
unveil(nullptr, nullptr);
const char* start_page = nullptr;
Core::ArgsParser args_parser;
args_parser.add_positional_argument(start_page, "Page to open at launch", "page", Core::ArgsParser::Required::No);
args_parser.parse(argc, argv);
auto app_icon = GUI::Icon::default_icon("app-help");
auto window = GUI::Window::construct();
window->set_icon(app_icon.bitmap_for_size(16));
window->set_title("Help");
window->resize(570, 500);
auto& widget = window->set_main_widget<GUI::Widget>();
widget.set_layout<GUI::VerticalBoxLayout>();
widget.set_fill_with_background_color(true);
widget.layout()->set_spacing(2);
auto& toolbar_container = widget.add<GUI::ToolBarContainer>();
auto& toolbar = toolbar_container.add<GUI::ToolBar>();
auto& splitter = widget.add<GUI::HorizontalSplitter>();
auto model = ManualModel::create();
auto& left_tab_bar = splitter.add<GUI::TabWidget>();
auto& tree_view_container = left_tab_bar.add_tab<GUI::Widget>("Browse");
tree_view_container.set_layout<GUI::VerticalBoxLayout>();
tree_view_container.layout()->set_margins({ 4, 4, 4, 4 });
auto& tree_view = tree_view_container.add<GUI::TreeView>();
auto& search_view = left_tab_bar.add_tab<GUI::Widget>("Search");
search_view.set_layout<GUI::VerticalBoxLayout>();
search_view.layout()->set_margins({ 4, 4, 4, 4 });
auto& search_box = search_view.add<GUI::TextBox>();
auto& search_list_view = search_view.add<GUI::ListView>();
search_box.set_fixed_height(20);
search_box.set_placeholder("Search...");
search_box.on_change = [&] {
if (auto model = search_list_view.model()) {
auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
search_model.set_filter_term(search_box.text());
search_model.update();
}
};
search_list_view.set_model(GUI::FilteringProxyModel::construct(model));
search_list_view.model()->update();
tree_view.set_model(model);
left_tab_bar.set_fixed_width(200);
auto& page_view = splitter.add<Web::OutOfProcessWebView>();
History history;
RefPtr<GUI::Action> go_back_action;
RefPtr<GUI::Action> go_forward_action;
auto update_actions = [&]() {
go_back_action->set_enabled(history.can_go_back());
go_forward_action->set_enabled(history.can_go_forward());
};
auto open_page = [&](const String& path) {
if (path.is_null()) {
window->set_title("Help");
page_view.load_empty_document();
return;
}
auto source_result = model->page_view(path);
if (source_result.is_error()) {
GUI::MessageBox::show(window, source_result.error().string(), "Failed to open man page", GUI::MessageBox::Type::Error);
return;
}
auto source = source_result.value();
String html;
{
auto md_document = Markdown::Document::parse(source);
ASSERT(md_document);
html = md_document->render_to_html();
}
auto url = URL::create_with_file_protocol(path);
page_view.load_html(html, url);
auto tree_view_index = model->index_from_path(path);
if (tree_view_index.has_value())
tree_view.expand_tree(tree_view_index.value().parent());
String page_and_section = model->page_and_section(tree_view_index.value());
window->set_title(String::formatted("{} - Help", page_and_section));
};
tree_view.on_selection_change = [&] {
String path = model->page_path(tree_view.selection().first());
history.push(path);
update_actions();
open_page(path);
};
tree_view.on_toggle = [&](const GUI::ModelIndex& index, const bool open) {
model->update_section_node_on_toggle(index, open);
};
auto open_external = [&](auto& url) {
if (!Desktop::Launcher::open(url)) {
GUI::MessageBox::show(window,
String::formatted("The link to '{}' could not be opened.", url),
"Failed to open link",
GUI::MessageBox::Type::Error);
}
};
search_list_view.on_selection = [&](auto index) {
if (!index.is_valid())
return;
if (auto model = search_list_view.model()) {
auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
index = search_model.map(index);
} else {
page_view.load_empty_document();
return;
}
String path = model->page_path(index);
if (path.is_null()) {
page_view.load_empty_document();
return;
}
tree_view.selection().clear();
tree_view.selection().add(index);
history.push(path);
update_actions();
open_page(path);
};
page_view.on_link_click = [&](auto& url, auto&, unsigned) {
if (url.protocol() != "file") {
open_external(url);
return;
}
auto path = Core::File::real_path_for(url.path());
if (!path.starts_with("/usr/share/man/")) {
open_external(url);
return;
}
auto tree_view_index = model->index_from_path(path);
if (tree_view_index.has_value()) {
dbgln("Found path _{}_ in model at index {}", path, tree_view_index.value());
tree_view.selection().set(tree_view_index.value());
return;
}
history.push(path);
update_actions();
open_page(path);
};
go_back_action = GUI::CommonActions::make_go_back_action([&](auto&) {
history.go_back();
update_actions();
open_page(history.current());
});
go_forward_action = GUI::CommonActions::make_go_forward_action([&](auto&) {
history.go_forward();
update_actions();
open_page(history.current());
});
go_back_action->set_enabled(false);
go_forward_action->set_enabled(false);
auto go_home_action = GUI::CommonActions::make_go_home_action([&](auto&) {
String path = "/usr/share/man/man7/Help-index.md";
history.push(path);
update_actions();
open_page(path);
});
toolbar.add_action(*go_back_action);
toolbar.add_action(*go_forward_action);
toolbar.add_action(*go_home_action);
auto menubar = GUI::MenuBar::construct();
auto& app_menu = menubar->add_menu("Help");
app_menu.add_action(GUI::CommonActions::make_about_action("Help", app_icon, window));
app_menu.add_separator();
app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
GUI::Application::the()->quit();
}));
auto& go_menu = menubar->add_menu("Go");
go_menu.add_action(*go_back_action);
go_menu.add_action(*go_forward_action);
go_menu.add_action(*go_home_action);
app->set_menubar(move(menubar));
if (start_page) {
URL url = URL::create_with_url_or_path(start_page);
if (url.is_valid() && url.path().ends_with(".md")) {
history.push(url.path());
update_actions();
open_page(url.path());
} else {
left_tab_bar.set_active_widget(&search_view);
search_box.set_text(start_page);
if (auto model = search_list_view.model()) {
auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
search_model.set_filter_term(search_box.text());
}
}
} else {
go_home_action->activate();
}
window->set_focused_widget(&left_tab_bar);
window->show();
return app->exec();
}