Removed 'not touching files if ok' since it's not working anyway.
[UnsignedByte.git] / src / Core / Editors / EditorGrantGroup.cpp
blob400a6f49d864a1c0f0c044f78ca00a8d515f4e96
1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 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 General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "EditorGrantGroup.h"
22 #include "EditorOLC.h"
23 #include "UBSocket.h"
24 #include "StringUtilities.h"
25 #include "TableImpls.h"
26 #include "Array.h"
27 #include "Account.h"
28 #include "GrantGroup.h"
29 #include "GrantGroupManager.h"
30 #include "Managers.h"
32 using mud::GrantGroup;
34 typedef EditorGrantGroup E;
35 typedef CommandInfoObject<E> O;
36 typedef CommandBinding<E> B;
38 // name function need: object lock
39 static O editName( "Name", &E::editName, true, true);
40 static O editImplication("Description", &E::editImplication, true, true);
41 static O showGrantGroup("Show", &E::showGrantGroup, true, false);
42 static O saveGrantGroup("Save", &E::saveGrantGroup, true, false);
44 static const B commands[] = {
45 B("implication", editImplication),
46 B("name", editName),
47 B("save", saveGrantGroup),
48 B("show", showGrantGroup),
51 EditorGrantGroup::EditorGrantGroup(UBSocket* sock) :
52 OLCEditor(sock),
53 m_commands(commands, array_size(commands)),
54 m_grantgroup()
56 listCommands(Global::Get()->EmptyString);
59 EditorGrantGroup::~EditorGrantGroup(void)
64 std::string EditorGrantGroup::lookup(const std::string& action)
66 std::string name = OLCEditor::lookup(action);
67 if(name.size() != 0)
68 return name;
70 const GrantGroupCommand* act = (GrantGroupCommand*)m_commands.getObject(action);
71 if(act)
72 return act->getName();
74 return Global::Get()->EmptyString;
77 void EditorGrantGroup::dispatch(const std::string& action, const std::string& argument)
79 const GrantGroupCommand* act = (GrantGroupCommand*)m_commands.getObject(action);
81 if(!act)
83 OLCEditor::dispatch(action, argument);
84 return;
87 if(!m_grantgroup)
89 m_sock->Send("You need to be editing a grantgroup first.\n");
90 m_sock->Send("(Use the 'edit' command.)\n");
91 return;
94 if(act->needLock())
96 try {
97 m_grantgroup->Lock();
98 } catch(SavableLocked& e) {
99 m_sock->Send("The grantgroup you are currently editing is locked (being edited by someone else), so you cannot edit it right now.\n");
100 m_sock->Send("Please try again later.\n");
101 return;
105 act->Run(this, argument);
106 return;
109 SavablePtr EditorGrantGroup::getEditing()
111 return m_grantgroup;
114 TableImplPtr EditorGrantGroup::getTable()
116 return db::TableImpls::Get()->GRANTGROUPS;
119 KeysPtr EditorGrantGroup::addNew()
121 return mud::Managers::Get()->GrantGroup->Add();
124 std::vector<std::string> EditorGrantGroup::getList()
126 return mud::Managers::Get()->GrantGroup->List();
129 void EditorGrantGroup::setEditing(KeysPtr keys)
131 if(!keys->size())
133 m_grantgroup.reset();
134 return;
137 m_grantgroup = mud::Managers::Get()->GrantGroup->GetByKey(keys->first()->getIntegerValue());
138 return;
141 std::vector<std::string> EditorGrantGroup::getCommands()
143 return m_commands.getCommandsVector();
146 void EditorGrantGroup::editName(const std::string& argument)
148 if(argument.size() == 0)
150 m_sock->Send("GrantGroup name can't be zero length!\n");
151 return;
154 m_sock->Sendf("GrantGroup name changed from '%s' to '%s'.\n", m_grantgroup->getName().c_str(), argument.c_str());
155 m_grantgroup->setName(argument);
156 return;
159 void EditorGrantGroup::editImplication(const std::string& argument)
161 if(argument.size() == 0)
163 m_sock->Send("Please specify the implicated grantgroup!\n");
164 return;
167 int implication = atoi(argument.c_str());
168 if(implication <= 0)
170 m_sock->Sendf("Please specify an implication > 0!\n");
171 return;
174 m_sock->Sendf("GrantGroup implication changed from '%d' to '%d'.\n", m_grantgroup->getImplication(), implication);
175 m_grantgroup->setImplication(implication);
178 void EditorGrantGroup::showGrantGroup(const std::string& argument)
180 m_sock->Send(m_grantgroup->toString());
181 return;
184 void EditorGrantGroup::saveGrantGroup(const std::string& argument)
186 m_sock->Sendf("Saving grantgroup '%s'.\n", m_grantgroup->getName().c_str());
187 m_grantgroup->Save();
188 m_sock->Send("Saved.\n");
189 return;