summaryrefslogtreecommitdiffstats
path: root/src/commands/addentries.cpp
blob: 6cd763629b097004f9b085ca9187f3f23e8b9d69 (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
/***************************************************************************
    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 "addentries.h"
#include "../collection.h"
#include "../controller.h"
#include "../datavectors.h"
#include "../tellico_debug.h"

#include <klocale.h>

using Tellico::Command::AddEntries;

AddEntries::AddEntries(Data::CollPtr coll_, const Data::EntryVec& entries_)
    : KCommand()
    , m_coll(coll_)
    , m_entries(entries_)
{
}

void AddEntries::execute() {
  if(!m_coll || m_entries.isEmpty()) {
    return;
  }

  m_coll->addEntries(m_entries);
  // now check for default values
  Data::FieldVec fields = m_coll->fields();
  for(Data::FieldVec::Iterator field = fields.begin(); field != fields.end(); ++field) {
    const QString defaultValue = field->defaultValue();
    if(!defaultValue.isEmpty()) {
      for(Data::EntryVec::Iterator entry = m_entries.begin(); entry != m_entries.end(); ++entry) {
        if(entry->field(field).isEmpty()) {
          entry->setField(field->name(), defaultValue);
        }
      }
    }
  }
  Controller::self()->addedEntries(m_entries);
}

void AddEntries::unexecute() {
  if(!m_coll || m_entries.isEmpty()) {
    return;
  }

  m_coll->removeEntries(m_entries);
  Controller::self()->removedEntries(m_entries);
}

QString AddEntries::name() const {
  return m_entries.count() > 1 ? i18n("Add Entries")
                               : i18n("Add (Entry Title)", "Add %1").arg(m_entries.begin()->title());
}