summaryrefslogtreecommitdiff
path: root/src/iconfetcher.h
blob: 3207a57ee897fb0d3353ca3f3fdd862d8e8436d0 (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
#ifndef MODORGANIZER_ICONFETCHER_INCLUDED
#define MODORGANIZER_ICONFETCHER_INCLUDED
#include <QFileIconProvider>
#include <QStringView>
#include <mutex>
#include <set>

class IconFetcher
{
public:
  IconFetcher();
  ~IconFetcher();

  void stop();

  QVariant icon(const QString& path) const;
  QPixmap genericFileIcon() const;
  QPixmap genericDirectoryIcon() const;

private:
  struct QuickCache
  {
    QPixmap file;
    QPixmap directory;
  };

  struct Cache
  {
    std::map<QString, QPixmap, std::less<>> map;
    std::mutex mapMutex;

    std::set<QString> queue;
    std::mutex queueMutex;
  };

  class Waiter
  {
  public:
    void wait();
    void wakeUp();

  private:
    mutable std::mutex m_wakeUpMutex;
    std::condition_variable m_wakeUp;
    bool m_queueAvailable = false;
  };

  const int m_iconSize;
  QFileIconProvider m_provider;
  std::thread m_thread;
  std::atomic<bool> m_stop;

  mutable QuickCache m_quickCache;
  mutable Cache m_extensionCache;
  mutable Cache m_fileCache;
  mutable Waiter m_waiter;

  bool hasOwnIcon(const QString& path) const;

  template <class T>
  QPixmap getPixmapIcon(T&& t) const
  {
    return m_provider.icon(t).pixmap({m_iconSize, m_iconSize});
  }

  void threadFun();

  void checkCache(Cache& cache);
  void queue(Cache& cache, QString path) const;

  QVariant fileIcon(const QString& path) const;
  QVariant extensionIcon(const QStringView& ext) const;
};

#endif  // MODORGANIZER_ICONFETCHER_INCLUDED