Skip to content
Permalink
Browse files

Add metadata to project for last save datetime alongside the existing

last saved username details
  • Loading branch information
nyalldawson committed Jun 4, 2020
1 parent 1d99584 commit d74715c31085eeef98172236840ae0e780e0815b
@@ -118,6 +118,13 @@ Returns the full user name that did the last save.
.. seealso:: :py:func:`saveUser`

.. versionadded:: 3.12
%End

QDateTime lastSaveDateTime() const;
%Docstring
Returns the date and time when the project was last saved.

.. versionadded:: 3.14
%End

bool isDirty() const;
@@ -717,6 +717,7 @@ void QgsExpression::initVariableHelp()
sVariableHelpTexts()->insert( QStringLiteral( "project_abstract" ), QCoreApplication::translate( "variable_help", "Project abstract, taken from project metadata." ) );
sVariableHelpTexts()->insert( QStringLiteral( "project_creation_date" ), QCoreApplication::translate( "variable_help", "Project creation date, taken from project metadata." ) );
sVariableHelpTexts()->insert( QStringLiteral( "project_identifier" ), QCoreApplication::translate( "variable_help", "Project identifier, taken from project metadata." ) );
sVariableHelpTexts()->insert( QStringLiteral( "project_last_saved" ), QCoreApplication::translate( "variable_help", "Date/time when project was last saved." ) );
sVariableHelpTexts()->insert( QStringLiteral( "project_keywords" ), QCoreApplication::translate( "variable_help", "Project keywords, taken from project metadata." ) );
sVariableHelpTexts()->insert( QStringLiteral( "project_area_units" ), QCoreApplication::translate( "variable_help", "Area unit for current project, used when calculating areas of geometries." ) );
sVariableHelpTexts()->insert( QStringLiteral( "project_distance_units" ), QCoreApplication::translate( "variable_help", "Distance unit for current project, used when calculating lengths of geometries." ) );
@@ -491,6 +491,11 @@ QString QgsProject::saveUserFullName() const
return mSaveUserFull;
}

QDateTime QgsProject::lastSaveDateTime() const
{
return mSaveDateTime;
}

bool QgsProject::isDirty() const
{
return mDirty;
@@ -765,6 +770,7 @@ void QgsProject::clear()
mProperties.clearKeys();
mSaveUser.clear();
mSaveUserFull.clear();
mSaveDateTime = QDateTime();
mHomePath.clear();
mCachedHomePath.clear();
mAutoTransaction = false;
@@ -962,7 +968,7 @@ static void _getTitle( const QDomDocument &doc, QString &title )

}

