e33e87ef5f3e169525703b1b723be2eb5679063e
[oweals/minetest.git] / src / guiConfigureWorld.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20
21 #include <iostream> 
22 #include <string>
23 #include <map>
24
25 #include "guiConfigureWorld.h"
26 #include "guiMessageMenu.h"
27 #include <IGUIButton.h>
28 #include <IGUICheckBox.h>
29 #include <IGUIListBox.h>
30 #include <IGUIStaticText.h>
31 #include <IGUITreeView.h>
32 #include "gettext.h"
33 #include "util/string.h"
34 #include "settings.h"
35 #include "filesys.h"
36
37 enum
38 {
39         GUI_ID_MOD_TREEVIEW = 101,
40         GUI_ID_ENABLED_CHECKBOX,
41         GUI_ID_ENABLEALL,
42         GUI_ID_DISABLEALL,
43         GUI_ID_DEPENDS_LISTBOX,
44         GUI_ID_RDEPENDS_LISTBOX,
45         GUI_ID_CANCEL,
46         GUI_ID_SAVE
47 };
48
49 #define QUESTIONMARK_STR L"?"
50 #define CHECKMARK_STR    L"\411"
51 #define CROSS_STR        L"\403"
52
53 GUIConfigureWorld::GUIConfigureWorld(gui::IGUIEnvironment* env,
54                 gui::IGUIElement* parent, s32 id,
55                 IMenuManager *menumgr, WorldSpec wspec):
56         GUIModalMenu(env, parent, id, menumgr),
57         m_wspec(wspec),
58         m_gspec(findWorldSubgame(m_wspec.path)),
59         m_menumgr(menumgr)
60 {
61         //will be initialized in regenerateGUI()
62         m_treeview=NULL;
63
64         // game mods
65         m_gamemods = flattenModTree(getModsInPath(m_gspec.gamemods_path));
66
67         // world mods
68         std::string worldmods_path = wspec.path + DIR_DELIM + "worldmods";
69         m_worldmods = flattenModTree(getModsInPath(worldmods_path));
70
71         // fill m_addontree with add-on mods
72         std::set<std::string> paths = m_gspec.addon_mods_paths;
73         for(std::set<std::string>::iterator it=paths.begin();
74                 it != paths.end(); ++it)
75         {
76                 std::map<std::string,ModSpec> mods = getModsInPath(*it);
77                 m_addontree.insert(mods.begin(), mods.end());
78         }
79
80         // expand modpacks
81         m_addonmods = flattenModTree(m_addontree);
82
83         // collect reverse dependencies
84         for(std::map<std::string, ModSpec>::iterator it = m_addonmods.begin();
85                 it != m_addonmods.end(); ++it)
86         {
87                 std::string modname = (*it).first;
88                 ModSpec mod = (*it).second;
89                 for(std::set<std::string>::iterator dep_it = mod.depends.begin();
90                         dep_it != mod.depends.end(); ++dep_it)
91                 {
92                         m_reverse_depends.insert(std::make_pair((*dep_it),modname));
93                 }
94         }
95
96         m_settings.readConfigFile((m_wspec.path + DIR_DELIM + "world.mt").c_str());
97         std::vector<std::string> names = m_settings.getNames();
98
99         // mod_names contains the names of mods mentioned in the world.mt file
100         std::set<std::string> mod_names;
101         for(std::vector<std::string>::iterator it = names.begin(); 
102                 it != names.end(); ++it)
103         {       
104                 std::string name = *it;  
105                 if (name.compare(0,9,"load_mod_")==0)
106                         mod_names.insert(name.substr(9));
107         }
108
109         // find new mods (installed but not mentioned in world.mt)
110         for(std::map<std::string, ModSpec>::iterator it = m_addonmods.begin();
111                 it != m_addonmods.end(); ++it)
112         {
113                 std::string modname = (*it).first;
114                 ModSpec mod = (*it).second;
115                 // a mod is new if it is not a modpack, and does not occur in
116                 // mod_names
117                 if(!mod.is_modpack &&
118                    mod_names.count(modname) == 0)
119                         m_settings.setBool("load_mod_"+modname, false);
120         }
121         // find missing mods (mentioned in world.mt, but not installed)
122         for(std::set<std::string>::iterator it = mod_names.begin();
123                 it != mod_names.end(); ++it)
124         {
125                 std::string modname = *it;
126                 if(m_addonmods.count(modname) == 0)
127                         m_settings.remove("load_mod_"+modname);
128         }
129         std::string worldmtfile = m_wspec.path+DIR_DELIM+"world.mt";
130         m_settings.updateConfigFile(worldmtfile.c_str());
131 }
132
133 void GUIConfigureWorld::drawMenu()
134 {
135         gui::IGUISkin* skin = Environment->getSkin();
136         if (!skin)
137                 return;
138         video::IVideoDriver* driver = Environment->getVideoDriver();
139         
140         video::SColor bgcolor(140,0,0,0);
141         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
142
143         gui::IGUIElement::draw();
144 }
145
146
147 void GUIConfigureWorld::regenerateGui(v2u32 screensize)
148 {
149
150         /*
151                 Remove stuff
152         */
153         removeChildren();
154         
155         /*
156                 Calculate new sizes and positions
157         */
158         core::rect<s32> rect(
159                         screensize.X/2 - 580/2,
160                         screensize.Y/2 - 300/2,
161                         screensize.X/2 + 580/2,
162                         screensize.Y/2 + 300/2
163         );
164         
165         DesiredRect = rect;
166         recalculateAbsolutePosition(false);
167
168         v2s32 size = rect.getSize();
169
170         v2s32 topleft = v2s32(10, 10);
171
172         /*
173                 Add stuff
174         */
175         changeCtype("");
176         {
177                 core::rect<s32> rect(0, 0, 200, 20);
178                 rect += topleft;
179                 //proper text is set below, when a mod is selected
180                 m_modname_text = Environment->addStaticText(L"Mod: N/A", rect, false, 
181                                                                                                         false, this, -1);
182         }
183         {
184                 core::rect<s32> rect(0, 0, 200, 20);
185                 rect += v2s32(0, 25) + topleft;
186                 wchar_t* text = wgettext("enabled");
187                 m_enabled_checkbox = 
188                         Environment->addCheckBox(false, rect, this, GUI_ID_ENABLED_CHECKBOX, 
189                                                                          text);
190                 delete[] text;
191                 m_enabled_checkbox->setVisible(false);
192         }
193         {
194                 core::rect<s32> rect(0, 0, 85, 30);
195                 rect = rect + v2s32(0, 25) + topleft;
196                 wchar_t* text = wgettext("Enable All");
197                 m_enableall = Environment->addButton(rect, this, GUI_ID_ENABLEALL,
198                                                                                          text);
199                 delete[] text;
200                 m_enableall->setVisible(false);
201         }
202         {
203                 core::rect<s32> rect(0, 0, 85, 30);
204                 rect = rect + v2s32(115, 25) + topleft;
205                 wchar_t* text = wgettext("Disable All");
206                 m_disableall = Environment->addButton(rect, this, GUI_ID_DISABLEALL, text );
207                 delete[] text;
208                 m_disableall->setVisible(false);
209         }
210         {
211                 core::rect<s32> rect(0, 0, 200, 20);
212                 rect += v2s32(0, 60) + topleft;
213                 wchar_t* text = wgettext("depends on:");
214                 Environment->addStaticText(text, rect, false, false, this, -1);
215                 delete[] text;
216         }
217         {
218                 core::rect<s32> rect(0, 0, 200, 85);
219                 rect += v2s32(0, 80) + topleft;
220                 m_dependencies_listbox = 
221                         Environment->addListBox(rect, this, GUI_ID_DEPENDS_LISTBOX, true);
222         }
223         {
224                 core::rect<s32> rect(0, 0, 200, 20);
225                 rect += v2s32(0, 175) + topleft;
226                 wchar_t* text = wgettext("is required by:");
227                 Environment->addStaticText( text, rect, false, false, this, -1);
228                 delete[] text;
229         }
230         {
231                 core::rect<s32> rect(0, 0, 200, 85);
232                 rect += v2s32(0, 195) + topleft;
233                 m_rdependencies_listbox = 
234                         Environment->addListBox(rect,this, GUI_ID_RDEPENDS_LISTBOX,true);
235         }
236         {
237                 core::rect<s32> rect(0, 0, 340, 250);
238                 rect += v2s32(220, 0) + topleft;
239                 m_treeview = Environment->addTreeView(rect, this,
240                                                                                           GUI_ID_MOD_TREEVIEW,true);
241                 gui::IGUITreeViewNode* node 
242                         = m_treeview->getRoot()->addChildBack(L"Add-Ons");
243                 buildTreeView(m_addontree, node);
244         }
245         {
246                 core::rect<s32> rect(0, 0, 120, 30);
247                 rect = rect + v2s32(330, 270) - topleft;
248                 wchar_t* text = wgettext("Cancel");
249                 Environment->addButton(rect, this, GUI_ID_CANCEL, text);
250                 delete[] text;
251         }
252         {
253                 core::rect<s32> rect(0, 0, 120, 30);
254                 rect = rect + v2s32(460, 270) - topleft;
255                 wchar_t* text = wgettext("Save");
256                 Environment->addButton(rect, this, GUI_ID_SAVE, text);
257                 delete[] text;
258         }
259         changeCtype("C");
260
261         // at start, none of the treeview nodes is selected, so we select
262         // the first element in the treeview of mods manually here.
263         if(m_treeview->getRoot()->hasChilds())
264         {
265                 m_treeview->getRoot()->getFirstChild()->setExpanded(true);
266                 m_treeview->getRoot()->getFirstChild()->setSelected(true);
267                 // Because a manual ->setSelected() doesn't cause an event, we
268                 // have to do this here:
269                 adjustSidebar();
270         }
271 }
272
273 bool GUIConfigureWorld::OnEvent(const SEvent& event)
274 {
275
276         gui::IGUITreeViewNode* selected_node = NULL;
277         if(m_treeview != NULL)
278                 selected_node = m_treeview->getSelected();
279
280         if(event.EventType==EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown)
281         {
282                 switch (event.KeyInput.Key) {
283                 case KEY_ESCAPE: {
284                         quitMenu();
285                         return true;
286                 }
287                 //      irrlicht's built-in TreeView gui has no keyboard control,
288                 //      so we do it here: up/down to select prev/next node,
289                 //      left/right to collapse/expand nodes, space to toggle
290                 //      enabled/disabled.
291                 case KEY_DOWN: {
292                         if(selected_node != NULL)
293                         {
294                                 gui::IGUITreeViewNode* node = selected_node->getNextVisible();
295                                 if(node != NULL)
296                                 {
297                                         node->setSelected(true);
298                                         adjustSidebar();
299                                 }
300                         }
301                         return true;
302                 }
303                 case KEY_UP: {
304                         if(selected_node != NULL)
305                         {
306                                 gui::IGUITreeViewNode* node = selected_node->getPrevSibling();
307                                 if(node!=NULL)
308                                 {
309                                         node->setSelected(true);
310                                         adjustSidebar();
311                                 }
312                                 else 
313                                 {
314                                         gui::IGUITreeViewNode* parent = selected_node->getParent();
315                                         if(selected_node == parent->getFirstChild() &&
316                                            parent != m_treeview->getRoot()) 
317                                         {
318                                                 parent->setSelected(true);
319                                                 adjustSidebar();
320                                         }
321                                 }
322                         }
323                         return true;
324                 }
325                 case KEY_RIGHT: {
326                         if(selected_node != NULL && selected_node->hasChilds())
327                                 selected_node->setExpanded(true);
328                         return true;
329                 }
330                 case KEY_LEFT: {
331                         if(selected_node != NULL && selected_node->hasChilds())
332                                 selected_node->setExpanded(false);
333                         return true;
334                 }
335                 case KEY_SPACE: {
336                         if(selected_node != NULL && !selected_node->hasChilds() && 
337                            selected_node->getText() != NULL)
338                         {
339                                 std::string modname = wide_to_narrow(selected_node->getText());
340                                 bool checked = m_enabled_checkbox->isChecked();
341                                 m_enabled_checkbox->setChecked(!checked);
342                                 setEnabled(modname,!checked);
343                         }
344                         return true;
345                 }
346                 default: {}
347                 }
348         }
349         if(event.EventType==EET_GUI_EVENT)
350         {
351                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
352                                 && isVisible())
353                 {
354                         if(!canTakeFocus(event.GUIEvent.Element))
355                         {
356                                 dstream<<"GUIConfigureWorld: Not allowing focus change."
357                                                 <<std::endl;
358                                 // Returning true disables focus change
359                                 return true;
360                         }
361                 }
362                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED){
363                         switch(event.GUIEvent.Caller->getID()){
364                         case GUI_ID_CANCEL: {
365                                 quitMenu();
366                                 return true;
367                         }
368                         case GUI_ID_SAVE: {
369                                 std::string worldmtfile = m_wspec.path+DIR_DELIM+"world.mt";
370                                 m_settings.updateConfigFile(worldmtfile.c_str());
371
372                                 // The trailing spaces are because there seems to be a
373                                 // bug in the text-size calculation. if the trailing
374                                 // spaces are removed from the message text, the
375                                 // message gets wrapped and parts of it are cut off:
376                                 wchar_t* text = wgettext("Configuration saved.  ");
377                                 GUIMessageMenu *menu = 
378                                         new GUIMessageMenu(Environment, Parent, -1, m_menumgr, 
379                                                                          text );
380                                 delete[] text;
381                                 menu->drop();
382
383                                 try
384                                 {
385                                         ModConfiguration modconf(m_wspec.path);
386                                         if(!modconf.isConsistent())
387                                         {
388                                                 wchar_t* text = wgettext("Warning: Configuration not consistent.  ");
389                                                 GUIMessageMenu *menu =
390                                                         new GUIMessageMenu(Environment, Parent, -1, m_menumgr, 
391                                                                                  text );
392                                                 delete[] text;
393                                                 menu->drop();
394                                         }
395                                 }
396                                 catch(ModError &err)
397                                 {
398                                         errorstream<<err.what()<<std::endl;
399                                         std::wstring text = narrow_to_wide(err.what()) + wgettext("\nCheck debug.txt for details.");
400                                         GUIMessageMenu *menu =
401                                                 new GUIMessageMenu(Environment, Parent, -1, m_menumgr, 
402                                                                          text );
403                                         menu->drop();
404                                 }
405
406                                 quitMenu();     
407                                 return true;
408                         }
409                         case GUI_ID_ENABLEALL: {
410                                 if(selected_node != NULL && selected_node->getParent() == m_treeview->getRoot())
411                                 {  
412                                         enableAllMods(m_addonmods,true);
413                                 } 
414                                 else if(selected_node != NULL && selected_node->getText() != NULL)
415                                 {  
416                                         std::string modname = wide_to_narrow(selected_node->getText());
417                                         std::map<std::string, ModSpec>::iterator mod_it = m_addonmods.find(modname);
418                                         if(mod_it != m_addonmods.end())
419                                                 enableAllMods(mod_it->second.modpack_content,true);
420                                 }
421                                 return true;
422                         }
423                         case GUI_ID_DISABLEALL: {
424                                 if(selected_node != NULL && selected_node->getParent() == m_treeview->getRoot())
425                                 {
426                                         enableAllMods(m_addonmods,false);
427                                 } 
428                                 if(selected_node != NULL && selected_node->getText() != NULL)
429                                 {
430                                         std::string modname = wide_to_narrow(selected_node->getText());
431                                         std::map<std::string, ModSpec>::iterator mod_it = m_addonmods.find(modname);
432                                         if(mod_it != m_addonmods.end())
433                                                 enableAllMods(mod_it->second.modpack_content,false);
434                                 }
435                                 return true;
436                         }
437                         }
438                 }       
439                 if(event.GUIEvent.EventType==gui::EGET_CHECKBOX_CHANGED &&
440                         event.GUIEvent.Caller->getID() == GUI_ID_ENABLED_CHECKBOX)
441                 {
442                         if(selected_node != NULL && !selected_node->hasChilds() && 
443                            selected_node->getText() != NULL)
444                         {
445                                 std::string modname = wide_to_narrow(selected_node->getText());
446                                 setEnabled(modname, m_enabled_checkbox->isChecked());
447                         }
448                         return true;
449                 }
450                 if(event.GUIEvent.EventType==gui::EGET_TREEVIEW_NODE_SELECT &&
451                    event.GUIEvent.Caller->getID() == GUI_ID_MOD_TREEVIEW)
452                 {
453                         selecting_dep = -1;
454                         selecting_rdep = -1;
455                         adjustSidebar();
456                         return true;
457                 }
458                 if(event.GUIEvent.EventType==gui::EGET_LISTBOX_CHANGED &&
459                    event.GUIEvent.Caller->getID() == GUI_ID_DEPENDS_LISTBOX)
460                 {
461                         selecting_dep = m_dependencies_listbox->getSelected();
462                         selecting_rdep = -1;
463                         return true;
464                 }
465                 if(event.GUIEvent.EventType==gui::EGET_LISTBOX_CHANGED &&
466                    event.GUIEvent.Caller->getID() == GUI_ID_RDEPENDS_LISTBOX)
467                 {
468                         selecting_dep = -1;
469                         selecting_rdep = m_rdependencies_listbox->getSelected();
470                         return true;
471                 }
472
473                 //double click in a dependency listbox: find corresponding
474                 //treeviewnode and select it:
475                 if(event.GUIEvent.EventType==gui::EGET_LISTBOX_SELECTED_AGAIN)
476                 {
477                         gui::IGUIListBox* box = NULL;
478                         if(event.GUIEvent.Caller->getID() == GUI_ID_DEPENDS_LISTBOX)
479                         {
480                                 box = m_dependencies_listbox;
481                                 if(box->getSelected() != selecting_dep)
482                                         return true;
483                         }
484                         if(event.GUIEvent.Caller->getID() == GUI_ID_RDEPENDS_LISTBOX)
485                         {
486                                 box = m_rdependencies_listbox;
487                                 if(box->getSelected() != selecting_rdep)
488                                         return true;
489                         }
490                         if(box != NULL && box->getSelected() != -1 &&
491                            box->getListItem(box->getSelected()) != NULL)
492                         {
493                                 std::string modname = 
494                                         wide_to_narrow(box->getListItem(box->getSelected()));
495                                 std::map<std::string, gui::IGUITreeViewNode*>::iterator it =
496                                         m_nodes.find(modname);
497                                 if(it != m_nodes.end())
498                                 {
499                                         // select node and make sure node is visible by
500                                         // expanding all parents
501                                         gui::IGUITreeViewNode* node = (*it).second;
502                                         node->setSelected(true);
503                                         while(!node->isVisible() && 
504                                                   node->getParent() != m_treeview->getRoot())
505                                         {
506                                                 node = node->getParent();
507                                                 node->setExpanded(true);
508                                         }
509                                         adjustSidebar();
510                                 }
511                         }
512                         return true;
513                 }
514         }
515
516         return Parent ? Parent->OnEvent(event) : false;
517 }
518
519 void GUIConfigureWorld::buildTreeView(std::map<std::string, ModSpec> mods, 
520                                                                           gui::IGUITreeViewNode* node)
521 {
522         for(std::map<std::string,ModSpec>::iterator it = mods.begin();
523                 it != mods.end(); ++it)    
524         {
525                 std::string modname = (*it).first;
526                 ModSpec mod = (*it).second;
527                 gui::IGUITreeViewNode* new_node = 
528                         node->addChildBack(narrow_to_wide(modname).c_str());
529                 m_nodes.insert(std::make_pair(modname, new_node));
530                 if(mod.is_modpack)
531                         buildTreeView(mod.modpack_content, new_node);
532                 else
533                 {
534                         // set icon for node: x for disabled mods, checkmark for enabled mods
535                         bool mod_enabled = false;
536                         if(m_settings.exists("load_mod_"+modname))
537                                 mod_enabled = m_settings.getBool("load_mod_"+modname);
538                         if(mod_enabled)
539                                 new_node->setIcon(CHECKMARK_STR);
540                         else
541                                 new_node->setIcon(CROSS_STR);
542                 }
543         }
544 }
545
546
547 void GUIConfigureWorld::adjustSidebar()
548 {
549         gui::IGUITreeViewNode* node = m_treeview->getSelected();
550         std::wstring modname_w;
551         if(node->getText() != NULL)
552                 modname_w = node->getText();
553         else 
554                 modname_w = L"N/A";
555         std::string modname = wide_to_narrow(modname_w);
556
557         ModSpec mspec;
558         std::map<std::string, ModSpec>::iterator it = m_addonmods.find(modname);
559         if(it != m_addonmods.end())
560                 mspec = it->second;
561
562         m_dependencies_listbox->clear();
563         m_rdependencies_listbox->clear();
564
565         // if no mods installed, there is nothing to enable/disable, so we
566         // don't show buttons or checkbox on the sidebar
567         if(node->getParent() == m_treeview->getRoot() && !node->hasChilds())
568         {
569                 m_disableall->setVisible(false);
570                 m_enableall->setVisible(false);
571                 m_enabled_checkbox->setVisible(false);
572                 return;
573         } 
574         
575     // a modpack is not enabled/disabled by itself, only its cotnents
576     // are. so we show show enable/disable all buttons, but hide the
577     // checkbox
578         if(node->getParent() == m_treeview->getRoot() ||
579            mspec.is_modpack)
580         {
581                 m_enabled_checkbox->setVisible(false);
582                 m_disableall->setVisible(true);
583                 m_enableall->setVisible(true);
584                 m_modname_text->setText((L"Modpack: "+modname_w).c_str());
585                 return;
586         }       
587
588         // for a normal mod, we hide the enable/disable all buttons, but show the checkbox.
589         m_disableall->setVisible(false);
590         m_enableall->setVisible(false);
591         m_enabled_checkbox->setVisible(true);
592         m_modname_text->setText((L"Mod: "+modname_w).c_str());
593
594         // the mod is enabled unless it is disabled in the world.mt settings. 
595         bool mod_enabled = true;
596         if(m_settings.exists("load_mod_"+modname))
597                 mod_enabled = m_settings.getBool("load_mod_"+modname);
598         m_enabled_checkbox->setChecked(mod_enabled);
599
600         for(std::set<std::string>::iterator it=mspec.depends.begin();
601                 it != mspec.depends.end(); ++it)
602         {
603                 // check if it is an add-on mod or a game/world mod. We only
604                 // want to show add-ons
605                 std::string dependency = (*it);
606                 if(m_gamemods.count(dependency) > 0)
607                         dependency += " (" + m_gspec.id + ")";
608                 else if(m_worldmods.count(dependency) > 0)
609                         dependency += " (" + m_wspec.name + ")";
610                 else if(m_addonmods.count(dependency) == 0)
611                         dependency += " (missing)";
612                 m_dependencies_listbox->addItem(narrow_to_wide(dependency).c_str());
613         }
614
615         // reverse dependencies of this mod:
616         std::pair< std::multimap<std::string, std::string>::iterator, 
617                         std::multimap<std::string, std::string>::iterator > rdep = 
618                 m_reverse_depends.equal_range(modname);
619         for(std::multimap<std::string,std::string>::iterator it = rdep.first;
620                 it != rdep.second; ++it)
621         {
622                 // check if it is an add-on mod or a game/world mod. We only
623                 // want to show add-ons
624                 std::string rdependency = (*it).second;
625                 if(m_addonmods.count(rdependency) > 0)
626                         m_rdependencies_listbox->addItem(narrow_to_wide(rdependency).c_str());
627         }
628 }
629
630 void GUIConfigureWorld::enableAllMods(std::map<std::string, ModSpec> mods,bool enable)
631 {
632         for(std::map<std::string, ModSpec>::iterator it = mods.begin();
633                 it != mods.end(); ++it)
634         {
635                 ModSpec mod = (*it).second;
636                 if(mod.is_modpack) 
637                         // a modpack, recursively enable all mods in it
638                         enableAllMods(mod.modpack_content,enable);
639                 else // not a modpack
640                         setEnabled(mod.name, enable);
641
642         }
643 }
644
645 void GUIConfigureWorld::enableMod(std::string modname)
646 {
647         std::map<std::string, ModSpec>::iterator mod_it = m_addonmods.find(modname);
648         if(mod_it == m_addonmods.end()){
649                 errorstream << "enableMod() called with invalid mod name \"" << modname << "\"" << std::endl;
650                 return;
651         }
652         ModSpec mspec = mod_it->second;
653         m_settings.setBool("load_mod_"+modname,true);
654         std::map<std::string,gui::IGUITreeViewNode*>::iterator it = 
655                 m_nodes.find(modname);
656         if(it != m_nodes.end())
657                 (*it).second->setIcon(CHECKMARK_STR);
658         //also enable all dependencies
659         for(std::set<std::string>::iterator it=mspec.depends.begin();
660                 it != mspec.depends.end(); ++it)
661         {
662                 std::string dependency = *it;
663                 // only enable it if it is an add-on mod
664                 if(m_addonmods.count(dependency) > 0)
665                         enableMod(dependency);
666         }
667 }
668
669 void GUIConfigureWorld::disableMod(std::string modname)
670 {
671         std::map<std::string, ModSpec>::iterator mod_it = m_addonmods.find(modname);
672         if(mod_it == m_addonmods.end()){
673                 errorstream << "disableMod() called with invalid mod name \"" << modname << "\"" << std::endl;
674                 return;
675         }
676
677         m_settings.setBool("load_mod_"+modname,false);
678         std::map<std::string,gui::IGUITreeViewNode*>::iterator it = 
679                 m_nodes.find(modname);
680         if(it != m_nodes.end())
681                 (*it).second->setIcon(CROSS_STR);
682         //also disable all mods that depend on this one
683         std::pair<std::multimap<std::string, std::string>::iterator, 
684                           std::multimap<std::string, std::string>::iterator > rdep = 
685                 m_reverse_depends.equal_range(modname);
686         for(std::multimap<std::string,std::string>::iterator it = rdep.first;
687                 it != rdep.second; ++it)
688         {
689                 std::string rdependency = (*it).second;
690                 // only disable it if it is an add-on mod
691                 if(m_addonmods.count(rdependency) > 0)
692                         disableMod(rdependency);
693         }       
694 }
695