Add core.open_url() to main menu API (#8592)
authorrubenwardy <rw@rubenwardy.com>
Sun, 17 May 2020 18:09:10 +0000 (19:09 +0100)
committerGitHub <noreply@github.com>
Sun, 17 May 2020 18:09:10 +0000 (19:09 +0100)
build/android/app/src/main/java/net/minetest/minetest/GameActivity.java
builtin/mainmenu/dlg_contentstore.lua
builtin/mainmenu/tab_credits.lua
doc/menu_lua_api.txt
src/porting.cpp
src/porting.h
src/porting_android.cpp
src/porting_android.h
src/script/lua_api/l_mainmenu.cpp
src/script/lua_api/l_mainmenu.h

index 02b61b598d860458d9f83108064c51ae3282d9d7..6355125698f1867672310d65226a7cc11b7a3fc4 100644 (file)
@@ -22,6 +22,7 @@ package net.minetest.minetest;
 
 import android.app.NativeActivity;
 import android.content.Intent;
+import android.net.Uri;
 import android.os.Build;
 import android.os.Bundle;
 import android.view.View;
@@ -117,4 +118,9 @@ public class GameActivity extends NativeActivity {
        public int getDisplayWidth() {
                return getResources().getDisplayMetrics().widthPixels;
        }
+
+       public void openURL(String url) {
+               Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
+               startActivity(browserIntent);
+       }
 }
index 3bc5f60bb3e96fd240935f5a03fdb7fffe721456..ce5c061c62916e32b0fea5d8055f147519509dfd 100644 (file)
@@ -16,7 +16,6 @@
 --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
 local store = { packages = {}, packages_full = {} }
-local package_dialog = {}
 
 -- Screenshot
 local screenshot_dir = core.get_cache_path() .. DIR_DELIM .. "cdb"
@@ -44,8 +43,6 @@ local filter_types_type = {
 }
 
 
-
-
 local function download_package(param)
        if core.download_file(param.package.url, param.filename) then
                return {
@@ -195,74 +192,6 @@ local function get_screenshot(package)
        return defaulttexturedir .. "loading_screenshot.png"
 end
 
-
-
-function package_dialog.get_formspec()
-       local package = package_dialog.package
-
-       store.update_paths()
-
-       local formspec = {
-               "size[9,4;true]",
-               "image[0,1;4.5,3;", core.formspec_escape(get_screenshot(package)), ']',
-               "label[3.8,1;",
-               minetest.colorize(mt_color_green, core.formspec_escape(package.title)), "\n",
-               minetest.colorize('#BFBFBF', "by " .. core.formspec_escape(package.author)), "]",
-               "textarea[4,2;5.3,2;;;", core.formspec_escape(package.short_description), "]",
-               "button[0,0;2,1;back;", fgettext("Back"), "]",
-       }
-
-       if not package.path then
-               formspec[#formspec + 1] = "button[7,0;2,1;install;"
-               formspec[#formspec + 1] = fgettext("Install")
-               formspec[#formspec + 1] = "]"
-       elseif package.installed_release < package.release then
-               -- The install_ action also handles updating
-               formspec[#formspec + 1] = "button[7,0;2,1;install;"
-               formspec[#formspec + 1] = fgettext("Update")
-               formspec[#formspec + 1] = "]"
-               formspec[#formspec + 1] = "button[5,0;2,1;uninstall;"
-               formspec[#formspec + 1] = fgettext("Uninstall")
-               formspec[#formspec + 1] = "]"
-       else
-               formspec[#formspec + 1] = "button[7,0;2,1;uninstall;"
-               formspec[#formspec + 1] = fgettext("Uninstall")
-               formspec[#formspec + 1] = "]"
-       end
-
-       return table.concat(formspec, "")
-end
-
-function package_dialog.handle_submit(this, fields)
-       if fields.back then
-               this:delete()
-               return true
-       end
-
-       if fields.install then
-               start_install(this, package_dialog.package)
-               return true
-       end
-
-       if fields.uninstall then
-               local dlg_delmod = create_delete_content_dlg(package_dialog.package)
-               dlg_delmod:set_parent(this)
-               this:hide()
-               dlg_delmod:show()
-               return true
-       end
-
-       return false
-end
-
-function package_dialog.create(package)
-       package_dialog.package = package
-       return dialog_create("package_view",
-               package_dialog.get_formspec,
-               package_dialog.handle_submit,
-               nil)
-end
-
 function store.load()
        local tmpdir = os.tempfolder()
        local target = tmpdir .. DIR_DELIM .. "packages.json"
@@ -462,44 +391,45 @@ function store.get_formspec(dlgdata)
                                minetest.colorize("#BFBFBF", " by " .. package.author))
                formspec[#formspec + 1] = "]"
 
-               -- description
-               if package.path and package.installed_release < package.release then
-                       formspec[#formspec + 1] = "textarea[1.25,0.3;7.5,1;;;"
-               else
-                       formspec[#formspec + 1] = "textarea[1.25,0.3;9,1;;;"
-               end
-               formspec[#formspec + 1] = core.formspec_escape(package.short_description)
-               formspec[#formspec + 1] = "]"
-
                -- buttons
+               local description_width = 7.5
                if not package.path then
-                       formspec[#formspec + 1] = "button[9.9,0;1.5,1;install_"
+                       formspec[#formspec + 1] = "button[8.4,0;1.5,1;install_"
                        formspec[#formspec + 1] = tostring(i)
                        formspec[#formspec + 1] = ";"
                        formspec[#formspec + 1] = fgettext("Install")
                        formspec[#formspec + 1] = "]"
                else
                        if package.installed_release < package.release then
+                               description_width = 6
+
                                -- The install_ action also handles updating
-                               formspec[#formspec + 1] = "button[8.4,0;1.5,1;install_"
+                               formspec[#formspec + 1] = "button[6.9,0;1.5,1;install_"
                                formspec[#formspec + 1] = tostring(i)
                                formspec[#formspec + 1] = ";"
                                formspec[#formspec + 1] = fgettext("Update")
                                formspec[#formspec + 1] = "]"
                        end
 
-                       formspec[#formspec + 1] = "button[9.9,0;1.5,1;uninstall_"
+                       formspec[#formspec + 1] = "button[8.4,0;1.5,1;uninstall_"
                        formspec[#formspec + 1] = tostring(i)
                        formspec[#formspec + 1] = ";"
                        formspec[#formspec + 1] = fgettext("Uninstall")
                        formspec[#formspec + 1] = "]"
                end
 
-               --formspec[#formspec + 1] = "button[9.9,0;1.5,1;view_"
-               --formspec[#formspec + 1] = tostring(i)
-               --formspec[#formspec + 1] = ";"
-               --formspec[#formspec + 1] = fgettext("View")
-               --formspec[#formspec + 1] = "]"
+               formspec[#formspec + 1] = "button[9.9,0;1.5,1;view_"
+               formspec[#formspec + 1] = tostring(i)
+               formspec[#formspec + 1] = ";"
+               formspec[#formspec + 1] = fgettext("View")
+               formspec[#formspec + 1] = "]"
+
+               -- description
+               formspec[#formspec + 1] = "textarea[1.25,0.3;"
+               formspec[#formspec + 1] = tostring(description_width)
+               formspec[#formspec + 1] = ",1;;;"
+               formspec[#formspec + 1] = core.formspec_escape(package.short_description)
+               formspec[#formspec + 1] = "]"
 
                formspec[#formspec + 1] = "container_end[]"
        end
@@ -576,10 +506,9 @@ function store.handle_submit(this, fields)
                end
 
                if fields["view_" .. i] then
-                       local dlg = package_dialog.create(package)
-                       dlg:set_parent(this)
-                       this:hide()
-                       dlg:show()
+                       local url = ("%s/packages/%s?protocol_version=%d"):format(
+                                       core.settings:get("contentdb_url"), package.id, core.get_max_supp_proto())
+                       core.open_url(url)
                        return true
                end
        end
index 962d2a3b468c26d2192e7f9fa0403fa48fde3a50..c2b7e503ad8d01e77fa917992905a3f013f8a539 100644 (file)
@@ -101,8 +101,8 @@ return {
                local logofile = defaulttexturedir .. "logo.png"
                local version = core.get_version()
                return "image[0.5,1;" .. core.formspec_escape(logofile) .. "]" ..
-                       "label[0.5,3.2;" .. version.project .. " " .. version.string .. "]" ..
-                       "label[0.5,3.5;http://minetest.net]" ..
+                       "label[0.5,2.8;" .. version.project .. " " .. version.string .. "]" ..
+                       "button[0.5,3;2,2;homepage;minetest.net]" ..
                        "tablecolumns[color;text]" ..
                        "tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
                        "table[3.5,-0.25;8.5,6.05;list_credits;" ..
@@ -115,5 +115,10 @@ return {
                        "#FFFF00," .. fgettext("Previous Contributors") .. ",," ..
                        buildCreditList(previous_contributors) .. "," ..
                        ";1]"
-       end
+       end,
+       cbf_button_handler = function(this, fields, name, tabdata)
+               if fields.homepage then
+                       core.open_url("https://www.minetest.net")
+               end
+       end,
 }
index 8f5460acb19c938f1dc1c63dd5448632b10a1ec8..485c501102f1da2ca8418bc47de760f56fe8f5d3 100644 (file)
@@ -234,6 +234,11 @@ core.get_min_supp_proto()
 core.get_max_supp_proto()
 ^ returns the maximum supported network protocol version
 
+Other:
+core.open_url(url)
+^ opens the URL in a web browser, returns false on failure.
+^ Must begin with http:// or https://
+
 Async:
 core.handle_async(async_job,parameters,finished)
 ^ execute a function asynchronously
index c0381ad063e310a5bd3fa59c7da717cb72cfc3c0..ef1640467d64f28feb83275d4e4694b1027a9a34 100644 (file)
@@ -33,22 +33,28 @@ with this program; if not, write to the Free Software Foundation, Inc.,
        #include <wincrypt.h>
        #include <algorithm>
        #include <shlwapi.h>
+       #include <shellapi.h>
 #endif
 #if !defined(_WIN32)
        #include <unistd.h>
        #include <sys/utsname.h>
+       #if !defined(__ANDROID__)
+               #include <spawn.h>
+       #endif
 #endif
 #if defined(__hpux)
        #define _PSTAT64
        #include <sys/pstat.h>
 #endif
+#if defined(__ANDROID__)
+       #include "porting_android.h"
+#endif
 
 #include "config.h"
 #include "debug.h"
 #include "filesys.h"
 #include "log.h"
 #include "util/string.h"
-#include "settings.h"
 #include <list>
 #include <cstdarg>
 #include <cstdio>
@@ -697,6 +703,28 @@ int mt_snprintf(char *buf, const size_t buf_size, const char *fmt, ...)
        return c;
 }
 
+bool openURL(const std::string &url)
+{
+       if ((url.substr(0, 7) != "http://" && url.substr(0, 8) != "https://") ||
+                       url.find_first_of("\r\n") != std::string::npos) {
+               errorstream << "Invalid url: " << url << std::endl;
+               return false;
+       }
+
+#if defined(_WIN32)
+       return (intptr_t)ShellExecuteA(NULL, NULL, url.c_str(), NULL, NULL, SW_SHOWNORMAL) > 32;
+#elif defined(__ANDROID__)
+       openURLAndroid(url);
+       return true;
+#elif defined(__APPLE__)
+       const char *argv[] = {"open", url.c_str(), NULL};
+       return posix_spawnp(NULL, "open", NULL, NULL, (char**)argv, environ) == 0;
+#else
+       const char *argv[] = {"xdg-open", url.c_str(), NULL};
+       return posix_spawnp(NULL, "xdg-open", NULL, NULL, (char**)argv, environ) == 0;
+#endif
+}
+
 // Load performance counter frequency only once at startup
 #ifdef _WIN32
 
index 4d30a5970f2ffd20b66088d60d48a4b2c1fac89d..f50f0a950f281d8973a96b8372a44b38d28e0793 100644 (file)
@@ -329,6 +329,9 @@ bool secure_rand_fill_buf(void *buf, size_t len);
 void attachOrCreateConsole();
 
 int mt_snprintf(char *buf, const size_t buf_size, const char *fmt, ...);
+
+bool openURL(const std::string &url);
+
 } // namespace porting
 
 #ifdef __ANDROID__
index 2c91df2356edea4dd5f9f9025eb32de16bed7dc6..41b521ec207e1e738bdf29806212f541e9a34ae8 100644 (file)
@@ -213,6 +213,18 @@ void showInputDialog(const std::string &acceptButton, const std::string &hint,
                        jacceptButton, jhint, jcurrent, jeditType);
 }
 
+void openURLAndroid(const std::string &url)
+{
+       jmethodID url_open = jnienv->GetMethodID(nativeActivity, "openURL",
+               "(Ljava/lang/String;)V");
+
+       FATAL_ERROR_IF(url_open == nullptr,
+               "porting::openURLAndroid unable to find java openURL method");
+
+       jstring jurl = jnienv->NewStringUTF(url.c_str());
+       jnienv->CallVoidMethod(app_global->activity->clazz, url_open, jurl);
+}
+
 int getInputDialogState()
 {
        jmethodID dialogstate = jnienv->GetMethodID(nativeActivity,
index 42f90b60bb2baa9460a0257a76401f4855fb267e..6eb0540412ad159f8634264a89b06fbbc22972e8 100644 (file)
@@ -58,6 +58,8 @@ void initializePathsAndroid();
 void showInputDialog(const std::string &acceptButton,
                                        const std::string &hint, const std::string &current, int editType);
 
+void openURLAndroid(const std::string &url);
+
 /**
  * WORKAROUND for not working callbacks from java -> c++
  * get current state of input dialog
index a76e9f079c7eda5d1e7ef0b63a4aee496c7c4a32..f32c477c28dca47345af75c7968f2b5f30c90892 100644 (file)
@@ -1063,6 +1063,14 @@ int ModApiMainMenu::l_get_max_supp_proto(lua_State *L)
        return 1;
 }
 
+/******************************************************************************/
+int ModApiMainMenu::l_open_url(lua_State *L)
+{
+       std::string url = luaL_checkstring(L, 1);
+       lua_pushboolean(L, porting::openURL(url));
+       return 1;
+}
+
 /******************************************************************************/
 int ModApiMainMenu::l_do_async_callback(lua_State *L)
 {
@@ -1125,6 +1133,7 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
        API_FCT(get_screen_info);
        API_FCT(get_min_supp_proto);
        API_FCT(get_max_supp_proto);
+       API_FCT(open_url);
        API_FCT(do_async_callback);
 }
 
index b2ca4932065cc74d8ab89693a5c38e53978a8a8f..5a16b3bfeebc4a9a46650fc1486ac29ea953a173 100644 (file)
@@ -145,6 +145,9 @@ private:
 
        static int l_get_max_supp_proto(lua_State *L);
 
+       // other
+       static int l_open_url(lua_State *L);
+
 
        // async
        static int l_do_async_callback(lua_State *L);