From d53e3180ac81ad186f650b06844e6b129227f7e9 Mon Sep 17 00:00:00 2001 From: hikerstk Date: Mon, 13 Jan 2014 11:35:12 +0000 Subject: [PATCH] Fixed enabling internet in the GUI (i.e. re-init of news manager and addons manager will happen now). For online menu a dialog is now presented to explain why it can't be selected if internet is disabled. Addons can be selected with internet is disabled if there are existing addons (so that it's possible to remove addons without internet access). git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@15035 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/addons/addons_manager.cpp | 18 +++++++++++--- src/addons/addons_manager.hpp | 1 + src/addons/news_manager.cpp | 30 ++++++++++-------------- src/online/http_request.cpp | 13 ++++++++-- src/online/request_manager.hpp | 20 ++++++++-------- src/states_screens/main_menu_screen.cpp | 19 +++++++++++++++ src/states_screens/options_screen_ui.cpp | 14 +++++++---- 7 files changed, 79 insertions(+), 36 deletions(-) diff --git a/src/addons/addons_manager.cpp b/src/addons/addons_manager.cpp index c54bbd9e0..42974a75f 100644 --- a/src/addons/addons_manager.cpp +++ b/src/addons/addons_manager.cpp @@ -98,14 +98,18 @@ void AddonsManager::init(const XMLNode *xml, } include->get("file", &addon_list_url); + int frequency = 0; + include->get("frequency", &frequency); int64_t tmp; include->get("mtime", &tmp); mtime = tmp; - bool download = mtime > UserConfigParams::m_addons_last_updated || - force_refresh || - !file_manager->fileExists(filename); + bool download = + ( mtime > UserConfigParams::m_addons_last_updated +frequency || + force_refresh || + !file_manager->fileExists(filename) ) + && UserConfigParams::m_internet_status == RequestManager::IPERM_ALLOWED; if (download) { @@ -445,6 +449,14 @@ int AddonsManager::getAddonIndex(const std::string &id) const } return -1; } // getAddonIndex +// ---------------------------------------------------------------------------- +bool AddonsManager::anyAddonsInstalled() const +{ + for(unsigned int i=0; i +using namespace Online; + NewsManager *NewsManager::m_news_manager=NULL; // ---------------------------------------------------------------------------- @@ -96,16 +99,17 @@ void* NewsManager::downloadNews(void *obj) // or if the time of the last update was more than news_frequency ago, // or because a 'refresh' was explicitly requested by the user, or no // news.xml file exists. - bool download = UserConfigParams::m_news_last_updated==0 || - UserConfigParams::m_news_last_updated - +UserConfigParams::m_news_frequency - < StkTime::getTimeSinceEpoch() || - me->m_force_refresh || - !news_exists; + bool download = ( UserConfigParams::m_news_last_updated==0 || + UserConfigParams::m_news_last_updated + +UserConfigParams::m_news_frequency + < StkTime::getTimeSinceEpoch() || + me->m_force_refresh || + !news_exists ) + && UserConfigParams::m_internet_status==RequestManager::IPERM_ALLOWED; const XMLNode *xml = NULL; - if(!download) + if(!download && news_exists) { // If (so far) we don't need to download, there should be an existing // file. Try to read this, and do some basic checks @@ -131,7 +135,7 @@ void* NewsManager::downloadNews(void *obj) { core::stringw error_message(""); - Online::HTTPRequest *download_req = new Online::HTTPRequest("news.xml"); + HTTPRequest *download_req = new HTTPRequest("news.xml"); download_req->setAddonsURL("news.xml"); // Initialise the online portion of the addons manager. if(UserConfigParams::logAddons()) @@ -147,7 +151,7 @@ void* NewsManager::downloadNews(void *obj) delete download_req; // We need a new object, since the state of the old // download request is now done. - download_req = new Online::HTTPRequest("news.xml"); + download_req = new HTTPRequest("news.xml"); UserConfigParams::m_server_addons.revertToDefaults(); // make sure the new server address is actually used download_req->setAddonsURL("news.xml"); @@ -191,14 +195,6 @@ void* NewsManager::downloadNews(void *obj) return 0; // prevent warning } // downloadNews -// --------------------------------------------------------------------------- -/** Initialises the online part of the network manager. It downloads the - * news.xml file from the server (if the frequency of downloads makes this - * necessary), and (again if necessary) the addons.xml file. - * \return 0 if an error happened and no online connection will be available, - * 1 otherwise. - */ - // --------------------------------------------------------------------------- /** Checks if a redirect is received, causing a new server to be used for * downloading addons. diff --git a/src/online/http_request.cpp b/src/online/http_request.cpp index 81593b038..5e5cab606 100644 --- a/src/online/http_request.cpp +++ b/src/online/http_request.cpp @@ -57,6 +57,9 @@ namespace Online int priority) : Request(manage_memory, priority, 0) { + // A http request should not even be created when internet is disabled + assert(UserConfigParams::m_internet_status == + RequestManager::IPERM_ALLOWED); assert(filename.size()>0); init(); m_filename = file_manager->getAddonsFile(filename); @@ -70,6 +73,9 @@ namespace Online int priority) : Request(manage_memory, priority, 0) { + // A http request should not even be created when internet is disabled + assert(UserConfigParams::m_internet_status == + RequestManager::IPERM_ALLOWED); init(); m_filename = file_manager->getAddonsFile(filename); } // HTTPRequest(filename ...) @@ -188,8 +194,11 @@ namespace Online m_parameters.erase(m_parameters.size()-1); } - Log::info("HTTPRequest", "Sending %s to %s", - m_parameters.c_str(), m_url.c_str()); + if(m_parameters.size()==0) + Log::info("HTTPRequest", "Downloading %s", m_url.c_str()); + else + Log::info("HTTPRequest", "Sending %s to %s", + m_parameters.c_str(), m_url.c_str()); curl_easy_setopt(m_curl_session, CURLOPT_POSTFIELDS, m_parameters.c_str()); std::string uagent( std::string("SuperTuxKart/") + STK_VERSION ); diff --git a/src/online/request_manager.hpp b/src/online/request_manager.hpp index 682f1a10f..6f060442a 100644 --- a/src/online/request_manager.hpp +++ b/src/online/request_manager.hpp @@ -48,16 +48,16 @@ namespace Online */ class RequestManager { - public: - /** If stk has permission to access the internet (for news - * server etc). - * IPERM_NOT_ASKED: The user needs to be asked if he wants to - * grant permission - * IPERM_ALLOWED: STK is allowed to access server. - * IPERM_NOT_ALLOWED: STK must not access external servers. */ - enum InternetPermission {IPERM_NOT_ASKED =0, - IPERM_ALLOWED =1, - IPERM_NOT_ALLOWED=2 }; + public: + /** If stk has permission to access the internet (for news + * server etc). + * IPERM_NOT_ASKED: The user needs to be asked if he wants to + * grant permission + * IPERM_ALLOWED: STK is allowed to access server. + * IPERM_NOT_ALLOWED: STK must not access external servers. */ + enum InternetPermission {IPERM_NOT_ASKED =0, + IPERM_ALLOWED =1, + IPERM_NOT_ALLOWED=2 }; protected: float m_time_since_poll; diff --git a/src/states_screens/main_menu_screen.cpp b/src/states_screens/main_menu_screen.cpp index 16436ee7d..4c56e1624 100644 --- a/src/states_screens/main_menu_screen.cpp +++ b/src/states_screens/main_menu_screen.cpp @@ -372,10 +372,29 @@ void MainMenuScreen::eventCallback(Widget* widget, const std::string& name, } else if (selection == "online") { + if(UserConfigParams::m_internet_status!=RequestManager::IPERM_ALLOWED) + { + new MessageDialog(_("You can not play online without internet access. " + "If you want to play online, go to options, select " + " tab 'User Interface', and edit " + "\"Allow STK to connect to the Internet\".")); + return; + } StateManager::get()->pushScreen(OnlineScreen::getInstance()); } else if (selection == "addons") { + // Don't go to addons if there is no internet, unless some addons are + // already installed (so that you can delete addons without being online). + if(UserConfigParams::m_internet_status!=RequestManager::IPERM_ALLOWED && + !addons_manager->anyAddonsInstalled()) + { + new MessageDialog(_("You can not download addons without internet access. " + "If you want to download addons, go to options, select " + " tab 'User Interface', and edit " + "\"Allow STK to connect to the Internet\".")); + return; + } StateManager::get()->pushScreen(AddonsScreen::getInstance()); } } // eventCallback diff --git a/src/states_screens/options_screen_ui.cpp b/src/states_screens/options_screen_ui.cpp index ded5aba45..927355c8e 100644 --- a/src/states_screens/options_screen_ui.cpp +++ b/src/states_screens/options_screen_ui.cpp @@ -17,6 +17,7 @@ #include "states_screens/options_screen_ui.hpp" +#include "addons/news_manager.hpp" #include "audio/music_manager.hpp" #include "audio/sfx_manager.hpp" #include "audio/sfx_base.hpp" @@ -228,11 +229,16 @@ void OptionsScreenUI::eventCallback(Widget* widget, const std::string& name, con } else if (name=="enable-internet") { - CheckBoxWidget* news = getWidget("enable-internet"); - assert( news != NULL ); + CheckBoxWidget* internet = getWidget("enable-internet"); + assert( internet != NULL ); UserConfigParams::m_internet_status = - news->getState() ? RequestManager::IPERM_ALLOWED - : RequestManager::IPERM_NOT_ALLOWED; + internet->getState() ? RequestManager::IPERM_ALLOWED + : RequestManager::IPERM_NOT_ALLOWED; + // If internet gets enabled, re-initialise the addon manager (which + // happens in a separate thread) so that news.xml etc can be + // downloaded if necessary. + if(internet->getState()) + NewsManager::get()->init(false); } else if (name == "language") {