File: IconCache.cc

package info (click to toggle)
transmission 4.1.0~beta3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 47,800 kB
  • sloc: cpp: 223,954; python: 6,998; javascript: 6,211; ansic: 5,826; sh: 771; xml: 550; makefile: 73
file content (44 lines) | stat: -rw-r--r-- 1,050 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
 * 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 "IconCache.h"

#include "GtkCompat.h"

#include <giomm/contenttype.h>

#include <functional> // for std::less<>
#include <map>
#include <string>
#include <string_view>
#include <utility> // for std::move()

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;
}