transmission/gtk/IconCache.cc

45 lines
1014 B
C++

/*
* icons.[ch] written by Paolo Bacchilega, who writes:
* "There is no problem for me, you can license my code
* under whatever licence you wish :)"
*
*/
#include <algorithm>
#include <map>
#include <string>
#include <string_view>
#include <utility>
#include <glibmm.h>
#include <giomm.h>
#include "IconCache.h"
#include "Utils.h"
using namespace std::literals;
using IconCache = std::map<std::string, Glib::RefPtr<Gio::Icon>, std::less<>>;
std::string_view const DirectoryMimeType = "folder"sv;
std::string_view const UnknownMimeType = "unknown"sv;
Glib::RefPtr<Gio::Icon> gtr_get_mime_type_icon(std::string_view mime_type)
{
static IconCache cache;
if (auto mime_it = cache.find(mime_type); mime_it != std::end(cache))
{
return mime_it->second;
}
auto mime_type_str = std::string{ mime_type };
auto icon = Gio::content_type_get_icon(mime_type_str);
if (icon != nullptr)
{
cache.try_emplace(std::move(mime_type_str), icon);
}
return icon;
}