Skip to content
Snippets Groups Projects
Commit fc541131 authored by Emmanuel Lepage Vallée's avatar Emmanuel Lepage Vallée Committed by gerrit2
Browse files

profile: Fix drag and drop between profiles with a proxy

When using a proxy to re-order account, the dropMimeData
code wasn't working anymore. This commit clean it and
fix the issue. Proper account re-ordering is still broken.

Issue: #79575
Change-Id: I908146ac3a86a484c775ac2b93727f00659e5e20
parent b622bbcd
No related branches found
No related tags found
No related merge requests found
...@@ -65,6 +65,7 @@ public: ...@@ -65,6 +65,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;
QHash<QByteArray,Node*> m_hAccountIdToNode;
QVector<Person*> m_lProfilePersons; QVector<Person*> m_lProfilePersons;
private: private:
...@@ -345,7 +346,8 @@ void ProfileContentBackend::addAccount(Node* parent, Account* acc) ...@@ -345,7 +346,8 @@ void ProfileContentBackend::addAccount(Node* parent, Account* acc)
ProfileModel::instance()->beginInsertRows(ProfileModel::instance()->index(parent->m_Index,0), parent->children.size(), parent->children.size()); ProfileModel::instance()->beginInsertRows(ProfileModel::instance()->index(parent->m_Index,0), parent->children.size(), parent->children.size());
parent->children << account_pro; parent->children << account_pro;
ProfileModel::instance()->endInsertRows(); ProfileModel::instance()->endInsertRows();
m_pEditor->m_hProfileByAccountId[acc->id()] = account_pro; m_pEditor->m_hProfileByAccountId[acc->id()] = parent;
m_pEditor->m_hAccountIdToNode[acc->id()] = account_pro;
if (parent->contact) if (parent->contact)
acc->contactMethod()->setPerson(parent->contact); acc->contactMethod()->setPerson(parent->contact);
...@@ -372,9 +374,7 @@ void ProfileContentBackend::loadProfiles() ...@@ -372,9 +374,7 @@ void ProfileContentBackend::loadProfiles()
pro->m_Index = m_pEditor->m_lProfiles.size() ; pro->m_Index = m_pEditor->m_lProfiles.size() ;
QList<Account*> accs; QList<Account*> accs;
qDebug() << "\n\n\nMAPPING!!!";
VCardUtils::mapToPerson(profile,QUrl(profilesDir.path()+'/'+item),&accs); VCardUtils::mapToPerson(profile,QUrl(profilesDir.path()+'/'+item),&accs);
qDebug() << "\n\nEND MAP";
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 << pro; m_pEditor->m_lProfiles << pro;
...@@ -586,15 +586,17 @@ QModelIndex ProfileModel::mapFromSource(const QModelIndex& idx) const ...@@ -586,15 +586,17 @@ QModelIndex ProfileModel::mapFromSource(const QModelIndex& idx) const
return QModelIndex(); return QModelIndex();
Account* acc = AccountModel::instance()->getAccountByModelIndex(idx); Account* acc = AccountModel::instance()->getAccountByModelIndex(idx);
Node* pro = d_ptr->m_pProfileBackend->m_pEditor->m_hProfileByAccountId[acc->id()]; Node* accNode = d_ptr->m_pProfileBackend->m_pEditor->m_hAccountIdToNode[acc->id()];
//Something is wrong, there is an orphan //Something is wrong, there is an orphan
if (!pro) { if (!accNode) {
d_ptr->m_pProfileBackend->setupDefaultProfile(); d_ptr->m_pProfileBackend->setupDefaultProfile();
pro = d_ptr->m_pProfileBackend->m_pEditor->m_hProfileByAccountId[acc->id()]; accNode = d_ptr->m_pProfileBackend->m_pEditor->m_hAccountIdToNode[acc->id()];
} }
return AccountModel::instance()->index(pro->m_Index,0,index(pro->parent->m_Index,0,QModelIndex())); Q_ASSERT(accNode->parent && accNode->type == Node::Type::ACCOUNT);
return ProfileModel::instance()->index(accNode->m_Index, 0, index(accNode->parent->m_Index,0,QModelIndex()));
} }
QVariant ProfileModel::data(const QModelIndex& index, int role ) const QVariant ProfileModel::data(const QModelIndex& index, int role ) const
...@@ -771,34 +773,55 @@ void ProfileModelPrivate::updateIndexes() ...@@ -771,34 +773,55 @@ void ProfileModelPrivate::updateIndexes()
bool ProfileModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) bool ProfileModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{ {
Q_UNUSED(action) Q_UNUSED(action)
if((parent.isValid() && row < 0) || column > 0) {
qDebug() << "row or column invalid"; QModelIndex accountIdx, profileIdx;
// For some reasons, it seem that row and column are {-1,-1} when using a
// proxy model. The proxy is required to properly sort the accounts, so
// this code "fix" the values
if (parent.parent().isValid()) {
accountIdx = parent;
profileIdx = parent.parent();
row = accountIdx.row();
column = accountIdx.column();
}
else {
accountIdx = index(row, column, parent);
profileIdx = parent;
}
if(((!profileIdx.isValid()) && row < 0) || column > 0) {
qDebug() << "Row or column invalid";
return false; return false;
} }
if (data->hasFormat(RingMimes::ACCOUNT)) { if (data->hasFormat(RingMimes::ACCOUNT)) {
qDebug() << "dropping account"; qDebug() << "Dropping account";
const QByteArray accountId = data->data(RingMimes::ACCOUNT); const QByteArray accountId = data->data(RingMimes::ACCOUNT);
Node* newProfile = nullptr; Node* newProfile = nullptr;
int destIdx = 0, indexOfAccountToMove = -1; // Where to insert in account list of profile int destIdx = 0, indexOfAccountToMove = -1; // Where to insert in account list of profile
if(!parent.isValid() && row < d_ptr->m_pProfileBackend->m_pEditor->m_lProfiles.size()) { // Dropped on a profile index, append it at the end
if(profileIdx.isValid()) {
qDebug() << "Dropping on profile title"; qDebug() << "Dropping on profile title";
qDebug() << "row:" << row; newProfile = static_cast<Node*>(profileIdx.internalPointer());
newProfile = d_ptr->m_pProfileBackend->m_pEditor->m_lProfiles[row];
destIdx = 0; destIdx = 0;
} }
else if (parent.isValid()) { // Dropped on an account
newProfile = static_cast<Node*>(parent.internalPointer()); else if (profileIdx.isValid()) {
newProfile = static_cast<Node*>(profileIdx.internalPointer());
destIdx = row; destIdx = row;
} }
if (!newProfile) if ((!newProfile) || (!newProfile->contact)) {
qDebug() << "Invalid profile";
return false; return false;
}
// Use the account ID to locate the original location
Node* accountProfile = d_ptr->m_pProfileBackend->m_pEditor->m_hProfileByAccountId[accountId]; Node* accountProfile = d_ptr->m_pProfileBackend->m_pEditor->m_hProfileByAccountId[accountId];
for (Node* acc : accountProfile->children) { foreach (Node* acc, accountProfile->children) {
if(acc->account->id() == accountId) { if(acc->account->id() == accountId) {
indexOfAccountToMove = acc->m_Index; indexOfAccountToMove = acc->m_Index;
break; break;
...@@ -806,7 +829,7 @@ bool ProfileModel::dropMimeData(const QMimeData *data, Qt::DropAction action, in ...@@ -806,7 +829,7 @@ bool ProfileModel::dropMimeData(const QMimeData *data, Qt::DropAction action, in
} }
if(indexOfAccountToMove == -1) { if(indexOfAccountToMove == -1) {
qDebug() << "indexOfAccountToMove:" << indexOfAccountToMove; qDebug() << "Failed to obtain the account ID";
return false; return false;
} }
...@@ -825,8 +848,7 @@ bool ProfileModel::dropMimeData(const QMimeData *data, Qt::DropAction action, in ...@@ -825,8 +848,7 @@ bool ProfileModel::dropMimeData(const QMimeData *data, Qt::DropAction action, in
endMoveRows(); endMoveRows();
} }
else if (data->hasFormat(RingMimes::PROFILE)) { else if (data->hasFormat(RingMimes::PROFILE)) {
qDebug() << "dropping profile"; qDebug() << "Dropping profile on row" << row;
qDebug() << "row:" << row;
int destinationRow = -1; int destinationRow = -1;
if(row < 0) { if(row < 0) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment