Browse Source

use native glib::uri parser

CPP-GTK4
yggverse 2 months ago
parent
commit
e8e46a6217
  1. 66
      src/app/browser/main/tab/page.cpp
  2. 2
      src/app/browser/main/tab/page.hpp
  3. 15
      src/app/browser/main/tab/page/content/text/gemini/reader.cpp
  4. 35
      src/app/browser/main/tab/page/navigation.cpp
  5. 6
      src/app/browser/main/tab/page/navigation.hpp
  6. 58
      src/app/browser/main/tab/page/navigation/request.cpp
  7. 16
      src/app/browser/main/tab/page/navigation/request.hpp

66
src/app/browser/main/tab/page.cpp

@ -177,6 +177,13 @@ void Page::navigation_reload(
} }
} }
// Parse request string
uri = g_uri_parse(
pageNavigation->get_request_text().c_str(),
G_URI_FLAGS_NONE,
NULL // @TODO GError *
);
// Reset page data // Reset page data
mime = MIME::UNDEFINED; mime = MIME::UNDEFINED;
@ -191,13 +198,36 @@ void Page::navigation_reload(
action__update->activate(); action__update->activate();
if (NULL == uri || NULL == g_uri_get_scheme(uri))
{
// Scheme not found but host provided, redirect to gemini://
if (NULL != g_uri_get_host(uri))
{
pageNavigation->set_request_text(
"gemini://" + pageNavigation->get_request_text()
);
navigation_reload(
false
);
}
// Make search request
else
{
throw _("Search request not implemented yet"); // @TODO
}
}
// Connect scheme driver // Connect scheme driver
if ("file" == pageNavigation->get_request_scheme()) else
if (g_uri_get_scheme(uri) == Glib::ustring("file"))
{ {
// @TODO // @TODO
} }
else if ("gemini" == pageNavigation->get_request_scheme()) else
if (g_uri_get_scheme(uri) == Glib::ustring("gemini"))
{ {
// Create new socket connection // Create new socket connection
GioSocketClient = Gio::SocketClient::create(); GioSocketClient = Gio::SocketClient::create();
@ -223,7 +253,9 @@ void Page::navigation_reload(
description = Glib::ustring::sprintf( description = Glib::ustring::sprintf(
_("Connecting to %s.."), _("Connecting to %s.."),
pageNavigation->get_request_host() g_uri_get_host(
uri
)
); );
progress_fraction = .25; progress_fraction = .25;
@ -264,7 +296,9 @@ void Page::navigation_reload(
description = Glib::ustring::sprintf( description = Glib::ustring::sprintf(
_("Begin request to %s.."), _("Begin request to %s.."),
pageNavigation->get_request_host() g_uri_get_host(
uri
)
); );
progress_fraction = .5; progress_fraction = .5;
@ -282,7 +316,9 @@ void Page::navigation_reload(
description = Glib::ustring::sprintf( description = Glib::ustring::sprintf(
_("Reading response from %s.."), _("Reading response from %s.."),
pageNavigation->get_request_host() g_uri_get_host(
uri
)
); );
progress_fraction = .75; progress_fraction = .75;
@ -299,14 +335,16 @@ void Page::navigation_reload(
if (meta[1] == "20") if (meta[1] == "20")
{ {
// Route by mime type or path extension // Route by mime type or path extension
if (meta[2] == "text/gemini" || Glib::str_has_suffix(pageNavigation->get_request_path(), ".gmi")) if (meta[2] == "text/gemini" || Glib::str_has_suffix(g_uri_get_path(uri), ".gmi"))
{ {
// Update // Update
mime = MIME::TEXT_GEMINI; mime = MIME::TEXT_GEMINI;
title = _("Done"); // @TODO page title title = _("Done"); // @TODO page title
description = pageNavigation->get_request_host(); description = g_uri_get_host(
uri
);
progress_fraction = 1; progress_fraction = 1;
@ -363,21 +401,9 @@ void Page::navigation_reload(
); );
} }
// Scheme not found but host provided, redirect to gemini://
else if (pageNavigation->get_request_scheme().empty() && !pageNavigation->get_request_host().empty())
{
pageNavigation->set_request_text(
"gemini://" + pageNavigation->get_request_text()
);
navigation_reload(
false
);
}
else else
{ {
// @TODO search request throw _("Exception"); // @TODO
} }
} }

2
src/app/browser/main/tab/page.hpp

@ -88,6 +88,8 @@ namespace app::browser::main::tab
// Tools // Tools
double progress_fraction; // async load indication (progress bar) double progress_fraction; // async load indication (progress bar)
GUri * uri;
// Actions // Actions
Glib::RefPtr<Gio::SimpleAction> action__update; Glib::RefPtr<Gio::SimpleAction> action__update;

15
src/app/browser/main/tab/page/content/text/gemini/reader.cpp

@ -265,6 +265,21 @@ Glib::ustring Reader::Make::link(
); );
} }
/* @TODO
GError * error;
g_uri_resolve_relative(
get_text().c_str(),
get_text().c_str(),
G_URI_FLAGS_NONE,
&error
);
if (NULL)
*/
return Glib::ustring::sprintf( return Glib::ustring::sprintf(
"<a href=\"%s\" title=\"%s\">%s</a>\n", "<a href=\"%s\" title=\"%s\">%s</a>\n",
Glib::Markup::escape_text( Glib::Markup::escape_text(

35
src/app/browser/main/tab/page/navigation.cpp

@ -91,8 +91,16 @@ void Navigation::update(
const double & PROGRESS_FRACTION const double & PROGRESS_FRACTION
) { ) {
// Toggle base button sensibility // Toggle base button sensibility
GUri * uri = g_uri_parse(
navigationRequest->get_text().c_str(),
G_URI_FLAGS_NONE,
NULL // @TODO GError *
);
navigationBase->set_sensitive( navigationBase->set_sensitive(
!navigationRequest->get_host().empty() && !navigationRequest->get_path().empty() NULL != uri &&
NULL != g_uri_get_host(uri) &&
NULL != g_uri_get_path(uri)
); );
// Update history widget // Update history widget
@ -246,31 +254,6 @@ Glib::ustring Navigation::get_request_text()
return navigationRequest->get_text(); return navigationRequest->get_text();
} }
Glib::ustring Navigation::get_request_scheme()
{
return navigationRequest->get_scheme();
}
Glib::ustring Navigation::get_request_host()
{
return navigationRequest->get_host();
}
Glib::ustring Navigation::get_request_path()
{
return navigationRequest->get_path();
}
Glib::ustring Navigation::get_request_query()
{
return navigationRequest->get_query();
}
Glib::ustring Navigation::get_request_port()
{
return navigationRequest->get_port();
}
// Setters // Setters
void Navigation::set_request_text( void Navigation::set_request_text(
const Glib::ustring & VALUE const Glib::ustring & VALUE

6
src/app/browser/main/tab/page/navigation.hpp

@ -124,12 +124,6 @@ namespace app::browser::main::tab::page
// Getters // Getters
Glib::ustring get_request_text(); Glib::ustring get_request_text();
Glib::ustring get_request_scheme();
Glib::ustring get_request_host();
Glib::ustring get_request_port();
Glib::ustring get_request_path();
Glib::ustring get_request_query();
// Setters // Setters
void set_request_text( void set_request_text(
const Glib::ustring & VALUE const Glib::ustring & VALUE

58
src/app/browser/main/tab/page/navigation/request.cpp

@ -34,8 +34,6 @@ Request::Request(
signal_changed().connect( signal_changed().connect(
[this] [this]
{ {
parse();
action__update->activate(); action__update->activate();
} }
); );
@ -43,8 +41,6 @@ Request::Request(
signal_activate().connect( signal_activate().connect(
[this] [this]
{ {
parse();
action__reload->activate(); action__reload->activate();
} }
); );
@ -146,60 +142,6 @@ int Request::save(
); );
} }
void Request::parse() // @TODO https://docs.gtk.org/glib/struct.Uri.html
{
scheme.clear();
host.clear();
port.clear();
path.clear();
query.clear();
auto match = Glib::Regex::split_simple(
R"regex(^((\w+)?:\/\/)?([^:\/]+)?(:(\d+)?)?([^\?$]+)?(\?(.*)?)?)regex",
get_text()
);
int index = 0; for (const Glib::ustring & VALUE : match)
{
switch (index)
{
case 2: scheme = VALUE; break;
case 3: host = VALUE; break;
case 5: port = VALUE; break;
case 6: path = VALUE; break;
case 8: query = VALUE; break;
}
index++;
}
}
// Getters
Glib::ustring Request::get_scheme()
{
return scheme;
}
Glib::ustring Request::get_host()
{
return host;
}
Glib::ustring Request::get_port()
{
return port;
}
Glib::ustring Request::get_path()
{
return path;
}
Glib::ustring Request::get_query()
{
return query;
}
// Database model // Database model
int Request::DB::SESSION::init( int Request::DB::SESSION::init(
sqlite3 * db sqlite3 * db

16
src/app/browser/main/tab/page/navigation/request.hpp

@ -66,20 +66,11 @@ namespace app::browser::main::tab::page::navigation
// Extras // Extras
double progress_fraction; double progress_fraction;
Glib::ustring scheme,
host,
port,
path,
query;
// Defaults // Defaults
const bool HEXPAND = true; const bool HEXPAND = true;
const double PROGRESS_PULSE_STEP = .1; const double PROGRESS_PULSE_STEP = .1;
const int PROGRESS_ANIMATION_TIME = 10; const int PROGRESS_ANIMATION_TIME = 10;
// Private helpers
void parse();
/* /*
* Class API * Class API
*/ */
@ -103,13 +94,6 @@ namespace app::browser::main::tab::page::navigation
int save( int save(
const sqlite3_int64 & APP_BROWSER_MAIN_TAB_PAGE_NAVIGATION__SESSION__ID const sqlite3_int64 & APP_BROWSER_MAIN_TAB_PAGE_NAVIGATION__SESSION__ID
); );
// Getters
Glib::ustring get_scheme();
Glib::ustring get_host();
Glib::ustring get_port();
Glib::ustring get_path();
Glib::ustring get_query();
}; };
} }

Loading…
Cancel
Save