UI/Qt: Do not rely on the URL implicit constructors

This commit is contained in:
Shannon Booth 2025-02-22 21:51:15 +13:00 committed by Tim Flynn
parent 0f495421f1
commit 3fb9c37783
Notes: github-actions[bot] 2025-03-04 21:26:06 +00:00
6 changed files with 21 additions and 11 deletions

View file

@ -596,7 +596,7 @@ BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, IsPopupWindow
auto* about_action = new QAction("&About Ladybird", this);
help_menu->addAction(about_action);
QObject::connect(about_action, &QAction::triggered, this, [this] {
new_tab_from_url("about:version"sv, Web::HTML::ActivateTab::Yes);
new_tab_from_url(URL::about_version(), Web::HTML::ActivateTab::Yes);
});
m_hamburger_menu->addSeparator();
@ -609,7 +609,9 @@ BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, IsPopupWindow
QObject::connect(quit_action, &QAction::triggered, this, &QMainWindow::close);
QObject::connect(m_new_tab_action, &QAction::triggered, this, [this] {
auto& tab = new_tab_from_url(ak_url_from_qstring(Settings::the()->new_tab_page()), Web::HTML::ActivateTab::Yes);
auto url = ak_url_from_qstring(Settings::the()->new_tab_page());
VERIFY(url.has_value());
auto& tab = new_tab_from_url(url.release_value(), Web::HTML::ActivateTab::Yes);
tab.set_url_is_hidden(true);
tab.focus_location_editor();
});

View file

@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibURL/Parser.h>
#include <LibURL/URL.h>
#include <LibWebView/Application.h>
#include <LibWebView/SearchEngine.h>
@ -50,11 +51,11 @@ SettingsDialog::SettingsDialog(QMainWindow* window)
m_new_tab_page->setText(Settings::the()->new_tab_page());
QObject::connect(m_new_tab_page, &QLineEdit::textChanged, this, [this] {
auto url_string = ak_string_from_qstring(m_new_tab_page->text());
m_new_tab_page->setStyleSheet(URL::URL(url_string).is_valid() ? "" : "border: 1px solid red;");
m_new_tab_page->setStyleSheet(URL::Parser::basic_parse(url_string).has_value() ? "" : "border: 1px solid red;");
});
QObject::connect(m_new_tab_page, &QLineEdit::editingFinished, this, [this] {
auto url_string = ak_string_from_qstring(m_new_tab_page->text());
if (URL::URL(url_string).is_valid())
if (URL::Parser::basic_parse(url_string).has_value())
Settings::the()->set_new_tab_page(m_new_tab_page->text());
});
QObject::connect(m_new_tab_page, &QLineEdit::returnPressed, this, [this] {

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibURL/Parser.h>
#include <UI/Qt/StringUtils.h>
AK::ByteString ak_byte_string_from_qstring(QString const& qstring)
@ -23,13 +24,13 @@ QString qstring_from_ak_string(StringView ak_string)
return QString::fromUtf8(ak_string.characters_without_null_termination(), static_cast<qsizetype>(ak_string.length()));
}
URL::URL ak_url_from_qstring(QString const& qstring)
Optional<URL::URL> ak_url_from_qstring(QString const& qstring)
{
auto utf8_data = qstring.toUtf8();
return URL::URL(StringView(utf8_data.data(), utf8_data.size()));
return URL::Parser::basic_parse(StringView(utf8_data.data(), utf8_data.size()));
}
URL::URL ak_url_from_qurl(QUrl const& qurl)
{
return ak_url_from_qstring(qurl.toString());
return ak_url_from_qstring(qurl.toString()).value();
}

View file

@ -8,6 +8,7 @@
#include <AK/ByteString.h>
#include <AK/Error.h>
#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <LibURL/URL.h>
@ -18,5 +19,5 @@
AK::ByteString ak_byte_string_from_qstring(QString const&);
String ak_string_from_qstring(QString const&);
QString qstring_from_ak_string(StringView);
URL::URL ak_url_from_qstring(QString const&);
Optional<URL::URL> ak_url_from_qstring(QString const&);
URL::URL ak_url_from_qurl(QUrl const&);

View file

@ -8,6 +8,7 @@
#include <AK/TemporaryChange.h>
#include <LibGfx/ImageFormats/BMPWriter.h>
#include <LibURL/Parser.h>
#include <LibWeb/HTML/SelectedFile.h>
#include <LibWebView/SearchEngine.h>
#include <LibWebView/SourceHighlighter.h>
@ -466,8 +467,10 @@ Tab::Tab(BrowserWindow* window, RefPtr<WebView::WebContentClient> parent_client,
auto* search_selected_text_action = new QAction("&Search for <query>", this);
search_selected_text_action->setIcon(load_icon_from_uri("resource://icons/16x16/find.png"sv));
QObject::connect(search_selected_text_action, &QAction::triggered, this, [this]() {
auto url = MUST(String::formatted(Settings::the()->search_engine().query_url, URL::percent_encode(*m_page_context_menu_search_text)));
m_window->new_tab_from_url(URL::URL(url), Web::HTML::ActivateTab::Yes);
auto url_string = MUST(String::formatted(Settings::the()->search_engine().query_url, URL::percent_encode(*m_page_context_menu_search_text)));
auto url = URL::Parser::basic_parse(url_string);
VERIFY(url.has_value());
m_window->new_tab_from_url(url.release_value(), Web::HTML::ActivateTab::Yes);
});
auto take_screenshot = [this](auto type) {

View file

@ -73,7 +73,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
Core::EventLoopManager::install(*new WebView::EventLoopManagerQt);
auto app = Ladybird::Application::create(arguments, ak_url_from_qstring(Ladybird::Settings::the()->new_tab_page()));
auto url = ak_url_from_qstring(Ladybird::Settings::the()->new_tab_page());
VERIFY(url.has_value());
auto app = Ladybird::Application::create(arguments, url.release_value());
static_cast<WebView::EventLoopImplementationQt&>(Core::EventLoop::current().impl()).set_main_loop();
TRY(handle_attached_debugger());