Skip to content
Snippets Groups Projects
Commit 28b4fecf authored by Emmanuel Lepage Vallee's avatar Emmanuel Lepage Vallee
Browse files

Make the Profile collection partially async to avoid an init race

Refs #65367
parent 0790af2a
No related branches found
No related tags found
No related merge requests found
...@@ -79,6 +79,12 @@ void CollectionInterface::addChildren(CollectionInterface* c) ...@@ -79,6 +79,12 @@ void CollectionInterface::addChildren(CollectionInterface* c)
d_ptr->m_lChildren << c; d_ptr->m_lChildren << c;
} }
bool CollectionInterface::add(ItemBase<QObject>* base)
{
return d_ptr->m_pAdd(base);
}
bool CollectionInterface::save(ItemBase<QObject>* base) bool CollectionInterface::save(ItemBase<QObject>* base)
{ {
return d_ptr->m_pSave(base); return d_ptr->m_pSave(base);
......
...@@ -47,6 +47,7 @@ template<typename T> class ItemBase; ...@@ -47,6 +47,7 @@ template<typename T> class ItemBase;
class LIB_EXPORT CollectionInterface class LIB_EXPORT CollectionInterface
{ {
template<typename T> friend class CollectionMediator; template<typename T> friend class CollectionMediator;
template<typename T> friend class CollectionManagerInterface;
friend class ItemBase<QObject>; friend class ItemBase<QObject>;
public: public:
...@@ -191,6 +192,10 @@ public: ...@@ -191,6 +192,10 @@ public:
template<typename T> template<typename T>
CollectionEditor<T>* editor() const; CollectionEditor<T>* editor() const;
/**
* Add a new element to the backend
*/
bool add (ItemBase<QObject>* base);
protected: protected:
void addChildren(CollectionInterface* c); void addChildren(CollectionInterface* c);
......
...@@ -36,6 +36,7 @@ public: ...@@ -36,6 +36,7 @@ public:
///Use Qt introspection to make sure casting is valid ///Use Qt introspection to make sure casting is valid
QMetaObject m_pEditorType; QMetaObject m_pEditorType;
std::function<bool(ItemBase<QObject>*)> m_pAdd ;
std::function<bool(ItemBase<QObject>*)> m_pSave ; std::function<bool(ItemBase<QObject>*)> m_pSave ;
std::function<bool(ItemBase<QObject>*)> m_pEdit ; std::function<bool(ItemBase<QObject>*)> m_pEdit ;
std::function<bool(ItemBase<QObject>*)> m_pRemove ; std::function<bool(ItemBase<QObject>*)> m_pRemove ;
...@@ -51,6 +52,9 @@ d_ptr(new CollectionInterfacePrivate()) ...@@ -51,6 +52,9 @@ d_ptr(new CollectionInterfacePrivate())
d_ptr->m_pEditor = (void*) editor; d_ptr->m_pEditor = (void*) editor;
//The cast is safe because the metatype is checked earlier //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 { d_ptr->m_pSave = [editor](ItemBase<QObject>* item)->bool {
return editor->edit(static_cast<T*>(item)); return editor->edit(static_cast<T*>(item));
}; };
......
...@@ -41,7 +41,7 @@ bool CollectionMediator<T>::addItem(T* item) ...@@ -41,7 +41,7 @@ bool CollectionMediator<T>::addItem(T* item)
template<typename T> template<typename T>
bool CollectionMediator<T>::removeItem(T* item) 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> template<typename T>
......
...@@ -44,5 +44,5 @@ bool ProfilePersisterDelegate::save(const Person* c) ...@@ -44,5 +44,5 @@ bool ProfilePersisterDelegate::save(const Person* c)
QDir ProfilePersisterDelegate::getProfilesDir() QDir ProfilePersisterDelegate::getProfilesDir()
{ {
return *(new QDir()); return QDir();
} }
...@@ -226,7 +226,7 @@ Person* Person::ContactMethods::contact() const ...@@ -226,7 +226,7 @@ Person* Person::ContactMethods::contact() const
} }
///Constructor ///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)) d_ptr(new PersonPrivate(this))
{ {
setBackend(parent?parent:TransitionalPersonBackend::instance()); setBackend(parent?parent:TransitionalPersonBackend::instance());
......
...@@ -258,9 +258,23 @@ void PersonModel::backendAddedCallback(CollectionInterface* backend) ...@@ -258,9 +258,23 @@ void PersonModel::backendAddedCallback(CollectionInterface* backend)
// return d_ptr->m_lBackends; // 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; return true;
} }
...@@ -282,21 +296,8 @@ bool PersonModel::addPerson(Person* c) ...@@ -282,21 +296,8 @@ bool PersonModel::addPerson(Person* c)
{ {
if (!c) if (!c)
return false; return false;
beginInsertRows(QModelIndex(),d_ptr->m_lPersons.size()-1,d_ptr->m_lPersons.size()); if (backends().size()) //TODO this is wrong, it work for now because profilemodel is [0]
d_ptr->m_lPersons << c; backends()[0]->add(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; return true;
} }
......
...@@ -147,6 +147,7 @@ public: ...@@ -147,6 +147,7 @@ public:
QList<Account*> getAccountsForProfile(const QString& id); QList<Account*> getAccountsForProfile(const QString& id);
QList<Node*> m_lProfiles; QList<Node*> m_lProfiles;
QHash<QByteArray,Node*> m_hProfileByAccountId; QHash<QByteArray,Node*> m_hProfileByAccountId;
QVector<Person*> m_lProfilePersons;
private: private:
virtual QVector<Person*> items() const override; virtual QVector<Person*> items() const override;
...@@ -187,6 +188,7 @@ public: ...@@ -187,6 +188,7 @@ public:
public Q_SLOTS: public Q_SLOTS:
void contactChanged(); void contactChanged();
void save(); void save();
void loadProfiles();
}; };
...@@ -237,6 +239,7 @@ bool ProfileEditor::append(const Person* item) ...@@ -237,6 +239,7 @@ bool ProfileEditor::append(const Person* item)
bool ProfileEditor::remove(Person* item) bool ProfileEditor::remove(Person* item)
{ {
Q_UNUSED(item) Q_UNUSED(item)
mediator()->removeItem(item);
return false; return false;
} }
...@@ -249,6 +252,8 @@ bool ProfileEditor::edit( Person* contact) ...@@ -249,6 +252,8 @@ bool ProfileEditor::edit( Person* contact)
bool ProfileEditor::addNew( Person* contact) bool ProfileEditor::addNew( Person* contact)
{ {
qDebug() << "Creating new profile" << contact->uid(); qDebug() << "Creating new profile" << contact->uid();
m_lProfilePersons << contact;
mediator()->addItem(contact);
save(contact); save(contact);
// load(); //FIXME // load(); //FIXME
return true; return true;
...@@ -256,7 +261,7 @@ bool ProfileEditor::addNew( Person* contact) ...@@ -256,7 +261,7 @@ bool ProfileEditor::addNew( Person* contact)
QVector<Person*> ProfileEditor::items() const QVector<Person*> ProfileEditor::items() const
{ {
return QVector<Person*>(); return m_lProfilePersons;
} }
...@@ -362,11 +367,12 @@ void ProfileContentBackend::setupDefaultProfile() ...@@ -362,11 +367,12 @@ void ProfileContentBackend::setupDefaultProfile()
orphans << i.key(); orphans << i.key();
} }
} }
qDebug() << "ORPHAN" << orphans.size(); qDebug() << "ORPHAN" << orphans.size() << m_pEditor << m_pEditor->m_lProfiles.size();
if (orphans.size() && (!m_pDefault)) { if (orphans.size() && (!m_pDefault)) {
qDebug() << "No profile found, creating one"; qDebug() << "No profile found, creating one";
Person* profile = new Person(this); Person* profile = new Person(this);
PersonModel::instance()->addPerson(profile);
profile->setFormattedName(tr("Default")); profile->setFormattedName(tr("Default"));
m_pDefault = new Node ; m_pDefault = new Node ;
...@@ -377,7 +383,6 @@ void ProfileContentBackend::setupDefaultProfile() ...@@ -377,7 +383,6 @@ void ProfileContentBackend::setupDefaultProfile()
ProfileModel::instance()->beginInsertRows(QModelIndex(), m_pEditor->m_lProfiles.size(), m_pEditor->m_lProfiles.size()); ProfileModel::instance()->beginInsertRows(QModelIndex(), m_pEditor->m_lProfiles.size(), m_pEditor->m_lProfiles.size());
m_pEditor->m_lProfiles << m_pDefault; m_pEditor->m_lProfiles << m_pDefault;
ProfileModel::instance()->endInsertRows(); ProfileModel::instance()->endInsertRows();
PersonModel::instance()->addPerson(profile);
} }
foreach(Account* a, orphans) { foreach(Account* a, orphans) {
...@@ -401,12 +406,13 @@ void ProfileContentBackend::addAccount(Node* parent, Account* acc) ...@@ -401,12 +406,13 @@ void ProfileContentBackend::addAccount(Node* parent, Account* acc)
m_pEditor->m_hProfileByAccountId[acc->id()] = account_pro; m_pEditor->m_hProfileByAccountId[acc->id()] = account_pro;
} }
bool ProfileContentBackend::load()
void ProfileContentBackend::loadProfiles()
{ {
if (ProfilePersisterDelegate::instance()) { if (ProfilePersisterDelegate::instance()) {
m_pEditor->m_lProfiles.clear(); m_pEditor->m_lProfiles.clear();
QDir profilesDir = ProfilePersisterDelegate::instance()->getProfilesDir(); const QDir profilesDir = ProfilePersisterDelegate::instance()->getProfilesDir();
qDebug() << "Loading vcf from:" << profilesDir; qDebug() << "Loading vcf from:" << profilesDir;
...@@ -471,8 +477,12 @@ bool ProfileContentBackend::load() ...@@ -471,8 +477,12 @@ bool ProfileContentBackend::load()
} }
else { else {
qDebug() << "No ProfilePersistor loaded!"; qDebug() << "No ProfilePersistor loaded!";
return false;
} }
}
bool ProfileContentBackend::load()
{
QTimer::singleShot(0,this,SLOT(loadProfiles()));
return true; return true;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment