2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2012-2022 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2010-03-15 02:53:31 +00:00
|
|
|
|
2022-12-26 21:13:21 +00:00
|
|
|
#include "FaviconCache.h"
|
|
|
|
|
|
|
|
#include "Utils.h" /* gtr_get_host_from_url() */
|
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/web-utils.h>
|
|
|
|
#include <libtransmission/web.h> // tr_sessionFetch()
|
|
|
|
|
2022-12-27 01:43:20 +00:00
|
|
|
#include <glibmm/error.h>
|
|
|
|
#include <glibmm/fileutils.h>
|
|
|
|
#include <glibmm/main.h>
|
|
|
|
#include <glibmm/miscutils.h>
|
|
|
|
|
2022-12-26 21:13:21 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
#include <array>
|
2022-07-26 02:45:54 +00:00
|
|
|
#include <functional>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2022-11-30 00:09:32 +00:00
|
|
|
#include <string_view>
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <glib/gstdio.h> /* g_remove() */
|
2010-03-15 02:53:31 +00:00
|
|
|
|
2022-11-30 00:09:32 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2022-11-30 00:09:32 +00:00
|
|
|
constexpr auto TimeoutSecs = 15s;
|
|
|
|
constexpr auto ImageTypes = std::array<std::string_view, 4>{ "ico"sv, "png"sv, "gif"sv, "jpg"sv };
|
2010-03-17 20:30:04 +00:00
|
|
|
|
2010-03-15 02:53:31 +00:00
|
|
|
struct favicon_data
|
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
tr_session* session = nullptr;
|
|
|
|
std::function<void(Glib::RefPtr<Gdk::Pixbuf> const&)> func;
|
|
|
|
std::string host;
|
|
|
|
std::string contents;
|
|
|
|
size_t type = 0;
|
2022-11-30 00:09:32 +00:00
|
|
|
long code = 0;
|
2010-03-15 02:53:31 +00:00
|
|
|
};
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
Glib::ustring get_url(std::string const& host, size_t image_type)
|
2010-03-17 20:30:04 +00:00
|
|
|
{
|
2022-11-30 00:09:32 +00:00
|
|
|
return fmt::format("http://{}/favicon.{}", host, ImageTypes.at(image_type));
|
2010-03-17 20:30:04 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
std::string favicon_get_cache_dir()
|
2010-03-15 02:53:31 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
static std::string dir;
|
2010-03-15 02:53:31 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
if (dir.empty())
|
2010-03-15 02:53:31 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
dir = Glib::build_filename(Glib::get_user_cache_dir(), "transmission", "favicons");
|
2022-04-02 14:06:02 +00:00
|
|
|
(void)g_mkdir_with_parents(dir.c_str(), 0777);
|
2010-03-15 02:53:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
std::string favicon_get_cache_filename(std::string const& host)
|
2010-03-15 02:53:31 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
return Glib::build_filename(favicon_get_cache_dir(), host);
|
2010-03-15 02:53:31 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void favicon_save_to_cache(std::string const& host, std::string const& data)
|
2010-03-15 02:53:31 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
Glib::file_set_contents(favicon_get_cache_filename(host), data);
|
2010-03-15 02:53:31 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
Glib::RefPtr<Gdk::Pixbuf> favicon_load_from_cache(std::string const& host)
|
2010-03-15 02:53:31 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
auto const filename = favicon_get_cache_filename(host);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
try
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
return Gdk::Pixbuf::create_from_file(filename, 16, 16, false);
|
|
|
|
}
|
|
|
|
catch (Glib::Error const&)
|
|
|
|
{
|
2022-08-13 17:11:07 +00:00
|
|
|
(void)g_remove(filename.c_str());
|
2021-10-18 20:22:31 +00:00
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-03-15 02:53:31 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 23:35:57 +00:00
|
|
|
void favicon_web_done_cb(tr_web::FetchResponse const& response);
|
2010-03-17 20:30:04 +00:00
|
|
|
|
2022-11-30 20:51:38 +00:00
|
|
|
constexpr bool should_keep_trying(long code)
|
|
|
|
{
|
|
|
|
return code != 0 && code <= 500;
|
|
|
|
}
|
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
bool favicon_web_done_idle_cb(std::unique_ptr<favicon_data> fav)
|
2010-03-15 02:53:31 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
Glib::RefPtr<Gdk::Pixbuf> pixbuf;
|
2010-03-15 02:53:31 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
if (!fav->contents.empty()) /* we got something... try to make a pixbuf from it */
|
2010-03-15 15:07:23 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
favicon_save_to_cache(fav->host, fav->contents);
|
2017-04-19 12:04:45 +00:00
|
|
|
pixbuf = favicon_load_from_cache(fav->host);
|
2010-03-15 15:07:23 +00:00
|
|
|
}
|
2010-03-15 02:53:31 +00:00
|
|
|
|
2022-11-30 20:51:38 +00:00
|
|
|
if (pixbuf == nullptr && should_keep_trying(fav->code) && ++fav->type < ImageTypes.size()) /* keep trying */
|
2010-03-17 20:30:04 +00:00
|
|
|
{
|
2021-12-14 08:43:27 +00:00
|
|
|
fav->contents.clear();
|
|
|
|
auto* const session = fav->session;
|
|
|
|
auto const next_url = get_url(fav->host, fav->type);
|
2022-11-30 00:09:32 +00:00
|
|
|
tr_sessionFetch(session, { next_url.raw(), favicon_web_done_cb, fav.release(), TimeoutSecs });
|
2022-11-24 16:17:02 +00:00
|
|
|
return false;
|
2010-03-17 20:30:04 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
// Not released into the next web request, means we're done trying (even if `pixbuf` is still invalid)
|
2022-11-24 16:17:02 +00:00
|
|
|
fav->func(pixbuf);
|
2021-10-18 20:22:31 +00:00
|
|
|
return false;
|
2010-03-15 02:53:31 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 23:35:57 +00:00
|
|
|
void favicon_web_done_cb(tr_web::FetchResponse const& response)
|
2010-03-15 02:53:31 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
auto* const fav = static_cast<favicon_data*>(response.user_data);
|
|
|
|
fav->contents = response.body;
|
2022-11-30 00:09:32 +00:00
|
|
|
fav->code = response.status;
|
2021-12-14 08:43:27 +00:00
|
|
|
Glib::signal_idle().connect([fav]() { return favicon_web_done_idle_cb(std::unique_ptr<favicon_data>(fav)); });
|
2010-03-15 02:53:31 +00:00
|
|
|
}
|
2010-03-17 17:07:40 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void gtr_get_favicon(
|
|
|
|
tr_session* session,
|
|
|
|
std::string const& host,
|
|
|
|
std::function<void(Glib::RefPtr<Gdk::Pixbuf> const&)> const& pixbuf_ready_func)
|
2010-03-15 02:53:31 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
auto pixbuf = favicon_load_from_cache(host);
|
2010-03-15 02:53:31 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (pixbuf != nullptr)
|
2010-03-15 02:53:31 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
pixbuf_ready_func(pixbuf);
|
2010-03-15 02:53:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-14 08:43:27 +00:00
|
|
|
auto data = std::make_unique<favicon_data>();
|
2010-03-17 20:30:04 +00:00
|
|
|
data->session = session;
|
2010-03-15 02:53:31 +00:00
|
|
|
data->func = pixbuf_ready_func;
|
2021-10-18 20:22:31 +00:00
|
|
|
data->host = host;
|
2022-11-30 00:09:32 +00:00
|
|
|
tr_sessionFetch(session, { get_url(host, 0).raw(), favicon_web_done_cb, data.release(), TimeoutSecs });
|
2010-03-15 02:53:31 +00:00
|
|
|
}
|
|
|
|
}
|
2010-04-23 01:46:02 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void gtr_get_favicon_from_url(
|
|
|
|
tr_session* session,
|
|
|
|
Glib::ustring const& url,
|
|
|
|
std::function<void(Glib::RefPtr<Gdk::Pixbuf> const&)> const& pixbuf_ready_func)
|
2010-04-23 01:46:02 +00:00
|
|
|
{
|
2022-11-15 19:30:32 +00:00
|
|
|
if (auto const parsed_url = tr_urlParse(url.c_str()); parsed_url.has_value())
|
|
|
|
{
|
|
|
|
auto const host = std::string{ parsed_url->host };
|
|
|
|
gtr_get_favicon(session, host, pixbuf_ready_func);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pixbuf_ready_func({});
|
|
|
|
}
|
2010-04-23 01:46:02 +00:00
|
|
|
}
|