1 //  SuperTux
2 //  Copyright (C) 2020 A. Semphris <semphris@protonmail.com>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (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, see <http://www.gnu.org/licenses/>.
16 
17 #include "supertux/menu/web_asset_menu.hpp"
18 
19 #include "gui/dialog.hpp"
20 #include "gui/menu_filesystem.hpp"
21 #include "gui/menu_item.hpp"
22 #include "gui/menu_manager.hpp"
23 #include "util/file_system.hpp"
24 #include "util/gettext.hpp"
25 
26 #ifdef __EMSCRIPTEN__
27 #include <emscripten.h>
28 #include <emscripten/html5.h>
29 #endif
30 
31 #include "physfs.h"
32 
33 #include <regex>
34 
WebAssetMenu()35 WebAssetMenu::WebAssetMenu() :
36   m_add_path("")
37 {
38   add_label(_("Manage Assets"));
39 
40   add_hl();
41 
42   add_textfield(_("New files location"), &m_add_path);
43   add_entry(MNID_ADDFILES, _("Add Files"));
44   add_entry(MNID_DOWNLOADFILES, _("Download Files"));
45 
46   add_hl();
47 
48   add_back(_("Back"));
49 }
50 
51 void
menu_action(MenuItem & item)52 WebAssetMenu::menu_action(MenuItem& item)
53 {
54   switch (item.get_id())
55   {
56     case MNID_ADDFILES:
57     {
58       std::regex regex("'");
59       m_add_path = std::regex_replace(m_add_path, regex, "\\'");
60 
61 #ifdef __EMSCRIPTEN__
62       emscripten_run_script(("supertux_upload('" + m_add_path + "');").c_str());
63 #endif
64     }
65       break;
66 
67     case MNID_DOWNLOADFILES:
68     {
69       std::vector<std::string> empty_vec;
70       MenuManager::instance().push_menu(
71         std::make_unique<FileSystemMenu>(nullptr, empty_vec, "", [](std::string file) {
72           std::string fullpath(std::string(PHYSFS_getRealDir(file.c_str())) + "/" + file);
73           FileSystem::open_path(fullpath);
74         })
75       );
76     }
77       break;
78 
79     default:
80       break;
81   }
82 }
83 
84 /* EOF */
85