Added file writing and then-callback to Downloader
[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 <memory>
22 #include <string>
23 #include <vector>
24
25 #include "addon/downloader.hpp"
26 #include "supertux/gameconfig.hpp"
27 #include "util/currenton.hpp"
28 #include "util/reader_fwd.hpp"
29 #include "util/writer_fwd.hpp"
30
31 class Addon;
32 class AddonRepository;
33
34 typedef std::string AddonId;
35
36 /** Checks for, installs and removes Add-ons */
37 class AddonManager : public Currenton<AddonManager>
38 {
39 public:
40   struct InstallStatus
41   {
42     InstallStatus() :
43       now(0),
44       total(0),
45       done(false)
46     {}
47
48     int now;
49     int total;
50     bool done;
51   };
52
53   struct InstallRequest
54   {
55     InstallRequest() :
56       addon_id(),
57       install_filename()
58     {}
59
60     std::string addon_id;
61     std::string install_filename;
62   };
63
64   using InstallStatusPtr = std::shared_ptr<InstallStatus>;
65   using InstallRequestPtr = std::shared_ptr<InstallRequest>;
66
67 private:
68   using AddonList = std::vector<std::unique_ptr<Addon> >;
69
70   Downloader m_downloader;
71   std::string m_addon_directory;
72   std::string m_repository_url;
73   std::vector<Config::Addon>& m_addon_config;
74
75   AddonList m_installed_addons;
76   AddonList m_repository_addons;
77
78   bool m_has_been_updated;
79
80   InstallRequestPtr m_install_request;
81   InstallStatusPtr m_install_status;
82   TransferStatusPtr m_transfer_status;
83
84 public:
85   AddonManager(const std::string& addon_directory,
86                std::vector<Config::Addon>& addon_config);
87   ~AddonManager();
88
89   bool has_online_support() const;
90   bool has_been_updated() const;
91   void check_online();
92
93   std::vector<AddonId> get_repository_addons() const;
94   std::vector<AddonId> get_installed_addons() const;
95
96   Addon& get_repository_addon(const AddonId& addon);
97   Addon& get_installed_addon(const AddonId& addon);
98
99   InstallStatusPtr request_install_addon(const AddonId& addon_id);
100   void abort_install();
101   void install_addon(const AddonId& addon_id);
102   void uninstall_addon(const AddonId& addon_id);
103
104   void enable_addon(const AddonId& addon_id);
105   void disable_addon(const AddonId& addon_id);
106
107   void update();
108
109 private:
110   std::vector<std::string> scan_for_archives() const;
111   void add_installed_addons();
112   AddonList parse_addon_infos(const std::string& addoninfos) const;
113
114   /** add \a archive, given as physfs path, to the list of installed
115       archives */
116   void add_installed_archive(const std::string& archive, const std::string& md5);
117
118   /** search for an .nfo file in the top level directory that
119       originates from \a archive, \a archive is a OS path */
120   std::string scan_for_info(const std::string& archive) const;
121
122 private:
123   AddonManager(const AddonManager&) = delete;
124   AddonManager& operator=(const AddonManager&) = delete;
125 };
126
127 #endif
128
129 /* EOF */