1 #pragma once
2 #include <string>
3 #include "util/uuid.hpp"
4 #include "common/common.hpp"
5 #include "nlohmann/json.hpp"
6 #include "util/changeable.hpp"
7 #include <glibmm/dispatcher.h>
8 #include <filesystem>
9 #include <thread>
10 #include <mutex>
11 
12 namespace horizon {
13 using json = nlohmann::json;
14 
15 class PoolStatusBase {
16 public:
17     enum class Status { BUSY, DONE };
18     Status status = Status::BUSY;
get_brief() const19     virtual std::string get_brief() const
20     {
21         return msg;
22     }
23     std::string msg = "Loading…";
24 
~PoolStatusBase()25     virtual ~PoolStatusBase()
26     {
27     }
28 };
29 
30 class PoolStatusPoolManager : public PoolStatusBase {
31 public:
32     class ItemInfo {
33     public:
34         enum class ItemState {
35             CURRENT,
36             LOCAL_ONLY,
37             REMOTE_ONLY,
38             MOVED,
39             CHANGED,
40             MOVED_CHANGED,
41         };
42 
43         std::string name;
44         std::string filename_local;
45         std::string filename_remote;
46         ObjectType type;
47         UUID uuid;
48         json delta;
49         bool merge;
50         ItemState state;
51     };
52     std::vector<ItemInfo> items;
53 
54     enum class UpdateState { NO, REQUIRED, OPTIONAL };
55 
56     UpdateState pool_info_needs_update = UpdateState::NO;
57     UpdateState layer_help_needs_update = UpdateState::NO;
58     UpdateState tables_needs_update = UpdateState::NO;
59 
60     bool can_update() const;
61 
62     std::string get_brief() const override;
63 };
64 
65 
66 class PoolStatusProviderBase : public Changeable {
67 public:
68     PoolStatusProviderBase(const std::string &bp);
69     const PoolStatusBase &get();
70     void refresh();
71     virtual void check_for_updates() = 0;
72     virtual ~PoolStatusProviderBase();
73 
74 protected:
75     void start_worker();
76     const std::filesystem::path base_path;
77     virtual void worker() = 0;
78     void worker_wrapper();
79     std::unique_ptr<PoolStatusBase> pool_status_thread;
80     std::mutex mutex;
81     Glib::Dispatcher dispatcher;
reset()82     virtual void reset()
83     {
84     }
85     void join_thread();
86 
87 private:
88     std::unique_ptr<PoolStatusBase> pool_status;
89     std::thread thread;
90 };
91 
92 class PoolStatusProviderPoolManager : public PoolStatusProviderBase {
93 public:
94     PoolStatusProviderPoolManager(const std::string &bp);
95     void apply_update(const PoolStatusPoolManager &st);
96     void check_for_updates() override;
97     ~PoolStatusProviderPoolManager();
98 
99 protected:
100     const std::filesystem::path remote_path;
101     std::optional<PoolStatusPoolManager> st_update;
102     bool do_check_updates = false;
103     void worker() override;
104     void reset() override;
105     void apply_update();
106     void check_update();
107 };
108 
109 } // namespace horizon
110