dlg_install.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. --Minetest
  2. --Copyright (C) 2018-24 rubenwardy
  3. --
  4. --This program is free software; you can redistribute it and/or modify
  5. --it under the terms of the GNU Lesser General Public License as published by
  6. --the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
  13. --
  14. --You should have received a copy of the GNU Lesser General Public License along
  15. --with this program; if not, write to the Free Software Foundation, Inc.,
  16. --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. local function get_formspec(data)
  18. local selected_game, selected_game_idx = pkgmgr.find_by_gameid(core.settings:get("menu_last_game"))
  19. if not selected_game_idx then
  20. selected_game_idx = 1
  21. selected_game = pkgmgr.games[1]
  22. end
  23. local game_list = {}
  24. for i, game in ipairs(pkgmgr.games) do
  25. game_list[i] = core.formspec_escape(game.title)
  26. end
  27. local package = data.package
  28. local will_install_deps = data.will_install_deps
  29. local deps_to_install = 0
  30. local deps_not_found = 0
  31. data.dependencies = contentdb.resolve_dependencies(package, selected_game)
  32. local formatted_deps = {}
  33. for _, dep in pairs(data.dependencies) do
  34. formatted_deps[#formatted_deps + 1] = "#fff"
  35. formatted_deps[#formatted_deps + 1] = core.formspec_escape(dep.name)
  36. if dep.installed then
  37. formatted_deps[#formatted_deps + 1] = "#ccf"
  38. formatted_deps[#formatted_deps + 1] = fgettext("Already installed")
  39. elseif dep.package then
  40. formatted_deps[#formatted_deps + 1] = "#cfc"
  41. formatted_deps[#formatted_deps + 1] = fgettext("$1 by $2", dep.package.title, dep.package.author)
  42. deps_to_install = deps_to_install + 1
  43. else
  44. formatted_deps[#formatted_deps + 1] = "#f00"
  45. formatted_deps[#formatted_deps + 1] = fgettext("Not found")
  46. deps_not_found = deps_not_found + 1
  47. end
  48. end
  49. local message_bg = "#3333"
  50. local message
  51. if will_install_deps then
  52. message = fgettext("$1 and $2 dependencies will be installed.", package.title, deps_to_install)
  53. else
  54. message = fgettext("$1 will be installed, and $2 dependencies will be skipped.", package.title, deps_to_install)
  55. end
  56. if deps_not_found > 0 then
  57. message = fgettext("$1 required dependencies could not be found.", deps_not_found) ..
  58. " " .. fgettext("Please check that the base game is correct.", deps_not_found) ..
  59. "\n" .. message
  60. message_bg = mt_color_orange
  61. end
  62. local ENABLE_TOUCH = core.settings:get_bool("enable_touch")
  63. local w = ENABLE_TOUCH and 14 or 7
  64. local padded_w = w - 2*0.375
  65. local dropdown_w = ENABLE_TOUCH and 10.2 or 4.25
  66. local button_w = (padded_w - 0.25) / 3
  67. local button_pad = button_w / 2
  68. local formspec = {
  69. "formspec_version[3]",
  70. "size[", w, ",9.05]",
  71. ENABLE_TOUCH and "padding[0.01,0.01]" or "position[0.5,0.55]",
  72. "style[title;border=false]",
  73. "box[0,0;", w, ",0.8;#3333]",
  74. "button[0,0;", w, ",0.8;title;", fgettext("Install $1", package.title) , "]",
  75. "container[0.375,1]",
  76. "label[0,0.4;", fgettext("Base Game:"), "]",
  77. "dropdown[", padded_w - dropdown_w, ",0;", dropdown_w, ",0.8;selected_game;",
  78. table.concat(game_list, ","), ";", selected_game_idx, "]",
  79. "label[0,1.1;", fgettext("Dependencies:"), "]",
  80. "tablecolumns[color;text;color;text]",
  81. "table[0,1.4;", padded_w, ",3;packages;", table.concat(formatted_deps, ","), "]",
  82. "container_end[]",
  83. "checkbox[0.375,5.7;will_install_deps;",
  84. fgettext("Install missing dependencies"), ";",
  85. will_install_deps and "true" or "false", "]",
  86. "box[0,6;", w, ",1.8;", message_bg, "]",
  87. "textarea[0.375,6.1;", padded_w, ",1.6;;;", message, "]",
  88. "container[", 0.375 + button_pad, ",8.05]",
  89. "button[0,0;", button_w, ",0.8;install_all;", fgettext("Install"), "]",
  90. "button[", 0.25 + button_w, ",0;", button_w, ",0.8;cancel;", fgettext("Cancel"), "]",
  91. "container_end[]",
  92. }
  93. return table.concat(formspec)
  94. end
  95. local function handle_submit(this, fields)
  96. local data = this.data
  97. if fields.cancel then
  98. this:delete()
  99. return true
  100. end
  101. if fields.will_install_deps ~= nil then
  102. data.will_install_deps = core.is_yes(fields.will_install_deps)
  103. return true
  104. end
  105. if fields.install_all then
  106. contentdb.queue_download(data.package, contentdb.REASON_NEW)
  107. if data.will_install_deps then
  108. for _, dep in pairs(data.dependencies) do
  109. if not dep.is_optional and not dep.installed and dep.package then
  110. contentdb.queue_download(dep.package, contentdb.REASON_DEPENDENCY)
  111. end
  112. end
  113. end
  114. this:delete()
  115. return true
  116. end
  117. if fields.selected_game then
  118. for _, game in pairs(pkgmgr.games) do
  119. if game.title == fields.selected_game then
  120. core.settings:set("menu_last_game", game.id)
  121. break
  122. end
  123. end
  124. return true
  125. end
  126. return false
  127. end
  128. function create_install_dialog(package)
  129. local dlg = dialog_create("install_dialog", get_formspec, handle_submit, nil)
  130. dlg.data.dependencies = nil
  131. dlg.data.package = package
  132. dlg.data.will_install_deps = true
  133. return dlg
  134. end