static void readProjectFileMetadata( const QDomDocument &doc, QString &lastUser, QString &lastUserFull )
static void readProjectFileMetadata( const QDomDocument &doc, QString &lastUser, QString &lastUserFull, QDateTime &lastSaveDateTime )
{
QDomNodeList nl = doc.elementsByTagName( QStringLiteral( "qgis" ) );

@@ -977,6 +983,7 @@ static void readProjectFileMetadata( const QDomDocument &doc, QString &lastUser,
QDomElement qgisElement = qgisNode.toElement(); // qgis node should be element
lastUser = qgisElement.attribute( QStringLiteral( "saveUser" ), QString() );
lastUserFull = qgisElement.attribute( QStringLiteral( "saveUserFull" ), QString() );
lastSaveDateTime = QDateTime::fromString( qgisElement.attribute( QStringLiteral( "saveDateTime" ), QString() ), Qt::ISODate );
}


@@ -1331,7 +1338,7 @@ bool QgsProject::readProjectFile( const QString &filename, QgsProject::ReadFlags
QString oldTitle;
_getTitle( *doc, oldTitle );

readProjectFileMetadata( *doc, mSaveUser, mSaveUserFull );
readProjectFileMetadata( *doc, mSaveUser, mSaveUserFull, mSaveDateTime );

QDomNodeList homePathNl = doc->elementsByTagName( QStringLiteral( "homePath" ) );
if ( homePathNl.count() > 0 )
@@ -1828,6 +1835,7 @@ QgsExpressionContextScope *QgsProject::createExpressionContextScope() const
mProjectScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_filename" ), projectFilename, true, true ) );
mProjectScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_basename" ), projectBasename, true, true ) );
mProjectScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_home" ), QDir::toNativeSeparators( homePath() ), true, true ) );
mProjectScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_last_saved" ), mSaveDateTime.isNull() ? QVariant() : QVariant( mSaveDateTime ), true, true ) );
QgsCoordinateReferenceSystem projectCrs = crs();
mProjectScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs" ), projectCrs.authid(), true, true ) );
mProjectScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs_definition" ), projectCrs.toProj(), true, true ) );
@@ -1985,13 +1993,12 @@ bool QgsProject::write( const QString &filename )
{
mFile.setFileName( filename );
mCachedHomePath.clear();
mProjectScope.reset();

return write();
}

bool QgsProject::write()
{
mProjectScope.reset();
if ( QgsProjectStorage *storage = projectStorage() )
{
QgsReadWriteContext context;
@@ -2097,11 +2104,14 @@ bool QgsProject::writeProjectFile( const QString &filename )
qgisNode.setAttribute( QStringLiteral( "saveUserFull" ), newSaveUserFull );
mSaveUser = newSaveUser;
mSaveUserFull = newSaveUserFull;
mSaveDateTime = QDateTime::currentDateTime();
qgisNode.setAttribute( QStringLiteral( "saveDateTime" ), mSaveDateTime.toString( Qt::ISODate ) );
}
else
{
mSaveUser.clear();
mSaveUserFull.clear();
mSaveDateTime = QDateTime();
}
doc->appendChild( qgisNode );

@@ -208,6 +208,13 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/
QString saveUserFullName() const;

/**
* Returns the date and time when the project was last saved.
*
* \since QGIS 3.14
*/
QDateTime lastSaveDateTime() const;

/**
* Returns TRUE if the project has been modified since the last write()
*/
@@ -1931,6 +1938,7 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera

QString mSaveUser; // last saved user.
QString mSaveUserFull; // last saved user full name.
QDateTime mSaveDateTime;

/**
* Manual override for project home path - if empty, home path is automatically
@@ -506,6 +506,7 @@ void TestQgsProject::projectSaveUser()
QgsProject p;
QVERIFY( p.saveUser().isEmpty() );
QVERIFY( p.saveUserFullName().isEmpty() );
QVERIFY( !p.lastSaveDateTime().isValid() );

QTemporaryFile f;
QVERIFY( f.open() );
@@ -515,6 +516,7 @@ void TestQgsProject::projectSaveUser()

QCOMPARE( p.saveUser(), QgsApplication::userLoginName() );
QCOMPARE( p.saveUserFullName(), QgsApplication::userFullName() );
QCOMPARE( p.lastSaveDateTime().date(), QDateTime::currentDateTime().date() );

QgsSettings s;
s.setValue( QStringLiteral( "projects/anonymize_saved_projects" ), true, QgsSettings::Core );
@@ -523,12 +525,14 @@ void TestQgsProject::projectSaveUser()

QVERIFY( p.saveUser().isEmpty() );
QVERIFY( p.saveUserFullName().isEmpty() );
QVERIFY( !p.lastSaveDateTime().isValid() );

s.setValue( QStringLiteral( "projects/anonymize_saved_projects" ), false, QgsSettings::Core );

p.write();
QCOMPARE( p.saveUser(), QgsApplication::userLoginName() );
QCOMPARE( p.saveUserFullName(), QgsApplication::userFullName() );
QCOMPARE( p.lastSaveDateTime().date(), QDateTime::currentDateTime().date() );
}

void TestQgsProject::testSetGetCrs()

0 comments on commit d74715c

Please sign in to comment.
You can’t perform that action at this time.