]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/mainmenu/dlg_contentstore.lua
Rework main menu confirmation dialogs (#12356)
[dragonfireclient.git] / builtin / mainmenu / dlg_contentstore.lua
1 --Minetest
2 --Copyright (C) 2018-20 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
18 if not core.get_http_api then
19         function create_store_dlg()
20                 return messagebox("store",
21                                 fgettext("ContentDB is not available when Minetest was compiled without cURL"))
22         end
23         return
24 end
25
26 -- Unordered preserves the original order of the ContentDB API,
27 -- before the package list is ordered based on installed state.
28 local store = { packages = {}, packages_full = {}, packages_full_unordered = {}, aliases = {} }
29
30 local http = core.get_http_api()
31
32 -- Screenshot
33 local screenshot_dir = core.get_cache_path() .. DIR_DELIM .. "cdb"
34 assert(core.create_dir(screenshot_dir))
35 local screenshot_downloading = {}
36 local screenshot_downloaded = {}
37
38 -- Filter
39 local search_string = ""
40 local cur_page = 1
41 local num_per_page = 5
42 local filter_type = 1
43 local filter_types_titles = {
44         fgettext("All packages"),
45         fgettext("Games"),
46         fgettext("Mods"),
47         fgettext("Texture packs"),
48 }
49
50 local number_downloading = 0
51 local download_queue = {}
52
53 local filter_types_type = {
54         nil,
55         "game",
56         "mod",
57         "txp",
58 }
59
60 local REASON_NEW = "new"
61 local REASON_UPDATE = "update"
62 local REASON_DEPENDENCY = "dependency"
63
64
65 -- encodes for use as URL parameter or path component
66 local function urlencode(str)
67         return str:gsub("[^%a%d()._~-]", function(char)
68                 return string.format("%%%02X", string.byte(char))
69         end)
70 end
71 assert(urlencode("sample text?") == "sample%20text%3F")
72
73
74 local function get_download_url(package, reason)
75         local base_url = core.settings:get("contentdb_url")
76         local ret = base_url .. ("/packages/%s/releases/%d/download/"):format(
77                 package.url_part, package.release)
78         if reason then
79                 ret = ret .. "?reason=" .. reason
80         end
81         return ret
82 end
83
84
85 local function download_and_extract(param)
86         local package = param.package
87
88         local filename = core.get_temp_path(true)
89         if filename == "" or not core.download_file(param.url, filename) then
90                 core.log("error", "Downloading " .. dump(param.url) .. " failed")
91                 return {
92                         msg = fgettext("Failed to download $1", package.name)
93                 }
94         end
95
96         local tempfolder = core.get_temp_path()
97         if tempfolder ~= "" then
98                 tempfolder = tempfolder .. DIR_DELIM .. "MT_" .. math.random(1, 1024000)
99                 if not core.extract_zip(filename, tempfolder) then
100                         tempfolder = nil
101                 end
102         else
103                 tempfolder = nil
104         end
105         os.remove(filename)
106         if not tempfolder then
107                 return {
108                         msg = fgettext("Install: Unsupported file type or broken archive"),
109                 }
110         end
111
112         return {
113                 path = tempfolder
114         }
115 end
116
117 local function start_install(package, reason)
118         local params = {
119                 package = package,
120                 url = get_download_url(package, reason),
121         }
122
123         number_downloading = number_downloading + 1
124
125         local function callback(result)
126                 if result.msg then
127                         gamedata.errormessage = result.msg
128                 else
129                         local path, msg = pkgmgr.install_dir(package.type, result.path, package.name, package.path)
130                         core.delete_dir(result.path)
131                         if not path then
132                                 gamedata.errormessage = msg
133                         else
134                                 core.log("action", "Installed package to " .. path)
135
136                                 local conf_path
137                                 local name_is_title = false
138                                 if package.type == "mod" then
139                                         local actual_type = pkgmgr.get_folder_type(path)
140                                         if actual_type.type == "modpack" then
141                                                 conf_path = path .. DIR_DELIM .. "modpack.conf"
142                                         else
143                                                 conf_path = path .. DIR_DELIM .. "mod.conf"
144                                         end
145                                 elseif package.type == "game" then
146                                         conf_path = path .. DIR_DELIM .. "game.conf"
147                                         name_is_title = true
148                                 elseif package.type == "txp" then
149                                         conf_path = path .. DIR_DELIM .. "texture_pack.conf"
150                                 end
151
152                                 if conf_path then
153                                         local conf = Settings(conf_path)
154                                         conf:set("title", package.title)
155                                         if not name_is_title then
156                                                 conf:set("name", package.name)
157                                         end
158                                         if not conf:get("description") then
159                                                 conf:set("description", package.short_description)
160                                         end
161                                         conf:set("author",     package.author)
162                                         conf:set("release",    package.release)
163                                         conf:write()
164                                 end
165                         end
166                 end
167
168                 package.downloading = false
169
170                 number_downloading = number_downloading - 1
171
172                 local next = download_queue[1]
173                 if next then
174                         table.remove(download_queue, 1)
175
176                         start_install(next.package, next.reason)
177                 end
178
179                 ui.update()
180         end
181
182         package.queued = false
183         package.downloading = true
184
185         if not core.handle_async(download_and_extract, params, callback) then
186                 core.log("error", "ERROR: async event failed")
187                 gamedata.errormessage = fgettext("Failed to download $1", package.name)
188                 return
189         end
190 end
191
192 local function queue_download(package, reason)
193         local max_concurrent_downloads = tonumber(core.settings:get("contentdb_max_concurrent_downloads"))
194         if number_downloading < max_concurrent_downloads then
195                 start_install(package, reason)
196         else
197                 table.insert(download_queue, { package = package, reason = reason })
198                 package.queued = true
199         end
200 end
201
202 local function get_raw_dependencies(package)
203         if package.raw_deps then
204                 return package.raw_deps
205         end
206
207         local url_fmt = "/api/packages/%s/dependencies/?only_hard=1&protocol_version=%s&engine_version=%s"
208         local version = core.get_version()
209         local base_url = core.settings:get("contentdb_url")
210         local url = base_url .. url_fmt:format(package.url_part, core.get_max_supp_proto(), urlencode(version.string))
211
212         local response = http.fetch_sync({ url = url })
213         if not response.succeeded then
214                 return
215         end
216
217         local data = core.parse_json(response.data) or {}
218
219         local content_lookup = {}
220         for _, pkg in pairs(store.packages_full) do
221                 content_lookup[pkg.id] = pkg
222         end
223
224         for id, raw_deps in pairs(data) do
225                 local package2 = content_lookup[id:lower()]
226                 if package2 and not package2.raw_deps then
227                         package2.raw_deps = raw_deps
228
229                         for _, dep in pairs(raw_deps) do
230                                 local packages = {}
231                                 for i=1, #dep.packages do
232                                         packages[#packages + 1] = content_lookup[dep.packages[i]:lower()]
233                                 end
234                                 dep.packages = packages
235                         end
236                 end
237         end
238
239         return package.raw_deps
240 end
241
242 local function has_hard_deps(raw_deps)
243         for i=1, #raw_deps do
244                 if not raw_deps[i].is_optional then
245                         return true
246                 end
247         end
248
249         return false
250 end
251
252 -- Recursively resolve dependencies, given the installed mods
253 local function resolve_dependencies_2(raw_deps, installed_mods, out)
254         local function resolve_dep(dep)
255                 -- Check whether it's already installed
256                 if installed_mods[dep.name] then
257                         return {
258                                 is_optional = dep.is_optional,
259                                 name = dep.name,
260                                 installed = true,
261                         }
262                 end
263
264                 -- Find exact name matches
265                 local fallback
266                 for _, package in pairs(dep.packages) do
267                         if package.type ~= "game" then
268                                 if package.name == dep.name then
269                                         return {
270                                                 is_optional = dep.is_optional,
271                                                 name = dep.name,
272                                                 installed = false,
273                                                 package = package,
274                                         }
275                                 elseif not fallback then
276                                         fallback = package
277                                 end
278                         end
279                 end
280
281                 -- Otherwise, find the first mod that fulfils it
282                 if fallback then
283                         return {
284                                 is_optional = dep.is_optional,
285                                 name = dep.name,
286                                 installed = false,
287                                 package = fallback,
288                         }
289                 end
290
291                 return {
292                         is_optional = dep.is_optional,
293                         name = dep.name,
294                         installed = false,
295                 }
296         end
297
298         for _, dep in pairs(raw_deps) do
299                 if not dep.is_optional and not out[dep.name] then
300                         local result  = resolve_dep(dep)
301                         out[dep.name] = result
302                         if result and result.package and not result.installed then
303                                 local raw_deps2 = get_raw_dependencies(result.package)
304                                 if raw_deps2 then
305                                         resolve_dependencies_2(raw_deps2, installed_mods, out)
306                                 end
307                         end
308                 end
309         end
310
311         return true
312 end
313
314 -- Resolve dependencies for a package, calls the recursive version.
315 local function resolve_dependencies(raw_deps, game)
316         assert(game)
317
318         local installed_mods = {}
319
320         local mods = {}
321         pkgmgr.get_game_mods(game, mods)
322         for _, mod in pairs(mods) do
323                 installed_mods[mod.name] = true
324         end
325
326         for _, mod in pairs(pkgmgr.global_mods:get_list()) do
327                 installed_mods[mod.name] = true
328         end
329
330         local out = {}
331         if not resolve_dependencies_2(raw_deps, installed_mods, out) then
332                 return nil
333         end
334
335         local retval = {}
336         for _, dep in pairs(out) do
337                 retval[#retval + 1] = dep
338         end
339
340         table.sort(retval, function(a, b)
341                 return a.name < b.name
342         end)
343
344         return retval
345 end
346
347 local install_dialog = {}
348 function install_dialog.get_formspec()
349         local package = install_dialog.package
350         local raw_deps = install_dialog.raw_deps
351         local will_install_deps = install_dialog.will_install_deps
352
353         local selected_game_idx = 1
354         local selected_gameid = core.settings:get("menu_last_game")
355         local games = table.copy(pkgmgr.games)
356         for i=1, #games do
357                 if selected_gameid and games[i].id == selected_gameid then
358                         selected_game_idx = i
359                 end
360
361                 games[i] = core.formspec_escape(games[i].title)
362         end
363
364         local selected_game = pkgmgr.games[selected_game_idx]
365         local deps_to_install = 0
366         local deps_not_found = 0
367
368         install_dialog.dependencies = resolve_dependencies(raw_deps, selected_game)
369         local formatted_deps = {}
370         for _, dep in pairs(install_dialog.dependencies) do
371                 formatted_deps[#formatted_deps + 1] = "#fff"
372                 formatted_deps[#formatted_deps + 1] = core.formspec_escape(dep.name)
373                 if dep.installed then
374                         formatted_deps[#formatted_deps + 1] = "#ccf"
375                         formatted_deps[#formatted_deps + 1] = fgettext("Already installed")
376                 elseif dep.package then
377                         formatted_deps[#formatted_deps + 1] = "#cfc"
378                         formatted_deps[#formatted_deps + 1] = fgettext("$1 by $2", dep.package.title, dep.package.author)
379                         deps_to_install = deps_to_install + 1
380                 else
381                         formatted_deps[#formatted_deps + 1] = "#f00"
382                         formatted_deps[#formatted_deps + 1] = fgettext("Not found")
383                         deps_not_found = deps_not_found + 1
384                 end
385         end
386
387         local message_bg = "#3333"
388         local message
389         if will_install_deps then
390                 message = fgettext("$1 and $2 dependencies will be installed.", package.title, deps_to_install)
391         else
392                 message = fgettext("$1 will be installed, and $2 dependencies will be skipped.", package.title, deps_to_install)
393         end
394         if deps_not_found > 0 then
395                 message = fgettext("$1 required dependencies could not be found.", deps_not_found) ..
396                                 " " .. fgettext("Please check that the base game is correct.", deps_not_found) ..
397                                 "\n" .. message
398                 message_bg = mt_color_orange
399         end
400
401         local formspec = {
402                 "formspec_version[3]",
403                 "size[7,7.85]",
404                 "style[title;border=false]",
405                 "box[0,0;7,0.5;#3333]",
406                 "button[0,0;7,0.5;title;", fgettext("Install $1", package.title) , "]",
407
408                 "container[0.375,0.70]",
409
410                 "label[0,0.25;", fgettext("Base Game:"), "]",
411                 "dropdown[2,0;4.25,0.5;selected_game;", table.concat(games, ","), ";", selected_game_idx, "]",
412
413                 "label[0,0.8;", fgettext("Dependencies:"), "]",
414
415                 "tablecolumns[color;text;color;text]",
416                 "table[0,1.1;6.25,3;packages;", table.concat(formatted_deps, ","), "]",
417
418                 "container_end[]",
419
420                 "checkbox[0.375,5.1;will_install_deps;",
421                         fgettext("Install missing dependencies"), ";",
422                         will_install_deps and "true" or "false", "]",
423
424                 "box[0,5.4;7,1.2;", message_bg, "]",
425                 "textarea[0.375,5.5;6.25,1;;;", message, "]",
426
427                 "container[1.375,6.85]",
428                 "button[0,0;2,0.8;install_all;", fgettext("Install"), "]",
429                 "button[2.25,0;2,0.8;cancel;", fgettext("Cancel"), "]",
430                 "container_end[]",
431         }
432
433         return table.concat(formspec, "")
434 end
435
436 function install_dialog.handle_submit(this, fields)
437         if fields.cancel then
438                 this:delete()
439                 return true
440         end
441
442         if fields.will_install_deps ~= nil then
443                 install_dialog.will_install_deps = core.is_yes(fields.will_install_deps)
444                 return true
445         end
446
447         if fields.install_all then
448                 queue_download(install_dialog.package, REASON_NEW)
449
450                 if install_dialog.will_install_deps then
451                         for _, dep in pairs(install_dialog.dependencies) do
452                                 if not dep.is_optional and not dep.installed and dep.package then
453                                         queue_download(dep.package, REASON_DEPENDENCY)
454                                 end
455                         end
456                 end
457
458                 this:delete()
459                 return true
460         end
461
462         if fields.selected_game then
463                 for _, game in pairs(pkgmgr.games) do
464                         if game.title == fields.selected_game then
465                                 core.settings:set("menu_last_game", game.id)
466                                 break
467                         end
468                 end
469                 return true
470         end
471
472         return false
473 end
474
475 function install_dialog.create(package, raw_deps)
476         install_dialog.dependencies = nil
477         install_dialog.package = package
478         install_dialog.raw_deps = raw_deps
479         install_dialog.will_install_deps = true
480         return dialog_create("install_dialog",
481                         install_dialog.get_formspec,
482                         install_dialog.handle_submit,
483                         nil)
484 end
485
486
487 local confirm_overwrite = {}
488 function confirm_overwrite.get_formspec()
489         local package = confirm_overwrite.package
490
491         return confirmation_formspec(
492                 fgettext("\"$1\" already exists. Would you like to overwrite it?", package.name),
493                 'install', fgettext("Overwrite"),
494                 'cancel', fgettext("Cancel"))
495 end
496
497 function confirm_overwrite.handle_submit(this, fields)
498         if fields.cancel then
499                 this:delete()
500                 return true
501         end
502
503         if fields.install then
504                 this:delete()
505                 confirm_overwrite.callback()
506                 return true
507         end
508
509         return false
510 end
511
512 function confirm_overwrite.create(package, callback)
513         assert(type(package) == "table")
514         assert(type(callback) == "function")
515
516         confirm_overwrite.package = package
517         confirm_overwrite.callback = callback
518         return dialog_create("confirm_overwrite",
519                 confirm_overwrite.get_formspec,
520                 confirm_overwrite.handle_submit,
521                 nil)
522 end
523
524
525 local function get_file_extension(path)
526         local parts = path:split(".")
527         return parts[#parts]
528 end
529
530 local function get_screenshot(package)
531         if not package.thumbnail then
532                 return defaulttexturedir .. "no_screenshot.png"
533         elseif screenshot_downloading[package.thumbnail] then
534                 return defaulttexturedir .. "loading_screenshot.png"
535         end
536
537         -- Get tmp screenshot path
538         local ext = get_file_extension(package.thumbnail)
539         local filepath = screenshot_dir .. DIR_DELIM ..
540                 ("%s-%s-%s.%s"):format(package.type, package.author, package.name, ext)
541
542         -- Return if already downloaded
543         local file = io.open(filepath, "r")
544         if file then
545                 file:close()
546                 return filepath
547         end
548
549         -- Show error if we've failed to download before
550         if screenshot_downloaded[package.thumbnail] then
551                 return defaulttexturedir .. "error_screenshot.png"
552         end
553
554         -- Download
555
556         local function download_screenshot(params)
557                 return core.download_file(params.url, params.dest)
558         end
559         local function callback(success)
560                 screenshot_downloading[package.thumbnail] = nil
561                 screenshot_downloaded[package.thumbnail] = true
562                 if not success then
563                         core.log("warning", "Screenshot download failed for some reason")
564                 end
565                 ui.update()
566         end
567         if core.handle_async(download_screenshot,
568                         { dest = filepath, url = package.thumbnail }, callback) then
569                 screenshot_downloading[package.thumbnail] = true
570         else
571                 core.log("error", "ERROR: async event failed")
572                 return defaulttexturedir .. "error_screenshot.png"
573         end
574
575         return defaulttexturedir .. "loading_screenshot.png"
576 end
577
578 function store.load()
579         local version = core.get_version()
580         local base_url = core.settings:get("contentdb_url")
581         local url = base_url ..
582                 "/api/packages/?type=mod&type=game&type=txp&protocol_version=" ..
583                 core.get_max_supp_proto() .. "&engine_version=" .. urlencode(version.string)
584
585         for _, item in pairs(core.settings:get("contentdb_flag_blacklist"):split(",")) do
586                 item = item:trim()
587                 if item ~= "" then
588                         url = url .. "&hide=" .. urlencode(item)
589                 end
590         end
591
592         local response = http.fetch_sync({ url = url })
593         if not response.succeeded then
594                 return
595         end
596
597         store.packages_full = core.parse_json(response.data) or {}
598         store.aliases = {}
599
600         for _, package in pairs(store.packages_full) do
601                 local name_len = #package.name
602                 -- This must match what store.update_paths() does!
603                 package.id = package.author:lower() .. "/"
604                 if package.type == "game" and name_len > 5 and package.name:sub(name_len - 4) == "_game" then
605                         package.id = package.id .. package.name:sub(1, name_len - 5)
606                 else
607                         package.id = package.id .. package.name
608                 end
609
610                 package.url_part = urlencode(package.author) .. "/" .. urlencode(package.name)
611
612                 if package.aliases then
613                         for _, alias in ipairs(package.aliases) do
614                                 -- We currently don't support name changing
615                                 local suffix = "/" .. package.name
616                                 if alias:sub(-#suffix) == suffix then
617                                         store.aliases[alias:lower()] = package.id
618                                 end
619                         end
620                 end
621         end
622
623         store.packages_full_unordered = store.packages_full
624         store.packages = store.packages_full
625         store.loaded = true
626 end
627
628 function store.update_paths()
629         local mod_hash = {}
630         pkgmgr.refresh_globals()
631         for _, mod in pairs(pkgmgr.global_mods:get_list()) do
632                 if mod.author and mod.release > 0 then
633                         local id = mod.author:lower() .. "/" .. mod.name
634                         mod_hash[store.aliases[id] or id] = mod
635                 end
636         end
637
638         local game_hash = {}
639         pkgmgr.update_gamelist()
640         for _, game in pairs(pkgmgr.games) do
641                 if game.author ~= "" and game.release > 0 then
642                         local id = game.author:lower() .. "/" .. game.id
643                         game_hash[store.aliases[id] or id] = game
644                 end
645         end
646
647         local txp_hash = {}
648         for _, txp in pairs(pkgmgr.get_texture_packs()) do
649                 if txp.author and txp.release > 0 then
650                         local id = txp.author:lower() .. "/" .. txp.name
651                         txp_hash[store.aliases[id] or id] = txp
652                 end
653         end
654
655         for _, package in pairs(store.packages_full) do
656                 local content
657                 if package.type == "mod" then
658                         content = mod_hash[package.id]
659                 elseif package.type == "game" then
660                         content = game_hash[package.id]
661                 elseif package.type == "txp" then
662                         content = txp_hash[package.id]
663                 end
664
665                 if content then
666                         package.path = content.path
667                         package.installed_release = content.release or 0
668                 else
669                         package.path = nil
670                 end
671         end
672 end
673
674 function store.sort_packages()
675         local ret = {}
676
677         -- Add installed content
678         for i=1, #store.packages_full_unordered do
679                 local package = store.packages_full_unordered[i]
680                 if package.path then
681                         ret[#ret + 1] = package
682                 end
683         end
684
685         -- Sort installed content by title
686         table.sort(ret, function(a, b)
687                 return a.title < b.title
688         end)
689
690         -- Add uninstalled content
691         for i=1, #store.packages_full_unordered do
692                 local package = store.packages_full_unordered[i]
693                 if not package.path then
694                         ret[#ret + 1] = package
695                 end
696         end
697
698         store.packages_full = ret
699 end
700
701 function store.filter_packages(query)
702         if query == "" and filter_type == 1 then
703                 store.packages = store.packages_full
704                 return
705         end
706
707         local keywords = {}
708         for word in query:lower():gmatch("%S+") do
709                 table.insert(keywords, word)
710         end
711
712         local function matches_keywords(package)
713                 for k = 1, #keywords do
714                         local keyword = keywords[k]
715
716                         if string.find(package.name:lower(), keyword, 1, true) or
717                                         string.find(package.title:lower(), keyword, 1, true) or
718                                         string.find(package.author:lower(), keyword, 1, true) or
719                                         string.find(package.short_description:lower(), keyword, 1, true) then
720                                 return true
721                         end
722                 end
723
724                 return false
725         end
726
727         store.packages = {}
728         for _, package in pairs(store.packages_full) do
729                 if (query == "" or matches_keywords(package)) and
730                                 (filter_type == 1 or package.type == filter_types_type[filter_type]) then
731                         store.packages[#store.packages + 1] = package
732                 end
733         end
734 end
735
736 function store.get_formspec(dlgdata)
737         store.update_paths()
738
739         dlgdata.pagemax = math.max(math.ceil(#store.packages / num_per_page), 1)
740         if cur_page > dlgdata.pagemax then
741                 cur_page = 1
742         end
743
744         local W = 15.75
745         local H = 9.5
746         local formspec
747         if #store.packages_full > 0 then
748                 formspec = {
749                         "formspec_version[3]",
750                         "size[15.75,9.5]",
751                         "position[0.5,0.55]",
752
753                         "style[status,downloading,queued;border=false]",
754
755                         "container[0.375,0.375]",
756                         "field[0,0;7.225,0.8;search_string;;", core.formspec_escape(search_string), "]",
757                         "field_close_on_enter[search_string;false]",
758                         "image_button[7.3,0;0.8,0.8;", core.formspec_escape(defaulttexturedir .. "search.png"), ";search;]",
759                         "image_button[8.125,0;0.8,0.8;", core.formspec_escape(defaulttexturedir .. "clear.png"), ";clear;]",
760                         "dropdown[9.6,0;2.4,0.8;type;", table.concat(filter_types_titles, ","), ";", filter_type, "]",
761                         "container_end[]",
762
763                         -- Page nav buttons
764                         "container[0,", H - 0.8 - 0.375, "]",
765                         "button[0.375,0;4,0.8;back;", fgettext("Back to Main Menu"), "]",
766
767                         "container[", W - 0.375 - 0.8*4 - 2,  ",0]",
768                         "image_button[0,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "start_icon.png;pstart;]",
769                         "image_button[0.8,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "prev_icon.png;pback;]",
770                         "style[pagenum;border=false]",
771                         "button[1.6,0;2,0.8;pagenum;", tonumber(cur_page), " / ", tonumber(dlgdata.pagemax), "]",
772                         "image_button[3.6,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "next_icon.png;pnext;]",
773                         "image_button[4.4,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "end_icon.png;pend;]",
774                         "container_end[]",
775
776                         "container_end[]",
777                 }
778
779                 if number_downloading > 0 then
780                         formspec[#formspec + 1] = "button[12.75,0.375;2.625,0.8;downloading;"
781                         if #download_queue > 0 then
782                                 formspec[#formspec + 1] = fgettext("$1 downloading,\n$2 queued", number_downloading, #download_queue)
783                         else
784                                 formspec[#formspec + 1] = fgettext("$1 downloading...", number_downloading)
785                         end
786                         formspec[#formspec + 1] = "]"
787                 else
788                         local num_avail_updates = 0
789                         for i=1, #store.packages_full do
790                                 local package = store.packages_full[i]
791                                 if package.path and package.installed_release < package.release and
792                                                 not (package.downloading or package.queued) then
793                                         num_avail_updates = num_avail_updates + 1
794                                 end
795                         end
796
797                         if num_avail_updates == 0 then
798                                 formspec[#formspec + 1] = "button[12.75,0.375;2.625,0.8;status;"
799                                 formspec[#formspec + 1] = fgettext("No updates")
800                                 formspec[#formspec + 1] = "]"
801                         else
802                                 formspec[#formspec + 1] = "button[12.75,0.375;2.625,0.8;update_all;"
803                                 formspec[#formspec + 1] = fgettext("Update All [$1]", num_avail_updates)
804                                 formspec[#formspec + 1] = "]"
805                         end
806                 end
807
808                 if #store.packages == 0 then
809                         formspec[#formspec + 1] = "label[4,3;"
810                         formspec[#formspec + 1] = fgettext("No results")
811                         formspec[#formspec + 1] = "]"
812                 end
813         else
814                 formspec = {
815                         "size[12,7]",
816                         "position[0.5,0.55]",
817                         "label[4,3;", fgettext("No packages could be retrieved"), "]",
818                         "container[0,", H - 0.8 - 0.375, "]",
819                         "button[0,0;4,0.8;back;", fgettext("Back to Main Menu"), "]",
820                         "container_end[]",
821                 }
822         end
823
824         -- download/queued tooltips always have the same message
825         local tooltip_colors = ";#dff6f5;#302c2e]"
826         formspec[#formspec + 1] = "tooltip[downloading;" .. fgettext("Downloading...") .. tooltip_colors
827         formspec[#formspec + 1] = "tooltip[queued;" .. fgettext("Queued") .. tooltip_colors
828
829         local start_idx = (cur_page - 1) * num_per_page + 1
830         for i=start_idx, math.min(#store.packages, start_idx+num_per_page-1) do
831                 local package = store.packages[i]
832                 local container_y = (i - start_idx) * 1.375 + (2*0.375 + 0.8)
833                 formspec[#formspec + 1] = "container[0.375,"
834                 formspec[#formspec + 1] = container_y
835                 formspec[#formspec + 1] = "]"
836
837                 -- image
838                 formspec[#formspec + 1] = "image[0,0;1.5,1;"
839                 formspec[#formspec + 1] = core.formspec_escape(get_screenshot(package))
840                 formspec[#formspec + 1] = "]"
841
842                 -- title
843                 formspec[#formspec + 1] = "label[1.875,0.1;"
844                 formspec[#formspec + 1] = core.formspec_escape(
845                                 core.colorize(mt_color_green, package.title) ..
846                                 core.colorize("#BFBFBF", " by " .. package.author))
847                 formspec[#formspec + 1] = "]"
848
849                 -- buttons
850                 local left_base = "image_button[-1.55,0;0.7,0.7;" .. core.formspec_escape(defaulttexturedir)
851                 formspec[#formspec + 1] = "container["
852                 formspec[#formspec + 1] = W - 0.375*2
853                 formspec[#formspec + 1] = ",0.1]"
854
855                 if package.downloading then
856                         formspec[#formspec + 1] = "animated_image[-1.7,-0.15;1,1;downloading;"
857                         formspec[#formspec + 1] = core.formspec_escape(defaulttexturedir)
858                         formspec[#formspec + 1] = "cdb_downloading.png;3;400;]"
859                 elseif package.queued then
860                         formspec[#formspec + 1] = left_base
861                         formspec[#formspec + 1] = "cdb_queued.png;queued;]"
862                 elseif not package.path then
863                         local elem_name = "install_" .. i .. ";"
864                         formspec[#formspec + 1] = "style[" .. elem_name .. "bgcolor=#71aa34]"
865                         formspec[#formspec + 1] = left_base .. "cdb_add.png;" .. elem_name .. "]"
866                         formspec[#formspec + 1] = "tooltip[" .. elem_name .. fgettext("Install") .. tooltip_colors
867                 else
868                         if package.installed_release < package.release then
869
870                                 -- The install_ action also handles updating
871                                 local elem_name = "install_" .. i .. ";"
872                                 formspec[#formspec + 1] = "style[" .. elem_name .. "bgcolor=#28ccdf]"
873                                 formspec[#formspec + 1] = left_base .. "cdb_update.png;" .. elem_name .. "]"
874                                 formspec[#formspec + 1] = "tooltip[" .. elem_name .. fgettext("Update") .. tooltip_colors
875                         else
876
877                                 local elem_name = "uninstall_" .. i .. ";"
878                                 formspec[#formspec + 1] = "style[" .. elem_name .. "bgcolor=#a93b3b]"
879                                 formspec[#formspec + 1] = left_base .. "cdb_clear.png;" .. elem_name .. "]"
880                                 formspec[#formspec + 1] = "tooltip[" .. elem_name .. fgettext("Uninstall") .. tooltip_colors
881                         end
882                 end
883
884                 local web_elem_name = "view_" .. i .. ";"
885                 formspec[#formspec + 1] = "image_button[-0.7,0;0.7,0.7;" ..
886                         core.formspec_escape(defaulttexturedir) .. "cdb_viewonline.png;" .. web_elem_name .. "]"
887                 formspec[#formspec + 1] = "tooltip[" .. web_elem_name ..
888                         fgettext("View more information in a web browser") .. tooltip_colors
889                 formspec[#formspec + 1] = "container_end[]"
890
891                 -- description
892                 local description_width = W - 0.375*5 - 0.85 - 2*0.7
893                 formspec[#formspec + 1] = "textarea[1.855,0.3;"
894                 formspec[#formspec + 1] = tostring(description_width)
895                 formspec[#formspec + 1] = ",0.8;;;"
896                 formspec[#formspec + 1] = core.formspec_escape(package.short_description)
897                 formspec[#formspec + 1] = "]"
898
899                 formspec[#formspec + 1] = "container_end[]"
900         end
901
902         return table.concat(formspec, "")
903 end
904
905 function store.handle_submit(this, fields)
906         if fields.search or fields.key_enter_field == "search_string" then
907                 search_string = fields.search_string:trim()
908                 cur_page = 1
909                 store.filter_packages(search_string)
910                 return true
911         end
912
913         if fields.clear then
914                 search_string = ""
915                 cur_page = 1
916                 store.filter_packages("")
917                 return true
918         end
919
920         if fields.back then
921                 this:delete()
922                 return true
923         end
924
925         if fields.pstart then
926                 cur_page = 1
927                 return true
928         end
929
930         if fields.pend then
931                 cur_page = this.data.pagemax
932                 return true
933         end
934
935         if fields.pnext then
936                 cur_page = cur_page + 1
937                 if cur_page > this.data.pagemax then
938                         cur_page = 1
939                 end
940                 return true
941         end
942
943         if fields.pback then
944                 if cur_page == 1 then
945                         cur_page = this.data.pagemax
946                 else
947                         cur_page = cur_page - 1
948                 end
949                 return true
950         end
951
952         if fields.type then
953                 local new_type = table.indexof(filter_types_titles, fields.type)
954                 if new_type ~= filter_type then
955                         filter_type = new_type
956                         store.filter_packages(search_string)
957                         return true
958                 end
959         end
960
961         if fields.update_all then
962                 for i=1, #store.packages_full do
963                         local package = store.packages_full[i]
964                         if package.path and package.installed_release < package.release and
965                                         not (package.downloading or package.queued) then
966                                 queue_download(package, REASON_UPDATE)
967                         end
968                 end
969                 return true
970         end
971
972         local start_idx = (cur_page - 1) * num_per_page + 1
973         assert(start_idx ~= nil)
974         for i=start_idx, math.min(#store.packages, start_idx+num_per_page-1) do
975                 local package = store.packages[i]
976                 assert(package)
977
978                 if fields["install_" .. i] then
979                         local install_parent
980                         if package.type == "mod" then
981                                 install_parent = core.get_modpath()
982                         elseif package.type == "game" then
983                                 install_parent = core.get_gamepath()
984                         elseif package.type == "txp" then
985                                 install_parent = core.get_texturepath()
986                         else
987                                 error("Unknown package type: " .. package.type)
988                         end
989
990
991                         local function on_confirm()
992                                 local deps = get_raw_dependencies(package)
993                                 if deps and has_hard_deps(deps) then
994                                         local dlg = install_dialog.create(package, deps)
995                                         dlg:set_parent(this)
996                                         this:hide()
997                                         dlg:show()
998                                 else
999                                         queue_download(package, package.path and REASON_UPDATE or REASON_NEW)
1000                                 end
1001                         end
1002
1003                         if not package.path and core.is_dir(install_parent .. DIR_DELIM .. package.name) then
1004                                 local dlg = confirm_overwrite.create(package, on_confirm)
1005                                 dlg:set_parent(this)
1006                                 this:hide()
1007                                 dlg:show()
1008                         else
1009                                 on_confirm()
1010                         end
1011
1012                         return true
1013                 end
1014
1015                 if fields["uninstall_" .. i] then
1016                         local dlg = create_delete_content_dlg(package)
1017                         dlg:set_parent(this)
1018                         this:hide()
1019                         dlg:show()
1020                         return true
1021                 end
1022
1023                 if fields["view_" .. i] then
1024                         local url = ("%s/packages/%s?protocol_version=%d"):format(
1025                                         core.settings:get("contentdb_url"), package.url_part,
1026                                         core.get_max_supp_proto())
1027                         core.open_url(url)
1028                         return true
1029                 end
1030         end
1031
1032         return false
1033 end
1034
1035 function create_store_dlg(type)
1036         if not store.loaded or #store.packages_full == 0 then
1037                 store.load()
1038         end
1039
1040         store.update_paths()
1041         store.sort_packages()
1042
1043         search_string = ""
1044         cur_page = 1
1045
1046         if type then
1047                 -- table.indexof does not work on tables that contain `nil`
1048                 for i, v in pairs(filter_types_type) do
1049                         if v == type then
1050                                 filter_type = i
1051                                 break
1052                         end
1053                 end
1054         end
1055
1056         store.filter_packages(search_string)
1057
1058         return dialog_create("store",
1059                         store.get_formspec,
1060                         store.handle_submit,
1061                         nil)
1062 end