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 private:
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;
63  m_display_count = 0;
64  m_important = important;
65  } // NewsMessage
67  const core::stringw& getNews() const {return m_news;}
69  void increaseDisplayCount() {m_display_count++;}
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 
114 public:
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
135  getNextNewsMessage();
136  const core::stringw
137  getImportantMessage();
138  void init(bool force_refresh);
139  void addNewsMessage(const core::stringw &s);
140 
141  // ------------------------------------------------------------------------
143  void setErrorMessage(const core::stringw &s)
144  {
145  m_error_message.setAtomic(s);
146  } // setErrorMessage
147  // ------------------------------------------------------------------------
149  void clearErrorMessage() {m_error_message.setAtomic(""); }
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
core::stringw m_all_news_messages
A single string that concatenats all news messages, separated by " +++ ".
Definition: news_manager.hpp:91
int getDisplayCount() const
Returns the display count.
Definition: news_manager.hpp:73
A variable that is automatically synchronised using pthreads mutex.
Definition: synchronised.hpp:27
int getMessageId() const
Returns the news id.
Definition: news_manager.hpp:71
bool isImportant() const
Returns if this is an important message.
Definition: news_manager.hpp:77
int m_current_news_message
Index of the current news message that is being displayed.
Definition: news_manager.hpp:86
Definition: three_d_animation.hpp:32
Definition: news_manager.hpp:39
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
void clearErrorMessage()
Clears the error message.
Definition: news_manager.hpp:149
void increaseDisplayCount()
Increases how often this message was being displayed.
Definition: news_manager.hpp:69
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:37
Definition: news_manager.hpp:46
utility class used to parse XML files
Definition: xml_node.hpp:47
int m_message_id
A message id used to store some information in the user config file.
Definition: news_manager.hpp:52
int m_display_count
Counts how often a message has been displayed.
Definition: news_manager.hpp:54
static std::string m_news_filename
The name of the news file on the remote server.
Definition: news_manager.hpp:81
core::stringw m_news
The actual news message.
Definition: news_manager.hpp:49
const core::stringw & getNews() const
Returns the news message.
Definition: news_manager.hpp:67
bool m_important
True if this is an important (i.e.
Definition: news_manager.hpp:56
void setDisplayCount(int n)
Sets the display count for this message.
Definition: news_manager.hpp:75
bool m_force_refresh
True when all .xml files should be re-downloaded.
Definition: news_manager.hpp:102
void setAtomic(const TYPE &v)
Sets the value of this variable using a mutex.
Definition: synchronised.hpp:59
std::vector< int > m_saved_display_count
Stores the news message display count from the user config file.
Definition: news_manager.hpp:95