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 "addloans.h"
0027 #include "../document.h"
0028 #include "../entry.h"
0029 #include "../collection.h"
0030 #include "../controller.h"
0031 #include "../tellico_debug.h"
0032 
0033 #include <KLocalizedString>
0034 
0035 using Tellico::Command::AddLoans;
0036 
0037 AddLoans::AddLoans(Tellico::Data::BorrowerPtr borrower_, Tellico::Data::LoanList loans_, bool addToCalendar_)
0038     : QUndoCommand()
0039     , m_borrower(borrower_)
0040     , m_loans(loans_)
0041     , m_addedLoanField(false)
0042     , m_addToCalendar(addToCalendar_)
0043 {
0044   if(m_loans.isEmpty()) {
0045     myWarning() << "no loans!";
0046   } else {
0047     setText(m_loans.count() > 1 ? i18n("Check-out Items")
0048                                 : i18nc("Check-out (Entry Title)", "Check-out %1", m_loans[0]->entry()->title()));
0049   }
0050 }
0051 
0052 void AddLoans::redo() {
0053   if(!m_borrower || m_loans.isEmpty()) {
0054     return;
0055   }
0056 
0057   // if the borrower is empty, assume it's getting added, otherwise it's being modified
0058   // the collection actually does the real check in addBorrower
0059   bool wasEmpty = m_borrower->isEmpty();
0060 
0061   // if there's no loaned field, we'll add one
0062   bool loanExisted = m_loans[0]->entry()->collection()->hasField(QStringLiteral("loaned"));
0063   m_addedLoanField = false; // assume we didn't add the field yet
0064 
0065   // add the loans to the borrower
0066   foreach(Data::LoanPtr loan, m_loans) {
0067     m_borrower->addLoan(loan);
0068     Data::Document::self()->checkOutEntry(loan->entry());
0069     Controller::self()->modifiedEntries(Data::EntryList() << loan->entry());
0070   }
0071   if(!loanExisted) {
0072     Data::CollPtr c = m_loans[0]->entry()->collection();
0073     Data::FieldPtr f = c->fieldByName(QStringLiteral("loaned"));
0074     if(f) {
0075       // notify everything that a new field was added
0076       Controller::self()->addedField(c, f);
0077       m_addedLoanField = true;
0078     }
0079   }
0080   if(m_addToCalendar) {
0081     myWarning() << "Add to calendar not implemented";
0082   }
0083   if(wasEmpty) {
0084     m_loans[0]->entry()->collection()->addBorrower(m_borrower);
0085     Controller::self()->addedBorrower(m_borrower);
0086   } else {
0087     // don't have to do anything to the document, it just holds a pointer
0088     Controller::self()->modifiedBorrower(m_borrower);
0089   }
0090 }
0091 
0092 void AddLoans::undo() {
0093   if(!m_borrower) {
0094     return;
0095   }
0096 
0097   // remove the loans from the borrower
0098   foreach(Data::LoanPtr loan, m_loans) {
0099     m_borrower->removeLoan(loan);
0100     Data::Document::self()->checkInEntry(loan->entry());
0101     Controller::self()->modifiedEntries(Data::EntryList() << loan->entry());
0102   }
0103   if(m_addedLoanField) {
0104     Data::CollPtr c = m_loans[0]->entry()->collection();
0105     Data::FieldPtr f = c->fieldByName(QStringLiteral("loaned"));
0106     if(f) {
0107       c->removeField(f);
0108       Controller::self()->removedField(c, f);
0109     }
0110   }
0111   if(m_addToCalendar) {
0112     myWarning() << "Add to calendar not implemented";
0113   }
0114   // the borrower object is kept in the document, it's just empty
0115   // it won't get saved in the document file
0116   // here, just notify everybody that it changed
0117   Controller::self()->modifiedBorrower(m_borrower);
0118 }