1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
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. *
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. *
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 ***************************************************************************/
25 #include "DatabaseMgr.h"
26 #include "EditorOLC.h"
27 #include "OLCEditor.h"
28 #include "Permission.h"
30 #include "StringUtilities.h"
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
[] = {
48 B("discard", discardChanges
),
49 B("done", doneEditing
),
50 B("edit", editSavable
),
51 B("erase", deleteSavable
),
52 B("list", listSavable
),
54 B("quit", quitEditor
),
57 OLCEditor::OLCEditor(UBSocket
* 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
);
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
);
83 act
->Run(this, argument
);
86 void OLCEditor::newSavable(const std::string
& argument
)
88 KeysPtr keys
= addNew();
91 Sendf("Could not create a new %s!\n", name().c_str());
95 Sendf("%s created!\n", name().c_str());
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());
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());
123 int count
= SavableManager::count(keys
);
127 Send("Please specify an id to edit.\n");
128 Sendf("'%s' does not exist!\n", argument
.c_str());
135 m_sock
->Sendf("You are now editing id %s> ", argument
.c_str());
136 m_sock
->Send(getEditing()->toString());
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");
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
);
166 Send("Please specify an id to delete.\n");
167 Sendf("'%s' does not exist!\n", argument
.c_str());
173 SavableManagerPtr manager
= SavableManager::bykeys(keys
);
174 bool locked
= manager
->lock();
178 m_sock
->Sendf("Could not lock '%s', it is being edited by someone else!\n", argument
.c_str());
185 m_sock
->Sendf("Deleted id %s.\n", keys
->toString().c_str());
190 void OLCEditor::listSavable(const std::string
& argument
)
192 std::string name
= this->name();
194 Strings list
= getList();
196 m_sock
->Send(String::Get()->box(list
,name
));
198 m_sock
->Sendf("No %ss.\n", this->name().c_str());
202 void OLCEditor::listCommands(const std::string
& argument
)
204 m_sock
->Send(String::Get()->box(m_olccommands
.getCommandsVector(), name()));
206 std::string name
= this->name();
207 name
.append("Editing");
208 m_sock
->Send(String::Get()->box(getCommands(), name
));
213 void OLCEditor::quitEditor(const std::string
& argument
)
215 m_sock
->Send("Ok.\n");
216 SavablePtr editing
= getEditing();
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
232 void OLCEditor::doneEditing(const std::string
& argument
)
234 SavablePtr editing
= getEditing();
238 m_sock
->Send("Ok.\n");
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
247 m_sock
->Send("You are not editing anything, so no need to type 'done'.\n");
250 TableImplPtr table
= getTable();
253 KeysPtr
keys(new Keys(table
));
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");
266 m_sock
->Send("Ok.\n");
267 SavablePtr editing
= getEditing();
274 } catch(SavableLocked
& e
) {
275 m_sock
->Send("You cannot discard all changes as you are not the one editing!\n");
278 m_sock
->Send("You're not editing anything!\n");