diff --git a/kmymoney/dialogs/settings/ksettingsgeneral.cpp b/kmymoney/dialogs/settings/ksettingsgeneral.cpp --- a/kmymoney/dialogs/settings/ksettingsgeneral.cpp +++ b/kmymoney/dialogs/settings/ksettingsgeneral.cpp @@ -136,8 +136,8 @@ accountsModel->slotObjectRemoved(eMyMoney::File::Object::Account, account.id()); // remove item from accounts' page institutionsModel->slotObjectRemoved(eMyMoney::File::Object::Account, account.id()); // remove item from institutions' page } else { - accountsModel->slotObjectAdded(eMyMoney::File::Object::Account, dynamic_cast(&account)); // add item to accounts' page - institutionsModel->slotObjectAdded(eMyMoney::File::Object::Account, dynamic_cast(&account)); // add item to institutions' page + accountsModel->slotObjectAdded(eMyMoney::File::Object::Account, account.id()); // add item to accounts' page + institutionsModel->slotObjectAdded(eMyMoney::File::Object::Account, account.id()); // add item to institutions' page } } } diff --git a/kmymoney/kmymoney.cpp b/kmymoney/kmymoney.cpp --- a/kmymoney/kmymoney.cpp +++ b/kmymoney/kmymoney.cpp @@ -1024,7 +1024,7 @@ qDebug("--- Starting performance tests ---"); // AccountList - MyMoneyFile::instance()->preloadCache(); +// MyMoneyFile::instance()->preloadCache(); measurement[0] = measurement[1] = 0; timer.start(); for (int i = 0; i < 1000; ++i) { @@ -1039,7 +1039,7 @@ std::cerr << "Average : " << (measurement[0] + measurement[1]) / 1000 << " msec" << std::endl; // Balance of asset account(s) - MyMoneyFile::instance()->preloadCache(); +// MyMoneyFile::instance()->preloadCache(); measurement[0] = measurement[1] = 0; acc = MyMoneyFile::instance()->asset(); for (int i = 0; i < 1000; ++i) { @@ -1052,7 +1052,7 @@ std::cerr << "Average : " << (measurement[0] + measurement[1]) / 1000 << " msec" << std::endl; // total balance of asset account - MyMoneyFile::instance()->preloadCache(); +// MyMoneyFile::instance()->preloadCache(); measurement[0] = measurement[1] = 0; acc = MyMoneyFile::instance()->asset(); for (int i = 0; i < 1000; ++i) { @@ -1065,7 +1065,7 @@ std::cerr << "Average : " << (measurement[0] + measurement[1]) / 1000 << " msec" << std::endl; // Balance of expense account(s) - MyMoneyFile::instance()->preloadCache(); +// MyMoneyFile::instance()->preloadCache(); measurement[0] = measurement[1] = 0; acc = MyMoneyFile::instance()->expense(); for (int i = 0; i < 1000; ++i) { @@ -1078,7 +1078,7 @@ std::cerr << "Average : " << (measurement[0] + measurement[1]) / 1000 << " msec" << std::endl; // total balance of expense account - MyMoneyFile::instance()->preloadCache(); +// MyMoneyFile::instance()->preloadCache(); measurement[0] = measurement[1] = 0; acc = MyMoneyFile::instance()->expense(); timer.start(); @@ -1092,7 +1092,7 @@ std::cerr << "Average : " << (measurement[0] + measurement[1]) / 1000 << " msec" << std::endl; // transaction list - MyMoneyFile::instance()->preloadCache(); +// MyMoneyFile::instance()->preloadCache(); measurement[0] = measurement[1] = 0; if (MyMoneyFile::instance()->asset().accountCount()) { MyMoneyTransactionFilter filter(MyMoneyFile::instance()->asset().accountList()[0]); @@ -1111,7 +1111,7 @@ } // transaction list - MyMoneyFile::instance()->preloadCache(); +// MyMoneyFile::instance()->preloadCache(); measurement[0] = measurement[1] = 0; if (MyMoneyFile::instance()->asset().accountCount()) { MyMoneyTransactionFilter filter(MyMoneyFile::instance()->asset().accountList()[0]); @@ -1128,7 +1128,7 @@ std::cerr << "Total time: " << (measurement[0] + measurement[1]) << " msec" << std::endl; std::cerr << "Average : " << (measurement[0] + measurement[1]) / 100 << " msec" << std::endl; } - MyMoneyFile::instance()->preloadCache(); +// MyMoneyFile::instance()->preloadCache(); } void KMyMoneyApp::slotFileNew() diff --git a/kmymoney/models/accountsmodel.h b/kmymoney/models/accountsmodel.h --- a/kmymoney/models/accountsmodel.h +++ b/kmymoney/models/accountsmodel.h @@ -97,8 +97,8 @@ public Q_SLOTS: void slotReconcileAccount(const MyMoneyAccount &account, const QDate &reconciliationDate, const MyMoneyMoney &endingBalance); - void slotObjectAdded(eMyMoney::File::Object objType, const MyMoneyObject * const obj); - void slotObjectModified(eMyMoney::File::Object objType, const MyMoneyObject * const obj); + void slotObjectAdded(eMyMoney::File::Object objType, const QString &id); + void slotObjectModified(eMyMoney::File::Object objType, const QString &id); void slotObjectRemoved(eMyMoney::File::Object objType, const QString& id); void slotBalanceOrValueChanged(const MyMoneyAccount &account); @@ -160,8 +160,8 @@ void load(); public Q_SLOTS: - void slotObjectAdded(eMyMoney::File::Object objType, const MyMoneyObject * const obj); - void slotObjectModified(eMyMoney::File::Object objType, const MyMoneyObject * const obj); + void slotObjectAdded(eMyMoney::File::Object objType, const QString &id); + void slotObjectModified(eMyMoney::File::Object objType, const QString &id); void slotObjectRemoved(eMyMoney::File::Object objType, const QString& id); private: diff --git a/kmymoney/models/accountsmodel.cpp b/kmymoney/models/accountsmodel.cpp --- a/kmymoney/models/accountsmodel.cpp +++ b/kmymoney/models/accountsmodel.cpp @@ -886,30 +886,28 @@ * Notify the model that an object has been added. An action is performed only if the object is an account. * */ -void AccountsModel::slotObjectAdded(File::Object objType, const MyMoneyObject * const obj) +void AccountsModel::slotObjectAdded(File::Object objType, const QString& id) { if (objType != File::Object::Account) return; - const MyMoneyAccount * const account = dynamic_cast(obj); - if (!account) - return; + const auto account = MyMoneyFile::instance()->account(id); auto favoriteAccountsItem = d->itemFromAccountId(this, favoritesAccountId); - auto parentAccountItem = d->itemFromAccountId(this, account->parentAccountId()); - auto item = d->itemFromAccountId(parentAccountItem, account->id()); + auto parentAccountItem = d->itemFromAccountId(this, account.parentAccountId()); + auto item = d->itemFromAccountId(parentAccountItem, account.id()); if (!item) { - item = new QStandardItem(account->name()); + item = new QStandardItem(account.name()); parentAccountItem->appendRow(item); item->setEditable(false); } // load the sub-accounts if there are any - there could be sub accounts if this is an add operation // that was triggered in slotObjectModified on an already existing account which went trough a hierarchy change - d->loadSubaccounts(item, favoriteAccountsItem, account->accountList()); + d->loadSubaccounts(item, favoriteAccountsItem, account.accountList()); const auto row = item->row(); - d->setAccountData(parentAccountItem, row, *account, d->m_columns); - d->loadPreferredAccount(*account, parentAccountItem, row, favoriteAccountsItem); + d->setAccountData(parentAccountItem, row, account, d->m_columns); + d->loadPreferredAccount(account, parentAccountItem, row, favoriteAccountsItem); checkNetWorth(); checkProfit(); @@ -919,34 +917,33 @@ * Notify the model that an object has been modified. An action is performed only if the object is an account. * */ -void AccountsModel::slotObjectModified(File::Object objType, const MyMoneyObject * const obj) +void AccountsModel::slotObjectModified(File::Object objType, const QString& id) { if (objType != File::Object::Account) return; - const MyMoneyAccount * const account = dynamic_cast(obj); - if (!account) - return; + const auto account = MyMoneyFile::instance()->account(id); + auto favoriteAccountsItem = d->itemFromAccountId(this, favoritesAccountId); - auto accountItem = d->itemFromAccountId(this, account->id()); + auto accountItem = d->itemFromAccountId(this, account.id()); const auto oldAccount = accountItem->data((int)Role::Account).value(); - if (oldAccount.parentAccountId() == account->parentAccountId()) { + if (oldAccount.parentAccountId() == account.parentAccountId()) { // the hierarchy did not change so update the account data auto parentAccountItem = accountItem->parent(); if (!parentAccountItem) parentAccountItem = this->invisibleRootItem(); const auto row = accountItem->row(); - d->setAccountData(parentAccountItem, row, *account, d->m_columns); + d->setAccountData(parentAccountItem, row, account, d->m_columns); // and the child of the favorite item if the account is a favorite account or it's favorite status has just changed - auto favItem = d->itemFromAccountId(favoriteAccountsItem, account->id()); - if (account->value("PreferredAccount") == QLatin1String("Yes")) - d->loadPreferredAccount(*account, parentAccountItem, row, favoriteAccountsItem); + auto favItem = d->itemFromAccountId(favoriteAccountsItem, account.id()); + if (account.value("PreferredAccount") == QLatin1String("Yes")) + d->loadPreferredAccount(account, parentAccountItem, row, favoriteAccountsItem); else if (favItem) favoriteAccountsItem->removeRow(favItem->row()); // it's not favorite anymore } else { // this means that the hierarchy was changed - simulate this with a remove followed by and add operation slotObjectRemoved(File::Object::Account, oldAccount.id()); - slotObjectAdded(File::Object::Account, obj); + slotObjectAdded(File::Object::Account, id); } checkNetWorth(); @@ -1096,8 +1093,7 @@ void InstitutionsModel::load() { // create items for all the institutions - QList institutionList; - d->m_file->institutionList(institutionList); + auto institutionList = d->m_file->institutionList(); MyMoneyInstitution none; none.setName(i18n("Accounts with no institution assigned")); institutionList.append(none); @@ -1128,32 +1124,30 @@ * Notify the model that an object has been added. An action is performed only if the object is an account or an institution. * */ -void InstitutionsModel::slotObjectAdded(File::Object objType, const MyMoneyObject * const obj) +void InstitutionsModel::slotObjectAdded(File::Object objType, const QString& id) { auto modelUtils = static_cast(d); if (objType == File::Object::Institution) { // if an institution was added then add the item which will represent it - const MyMoneyInstitution * const institution = dynamic_cast(obj); - if (!institution) - return; - modelUtils->addInstitutionItem(this, *institution); + const auto institution = MyMoneyFile::instance()->institution(id); + modelUtils->addInstitutionItem(this, institution); } if (objType != File::Object::Account) return; // if an account was added then add the item which will represent it only for real accounts - const MyMoneyAccount * const account = dynamic_cast(obj); + const auto account = MyMoneyFile::instance()->account(id); // nothing to do for root accounts and categories - if (!account || account->parentAccountId().isEmpty() || account->isIncomeExpense()) + if (account.parentAccountId().isEmpty() || account.isIncomeExpense()) return; // load the account into the institution - modelUtils->loadInstitution(this, *account); + modelUtils->loadInstitution(this, account); // load the investment sub-accounts if there are any - there could be sub-accounts if this is an add operation // that was triggered in slotObjectModified on an already existing account which went trough a hierarchy change - const auto sAccounts = account->accountList(); + const auto sAccounts = account.accountList(); if (!sAccounts.isEmpty()) { QList subAccounts; d->m_file->accountList(subAccounts, sAccounts); @@ -1169,37 +1163,35 @@ * Notify the model that an object has been modified. An action is performed only if the object is an account or an institution. * */ -void InstitutionsModel::slotObjectModified(File::Object objType, const MyMoneyObject * const obj) +void InstitutionsModel::slotObjectModified(File::Object objType, const QString& id) { if (objType == File::Object::Institution) { // if an institution was modified then modify the item which represents it - const MyMoneyInstitution * const institution = dynamic_cast(obj); - if (!institution) - return; - auto institutionItem = static_cast(d)->institutionItemFromId(this, institution->id()); - institutionItem->setData(institution->name(), Qt::DisplayRole); - institutionItem->setData(QVariant::fromValue(*institution), (int)Role::Account); - institutionItem->setIcon(institution->pixmap()); + const auto institution = MyMoneyFile::instance()->institution(id); + auto institutionItem = static_cast(d)->institutionItemFromId(this, institution.id()); + institutionItem->setData(institution.name(), Qt::DisplayRole); + institutionItem->setData(QVariant::fromValue(institution), (int)Role::Account); + institutionItem->setIcon(institution.pixmap()); } if (objType != File::Object::Account) return; // if an account was modified then modify the item which represents it - const MyMoneyAccount * const account = dynamic_cast(obj); + const auto account = MyMoneyFile::instance()->account(id); // nothing to do for root accounts, categories and equity accounts since they don't have a representation in this model - if (!account || account->parentAccountId().isEmpty() || account->isIncomeExpense() || account->accountType() == Account::Type::Equity) + if (account.parentAccountId().isEmpty() || account.isIncomeExpense() || account.accountType() == Account::Type::Equity) return; - auto accountItem = d->itemFromAccountId(this, account->id()); + auto accountItem = d->itemFromAccountId(this, account.id()); const auto oldAccount = accountItem->data((int)Role::Account).value(); - if (oldAccount.institutionId() == account->institutionId()) { + if (oldAccount.institutionId() == account.institutionId()) { // the hierarchy did not change so update the account data - d->setAccountData(accountItem->parent(), accountItem->row(), *account, d->m_columns); + d->setAccountData(accountItem->parent(), accountItem->row(), account, d->m_columns); } else { // this means that the hierarchy was changed - simulate this with a remove followed by and add operation slotObjectRemoved(File::Object::Account, oldAccount.id()); - slotObjectAdded(File::Object::Account, obj); + slotObjectAdded(File::Object::Account, id); } } diff --git a/kmymoney/models/equitiesmodel.h b/kmymoney/models/equitiesmodel.h --- a/kmymoney/models/equitiesmodel.h +++ b/kmymoney/models/equitiesmodel.h @@ -48,8 +48,8 @@ static QString getHeaderName(const Column column); public Q_SLOTS: - void slotObjectAdded(eMyMoney::File::Object objType, const MyMoneyObject * const obj); - void slotObjectModified(eMyMoney::File::Object objType, const MyMoneyObject * const obj); + void slotObjectAdded(eMyMoney::File::Object objType, const QString &id); + void slotObjectModified(eMyMoney::File::Object objType, const QString &id); void slotObjectRemoved(eMyMoney::File::Object objType, const QString& id); void slotBalanceOrValueChanged(const MyMoneyAccount &account); diff --git a/kmymoney/models/equitiesmodel.cpp b/kmymoney/models/equitiesmodel.cpp --- a/kmymoney/models/equitiesmodel.cpp +++ b/kmymoney/models/equitiesmodel.cpp @@ -222,58 +222,57 @@ * Notify the model that an object has been added. An action is performed only if the object is an account. * */ -void EquitiesModel::slotObjectAdded(eMyMoney::File::Object objType, const MyMoneyObject * const obj) +void EquitiesModel::slotObjectAdded(eMyMoney::File::Object objType, const QString& id) { // check whether change is about accounts if (objType != eMyMoney::File::Object::Account) return; // check whether change is about either investment or stock account - const auto acc = dynamic_cast(obj); - if (!acc || - (acc->accountType() != eMyMoney::Account::Type::Investment && - acc->accountType() != eMyMoney::Account::Type::Stock)) + const auto acc = MyMoneyFile::instance()->account(id); + if (acc.accountType() != eMyMoney::Account::Type::Investment && + acc.accountType() != eMyMoney::Account::Type::Stock) return; - auto itAcc = d->itemFromId(this, acc->id(), Role::EquityID); + auto itAcc = d->itemFromId(this, id, Role::EquityID); QStandardItem *itParentAcc; - if (acc->accountType() == eMyMoney::Account::Type::Investment) // if it's investment account then its parent is root item + if (acc.accountType() == eMyMoney::Account::Type::Investment) // if it's investment account then its parent is root item itParentAcc = invisibleRootItem(); else // otherwise it's stock account and its parent is investment account - itParentAcc = d->itemFromId(this, acc->parentAccountId(), Role::InvestmentID); + itParentAcc = d->itemFromId(this, acc.parentAccountId(), Role::InvestmentID); // if account doesn't exist in model then add it if (!itAcc) { - itAcc = new QStandardItem(acc->name()); + itAcc = new QStandardItem(acc.name()); itParentAcc->appendRow(itAcc); itAcc->setEditable(false); } - d->setAccountData(itParentAcc, itAcc->row(), *acc, d->m_columns); + d->setAccountData(itParentAcc, itAcc->row(), acc, d->m_columns); } /** * Notify the model that an object has been modified. An action is performed only if the object is an account. * */ -void EquitiesModel::slotObjectModified(eMyMoney::File::Object objType, const MyMoneyObject * const obj) +void EquitiesModel::slotObjectModified(eMyMoney::File::Object objType, const QString& id) { MyMoneyAccount acc; QStandardItem *itAcc; switch (objType) { case eMyMoney::File::Object::Account: { - auto tmpAcc = dynamic_cast(obj); - if (!tmpAcc || tmpAcc->accountType() != eMyMoney::Account::Type::Stock) + auto tmpAcc = MyMoneyFile::instance()->account(id); + if (tmpAcc.accountType() != eMyMoney::Account::Type::Stock) return; - acc = MyMoneyAccount(*tmpAcc); + acc = MyMoneyAccount(tmpAcc); itAcc = d->itemFromId(this, acc.id(), Role::EquityID); break; } case eMyMoney::File::Object::Security: { - auto sec = dynamic_cast(obj); - itAcc = d->itemFromId(this, sec->id(), Role::SecurityID); + auto sec = MyMoneyFile::instance()->security(id); + itAcc = d->itemFromId(this, sec.id(), Role::SecurityID); if (!itAcc) return; const auto idAcc = itAcc->data(Role::EquityID).toString(); @@ -291,7 +290,7 @@ d->setAccountData(itParentAcc, itAcc->row(), acc, d->m_columns); } else { // and if not then reparent slotObjectRemoved(eMyMoney::File::Object::Account, acc.id()); - slotObjectAdded(eMyMoney::File::Object::Account, obj); + slotObjectAdded(eMyMoney::File::Object::Account, id); } } diff --git a/kmymoney/models/ledgermodel.h b/kmymoney/models/ledgermodel.h --- a/kmymoney/models/ledgermodel.h +++ b/kmymoney/models/ledgermodel.h @@ -96,11 +96,11 @@ protected Q_SLOTS: void removeTransaction(eMyMoney::File::Object objType, const QString& id); - void addTransaction (eMyMoney::File::Object objType, const MyMoneyObject * const obj); - void modifyTransaction(eMyMoney::File::Object objType, const MyMoneyObject * const obj); + void addTransaction (eMyMoney::File::Object objType, const QString& id); + void modifyTransaction(eMyMoney::File::Object objType, const QString& id); void removeSchedule (eMyMoney::File::Object objType, const QString& id); - void addSchedule (eMyMoney::File::Object objType, const MyMoneyObject * const obj); - void modifySchedule (eMyMoney::File::Object objType, const MyMoneyObject * const obj); + void addSchedule (eMyMoney::File::Object objType, const QString& id); + void modifySchedule (eMyMoney::File::Object objType, const QString& id); private: Q_DISABLE_COPY(LedgerModel) diff --git a/kmymoney/models/ledgermodel.cpp b/kmymoney/models/ledgermodel.cpp --- a/kmymoney/models/ledgermodel.cpp +++ b/kmymoney/models/ledgermodel.cpp @@ -66,7 +66,7 @@ { MyMoneyFile* file = MyMoneyFile::instance(); - connect(file, &MyMoneyFile::objectAdded, this, static_cast(&LedgerModel::addTransaction)); + connect(file, &MyMoneyFile::objectAdded, this, static_cast(&LedgerModel::addTransaction)); connect(file, &MyMoneyFile::objectModified, this, &LedgerModel::modifyTransaction); connect(file, &MyMoneyFile::objectRemoved, this, &LedgerModel::removeTransaction); @@ -502,37 +502,37 @@ qDebug() << "Loaded" << rowCount() << "elements"; } -void LedgerModel::addTransaction(File::Object objType, const MyMoneyObject * const obj) +void LedgerModel::addTransaction(File::Object objType, const QString& id) { if(objType != File::Object::Transaction) { return; } Q_D(LedgerModel); - qDebug() << "Adding transaction" << obj->id(); + qDebug() << "Adding transaction" << id; - const MyMoneyTransaction * const t = static_cast(obj); + const auto t = MyMoneyFile::instance()->transaction(id); - beginInsertRows(QModelIndex(), rowCount(), rowCount() + t->splitCount() - 1); - foreach (auto s, t->splits()) - d->m_ledgerItems.append(new LedgerTransaction(*t, s)); + beginInsertRows(QModelIndex(), rowCount(), rowCount() + t.splitCount() - 1); + foreach (auto s, t.splits()) + d->m_ledgerItems.append(new LedgerTransaction(t, s)); endInsertRows(); // just make sure we're in sync Q_ASSERT(d->m_ledgerItems.count() == rowCount()); } -void LedgerModel::modifyTransaction(File::Object objType, const MyMoneyObject* const obj) +void LedgerModel::modifyTransaction(File::Object objType, const QString& id) { if(objType != File::Object::Transaction) { return; } Q_D(LedgerModel); - const MyMoneyTransaction * const t = static_cast(obj); + const auto t = MyMoneyFile::instance()->transaction(id); // get indexes of all existing splits for this transaction - QModelIndexList list = match(index(0, 0), (int)Role::TransactionId, obj->id(), -1); + auto list = match(index(0, 0), (int)Role::TransactionId, id, -1); // get list of splits to be stored - QList splits = t->splits(); + auto splits = t.splits(); int lastRowUsed = -1; int firstRowUsed = 99999999; @@ -547,9 +547,9 @@ QModelIndex index = list.takeFirst(); MyMoneySplit split = splits.takeFirst(); // get rid of the old split and store new split - qDebug() << "Modify split in row:" << index.row() << t->id() << split.id(); + qDebug() << "Modify split in row:" << index.row() << t.id() << split.id(); delete d->m_ledgerItems[index.row()]; - d->m_ledgerItems[index.row()] = new LedgerTransaction(*t, split); + d->m_ledgerItems[index.row()] = new LedgerTransaction(t, split); } // inform every one else about the changes @@ -567,7 +567,7 @@ d->m_ledgerItems.insert(lastRowUsed, splits.count(), 0); while(!splits.isEmpty()) { MyMoneySplit split = splits.takeFirst(); - d->m_ledgerItems[lastRowUsed] = new LedgerTransaction(*t, split); + d->m_ledgerItems[lastRowUsed] = new LedgerTransaction(t, split); lastRowUsed++; } endInsertRows(); @@ -616,9 +616,9 @@ } } -void LedgerModel::addSchedule(File::Object objType, const MyMoneyObject*const obj) +void LedgerModel::addSchedule(File::Object objType, const QString& id) { - Q_UNUSED(obj); + Q_UNUSED(id); if(objType != File::Object::Schedule) { return; } @@ -626,9 +626,9 @@ /// @todo implement LedgerModel::addSchedule } -void LedgerModel::modifySchedule(File::Object objType, const MyMoneyObject*const obj) +void LedgerModel::modifySchedule(File::Object objType, const QString& id) { - Q_UNUSED(obj); + Q_UNUSED(id); if(objType != File::Object::Schedule) { return; } diff --git a/kmymoney/models/onlinejobmodel.h b/kmymoney/models/onlinejobmodel.h --- a/kmymoney/models/onlinejobmodel.h +++ b/kmymoney/models/onlinejobmodel.h @@ -60,8 +60,8 @@ public Q_SLOTS: void reloadAll(); - void slotObjectAdded(eMyMoney::File::Object objType, const MyMoneyObject * const obj); - void slotObjectModified(eMyMoney::File::Object objType, const MyMoneyObject * const obj); + void slotObjectAdded(eMyMoney::File::Object objType, const QString &id); + void slotObjectModified(eMyMoney::File::Object objType, const QString &id); void slotObjectRemoved(eMyMoney::File::Object objType, const QString& id); /** @brief Load data from MyMoneyFile */ diff --git a/kmymoney/models/onlinejobmodel.cpp b/kmymoney/models/onlinejobmodel.cpp --- a/kmymoney/models/onlinejobmodel.cpp +++ b/kmymoney/models/onlinejobmodel.cpp @@ -244,21 +244,21 @@ return true; } -void onlineJobModel::slotObjectAdded(eMyMoney::File::Object objType, const MyMoneyObject * const obj) +void onlineJobModel::slotObjectAdded(eMyMoney::File::Object objType, const QString& id) { if (Q_LIKELY(objType != eMyMoney::File::Object::OnlineJob)) return; beginInsertRows(QModelIndex(), rowCount(), rowCount()); - m_jobIdList.append(obj->id()); + m_jobIdList.append(id); endInsertRows(); } -void onlineJobModel::slotObjectModified(eMyMoney::File::Object objType, const MyMoneyObject * const obj) +void onlineJobModel::slotObjectModified(eMyMoney::File::Object objType, const QString& id) { if (Q_LIKELY(objType != eMyMoney::File::Object::OnlineJob)) return; - int row = m_jobIdList.indexOf(obj->id()); + int row = m_jobIdList.indexOf(id); if (row != -1) emit dataChanged(index(row, 0), index(row, columnCount() - 1)); } diff --git a/kmymoney/models/securitiesmodel.h b/kmymoney/models/securitiesmodel.h --- a/kmymoney/models/securitiesmodel.h +++ b/kmymoney/models/securitiesmodel.h @@ -46,8 +46,8 @@ static QString getHeaderName(const Column column); public Q_SLOTS: - void slotObjectAdded(eMyMoney::File::Object objType, const MyMoneyObject * const obj); - void slotObjectModified(eMyMoney::File::Object objType, const MyMoneyObject * const obj); + void slotObjectAdded(eMyMoney::File::Object objType, const QString &id); + void slotObjectModified(eMyMoney::File::Object objType, const QString &id); void slotObjectRemoved(eMyMoney::File::Object objType, const QString& id); private: diff --git a/kmymoney/models/securitiesmodel.cpp b/kmymoney/models/securitiesmodel.cpp --- a/kmymoney/models/securitiesmodel.cpp +++ b/kmymoney/models/securitiesmodel.cpp @@ -193,57 +193,53 @@ * Notify the model that an object has been added. An action is performed only if the object is a security. * */ -void SecuritiesModel::slotObjectAdded(eMyMoney::File::Object objType, const MyMoneyObject * const obj) +void SecuritiesModel::slotObjectAdded(eMyMoney::File::Object objType, const QString& id) { // check whether change is about security if (objType != eMyMoney::File::Object::Security) return; // check that we're about to add security - auto sec = dynamic_cast(obj); - if (!sec) - return; + auto sec = MyMoneyFile::instance()->security(id); - auto itSec = d->itemFromSecurityId(this, sec->id()); + auto itSec = d->itemFromSecurityId(this, id); QStandardItem *node; - if (sec->isCurrency()) + if (sec.isCurrency()) node = d->m_ndCurrencies; else node = d->m_ndSecurities; // if security doesn't exist in model then add it if (!itSec) { - itSec = new QStandardItem(sec->name()); + itSec = new QStandardItem(sec.name()); node->appendRow(itSec); itSec->setEditable(false); } - d->setSecurityData(node, itSec->row(), *sec, d->m_columns); + d->setSecurityData(node, itSec->row(), sec, d->m_columns); } /** * Notify the model that an object has been modified. An action is performed only if the object is a security. * */ -void SecuritiesModel::slotObjectModified(eMyMoney::File::Object objType, const MyMoneyObject * const obj) +void SecuritiesModel::slotObjectModified(eMyMoney::File::Object objType, const QString& id) { if (objType != eMyMoney::File::Object::Security) return; // check that we're about to modify security - auto sec = dynamic_cast(obj); - if (!sec) - return; + auto sec = MyMoneyFile::instance()->security(id); - auto itSec = d->itemFromSecurityId(this, sec->id()); + auto itSec = d->itemFromSecurityId(this, id); QStandardItem *node; - if (sec->isCurrency()) + if (sec.isCurrency()) node = d->m_ndCurrencies; else node = d->m_ndSecurities; - d->setSecurityData(node, itSec->row(), *sec, d->m_columns); + d->setSecurityData(node, itSec->row(), sec, d->m_columns); } /** diff --git a/kmymoney/mymoney/CMakeLists.txt b/kmymoney/mymoney/CMakeLists.txt --- a/kmymoney/mymoney/CMakeLists.txt +++ b/kmymoney/mymoney/CMakeLists.txt @@ -18,7 +18,6 @@ mymoneycostcenter.cpp mymoneyaccount.cpp mymoneyaccountloan.cpp mymoneyreport.cpp mymoneystatement.cpp mymoneyprice.cpp mymoneybudget.cpp - mymoneyobjectcontainer.cpp mymoneyforecast.cpp mymoneybalancecache.cpp onlinejob.cpp @@ -37,7 +36,7 @@ mymoneypayee.h mymoneytag.h mymoneyprice.h mymoneyreport.h mymoneyschedule.h mymoneysecurity.h mymoneysplit.h mymoneystatement.h mymoneytransactionfilter.h mymoneytransaction.h - mymoneyutils.h mymoneybudget.h mymoneyobjectcontainer.h mymoneyforecast.h + mymoneyutils.h mymoneybudget.h mymoneyforecast.h imymoneyprocessingcalendar.h mymoneycostcenter.h mymoneyunittestable.h diff --git a/kmymoney/mymoney/mymoneyenums.h b/kmymoney/mymoney/mymoneyenums.h --- a/kmymoney/mymoney/mymoneyenums.h +++ b/kmymoney/mymoney/mymoneyenums.h @@ -272,7 +272,8 @@ Tag, Schedule, Security, - OnlineJob + OnlineJob, + CostCenter }; /** diff --git a/kmymoney/mymoney/mymoneyfile.h b/kmymoney/mymoney/mymoneyfile.h --- a/kmymoney/mymoney/mymoneyfile.h +++ b/kmymoney/mymoney/mymoneyfile.h @@ -694,14 +694,6 @@ */ MyMoneyInstitution institution(const QString& id) const; - /** - * This method returns a list of the institutions - * inside a MyMoneyFile object - * - * @param list reference to the list. It will be cleared by this method first - */ - void institutionList(QList& list) const; - /** * This method returns a list of the institutions * inside a MyMoneyFile object. This is a convenience method @@ -1516,8 +1508,6 @@ void forceDataChanged(); - void preloadCache(); - /** * This returns @p true if file and online balance of a specific * @p account are matching. Returns false if there is no online balance. @@ -1611,7 +1601,7 @@ * had been added. The data for the new object is contained in * @a obj. */ - void objectAdded(eMyMoney::File::Object objType, const MyMoneyObject * const obj); + void objectAdded(eMyMoney::File::Object objType, const QString& id); /** * This signal is emitted by the engine whenever an object @@ -1629,7 +1619,7 @@ * had been changed. The new state of the object is contained * in @a obj. */ - void objectModified(eMyMoney::File::Object objType, const MyMoneyObject * const obj); + void objectModified(eMyMoney::File::Object objType, const QString& id); /** * This signal is emitted by the engine whenever the balance diff --git a/kmymoney/mymoney/mymoneyfile.cpp b/kmymoney/mymoney/mymoneyfile.cpp --- a/kmymoney/mymoney/mymoneyfile.cpp +++ b/kmymoney/mymoney/mymoneyfile.cpp @@ -46,7 +46,6 @@ #include "mymoneybalancecache.h" #include "mymoneybudget.h" #include "mymoneyprice.h" -#include "mymoneyobjectcontainer.h" #include "mymoneypayee.h" #include "mymoneytag.h" #include "mymoneyschedule.h" @@ -170,16 +169,9 @@ * @param reload reload the object (@c true) or not (@c false). The default is @c true * @see attach, detach */ - void addCacheNotification(const QString& id, bool reload = true) { + void addCacheNotification(const QString& id, const QDate& date) { if (!id.isEmpty()) - m_notificationList[id] = reload; - } - - void addCacheNotification(const QString& id, const QDate& date, bool reload = true) { - if (!id.isEmpty()) { - m_notificationList[id] = reload; m_balanceNotifyList.append(std::make_pair(id, date)); - } } /** @@ -187,7 +179,6 @@ */ void clearCacheNotification() { // reset list to be empty - m_notificationList.clear(); m_balanceNotifyList.clear(); } @@ -196,15 +187,6 @@ * objects mentioned in m_notificationList from the cache. */ void notify() { - QMap::ConstIterator it = m_notificationList.constBegin(); - while (it != m_notificationList.constEnd()) { - if (*it) - m_cache.refresh(it.key()); - else - m_cache.clear(it.key()); - ++it; - } - foreach (const BalanceNotifyList::value_type & i, m_balanceNotifyList) { m_balanceChangedSet += i.first; if (i.second.isValid()) { @@ -267,20 +249,9 @@ * It is also used to emit the objectAdded() and objectModified() signals. * => If one of these signals is used, you must use this cache. */ - MyMoneyObjectContainer m_cache; MyMoneyPriceList m_priceCache; MyMoneyBalanceCache m_balanceCache; - /** - * This member keeps a list of ids to notify after a single - * operation is completed. The boolean is used as follows - * during processing of the list: - * - * false - don't reload the object immediately - * true - reload the object immediately - */ - CacheNotifyList m_notificationList; - /** * This member keeps a list of account ids to notify * after a single operation is completed. The balance cache @@ -322,10 +293,10 @@ public: MyMoneyNotifier(MyMoneyFile::Private* file) { m_file = file; m_file->clearCacheNotification(); - }; + } ~MyMoneyNotifier() { m_file->notify(); - }; + } private: MyMoneyFile::Private* m_file; }; @@ -368,9 +339,7 @@ // and the whole cache d->m_balanceCache.clear(); - d->m_cache.clear(storage); d->m_priceCache.clear(); - preloadCache(); // notify application about new data availability emit beginChangeNotification(); @@ -381,9 +350,8 @@ void MyMoneyFile::detachStorage(MyMoneyStorageMgr* const /* storage */) { d->m_balanceCache.clear(); - d->m_cache.clear(); d->m_priceCache.clear(); - d->m_storage = 0; + d->m_storage = nullptr; } MyMoneyStorageMgr* MyMoneyFile::storage() const @@ -418,7 +386,7 @@ d->checkTransaction(Q_FUNC_INFO); // commit the transaction in the storage - bool changed = d->m_storage->commitTransaction(); + const auto changed = d->m_storage->commitTransaction(); d->m_inTransaction = false; // inform the outside world about the beginning of notifications @@ -427,37 +395,86 @@ // Now it's time to send out some signals to the outside world // First we go through the d->m_changeSet and emit respective // signals about addition, modification and removal of engine objects - QList::const_iterator it = d->m_changeSet.constBegin(); - while (it != d->m_changeSet.constEnd()) { - if ((*it).notificationMode() == File::Mode::Remove) { - emit objectRemoved((*it).objectType(), (*it).id()); - // if there is a balance change recorded for this account remove it since the account itself will be removed - // this can happen when deleting categories that have transactions and the reassign category feature was used - d->m_balanceChangedSet.remove((*it).id()); - } else { - const MyMoneyObject * obj = 0; - MyMoneyTransaction tr; - - switch((*it).objectType()) { - case File::Object::Transaction: - tr = transaction((*it).id()); - obj = &tr; - break; - - default: - obj = d->m_cache.object((*it).id()); - break; - } - if (obj) { - if ((*it).notificationMode() == File::Mode::Add) { - emit objectAdded((*it).objectType(), obj); - - } else { - emit objectModified((*it).objectType(), obj); - } - } + const auto& changes = d->m_changeSet; + for (const auto& change : changes) { + switch (change.notificationMode()) { + case File::Mode::Remove: + emit objectRemoved(change.objectType(), change.id()); + // if there is a balance change recorded for this account remove it since the account itself will be removed + // this can happen when deleting categories that have transactions and the reassign category feature was used + d->m_balanceChangedSet.remove(change.id()); + break; + case File::Mode::Add: + emit objectAdded(change.objectType(), change.id()); +// switch (change.objectType()) { +// case File::Object::Account: +// emit objectAdded(change.objectType(), account(change.id())); +// break; +// case File::Object::Institution: +// emit objectAdded(change.objectType(), institution(change.id())); +// break; +// case File::Object::Payee: +// emit objectAdded(change.objectType(), payee(change.id())); +// break; +// case File::Object::Transaction: +// emit objectAdded(change.objectType(), transaction(change.id())); +// break; +// case File::Object::Tag: +// emit objectAdded(change.objectType(), tag(change.id())); +// break; +// case File::Object::Schedule: +// emit objectAdded(change.objectType(), schedule(change.id())); +// break; +// case File::Object::Security: +// emit objectAdded(change.objectType(), security(change.id())); +// break; +// case File::Object::OnlineJob: +// emit objectAdded(change.objectType(), getOnlineJob(change.id())); +// break; +//// case File::Object::CostCenter: +//// emit objectAdded(change.objectType(), costcenter(change.id())); +//// break; +// default: +// throw MYMONEYEXCEPTION(QString::fromLatin1("Unrecognized object id passed: %1").arg(change.id())); +// break; +// } + break; + case File::Mode::Modify: + emit objectModified(change.objectType(), change.id()); +// switch (change.objectType()) { +// case File::Object::Account: +// emit objectModified(change.objectType(), account(change.id())); +// break; +// case File::Object::Institution: +// emit objectModified(change.objectType(), institution(change.id())); +// break; +// case File::Object::Payee: +// emit objectModified(change.objectType(), payee(change.id())); +// break; +// case File::Object::Transaction: +// emit objectModified(change.objectType(), transaction(change.id())); +// break; +// case File::Object::Tag: +// emit objectModified(change.objectType(), tag(change.id())); +// break; +// case File::Object::Schedule: +// emit objectModified(change.objectType(), schedule(change.id())); +// break; +// case File::Object::Security: +// emit objectModified(change.objectType(), security(change.id())); +// break; +// case File::Object::OnlineJob: +// emit objectModified(change.objectType(), getOnlineJob(change.id())); +// break; +//// case File::Object::CostCenter: +//// emit objectModified(change.objectType(), costcenter(change.id())); +//// break; +// default: +// throw MYMONEYEXCEPTION(QString::fromLatin1("Unrecognized object id passed: %1").arg(change.id())); +// break; +// } + break; } - ++it; } // we're done with the change set, so we clear it @@ -466,26 +483,25 @@ // now send out the balanceChanged signal for all those // accounts for which we have an indication about a possible // change. - foreach (const QString& id, d->m_balanceChangedSet) { + const auto& balanceChanges = d->m_balanceChangedSet; + for (const auto& id : balanceChanges) { // if we notify about balance change we don't need to notify about value change // for the same account since a balance change implies a value change d->m_valueChangedSet.remove(id); - const auto acc = d->m_cache.account(id); - emit balanceChanged(acc); + emit balanceChanged(account(id)); } d->m_balanceChangedSet.clear(); // now notify about the remaining value changes - foreach (const QString& id, d->m_valueChangedSet) { - const auto acc = d->m_cache.account(id); - emit valueChanged(acc); - } + const auto& m_valueChanges = d->m_valueChangedSet; + for (const auto& id : m_valueChanges) + emit valueChanged(account(id)); + d->m_valueChangedSet.clear(); // as a last action, send out the global dataChanged signal - if (changed) { + if (changed) emit dataChanged(); - } // inform the outside world about the end of notifications emit endChangeNotification(); @@ -497,7 +513,6 @@ d->m_storage->rollbackTransaction(); d->m_inTransaction = false; - preloadCache(); d->m_balanceChangedSet.clear(); d->m_valueChangedSet.clear(); d->m_changeSet.clear(); @@ -515,15 +530,7 @@ d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - d->m_storage->addInstitution(institution); - - // The notifier mechanism only refreshes the cache but does not - // load new objects. So we simply force loading of the new one here - d->m_cache.preloadInstitution(institution); - d->m_changeSet += MyMoneyNotification(File::Mode::Add, institution); } @@ -531,12 +538,7 @@ { d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - d->m_storage->modifyInstitution(institution); - - d->addCacheNotification(institution.id()); d->m_changeSet += MyMoneyNotification(File::Mode::Modify, institution); } @@ -548,7 +550,8 @@ // now check the splits bool loanAccountAffected = false; - foreach (const auto split, transaction.splits()) { + const auto splits1 = transaction.splits(); + for (const auto& split : splits1) { // the following line will throw an exception if the // account does not exist auto acc = MyMoneyFile::account(split.accountId()); @@ -563,7 +566,8 @@ // change transfer splits between asset/liability and loan accounts // into amortization splits if (loanAccountAffected) { - foreach (const auto split, transaction.splits()) { + const auto splits = transaction.splits(); + for (const auto& split : splits) { if (split.action() == MyMoneySplit::actionName(eMyMoney::Split::Action::Transfer)) { auto acc = MyMoneyFile::account(split.accountId()); @@ -584,11 +588,9 @@ // scan the splits again to update notification list // and mark all accounts that are referenced - foreach (const auto split, tr.splits()) { + const auto splits2 = tr.splits(); + foreach (const auto& split, splits2) d->addCacheNotification(split.accountId(), tr.postDate()); - d->addCacheNotification(split.payeeId()); - //FIXME-ALEX Do I need to add d->addCacheNotification(split.tagList()); ?? - } // make sure the value is rounded to the accounts precision fixSplitPrecision(tCopy); @@ -597,11 +599,9 @@ d->m_storage->modifyTransaction(tCopy); // and mark all accounts that are referenced - foreach (const auto split, tCopy.splits()) { + const auto splits3 = tCopy.splits(); + for (const auto& split : splits3) d->addCacheNotification(split.accountId(), tCopy.postDate()); - d->addCacheNotification(split.payeeId()); - //FIXME-ALEX Do I need to add d->addCacheNotification(split.tagList()); ?? - } d->m_changeSet += MyMoneyNotification(File::Mode::Modify, transaction); } @@ -632,9 +632,6 @@ !account.isLiquidAsset() && !acc.isLiquidAsset()) throw MYMONEYEXCEPTION("Unable to change account type"); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - // if the account was moved to another institution, we notify // the old one as well as the new one and the structure change if (acc.institutionId() != account.institutionId()) { @@ -651,13 +648,9 @@ modifyInstitution(inst); // modifyInstitution updates d->m_changeSet already } - d->addCacheNotification(acc.institutionId()); - d->addCacheNotification(account.institutionId()); } d->m_storage->modifyAccount(account); - - d->addCacheNotification(account.id()); d->m_changeSet += MyMoneyNotification(File::Mode::Modify, account); } @@ -679,20 +672,11 @@ if (parent.accountType() == Account::Type::Investment && !acc.isInvest()) throw MYMONEYEXCEPTION("Unable to reparent non-stock to investment account"); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - // keep a notification of the current parent MyMoneyAccount curParent = account(acc.parentAccountId()); - d->addCacheNotification(curParent.id()); - d->m_storage->reparentAccount(acc, parent); - // and also keep one for the account itself and the new parent - d->addCacheNotification(acc.id()); - d->addCacheNotification(parent.id()); - d->m_changeSet += MyMoneyNotification(File::Mode::Modify, curParent); d->m_changeSet += MyMoneyNotification(File::Mode::Modify, parent); d->m_changeSet += MyMoneyNotification(File::Mode::Modify, acc); @@ -703,21 +687,25 @@ MyMoneyInstitution MyMoneyFile::institution(const QString& id) const { - return d->m_cache.institution(id); + return d->m_storage->institution(id); } MyMoneyAccount MyMoneyFile::account(const QString& id) const { - return d->m_cache.account(id); + if (Q_UNLIKELY(id.isEmpty())) // FIXME: Stop requesting accounts with empty id + return MyMoneyAccount(); + + return d->m_storage->account(id); } MyMoneyAccount MyMoneyFile::subAccountByName(const MyMoneyAccount& acc, const QString& name) const { static MyMoneyAccount nullAccount; - foreach (const auto sAccount, acc.accountList()) { - const auto sacc = account(sAccount); - if (sacc.name() == name) + const auto accounts = acc.accountList(); + for (const auto& acc : accounts) { + const auto sacc = account(acc); + if (sacc.name().compare(name) == 0) return sacc; } return nullAccount; @@ -725,7 +713,7 @@ MyMoneyAccount MyMoneyFile::accountByName(const QString& name) const { - return d->m_cache.accountByName(name); + return d->m_storage->accountByName(name); } void MyMoneyFile::removeTransaction(const MyMoneyTransaction& transaction) @@ -739,12 +727,12 @@ MyMoneyTransaction tr = MyMoneyFile::transaction(transaction.id()); // scan the splits again to update notification list - foreach (const auto split, tr.splits()) { + const auto splits = tr.splits(); + for (const auto& split : splits) { auto acc = account(split.accountId()); if (acc.isClosed()) throw MYMONEYEXCEPTION(i18n("Cannot remove transaction that references a closed account.")); d->addCacheNotification(split.accountId(), tr.postDate()); - d->addCacheNotification(split.payeeId()); //FIXME-ALEX Do I need to add d->addCacheNotification(split.tagList()); ?? } @@ -782,12 +770,8 @@ { d->checkTransaction(Q_FUNC_INFO); - MyMoneyNotifier notifier(d); - auto acc = account(id); d->m_storage->setAccountName(id, name); - d->addCacheNotification(id); - d->m_changeSet += MyMoneyNotification(File::Mode::Modify, acc); } @@ -814,18 +798,12 @@ throw MYMONEYEXCEPTION("Unable to remove account with active splits"); } - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - // collect all sub-ordinate accounts for notification - foreach (const QString& id, acc.accountList()) { - d->addCacheNotification(id); - const auto acc = MyMoneyFile::account(id); - d->m_changeSet += MyMoneyNotification(File::Mode::Modify, acc); - } + const auto accounts = acc.accountList(); + for (const auto& id : accounts) + d->m_changeSet += MyMoneyNotification(File::Mode::Modify, MyMoneyFile::account(id)); + // don't forget the parent and a possible institution - d->addCacheNotification(parent.id()); - d->addCacheNotification(account.institutionId()); if (!institution.id().isEmpty()) { institution.removeAccountId(account.id()); @@ -836,8 +814,6 @@ d->m_storage->removeAccount(acc); - d->addCacheNotification(acc.id(), false); - d->m_cache.clear(acc.id()); d->m_balanceCache.clear(acc.id()); d->m_changeSet += MyMoneyNotification(File::Mode::Modify, parent); @@ -874,7 +850,6 @@ } // make sure to remove the item from the cache - d->m_cache.clear(a.id()); removeAccount(a); } } @@ -884,7 +859,7 @@ if (level > 100) throw MYMONEYEXCEPTION("Too deep recursion in [MyMoneyFile::hasOnlyUnusedAccounts]!"); // process all accounts in the list and test if they have transactions assigned - foreach (const auto sAccount, account_list) { + for (const auto& sAccount : account_list) { if (transactionCount(sAccount) != 0) return false; // the current account has a transaction assigned if (!hasOnlyUnusedAccounts(account(sAccount).accountList(), level + 1)) @@ -898,27 +873,22 @@ { d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - - QList::ConstIterator it_a; MyMoneyInstitution inst = MyMoneyFile::institution(institution.id()); bool blocked = signalsBlocked(); blockSignals(true); - for (it_a = inst.accountList().constBegin(); it_a != inst.accountList().constEnd(); ++it_a) { - auto acc = account(*it_a); - acc.setInstitutionId(QString()); - modifyAccount(acc); - d->m_changeSet += MyMoneyNotification(File::Mode::Modify, acc); + const auto accounts = inst.accountList(); + for (const auto& acc : accounts) { + auto a = account(acc); + a.setInstitutionId(QString()); + modifyAccount(a); + d->m_changeSet += MyMoneyNotification(File::Mode::Modify, a); } blockSignals(blocked); d->m_storage->removeInstitution(institution); d->m_changeSet += MyMoneyNotification(File::Mode::Remove, institution); - - d->addCacheNotification(institution.id(), false); } void MyMoneyFile::createAccount(MyMoneyAccount& newAccount, MyMoneyAccount& parentAccount, MyMoneyAccount& brokerageAccount, MyMoneyMoney openingBal) @@ -1065,9 +1035,6 @@ if (!account.isInvest() && parent.accountType() == Account::Type::Investment) throw MYMONEYEXCEPTION("Investment account can only have stock accounts as children"); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - // if an institution is set, verify that it exists if (account.institutionId().length() != 0) { // check the presence of the institution. if it @@ -1104,14 +1071,7 @@ institution.addAccountId(account.id()); d->m_storage->modifyInstitution(institution); d->m_changeSet += MyMoneyNotification(File::Mode::Modify, institution); - d->addCacheNotification(institution.id()); } - - // The notifier mechanism only refreshes the cache but does not - // load new objects. So we simply force loading of the new one here - d->m_cache.preloadAccount(account); - - d->addCacheNotification(parent.id()); } MyMoneyTransaction MyMoneyFile::createOpeningBalanceTransaction(const MyMoneyAccount& acc, const MyMoneyMoney& balance) @@ -1310,7 +1270,8 @@ // now check the splits auto loanAccountAffected = false; - foreach (const auto split, transaction.splits()) { + const auto splits1 = transaction.splits(); + for (const auto& split : splits1) { // the following line will throw an exception if the // account does not exist or is one of the standard accounts auto acc = MyMoneyFile::account(split.accountId()); @@ -1350,11 +1311,9 @@ d->m_storage->addTransaction(transaction); // scan the splits again to update notification list - foreach (const auto split, transaction.splits()) { + const auto splits2 = transaction.splits(); + for (const auto& split : splits2) d->addCacheNotification(split.accountId(), transaction.postDate()); - d->addCacheNotification(split.payeeId()); - //FIXME-ALEX Do I need to add d->addCacheNotification(split.tagList()); ?? - } d->m_changeSet += MyMoneyNotification(File::Mode::Add, transaction); } @@ -1377,41 +1336,30 @@ { d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - d->m_storage->addPayee(payee); - - // The notifier mechanism only refreshes the cache but does not - // load new objects. So we simply force loading of the new one here - d->m_cache.preloadPayee(payee); - d->m_changeSet += MyMoneyNotification(File::Mode::Add, payee); } MyMoneyPayee MyMoneyFile::payee(const QString& id) const { - return d->m_cache.payee(id); + if (Q_UNLIKELY(id.isEmpty())) + return MyMoneyPayee(); + + return d->m_storage->payee(id); } MyMoneyPayee MyMoneyFile::payeeByName(const QString& name) const { d->checkStorage(); - return d->m_cache.payee(d->m_storage->payeeByName(name).id()); + return d->m_storage->payeeByName(name); } void MyMoneyFile::modifyPayee(const MyMoneyPayee& payee) { d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - - d->addCacheNotification(payee.id()); - d->m_storage->modifyPayee(payee); - d->m_changeSet += MyMoneyNotification(File::Mode::Modify, payee); } @@ -1420,14 +1368,7 @@ d->checkTransaction(Q_FUNC_INFO); // FIXME we need to make sure, that the payee is not referenced anymore - - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - d->m_storage->removePayee(payee); - - d->addCacheNotification(payee.id(), false); - d->m_changeSet += MyMoneyNotification(File::Mode::Remove, payee); } @@ -1435,41 +1376,27 @@ { d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - d->m_storage->addTag(tag); - - // The notifier mechanism only refreshes the cache but does not - // load new objects. So we simply force loading of the new one here - d->m_cache.preloadTag(tag); - d->m_changeSet += MyMoneyNotification(File::Mode::Add, tag); } MyMoneyTag MyMoneyFile::tag(const QString& id) const { - return d->m_cache.tag(id); + return d->m_storage->tag(id); } MyMoneyTag MyMoneyFile::tagByName(const QString& name) const { d->checkStorage(); - return d->m_cache.tag(d->m_storage->tagByName(name).id()); + return d->m_storage->tagByName(name); } void MyMoneyFile::modifyTag(const MyMoneyTag& tag) { d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - - d->addCacheNotification(tag.id()); - d->m_storage->modifyTag(tag); - d->m_changeSet += MyMoneyNotification(File::Mode::Modify, tag); } @@ -1478,21 +1405,14 @@ d->checkTransaction(Q_FUNC_INFO); // FIXME we need to make sure, that the tag is not referenced anymore - - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - d->m_storage->removeTag(tag); - - d->addCacheNotification(tag.id(), false); - d->m_changeSet += MyMoneyNotification(File::Mode::Remove, tag); } void MyMoneyFile::accountList(QList& list, const QStringList& idlist, const bool recursive) const { if (idlist.isEmpty()) { - d->m_cache.account(list); + d->m_storage->accountList(list); #if 0 // TODO: I have no idea what this was good for, but it caused the networth report @@ -1514,7 +1434,7 @@ } else { QList::ConstIterator it; QList list_a; - d->m_cache.account(list_a); + d->m_storage->accountList(list_a); for (it = list_a.constBegin(); it != list_a.constEnd(); ++it) { if (!isStandardAccount((*it).id())) { @@ -1529,16 +1449,9 @@ } } -void MyMoneyFile::institutionList(QList& list) const -{ - d->m_cache.institution(list); -} - QList MyMoneyFile::institutionList() const { - QList list; - institutionList(list); - return list; + return d->m_storage->institutionList(); } // general get functions @@ -1553,9 +1466,6 @@ { d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - d->m_storage->setUser(user); } @@ -1593,35 +1503,35 @@ { d->checkStorage(); - return d->m_cache.account(stdAccNames[stdAccLiability]); + return account(stdAccNames[stdAccLiability]); } MyMoneyAccount MyMoneyFile::asset() const { d->checkStorage(); - return d->m_cache.account(stdAccNames[stdAccAsset]); + return account(stdAccNames[stdAccAsset]); } MyMoneyAccount MyMoneyFile::expense() const { d->checkStorage(); - return d->m_cache.account(stdAccNames[stdAccExpense]); + return account(stdAccNames[stdAccExpense]); } MyMoneyAccount MyMoneyFile::income() const { d->checkStorage(); - return d->m_cache.account(stdAccNames[stdAccIncome]); + return account(stdAccNames[stdAccIncome]); } MyMoneyAccount MyMoneyFile::equity() const { d->checkStorage(); - return d->m_cache.account(stdAccNames[stdAccEquity]); + return account(stdAccNames[stdAccEquity]); } unsigned int MyMoneyFile::transactionCount(const QString& account) const @@ -1744,23 +1654,18 @@ QList MyMoneyFile::transactionList(MyMoneyTransactionFilter& filter) const { - QList list; - transactionList(list, filter); - return list; + d->checkStorage(); + return d->m_storage->transactionList(filter); } QList MyMoneyFile::payeeList() const { - QList list; - d->m_cache.payee(list); - return list; + return d->m_storage->payeeList(); } QList MyMoneyFile::tagList() const { - QList list; - d->m_cache.tag(list); - return list; + return d->m_storage->tagList(); } QString MyMoneyFile::accountToCategory(const QString& accountId, bool includeStandardAccounts) const @@ -1852,9 +1757,6 @@ { d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - d->m_storage->setValue(key, val); } @@ -1862,9 +1764,6 @@ { d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - d->m_storage->deletePair(key); } @@ -1872,25 +1771,18 @@ { d->checkTransaction(Q_FUNC_INFO); - foreach (const auto split, sched.transaction().splits()) { + const auto splits = sched.transaction().splits(); + for (const auto& split : splits) { // the following line will throw an exception if the // account does not exist or is one of the standard accounts - auto acc = MyMoneyFile::account(split.accountId()); + const auto acc = account(split.accountId()); if (acc.id().isEmpty()) throw MYMONEYEXCEPTION("Cannot add split with no account assigned"); if (isStandardAccount(split.accountId())) throw MYMONEYEXCEPTION("Cannot add split referencing standard account"); } - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - d->m_storage->addSchedule(sched); - - // The notifier mechanism only refreshes the cache but does not - // load new objects. So we simply force loading of the new one here - d->m_cache.preloadSchedule(sched); - d->m_changeSet += MyMoneyNotification(File::Mode::Add, sched); } @@ -1908,12 +1800,7 @@ throw MYMONEYEXCEPTION("Cannot store split referencing standard account"); } - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - d->m_storage->modifySchedule(sched); - - d->addCacheNotification(sched.id()); d->m_changeSet += MyMoneyNotification(File::Mode::Modify, sched); } @@ -1921,18 +1808,13 @@ { d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - d->m_storage->removeSchedule(sched); - - d->addCacheNotification(sched.id(), false); d->m_changeSet += MyMoneyNotification(File::Mode::Remove, sched); } MyMoneySchedule MyMoneyFile::schedule(const QString& id) const { - return d->m_cache.schedule(id); + return d->m_storage->schedule(id); } QList MyMoneyFile::scheduleList( @@ -1988,9 +1870,6 @@ // check that we have a storage object d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - // get the current list of accounts accountList(list); // add the standard accounts @@ -2109,8 +1988,6 @@ rc << i18n(" New parent account is the top level account '%1'.", toplevel.name()); (*it_a).setParentAccountId(toplevel.id()); - d->addCacheNotification((*it_a).id()); - // make sure to rebuild the sub-accounts of the top account if (accountRebuild.contains(toplevel.id()) == 0) accountRebuild << toplevel.id(); @@ -2181,7 +2058,6 @@ if (!(d->m_storage->account((*it_a).id()) == (*it_a))) { try { d->m_storage->modifyAccount(*it_a, true); - d->addCacheNotification((*it_a).id()); } catch (const MyMoneyException &) { rc << i18n(" * Unable to update account data in engine."); return rc; @@ -2221,7 +2097,6 @@ if (accountRebuild.contains((*it_a).id())) { try { d->m_storage->modifyAccount(*it_a, true); - d->addCacheNotification((*it_a).id()); } catch (const MyMoneyException &) { rc << i18n(" * Unable to update account data for account %1 in engine", (*it_a).name()); } @@ -2247,13 +2122,13 @@ } // Fix the transactions - QList tList; MyMoneyTransactionFilter filter; filter.setReportAllSplits(false); - d->m_storage->transactionList(tList, filter); + const auto tList = d->m_storage->transactionList(filter); // Generate the list of interest accounts - foreach (const auto transaction, tList) { - foreach (const auto split, transaction.splits()) { + for (const auto& transaction : tList) { + const auto splits = transaction.splits(); + for (const auto& split : splits) { if (split.action() == MyMoneySplit::actionName(eMyMoney::Split::Action::Interest)) interestAccounts[split.accountId()] = true; } @@ -2267,12 +2142,13 @@ << Account::Type::Liability; QSet reportedUnsupportedAccounts; - for (it_t = tList.begin(); it_t != tList.end(); ++it_t) { - MyMoneyTransaction t = (*it_t); + for (const auto& transaction : tList) { + MyMoneyTransaction t = transaction; bool tChanged = false; QDate accountOpeningDate; QStringList accountList; - foreach (const auto split, t.splits()) { + const auto splits = t.splits(); + foreach (const auto split, splits) { bool sChanged = false; MyMoneySplit s = split; if (payeeConversionMap.find(split.payeeId()) != payeeConversionMap.end()) { @@ -2731,15 +2607,7 @@ { d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - d->m_storage->addSecurity(security); - - // The notifier mechanism only refreshes the cache but does not - // load new objects. So we simply force loading of the new one here - d->m_cache.preloadSecurity(security); - d->m_changeSet += MyMoneyNotification(File::Mode::Add, security); } @@ -2747,13 +2615,7 @@ { d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - d->m_storage->modifySecurity(security); - - d->addCacheNotification(security.id()); - d->m_changeSet += MyMoneyNotification(File::Mode::Modify, security); } @@ -2763,22 +2625,16 @@ // FIXME check that security is not referenced by other object - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - d->m_storage->removeSecurity(security); - - d->addCacheNotification(security.id(), false); - d->m_changeSet += MyMoneyNotification(File::Mode::Remove, security); } MyMoneySecurity MyMoneyFile::security(const QString& id) const { - if (id.isEmpty()) + if (Q_UNLIKELY(id.isEmpty())) return baseCurrency(); - return d->m_cache.security(id); + return d->m_storage->security(id); } QList MyMoneyFile::securityList() const @@ -2792,15 +2648,7 @@ { d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - d->m_storage->addCurrency(currency); - - // The notifier mechanism only refreshes the cache but does not - // load new objects. So we simply force loading of the new one here - d->m_cache.preloadSecurity(currency); - d->m_changeSet += MyMoneyNotification(File::Mode::Add, currency); } @@ -2808,17 +2656,11 @@ { d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - // force reload of base currency object if (currency.id() == d->m_baseCurrency.id()) d->m_baseCurrency.clearId(); d->m_storage->modifyCurrency(currency); - - d->addCacheNotification(currency.id()); - d->m_changeSet += MyMoneyNotification(File::Mode::Modify, currency); } @@ -2826,19 +2668,12 @@ { d->checkTransaction(Q_FUNC_INFO); - if (currency.id() == d->m_baseCurrency.id()) { + if (currency.id() == d->m_baseCurrency.id()) throw MYMONEYEXCEPTION("Cannot delete base currency."); - } // FIXME check that security is not referenced by other object - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - d->m_storage->removeCurrency(currency); - - d->addCacheNotification(currency.id(), false); - d->m_changeSet += MyMoneyNotification(File::Mode::Remove, currency); } @@ -2847,12 +2682,10 @@ if (id.isEmpty()) return baseCurrency(); - const MyMoneySecurity& curr = d->m_cache.security(id); - if (curr.id().isEmpty()) { - QString msg; - msg = QString("Currency '%1' not found.").arg(id); - throw MYMONEYEXCEPTION(msg); - } + const auto curr = d->m_storage->currency(id); + if (curr.id().isEmpty()) + throw MYMONEYEXCEPTION(QString::fromLatin1("Currency '%1' not found.").arg(id)); + return curr; } @@ -3117,9 +2950,6 @@ // make sure the currency exists MyMoneySecurity c = currency(curr.id()); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - if (c.id() != d->m_baseCurrency.id()) { setValue("kmm-baseCurrency", curr.id()); // force reload of base currency cache @@ -3134,12 +2964,8 @@ d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - // store the account's which are affected by this price regarding their value d->priceChanged(*this, price); - d->m_storage->addPrice(price); } @@ -3147,12 +2973,8 @@ { d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - // store the account's which are affected by this price regarding their value d->priceChanged(*this, price); - d->m_storage->removePrice(price); } @@ -3224,14 +3046,12 @@ bool MyMoneyFile::hasAccount(const QString& id, const QString& name) const { - auto acc = d->m_cache.account(id); - auto rc = false; - foreach (const auto sAccount, acc.accountList()) { - auto a = d->m_cache.account(sAccount); - if (a.name() == name) - rc = true; + const auto accounts = account(id).accountList(); + for (const auto& acc : accounts) { + if (account(acc).name().compare(name) == 0) + return true; } - return rc; + return false; } QList MyMoneyFile::reportList() const @@ -3245,9 +3065,6 @@ { d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - d->m_storage->addReport(report); } @@ -3255,12 +3072,7 @@ { d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - d->m_storage->modifyReport(report); - - d->addCacheNotification(report.id()); } unsigned MyMoneyFile::countReports() const @@ -3280,11 +3092,8 @@ void MyMoneyFile::removeReport(const MyMoneyReport& report) { d->checkTransaction(Q_FUNC_INFO); - MyMoneyNotifier notifier(d); d->m_storage->removeReport(report); - - d->addCacheNotification(report.id(), false); } @@ -3299,9 +3108,6 @@ { d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - d->m_storage->addBudget(budget); } @@ -3316,12 +3122,7 @@ { d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - d->m_storage->modifyBudget(budget); - - d->addCacheNotification(budget.id()); } unsigned MyMoneyFile::countBudgets() const @@ -3342,22 +3143,14 @@ { d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); - d->m_storage->removeBudget(budget); - - d->addCacheNotification(budget.id(), false); } void MyMoneyFile::addOnlineJob(onlineJob& job) { d->checkTransaction(Q_FUNC_INFO); - // clear all changed objects from cache - MyMoneyNotifier notifier(d); d->m_storage->addOnlineJob(job); - d->m_cache.preloadOnlineJob(job); d->m_changeSet += MyMoneyNotification(File::Mode::Add, job); } @@ -3366,7 +3159,6 @@ d->checkTransaction(Q_FUNC_INFO); d->m_storage->modifyOnlineJob(job); d->m_changeSet += MyMoneyNotification(File::Mode::Modify, job); - d->addCacheNotification(job.id()); } onlineJob MyMoneyFile::getOnlineJob(const QString &jobId) const @@ -3396,11 +3188,9 @@ d->checkTransaction(Q_FUNC_INFO); // clear all changed objects from cache - MyMoneyNotifier notifier(d); if (job.isLocked()) { return; } - d->addCacheNotification(job.id(), false); d->m_changeSet += MyMoneyNotification(File::Mode::Remove, job); d->m_storage->removeOnlineJob(job); } @@ -3552,7 +3342,6 @@ void MyMoneyFile::clearCache() { d->checkStorage(); - d->m_cache.clear(); d->m_balanceCache.clear(); } @@ -3561,24 +3350,6 @@ emit dataChanged(); } -void MyMoneyFile::preloadCache() -{ - d->checkStorage(); - - d->m_cache.clear(); - QList a_list; - d->m_storage->accountList(a_list); - d->m_cache.preloadAccount(a_list); - d->m_cache.preloadPayee(d->m_storage->payeeList()); - d->m_cache.preloadTag(d->m_storage->tagList()); - d->m_cache.preloadInstitution(d->m_storage->institutionList()); - d->m_cache.preloadSecurity(d->m_storage->securityList() + - d->m_storage->currencyList()); - d->m_cache.preloadSchedule(d->m_storage->scheduleList(QString(), Schedule::Type::Any, Schedule::Occurrence::Any, Schedule::PaymentType::Any, - QDate(), QDate(), false)); - d->m_cache.preloadOnlineJob(d->m_storage->onlineJobList()); -} - bool MyMoneyFile::isTransfer(const MyMoneyTransaction& t) const { auto rc = false; diff --git a/kmymoney/mymoney/mymoneyobjectcontainer.h b/kmymoney/mymoney/mymoneyobjectcontainer.h deleted file mode 100644 --- a/kmymoney/mymoney/mymoneyobjectcontainer.h +++ /dev/null @@ -1,108 +0,0 @@ -/*************************************************************************** - mymoneyobjectcontainer.h - ------------------- - copyright : (C) 2006 by Thomas Baumgart - email : ipwizard@users.sourceforge.net - ***************************************************************************/ - -/*************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ - -#ifndef MYMONEYOBJECTCONTAINER_H -#define MYMONEYOBJECTCONTAINER_H - -// ---------------------------------------------------------------------------- -// QT Includes - -#include -#include -#include - -// ---------------------------------------------------------------------------- -// Project Includes - -#include "kmm_mymoney_export.h" - -/** - * @author Thomas Baumgart - */ - -/** - * This class represents a generic container for all MyMoneyObject derived objects. - */ -class MyMoneyStorageMgr; -class MyMoneyObject; -class MyMoneyInstitution; -class MyMoneyAccount; -class MyMoneySecurity; -class MyMoneyPayee; -class MyMoneyTag; -class MyMoneySchedule; -class MyMoneyCostCenter; -class onlineJob; -class KMM_MYMONEY_EXPORT MyMoneyObjectContainer : public QObject -{ - Q_OBJECT -public: - MyMoneyObjectContainer(); - ~MyMoneyObjectContainer(); - - MyMoneyAccount account(const QString& id); - MyMoneyPayee payee(const QString& id); - MyMoneyTag tag(const QString& id); - MyMoneySecurity security(const QString& id); - MyMoneyInstitution institution(const QString& id); - MyMoneySchedule schedule(const QString& id); - MyMoneyCostCenter costCenter(const QString& id); - - void account(QList& list); - void payee(QList& list); - void tag(QList& list); - void institution(QList& list); - void costCenter(QList& list); - - void preloadAccount(const QList& list); - void preloadPayee(const QList& list); - void preloadTag(const QList& list); - void preloadInstitution(const QList& list); - void preloadSecurity(const QList& list); - void preloadSchedule(const QList& list); - void preloadOnlineJob(const QList& list); - void preloadCostCenter(const QList& list); - - void preloadAccount(const MyMoneyAccount& account); - void preloadSecurity(const MyMoneySecurity& security); - void preloadPayee(const MyMoneyPayee& payee); - void preloadTag(const MyMoneyTag& tag); - void preloadInstitution(const MyMoneyInstitution& institution); - void preloadSchedule(const MyMoneySchedule& schedule); - void preloadOnlineJob(const onlineJob& job); - - void clear(const QString& id); - void clear(MyMoneyStorageMgr* storage = 0); - - MyMoneyAccount accountByName(const QString& name) const; - - const MyMoneyObject * object(const QString& id) const; - - /** - * This method refreshes an already existing object in the container - * with a copy from the engine. The object is identified by its @a id. - * If the object is unknown or the @a id is empty, nothing is done. - */ - void refresh(const QString& id); - -private: - struct Private; - Private * const d; -}; - -#endif - - diff --git a/kmymoney/mymoney/mymoneyobjectcontainer.cpp b/kmymoney/mymoney/mymoneyobjectcontainer.cpp deleted file mode 100644 --- a/kmymoney/mymoney/mymoneyobjectcontainer.cpp +++ /dev/null @@ -1,397 +0,0 @@ -/*************************************************************************** - mymoneyobjectcontainer.cpp - ------------------- - copyright : (C) 2006 by Thomas Baumagrt - email : ipwizard@users.sourceforge.net - ***************************************************************************/ - -/*************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ - -#include "mymoneyobjectcontainer.h" - -// ---------------------------------------------------------------------------- -// QT Includes - -// ---------------------------------------------------------------------------- -// Project Includes - -#include "mymoney/onlinejob.h" - -#include "mymoneystoragemgr.h" -#include "mymoneyinstitution.h" -#include "mymoneyaccount.h" -#include "mymoneysecurity.h" -#include "mymoneyschedule.h" -#include "mymoneypayee.h" -#include "mymoneytag.h" -#include "mymoneycostcenter.h" - -struct MyMoneyObjectContainer::Private { - Private(MyMoneyObjectContainer *p) : - storage(0), - pub(p) { - } - - void assignFraction(MyMoneyAccount* acc) { - if (acc != 0 && acc->fraction() == -1) { - const MyMoneySecurity& sec = pub->security(acc->currencyId()); - acc->fraction(sec); - } - } - - template - const MyMoneyObject * getObject(const CacheType& map, const QString& id) { - typename CacheType::const_iterator it = map.find(id); - if (it != map.end()) { - return (*it); - } - return 0; - } - - template - bool refreshObject(const QString& id, QHash &hash, ObjFactory f) { - typename QHash::iterator it = hash.find(id); - if (it != hash.end()) { - delete *it; - const ObjType& t = (storage->*f)(id); - *it = new ObjType(t); - return true; - } - return false; - } - - template - bool clearObject(CacheType& map, const QString& id) { - typename CacheType::iterator it = map.find(id); - if (it != map.end()) { - delete(*it); - map.erase(it); - return true; - } - return false; - } - - template - void clearCache(CacheType& map) { - // delete all objects - qDeleteAll(map); - // then delete the pointers to them - map.clear(); - } - - template - void listMethodImpl(QList& list, const QHash &hash) { - for (typename QHash::const_iterator it = hash.begin(); it != hash.end(); ++it) { - list.append(*it.value()); - } - } - - template - void preloadMethodImpl(const ObjType& obj, QHash &hash) { - const QString &objId = obj.id(); - typename QHash::iterator it = hash.find(objId); - if (it != hash.end()) { - delete hash.value(objId); - } - hash[objId] = new ObjType(obj); - } - - template - void preloadListMethodImpl(const QList& list, QHash &hash) { - for (typename QList::const_iterator it = list.begin(); it != list.end(); ++it) - preloadMethodImpl(*it, hash); - } - - template - const ObjType& objectAccessMethodImpl(const QString& id, QHash &hash, ObjFactory f) { - static ObjType nullElement; - if (id.isEmpty()) - return nullElement; - typename QHash::const_iterator it = hash.constFind(id); - if (it == hash.constEnd()) { - /* not found, need to load from engine */ - const ObjType &x = (storage->*f)(id); - hash[id] = new ObjType(x); - return *hash.value(id); - } - return *(*it); - } - - QHash accountCache; - QHash payeeCache; - QHash tagCache; - QHash securityCache; - QHash institutionCache; - QHash scheduleCache; - QHash onlineJobCache; - QHash costCenterCache; - - MyMoneyStorageMgr* storage; - MyMoneyObjectContainer *pub; -}; - -MyMoneyObjectContainer::MyMoneyObjectContainer() : d(new Private(this)) -{ -} - -MyMoneyObjectContainer::~MyMoneyObjectContainer() -{ - clear(); - delete d; -} - -void MyMoneyObjectContainer::clear(MyMoneyStorageMgr* storage) -{ - d->clearCache(d->accountCache); - d->clearCache(d->payeeCache); - d->clearCache(d->tagCache); - d->clearCache(d->securityCache); - d->clearCache(d->institutionCache); - d->clearCache(d->scheduleCache); - d->clearCache(d->onlineJobCache); - d->clearCache(d->costCenterCache); - - if (storage) - d->storage = storage; -} - -void MyMoneyObjectContainer::clear(const QString& id) -{ - if (d->clearObject(d->accountCache, id)) - return; - if (d->clearObject(d->payeeCache, id)) - return; - if (d->clearObject(d->tagCache, id)) - return; - if (d->clearObject(d->securityCache, id)) - return; - if (d->clearObject(d->institutionCache, id)) - return; - if (d->clearObject(d->scheduleCache, id)) - return; - if (d->clearObject(d->onlineJobCache, id)) - return; - if (d->clearObject(d->costCenterCache, id)) - return; - qWarning("Ooops, should clear an unknown object with id '%s'", qPrintable(id)); -} - -const MyMoneyObject * MyMoneyObjectContainer::object(const QString& id) const -{ - const MyMoneyObject * obj = 0; - if ((obj = d->getObject(d->accountCache, id))) - return obj; - if ((obj = d->getObject(d->payeeCache, id))) - return obj; - if ((obj = d->getObject(d->tagCache, id))) - return obj; - if ((obj = d->getObject(d->securityCache, id))) - return obj; - if ((obj = d->getObject(d->institutionCache, id))) - return obj; - if ((obj = d->getObject(d->scheduleCache, id))) - return obj; - if ((obj = d->getObject(d->onlineJobCache, id))) - return obj; - if ((obj = d->getObject(d->costCenterCache, id))) - return obj; - qWarning("Ooops, should get an unknown object with id '%s'", qPrintable(id)); - return 0; -} - -void MyMoneyObjectContainer::account(QList& list) -{ - QHash::const_iterator it; - for (it = d->accountCache.constBegin(); it != d->accountCache.constEnd(); ++it) { - const MyMoneyAccount* node = *it; - if (node) { - d->assignFraction(const_cast(node)); - list.append(*node); - } - } -} - -MyMoneyAccount MyMoneyObjectContainer::account(const QString& id) -{ - static MyMoneyAccount nullElement; - - auto acc = d->accountCache.value(id, &nullElement); - - if (id.isEmpty()) { - return *acc; - } else if (acc->id().isEmpty()) { - /* not found, need to load from engine */ - acc = new MyMoneyAccount(d->storage->account(id)); - d->accountCache.insert(id, acc); - } - - d->assignFraction(const_cast(acc)); - return *acc; -} - -MyMoneyAccount MyMoneyObjectContainer::accountByName(const QString& name) const -{ - static MyMoneyAccount nullElement; - QHash::const_iterator it; - for (it = d->accountCache.constBegin(); it != d->accountCache.constEnd(); ++it) { - const MyMoneyAccount* node = *it; - if (node && node->name() == name) { - return **it; - } - } - return nullElement; -} - -void MyMoneyObjectContainer::refresh(const QString& id) -{ - if (id.isEmpty()) - return; - - if (d->refreshObject(id, d->accountCache, &MyMoneyStorageMgr::account)) - return; - if (d->refreshObject(id, d->payeeCache, &MyMoneyStorageMgr::payee)) - return; - if (d->refreshObject(id, d->tagCache, &MyMoneyStorageMgr::tag)) - return; - if (d->refreshObject(id, d->institutionCache, &MyMoneyStorageMgr::institution)) - return; - if (d->refreshObject(id, d->scheduleCache, &MyMoneyStorageMgr::schedule)) - return; - if (d->refreshObject(id, d->onlineJobCache, &MyMoneyStorageMgr::getOnlineJob)) - return; - if (d->refreshObject(id, d->costCenterCache, &MyMoneyStorageMgr::costCenter)) - return; - - // special handling of securities - QHash::iterator it = d->securityCache.find(id); - if (it != d->securityCache.end()) { - delete *it; - const MyMoneySecurity& s = d->storage->security(id); - if (s.id().isEmpty()) { - const MyMoneySecurity& c = d->storage->currency(id); - *it = new MyMoneySecurity(c); - } else { - *it = new MyMoneySecurity(s); - } - return; - } - - qWarning("Ooops, should refresh an unknown object with id '%s'", qPrintable(id)); -} - -MyMoneyPayee MyMoneyObjectContainer::payee(const QString& id) -{ - return d->objectAccessMethodImpl(id, d->payeeCache, &MyMoneyStorageMgr::payee); -} - -MyMoneyTag MyMoneyObjectContainer::tag(const QString& id) -{ - return d->objectAccessMethodImpl(id, d->tagCache, &MyMoneyStorageMgr::tag); -} - -MyMoneySecurity MyMoneyObjectContainer::security(const QString& id) -{ - return d->objectAccessMethodImpl(id, d->securityCache, &MyMoneyStorageMgr::security); -} - -MyMoneyInstitution MyMoneyObjectContainer::institution(const QString& id) -{ - return d->objectAccessMethodImpl(id, d->institutionCache, &MyMoneyStorageMgr::institution); -} - -MyMoneySchedule MyMoneyObjectContainer::schedule(const QString& id) -{ - return d->objectAccessMethodImpl(id, d->scheduleCache, &MyMoneyStorageMgr::schedule); -} - -void MyMoneyObjectContainer::preloadAccount(const QList& list) -{ - d->preloadListMethodImpl(list, d->accountCache); -} - -void MyMoneyObjectContainer::preloadPayee(const QList& list) -{ - d->preloadListMethodImpl(list, d->payeeCache); -} - -void MyMoneyObjectContainer::preloadTag(const QList& list) -{ - d->preloadListMethodImpl(list, d->tagCache); -} - -void MyMoneyObjectContainer::preloadInstitution(const QList& list) -{ - d->preloadListMethodImpl(list, d->institutionCache); -} - -void MyMoneyObjectContainer::preloadSecurity(const QList& list) -{ - d->preloadListMethodImpl(list, d->securityCache); -} - -void MyMoneyObjectContainer::preloadSchedule(const QList& list) -{ - d->preloadListMethodImpl(list, d->scheduleCache); -} - -void MyMoneyObjectContainer::preloadOnlineJob(const QList< onlineJob >& list) -{ - d->preloadListMethodImpl(list, d->onlineJobCache); -} - -void MyMoneyObjectContainer::preloadAccount(const MyMoneyAccount& account) -{ - d->preloadMethodImpl(account, d->accountCache); -} - -void MyMoneyObjectContainer::preloadSecurity(const MyMoneySecurity& security) -{ - d->preloadMethodImpl(security, d->securityCache); -} - -void MyMoneyObjectContainer::preloadPayee(const MyMoneyPayee& payee) -{ - d->preloadMethodImpl(payee, d->payeeCache); -} - -void MyMoneyObjectContainer::preloadTag(const MyMoneyTag& tag) -{ - d->preloadMethodImpl(tag, d->tagCache); -} - -void MyMoneyObjectContainer::preloadInstitution(const MyMoneyInstitution& institution) -{ - d->preloadMethodImpl(institution, d->institutionCache); -} - -void MyMoneyObjectContainer::preloadSchedule(const MyMoneySchedule& schedule) -{ - d->preloadMethodImpl(schedule, d->scheduleCache); -} - -void MyMoneyObjectContainer::preloadOnlineJob(const onlineJob& job) -{ - d->preloadMethodImpl(job, d->onlineJobCache); -} - -void MyMoneyObjectContainer::payee(QList& list) -{ - d->listMethodImpl(list, d->payeeCache); -} - -void MyMoneyObjectContainer::tag(QList& list) -{ - d->listMethodImpl(list, d->tagCache); -} - -void MyMoneyObjectContainer::institution(QList& list) -{ - d->listMethodImpl(list, d->institutionCache); -} diff --git a/kmymoney/mymoney/storage/mymoneystoragemgr.h b/kmymoney/mymoney/storage/mymoneystoragemgr.h --- a/kmymoney/mymoney/storage/mymoneystoragemgr.h +++ b/kmymoney/mymoney/storage/mymoneystoragemgr.h @@ -119,6 +119,17 @@ */ MyMoneyAccount account(const QString& id) const; + /** + * This method is used to retrieve the id to a corresponding + * name of an account. + * An exception will be thrown upon error conditions. + * + * @param name QString reference to name of account + * + * @return MyMoneyAccount reference to object of account + */ + MyMoneyAccount accountByName(const QString& name) const; + /** * This method is used to check whether a given * account id references one of the standard accounts or not. diff --git a/kmymoney/mymoney/storage/mymoneystoragemgr.cpp b/kmymoney/mymoney/storage/mymoneystoragemgr.cpp --- a/kmymoney/mymoney/storage/mymoneystoragemgr.cpp +++ b/kmymoney/mymoney/storage/mymoneystoragemgr.cpp @@ -139,6 +139,23 @@ throw MYMONEYEXCEPTION(msg); } +MyMoneyAccount MyMoneyStorageMgr::accountByName(const QString& name) const +{ + Q_D(const MyMoneyStorageMgr); + if (name.isEmpty()) + return MyMoneyAccount(); + + QMap::ConstIterator it_a; + + for (it_a = d->m_accountList.begin(); it_a != d->m_accountList.end(); ++it_a) { + if ((*it_a).name() == name) { + return *it_a; + } + } + + throw MYMONEYEXCEPTION("Unknown account '" + name + '\''); +} + void MyMoneyStorageMgr::accountList(QList& list) const { Q_D(const MyMoneyStorageMgr); @@ -830,7 +847,8 @@ Q_D(const MyMoneyStorageMgr); list.clear(); - for (const auto& transaction : d->m_transactionList) { + const auto& transactions = d->m_transactionList; + for (const auto& transaction : transactions) { // This code is used now. It adds the transaction to the list for // each matching split exactly once. This allows to show information // about different splits in the same register view (e.g. search result) @@ -927,7 +945,6 @@ */ // new implementation if the above code does not work anymore - QList list; auto acc = d->m_accountList[account]; MyMoneyTransactionFilter filter; @@ -937,7 +954,7 @@ else filter.addAccount(account); - transactionList(list, filter); + const auto list = transactionList(filter); if (idx < 0 || idx >= static_cast(list.count())) throw MYMONEYEXCEPTION("Unknown idx for transaction"); @@ -1444,6 +1461,12 @@ return it.value(); } + // FIXME: in places where a currency is needed, a currency method should be called even if the currency is in fact a security + it = d->m_currencyList.find(id); + if (it != d->m_currencyList.end()) { + return it.value(); + } + return MyMoneySecurity(); } diff --git a/kmymoney/mymoney/storage/mymoneystoragemgr_p.h b/kmymoney/mymoney/storage/mymoneystoragemgr_p.h --- a/kmymoney/mymoney/storage/mymoneystoragemgr_p.h +++ b/kmymoney/mymoney/storage/mymoneystoragemgr_p.h @@ -235,13 +235,12 @@ { Q_Q(const MyMoneyStorageMgr); MyMoneyMoney balance; - QList list; MyMoneyTransactionFilter filter; filter.setDateFilter(QDate(), date); filter.setReportAllSplits(false); - q->transactionList(list, filter); + const auto list = q->transactionList(filter); for (const auto& transaction : list) { const auto splits = transaction.splits(); for (const auto& split : splits) { diff --git a/kmymoney/mymoney/storage/mymoneystoragexml.cpp b/kmymoney/mymoney/storage/mymoneystoragexml.cpp --- a/kmymoney/mymoney/storage/mymoneystoragexml.cpp +++ b/kmymoney/mymoney/storage/mymoneystoragexml.cpp @@ -788,8 +788,7 @@ { MyMoneyTransactionFilter filter; filter.setReportAllSplits(false); - QList list; - m_storage->transactionList(list, filter); + const auto list = m_storage->transactionList(filter); transactions.setAttribute(attrNames[anCount], list.count()); QList::ConstIterator it; diff --git a/kmymoney/mymoney/tests/mymoneyfile-test.h b/kmymoney/mymoney/tests/mymoneyfile-test.h --- a/kmymoney/mymoney/tests/mymoneyfile-test.h +++ b/kmymoney/mymoney/tests/mymoneyfile-test.h @@ -107,8 +107,8 @@ void testEmptyFilter(); private Q_SLOTS: - void objectAdded(eMyMoney::File::Object type, const MyMoneyObject * const obj); - void objectModified(eMyMoney::File::Object type, const MyMoneyObject * const obj); + void objectAdded(eMyMoney::File::Object type, const QString &id); + void objectModified(eMyMoney::File::Object type, const QString &id); void objectRemoved(eMyMoney::File::Object type, const QString& id); void balanceChanged(const MyMoneyAccount& account); void valueChanged(const MyMoneyAccount& account); diff --git a/kmymoney/mymoney/tests/mymoneyfile-test.cpp b/kmymoney/mymoney/tests/mymoneyfile-test.cpp --- a/kmymoney/mymoney/tests/mymoneyfile-test.cpp +++ b/kmymoney/mymoney/tests/mymoneyfile-test.cpp @@ -46,10 +46,10 @@ QTEST_GUILESS_MAIN(MyMoneyFileTest) -void MyMoneyFileTest::objectAdded(eMyMoney::File::Object type, const MyMoneyObject * const obj) +void MyMoneyFileTest::objectAdded(eMyMoney::File::Object type, const QString& id) { Q_UNUSED(type); - m_objectsAdded += obj->id(); + m_objectsAdded += id; } void MyMoneyFileTest::objectRemoved(eMyMoney::File::Object type, const QString& id) @@ -58,10 +58,10 @@ m_objectsRemoved += id; } -void MyMoneyFileTest::objectModified(eMyMoney::File::Object type, const MyMoneyObject * const obj) +void MyMoneyFileTest::objectModified(eMyMoney::File::Object type, const QString& id) { Q_UNUSED(type); - m_objectsModified += obj->id(); + m_objectsModified += id; } void MyMoneyFileTest::clearObjectLists() @@ -88,11 +88,11 @@ { m = MyMoneyFile::instance(); - connect(m, SIGNAL(objectAdded(eMyMoney::File::Object,MyMoneyObject*const)), this, SLOT(objectAdded(eMyMoney::File::Object,MyMoneyObject*const))); - connect(m, SIGNAL(objectRemoved(eMyMoney::File::Object,QString)), this, SLOT(objectRemoved(eMyMoney::File::Object,QString))); - connect(m, SIGNAL(objectModified(eMyMoney::File::Object,MyMoneyObject*const)), this, SLOT(objectModified(eMyMoney::File::Object,MyMoneyObject*const))); - connect(m, SIGNAL(balanceChanged(MyMoneyAccount)), this, SLOT(balanceChanged(MyMoneyAccount))); - connect(m, SIGNAL(valueChanged(MyMoneyAccount)), this, SLOT(valueChanged(MyMoneyAccount))); + connect(m, &MyMoneyFile::objectAdded, this, &MyMoneyFileTest::objectAdded); + connect(m, &MyMoneyFile::objectRemoved, this, &MyMoneyFileTest::objectRemoved); + connect(m, &MyMoneyFile::objectModified, this, &MyMoneyFileTest::objectModified); + connect(m, &MyMoneyFile::balanceChanged, this, &MyMoneyFileTest::balanceChanged); + connect(m, &MyMoneyFile::valueChanged, this, &MyMoneyFileTest::valueChanged); } // this method will be called before each testfunction diff --git a/kmymoney/plugins/sql/tests/mymoneydatabasemgr-test.cpp b/kmymoney/plugins/sql/tests/mymoneydatabasemgr-test.cpp --- a/kmymoney/plugins/sql/tests/mymoneydatabasemgr-test.cpp +++ b/kmymoney/plugins/sql/tests/mymoneydatabasemgr-test.cpp @@ -679,7 +679,7 @@ ft.commit(); QVERIFY(ch.value("Key") == "Value"); - MyMoneyFile::instance()->preloadCache(); + // MyMoneyFile::instance()->preloadCache(); QVERIFY(m->expense().accountCount() == 3); QVERIFY(m->account(ex1.id()).accountCount() == 1); QVERIFY(ex3.parentAccountId() == stdAccNames[stdAccExpense]); @@ -689,7 +689,7 @@ m->reparentAccount(ex3, ex1); ft.commit(); //} - MyMoneyFile::instance()->preloadCache(); + // MyMoneyFile::instance()->preloadCache(); QVERIFY(m->expense().accountCount() == 2); QVERIFY(m->account(ex1.id()).accountCount() == 2); QVERIFY(ex3.parentAccountId() == ex1.id()); @@ -1211,7 +1211,7 @@ // now really remove an account try { - MyMoneyFile::instance()->preloadCache(); + // MyMoneyFile::instance()->preloadCache(); i = m->institution("I000001"); QVERIFY(i.accountCount() == 0); @@ -1440,7 +1440,7 @@ unexpectedException(e); } - MyMoneyFile::instance()->preloadCache(); + // MyMoneyFile::instance()->preloadCache(); try { QVERIFY(m->liability().name() == "Verbindlichkeiten"); diff --git a/kmymoney/views/kmymoneyview.cpp b/kmymoney/views/kmymoneyview.cpp --- a/kmymoney/views/kmymoneyview.cpp +++ b/kmymoney/views/kmymoneyview.cpp @@ -1147,7 +1147,6 @@ Models::instance()->securitiesModel(), &SecuritiesModel::slotObjectRemoved); // inform everyone about new data - MyMoneyFile::instance()->preloadCache(); MyMoneyFile::instance()->forceDataChanged(); // views can wait since they are going to be refresed in slotRefreshViews diff --git a/kmymoney/wizards/newaccountwizard/kinstitutionpage.cpp b/kmymoney/wizards/newaccountwizard/kinstitutionpage.cpp --- a/kmymoney/wizards/newaccountwizard/kinstitutionpage.cpp +++ b/kmymoney/wizards/newaccountwizard/kinstitutionpage.cpp @@ -71,7 +71,7 @@ d_func()->ui->m_institutionComboBox->clear(); d->m_list.clear(); - MyMoneyFile::instance()->institutionList(d->m_list); + d->m_list = MyMoneyFile::instance()->institutionList(); qSort(d->m_list); QList::const_iterator it_l; diff --git a/kmymoney/wizards/newloanwizard/keditloanwizard.cpp b/kmymoney/wizards/newloanwizard/keditloanwizard.cpp --- a/kmymoney/wizards/newloanwizard/keditloanwizard.cpp +++ b/kmymoney/wizards/newloanwizard/keditloanwizard.cpp @@ -503,9 +503,8 @@ QString institution = d->ui->m_loanAttributesPage->ui->m_qcomboboxInstitutions->currentText(); if (institution != i18n("(No Institution)")) { - QList list; - file->institutionList(list); - Q_FOREACH(const MyMoneyInstitution& testInstitution, list) { + const auto list = file->institutionList(); + for (const auto& testInstitution : list) { if (testInstitution.name() == institution) { acc.setInstitutionId(testInstitution.id()); break; diff --git a/kmymoney/wizards/newloanwizard/loanattributeswizardpage.cpp b/kmymoney/wizards/newloanwizard/loanattributeswizardpage.cpp --- a/kmymoney/wizards/newloanwizard/loanattributeswizardpage.cpp +++ b/kmymoney/wizards/newloanwizard/loanattributeswizardpage.cpp @@ -56,8 +56,7 @@ try { MyMoneyFile *file = MyMoneyFile::instance(); - QList list; - file->institutionList(list); + auto list = file->institutionList(); qSort(list); Q_FOREACH(const MyMoneyInstitution &institution, list) {