2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2012-2023 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
|
|
|
|
2023-04-23 02:11:16 +00:00
|
|
|
#include <libtransmission/favicon-cache.h>
|
2022-12-26 21:13:21 +00:00
|
|
|
|
2023-04-23 02:11:16 +00:00
|
|
|
#include <gdkmm/pixbuf.h>
|
|
|
|
#include <giomm/memoryinputstream.h>
|
2022-12-27 01:43:20 +00:00
|
|
|
#include <glibmm/error.h>
|
|
|
|
#include <glibmm/main.h>
|
|
|
|
#include <glibmm/miscutils.h>
|
|
|
|
|
2023-04-23 02:11:16 +00:00
|
|
|
using Icon = Glib::RefPtr<Gdk::Pixbuf>;
|
2010-03-15 02:53:31 +00:00
|
|
|
|
2023-04-23 02:11:16 +00:00
|
|
|
template<>
|
|
|
|
Icon FaviconCache<Icon>::create_from_file(std::string_view filename) const
|
2010-03-15 02:53:31 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
try
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-04-23 02:11:16 +00:00
|
|
|
return Gdk::Pixbuf::create_from_file(std::string{ filename }, Width, Height, false);
|
2021-10-18 20:22:31 +00:00
|
|
|
}
|
|
|
|
catch (Glib::Error const&)
|
|
|
|
{
|
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-03-15 02:53:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 02:11:16 +00:00
|
|
|
template<>
|
|
|
|
Icon FaviconCache<Icon>::create_from_data(void const* data, size_t datalen) const
|
2022-11-30 20:51:38 +00:00
|
|
|
{
|
2023-04-23 02:11:16 +00:00
|
|
|
try
|
2010-03-15 15:07:23 +00:00
|
|
|
{
|
2023-04-23 02:11:16 +00:00
|
|
|
auto memory_stream = Gio::MemoryInputStream::create();
|
|
|
|
memory_stream->add_data(data, datalen, nullptr);
|
|
|
|
return Gdk::Pixbuf::create_from_stream_at_scale(memory_stream, Width, Height, false);
|
2010-03-15 15:07:23 +00:00
|
|
|
}
|
2023-04-23 02:11:16 +00:00
|
|
|
catch (Glib::Error const&)
|
2010-03-17 20:30:04 +00:00
|
|
|
{
|
2023-04-23 02:11:16 +00:00
|
|
|
return {};
|
2010-03-17 20:30:04 +00:00
|
|
|
}
|
2010-03-15 02:53:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 02:11:16 +00:00
|
|
|
template<>
|
|
|
|
std::string FaviconCache<Icon>::app_cache_dir() const
|
2010-03-15 02:53:31 +00:00
|
|
|
{
|
2023-04-23 02:11:16 +00:00
|
|
|
return fmt::format("{:s}/{:s}", Glib::get_user_cache_dir(), "transmission");
|
2010-03-15 02:53:31 +00:00
|
|
|
}
|
2010-03-17 17:07:40 +00:00
|
|
|
|
2023-04-23 02:11:16 +00:00
|
|
|
template<>
|
|
|
|
void FaviconCache<Icon>::add_to_ui_thread(std::function<void()> idlefunc)
|
2010-03-15 02:53:31 +00:00
|
|
|
{
|
2023-04-23 02:11:16 +00:00
|
|
|
Glib::signal_idle().connect_once([idlefunc = std::move(idlefunc)]() { idlefunc(); });
|
2010-04-23 01:46:02 +00:00
|
|
|
}
|