2009-03-02 23:31:01 +00:00
|
|
|
/*
|
|
|
|
* icons.[ch] written by Paolo Bacchilega, who writes:
|
2011-01-19 13:48:47 +00:00
|
|
|
* "There is no problem for me, you can license my code
|
|
|
|
* under whatever licence you wish :)"
|
2010-10-01 20:22:51 +00:00
|
|
|
*
|
2009-03-02 23:31:01 +00:00
|
|
|
*/
|
|
|
|
|
2022-12-26 21:13:21 +00:00
|
|
|
#include "IconCache.h"
|
|
|
|
|
2022-12-29 02:42:20 +00:00
|
|
|
#include "GtkCompat.h"
|
2022-12-26 21:13:21 +00:00
|
|
|
|
2022-12-27 01:43:20 +00:00
|
|
|
#include <giomm/contenttype.h>
|
2022-12-26 21:13:21 +00:00
|
|
|
|
2023-11-21 15:02:03 +00:00
|
|
|
#include <functional> // for std::less<>
|
2022-04-26 23:24:07 +00:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
2023-11-21 15:02:03 +00:00
|
|
|
#include <utility> // for std::move()
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
using IconCache = std::map<std::string, Glib::RefPtr<Gio::Icon>, std::less<>>;
|
|
|
|
|
2022-04-26 23:24:07 +00:00
|
|
|
std::string_view const DirectoryMimeType = "folder"sv;
|
|
|
|
std::string_view const UnknownMimeType = "unknown"sv;
|
2009-03-02 23:31:01 +00:00
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
Glib::RefPtr<Gio::Icon> gtr_get_mime_type_icon(std::string_view mime_type)
|
2009-03-02 23:31:01 +00:00
|
|
|
{
|
2022-10-08 22:50:03 +00:00
|
|
|
static IconCache cache;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-26 23:24:07 +00:00
|
|
|
if (auto mime_it = cache.find(mime_type); mime_it != std::end(cache))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-26 23:24:07 +00:00
|
|
|
return mime_it->second;
|
2009-03-02 23:31:01 +00:00
|
|
|
}
|
2009-07-27 18:48:21 +00:00
|
|
|
|
2022-04-26 23:24:07 +00:00
|
|
|
auto mime_type_str = std::string{ mime_type };
|
|
|
|
auto icon = Gio::content_type_get_icon(mime_type_str);
|
2022-10-08 22:50:03 +00:00
|
|
|
if (icon != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-10-08 22:50:03 +00:00
|
|
|
cache.try_emplace(std::move(mime_type_str), icon);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-03-02 23:31:01 +00:00
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
return icon;
|
2010-01-05 23:47:50 +00:00
|
|
|
}
|