Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[needs-doc] Add stable id, save id, save count to project spec #6527

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions python/core/qgsproject.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,33 @@ Sets the project's title.
Returns the project's title.

.. seealso:: :py:func:`setTitle`
%End

QString id() const;
%Docstring
Returns the project's stable id.
%End

QString saveId() const;
%Docstring
Returns the project's current save id. This value is changed on after each save.
Use :py:func:`id` if you need a stable id for the project.
%End

int saveCounter() const;
%Docstring
Returns the save counter for the project. Like a car odometer but better because it's QGIS.
This will change with each save like :py:func:`saveId`
%End

QString saveUser() const;
%Docstring
Returns the user name that did the last save.
%End

QString saveUserFullname() const;
%Docstring
Returns the full user name that did the last save.
%End

bool isDirty() const;
Expand Down
83 changes: 83 additions & 0 deletions src/core/qgsproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
#include <utime.h>
#endif

static QString generateUuid()
{
QString uuid = QUuid::createUuid().toString();
return uuid.mid( 1, uuid.length() - 3 );
Copy link
Collaborator

Choose a reason for hiding this comment

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

off topic - but wow it annoys me the format QUuid uses.

Copy link
Member Author

Choose a reason for hiding this comment

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

You and be both. Never once have I thought "man I wish this guid had { } around it". Silly.

}

// canonical project instance
QgsProject *QgsProject::sProject = nullptr;

Expand Down Expand Up @@ -405,6 +411,31 @@ QString QgsProject::title() const
return mTitle;
}

QString QgsProject::id() const
{
return mId;
}

QString QgsProject::saveId() const
{
return mSaveId;
}

int QgsProject::saveCounter() const
{
return mSaveCounter;
}

QString QgsProject::saveUser() const
{
return mSaveUser;
}

QString QgsProject::saveUserFullname() const
{
return mSaveUserFull;
}


bool QgsProject::isDirty() const
{
Expand Down Expand Up @@ -487,6 +518,11 @@ void QgsProject::clear()
mFile.setFileName( QString() );
mProperties.clearKeys();
mTitle.clear();
mId.clear();
mSaveId.clear();
mSaveCounter = 0;
mSaveUser.clear();
mSaveUserFull.clear();
mAutoTransaction = false;
mEvaluateDefaultValues = false;
mDirty = false;
Expand Down Expand Up @@ -631,6 +667,32 @@ static void _getTitle( const QDomDocument &doc, QString &title )

}

static void getProjectMetadata( const QDomDocument &doc, QString &stableId,
QString &saveId, int &saveCounter, QString &lastUser, QString &lastUserFull )
{
QDomNodeList nl = doc.elementsByTagName( QStringLiteral( "qgis" ) );

if ( !nl.count() )
{
QgsDebugMsg( "unable to find qgis element" );
return;
}

QDomNode qgisNode = nl.item( 0 ); // there should only be one, so zeroth element OK

QDomElement qgisElement = qgisNode.toElement(); // qgis node should be element
QString uuid = generateUuid();
stableId = qgisElement.attribute( QStringLiteral( "id" ), uuid );
if ( stableId.isEmpty() )
stableId = uuid;

saveId = qgisElement.attribute( QStringLiteral( "save-id" ), QString() );
saveCounter = qgisElement.attribute( QStringLiteral( "save-counter" ), 0 ).toInt();
lastUser = qgisElement.attribute( QStringLiteral( "save-user" ), QString() );
lastUserFull = qgisElement.attribute( QStringLiteral( "save-user-full" ), QString() );
}


QgsProjectVersion getVersion( const QDomDocument &doc )
{
QDomNodeList nl = doc.elementsByTagName( QStringLiteral( "qgis" ) );
Expand Down Expand Up @@ -856,6 +918,11 @@ bool QgsProject::readProjectFile( const QString &filename )

QgsDebugMsg( "Opened document " + projectFile.fileName() );
QgsDebugMsg( "Project title: " + mTitle );
QgsDebugMsg( "Project id: " + mId );
QgsDebugMsg( "Project save id: " + mSaveId );
QgsDebugMsg( QString( "Project save counter: %1" ).arg( mSaveCounter ) );
QgsDebugMsg( QString( "Project save user: %1" ).arg( mSaveUser ) );
QgsDebugMsg( QString( "Project save user: %1" ).arg( mSaveUserFull ) );

// get project version string, if any
QgsProjectVersion fileVersion = getVersion( *doc );
Expand Down Expand Up @@ -894,6 +961,8 @@ bool QgsProject::readProjectFile( const QString &filename )
// now get project title
_getTitle( *doc, mTitle );

getProjectMetadata( *doc, mId, mSaveId, mSaveCounter, mSaveUser, mSaveUserFull );

QgsReadWriteContext context;
context.setPathResolver( pathResolver() );

Expand Down Expand Up @@ -1366,6 +1435,15 @@ bool QgsProject::writeProjectFile( const QString &filename )
QDomElement qgisNode = doc->createElement( QStringLiteral( "qgis" ) );
qgisNode.setAttribute( QStringLiteral( "projectname" ), title() );
qgisNode.setAttribute( QStringLiteral( "version" ), QStringLiteral( "%1" ).arg( Qgis::QGIS_VERSION ) );
qgisNode.setAttribute( QStringLiteral( "id" ), id() );
QString newSaveId = generateUuid();
int newSaveCounter = saveCounter() + 1;
QString newSaveUser = QgsApplication::userLoginName();
QString newSaveUserFull = QgsApplication::userFullName();
qgisNode.setAttribute( QStringLiteral( "save-id" ), newSaveId );
qgisNode.setAttribute( QStringLiteral( "save-counter" ), newSaveCounter );
qgisNode.setAttribute( QStringLiteral( "save-user" ), newSaveUser );
qgisNode.setAttribute( QStringLiteral( "save-user-full" ), newSaveUserFull );

doc->appendChild( qgisNode );

Expand Down Expand Up @@ -1563,6 +1641,11 @@ bool QgsProject::writeProjectFile( const QString &filename )

emit projectSaved();

mSaveId = newSaveId;
mSaveCounter = newSaveCounter;
mSaveUser = newSaveUser;
mSaveUserFull = newSaveUserFull;

return true;
}

Expand Down
32 changes: 32 additions & 0 deletions src/core/qgsproject.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,33 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/
QString title() const;

/**
* Returns the project's stable id.
*/
QString id() const;

/**
* Returns the project's current save id. This value is changed on after each save.
* Use \see id() if you need a stable id for the project.
*/
QString saveId() const;

/**
* Returns the save counter for the project. Like a car odometer but better because it's QGIS.
* This will change with each save like \see saveId()
*/
int saveCounter() const;

/**
* Returns the user name that did the last save.
*/
QString saveUser() const;

/**
* Returns the full user name that did the last save.
*/
QString saveUserFullname() const;

/**
* Returns true if the project has been modified since the last write()
*/
Expand Down Expand Up @@ -1207,6 +1234,11 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
QFile mFile; // current physical project file
mutable QgsProjectPropertyKey mProperties; // property hierarchy, TODO: this shouldn't be mutable
QString mTitle; // project title
QString mId; // project id
QString mSaveId; // changing id for each save.
QString mSaveUser; // last saved user.
QString mSaveUserFull; // last saved user.
int mSaveCounter; // increasing save counter
Copy link
Collaborator

Choose a reason for hiding this comment

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

initialize to 0 here

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

bool mAutoTransaction = false; // transaction grouped editing
bool mEvaluateDefaultValues = false; // evaluate default values immediately
QgsCoordinateReferenceSystem mCrs;
Expand Down