1 /***************************************************************************
2 Copyright (C) 2006-2009 Robby Stephenson <robby@periapsis.org>
3 ***************************************************************************/
4
5 /***************************************************************************
6 * *
7 * This program is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU General Public License as *
9 * published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) version 3 or any later version *
11 * accepted by the membership of KDE e.V. (or its successor approved *
12 * by the membership of KDE e.V.), which shall act as a proxy *
13 * defined in Section 14 of version 3 of the license. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
22 * *
23 ***************************************************************************/
24
25 #include "updateentries.h"
26 #include "fieldcommand.h"
27 #include "modifyentries.h"
28 #include "../collection.h"
29 #include "../utils/mergeconflictresolver.h"
30
31 #include <KLocalizedString>
32
33 namespace {
34
35 class OverWriteResolver : public Tellico::Merge::ConflictResolver {
36 public:
OverWriteResolver(bool overwrite_)37 OverWriteResolver(bool overwrite_) : m_overWrite(overwrite_) {};
resolve(Tellico::Data::EntryPtr,Tellico::Data::EntryPtr,Tellico::Data::FieldPtr,const QString & value1=QString (),const QString & value2=QString ())38 Tellico::Merge::ConflictResolver::Result resolve(Tellico::Data::EntryPtr,
39 Tellico::Data::EntryPtr,
40 Tellico::Data::FieldPtr,
41 const QString& value1 = QString(),
42 const QString& value2 = QString()) Q_DECL_OVERRIDE {
43 Q_UNUSED(value1);
44 Q_UNUSED(value2);
45 return m_overWrite ? Tellico::Merge::ConflictResolver::KeepSecond : Tellico::Merge::ConflictResolver::KeepFirst;
46 }
47
48 private:
49 Q_DISABLE_COPY(OverWriteResolver)
50 bool m_overWrite;
51 };
52
53 }
54
55 using Tellico::Command::UpdateEntries;
56
57 namespace Tellico {
58 namespace Command {
59
60 class MergeEntries : public QUndoCommand {
61 public:
MergeEntries(QUndoCommand * updater,Data::EntryPtr currEntry_,Data::EntryPtr newEntry_,bool overWrite_)62 MergeEntries(QUndoCommand* updater, Data::EntryPtr currEntry_, Data::EntryPtr newEntry_, bool overWrite_)
63 : QUndoCommand(updater)
64 , m_currEntry(currEntry_)
65 , m_newEntry(newEntry_)
66 , m_orphanEntry(new Data::Entry(*currEntry_))
67 , m_overWrite(overWrite_) {
68 // we merge the entries here instead of in redo() because this
69 // command is never called without also calling ModifyEntries()
70 // which takes care of copying the entry values
71 // Data::Document::mergeEntry(currEntry_, newEntry_, overWrite_);
72 }
73
redo()74 virtual void redo() Q_DECL_OVERRIDE {
75 OverWriteResolver res(m_overWrite);
76 Tellico::Merge::mergeEntry(m_currEntry, m_newEntry, &res);
77 }
undo()78 virtual void undo() Q_DECL_OVERRIDE {} // does nothing
orphanEntry() const79 Data::EntryPtr orphanEntry() const { return m_orphanEntry; }
80
81 private:
82 Data::EntryPtr m_currEntry;
83 Data::EntryPtr m_newEntry;
84 Data::EntryPtr m_orphanEntry;
85 bool m_overWrite;
86 };
87 }
88 }
89
UpdateEntries(Tellico::Data::CollPtr coll_,Tellico::Data::EntryPtr oldEntry_,Tellico::Data::EntryPtr newEntry_,bool overWrite_)90 UpdateEntries::UpdateEntries(Tellico::Data::CollPtr coll_, Tellico::Data::EntryPtr oldEntry_, Tellico::Data::EntryPtr newEntry_, bool overWrite_)
91 : QUndoCommand(i18nc("Modify (Entry Title)", "Modify %1", newEntry_->title()))
92 , m_coll(coll_)
93 , m_oldEntry(oldEntry_)
94 , m_newEntry(newEntry_)
95 , m_overWrite(overWrite_)
96 {
97 }
98
redo()99 void UpdateEntries::redo() {
100 if(childCount() == 0) {
101 // add commands
102 // do this here instead of the constructor because several UpdateEntries may be in one command
103 // and I don't want to add new fields multiple times
104
105 auto p = Tellico::Merge::mergeFields(m_coll,
106 m_newEntry->collection()->fields(),
107 Data::EntryList() << m_newEntry);
108 Data::FieldList modifiedFields = p.first;
109 Data::FieldList addedFields = p.second;
110
111 QStringList updatedFields;
112
113 foreach(Data::FieldPtr field, modifiedFields) {
114 if(m_coll->hasField(field->name())) {
115 new FieldCommand(this, FieldCommand::FieldModify, m_coll,
116 field, m_coll->fieldByName(field->name()));
117 }
118 updatedFields << field->name();
119 }
120
121 foreach(Data::FieldPtr field, addedFields) {
122 new FieldCommand(this, FieldCommand::FieldAdd, m_coll, field);
123 updatedFields << field->name();
124 }
125
126 // MergeEntries copies values from m_newEntry into m_oldEntry
127 // m_oldEntry is in the current collection
128 // m_newEntry isn't...
129 MergeEntries* cmd = new MergeEntries(this, m_oldEntry, m_newEntry, m_overWrite);
130 // cmd->orphanEntry() returns a copy of m_oldEntry before values were merged
131 // m_oldEntry has new values
132 // in the ModifyEntries command, the second entry should be owned by the current
133 // collection and contain the updated values
134 // the first one is not owned by current collection
135 new ModifyEntries(this, m_coll, Data::EntryList() << cmd->orphanEntry(), Data::EntryList() << m_oldEntry, updatedFields);
136 }
137 // calls redo() on all child commands
138 QUndoCommand::redo();
139 }
140