summaryrefslogtreecommitdiff
path: root/src/iconfetcher.cpp
blob: c677424c8f4488dd4a95c5761cb3ca28c37e85fc (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "iconfetcher.h"
#include "shared/util.h"
#include "thread_utils.h"

void IconFetcher::Waiter::wait()
{
  std::unique_lock lock(m_wakeUpMutex);
  m_wakeUp.wait(lock, [&] {
    return m_queueAvailable;
  });
  m_queueAvailable = false;
}

void IconFetcher::Waiter::wakeUp()
{
  {
    std::scoped_lock lock(m_wakeUpMutex);
    m_queueAvailable = true;
  }

  m_wakeUp.notify_one();
}

IconFetcher::IconFetcher() : m_iconSize(GetSystemMetrics(SM_CXSMICON)), m_stop(false)
{
  m_quickCache.file      = getPixmapIcon(QFileIconProvider::File);
  m_quickCache.directory = getPixmapIcon(QFileIconProvider::Folder);

  m_thread = MOShared::startSafeThread([&] {
    threadFun();
  });
}

IconFetcher::~IconFetcher()
{
  stop();
  m_thread.join();
}

void IconFetcher::stop()
{
  m_stop = true;
  m_waiter.wakeUp();
}

QVariant IconFetcher::icon(const QString& path) const
{
  if (hasOwnIcon(path)) {
    return fileIcon(path);
  } else {
    const auto dot = path.lastIndexOf(".");

    if (dot == -1) {
      // no extension
      return m_quickCache.file;
    }

    return extensionIcon(QStringView{path}.mid(dot));
  }
}

QPixmap IconFetcher::genericFileIcon() const
{
  return m_quickCache.file;
}

QPixmap IconFetcher::genericDirectoryIcon() const
{
  return m_quickCache.directory;
}

bool IconFetcher::hasOwnIcon(const QString& path) const
{
  static const QString exe = ".exe";
  static const QString lnk = ".lnk";
  static const QString ico = ".ico";

  return path.endsWith(exe, Qt::CaseInsensitive) ||
         path.endsWith(lnk, Qt::CaseInsensitive) ||
         path.endsWith(ico, Qt::CaseInsensitive);
}

void IconFetcher::threadFun()
{
  MOShared::SetThisThreadName("IconFetcher");

  while (!m_stop) {
    m_waiter.wait();
    if (m_stop) {
      break;
    }

    checkCache(m_extensionCache);
    checkCache(m_fileCache);
  }
}

void IconFetcher::checkCache(Cache& cache)
{
  std::set<QString> queue;

  {
    std::scoped_lock lock(cache.queueMutex);
    queue = std::move(cache.queue);
    cache.queue.clear();
  }

  if (queue.empty()) {
    return;
  }

  std::map<QString, QPixmap> map;
  for (auto&& ext : queue) {
    map.emplace(std::move(ext), getPixmapIcon(QFileInfo(ext)));
  }

  {
    std::scoped_lock lock(cache.mapMutex);
    for (auto&& p : map) {
      cache.map.insert(std::move(p));
    }
  }
}

void IconFetcher::queue(Cache& cache, QString path) const
{
  {
    std::scoped_lock lock(cache.queueMutex);
    cache.queue.insert(std::move(path));
  }

  m_waiter.wakeUp();
}

QVariant IconFetcher::fileIcon(const QString& path) const
{
  {
    std::scoped_lock lock(m_fileCache.mapMutex);
    auto itor = m_fileCache.map.find(path);
    if (itor != m_fileCache.map.end()) {
      return itor->second;
    }
  }

  queue(m_fileCache, path);
  return {};
}

QVariant IconFetcher::extensionIcon(const QStringView& ext) const
{
  {
    std::scoped_lock lock(m_extensionCache.mapMutex);
    auto itor = m_extensionCache.map.find(ext);
    if (itor != m_extensionCache.map.end()) {
      return itor->second;
    }
  }

  queue(m_extensionCache, ext.toString());
  return {};
}