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
|
|
|
|
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>
|
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
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
2022-02-16 18:33:50 +00:00
|
|
|
#include <libtransmission/web.h> // tr_sessionFetch()
|
2022-02-12 17:30:27 +00:00
|
|
|
#include <libtransmission/web-utils.h>
|
2010-03-15 02:53:31 +00:00
|
|
|
|
2021-11-01 00:11:23 +00:00
|
|
|
#include "FaviconCache.h"
|
|
|
|
#include "Utils.h" /* gtr_get_host_from_url() */
|
2010-03-15 02:53:31 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
std::array<char const*, 4> const image_types = { "ico", "png", "gif", "jpg" };
|
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;
|
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
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
return gtr_sprintf("http://%s/favicon.%s", host, image_types[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
|
|
|
|
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
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
if (pixbuf == nullptr && ++fav->type < image_types.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-02-16 18:33:50 +00:00
|
|
|
tr_sessionFetch(session, { next_url.raw(), favicon_web_done_cb, fav.release() });
|
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-06-04 16:33:03 +00:00
|
|
|
if (fav != nullptr)
|
|
|
|
{
|
|
|
|
fav->func(pixbuf);
|
|
|
|
}
|
2010-03-15 02:53:31 +00:00
|
|
|
|
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;
|
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;
|
2010-03-15 02:53:31 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
tr_sessionFetch(session, { get_url(host, 0).raw(), favicon_web_done_cb, data.release() });
|
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-02-12 17:30:27 +00:00
|
|
|
auto const host = std::string{ tr_urlParse(url.c_str())->host };
|
|
|
|
gtr_get_favicon(session, host, pixbuf_ready_func);
|
2010-04-23 01:46:02 +00:00
|
|
|
}
|