Implemented non-blocking download for the repository index list
[supertux.git] / src / addon / addon_manager.hpp
1 //  SuperTux - Add-on Manager
2 //  Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
3 //                2014 Ingo Ruhnke <grumbel@gmail.com>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #ifndef HEADER_SUPERTUX_ADDON_ADDON_MANAGER_HPP
19 #define HEADER_SUPERTUX_ADDON_ADDON_MANAGER_HPP
20
21 #include <functional>
22 #include <memory>
23 #include <string>
24 #include <vector>
25
26 #include "addon/downloader.hpp"
27 #include "supertux/gameconfig.hpp"
28 #include "util/currenton.hpp"
29 #include "util/reader_fwd.hpp"
30 #include "util/writer_fwd.hpp"
31
32 class Addon;
33 class AddonRepository;
34
35 typedef std::string AddonId;
36
37 /** Checks for, installs and removes Add-ons */
38 class AddonManager : public Currenton<AddonManager>
39 {
40 public:
41   struct InstallStatus
42   {
43     InstallStatus() :
44       now(0),
45       total(0),
46       done(false),
47       callback()
48     {}
49
50     int now;
51     int total;
52     bool done;
53     std::function<void ()> callback;
54
55     void then(const std::function<void ()>& callback_)
56     {
57       callback = callback_;
58     }
59   };
60
61   struct InstallRequest
62   {
63     InstallRequest() :
64       addon_id(),
65       install_filename()
66     {}
67
68     std::string addon_id;
69     std::string install_filename;
70   };
71
72   using InstallStatusPtr = std::shared_ptr<InstallStatus>;
73   using InstallRequestPtr = std::shared_ptr<InstallRequest>;
74
75 private:
76   using AddonList = std::vector<std::unique_ptr<Addon> >;
77
78   Downloader m_downloader;
79   std::string m_addon_directory;
80   std::string m_repository_url;
81   std::vector<Config::Addon>& m_addon_config;
82
83   AddonList m_installed_addons;
84   AddonList m_repository_addons;
85
86   bool m_has_been_updated;
87
88   InstallRequestPtr m_install_request;
89   InstallStatusPtr m_install_status;
90   TransferStatusPtr m_transfer_status;
91
92 public:
93   AddonManager(const std::string& addon_directory,
94                std::vector<Config::Addon>& addon_config);
95   ~AddonManager();
96
97   bool has_online_support() const;
98   bool has_been_updated() const;
99   void check_online();
100   InstallStatusPtr request_check_online();
101
102   std::vector<AddonId> get_repository_addons() const;
103   std::vector<AddonId> get_installed_addons() const;
104
105   Addon& get_repository_addon(const AddonId& addon);
106   Addon& get_installed_addon(const AddonId& addon);
107
108   InstallStatusPtr request_install_addon(const AddonId& addon_id);
109   void abort_install();
110   void install_addon(const AddonId& addon_id);
111   void uninstall_addon(const AddonId& addon_id);
112
113   void enable_addon(const AddonId& addon_id);
114   void disable_addon(const AddonId& addon_id);
115
116   void update();
117
118 private:
119   std::vector<std::string> scan_for_archives() const;
120   void add_installed_addons();
121   AddonList parse_addon_infos(const std::string& filename) const;
122
123   /** add \a archive, given as physfs path, to the list of installed
124       archives */
125   void add_installed_archive(const std::string& archive, const std::string& md5);
126
127   /** search for an .nfo file in the top level directory that
128       originates from \a archive, \a archive is a OS path */
129   std::string scan_for_info(const std::string& archive) const;
130
131 private:
132   AddonManager(const AddonManager&) = delete;
133   AddonManager& operator=(const AddonManager&) = delete;
134 };
135
136 #endif
137
138 /* EOF */