blob: 19b811a07804834ba8e14a04d887910e5bfde7d7 (
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
|
#pragma once
#include <boost/filesystem/path.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <string>
#include <unordered_map>
#include <thread>
#include <mutex>
#include <vector>
namespace TinyProcessLib
{
class Process;
}
namespace dchat
{
class Gif;
struct ImageByUrlResult
{
enum class Type
{
CACHED,
DOWNLOADING,
FAILED_DOWNLOAD
};
ImageByUrlResult() : texture(nullptr), type(Type::DOWNLOADING), isGif(false) {}
ImageByUrlResult(sf::Texture *_texture, Type _type) : texture(_texture), type(_type), isGif(false) {}
ImageByUrlResult(Gif *_gif, Type _type) : gif(_gif), type(_type), isGif(true) {}
// @texture is null if @type is DOWNLOADING or FAILED_DOWNLOAD
union
{
sf::Texture *texture;
Gif *gif;
};
Type type;
bool isGif;
};
class Cache
{
public:
Cache();
~Cache();
// Creates directory if it doesn't exist (recursively). Throws boost exception on failure
static boost::filesystem::path getDchatDir();
static void loadBindsFromFile();
static void replaceBindsInFile(const std::unordered_map<std::string, std::string> &binds);
// Get cached image or downloads it.
// Default download file limit is 12MB
// Returns ImageByUrlResult describing texture status.
const ImageByUrlResult getImageByUrl(const std::string &url, int downloadLimitBytes = 12582912);
private:
struct ImageDownloadInfo
{
TinyProcessLib::Process *process;
std::string url;
};
std::thread downloadWaitThread;
std::vector<ImageDownloadInfo> imageDownloadProcesses;
std::vector<ImageDownloadInfo> imageDownloadProcessesQueue;
std::mutex imageDownloadMutex;
bool alive;
};
}
|