diff --git a/sources.cmake b/sources.cmake index 532b8da7d..66789c1bd 100644 --- a/sources.cmake +++ b/sources.cmake @@ -143,6 +143,9 @@ src/network/network_manager.cpp src/network/race_info_message.cpp src/network/race_result_message.cpp src/network/race_state.cpp +src/online/current_online_user.cpp +src/online/http_functions.cpp +src/online/online_user.cpp src/physics/btKart.cpp src/physics/btKartRaycast.cpp src/physics/btUprightConstraint.cpp @@ -402,6 +405,9 @@ src/network/race_start_message.hpp src/network/race_state.hpp src/network/remote_kart_info.hpp src/network/world_loaded_message.hpp +src/online/current_online_user.hpp +src/online/http_functions.hpp +src/online/online_user.hpp src/physics/btKart.hpp src/physics/btKartRaycast.hpp src/physics/btUprightConstraint.hpp diff --git a/src/io/file_manager.cpp b/src/io/file_manager.cpp index 966a38a58..6309960de 100644 --- a/src/io/file_manager.cpp +++ b/src/io/file_manager.cpp @@ -302,7 +302,33 @@ XMLNode *FileManager::createXMLTree(const std::string &filename) } return NULL; } -} // getXMLTree +} // createXMLTree + +//----------------------------------------------------------------------------- +/** Reads in XML from a string and converts it into a XMLNode tree. + * \param content the string containing the XML content. + */ +XMLNode *FileManager::createXMLTreeFromString(const std::string & content) +{ + try + { + char *b = new char[content.size()]; + memcpy(b, content.c_str(), content.size()); + io::IReadFile * ireadfile = m_file_system->createMemoryReadFile(b, strlen(b), "tempfile", true); + io::IXMLReader * reader = m_file_system->createXMLReader(ireadfile); + XMLNode* node = new XMLNode(reader); + reader->drop(); + return node; + } + catch (std::runtime_error& e) + { + if (UserConfigParams::logMisc()) + { + Log::error("FileManager", "createXMLTreeFromString: %s\n", e.what()); + } + return NULL; + } +} // createXMLTreeFromString //----------------------------------------------------------------------------- /** In order to add and later remove paths we have to specify the absolute diff --git a/src/io/file_manager.hpp b/src/io/file_manager.hpp index 132f7c17d..77daf4d35 100644 --- a/src/io/file_manager.hpp +++ b/src/io/file_manager.hpp @@ -88,6 +88,7 @@ public: void dropFileSystem(); io::IXMLReader *createXMLReader(const std::string &filename); XMLNode *createXMLTree(const std::string &filename); + XMLNode *createXMLTreeFromString(const std::string & content); std::string getConfigDir() const; std::string getTextureDir() const; diff --git a/src/online/current_online_user.cpp b/src/online/current_online_user.cpp new file mode 100644 index 000000000..f860b8618 --- /dev/null +++ b/src/online/current_online_user.cpp @@ -0,0 +1,77 @@ +// +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2013 Glenn De Jonghe +// +// 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. + + +#include "online/current_online_user.hpp" + +#include +#include +#include + +static CurrentOnlineUser* user_singleton = NULL; + +CurrentOnlineUser* CurrentOnlineUser::get() +{ + if (user_singleton == NULL) + user_singleton = new CurrentOnlineUser(); + return user_singleton; +} // get + +void CurrentOnlineUser::deallocate() +{ + delete user_singleton; + user_singleton = NULL; +} // deallocate + +// ============================================================================ + + +CurrentOnlineUser::CurrentOnlineUser(){ + m_is_signed_in = false; +} + +// ============================================================================ + +bool CurrentOnlineUser::logIn(const std::string &username, const std::string &password) +{ + assert(m_is_signed_in == false); + //Sign in + if(/*succes*/true) + { + m_user = new OnlineUser(username); + m_is_signed_in = true; + } + else + { + //I don't know if something should happen here yet + } + return m_is_signed_in; +} + +// ============================================================================ + +std::string CurrentOnlineUser::getUserName() const +{ + if(m_is_signed_in){ + assert(m_user != NULL); + return m_user->getUserName(); + }else{ + return "Guest"; + } + +} diff --git a/src/online/current_online_user.hpp b/src/online/current_online_user.hpp new file mode 100644 index 000000000..00d5038ae --- /dev/null +++ b/src/online/current_online_user.hpp @@ -0,0 +1,55 @@ +// +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2013 Glenn De Jonghe +// +// 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_CURRENT_ONLINE_USER_HPP +#define HEADER_CURRENT_ONLINE_USER_HPP + +#include + +#include "online/online_user.hpp" + + +// ============================================================================ + +/** + * \brief Class that represents an online registered user + * \ingroup online + */ +class CurrentOnlineUser +{ +private: + +protected: + std::string m_token; + bool m_is_signed_in; + OnlineUser * m_user; + CurrentOnlineUser(); + +public: + // singleton + static CurrentOnlineUser* get(); + static void deallocate(); + bool logIn(const std::string &username, const std::string &password); + /** Returns the username if signed in. */ + std::string getUserName() const; + +}; // class CurrentOnlineUser + +#endif + +/*EOF*/ diff --git a/src/online/http_functions.cpp b/src/online/http_functions.cpp new file mode 100644 index 000000000..0b8083f37 --- /dev/null +++ b/src/online/http_functions.cpp @@ -0,0 +1,70 @@ +// +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2013 SuperTuxKart-Team +// +// 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. + +#include "http_functions.hpp" + +#include +#include +#include +#include +#include "io/file_manager.hpp" + +namespace HTTP +{ +CURL *curl; +CURLcode res; + +static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) +{ + ((std::string*)userp)->append((char*)contents, size * nmemb); + return size * nmemb; +} + +void init() +{ + curl_global_init(CURL_GLOBAL_DEFAULT); + curl = curl_easy_init(); + if(!curl) + printf("Error while loading cURL library.\n"); +} + +XMLNode * getXMLFromPage(std::string url) +{ + return NULL; +} + +std::string getPage(std::string url) +{ + std::string readBuffer; + curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); + res = curl_easy_perform(curl); + if(res != CURLE_OK) + Log::error("online/http_functions", "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); + + return readBuffer; +} + +void shutdown() +{ + curl_easy_cleanup(curl); + curl_global_cleanup(); +} + +} diff --git a/src/online/http_functions.hpp b/src/online/http_functions.hpp new file mode 100644 index 000000000..65374bcba --- /dev/null +++ b/src/online/http_functions.hpp @@ -0,0 +1,44 @@ +// +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2013 SuperTuxKart-Team +// +// 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 HTTP_FUNCTIONS_HPP +#define HTTP_FUNCTIONS_HPP + +#include +#include "io/xml_node.hpp" + +/** + * \brief HTTP functions to connect with the server + * \ingroup online + */ + +namespace HTTP +{ + +void init(); +void shutdown(); + +std::string getPage(std::string url); +XMLNode * getXMLFromPage(std::string url); + + +} + +#endif // HTTP_FUNCTIONS_HPP + +/*EOF*/ diff --git a/src/online/online_user.cpp b/src/online/online_user.cpp new file mode 100644 index 000000000..ac03c57f4 --- /dev/null +++ b/src/online/online_user.cpp @@ -0,0 +1,31 @@ +// +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2013 Glenn De Jonghe +// +// 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. + + +#include "online/online_user.hpp" + +#include +#include + + +// ============================================================================ +OnlineUser::OnlineUser(const std::string &username) +{ + +} // OnlineUser + diff --git a/src/online/online_user.hpp b/src/online/online_user.hpp new file mode 100644 index 000000000..8e5b0a953 --- /dev/null +++ b/src/online/online_user.hpp @@ -0,0 +1,53 @@ +// +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2013 Glenn De Jonghe +// +// 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_ONLINE_USER_HPP +#define HEADER_ONLINE_USER_HPP + +#include + + +// ============================================================================ + +/** + * \brief Class that represents an online registered user + * \ingroup online + */ +class OnlineUser +{ +private: + +protected: + + std::string m_username; + +public: + + /** + * Constructor + */ + OnlineUser(const std::string &username); + + /** Returns the username. */ + std::string getUserName() const { return m_username; } + +}; // class OnlineUser + +#endif + +/*EOF*/ diff --git a/src/states_screens/dialogs/login_dialog.cpp b/src/states_screens/dialogs/login_dialog.cpp index fc2b07c0b..cfcfe8f23 100644 --- a/src/states_screens/dialogs/login_dialog.cpp +++ b/src/states_screens/dialogs/login_dialog.cpp @@ -1,5 +1,5 @@ // SuperTuxKart - a fun racing game with go-kart -// Copyright (C) Glenn De Jonghe +// Copyright (C) 2013 Glenn De Jonghe // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License @@ -98,8 +98,6 @@ void LoginDialog::onEnterPressedInternal() if (size > 0 && nonEmptyChars > 0) { Log::info("Login Dialog","Username : %ls", username.c_str()); - // It's unsafe to delete from inside the event handler so we do it - // in onUpdate (which checks for m_self_destroy) m_self_destroy = true; } // if valid name else diff --git a/src/states_screens/dialogs/login_dialog.hpp b/src/states_screens/dialogs/login_dialog.hpp index 5176576e4..010c7b6b0 100644 --- a/src/states_screens/dialogs/login_dialog.hpp +++ b/src/states_screens/dialogs/login_dialog.hpp @@ -1,5 +1,5 @@ // SuperTuxKart - a fun racing game with go-kart -// Copyright (C) 2009 Marianne Gagnon +// Copyright (C) 2013 Glenn De Jonghe // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License diff --git a/src/states_screens/online_screen.cpp b/src/states_screens/online_screen.cpp index 1f6d703bf..9f94c1e22 100644 --- a/src/states_screens/online_screen.cpp +++ b/src/states_screens/online_screen.cpp @@ -1,5 +1,5 @@ // SuperTuxKart - a fun racing game with go-kart -// Copyright (C) 2009 Marianne Gagnon +// Copyright (C) 2013 Glenn De Jonghe // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License diff --git a/src/states_screens/state_manager.cpp b/src/states_screens/state_manager.cpp index 74a1a9c72..e787d1b2c 100644 --- a/src/states_screens/state_manager.cpp +++ b/src/states_screens/state_manager.cpp @@ -202,7 +202,7 @@ void StateManager::onGameStateChange(GameState new_state) if (new_state == MENU) { - Screen* screen = GUIEngine::getCurrentScreen(); + GUIEngine::Screen* screen = GUIEngine::getCurrentScreen(); if (screen != NULL) { music_manager->startMusic(