#pragma once #include #include #include #include #include #include namespace TinyProcessLib { class Process; } namespace dchat { struct ImageByUrlResult { enum class Type { CACHED, DOWNLOADING, FAILED_DOWNLOAD }; // @texture is null if @type is DOWNLOADING or FAILED_DOWNLOAD const sf::Texture *texture; Type type; }; class Cache { public: Cache(); // Creates directory if it doesn't exist (recursively). Throws boost exception on failure static boost::filesystem::path getDchatDir(); // 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 imageDownloadProcesses; std::mutex imageDownloadMutex; }; }