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

0001 /***************************************************************************
0002     Copyright (C) 2005-2011 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #include "removeentries.h"
0026 #include "removeloans.h"
0027 #include "../collection.h"
0028 #include "../controller.h"
0029 #include "../tellico_debug.h"
0030 
0031 #include <KLocalizedString>
0032 
0033 using Tellico::Command::RemoveEntries;
0034 
0035 RemoveEntries::RemoveEntries(Tellico::Data::CollPtr coll_, const Tellico::Data::EntryList& entries_)
0036     : QUndoCommand()
0037     , m_coll(coll_)
0038     , m_entries(entries_)
0039 {
0040   if(!m_entries.isEmpty()) {
0041     setText(m_entries.count() > 1 ? i18n("Delete Entries")
0042                                   : i18nc("Delete (Entry Title)", "Delete %1", m_entries[0]->title()));
0043   }
0044 
0045   // also need to allow for removing entries that might be loaned out
0046   // nothing for it but to do full-blown iterative search
0047   Data::LoanList loans;
0048   foreach(Data::BorrowerPtr borrower, m_coll->borrowers()) {
0049     foreach(Data::EntryPtr entry, m_entries) {
0050       if(borrower->hasEntry(entry)) {
0051         loans += borrower->loan(entry);
0052       }
0053     }
0054   }
0055   if(!loans.isEmpty()) {
0056     new RemoveLoans(loans, this);
0057   }
0058 }
0059 
0060 void RemoveEntries::redo() {
0061   if(!m_coll || m_entries.isEmpty()) {
0062     return;
0063   }
0064 
0065   m_coll->removeEntries(m_entries);
0066   Controller::self()->removedEntries(m_entries);
0067 
0068   QUndoCommand::redo();
0069 }
0070 
0071 void RemoveEntries::undo() {
0072   if(!m_coll || m_entries.isEmpty()) {
0073     return;
0074   }
0075 
0076   m_coll->addEntries(m_entries);
0077   Controller::self()->addedEntries(m_entries);
0078 
0079   QUndoCommand::undo();
0080 }