SuperTuxKart
Loading...
Searching...
No Matches
addons_manager.hpp
1// SuperTuxKart - a fun racing game with go-kart
2// Copyright (C) 2010-2015 Lucas Baudin, Joerg Henrichs
3//
4// This program is free software; you can redistribute it and/or
5// modify it under the terms of the GNU General Public License
6// as published by the Free Software Foundation; either version 3
7// of the License, or (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19#ifndef HEADER_ADDONS_MANAGER_HPP
20#define HEADER_ADDONS_MANAGER_HPP
21
22#ifndef SERVER_ONLY
23
24#include <atomic>
25#include <string>
26#include <map>
27#include <memory>
28#include <vector>
29
30#include "addons/addon.hpp"
31#include "io/xml_node.hpp"
32#include "utils/synchronised.hpp"
33
38{
39private:
45 std::string m_file_installed;
46
48 std::vector<std::string> m_icon_list;
49
54 enum STATE_TYPE {STATE_INIT, STATE_READY, STATE_ERROR};
55 // Synchronise the state between threads (e.g. GUI and update thread)
57
58 /* Return true if any icons have been downloaded, so we need to call
59 * saveInstalled in mobile stk when pressing home button, so the icons
60 * won't need to be redownload when stk is killed by OS in the
61 * background. */
62 bool m_downloaded_icons;
63
64 std::atomic_bool m_has_new_addons;
65
67
68public:
71 void init(const XMLNode *xml, bool force_refresh);
72 void initAddons(const XMLNode *xml);
74 Addon* getAddon(const std::string &id);
75 int getAddonIndex(const std::string &id) const;
76 bool install(const Addon &addon);
77 bool uninstall(const Addon &addon);
78 void reInit();
79 bool anyAddonsInstalled() const;
80 void downloadIconForAddon(const std::string& addon_id,
81 std::weak_ptr<bool> result);
82 // ------------------------------------------------------------------------
86 bool onlineReady() const {return m_state.getAtomic()==STATE_READY; }
87 // ------------------------------------------------------------------------
88 bool wasError() const { return m_state.getAtomic()==STATE_ERROR;}
89 // ------------------------------------------------------------------------
90 bool isLoading() const { return m_state.getAtomic()==STATE_INIT; }
91 // ------------------------------------------------------------------------
93 void setErrorState() { m_state.setAtomic(STATE_ERROR); }
94 // ------------------------------------------------------------------------
95 void saveInstalled();
96 // ------------------------------------------------------------------------
98 unsigned int getNumAddons() const { return (unsigned int) m_addons_list.getData().size();}
99 // ------------------------------------------------------------------------
101 const Addon& getAddon(unsigned int i) { return m_addons_list.getData()[i];}
102 // ------------------------------------------------------------------------
103 bool hasDownloadedIcons() const { return m_downloaded_icons; }
104 // ------------------------------------------------------------------------
105 bool hasNewAddons() const { return m_has_new_addons; }
106}; // class AddonsManager
107
108extern AddonsManager *addons_manager;
109
110#endif
111#endif
Definition: addon.hpp:40
Definition: addons_manager.hpp:38
const Addon & getAddon(unsigned int i)
Returns the i-th addons.
Definition: addons_manager.hpp:101
std::vector< std::string > m_icon_list
List of loaded icons.
Definition: addons_manager.hpp:48
void saveInstalled()
Saves the information about installed addons and cached icons to addons_installed....
Definition: addons_manager.cpp:644
unsigned int getNumAddons() const
Returns the list of addons (installed and uninstalled).
Definition: addons_manager.hpp:98
void setErrorState()
Marks addon as not being available.
Definition: addons_manager.hpp:93
std::string m_file_installed
Full filename of the addons_installed.xml file.
Definition: addons_manager.hpp:45
void init(const XMLNode *xml, bool force_refresh)
This init function is called from a separate thread (started in news_manager, since the news....
Definition: addons_manager.cpp:97
void checkInstalledAddons()
This function checks if the information in the installed addons file is consistent with what is actua...
Definition: addons_manager.cpp:335
void reInit()
Reinitialises the addon manager, which happens when the user selects 'reload' in the addon manager.
Definition: addons_manager.cpp:324
void initAddons(const XMLNode *xml)
This initialises the online portion of the addons manager.
Definition: addons_manager.cpp:171
AddonsManager()
Initialises the non-online component of the addons manager (i.e.
Definition: addons_manager.cpp:59
Synchronised< std::vector< Addon > > m_addons_list
The list of all addons - installed or uninstalled.
Definition: addons_manager.hpp:43
bool onlineReady() const
Returns true if the list of online addons has been downloaded.
Definition: addons_manager.hpp:86
bool install(const Addon &addon)
Installs or updates (i.e.
Definition: addons_manager.cpp:511
void loadInstalledAddons()
Loads the installed addons from .../addons/addons_installed.xml.
Definition: addons_manager.cpp:444
bool uninstall(const Addon &addon)
Removes all files froma login.
Definition: addons_manager.cpp:595
int getAddonIndex(const std::string &id) const
Returns the index of the addon with the given id, or -1 if no such addon exist.
Definition: addons_manager.cpp:484
STATE_TYPE
Which state the addons manager is: INIT: Waiting to download the list of addons.
Definition: addons_manager.hpp:54
~AddonsManager()
The destructor saves the installed addons file again.
Definition: addons_manager.cpp:83
void downloadIconForAddon(const std::string &addon_id, std::weak_ptr< bool > result)
Download icon for specific addon.
Definition: addons_manager.cpp:388
Addon * getAddon(const std::string &id)
Returns an addon with a given id.
Definition: addons_manager.cpp:473
A variable that is automatically synchronised using pthreads mutex.
Definition: synchronised.hpp:28
TYPE & getData()
Returns a reference to the original data file.
Definition: synchronised.hpp:82
void setAtomic(const TYPE &v)
Sets the value of this variable using a mutex.
Definition: synchronised.hpp:59
TYPE getAtomic() const
Returns a copy of this variable.
Definition: synchronised.hpp:68
utility class used to parse XML files
Definition: xml_node.hpp:48