File indexing completed on 2024-05-12 05:09:24

0001 /***************************************************************************
0002     Copyright (C) 2006-2009 Robby Stephenson <robby@periapsis.org>
0003 
0004  ***************************************************************************/
0005 
0006 /***************************************************************************
0007  *                                                                         *
0008  *   This program is free software; you can redistribute it and/or         *
0009  *   modify it under the terms of the GNU General Public License as        *
0010  *   published by the Free Software Foundation; either version 2 of        *
0011  *   the License or (at your option) version 3 or any later version        *
0012  *   accepted by the membership of KDE e.V. (or its successor approved     *
0013  *   by the membership of KDE e.V.), which shall act as a proxy            *
0014  *   defined in Section 14 of version 3 of the license.                    *
0015  *                                                                         *
0016  *   This program is distributed in the hope that it will be useful,       *
0017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0019  *   GNU General Public License for more details.                          *
0020  *                                                                         *
0021  *   You should have received a copy of the GNU General Public License     *
0022  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0023  *                                                                         *
0024  ***************************************************************************/
0025 
0026 #include "modifyentries.h"
0027 #include "../collection.h"
0028 #include "../controller.h"
0029 #include "../tellico_debug.h"
0030 
0031 #include <KLocalizedString>
0032 
0033 using Tellico::Command::ModifyEntries;
0034 
0035 ModifyEntries::ModifyEntries(Tellico::Data::CollPtr coll_, const Tellico::Data::EntryList& oldEntries_,
0036                              const Tellico::Data::EntryList& newEntries_, const QStringList& modifiedFields_)
0037     : QUndoCommand()
0038     , m_coll(coll_)
0039     , m_oldEntries(oldEntries_)
0040     , m_entries(newEntries_)
0041     , m_modifiedFields(modifiedFields_)
0042     , m_needToSwap(false)
0043 {
0044 #ifndef NDEBUG
0045   if(m_oldEntries.count() != m_entries.count()) {
0046     myWarning() << "unequal number of entries";
0047   }
0048 #endif
0049   if(!m_entries.isEmpty()) {
0050     setText(m_entries.count() > 1 ? i18n("Modify Entries")
0051                                   : i18nc("Modify (Entry Title)", "Modify %1", m_entries[0]->title()));
0052   }
0053 }
0054 
0055 ModifyEntries::ModifyEntries(QUndoCommand* parent, Tellico::Data::CollPtr coll_, const Tellico::Data::EntryList& oldEntries_,
0056                              const Tellico::Data::EntryList& newEntries_, const QStringList& modifiedFields_)
0057     : QUndoCommand(parent)
0058     , m_coll(coll_)
0059     , m_oldEntries(oldEntries_)
0060     , m_entries(newEntries_)
0061     , m_modifiedFields(modifiedFields_)
0062     , m_needToSwap(false)
0063 {
0064 #ifndef NDEBUG
0065   if(m_oldEntries.count() != m_entries.count()) {
0066     myWarning() << "unequal number of entries";
0067   }
0068 #endif
0069   if(!m_entries.isEmpty()) {
0070     setText(m_entries.count() > 1 ? i18n("Modify Entries")
0071                                   : i18nc("Modify (Entry Title)", "Modify %1", m_entries[0]->title()));
0072   }
0073 }
0074 
0075 void ModifyEntries::redo() {
0076   if(!m_coll || m_entries.isEmpty()) {
0077     return;
0078   }
0079   if(m_needToSwap) {
0080     swapValues();
0081     m_needToSwap = false;
0082   }
0083   // loans expose a field named "loaned", and the user might modify that without
0084   // checking in the loan, so verify that. Heavy-handed, yes...
0085   const QString loaned = QStringLiteral("loaned");
0086   bool hasLoanField = m_coll->hasField(loaned);
0087   if(hasLoanField && m_modifiedFields.contains(loaned)) {
0088     Data::EntryList notLoaned;
0089     foreach(Data::EntryPtr entry, m_entries) {
0090       if(entry->field(loaned).isEmpty()) {
0091         notLoaned.append(entry);
0092       }
0093     }
0094     if(!notLoaned.isEmpty()) {
0095       Controller::self()->slotCheckIn(notLoaned);
0096     }
0097   }
0098   m_coll->updateDicts(m_entries, m_modifiedFields);
0099   Controller::self()->modifiedEntries(m_entries);
0100 }
0101 
0102 void ModifyEntries::undo() {
0103   if(!m_coll || m_entries.isEmpty()) {
0104     return;
0105   }
0106   swapValues();
0107   m_needToSwap = true;
0108   m_coll->updateDicts(m_entries, m_modifiedFields);
0109   Controller::self()->modifiedEntries(m_entries);
0110   //TODO: need to tell edit dialog that it's not modified
0111 }
0112 
0113 void ModifyEntries::swapValues() {
0114   // since things like the detailedlistview and the entryiconview hold pointers to the entries
0115   // can't just call Controller::modifiedEntry() on the old pointers
0116   for(int i = 0; i < m_entries.count(); ++i) {
0117     // need to swap entry values, not just pointers
0118     // the id gets reset when copying, so need to keep it
0119     const Data::ID id = m_entries[i]->id();
0120     Data::Entry tmp(*m_entries[i]); // tmp id becomes -1
0121     *m_entries[i] = *m_oldEntries[i]; // id becomes -1
0122     m_entries[i]->setId(id); // id becomes what was originally
0123     *m_oldEntries[i] = tmp; // id becomes -1
0124   }
0125 }