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