mirror of
https://github.com/transmission/transmission.git
synced 2025-12-24 20:35:36 +00:00
refactor: GTK favicon lookup improvements (#4278)
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <glib/gstdio.h> /* g_remove() */
|
||||
|
||||
@@ -19,10 +20,13 @@
|
||||
#include "FaviconCache.h"
|
||||
#include "Utils.h" /* gtr_get_host_from_url() */
|
||||
|
||||
using namespace std::literals;
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
std::array<char const*, 4> const image_types = { "ico", "png", "gif", "jpg" };
|
||||
constexpr auto TimeoutSecs = 15s;
|
||||
constexpr auto ImageTypes = std::array<std::string_view, 4>{ "ico"sv, "png"sv, "gif"sv, "jpg"sv };
|
||||
|
||||
struct favicon_data
|
||||
{
|
||||
@@ -31,11 +35,12 @@ struct favicon_data
|
||||
std::string host;
|
||||
std::string contents;
|
||||
size_t type = 0;
|
||||
long code = 0;
|
||||
};
|
||||
|
||||
Glib::ustring get_url(std::string const& host, size_t image_type)
|
||||
{
|
||||
return fmt::format("http://{}/favicon.{}", host, image_types.at(image_type));
|
||||
return fmt::format("http://{}/favicon.{}", host, ImageTypes.at(image_type));
|
||||
}
|
||||
|
||||
std::string favicon_get_cache_dir()
|
||||
@@ -88,12 +93,12 @@ bool favicon_web_done_idle_cb(std::unique_ptr<favicon_data> fav)
|
||||
pixbuf = favicon_load_from_cache(fav->host);
|
||||
}
|
||||
|
||||
if (pixbuf == nullptr && ++fav->type < image_types.size()) /* keep trying */
|
||||
if (fav->code == 404 && pixbuf == nullptr && ++fav->type < ImageTypes.size()) /* keep trying */
|
||||
{
|
||||
fav->contents.clear();
|
||||
auto* const session = fav->session;
|
||||
auto const next_url = get_url(fav->host, fav->type);
|
||||
tr_sessionFetch(session, { next_url.raw(), favicon_web_done_cb, fav.release() });
|
||||
tr_sessionFetch(session, { next_url.raw(), favicon_web_done_cb, fav.release(), TimeoutSecs });
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -106,6 +111,7 @@ void favicon_web_done_cb(tr_web::FetchResponse const& response)
|
||||
{
|
||||
auto* const fav = static_cast<favicon_data*>(response.user_data);
|
||||
fav->contents = response.body;
|
||||
fav->code = response.status;
|
||||
Glib::signal_idle().connect([fav]() { return favicon_web_done_idle_cb(std::unique_ptr<favicon_data>(fav)); });
|
||||
}
|
||||
|
||||
@@ -128,8 +134,7 @@ void gtr_get_favicon(
|
||||
data->session = session;
|
||||
data->func = pixbuf_ready_func;
|
||||
data->host = host;
|
||||
|
||||
tr_sessionFetch(session, { get_url(host, 0).raw(), favicon_web_done_cb, data.release() });
|
||||
tr_sessionFetch(session, { get_url(host, 0).raw(), favicon_web_done_cb, data.release(), TimeoutSecs });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user