SuperTuxKart
Loading...
Searching...
No Matches
news_manager.hpp
1// SuperTuxKart - a fun racing game with go-kart
2// Copyright (C) 2011-2015 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#ifndef HEADER_NEWS_MANAGER_HPP
19#define HEADER_NEWS_MANAGER_HPP
20
21#ifndef SERVER_ONLY
22
23#include <string>
24#include <thread>
25#include <vector>
26
27
28#include <irrString.h>
29using namespace irr;
30
31#include "utils/can_be_deleted.hpp"
32#include "utils/synchronised.hpp"
33
34class XMLNode;
35
40{
41private:
42 static NewsManager *m_news_manager;
43
44 // A wrapper class to store news message together with
45 // a message id and a display count.
47 {
49 core::stringw m_news;
57
58 public:
59 NewsMessage(const core::stringw &m, int id, bool important=false)
60 {
61 m_news = m;
62 m_message_id = id;
64 m_important = important;
65 } // NewsMessage
67 const core::stringw& getNews() const {return m_news;}
71 int getMessageId() const {return m_message_id;}
73 int getDisplayCount() const {return m_display_count; }
75 void setDisplayCount(int n) {m_display_count = n; }
77 bool isImportant() const { return m_important; }
78 }; // NewsMessage
79
81 static std::string m_news_filename;
82
84
87
91 core::stringw m_all_news_messages;
92
95 std::vector<int> m_saved_display_count;
96
100
103
104 std::thread m_download_thread;
105
106 void checkRedirect(const XMLNode *xml);
107 void updateNews(const XMLNode *xml,
108 const std::string &filename);
109 bool conditionFulfilled(const std::string &cond);
110 void downloadNews();
111 NewsManager();
112 ~NewsManager();
113
114public:
116 static NewsManager* get()
117 {
118 if(!m_news_manager)
119 m_news_manager = new NewsManager();
120 return m_news_manager;
121 } // get
122 // ------------------------------------------------------------------------
123 static bool isRunning() { return m_news_manager != NULL; }
124 // ------------------------------------------------------------------------
125 static void deallocate()
126 {
127 if(m_news_manager)
128 {
129 delete m_news_manager;
130 m_news_manager = NULL;
131 }
132 } // deallocate
133 // ------------------------------------------------------------------------
134 const core::stringw
136 const core::stringw
138 void init(bool force_refresh);
139 void addNewsMessage(const core::stringw &s);
140
141 // ------------------------------------------------------------------------
143 void setErrorMessage(const core::stringw &s)
144 {
146 } // setErrorMessage
147 // ------------------------------------------------------------------------
150 // ------------------------------------------------------------------------
151 void joinDownloadThreadIfExit()
152 {
153 if (CanBeDeleted::canBeDeletedNow() && m_download_thread.joinable())
154 m_download_thread.join();
155 }
156}; // NewsManager
157
158#endif
159#endif
A simple class that a adds a function to wait with a timeout for a class to be ready to be deleted.
Definition: can_be_deleted.hpp:38
Definition: news_manager.hpp:47
const core::stringw & getNews() const
Returns the news message.
Definition: news_manager.hpp:67
int getDisplayCount() const
Returns the display count.
Definition: news_manager.hpp:73
bool m_important
True if this is an important (i.e.
Definition: news_manager.hpp:56
core::stringw m_news
The actual news message.
Definition: news_manager.hpp:49
int m_message_id
A message id used to store some information in the user config file.
Definition: news_manager.hpp:52
bool isImportant() const
Returns if this is an important message.
Definition: news_manager.hpp:77
int getMessageId() const
Returns the news id.
Definition: news_manager.hpp:71
void setDisplayCount(int n)
Sets the display count for this message.
Definition: news_manager.hpp:75
int m_display_count
Counts how often a message has been displayed.
Definition: news_manager.hpp:54
void increaseDisplayCount()
Increases how often this message was being displayed.
Definition: news_manager.hpp:69
Definition: news_manager.hpp:40
bool conditionFulfilled(const std::string &cond)
Checks if the given condition list are all fulfilled.
Definition: news_manager.cpp:448
static std::string m_news_filename
The name of the news file on the remote server.
Definition: news_manager.hpp:81
void checkRedirect(const XMLNode *xml)
Checks if a redirect is received, causing a new server to be used for downloading addons.
Definition: news_manager.cpp:223
core::stringw m_all_news_messages
A single string that concatenats all news messages, separated by " +++ ".
Definition: news_manager.hpp:91
int m_current_news_message
Index of the current news message that is being displayed.
Definition: news_manager.hpp:86
void setErrorMessage(const core::stringw &s)
Sets an error message that is displayed instead of any news message.
Definition: news_manager.hpp:143
Synchronised< core::stringw > m_error_message
A high priority error message that is shown instead of any news message (usually indicating connectio...
Definition: news_manager.hpp:99
std::vector< int > m_saved_display_count
Stores the news message display count from the user config file.
Definition: news_manager.hpp:95
const core::stringw getNextNewsMessage()
Returns the next loaded news message.
Definition: news_manager.cpp:404
void updateNews(const XMLNode *xml, const std::string &filename)
Updates the 'news' string to be displayed in the main menu.
Definition: news_manager.cpp:286
void init(bool force_refresh)
This function initialises the data for the news manager.
Definition: news_manager.cpp:79
void downloadNews()
This function submits request which will download the m_news_filename file if necessary.
Definition: news_manager.cpp:103
void clearErrorMessage()
Clears the error message.
Definition: news_manager.hpp:149
static NewsManager * get()
Singleton: if necessary create and get the news managers.
Definition: news_manager.hpp:116
const core::stringw getImportantMessage()
Returns the important message with the smallest id that has not been shown, or NULL if no important (...
Definition: news_manager.cpp:369
bool m_force_refresh
True when all .xml files should be re-downloaded.
Definition: news_manager.hpp:102
void addNewsMessage(const core::stringw &s)
Add a news message.
Definition: news_manager.cpp:357
A variable that is automatically synchronised using pthreads mutex.
Definition: synchronised.hpp:28
void setAtomic(const TYPE &v)
Sets the value of this variable using a mutex.
Definition: synchronised.hpp:59
utility class used to parse XML files
Definition: xml_node.hpp:48