2011-05-12 23:51:00 +00:00
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
2015-03-30 11:31:42 +11:00
|
|
|
// Copyright (C) 2011-2015 Joerg Henrichs
|
2011-05-12 23:51:00 +00:00
|
|
|
//
|
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
|
// as published by the Free Software Foundation; either version 3
|
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
|
|
#ifndef HEADER_NEWS_MANAGER_HPP
|
|
|
|
|
#define HEADER_NEWS_MANAGER_HPP
|
|
|
|
|
|
2018-06-09 00:38:10 +02:00
|
|
|
#ifndef SERVER_ONLY
|
|
|
|
|
|
2011-05-12 23:51:00 +00:00
|
|
|
#include <string>
|
2020-04-11 00:33:29 +08:00
|
|
|
#include <thread>
|
2011-05-12 23:51:00 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
|
2011-05-22 19:29:52 +00:00
|
|
|
#include <irrString.h>
|
2011-05-12 23:51:00 +00:00
|
|
|
using namespace irr;
|
|
|
|
|
|
2014-06-04 16:51:29 +10:00
|
|
|
#include "utils/can_be_deleted.hpp"
|
2011-05-12 23:51:00 +00:00
|
|
|
#include "utils/synchronised.hpp"
|
|
|
|
|
|
|
|
|
|
class XMLNode;
|
|
|
|
|
|
2011-10-25 17:53:07 +00:00
|
|
|
/**
|
|
|
|
|
* \ingroup addonsgroup
|
|
|
|
|
*/
|
2014-06-04 16:51:29 +10:00
|
|
|
class NewsManager : public CanBeDeleted
|
2011-05-12 23:51:00 +00:00
|
|
|
{
|
|
|
|
|
private:
|
2014-01-12 11:20:32 +00:00
|
|
|
static NewsManager *m_news_manager;
|
|
|
|
|
|
2011-05-12 23:51:00 +00:00
|
|
|
// A wrapper class to store news message together with
|
|
|
|
|
// a message id and a display count.
|
|
|
|
|
class NewsMessage
|
|
|
|
|
{
|
2013-01-09 05:46:26 +00:00
|
|
|
/** The actual news message. */
|
2011-05-12 23:51:00 +00:00
|
|
|
core::stringw m_news;
|
2013-01-09 05:46:26 +00:00
|
|
|
/** A message id used to store some information in the
|
|
|
|
|
* user config file. */
|
2011-05-12 23:51:00 +00:00
|
|
|
int m_message_id;
|
2013-01-09 05:46:26 +00:00
|
|
|
/** Counts how often a message has been displayed. */
|
2011-05-12 23:51:00 +00:00
|
|
|
int m_display_count;
|
2013-01-09 05:46:26 +00:00
|
|
|
/** True if this is an important (i.e. popup) message. */
|
|
|
|
|
bool m_important;
|
|
|
|
|
|
2011-05-12 23:51:00 +00:00
|
|
|
public:
|
2013-01-09 05:46:26 +00:00
|
|
|
NewsMessage(const core::stringw &m, int id, bool important=false)
|
2011-05-12 23:51:00 +00:00
|
|
|
{
|
|
|
|
|
m_news = m;
|
|
|
|
|
m_message_id = id;
|
|
|
|
|
m_display_count = 0;
|
2013-01-09 05:46:26 +00:00
|
|
|
m_important = important;
|
2011-05-12 23:51:00 +00:00
|
|
|
} // NewsMessage
|
|
|
|
|
/** Returns the news message. */
|
|
|
|
|
const core::stringw& getNews() const {return m_news;}
|
|
|
|
|
/** Increases how often this message was being displayed. */
|
|
|
|
|
void increaseDisplayCount() {m_display_count++;}
|
|
|
|
|
/** Returns the news id. */
|
|
|
|
|
int getMessageId() const {return m_message_id;}
|
|
|
|
|
/** Returns the display count. */
|
|
|
|
|
int getDisplayCount() const {return m_display_count; }
|
|
|
|
|
/** Sets the display count for this message. */
|
|
|
|
|
void setDisplayCount(int n) {m_display_count = n; }
|
2013-01-09 05:46:26 +00:00
|
|
|
/** Returns if this is an important message. */
|
|
|
|
|
bool isImportant() const { return m_important; }
|
2011-05-12 23:51:00 +00:00
|
|
|
}; // NewsMessage
|
|
|
|
|
|
2019-04-19 20:31:26 +03:00
|
|
|
/** The name of the news file on the remote server */
|
|
|
|
|
static std::string m_news_filename;
|
|
|
|
|
|
2011-05-12 23:51:00 +00:00
|
|
|
mutable Synchronised< std::vector<NewsMessage> > m_news;
|
|
|
|
|
|
|
|
|
|
/** Index of the current news message that is being displayed. */
|
|
|
|
|
int m_current_news_message;
|
|
|
|
|
|
2013-02-05 22:11:43 +00:00
|
|
|
/** A single string that concatenats all news messages, separated
|
|
|
|
|
* by " +++ ". Using this to display the news message avoids
|
|
|
|
|
* the delay between messages. */
|
|
|
|
|
core::stringw m_all_news_messages;
|
|
|
|
|
|
2013-05-30 19:47:39 +00:00
|
|
|
/** Stores the news message display count from the user config file.
|
2011-05-12 23:51:00 +00:00
|
|
|
*/
|
|
|
|
|
std::vector<int> m_saved_display_count;
|
|
|
|
|
|
2011-05-24 12:15:54 +00:00
|
|
|
/** A high priority error message that is shown instead of
|
|
|
|
|
* any news message (usually indicating connection problems). */
|
2014-01-12 11:20:32 +00:00
|
|
|
Synchronised<core::stringw> m_error_message;
|
2011-05-12 23:51:00 +00:00
|
|
|
|
2014-01-13 05:21:48 +00:00
|
|
|
/** True when all .xml files should be re-downloaded. */
|
|
|
|
|
bool m_force_refresh;
|
|
|
|
|
|
2020-04-11 00:33:29 +08:00
|
|
|
std::thread m_download_thread;
|
|
|
|
|
|
2011-05-12 23:51:00 +00:00
|
|
|
void checkRedirect(const XMLNode *xml);
|
|
|
|
|
void updateNews(const XMLNode *xml,
|
|
|
|
|
const std::string &filename);
|
|
|
|
|
bool conditionFulfilled(const std::string &cond);
|
2020-04-11 00:33:29 +08:00
|
|
|
void downloadNews();
|
2014-01-12 11:20:32 +00:00
|
|
|
NewsManager();
|
|
|
|
|
~NewsManager();
|
2011-05-12 23:51:00 +00:00
|
|
|
|
|
|
|
|
public:
|
2014-01-12 11:20:32 +00:00
|
|
|
/** Singleton: if necessary create and get the news managers */
|
|
|
|
|
static NewsManager* get()
|
|
|
|
|
{
|
|
|
|
|
if(!m_news_manager)
|
|
|
|
|
m_news_manager = new NewsManager();
|
|
|
|
|
return m_news_manager;
|
2014-01-13 05:21:48 +00:00
|
|
|
} // get
|
2014-01-12 11:20:32 +00:00
|
|
|
// ------------------------------------------------------------------------
|
2019-07-24 01:33:12 +08:00
|
|
|
static bool isRunning() { return m_news_manager != NULL; }
|
|
|
|
|
// ------------------------------------------------------------------------
|
2014-01-12 11:20:32 +00:00
|
|
|
static void deallocate()
|
|
|
|
|
{
|
|
|
|
|
if(m_news_manager)
|
|
|
|
|
{
|
|
|
|
|
delete m_news_manager;
|
|
|
|
|
m_news_manager = NULL;
|
|
|
|
|
}
|
|
|
|
|
} // deallocate
|
|
|
|
|
// ------------------------------------------------------------------------
|
2011-05-12 23:51:00 +00:00
|
|
|
const core::stringw
|
|
|
|
|
getNextNewsMessage();
|
2013-05-30 19:47:39 +00:00
|
|
|
const core::stringw
|
2013-01-09 05:46:26 +00:00
|
|
|
getImportantMessage();
|
2014-01-12 11:20:32 +00:00
|
|
|
void init(bool force_refresh);
|
2011-05-12 23:51:00 +00:00
|
|
|
void addNewsMessage(const core::stringw &s);
|
2011-05-24 12:15:54 +00:00
|
|
|
|
2014-01-12 11:20:32 +00:00
|
|
|
// ------------------------------------------------------------------------
|
2011-05-24 12:15:54 +00:00
|
|
|
/** Sets an error message that is displayed instead of any news message. */
|
2014-01-12 11:20:32 +00:00
|
|
|
void setErrorMessage(const core::stringw &s)
|
|
|
|
|
{
|
|
|
|
|
m_error_message.setAtomic(s);
|
|
|
|
|
} // setErrorMessage
|
2011-05-24 12:15:54 +00:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
/** Clears the error message. */
|
2014-01-12 11:20:32 +00:00
|
|
|
void clearErrorMessage() {m_error_message.setAtomic(""); }
|
2011-05-24 12:15:54 +00:00
|
|
|
// ------------------------------------------------------------------------
|
2020-04-11 00:33:29 +08:00
|
|
|
void joinDownloadThreadIfExit()
|
|
|
|
|
{
|
|
|
|
|
if (CanBeDeleted::canBeDeletedNow() && m_download_thread.joinable())
|
|
|
|
|
m_download_thread.join();
|
|
|
|
|
}
|
2011-05-12 23:51:00 +00:00
|
|
|
}; // NewsManager
|
|
|
|
|
|
|
|
|
|
#endif
|
2018-06-09 00:38:10 +02:00
|
|
|
#endif
|