summaryrefslogtreecommitdiffstats
path: root/src/commands/filtercommand.cpp
blob: c95db132835788eedb9ef532844835614bc3a856 (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
/***************************************************************************
    copyright            : (C) 2005-2006 by Robby Stephenson
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "filtercommand.h"
#include "../document.h"
#include "../collection.h"
#include "../controller.h"
#include "../tellico_debug.h"

#include <klocale.h>

using Tellico::Command::FilterCommand;

FilterCommand::FilterCommand(Mode mode_, FilterPtr activeFilter_, FilterPtr oldFilter_/*=0*/)
    : KCommand()
    , m_mode(mode_)
    , m_activeFilter(activeFilter_)
    , m_oldFilter(oldFilter_)
{
  if(!m_activeFilter) {
    myDebug() << "FilterCommand() - null active filter pointer" << endl;
  }
#ifndef NDEBUG
// just some sanity checking
  if(m_mode == FilterAdd && m_oldFilter != 0) {
    myDebug() << "FilterCommand() - adding field, but pointers are wrong" << endl;
  } else if(m_mode == FilterModify && m_oldFilter == 0) {
    myDebug() << "FilterCommand() - modifying field, but pointers are wrong" << endl;
  } else if(m_mode == FilterRemove && m_oldFilter != 0) {
    myDebug() << "FilterCommand() - removing field, but pointers are wrong" << endl;
  }
#endif
}

void FilterCommand::execute() {
  if(!m_activeFilter) {
    return;
  }

  switch(m_mode) {
    case FilterAdd:
      Data::Document::self()->collection()->addFilter(m_activeFilter);
      Controller::self()->addedFilter(m_activeFilter);
      break;

    case FilterModify:
      Data::Document::self()->collection()->removeFilter(m_oldFilter);
      Controller::self()->removedFilter(m_oldFilter);
      Data::Document::self()->collection()->addFilter(m_activeFilter);
      Controller::self()->addedFilter(m_activeFilter);
      break;

    case FilterRemove:
      Data::Document::self()->collection()->removeFilter(m_activeFilter);
      Controller::self()->removedFilter(m_activeFilter);
      break;
  }
}

void FilterCommand::unexecute() {
  if(!m_activeFilter) {
    return;
  }

  switch(m_mode) {
    case FilterAdd:
      Data::Document::self()->collection()->removeFilter(m_activeFilter);
      Controller::self()->removedFilter(m_activeFilter);
      break;

    case FilterModify:
      Data::Document::self()->collection()->removeFilter(m_activeFilter);
      Controller::self()->removedFilter(m_activeFilter);
      Data::Document::self()->collection()->addFilter(m_oldFilter);
      Controller::self()->addedFilter(m_oldFilter);
      break;

    case FilterRemove:
      Data::Document::self()->collection()->addFilter(m_activeFilter);
      Controller::self()->addedFilter(m_activeFilter);
      break;
  }
}

TQString FilterCommand::name() const {
  switch(m_mode) {
    case FilterAdd:
      return i18n("Add Filter");
    case FilterModify:
      return i18n("Modify Filter");
    case FilterRemove:
      return i18n("Delete Filter");
  }
  // hush warnings
  return TQString();
}