Well don't that own? Sorted all the #include's.
[UnsignedByte.git] / src / Core / Editors / OLCEditor.cpp
bloba13c8a25d5543c594c89806111545ab51b8535dd
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 "Account.h"
22 #include "Array.h"
23 #include "Assert.h"
24 #include "Command.h"
25 #include "DatabaseMgr.h"
26 #include "EditorOLC.h"
27 #include "OLCEditor.h"
28 #include "Permission.h"
29 #include "Savable.h"
30 #include "StringUtilities.h"
31 #include "UBSocket.h"
33 typedef OLCEditor E;
34 typedef CommandObject<E> O;
35 typedef CommandBinding<E> B;
37 static O listCommands("Commands", &E::listCommands);
38 static O newSavable("New", &E::newSavable);
39 static O deleteSavable("Delete", &E::deleteSavable);
40 static O editSavable("Edit", &E::editSavable);
41 static O quitEditor("Quit", &E::quitEditor);
42 static O doneEditing("Done", &E::doneEditing);
43 static O discardChanges("Discard", &E::discardChanges);
44 static O listSavable("List", &E::listSavable);
46 static const B commands[] = {
47 B("?", listCommands),
48 B("discard", discardChanges),
49 B("done", doneEditing),
50 B("edit", editSavable),
51 B("erase", deleteSavable),
52 B("list", listSavable),
53 B("new", newSavable),
54 B("quit", quitEditor),
57 OLCEditor::OLCEditor(UBSocket* sock) :
58 Editor(sock),
59 m_olccommands(commands, array_size(commands))
64 OLCEditor::~OLCEditor()
69 std::string OLCEditor::lookup(const std::string& action)
71 const SavableCommand* act = m_olccommands.getObject(action);
72 if(act)
73 return act->getName();
75 return Global::Get()->EmptyString;
78 void OLCEditor::dispatch(const std::string& action, const std::string& argument)
80 const SavableCommand* act = m_olccommands.getObject(action);
82 Assert(act);
83 act->Run(this, argument);
86 void OLCEditor::newSavable(const std::string& argument)
88 KeysPtr keys = addNew();
89 if(!keys->size())
91 Sendf("Could not create a new %s!\n", name().c_str());
92 return;
95 Sendf("%s created!\n", name().c_str());
96 setEditing(keys);
97 return;
100 void OLCEditor::editSavable(const std::string& argument)
102 TableImplPtr table = getTable();
104 if(argument.size() == 0)
106 Send("Please specify an id to edit.\n");
107 Send("Type 'list' to see a list of available id's.\n");
108 Sendf("Type 'new' to create a new %s!\n", name().c_str());
109 return;
112 KeysPtr keys;
114 try {
115 keys = KeysPtr(new Keys(table, argument));
116 } catch(std::invalid_argument& e)
118 Send("Please specify an id to edit.\n");
119 Sendf("'%s' is not a valid id.\n", argument.c_str());
120 return;
123 int count = SavableManager::count(keys);
125 if(count < 1)
127 Send("Please specify an id to edit.\n");
128 Sendf("'%s' does not exist!\n", argument.c_str());
129 return;
132 Assert(count == 1);
134 setEditing(keys);
135 m_sock->Sendf("You are now editing id %s> ", argument.c_str());
136 m_sock->Send(getEditing()->toString());
137 m_sock->Send("\n");
138 return;
141 void OLCEditor::deleteSavable(const std::string& argument)
143 if(argument.size() == 0)
145 Send("Please specify an id to delete.\n");
146 Send("Type 'list' to see a list of available id's.\n");
147 return;
150 KeysPtr keys;
154 keys = KeysPtr(new Keys(getTable(), argument));
156 catch(std::invalid_argument& e)
158 Send("Please specify an id to edit.\n");
159 Sendf("'%s' is not a valid id.\n", argument.c_str());
162 int count = SavableManager::count(keys);
164 if(count < 1)
166 Send("Please specify an id to delete.\n");
167 Sendf("'%s' does not exist!\n", argument.c_str());
168 return;
171 Assert(count == 1);
173 SavableManagerPtr manager = SavableManager::bykeys(keys);
174 bool locked = manager->lock();
176 if(!locked)
178 m_sock->Sendf("Could not lock '%s', it is being edited by someone else!\n", argument.c_str());
179 return;
182 manager->erase();
183 manager->unlock();
185 m_sock->Sendf("Deleted id %s.\n", keys->toString().c_str());
186 m_sock->Send("\n");
187 return;
190 void OLCEditor::listSavable(const std::string& argument)
192 std::string name = this->name();
193 name.append("s");
194 Strings list = getList();
195 if(list.size())
196 m_sock->Send(String::Get()->box(list,name));
197 else
198 m_sock->Sendf("No %ss.\n", this->name().c_str());
199 return;
202 void OLCEditor::listCommands(const std::string& argument)
204 m_sock->Send(String::Get()->box(m_olccommands.getCommandsVector(), name()));
205 m_sock->Send("\n");
206 std::string name = this->name();
207 name.append("Editing");
208 m_sock->Send(String::Get()->box(getCommands(), name));
209 m_sock->Send("\n");
210 return;
213 void OLCEditor::quitEditor(const std::string& argument)
215 m_sock->Send("Ok.\n");
216 SavablePtr editing = getEditing();
218 if(editing)
220 try {
221 editing->Save();
222 } catch(SavableLocked& e) {
223 // if they don't have a lock, they didn't edit anything anyway
224 // so no need to save or notify them
228 m_sock->PopEditor();
229 return;
232 void OLCEditor::doneEditing(const std::string& argument)
234 SavablePtr editing = getEditing();
236 if(editing)
238 m_sock->Send("Ok.\n");
240 try {
241 editing->Save();
242 } catch(SavableLocked& e) {
243 // if they don't have a lock, they didn't edit anything anyway
244 // so no need to save or notify them
246 } else {
247 m_sock->Send("You are not editing anything, so no need to type 'done'.\n");
250 TableImplPtr table = getTable();
251 Assert(table);
253 KeysPtr keys(new Keys(table));
254 setEditing(keys);
255 return;
258 void OLCEditor::discardChanges(const std::string& argument)
260 if(argument.compare("confirm"))
262 m_sock->Send("Please type 'discard confirm' to discard your changes.\n");
263 return;
266 m_sock->Send("Ok.\n");
267 SavablePtr editing = getEditing();
269 if(editing)
271 try {
272 editing->Lock();
273 editing->Discard();
274 } catch(SavableLocked& e) {
275 m_sock->Send("You cannot discard all changes as you are not the one editing!\n");
277 } else {
278 m_sock->Send("You're not editing anything!\n");
281 return;