1
0
mirror of synced 2024-12-25 05:54:50 +01:00
ImHex/plugins/builtin/include/content/views/view_store.hpp

87 lines
2.5 KiB
C++
Raw Normal View History

#pragma once
#include <hex.hpp>
#include <hex/ui/view.hpp>
2023-11-30 10:22:15 +01:00
#include <hex/api/task_manager.hpp>
#include <hex/helpers/http_requests.hpp>
#include <hex/helpers/fs.hpp>
#include <hex/helpers/default_paths.hpp>
#include <future>
#include <string>
#include <filesystem>
2021-12-07 22:47:41 +01:00
namespace hex::plugin::builtin {
enum class RequestStatus {
NotAttempted,
InProgress,
Failed,
Succeeded,
};
struct StoreEntry {
std::string name;
std::string description;
std::vector<std::string> authors;
std::string fileName;
std::string link;
std::string hash;
bool isFolder;
bool downloading;
bool installed;
bool hasUpdate;
bool system;
};
struct StoreCategory {
UnlocalizedString unlocalizedName;
std::string requestName;
const paths::impl::DefaultPath* path;
std::vector<StoreEntry> entries;
std::function<void()> downloadCallback;
};
class ViewStore : public View::Floating {
public:
ViewStore();
~ViewStore() override = default;
void drawContent() override;
[[nodiscard]] bool shouldDraw() const override { return true; }
2022-01-10 21:05:37 +01:00
[[nodiscard]] bool hasViewMenuItemEntry() const override { return false; }
[[nodiscard]] ImVec2 getMinSize() const override { return scaled({ 600, 400 }); }
[[nodiscard]] ImVec2 getMaxSize() const override { return scaled({ 900, 700 }); }
private:
void drawStore();
void drawTab(StoreCategory &category);
void handleDownloadFinished(const StoreCategory &category, StoreEntry &entry);
void refresh();
void parseResponse();
void addCategory(const UnlocalizedString &unlocalizedName, const std::string &requestName, const paths::impl::DefaultPath *path, std::function<void()> downloadCallback = []{});
bool download(const paths::impl::DefaultPath *pathType, const std::string &fileName, const std::string &url);
bool remove(const paths::impl::DefaultPath *pathType, const std::string &fileName);
2023-12-27 16:33:49 +01:00
private:
HttpRequest m_httpRequest = HttpRequest("GET", "");
std::future<HttpRequest::Result<std::string>> m_apiRequest;
std::future<HttpRequest::Result<std::string>> m_download;
std::fs::path m_downloadPath;
RequestStatus m_requestStatus = RequestStatus::NotAttempted;
std::vector<StoreCategory> m_categories;
TaskHolder m_updateAllTask;
std::atomic<u32> m_updateCount = 0;
};
}