Skip to content

Commit

Permalink
add userLoginName and userFullName to QGIS project when writing the p…
Browse files Browse the repository at this point in the history
…roject
  • Loading branch information
Thomas Baumann authored and nyalldawson committed Jan 21, 2020
1 parent 1bf50cf commit bc1f21a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/core/qgsproject.cpp
Expand Up @@ -473,6 +473,16 @@ QString QgsProject::title() const
return mMetadata.title();
}

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

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

bool QgsProject::isDirty() const
{
return mDirty;
Expand Down Expand Up @@ -725,6 +735,8 @@ void QgsProject::clear()
mProjectScope.reset();
mFile.setFileName( QString() );
mProperties.clearKeys();
mSaveUser.clear();
mSaveUserFull.clear();
mHomePath.clear();
mCachedHomePath.clear();
mAutoTransaction = false;
Expand Down Expand Up @@ -900,6 +912,24 @@ static void _getTitle( const QDomDocument &doc, QString &title )

}

static void getProjectMetadata(const QDomDocument &doc, 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
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 @@ -1235,6 +1265,8 @@ bool QgsProject::readProjectFile( const QString &filename, QgsProject::ReadFlags
QString oldTitle;
_getTitle( *doc, oldTitle );

getProjectMetadata(*doc, mSaveUser, mSaveUserFull);

QDomNodeList homePathNl = doc->elementsByTagName( QStringLiteral( "homePath" ) );
if ( homePathNl.count() > 0 )
{
Expand Down Expand Up @@ -1525,6 +1557,9 @@ bool QgsProject::readProjectFile( const QString &filename, QgsProject::ReadFlags
// if all went well, we're allegedly in pristine state
if ( clean )
setDirty( false );

QgsDebugMsg(QString("Project save user: %1").arg(mSaveUser));
QgsDebugMsg(QString("Project save user: %1").arg(mSaveUserFull));

Q_NOWARN_DEPRECATED_PUSH
emit nonIdentifiableLayersChanged( nonIdentifiableLayers() );
Expand Down Expand Up @@ -1951,7 +1986,10 @@ 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::version() ) );

QString newSaveUser = QgsApplication::userLoginName();
QString newSaveUserFull = QgsApplication::userFullName();
qgisNode.setAttribute(QStringLiteral("save-user"), newSaveUser);
qgisNode.setAttribute(QStringLiteral("save-user-full"), newSaveUserFull);
doc->appendChild( qgisNode );

QDomElement homePathNode = doc->createElement( QStringLiteral( "homePath" ) );
Expand Down Expand Up @@ -2191,7 +2229,8 @@ bool QgsProject::writeProjectFile( const QString &filename )
setDirty( false ); // reset to pristine state

emit projectSaved();

mSaveUser = newSaveUser;
mSaveUserFull = newSaveUserFull;
return true;
}

Expand Down
13 changes: 13 additions & 0 deletions src/core/qgsproject.h
Expand Up @@ -160,6 +160,16 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/
QString title() 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 @@ -1793,6 +1803,9 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera

QFile mFile; // current physical project file

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

/**
* Manual override for project home path - if empty, home path is automatically
* created based on file name.
Expand Down

0 comments on commit bc1f21a

Please sign in to comment.