Skip to content

Commit

Permalink
Merge pull request #4077 from opengisch/backport-4054-to-release-2_7
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Mar 25, 2023
2 parents 58ac8cb + 1be52e8 commit 86617e8
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/core/deltafilewrapper.cpp
Expand Up @@ -20,6 +20,7 @@
#include "utils/fileutils.h"
#include "utils/qfieldcloudutils.h"

#include <QDebug>
#include <QFile>
#include <QFileInfo>
#include <QUuid>
Expand Down Expand Up @@ -650,13 +651,20 @@ void DeltaFileWrapper::addPatch( const QString &localLayerId, const QString &sou
QMap<QString, int> layerPkDeltaIdx = mLocalPkDeltaIdx.value( localLayerId );
QString localPk = delta.value( QStringLiteral( "localPk" ) ).toString();

qInfo() << "DeltaFileWrapper::addPatch: localPk=" << localPk << " layerPkDeltaIdx=" << layerPkDeltaIdx;

if ( layerPkDeltaIdx.contains( localPk ) )
{
int deltaIdx = layerPkDeltaIdx.take( localPk );
QJsonObject deltaCreate = mDeltas.at( deltaIdx ).toObject();

Q_ASSERT( deltaCreate.value( QStringLiteral( "method" ) ).toString() == QStringLiteral( "create" ) );

QJsonObject newCreate = deltaCreate.value( QStringLiteral( "new" ) ).toObject();
QJsonObject attributesCreate = newCreate.value( QStringLiteral( "attributes" ) ).toObject();

qInfo() << "DeltaFileWrapper::addPatch: replacing an existing create delta: " << deltaCreate << "at" << deltaIdx;

if ( !newData.value( QStringLiteral( "geometry" ) ).isUndefined() )
{
newCreate.insert( QStringLiteral( "geometry" ), newData.value( QStringLiteral( "geometry" ) ) );
Expand All @@ -675,12 +683,16 @@ void DeltaFileWrapper::addPatch( const QString &localLayerId, const QString &sou

mDeltas.replace( deltaIdx, deltaCreate );

qInfo() << "DeltaFileWrapper::addPatch: replaced an existing create delta: " << deltaCreate;

return;
}
else
{
mDeltas.append( delta );

qInfo() << "DeltaFileWrapper::addPatch: Added a new patch delta: " << delta;

emit countChanged();
}
}
Expand Down Expand Up @@ -756,6 +768,8 @@ void DeltaFileWrapper::addDelete( const QString &localLayerId, const QString &so
mDeltas.append( delta );
mIsDirty = true;

qInfo() << "DeltaFileWrapper::addDelete: Added a new delete delta: " << delta;

emit countChanged();
}

Expand Down Expand Up @@ -835,6 +849,8 @@ void DeltaFileWrapper::addCreate( const QString &localLayerId, const QString &so
mDeltas.append( delta );
mIsDirty = true;

qInfo() << "DeltaFileWrapper::addCreate: Added a new create delta: " << delta;

emit countChanged();
}

Expand Down Expand Up @@ -1114,11 +1130,15 @@ QPair<int, QString> DeltaFileWrapper::getLocalPkAttribute( const QgsVectorLayer
// we assume the first index to be the primary key index... kinda stupid, but memory layers don't have primary key at all, but we use it on geopackages, but... snap!
const int pkAttrIdx = pkAttrs[0];

qInfo() << "DeltaFileWrapper::getLocalPkAttribute: vl->primaryKeyglAttributes()=" << vl->primaryKeyAttributes() << " pkAttrs=" << pkAttrs;

if ( pkAttrIdx == -1 )
return QPair<int, QString>( -1, QString() );

const QString pkAttrName = fields.at( pkAttrIdx ).name();

qInfo() << "DeltaFileWrapper::getLocalPkAttribute: pkAttrName=" << pkAttrName << " pkAttrIdx=" << pkAttrIdx;

return QPair<int, QString>( pkAttrIdx, pkAttrName );
}

Expand All @@ -1127,18 +1147,28 @@ QPair<int, QString> DeltaFileWrapper::getSourcePkAttribute( const QgsVectorLayer
{
QString pkAttrNamesAggr = vl->customProperty( QStringLiteral( "QFieldSync/sourceDataPrimaryKeys" ) ).toString();

qInfo() << "DeltaFileWrapper::getSourcePkAttribute: getting pkAttrNamesAggr=" << pkAttrNamesAggr << " with type=" << vl->customProperty( QStringLiteral( "QFieldSync/sourceDataPrimaryKeys" ) ).typeName();

if ( pkAttrNamesAggr.isEmpty() )
{
qInfo() << "DeltaFileWrapper::getSourcePkAttribute: empty pkAttrNamesAggr, gotcha!";

return getLocalPkAttribute( vl );
}

QStringList pkAttrNames = pkAttrNamesAggr.split( QStringLiteral( "," ) );

qInfo() << "DeltaFileWrapper::getSourcePkAttribute: pk attrs pkAttrNames=" << pkAttrNames.size();

if ( pkAttrNames.size() > 1 )
return QPair<int, QString>( -1, QString() );

const QString pkAttrName = pkAttrNames[0];
const QgsFields fields = vl->fields();
const int pkAttrIdx = fields.indexFromName( pkAttrName );

qInfo() << "DeltaFileWrapper::getSourcePkAttribute: pk pkAttrName=" << pkAttrName << " with index=" << pkAttrIdx;

if ( pkAttrIdx == -1 )
return QPair<int, QString>( -1, QString() );

Expand All @@ -1147,5 +1177,7 @@ QPair<int, QString> DeltaFileWrapper::getSourcePkAttribute( const QgsVectorLayer

QString DeltaFileWrapper::getSourceLayerId( const QgsVectorLayer *vl )
{
qInfo() << "DeltaFileWrapper::getSourceLayerId: remoteLayerId=" << ( vl ? vl->customProperty( QStringLiteral( "remoteLayerId" ) ).toString() : QString() );

return vl ? vl->customProperty( QStringLiteral( "remoteLayerId" ) ).toString() : QString();
}
45 changes: 44 additions & 1 deletion src/core/layerobserver.cpp
Expand Up @@ -18,13 +18,16 @@
#include "layerobserver.h"
#include "qfieldcloudutils.h"

#include <QDebug>
#include <QDir>
#include <qgsfeature.h>
#include <qgsfeatureiterator.h>
#include <qgsfeaturerequest.h>
#include <qgsmessagelog.h>
#include <qgsvectorlayereditbuffer.h>

#include <appinterface.h>


LayerObserver::LayerObserver( const QgsProject *project )
: mProject( project )
Expand Down Expand Up @@ -113,6 +116,8 @@ void LayerObserver::onBeforeCommitChanges()
while ( featuresIt.nextFeature( f ) )
changedFeatures.insert( f.id(), f );

qInfo() << "LayerObserver::onBeforeCommitChanges: vl->id()=" << vl->id() << "sourcePkAttrPair=" << changedFids;

// NOTE no need to keep track of added features, as they are always present in the layer after commit
mChangedFeatures.insert( vl->id(), changedFeatures );
mPatchedFids.insert( vl->id(), QgsFeatureIds() );
Expand All @@ -129,8 +134,12 @@ void LayerObserver::onCommittedFeaturesAdded( const QString &localLayerId, const
const QPair<int, QString> localPkAttrPair = DeltaFileWrapper::getLocalPkAttribute( vl );
const QPair<int, QString> sourcePkAttrPair = DeltaFileWrapper::getSourcePkAttribute( vl );

qInfo() << "LayerObserver::onCommittedFeaturesAdded: sourcePkAttrPair=" << sourcePkAttrPair << " sourceLayerId=" << sourceLayerId;

for ( const QgsFeature &newFeature : addedFeatures )
{
qInfo() << " LayerObserver::onCommittedFeaturesAdded: adding create delta... FID=" << newFeature.id();

mDeltaFileWrapper->addCreate( localLayerId, sourceLayerId, localPkAttrPair.second, sourcePkAttrPair.second, newFeature );
}
}
Expand All @@ -147,11 +156,16 @@ void LayerObserver::onCommittedFeaturesRemoved( const QString &localLayerId, con
const QPair<int, QString> localPkAttrPair = DeltaFileWrapper::getLocalPkAttribute( vl );
const QPair<int, QString> sourcePkAttrPair = DeltaFileWrapper::getSourcePkAttribute( vl );

qInfo() << "LayerObserver::onCommittedFeaturesRemoved: sourcePkAttrPair=" << sourcePkAttrPair << " sourceLayerId=" << sourceLayerId << " deletedFeatureIds=" << deletedFeatureIds;

for ( const QgsFeatureId &fid : deletedFeatureIds )
{
Q_ASSERT( changedFeatures.contains( fid ) );

QgsFeature oldFeature = changedFeatures.take( fid );

qInfo() << " LayerObserver::onCommittedFeaturesRemoved: adding delete delta... FID=" << fid;

mDeltaFileWrapper->addDelete( localLayerId, sourceLayerId, localPkAttrPair.second, sourcePkAttrPair.second, oldFeature );
}

Expand All @@ -172,7 +186,9 @@ void LayerObserver::onCommittedAttributeValuesChanges( const QString &localLayer
const QPair<int, QString> localPkAttrPair = DeltaFileWrapper::getLocalPkAttribute( vl );
const QPair<int, QString> sourcePkAttrPair = DeltaFileWrapper::getSourcePkAttribute( vl );

for ( const QgsFeatureId &fid : changedAttributesValuesFids )
qInfo() << "LayerObserver::onCommittedAttributeValuesChanges: sourcePkAttrPair=" << sourcePkAttrPair << " sourceLayerId=" << sourceLayerId << " changedAttributesValuesFids=" << changedAttributesValuesFids;

for ( const QgsFeatureId fid : changedAttributesValuesFids )
{
if ( patchedFids.contains( fid ) )
continue;
Expand All @@ -183,6 +199,13 @@ void LayerObserver::onCommittedAttributeValuesChanges( const QString &localLayer

QgsFeature oldFeature = changedFeatures.take( fid );
QgsFeature newFeature = vl->getFeature( fid );

qInfo() << "LayerObserver::onCommittedAttributeValuesChanges: adding patch delta... FID=" << fid;

if ( vl->fields().indexOf( "fid_1" ) != -1 && localPkAttrPair.second == sourcePkAttrPair.second && newFeature.attribute( "fid" ) != newFeature.attribute( "fid_1" ) )
{
mLocalAndSourcePkAttrAreEqual = true;
}
mDeltaFileWrapper->addPatch( localLayerId, sourceLayerId, localPkAttrPair.second, sourcePkAttrPair.second, oldFeature, newFeature );
}

Expand All @@ -204,6 +227,8 @@ void LayerObserver::onCommittedGeometriesChanges( const QString &localLayerId, c
const QPair<int, QString> localPkAttrPair = DeltaFileWrapper::getLocalPkAttribute( vl );
const QPair<int, QString> sourcePkAttrPair = DeltaFileWrapper::getSourcePkAttribute( vl );

qInfo() << "LayerObserver::onCommittedGeometriesChanges: sourcePkAttrPair=" << sourcePkAttrPair << " sourceLayerId=" << sourceLayerId << " changedGeometriesFids=" << changedGeometriesFids;

for ( const QgsFeatureId &fid : changedGeometriesFids )
{
if ( patchedFids.contains( fid ) )
Expand All @@ -216,6 +241,12 @@ void LayerObserver::onCommittedGeometriesChanges( const QString &localLayerId, c
QgsFeature oldFeature = changedFeatures.take( fid );
QgsFeature newFeature = vl->getFeature( fid );

qInfo() << " LayerObserver::onCommittedGeometriesChanges: adding patch delta... FID=" << fid;

if ( vl->fields().indexOf( "fid_1" ) != -1 && localPkAttrPair.second == sourcePkAttrPair.second && newFeature.attribute( "fid" ) != newFeature.attribute( "fid_1" ) )
{
mLocalAndSourcePkAttrAreEqual = true;
}
mDeltaFileWrapper->addPatch( localLayerId, sourceLayerId, localPkAttrPair.second, sourcePkAttrPair.second, oldFeature, newFeature );
}

Expand All @@ -237,6 +268,13 @@ void LayerObserver::onEditingStopped()
// TODO somehow indicate the user that writing failed
QgsMessageLog::logMessage( QStringLiteral( "Failed writing JSON file" ) );
}

if ( vl->source().contains( QStringLiteral( "data.gpkg" ) ) && mLocalAndSourcePkAttrAreEqual )
{
AppInterface::instance()->sendLog( QStringLiteral( "Called LayerObserver::onEditingStopped!" ) );
mLocalAndSourcePkAttrAreEqual = false;
}

emit layerEdited( layerId );
}

Expand Down Expand Up @@ -273,6 +311,11 @@ void LayerObserver::addLayerListeners()
continue;
}

qInfo() << "LayerObserver::addLayerListeners: vl->getLocalPkAttribute()=" << DeltaFileWrapper::getLocalPkAttribute( vl );
qInfo() << "LayerObserver::addLayerListeners: vl->getSourcePkAttribute()=" << DeltaFileWrapper::getSourcePkAttribute( vl );
qInfo() << "LayerObserver::addLayerListeners: vl->customProperties()=" << vl->customProperties().keys();
qInfo() << "LayerObserver::addLayerListeners: vl->originalXmlProperties()=" << vl->originalXmlProperties();

disconnect( vl, &QgsVectorLayer::beforeCommitChanges, this, &LayerObserver::onBeforeCommitChanges );
disconnect( vl, &QgsVectorLayer::committedFeaturesAdded, this, &LayerObserver::onCommittedFeaturesAdded );
disconnect( vl, &QgsVectorLayer::committedFeaturesRemoved, this, &LayerObserver::onCommittedFeaturesRemoved );
Expand Down
2 changes: 2 additions & 0 deletions src/core/layerobserver.h
Expand Up @@ -176,6 +176,8 @@ class LayerObserver : public QObject
* Assigns listeners only for layer actions of `cloud` and `offline`.
*/
void addLayerListeners();

bool mLocalAndSourcePkAttrAreEqual = false;
};

#endif // LAYEROBSERVER_H

1 comment on commit 86617e8

@qfield-fairy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.