| 1 | /*
 | 
|---|
| 2 |  * VerboseAction.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: May 9, 2010
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include "Actions/CmdAction/VerboseAction.hpp"
 | 
|---|
| 11 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| 12 | #include "Helpers/Log.hpp"
 | 
|---|
| 13 | #include "Helpers/Verbose.hpp"
 | 
|---|
| 14 | 
 | 
|---|
| 15 | #include <iostream>
 | 
|---|
| 16 | #include <string>
 | 
|---|
| 17 | 
 | 
|---|
| 18 | using namespace std;
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "UIElements/UIFactory.hpp"
 | 
|---|
| 21 | #include "UIElements/Dialog.hpp"
 | 
|---|
| 22 | #include "Actions/ValueStorage.hpp"
 | 
|---|
| 23 | 
 | 
|---|
| 24 | // memento to remember the state when undoing
 | 
|---|
| 25 | 
 | 
|---|
| 26 | class CommandLineVerboseState : public ActionState {
 | 
|---|
| 27 | public:
 | 
|---|
| 28 |   CommandLineVerboseState(const int _oldverbosity, const int _newverbosity) :
 | 
|---|
| 29 |     oldverbosity(_oldverbosity),
 | 
|---|
| 30 |     newverbosity(_newverbosity)
 | 
|---|
| 31 |   {}
 | 
|---|
| 32 |   int oldverbosity;
 | 
|---|
| 33 |   int newverbosity;
 | 
|---|
| 34 | };
 | 
|---|
| 35 | 
 | 
|---|
| 36 | 
 | 
|---|
| 37 | const char CommandLineVerboseAction::NAME[] = "verbose";
 | 
|---|
| 38 | 
 | 
|---|
| 39 | CommandLineVerboseAction::CommandLineVerboseAction() :
 | 
|---|
| 40 |   Action(NAME)
 | 
|---|
| 41 | {}
 | 
|---|
| 42 | 
 | 
|---|
| 43 | CommandLineVerboseAction::~CommandLineVerboseAction()
 | 
|---|
| 44 | {}
 | 
|---|
| 45 | 
 | 
|---|
| 46 | void CommandVerbose(int verbosity) {
 | 
|---|
| 47 |   ValueStorage::getInstance().setCurrentValue(CommandLineVerboseAction::NAME, verbosity);
 | 
|---|
| 48 |   ActionRegistry::getInstance().getActionByName(CommandLineVerboseAction::NAME)->call(Action::NonInteractive);
 | 
|---|
| 49 | };
 | 
|---|
| 50 | 
 | 
|---|
| 51 | Dialog* CommandLineVerboseAction::fillDialog(Dialog *dialog) {
 | 
|---|
| 52 |   ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
| 53 | 
 | 
|---|
| 54 |   dialog->queryInt(NAME, ValueStorage::getInstance().getDescription(NAME));
 | 
|---|
| 55 | 
 | 
|---|
| 56 |   return dialog;
 | 
|---|
| 57 | }
 | 
|---|
| 58 | 
 | 
|---|
| 59 | Action::state_ptr CommandLineVerboseAction::performCall() {
 | 
|---|
| 60 |   int oldverbosity = getVerbosity();
 | 
|---|
| 61 |   int newverbosity = 2;
 | 
|---|
| 62 | 
 | 
|---|
| 63 |   ValueStorage::getInstance().queryCurrentValue(NAME, newverbosity);
 | 
|---|
| 64 | 
 | 
|---|
| 65 |   if (oldverbosity != newverbosity) {
 | 
|---|
| 66 |     CommandLineVerboseState *UndoState = new CommandLineVerboseState(oldverbosity, newverbosity);
 | 
|---|
| 67 |     setVerbosity(newverbosity);
 | 
|---|
| 68 |     DoLog(0) && (Log() << Verbose(0) << "Setting verbosity from " << oldverbosity << " to " << newverbosity << "." << endl);
 | 
|---|
| 69 |     return Action::state_ptr(UndoState);
 | 
|---|
| 70 |   } else {
 | 
|---|
| 71 |     DoLog(0) && (Log() << Verbose(0) << "Verbosity remains unchanged at " << oldverbosity << "." << endl);
 | 
|---|
| 72 |     return Action::failure;
 | 
|---|
| 73 |   }
 | 
|---|
| 74 | }
 | 
|---|
| 75 | 
 | 
|---|
| 76 | Action::state_ptr CommandLineVerboseAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 77 |   CommandLineVerboseState *state = assert_cast<CommandLineVerboseState*>(_state.get());
 | 
|---|
| 78 | 
 | 
|---|
| 79 |   DoLog(0) && (Log() << Verbose(0) << "Setting verbosity from " << state->newverbosity << " to " << state->oldverbosity << "." << endl);
 | 
|---|
| 80 |   setVerbosity(state->oldverbosity);
 | 
|---|
| 81 | 
 | 
|---|
| 82 |   return Action::state_ptr(_state);
 | 
|---|
| 83 | }
 | 
|---|
| 84 | 
 | 
|---|
| 85 | Action::state_ptr CommandLineVerboseAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 86 |   CommandLineVerboseState *state = assert_cast<CommandLineVerboseState*>(_state.get());
 | 
|---|
| 87 | 
 | 
|---|
| 88 |   DoLog(0) && (Log() << Verbose(0) << "Setting verbosity from " << state->oldverbosity << " to " << state->newverbosity << "." << endl);
 | 
|---|
| 89 |   setVerbosity(state->newverbosity);
 | 
|---|
| 90 | 
 | 
|---|
| 91 |   return Action::state_ptr(_state);
 | 
|---|
| 92 | }
 | 
|---|
| 93 | 
 | 
|---|
| 94 | bool CommandLineVerboseAction::canUndo() {
 | 
|---|
| 95 |   return true;
 | 
|---|
| 96 | }
 | 
|---|
| 97 | 
 | 
|---|
| 98 | bool CommandLineVerboseAction::shouldUndo() {
 | 
|---|
| 99 |   return true;
 | 
|---|
| 100 | }
 | 
|---|
| 101 | 
 | 
|---|
| 102 | const string CommandLineVerboseAction::getName() {
 | 
|---|
| 103 |   return NAME;
 | 
|---|
| 104 | }
 | 
|---|