SuperTuxKart
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>
29 using namespace irr;
30 
31 #include "utils/can_be_deleted.hpp"
32 #include "utils/synchronised.hpp"
33 
34 class XMLNode;
35 
39 class NewsManager : public CanBeDeleted
40 {
41 public:
42  // Means the exact place the news being shown
43  enum NewsType : uint8_t
44  {
45  NTYPE_MAINMENU,
46  NTYPE_LIST,
47  NTYPE_COUNT
48  };
49 
50 private:
51  static NewsManager *m_news_manager;
52 
53  // A wrapper class to store news message together with
54  // a message id and a display count.
56  {
58  core::stringw m_news;
60  std::string m_date;
61  std::string m_link;
69 
70  public:
71  NewsMessage(const core::stringw &m, int id, const std::string &date="",
72  const std::string &link="", bool important=false)
73  {
74  m_news = m;
75  m_date = date;
76  m_link = link;
77  m_message_id = id;
78  m_display_count = 0;
79  m_important = important;
80  } // NewsMessage
82  const core::stringw& getNews() const { return m_news; }
84  const std::string& getDate() const { return m_date; }
86  const std::string& getLink() const { return m_link; }
88  void increaseDisplayCount() { m_display_count++; }
90  int getMessageId() const { return m_message_id; }
92  int getDisplayCount() const { return m_display_count; }
94  void setDisplayCount(int n) { m_display_count = n; }
96  bool isImportant() const { return m_important; }
98  bool operator<(NewsMessage other) const
99  {
100  return m_important == other.m_important
101  ? m_message_id > other.m_message_id
102  : m_important;
103  }
104  }; // NewsMessage
105 
107  static std::string m_news_filename;
108 
110  mutable Synchronised< std::vector<NewsMessage> > m_news[NTYPE_COUNT];
111 
113  int m_current_news_ptr[NTYPE_COUNT];
115  int m_news_prioritize_after_id[NTYPE_COUNT];
116 
119  std::vector<int> m_saved_display_count;
120 
124 
127 
128  std::thread m_download_thread;
129 
130  void checkRedirect(const XMLNode *xml);
131  void updateNews(const XMLNode *xml,
132  const std::string &filename);
133  bool conditionFulfilled(const std::string &cond);
134  void downloadNews();
135  NewsManager();
136  ~NewsManager();
137 
138 public:
140  static NewsManager* get()
141  {
142  if(!m_news_manager)
143  m_news_manager = new NewsManager();
144  return m_news_manager;
145  } // get
146  // ------------------------------------------------------------------------
147  static bool isRunning() { return m_news_manager != NULL; }
148  // ------------------------------------------------------------------------
149  static void deallocate()
150  {
151  if(m_news_manager)
152  {
153  delete m_news_manager;
154  m_news_manager = NULL;
155  }
156  } // deallocate
157  // ------------------------------------------------------------------------
161  const int getNextNewsID(NewsType type);
162  const core::stringw
163  getCurrentNewsMessage(NewsType type);
164  const std::string
165  getCurrentNewsDate(NewsType type);
166  const std::string
167  getCurrentNewsLink(NewsType type);
168  const bool isCurrentNewsImportant(NewsType type);
169  const int getNewsCount(NewsType type);
170 
172  void prioritizeNewsAfterID(NewsType type, int id);
173 
174  void init(bool force_refresh);
175  void addNewsMessage(NewsType type, const core::stringw &s);
176 
177  // ------------------------------------------------------------------------
179  void resetNewsPtr(NewsType type) { m_current_news_ptr[type] = -1; }
181  bool isNewsFetching(NewsType type) { return m_current_news_ptr[type] != -1; }
182  // ------------------------------------------------------------------------
184  void setErrorMessage(const core::stringw &s)
185  {
186  m_error_message.setAtomic(s);
187  } // setErrorMessage
188  // ------------------------------------------------------------------------
190  void clearErrorMessage() { m_error_message.setAtomic(""); }
191  // ------------------------------------------------------------------------
192  void joinDownloadThreadIfExit()
193  {
194  if (CanBeDeleted::canBeDeletedNow() && m_download_thread.joinable())
195  m_download_thread.join();
196  }
197 }; // NewsManager
198 
199 #endif
200 #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:56
const std::string & getLink() const
Returns the link of the news.
Definition: news_manager.hpp:86
bool operator<(NewsMessage other) const
For sorting the news.
Definition: news_manager.hpp:98
int getDisplayCount() const
Returns the display count.
Definition: news_manager.hpp:92
bool m_important
True if this is an important (i.e.
Definition: news_manager.hpp:68
core::stringw m_news
The actual news message.
Definition: news_manager.hpp:58
std::string m_date
Additional data.
Definition: news_manager.hpp:60
int m_message_id
A message id used to store some information in the user config file.
Definition: news_manager.hpp:64
bool isImportant() const
Returns if this is an important message.
Definition: news_manager.hpp:96
int getMessageId() const
Returns the news id.
Definition: news_manager.hpp:90
void setDisplayCount(int n)
Sets the display count for this message.
Definition: news_manager.hpp:94
const core::stringw & getNews() const
Returns the news message.
Definition: news_manager.hpp:82
int m_display_count
Counts how often a message has been displayed.
Definition: news_manager.hpp:66
const std::string & getDate() const
Returns the date of the news.
Definition: news_manager.hpp:84
void increaseDisplayCount()
Increases how often this message was being displayed.
Definition: news_manager.hpp:88
Definition: news_manager.hpp:40
static std::string m_news_filename
The name of the news file on the remote server.
Definition: news_manager.hpp:107
bool isNewsFetching(NewsType type)
Check if this type of news is on the way of fetching.
Definition: news_manager.hpp:181
void setErrorMessage(const core::stringw &s)
Sets an error message that is displayed instead of any news message.
Definition: news_manager.hpp:184
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:123
std::vector< int > m_saved_display_count
Stores the news message display count from the user config file.
Definition: news_manager.hpp:119
static NewsManager * get()
Singleton: if necessary create and get the news managers.
Definition: news_manager.hpp:140
void resetNewsPtr(NewsType type)
Goes back to the place before first message when called.
Definition: news_manager.hpp:179
void clearErrorMessage()
Clears the error message.
Definition: news_manager.hpp:190
bool m_force_refresh
True when all .xml files should be re-downloaded.
Definition: news_manager.hpp:126
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
void deallocate()
To be called after cleanup().
Definition: engine.cpp:1077