Remove indev mapgen
[oweals/minetest.git] / builtin / mainmenu / store.lua
1 --Minetest
2 --Copyright (C) 2013 sapier
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 --------------------------------------------------------------------------------
19
20 --modstore implementation
21 modstore = {}
22
23 --------------------------------------------------------------------------------
24 -- @function [parent=#modstore] init
25 function modstore.init(size, unsortedmods, searchmods)
26
27         modstore.mods_on_unsorted_page = unsortedmods
28         modstore.mods_on_search_page = searchmods
29         modstore.modsperpage = modstore.mods_on_unsorted_page
30
31         modstore.basetexturedir = core.get_texturepath() .. DIR_DELIM .. "base" ..
32                                                 DIR_DELIM .. "pack" .. DIR_DELIM
33
34         modstore.lastmodtitle = ""
35         modstore.last_search = ""
36
37         modstore.searchlist = filterlist.create(
38                 function()
39                         if modstore.modlist_unsorted ~= nil and
40                                 modstore.modlist_unsorted.data ~= nil then
41                                 return modstore.modlist_unsorted.data
42                         end
43                         return {}
44                 end,
45                 function(element,modid)
46                         if element.id == modid then
47                                 return true
48                         end
49                         return false
50                 end, --compare fct
51                 nil, --uid match fct
52                 function(element,substring)
53                         if substring == nil or
54                                 substring == "" then
55                                 return false
56                         end
57                         substring = substring:upper()
58
59                         if element.title ~= nil and
60                                 element.title:upper():find(substring) ~= nil then
61                                 return true
62                         end
63
64                         if element.details ~= nil and
65                                 element.details.author ~= nil and
66                                 element.details.author:upper():find(substring) ~= nil then
67                                 return true
68                         end
69
70                         if element.details ~= nil and
71                                 element.details.description ~= nil and
72                                 element.details.description:upper():find(substring) ~= nil then
73                                 return true
74                         end
75                         return false
76                 end --filter fct
77                 )
78
79         modstore.current_list = nil
80
81         modstore.tv_store = tabview_create("modstore",size,{x=0,y=0})
82
83         modstore.tv_store:set_global_event_handler(modstore.handle_events)
84
85         modstore.tv_store:add(
86                 {
87                 name = "unsorted",
88                 caption = fgettext("Unsorted"),
89                 cbf_formspec       = modstore.unsorted_tab,
90                 cbf_button_handler = modstore.handle_buttons,
91                 on_change          =
92                         function() modstore.modsperpage = modstore.mods_on_unsorted_page end
93                 }
94         )
95
96         modstore.tv_store:add(
97                 {
98                 name = "search",
99                 caption            = fgettext("Search"),
100                 cbf_formspec       = modstore.getsearchpage,
101                 cbf_button_handler = modstore.handle_buttons,
102                 on_change          = modstore.activate_search_tab
103                 }
104         )
105 end
106
107 --------------------------------------------------------------------------------
108 -- @function [parent=#modstore] nametoindex
109 function modstore.nametoindex(name)
110
111         for i=1,#modstore.tabnames,1 do
112                 if modstore.tabnames[i] == name then
113                         return i
114                 end
115         end
116
117         return 1
118 end
119
120 --------------------------------------------------------------------------------
121 -- @function [parent=#modstore] showdownloading
122 function modstore.showdownloading(title)
123         local new_dlg = dialog_create("store_downloading",
124                 function(data)
125                         return "size[6,2]label[0.25,0.75;" .. fgettext("Downloading") ..
126                                 " " .. data.title .. " " ..
127                                 fgettext("please wait...") .. "]"
128                 end,
129                 function(this,fields)
130                         if fields["btn_hidden_close_download"] ~= nil then
131                                 if fields["btn_hidden_close_download"].successfull then
132                                         modstore.lastmodentry = fields["btn_hidden_close_download"]
133                                         modstore.successfulldialog()
134                                 else
135                                         modstore.lastmodtitle = ""
136                                 end
137
138                                 this:delete()
139                                 return true
140                         end
141
142                         return false
143                 end,
144                 nil,
145                 modstore.tv_store)
146
147         new_dlg.data.title = title
148         new_dlg:show()
149 end
150
151 --------------------------------------------------------------------------------
152 -- @function [parent=#modstore] successfulldialog
153 function modstore.successfulldialog()
154         local new_dlg = dialog_create("store_downloading",
155                 function(data)
156                         local retval = ""
157                         retval = retval .. "size[6,2,true]"
158                         if modstore.lastmodentry ~= nil then
159                                 retval = retval .. "label[0,0.25;" .. fgettext("Successfully installed:") .. "]"
160                                 retval = retval .. "label[3,0.25;" .. modstore.lastmodentry.moddetails.title .. "]"
161
162
163                                 retval = retval .. "label[0,0.75;" .. fgettext("Shortname:") .. "]"
164                                 retval = retval .. "label[3,0.75;" .. core.formspec_escape(modstore.lastmodentry.moddetails.basename) .. "]"
165
166                         end
167                         retval = retval .. "button[2.5,1.5;1,0.5;btn_confirm_mod_successfull;" .. fgettext("ok") .. "]"
168                 end,
169                 function(this,fields)
170                         if fields["btn_confirm_mod_successfull"] ~= nil then
171                                 this.parent:show()
172                                 this:hide()
173                                 this:delete()
174
175                                 return true
176                         end
177
178                         return false
179                 end,
180                 nil,
181                 modstore.tv_store)
182
183         new_dlg.data.title = title
184         new_dlg:show()
185 end
186
187 --------------------------------------------------------------------------------
188 -- @function [parent=#modstore] handle_buttons
189 function modstore.handle_buttons(parent, fields, name, data)
190
191         if fields["btn_modstore_page_up"] then
192                 if modstore.current_list ~= nil and modstore.current_list.page > 0 then
193                         modstore.current_list.page = modstore.current_list.page - 1
194                 end
195                 return true
196         end
197
198         if fields["btn_modstore_page_down"] then
199                 if modstore.current_list ~= nil and
200                         modstore.current_list.page <modstore.current_list.pagecount-1 then
201                         modstore.current_list.page = modstore.current_list.page +1
202                 end
203                 return true
204         end
205
206         if fields["btn_modstore_search"] or
207                 (fields["key_enter"] and fields["te_modstore_search"] ~= nil) then
208                 modstore.last_search = fields["te_modstore_search"]
209                 filterlist.set_filtercriteria(modstore.searchlist,fields["te_modstore_search"])
210                 filterlist.refresh(modstore.searchlist)
211                 modstore.currentlist = {
212                         page = 0,
213                         pagecount =  math.ceil(filterlist.size(modstore.searchlist) / modstore.modsperpage),
214                         data = filterlist.get_list(modstore.searchlist),
215                 }
216                 return true
217         end
218
219         if fields["btn_modstore_close"] then
220                 parent:hide()
221                 return true
222         end
223
224         for key,value in pairs(fields) do
225                 local foundat = key:find("btn_install_mod_")
226                 if ( foundat == 1) then
227                         local modid = tonumber(key:sub(17))
228                         for i=1,#modstore.modlist_unsorted.data,1 do
229                                 if modstore.modlist_unsorted.data[i].id == modid then
230                                         local moddetails = modstore.modlist_unsorted.data[i].details
231
232                                         if modstore.lastmodtitle ~= "" then
233                                                 modstore.lastmodtitle = modstore.lastmodtitle .. ", "
234                                         end
235
236                                         modstore.lastmodtitle = modstore.lastmodtitle .. moddetails.title
237
238                                         if not core.handle_async(
239                                                 function(param)
240                                                         local fullurl = core.setting_get("modstore_download_url") ..
241                                                                                         param.moddetails.download_url
242
243                                                         if param.version ~= nil then
244                                                                 local found = false
245                                                                 for i=1,#param.moddetails.versions, 1 do
246                                                                         if param.moddetails.versions[i].date:sub(1,10) == param.version then
247                                                                                 fullurl = core.setting_get("modstore_download_url") ..
248                                                                                                                 param.moddetails.versions[i].download_url
249                                                                                 found = true
250                                                                         end
251                                                                 end
252
253                                                                 if not found then
254                                                                         core.log("error","no download url found for version " .. dump(param.version))
255                                                                         return {
256                                                                                 moddetails = param.moddetails,
257                                                                                 successfull = false
258                                                                         }
259                                                                 end
260                                                         end
261
262                                                         if core.download_file(fullurl,param.filename) then
263                                                                 return {
264                                                                         texturename = param.texturename,
265                                                                         moddetails = param.moddetails,
266                                                                         filename = param.filename,
267                                                                         successfull = true
268                                                                 }
269                                                         else
270                                                                 core.log("error","downloading " .. dump(fullurl) .. " failed")
271                                                                 return {
272                                                                         moddetails = param.moddetails,
273                                                                         successfull = false
274                                                                 }
275                                                         end
276                                                 end,
277                                                 {
278                                                         moddetails = moddetails,
279                                                         version = fields["dd_version" .. modid],
280                                                         filename = os.tempfolder() .. "_MODNAME_" .. moddetails.basename .. ".zip",
281                                                         texturename = modstore.modlist_unsorted.data[i].texturename
282                                                 },
283                                                 function(result)
284                                                         print("Result from async: " .. dump(result.successfull))
285                                                         if result.successfull then
286                                                                 modmgr.installmod(result.filename,result.moddetails.basename)
287                                                                 os.remove(result.filename)
288                                                         else
289                                                                 gamedata.errormessage = "Failed to download " .. result.moddetails.title
290                                                         end
291
292                                                         if gamedata.errormessage == nil then
293                                                                 core.button_handler({btn_hidden_close_download=result})
294                                                         else
295                                                                 core.button_handler({btn_hidden_close_download={successfull=false}})
296                                                         end
297                                                 end
298                                         ) then
299                                                 print("ERROR: async event failed")
300                                                 gamedata.errormessage = "Failed to download " .. modstore.lastmodtitle
301                                         end
302                                         parent:hide()
303                                         modstore.showdownloading(modstore.lastmodtitle)
304                                         return true
305                                 end
306                         end
307                         return true
308                 end
309         end
310
311         return false
312 end
313
314 --------------------------------------------------------------------------------
315 -- @function [parent=#modstore] handle_events
316 function modstore.handle_events(this,event)
317         if (event == "MenuQuit") then
318                 this:hide()
319                 return true
320         end
321 end
322
323 --------------------------------------------------------------------------------
324 -- @function [parent=#modstore] update_modlist
325 function modstore.update_modlist()
326         modstore.modlist_unsorted = {}
327         modstore.modlist_unsorted.data = {}
328         modstore.modlist_unsorted.pagecount = 1
329         modstore.modlist_unsorted.page = 0
330
331         core.handle_async(
332                 function(param)
333                         return core.get_modstore_list()
334                 end,
335                 nil,
336                 function(result)
337                         if result ~= nil then
338                                 modstore.modlist_unsorted = {}
339                                 modstore.modlist_unsorted.data = result
340
341                                 if modstore.modlist_unsorted.data ~= nil then
342                                         modstore.modlist_unsorted.pagecount =
343                                                 math.ceil((#modstore.modlist_unsorted.data / modstore.modsperpage))
344                                 else
345                                         modstore.modlist_unsorted.data = {}
346                                         modstore.modlist_unsorted.pagecount = 1
347                                 end
348                                 modstore.modlist_unsorted.page = 0
349                                 modstore.fetchdetails()
350                                 core.event_handler("Refresh")
351                         end
352                 end
353         )
354 end
355
356 --------------------------------------------------------------------------------
357 -- @function [parent=#modstore] fetchdetails
358 function modstore.fetchdetails()
359
360         for i=1,#modstore.modlist_unsorted.data,1 do
361                 core.handle_async(
362                 function(param)
363                         param.details = core.get_modstore_details(tostring(param.modid))
364                         return param
365                 end,
366                 {
367                         modid=modstore.modlist_unsorted.data[i].id,
368                         listindex=i
369                 },
370                 function(result)
371                         if result ~= nil and
372                                 modstore.modlist_unsorted ~= nil
373                                 and modstore.modlist_unsorted.data ~= nil and
374                                 modstore.modlist_unsorted.data[result.listindex] ~= nil and
375                                 modstore.modlist_unsorted.data[result.listindex].id ~= nil then
376
377                                 modstore.modlist_unsorted.data[result.listindex].details = result.details
378                                 core.event_handler("Refresh")
379                         end
380                 end
381                 )
382         end
383 end
384
385 --------------------------------------------------------------------------------
386 -- @function [parent=#modstore] getscreenshot
387 function modstore.getscreenshot(ypos,listentry)
388
389         if      listentry.details ~= nil and
390                 (listentry.details.screenshot_url == nil or
391                 listentry.details.screenshot_url == "") then
392
393                 if listentry.texturename == nil then
394                         listentry.texturename = modstore.basetexturedir .. "no_screenshot.png"
395                 end
396
397                 return "image[0,".. ypos .. ";3,2;" ..
398                         core.formspec_escape(listentry.texturename) .. "]"
399         end
400
401         if listentry.details ~= nil and
402                 listentry.texturename == nil then
403                 --make sure we don't download multiple times
404                 listentry.texturename = "in progress"
405
406                 --prepare url and filename
407                 local fullurl = core.setting_get("modstore_download_url") ..
408                                         listentry.details.screenshot_url
409                 local filename = os.tempfolder() .. "_MID_" .. listentry.id
410
411                 --trigger download
412                 core.handle_async(
413                         --first param is downloadfct
414                         function(param)
415                                 param.successfull = core.download_file(param.fullurl,param.filename)
416                                 return param
417                         end,
418                         --second parameter is data passed to async job
419                         {
420                                 fullurl = fullurl,
421                                 filename = filename,
422                                 modid = listentry.id
423                         },
424                         --integrate result to raw list
425                         function(result)
426                                 if result.successfull then
427                                         local found = false
428                                         for i=1,#modstore.modlist_unsorted.data,1 do
429                                                 if modstore.modlist_unsorted.data[i].id == result.modid then
430                                                         found = true
431                                                         modstore.modlist_unsorted.data[i].texturename = result.filename
432                                                         break
433                                                 end
434                                         end
435                                         if found then
436                                                 core.event_handler("Refresh")
437                                         else
438                                                 core.log("error","got screenshot but didn't find matching mod: " .. result.modid)
439                                         end
440                                 end
441                         end
442                 )
443         end
444
445         if listentry.texturename ~= nil and
446                 listentry.texturename ~= "in progress" then
447                 return "image[0,".. ypos .. ";3,2;" ..
448                         core.formspec_escape(listentry.texturename) .. "]"
449         end
450
451         return ""
452 end
453
454 --------------------------------------------------------------------------------
455 --@function [parent=#modstore] getshortmodinfo
456 function modstore.getshortmodinfo(ypos,listentry,details)
457         local retval = ""
458
459         retval = retval .. "box[0," .. ypos .. ";11.4,1.75;#FFFFFF]"
460
461         --screenshot
462         retval = retval .. modstore.getscreenshot(ypos,listentry)
463
464         --title + author
465         retval = retval .."label[2.75," .. ypos .. ";" ..
466                 core.formspec_escape(details.title) .. " (" .. details.author .. ")]"
467
468         --description
469         local descriptiony = ypos + 0.5
470         retval = retval .. "textarea[3," .. descriptiony .. ";6.5,1.55;;" ..
471                 core.formspec_escape(details.description) .. ";]"
472
473         --rating
474         local ratingy = ypos
475         retval = retval .."label[7," .. ratingy .. ";" ..
476                                         fgettext("Rating") .. ":]"
477         retval = retval .. "label[8.7," .. ratingy .. ";" .. details.rating .."]"
478
479         --versions (IMPORTANT has to be defined AFTER rating)
480         if details.versions ~= nil and
481                 #details.versions > 1 then
482                 local versiony = ypos + 0.05
483                 retval = retval .. "dropdown[9.1," .. versiony .. ";2.48,0.25;dd_version" .. details.id .. ";"
484                 local versions = ""
485                 for i=1,#details.versions , 1 do
486                         if versions ~= "" then
487                                 versions = versions .. ","
488                         end
489
490                         versions = versions .. details.versions[i].date:sub(1,10)
491                 end
492                 retval = retval .. versions .. ";1]"
493         end
494
495         if details.basename then
496                 --install button
497                 local buttony = ypos + 1.2
498                 retval = retval .."button[9.1," .. buttony .. ";2.5,0.5;btn_install_mod_" .. details.id .. ";"
499
500                 if modmgr.mod_exists(details.basename) then
501                         retval = retval .. fgettext("re-Install") .."]"
502                 else
503                         retval = retval .. fgettext("Install") .."]"
504                 end
505         end
506
507         return retval
508 end
509
510 --------------------------------------------------------------------------------
511 --@function [parent=#modstore] getmodlist
512 function modstore.getmodlist(list,yoffset)
513         modstore.current_list = list
514
515         if yoffset == nil then
516                 yoffset = 0
517         end
518
519         local sb_y_start = 0.2 + yoffset
520         local sb_y_end   = (modstore.modsperpage * 1.75) + ((modstore.modsperpage-1) * 0.15)
521         local close_button = "button[4," .. (sb_y_end + 0.3 + yoffset) ..
522                         ";4,0.5;btn_modstore_close;" .. fgettext("Close store") .. "]"
523
524         if #list.data == 0 then
525                 return close_button
526         end
527
528         local scrollbar = ""
529         scrollbar = scrollbar .. "label[0.1,".. (sb_y_end + 0.25 + yoffset) ..";"
530                                 .. fgettext("Page $1 of $2", list.page+1, list.pagecount) .. "]"
531         scrollbar = scrollbar .. "box[11.6," .. sb_y_start .. ";0.28," .. sb_y_end .. ";#000000]"
532         local scrollbarpos = (sb_y_start + 0.5) +
533                                 ((sb_y_end -1.6)/(list.pagecount-1)) * list.page
534         scrollbar = scrollbar .. "box[11.6," ..scrollbarpos .. ";0.28,0.5;#32CD32]"
535         scrollbar = scrollbar .. "button[11.6," .. (sb_y_start)
536                                 .. ";0.5,0.5;btn_modstore_page_up;^]"
537         scrollbar = scrollbar .. "button[11.6," .. (sb_y_start + sb_y_end - 0.5)
538                                 .. ";0.5,0.5;btn_modstore_page_down;v]"
539
540         local retval = ""
541
542         local endmod = (list.page * modstore.modsperpage) + modstore.modsperpage
543
544         if (endmod > #list.data) then
545                 endmod = #list.data
546         end
547
548         for i=(list.page * modstore.modsperpage) +1, endmod, 1 do
549                 --getmoddetails
550                 local details = list.data[i].details
551
552                 if details == nil then
553                         details = {}
554                         details.title = list.data[i].title
555                         details.author = ""
556                         details.rating = -1
557                         details.description = ""
558                 end
559
560                 if details ~= nil then
561                         local screenshot_ypos =
562                                 yoffset +(i-1 - (list.page * modstore.modsperpage))*1.9 +0.2
563
564                         retval = retval .. modstore.getshortmodinfo(screenshot_ypos,
565                                                                                                                 list.data[i],
566                                                                                                                 details)
567                 end
568         end
569
570         return retval .. scrollbar .. close_button
571 end
572
573 --------------------------------------------------------------------------------
574 --@function [parent=#modstore] getsearchpage
575 function modstore.getsearchpage(tabview, name, tabdata)
576         local retval = ""
577         local search = ""
578
579         if modstore.last_search ~= nil then
580                 search = modstore.last_search
581         end
582
583         retval = retval ..
584                 "button[9.5,0.2;2.5,0.5;btn_modstore_search;".. fgettext("Search") .. "]" ..
585                 "field[0.5,0.5;9,0.5;te_modstore_search;;" .. search .. "]"
586
587         retval = retval ..
588                 modstore.getmodlist(
589                         modstore.currentlist,
590                         1.75)
591
592         return retval;
593 end
594
595 --------------------------------------------------------------------------------
596 --@function [parent=#modstore] unsorted_tab
597 function modstore.unsorted_tab()
598         return modstore.getmodlist(modstore.modlist_unsorted)
599 end
600
601 --------------------------------------------------------------------------------
602 --@function [parent=#modstore] activate_search_tab
603 function modstore.activate_search_tab(type, old_tab, new_tab)
604
605         if old_tab == new_tab then
606                 return
607         end
608         filterlist.set_filtercriteria(modstore.searchlist,modstore.last_search)
609         filterlist.refresh(modstore.searchlist)
610         modstore.modsperpage = modstore.mods_on_search_page
611         modstore.currentlist = {
612                 page = 0,
613                 pagecount =
614                         math.ceil(filterlist.size(modstore.searchlist) / modstore.modsperpage),
615                 data = filterlist.get_list(modstore.searchlist),
616         }
617 end
618