summaryrefslogtreecommitdiffstats
path: root/src/commands/fieldcommand.cpp
blob: 5ba080bc8fefc8331ea85c37b77616fb974c3149 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/***************************************************************************
    copyright            : (C) 2005-2006 by Robby Stephenson
    email                : robby@periapsis.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of version 2 of the GNU General Public License as  *
 *   published by the Free Software Foundation;                            *
 *                                                                         *
 ***************************************************************************/

#include "fieldcommand.h"
#include "../collection.h"
#include "../controller.h"
#include "../tellico_debug.h"

#include <klocale.h>

using Tellico::Command::FieldCommand;

FieldCommand::FieldCommand(Mode mode_, Data::CollPtr coll_,
                           Data::FieldPtr activeField_, Data::FieldPtr oldField_/*=0*/)
    : KCommand()
    , m_mode(mode_)
    , m_coll(coll_)
    , m_activeField(activeField_)
    , m_oldField(oldField_)
{
  if(!m_coll) {
    myDebug() << "FieldCommand() - null collection pointer" << endl;
  } else if(!m_activeField) {
    myDebug() << "FieldCommand() - null active field pointer" << endl;
  }
#ifndef NDEBUG
// just some sanity checking
  if(m_mode == FieldAdd && m_oldField != 0) {
    myDebug() << "FieldCommand() - adding field, but pointers are wrong" << endl;
  } else if(m_mode == FieldModify && m_oldField == 0) {
    myDebug() << "FieldCommand() - modifying field, but pointers are wrong" << endl;
  } else if(m_mode == FieldRemove && m_oldField != 0) {
    myDebug() << "FieldCommand() - removing field, but pointers are wrong" << endl;
  }
#endif
}

void FieldCommand::execute() {
  if(!m_coll || !m_activeField) {
    return;
  }

  switch(m_mode) {
    case FieldAdd:
      // if there's currently a field in the collection with the same name, it will get overwritten
      // so save a pointer to it here, the collection should not delete it
      m_oldField = m_coll->fieldByName(m_activeField->name());
      m_coll->addField(m_activeField);
      Controller::self()->addedField(m_coll, m_activeField);
      break;

    case FieldModify:
      m_coll->modifyField(m_activeField);
      Controller::self()->modifiedField(m_coll, m_oldField, m_activeField);
      break;

    case FieldRemove:
      m_coll->removeField(m_activeField);
      Controller::self()->removedField(m_coll, m_activeField);
      break;
  }
}

void FieldCommand::unexecute() {
  if(!m_coll || !m_activeField) {
    return;
  }

  switch(m_mode) {
    case FieldAdd:
      m_coll->removeField(m_activeField);
      Controller::self()->removedField(m_coll, m_activeField);
      if(m_oldField) {
        m_coll->addField(m_oldField);
        Controller::self()->addedField(m_coll, m_oldField);
      }
      break;

    case FieldModify:
      m_coll->modifyField(m_oldField);
      Controller::self()->modifiedField(m_coll, m_activeField, m_oldField);
      break;

    case FieldRemove:
      m_coll->addField(m_activeField);
      Controller::self()->addedField(m_coll, m_activeField);
      break;
  }
}

TQString FieldCommand::name() const {
  switch(m_mode) {
    case FieldAdd:
      return i18n("Add %1 Field").tqarg(m_activeField->title());
    case FieldModify:
      return i18n("Modify %1 Field").tqarg(m_activeField->title());
    case FieldRemove:
      return i18n("Delete %1 Field").tqarg(m_activeField->title());
  }
  // hush warnings
  return TQString();
}