diff --git a/Applications/Browser/BookmarksBarWidget.cpp b/Applications/Browser/BookmarksBarWidget.cpp index f51bfe0e69..eb9b6ad1db 100644 --- a/Applications/Browser/BookmarksBarWidget.cpp +++ b/Applications/Browser/BookmarksBarWidget.cpp @@ -162,3 +162,45 @@ void BookmarksBarWidget::update_content_size() } } } + +bool BookmarksBarWidget::contains_bookmark(const String& url) +{ + for (int item_index = 0; item_index < model()->row_count(); ++item_index) { + + auto item_title = model()->data(model()->index(item_index, 0)).to_string(); + auto item_url = model()->data(model()->index(item_index, 1)).to_string(); + if (item_url == url) { + return true; + } + } + return false; +} + +bool BookmarksBarWidget::remove_bookmark(const String& url) +{ + for (int item_index = 0; item_index < model()->row_count(); ++item_index) { + + auto item_title = model()->data(model()->index(item_index, 0)).to_string(); + auto item_url = model()->data(model()->index(item_index, 1)).to_string(); + if (item_url == url) { + auto& json_model = *static_cast(model()); + json_model.remove(item_index); + return true; + } + } + + return false; +} +bool BookmarksBarWidget::add_bookmark(const String& url, const String& title) +{ + Vector values; + values.append(title); + values.append(url); + + auto& json_model = *static_cast(model()); + if (json_model.add(move(values))) { + json_model.store(); + return true; + } + return false; +} diff --git a/Applications/Browser/BookmarksBarWidget.h b/Applications/Browser/BookmarksBarWidget.h index c9135d6549..586192eea7 100644 --- a/Applications/Browser/BookmarksBarWidget.h +++ b/Applications/Browser/BookmarksBarWidget.h @@ -41,6 +41,10 @@ public: Function on_bookmark_click; Function on_bookmark_hover; + bool contains_bookmark(const String& url); + bool remove_bookmark(const String& url); + bool add_bookmark(const String& url, const String& title); + private: BookmarksBarWidget(const String&, bool enabled); diff --git a/Applications/Browser/main.cpp b/Applications/Browser/main.cpp index 441d8ddfb7..25f01f5755 100644 --- a/Applications/Browser/main.cpp +++ b/Applications/Browser/main.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -42,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -56,9 +58,11 @@ static const char* home_url = "file:///home/anon/www/welcome.html"; static const char* bookmarks_filename = "/home/anon/bookmarks.json"; +static String s_title = ""; + int main(int argc, char** argv) { - if (pledge("stdio shared_buffer accept unix cpath rpath fattr", nullptr) < 0) { + if (pledge("stdio shared_buffer accept unix cpath rpath wpath fattr", nullptr) < 0) { perror("pledge"); return 1; } @@ -68,11 +72,23 @@ int main(int argc, char** argv) // Connect to the ProtocolServer immediately so we can drop the "unix" pledge. Web::ResourceLoader::the(); - if (pledge("stdio shared_buffer accept rpath", nullptr) < 0) { + if (pledge("stdio shared_buffer accept cpath rpath wpath", nullptr) < 0) { perror("pledge"); return 1; } + if (unveil("/home", "rwc") < 0) { + perror("unveil"); + return 1; + } + + if (unveil("/res", "r") < 0) { + perror("unveil"); + return 1; + } + + unveil(nullptr, nullptr); + auto window = GUI::Window::construct(); window->set_rect(100, 100, 640, 480); @@ -135,11 +151,36 @@ int main(int argc, char** argv) html_widget.load(location_box.text()); }; + auto& bookmark_button = toolbar.add(); + bookmark_button.set_button_style(Gfx::ButtonStyle::CoolBar); + bookmark_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/star-black.png")); + bookmark_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed); + bookmark_button.set_preferred_size(22, 22); + + auto update_bookmark_button = [&](const String& url) { + if (bookmarksbar.contains_bookmark(url)) { + bookmark_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/star-yellow.png")); + } else { + bookmark_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/star-contour.png")); + } + }; + + bookmark_button.on_click = [&] { + auto url = html_widget.main_frame().document()->url().to_string(); + if (bookmarksbar.contains_bookmark(url)) { + bookmarksbar.remove_bookmark(url); + } else { + bookmarksbar.add_bookmark(url, s_title); + } + update_bookmark_button(url); + }; + html_widget.on_load_start = [&](auto& url) { location_box.set_text(url.to_string()); if (should_push_loads_to_history) history.push(url); update_actions(); + update_bookmark_button(url.to_string()); }; html_widget.on_link_click = [&](auto& url) { @@ -151,6 +192,7 @@ int main(int argc, char** argv) }; html_widget.on_title_change = [&](auto& title) { + s_title = title; window->set_title(String::format("%s - Browser", title.characters())); }; diff --git a/Base/res/icons/16x16/star-contour.png b/Base/res/icons/16x16/star-contour.png new file mode 100644 index 0000000000..ce7fbbe992 Binary files /dev/null and b/Base/res/icons/16x16/star-contour.png differ diff --git a/Base/res/icons/16x16/star-yellow.png b/Base/res/icons/16x16/star-yellow.png new file mode 100644 index 0000000000..d169024740 Binary files /dev/null and b/Base/res/icons/16x16/star-yellow.png differ