diff --git a/src/collectioninterface.cpp b/src/collectioninterface.cpp index d6b536da5ee80bdbfbb8c220b7bcc0d401001fb6..dbdbaf1de5efc71bb8fb913137c904c3a145a9d0 100644 --- a/src/collectioninterface.cpp +++ b/src/collectioninterface.cpp @@ -79,6 +79,12 @@ void CollectionInterface::addChildren(CollectionInterface* c) d_ptr->m_lChildren << c; } + +bool CollectionInterface::add(ItemBase<QObject>* base) +{ + return d_ptr->m_pAdd(base); +} + bool CollectionInterface::save(ItemBase<QObject>* base) { return d_ptr->m_pSave(base); diff --git a/src/collectioninterface.h b/src/collectioninterface.h index 2c8af924842c718c29a0ea619133fe0d07777cbd..a04c95d5f5ea8f2d56404c5ca2e3051b1cd89995 100644 --- a/src/collectioninterface.h +++ b/src/collectioninterface.h @@ -47,6 +47,7 @@ template<typename T> class ItemBase; class LIB_EXPORT CollectionInterface { template<typename T> friend class CollectionMediator; + template<typename T> friend class CollectionManagerInterface; friend class ItemBase<QObject>; public: @@ -191,6 +192,10 @@ public: template<typename T> CollectionEditor<T>* editor() const; + /** + * Add a new element to the backend + */ + bool add (ItemBase<QObject>* base); protected: void addChildren(CollectionInterface* c); diff --git a/src/collectioninterface.hpp b/src/collectioninterface.hpp index c69f1af20bc3a14704630d045870f71bdec85aa1..ae5e4b32ac6f36031789cb67b367015313afc56f 100644 --- a/src/collectioninterface.hpp +++ b/src/collectioninterface.hpp @@ -36,6 +36,7 @@ public: ///Use Qt introspection to make sure casting is valid QMetaObject m_pEditorType; + std::function<bool(ItemBase<QObject>*)> m_pAdd ; std::function<bool(ItemBase<QObject>*)> m_pSave ; std::function<bool(ItemBase<QObject>*)> m_pEdit ; std::function<bool(ItemBase<QObject>*)> m_pRemove ; @@ -51,6 +52,9 @@ d_ptr(new CollectionInterfacePrivate()) d_ptr->m_pEditor = (void*) editor; //The cast is safe because the metatype is checked earlier + d_ptr->m_pAdd = [editor](ItemBase<QObject>* item)->bool { + return editor->addNew(static_cast<T*>(item)); + }; d_ptr->m_pSave = [editor](ItemBase<QObject>* item)->bool { return editor->edit(static_cast<T*>(item)); }; diff --git a/src/collectionmediator.hpp b/src/collectionmediator.hpp index 6349de194f3d408c7b124df50e7812d51e2d7f00..a5e872daa38c421bfd4884ed03b2429496761ba0 100644 --- a/src/collectionmediator.hpp +++ b/src/collectionmediator.hpp @@ -41,7 +41,7 @@ bool CollectionMediator<T>::addItem(T* item) template<typename T> bool CollectionMediator<T>::removeItem(T* item) { - return d_ptr->m_pParent->removeItemCallback(item); + return d_ptr->m_pParent->removeItemCallback(item); //TODO wrong } template<typename T> diff --git a/src/delegates/profilepersisterdelegate.cpp b/src/delegates/profilepersisterdelegate.cpp index 4fec3a5ba5dd00af3b96f1160164579b6d023a2b..dfc40f6a6ba2bacdc09a93859d95fe0052db39c2 100644 --- a/src/delegates/profilepersisterdelegate.cpp +++ b/src/delegates/profilepersisterdelegate.cpp @@ -44,5 +44,5 @@ bool ProfilePersisterDelegate::save(const Person* c) QDir ProfilePersisterDelegate::getProfilesDir() { - return *(new QDir()); + return QDir(); } diff --git a/src/itembase.hpp b/src/itembase.hpp index e3fafe165a6956909ea3202ab131fe2dd12b01dc..6e605e9733b1321cd6045d33bdb545dcbc41345b 100644 --- a/src/itembase.hpp +++ b/src/itembase.hpp @@ -28,7 +28,7 @@ public: }; template<typename Base> -ItemBase<Base>::ItemBase(Base* parent) : Base(parent), d_ptr(new ItemBasePrivate()) +ItemBase<Base>::ItemBase(Base* parent) :Base(parent), d_ptr(new ItemBasePrivate()) { } diff --git a/src/person.cpp b/src/person.cpp index 4aa57a079db6907e967ecde274f87012a448730f..5b68cd67b30d945df29b4816013cc2befd22af53 100644 --- a/src/person.cpp +++ b/src/person.cpp @@ -226,7 +226,7 @@ Person* Person::ContactMethods::contact() const } ///Constructor -Person::Person(CollectionInterface* parent): ItemBase<QObject>(parent?parent->model():TransitionalPersonBackend::instance()->model()), +Person::Person(CollectionInterface* parent): ItemBase<QObject>(nullptr), d_ptr(new PersonPrivate(this)) { setBackend(parent?parent:TransitionalPersonBackend::instance()); diff --git a/src/personmodel.cpp b/src/personmodel.cpp index fa5b6b7af7dfde5d2b0839329b9c8dc520396084..6e16e9b02141de5b2541a67980642bdb12ad9c15 100644 --- a/src/personmodel.cpp +++ b/src/personmodel.cpp @@ -258,9 +258,23 @@ void PersonModel::backendAddedCallback(CollectionInterface* backend) // return d_ptr->m_lBackends; // } -bool PersonModel::addItemCallback(Person* item) +bool PersonModel::addItemCallback(Person* c) { - addPerson(item); + beginInsertRows(QModelIndex(),d_ptr->m_lPersons.size()-1,d_ptr->m_lPersons.size()); + d_ptr->m_lPersons << c; + d_ptr->m_hPersonsByUid[c->uid()] = c; + + //Deprecate the placeholder + if (d_ptr->m_hPlaceholders.contains(c->uid())) { + PersonPlaceHolder* c2 = d_ptr->m_hPlaceholders[c->uid()]; + if (c2) { + c2->merge(c); + d_ptr->m_hPlaceholders[c->uid()] = nullptr; + } + } + endInsertRows(); + emit layoutChanged(); + emit newPersonAdded(c); return true; } @@ -282,21 +296,8 @@ bool PersonModel::addPerson(Person* c) { if (!c) return false; - beginInsertRows(QModelIndex(),d_ptr->m_lPersons.size()-1,d_ptr->m_lPersons.size()); - d_ptr->m_lPersons << c; - d_ptr->m_hPersonsByUid[c->uid()] = c; - - //Deprecate the placeholder - if (d_ptr->m_hPlaceholders.contains(c->uid())) { - PersonPlaceHolder* c2 = d_ptr->m_hPlaceholders[c->uid()]; - if (c2) { - c2->merge(c); - d_ptr->m_hPlaceholders[c->uid()] = nullptr; - } - } - endInsertRows(); - emit layoutChanged(); - emit newPersonAdded(c); + if (backends().size()) //TODO this is wrong, it work for now because profilemodel is [0] + backends()[0]->add(c); return true; } diff --git a/src/profilemodel.cpp b/src/profilemodel.cpp index 7accc25f479654d3548022947e0825214e1da3d6..2cb5b81d44c99204ed011c8e2af46637eec737c2 100644 --- a/src/profilemodel.cpp +++ b/src/profilemodel.cpp @@ -147,6 +147,7 @@ public: QList<Account*> getAccountsForProfile(const QString& id); QList<Node*> m_lProfiles; QHash<QByteArray,Node*> m_hProfileByAccountId; + QVector<Person*> m_lProfilePersons; private: virtual QVector<Person*> items() const override; @@ -187,6 +188,7 @@ public: public Q_SLOTS: void contactChanged(); void save(); + void loadProfiles(); }; @@ -237,6 +239,7 @@ bool ProfileEditor::append(const Person* item) bool ProfileEditor::remove(Person* item) { Q_UNUSED(item) + mediator()->removeItem(item); return false; } @@ -249,6 +252,8 @@ bool ProfileEditor::edit( Person* contact) bool ProfileEditor::addNew( Person* contact) { qDebug() << "Creating new profile" << contact->uid(); + m_lProfilePersons << contact; + mediator()->addItem(contact); save(contact); // load(); //FIXME return true; @@ -256,7 +261,7 @@ bool ProfileEditor::addNew( Person* contact) QVector<Person*> ProfileEditor::items() const { - return QVector<Person*>(); + return m_lProfilePersons; } @@ -362,11 +367,12 @@ void ProfileContentBackend::setupDefaultProfile() orphans << i.key(); } } - qDebug() << "ORPHAN" << orphans.size(); + qDebug() << "ORPHAN" << orphans.size() << m_pEditor << m_pEditor->m_lProfiles.size(); if (orphans.size() && (!m_pDefault)) { qDebug() << "No profile found, creating one"; Person* profile = new Person(this); + PersonModel::instance()->addPerson(profile); profile->setFormattedName(tr("Default")); m_pDefault = new Node ; @@ -377,7 +383,6 @@ void ProfileContentBackend::setupDefaultProfile() ProfileModel::instance()->beginInsertRows(QModelIndex(), m_pEditor->m_lProfiles.size(), m_pEditor->m_lProfiles.size()); m_pEditor->m_lProfiles << m_pDefault; ProfileModel::instance()->endInsertRows(); - PersonModel::instance()->addPerson(profile); } foreach(Account* a, orphans) { @@ -401,12 +406,13 @@ void ProfileContentBackend::addAccount(Node* parent, Account* acc) m_pEditor->m_hProfileByAccountId[acc->id()] = account_pro; } -bool ProfileContentBackend::load() + +void ProfileContentBackend::loadProfiles() { if (ProfilePersisterDelegate::instance()) { m_pEditor->m_lProfiles.clear(); - QDir profilesDir = ProfilePersisterDelegate::instance()->getProfilesDir(); + const QDir profilesDir = ProfilePersisterDelegate::instance()->getProfilesDir(); qDebug() << "Loading vcf from:" << profilesDir; @@ -471,8 +477,12 @@ bool ProfileContentBackend::load() } else { qDebug() << "No ProfilePersistor loaded!"; - return false; } +} + +bool ProfileContentBackend::load() +{ + QTimer::singleShot(0,this,SLOT(loadProfiles())); return true; }