2 --Copyright (C) 2018 rubenwardy
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.
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.
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.
19 local package_dialog = {}
21 local search_string = ""
23 local num_per_page = 5
25 local filter_types_titles = {
26 fgettext("All packages"),
29 fgettext("Texture packs"),
32 local filter_types_type = {
42 local function download_package(param)
43 if core.download_file(param.package.url, param.filename) then
45 package = param.package,
46 filename = param.filename,
50 core.log("error", "downloading " .. dump(param.package.url) .. " failed")
52 package = param.package,
58 local function start_install(calling_dialog, package)
61 filename = os.tempfolder() .. "_MODNAME_" .. package.name .. ".zip",
64 local function callback(result)
65 if result.successful then
66 local path, msg = pkgmgr.install(result.package.type,
67 result.filename, result.package.name,
70 gamedata.errormessage = msg
73 local name_is_title = false
74 if result.package.type == "mod" then
75 conf_path = path .. DIR_DELIM .. "mod.conf"
76 elseif result.package.type == "game" then
77 conf_path = path .. DIR_DELIM .. "game.conf"
79 elseif result.package.type == "txp" then
80 conf_path = path .. DIR_DELIM .. "texture_pack.conf"
84 local conf = Settings(conf_path)
85 local function set_def(key, value)
86 if conf:get(key) == nil then
91 set_def("name", result.package.title)
93 set_def("title", result.package.title)
94 set_def("name", result.package.name)
96 set_def("description", result.package.short_description)
97 set_def("author", result.package.author)
98 conf:set("release", result.package.release)
102 os.remove(result.filename)
104 gamedata.errormessage = fgettext("Failed to download $1", package.name)
107 if gamedata.errormessage == nil then
108 core.button_handler({btn_hidden_close_download=result})
110 core.button_handler({btn_hidden_close_download={successful=false}})
114 if not core.handle_async(download_package, params, callback) then
115 core.log("error", "ERROR: async event failed")
116 gamedata.errormessage = fgettext("Failed to download $1", package.name)
119 local new_dlg = dialog_create("store_downloading",
121 return "size[7,2]label[0.25,0.75;" ..
122 fgettext("Downloading and installing $1, please wait...", data.title) .. "]"
124 function(this,fields)
125 if fields["btn_hidden_close_download"] ~= nil then
134 new_dlg:set_parent(calling_dialog)
135 new_dlg.data.title = package.title
136 calling_dialog:hide()
141 function package_dialog.get_formspec()
142 local package = package_dialog.package
148 "label[2.5,0.2;", core.formspec_escape(package.title), "]",
149 "textarea[0.2,1;9,3;;;", core.formspec_escape(package.short_description), "]",
150 "button[0,0;2,1;back;", fgettext("Back"), "]",
153 if not package.path then
154 formspec[#formspec + 1] = "button[7,0;2,1;install;"
155 formspec[#formspec + 1] = fgettext("Install")
156 formspec[#formspec + 1] = "]"
157 elseif package.installed_release < package.release then
158 formspec[#formspec + 1] = "button[7,0;2,1;install;"
159 formspec[#formspec + 1] = fgettext("Update")
160 formspec[#formspec + 1] = "]"
161 formspec[#formspec + 1] = "button[7,1;2,1;uninstall;"
162 formspec[#formspec + 1] = fgettext("Uninstall")
163 formspec[#formspec + 1] = "]"
165 formspec[#formspec + 1] = "button[7,0;2,1;uninstall;"
166 formspec[#formspec + 1] = fgettext("Uninstall")
167 formspec[#formspec + 1] = "]"
172 return table.concat(formspec, "")
175 function package_dialog.handle_submit(this, fields, tabname, tabdata)
181 if fields.install then
182 start_install(this, package_dialog.package)
186 if fields.uninstall then
187 local dlg_delmod = create_delete_content_dlg(package_dialog.package)
188 dlg_delmod:set_parent(this)
197 function package_dialog.create(package)
198 package_dialog.package = package
199 return dialog_create("package_view",
200 package_dialog.get_formspec,
201 package_dialog.handle_submit,
205 function store.load()
206 store.packages_full = core.get_package_list()
207 store.packages = store.packages_full
211 function store.update_paths()
213 pkgmgr.refresh_globals()
214 for _, mod in pairs(pkgmgr.global_mods:get_list()) do
215 mod_hash[mod.name] = mod
219 pkgmgr.update_gamelist()
220 for _, game in pairs(pkgmgr.games) do
221 game_hash[game.id] = game
225 for _, txp in pairs(pkgmgr.get_texture_packs()) do
226 txp_hash[txp.name] = txp
229 for _, package in pairs(store.packages_full) do
231 if package.type == "mod" then
232 content = mod_hash[package.name]
233 elseif package.type == "game" then
234 content = game_hash[package.name]
235 elseif package.type == "txp" then
236 content = txp_hash[package.name]
239 if content and content.author == package.author then
240 package.path = content.path
241 package.installed_release = content.release
248 function store.filter_packages(query)
249 if query == "" and filter_type == 1 then
250 store.packages = store.packages_full
255 for word in query:lower():gmatch("%S+") do
256 table.insert(keywords, word)
259 local function matches_keywords(package, keywords)
260 for k = 1, #keywords do
261 local keyword = keywords[k]
263 if string.find(package.name:lower(), keyword, 1, true) or
264 string.find(package.title:lower(), keyword, 1, true) or
265 string.find(package.author:lower(), keyword, 1, true) or
266 string.find(package.short_description:lower(), keyword, 1, true) then
275 for _, package in pairs(store.packages_full) do
276 if (query == "" or matches_keywords(package, keywords)) and
277 (filter_type == 1 or package.type == filter_types_type[filter_type]) then
278 store.packages[#store.packages + 1] = package
284 function store.get_formspec()
289 local pages = math.ceil(#store.packages / num_per_page)
290 if cur_page > pages then
296 "field[0.3,0.1;10.2,1;search_string;;", core.formspec_escape(search_string), "]",
297 "field_close_on_enter[search_string;false]",
298 "button[10.2,-0.2;2,1;search;", fgettext("Search"), "]",
299 "dropdown[0,1;2.4;type;",
300 table.concat(filter_types_titles, ","),
304 -- "textlist[0,1;2.4,5.6;a;",
305 -- table.concat(taglist, ","),
309 local start_idx = (cur_page - 1) * num_per_page + 1
310 for i=start_idx, math.min(#store.packages, start_idx+num_per_page-1) do
311 local package = store.packages[i]
312 formspec[#formspec + 1] = "container[3,"
313 formspec[#formspec + 1] = i - start_idx + 1
314 formspec[#formspec + 1] = "]"
317 formspec[#formspec + 1] = "image[-0.4,0;1.5,1;"
318 formspec[#formspec + 1] = defaulttexturedir
319 formspec[#formspec + 1] = "no_screenshot.png"
320 formspec[#formspec + 1] = "]"
323 formspec[#formspec + 1] = "label[1,-0.1;"
324 formspec[#formspec + 1] = core.formspec_escape(package.title ..
325 " by " .. package.author)
326 formspec[#formspec + 1] = "]"
329 formspec[#formspec + 1] = "textarea[1.25,0.3;5,1;;;"
330 formspec[#formspec + 1] = core.formspec_escape(package.short_description)
331 formspec[#formspec + 1] = "]"
334 if not package.path then
335 formspec[#formspec + 1] = "button[6,0;1.5,1;install_"
336 formspec[#formspec + 1] = tostring(i)
337 formspec[#formspec + 1] = ";"
338 formspec[#formspec + 1] = fgettext("Install")
339 formspec[#formspec + 1] = "]"
340 elseif package.installed_release < package.release then
341 formspec[#formspec + 1] = "button[6,0;1.5,1;install_"
342 formspec[#formspec + 1] = tostring(i)
343 formspec[#formspec + 1] = ";"
344 formspec[#formspec + 1] = fgettext("Update")
345 formspec[#formspec + 1] = "]"
347 formspec[#formspec + 1] = "button[6,0;1.5,1;uninstall_"
348 formspec[#formspec + 1] = tostring(i)
349 formspec[#formspec + 1] = ";"
350 formspec[#formspec + 1] = fgettext("Uninstall")
351 formspec[#formspec + 1] = "]"
353 formspec[#formspec + 1] = "button[7.5,0;1.5,1;view_"
354 formspec[#formspec + 1] = tostring(i)
355 formspec[#formspec + 1] = ";"
356 formspec[#formspec + 1] = fgettext("View")
357 formspec[#formspec + 1] = "]"
359 formspec[#formspec + 1] = "container_end[]"
362 formspec[#formspec + 1] = "container[0,"
363 formspec[#formspec + 1] = num_per_page + 1
364 formspec[#formspec + 1] = "]"
365 formspec[#formspec + 1] = "button[2.6,0;3,1;back;"
366 formspec[#formspec + 1] = fgettext("Back to Main Menu")
367 formspec[#formspec + 1] = "]"
368 formspec[#formspec + 1] = "button[7,0;1,1;pstart;<<]"
369 formspec[#formspec + 1] = "button[8,0;1,1;pback;<]"
370 formspec[#formspec + 1] = "label[9.2,0.2;"
371 formspec[#formspec + 1] = tonumber(cur_page)
372 formspec[#formspec + 1] = " / "
373 formspec[#formspec + 1] = tonumber(pages)
374 formspec[#formspec + 1] = "]"
375 formspec[#formspec + 1] = "button[10,0;1,1;pnext;>]"
376 formspec[#formspec + 1] = "button[11,0;1,1;pend;>>]"
377 formspec[#formspec + 1] = "container_end[]"
379 formspec[#formspec + 1] = "]"
380 return table.concat(formspec, "")
383 function store.handle_submit(this, fields, tabname, tabdata)
384 if fields.search or fields.key_enter_field == "search_string" then
385 search_string = fields.search_string:trim()
387 store.filter_packages(search_string)
388 core.update_formspec(store.get_formspec())
397 if fields.pstart then
399 core.update_formspec(store.get_formspec())
404 cur_page = math.ceil(#store.packages / num_per_page)
405 core.update_formspec(store.get_formspec())
410 cur_page = cur_page + 1
411 local pages = math.ceil(#store.packages / num_per_page)
412 if cur_page > pages then
415 core.update_formspec(store.get_formspec())
420 if cur_page == 1 then
421 local pages = math.ceil(#store.packages / num_per_page)
424 cur_page = cur_page - 1
426 core.update_formspec(store.get_formspec())
431 local new_type = table.indexof(filter_types_titles, fields.type)
432 if new_type ~= filter_type then
433 filter_type = new_type
434 store.filter_packages(search_string)
439 local start_idx = (cur_page - 1) * num_per_page + 1
440 assert(start_idx ~= nil)
441 for i=start_idx, math.min(#store.packages, start_idx+num_per_page-1) do
442 local package = store.packages[i]
445 if fields["install_" .. i] then
446 start_install(this, package)
450 if fields["uninstall_" .. i] then
451 local dlg_delmod = create_delete_content_dlg(package)
452 dlg_delmod:set_parent(this)
458 if fields["view_" .. i] then
459 local dlg = package_dialog.create(package)
470 function create_store_dlg(type)
471 if not store.loaded then
477 store.filter_packages(search_string)
479 return dialog_create("store",