summaryrefslogtreecommitdiffstats
path: root/src/commands/addloans.cpp
diff options
context:
space:
mode:
authortpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2010-03-01 19:17:32 +0000
committertpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2010-03-01 19:17:32 +0000
commite38d2351b83fa65c66ccde443777647ef5cb6cff (patch)
tree1897fc20e9f73a81c520a5b9f76f8ed042124883 /src/commands/addloans.cpp
downloadtellico-e38d2351b83fa65c66ccde443777647ef5cb6cff.tar.gz
tellico-e38d2351b83fa65c66ccde443777647ef5cb6cff.zip
Added KDE3 version of Tellico
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/tellico@1097620 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'src/commands/addloans.cpp')
-rw-r--r--src/commands/addloans.cpp110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/commands/addloans.cpp b/src/commands/addloans.cpp
new file mode 100644
index 0000000..dab67d7
--- /dev/null
+++ b/src/commands/addloans.cpp
@@ -0,0 +1,110 @@
+/***************************************************************************
+ copyright : (C) 2005-2006 by Robby Stephenson
+ ***************************************************************************/
+
+/***************************************************************************
+ * *
+ * 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 "addloans.h"
+#include "../document.h"
+#include "../entry.h"
+#include "../collection.h"
+#include "../controller.h"
+#include "../calendarhandler.h"
+#include "../tellico_debug.h"
+
+#include <klocale.h>
+
+using Tellico::Command::AddLoans;
+
+AddLoans::AddLoans(Data::BorrowerPtr borrower_, Data::LoanVec loans_, bool addToCalendar_)
+ : KCommand()
+ , m_borrower(borrower_)
+ , m_loans(loans_)
+ , m_addedLoanField(false)
+ , m_addToCalendar(addToCalendar_)
+{
+}
+
+void AddLoans::execute() {
+ if(!m_borrower || m_loans.isEmpty()) {
+ return;
+ }
+
+ // if the borrower is empty, assume it's getting added, otherwise it's being modified
+ bool wasEmpty = m_borrower->isEmpty();
+
+ // if there's no loaned field, we'll add one
+ bool loanExisted = m_loans.begin()->entry()->collection()->hasField(QString::fromLatin1("loaned"));
+ m_addedLoanField = false; // assume we didn't add the field yet
+
+ // add the loans to the borrower
+ for(Data::LoanVec::Iterator loan = m_loans.begin(); loan != m_loans.end(); ++loan) {
+ m_borrower->addLoan(loan);
+ Data::Document::self()->checkOutEntry(loan->entry());
+ Data::EntryVec vec;
+ vec.append(loan->entry());
+ Controller::self()->modifiedEntries(vec);
+ }
+ if(!loanExisted) {
+ Data::CollPtr c = m_loans.begin()->entry()->collection();
+ Data::FieldPtr f = c->fieldByName(QString::fromLatin1("loaned"));
+ if(f) {
+ // notify everything that a new field was added
+ Controller::self()->addedField(c, f);
+ m_addedLoanField = true;
+ }
+ }
+ if(m_addToCalendar) {
+ CalendarHandler::addLoans(m_loans);
+ }
+ if(wasEmpty) {
+ m_loans.begin()->entry()->collection()->addBorrower(m_borrower);
+ Controller::self()->addedBorrower(m_borrower);
+ } else {
+ // don't have to do anything to the document, it just holds a pointer
+ myDebug() << "AddLoansCommand::execute() - modifying an existing borrower! " << endl;
+ Controller::self()->modifiedBorrower(m_borrower);
+ }
+}
+
+void AddLoans::unexecute() {
+ if(!m_borrower) {
+ return;
+ }
+
+ // remove the loans from the borrower
+ for(Data::LoanVec::Iterator loan = m_loans.begin(); loan != m_loans.end(); ++loan) {
+ m_borrower->removeLoan(loan);
+ Data::Document::self()->checkInEntry(loan->entry());
+ Data::EntryVec vec;
+ vec.append(loan->entry());
+ Controller::self()->modifiedEntries(vec);
+ }
+ if(m_addedLoanField) {
+ Data::CollPtr c = m_loans.begin()->entry()->collection();
+ Data::FieldPtr f = c->fieldByName(QString::fromLatin1("loaned"));
+ if(f) {
+ c->removeField(f);
+ Controller::self()->removedField(c, f);
+ }
+ }
+ if(m_addToCalendar) {
+ CalendarHandler::removeLoans(m_loans);
+ }
+ // the borrower object is kept in the document, it's just empty
+ // it won't get saved in the document file
+ // here, just notify everybody that it changed
+ Controller::self()->modifiedBorrower(m_borrower);
+}
+
+QString AddLoans::name() const {
+ return m_loans.count() > 1 ? i18n("Check-out Items")
+ : i18n("Check-out (Entry Title)", "Check-out %1").arg(m_loans.begin()->entry()->title());
+}