Removed "Exception.h" references and replaced showShort with toString calls.
[UnsignedByte.git] / src / Core / EditorDetail.cpp
blob25286163cd8115128df959e59a7c4860976fc61e
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 "SavableHeaders.h"
23 #include "EditorDetail.h"
24 #include "EditorString.h"
25 #include "EditorBool.h"
27 #include "Array.h"
28 #include "Assert.h"
29 #include "StringUtilities.h"
30 #include "TableImpls.h"
31 #include "UBSocket.h"
33 #include "Account.h"
34 #include "Detail.h"
35 #include "DetailManager.h"
36 #include "Area.h"
37 #include "AreaManager.h"
38 #include "Room.h"
39 #include "RoomManager.h"
40 #include "Chunk.h"
41 #include "ChunkManager.h"
43 #include <boost/spirit/core.hpp>
45 typedef EditorDetail E;
46 typedef CommandObject<E> O;
47 typedef CommandBinding<E> B;
49 using namespace boost::spirit;
51 static O editKey("Key", &E::editKey);
52 static O editDescription("Description", &E::editDescription);
53 static O showDetail("Show", &E::showDetail);
54 static O saveDetail("Save", &E::saveDetail);
55 static O quitEditor("Quit", &E::quitEditor);
56 static O clearParents("ClearParents", &E::clearParents);
57 static O setParent("SetParent", &E::setParent);
59 static const B commands[] = {
60 B("all", clearParents),
61 B("description", editDescription),
62 B("filter", setParent),
63 B("name", editKey),
64 B("quit", quitEditor),
65 B("save", saveDetail),
66 B("show", showDetail),
69 EditorDetail::EditorDetail(UBSocket* sock) :
70 OLCEditor(sock),
71 m_commands(commands, array_size(commands)),
72 m_detail(),
73 m_target(M_NONE)
75 listCommands(Global::Get()->EmptyString);
78 EditorDetail::EditorDetail(UBSocket* sock, mud::DetailPtr detail) :
79 OLCEditor(sock),
80 m_commands(commands, array_size(commands)),
81 m_detail(detail),
82 m_target(M_NONE)
87 EditorDetail::EditorDetail(UBSocket* sock, mud::AreaPtr parentArea) :
88 OLCEditor(sock),
89 m_commands(commands, array_size(commands)),
90 m_detail(),
91 m_parentArea(parentArea),
92 m_target(M_NONE)
94 listCommands(Global::Get()->EmptyString);
95 m_sock->Sendf("Only Details that belong to area '%d' are shown, to clear this restriction type 'all'.\n", parentArea->getID());
98 EditorDetail::EditorDetail(UBSocket* sock, mud::RoomPtr parentRoom) :
99 OLCEditor(sock),
100 m_commands(commands, array_size(commands)),
101 m_detail(),
102 m_parentRoom(parentRoom),
103 m_target(M_NONE)
105 listCommands(Global::Get()->EmptyString);
106 m_sock->Sendf("Only Details that belong to room '%d' are shown, to clear this restriction type 'all'.\n", parentRoom->getID());
109 EditorDetail::EditorDetail(UBSocket* sock, mud::ChunkPtr parentChunk) :
110 OLCEditor(sock),
111 m_commands(commands, array_size(commands)),
112 m_detail(),
113 m_parentChunk(parentChunk),
114 m_target(M_NONE)
116 listCommands(Global::Get()->EmptyString);
117 m_sock->Sendf("Only Details that belong to chunk '%d' are shown, to clear this restriction type 'all'.\n", parentChunk->getID());
120 EditorDetail::~EditorDetail(void)
125 void EditorDetail::OnFocus()
127 switch(m_target)
129 case M_NONE:
130 return;
132 case M_DESCRIPTION:
133 m_detail->setDescription(m_value);
134 break;
137 m_target = M_NONE;
140 std::string EditorDetail::lookup(const std::string& action)
142 const DetailCommand* act = m_commands.getObject(action);
143 if(act)
144 return act->getName();
146 return Global::Get()->EmptyString;
149 void EditorDetail::dispatch(const std::string& action, const std::string& argument)
151 const DetailCommand* act = m_commands.getObject(action);
153 Assert(act);
154 act->Run(this, argument);
157 SavablePtr EditorDetail::getEditing()
159 return m_detail;
162 TableImplPtr EditorDetail::getTable()
164 return db::TableImpls::Get()->DETAILS;
167 KeysPtr EditorDetail::addNew()
169 return mud::DetailManager::Get()->Add();
172 std::vector<std::string> EditorDetail::getList()
174 if(m_parentArea)
175 return mud::DetailManager::Get()->List(m_parentArea);
177 if(m_parentRoom)
178 return mud::DetailManager::Get()->List(m_parentRoom);
180 if(m_parentChunk)
181 return mud::DetailManager::Get()->List(m_parentChunk);
183 return mud::DetailManager::Get()->List();
186 void EditorDetail::setEditing(KeysPtr keys)
188 if(!keys->size())
190 m_detail.reset();
191 return;
194 m_detail = mud::DetailManager::Get()->GetByKey(keys->first()->getIntegerValue());
195 return;
198 std::vector<std::string> EditorDetail::getCommands()
200 return m_commands.getCommandsVector();
203 void EditorDetail::editKey(const std::string& argument)
205 if(argument.size() == 0)
207 m_sock->Send("Detail key can't be zero length!\n");
208 return;
211 m_sock->Sendf("Detail key changed from '%s' to '%s'.\n", m_detail->getKey().c_str(), argument.c_str());
212 m_detail->setKey(argument);
213 return;
216 void EditorDetail::editDescription(const std::string& argument)
218 if(argument.size() == 0)
220 m_sock->Send("No argument, dropping you into the string editor!\n");
221 m_sock->SetEditor(new EditorString(m_sock, m_value));
222 m_target = M_DESCRIPTION;
223 return;
226 m_sock->Sendf("Detail description changed from '%s' to '%s'.\n", m_detail->getDescription().c_str(), argument.c_str());
227 m_detail->setDescription(argument);
228 return;
231 void EditorDetail::showDetail(const std::string& argument)
233 std::vector<std::string> result;
234 std::string key = "Key: '";
235 key.append(m_detail->getKey());
236 key.append("'.");
237 result.push_back(key);
239 std::string description = "Description: '";
240 description.append(m_detail->getDescription());
241 description.append("'.");
242 result.push_back(description);
244 m_sock->Send(String::Get()->box(result, "Detail"));
247 void EditorDetail::saveDetail(const std::string& argument)
249 m_sock->Sendf("Saving detail '%s'.\n", m_detail->getKey().c_str());
250 if(argument.size())
251 m_detail->Save(m_sock->GetAccount()->getID(), argument);
252 else
253 m_detail->Save(m_sock->GetAccount()->getID(), Global::Get()->EmptyString);
254 m_sock->Send("Saved.\n");
255 return;
258 void EditorDetail::clearParents(const std::string& argument)
260 m_sock->Send("Ok.\n");
261 m_parentArea.reset();
262 m_parentRoom.reset();
263 m_parentChunk.reset();
264 return;
267 void EditorDetail::setParent(const std::string& argument)
269 if(!argument.size())
271 m_sock->Send("Please provide an area, room, or chunk to filter on.\n");
272 return;
275 value_type id = 0;
276 bool area = false;
277 bool chunk = false;
278 bool room = false;
280 bool good = parse(argument.c_str(),
281 // Begin grammar
283 (str_p("area")[assign_a(area, true)] |
284 str_p("chunk")[assign_a(chunk, true)] |
285 str_p("room")[assign_a(room, true)])
287 >> int_p[assign_a(id)]
290 // End grammar
291 space_p).full;
293 if(!good)
295 m_sock->Send("Please specify what area, room, or chunk to filter on in the following format:\n");
296 m_sock->Send("area/room/chunk id\n");
297 return;
300 Assert(area || chunk || room);
302 if(area)
304 try {
305 mud::AreaPtr area = mud::AreaManager::Get()->GetByKey(id);
306 m_parentArea = area;
307 m_sock->Send("Ok.\n");
308 m_sock->Sendf("Only Details that belong to area '%d' are shown, to clear this restriction type 'all'.\n", m_parentArea->getID());
309 } catch(RowNotFoundException& e) {
310 m_sock->Sendf("'%s' is not an area.\n", argument.c_str());
314 if(room)
316 try {
317 mud::RoomPtr room = mud::RoomManager::Get()->GetByKey(id);
318 m_parentRoom = room;
319 m_sock->Send("Ok.\n");
320 m_sock->Sendf("Only Details that belong to room '%d' are shown, to clear this restriction type 'all'.\n", m_parentRoom->getID());
321 } catch(RowNotFoundException& e) {
322 m_sock->Sendf("'%s' is not a room.\n", argument.c_str());
326 if(chunk)
328 try {
329 mud::ChunkPtr chunk = mud::ChunkManager::Get()->GetByKey(id);
330 m_parentChunk = chunk;
331 m_sock->Send("Ok.\n");
332 m_sock->Sendf("Only Details that belong to chunk '%d' are shown, to clear this restriction type 'all'.\n", m_parentChunk->getID());
333 } catch(RowNotFoundException& e) {
334 m_sock->Sendf("'%d' is not a chunk.\n", id);