Most important one got forgotten!
[UnsignedByte.git] / src / Core / Editors / EditorDetail.cpp
blob7c8a9f1eba47b1b98ab7db90584fb99a42175aaf
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 <boost/spirit/core.hpp>
23 #include "Account.h"
24 #include "Area.h"
25 #include "AreaManager.h"
26 #include "Array.h"
27 #include "Assert.h"
28 #include "Chunk.h"
29 #include "ChunkManager.h"
30 #include "Detail.h"
31 #include "DetailManager.h"
32 #include "EditorBool.h"
33 #include "EditorDetail.h"
34 #include "EditorString.h"
35 #include "Managers.h"
36 #include "Room.h"
37 #include "RoomManager.h"
38 #include "SavableHeaders.h"
39 #include "StringUtilities.h"
40 #include "TableImpls.h"
41 #include "UBSocket.h"
43 typedef EditorDetail E;
44 typedef CommandInfoObject<E> O;
45 typedef CommandBinding<E> B;
47 using namespace boost::spirit;
49 // name function need: object lock
50 static O editKey( "Key", &E::editKey, true, true);
51 static O editDescription("Description", &E::editDescription, true, true);
52 static O showDetail("Show", &E::showDetail, true, false);
53 static O saveDetail("Save", &E::saveDetail, true, true);
54 static O clearParents("ClearParents", &E::clearParents);
55 static O setParent( "SetParent",&E::setParent);
57 static const B commands[] = {
58 B("all", clearParents),
59 B("description", editDescription),
60 B("filter", setParent),
61 B("name", editKey),
62 B("save", saveDetail),
63 B("show", showDetail),
66 EditorDetail::EditorDetail(UBSocket* sock) :
67 OLCEditor(sock),
68 m_commands(commands, array_size(commands)),
69 m_detail(),
70 m_target(M_NONE)
72 listCommands(Global::Get()->EmptyString);
75 EditorDetail::EditorDetail(UBSocket* sock, mud::DetailPtr detail) :
76 OLCEditor(sock),
77 m_commands(commands, array_size(commands)),
78 m_detail(detail),
79 m_target(M_NONE)
84 EditorDetail::EditorDetail(UBSocket* sock, mud::AreaPtr parentArea) :
85 OLCEditor(sock),
86 m_commands(commands, array_size(commands)),
87 m_detail(),
88 m_parentArea(parentArea),
89 m_target(M_NONE)
91 listCommands(Global::Get()->EmptyString);
92 m_sock->Sendf("Only Details that belong to area '%d' are shown, to clear this restriction type 'all'.\n", parentArea->getID());
95 EditorDetail::EditorDetail(UBSocket* sock, mud::RoomPtr parentRoom) :
96 OLCEditor(sock),
97 m_commands(commands, array_size(commands)),
98 m_detail(),
99 m_parentRoom(parentRoom),
100 m_target(M_NONE)
102 listCommands(Global::Get()->EmptyString);
103 m_sock->Sendf("Only Details that belong to room '%d' are shown, to clear this restriction type 'all'.\n", parentRoom->getID());
106 EditorDetail::EditorDetail(UBSocket* sock, mud::ChunkPtr parentChunk) :
107 OLCEditor(sock),
108 m_commands(commands, array_size(commands)),
109 m_detail(),
110 m_parentChunk(parentChunk),
111 m_target(M_NONE)
113 listCommands(Global::Get()->EmptyString);
114 m_sock->Sendf("Only Details that belong to chunk '%d' are shown, to clear this restriction type 'all'.\n", parentChunk->getID());
117 EditorDetail::~EditorDetail(void)
122 void EditorDetail::OnFocus()
124 switch(m_target)
126 case M_NONE:
127 return;
129 case M_DESCRIPTION:
130 m_detail->setDescription(m_value);
131 break;
134 m_target = M_NONE;
137 std::string EditorDetail::lookup(const std::string& action)
139 std::string name = OLCEditor::lookup(action);
140 if(name.size() != 0)
141 return name;
143 const DetailCommand* act = (DetailCommand*)m_commands.getObject(action);
144 if(act)
145 return act->getName();
147 return Global::Get()->EmptyString;
150 void EditorDetail::dispatch(const std::string& action, const std::string& argument)
152 const DetailCommand* act = (DetailCommand*)m_commands.getObject(action);
154 if(!act)
156 OLCEditor::dispatch(action, argument);
157 return;
160 if(!m_detail && act->needObject())
162 m_sock->Send("You need to be editing a detail first.\n");
163 m_sock->Send("(Use the 'edit' command.)\n");
164 return;
167 if(act->needLock())
169 try {
170 m_detail->Lock();
171 } catch(SavableLocked& e) {
172 m_sock->Send("The detail you are currently editing is locked (being edited by someone else), so you cannot edit it right now.\n");
173 m_sock->Send("Please try again later.\n");
174 return;
178 act->Run(this, argument);
179 return;
182 SavablePtr EditorDetail::getEditing()
184 return m_detail;
187 TableImplPtr EditorDetail::getTable()
189 return db::TableImpls::Get()->DETAILS;
192 KeysPtr EditorDetail::addNew()
194 return mud::Managers::Get()->Detail->Add();
197 std::vector<std::string> EditorDetail::getList()
199 if(m_parentArea)
200 return mud::Managers::Get()->Detail->List(m_parentArea);
202 if(m_parentRoom)
203 return mud::Managers::Get()->Detail->List(m_parentRoom);
205 if(m_parentChunk)
206 return mud::Managers::Get()->Detail->List(m_parentChunk);
208 return mud::Managers::Get()->Detail->List();
211 void EditorDetail::setEditing(KeysPtr keys)
213 if(!keys->size())
215 m_detail.reset();
216 return;
219 m_detail = mud::Managers::Get()->Detail->GetByKey(keys->first()->getIntegerValue());
220 return;
223 std::vector<std::string> EditorDetail::getCommands()
225 return m_commands.getCommandsVector();
228 void EditorDetail::editKey(const std::string& argument)
230 if(argument.size() == 0)
232 m_sock->Send("Detail key can't be zero length!\n");
233 return;
236 m_sock->Sendf("Detail key changed from '%s' to '%s'.\n", m_detail->getKey().c_str(), argument.c_str());
237 m_detail->setKey(argument);
238 return;
241 void EditorDetail::editDescription(const std::string& argument)
243 if(argument.size() == 0)
245 m_sock->Send("No argument, dropping you into the string editor!\n");
246 m_sock->SetEditor(new EditorString(m_sock, m_value));
247 m_target = M_DESCRIPTION;
248 return;
251 m_sock->Sendf("Detail description changed from '%s' to '%s'.\n", m_detail->getDescription().c_str(), argument.c_str());
252 m_detail->setDescription(argument);
253 return;
256 void EditorDetail::showDetail(const std::string& argument)
258 std::vector<std::string> result;
259 std::string key = "Key: '";
260 key.append(m_detail->getKey());
261 key.append("'.");
262 result.push_back(key);
264 std::string description = "Description: '";
265 description.append(m_detail->getDescription());
266 description.append("'.");
267 result.push_back(description);
269 m_sock->Send(String::Get()->box(result, "Detail"));
272 void EditorDetail::saveDetail(const std::string& argument)
274 m_sock->Sendf("Saving detail '%s'.\n", m_detail->getKey().c_str());
275 if(argument.size())
276 m_detail->Save(m_sock->GetAccount()->getID(), argument);
277 else
278 m_detail->Save(m_sock->GetAccount()->getID(), Global::Get()->EmptyString);
279 m_sock->Send("Saved.\n");
280 return;
283 void EditorDetail::clearParents(const std::string& argument)
285 m_sock->Send("Ok.\n");
286 m_parentArea.reset();
287 m_parentRoom.reset();
288 m_parentChunk.reset();
289 return;
292 void EditorDetail::setParent(const std::string& argument)
294 if(!argument.size())
296 m_sock->Send("Please provide an area, room, or chunk to filter on.\n");
297 return;
300 value_type id = 0;
301 bool area = false;
302 bool chunk = false;
303 bool room = false;
305 bool good = parse(argument.c_str(),
306 // Begin grammar
308 (str_p("area")[assign_a(area, true)] |
309 str_p("chunk")[assign_a(chunk, true)] |
310 str_p("room")[assign_a(room, true)])
312 >> int_p[assign_a(id)]
315 // End grammar
316 space_p).full;
318 if(!good)
320 m_sock->Send("Please specify what area, room, or chunk to filter on in the following format:\n");
321 m_sock->Send("area/room/chunk id\n");
322 return;
325 Assert(area || chunk || room);
327 m_parentArea.reset();
328 m_parentRoom.reset();
329 m_parentChunk.reset();
332 if(area)
334 try {
335 mud::AreaPtr area = mud::Managers::Get()->Area->GetByKey(id);
336 m_parentArea = area;
337 m_sock->Send("Ok.\n");
338 m_sock->Sendf("Only Details that belong to area '%d' are shown, to clear this restriction type 'all'.\n", m_parentArea->getID());
339 } catch(RowNotFoundException& e) {
340 m_sock->Sendf("'%s' is not an area.\n", argument.c_str());
344 if(room)
346 try {
347 mud::RoomPtr room = mud::Managers::Get()->Room->GetByKey(id);
348 m_parentRoom = room;
349 m_sock->Send("Ok.\n");
350 m_sock->Sendf("Only Details that belong to room '%d' are shown, to clear this restriction type 'all'.\n", m_parentRoom->getID());
351 } catch(RowNotFoundException& e) {
352 m_sock->Sendf("'%s' is not a room.\n", argument.c_str());
356 if(chunk)
358 try {
359 mud::ChunkPtr chunk = mud::Managers::Get()->Chunk->GetByKey(id);
360 m_parentChunk = chunk;
361 m_sock->Send("Ok.\n");
362 m_sock->Sendf("Only Details that belong to chunk '%d' are shown, to clear this restriction type 'all'.\n", m_parentChunk->getID());
363 } catch(RowNotFoundException& e) {
364 m_sock->Sendf("'%d' is not a chunk.\n", id);