2017-09-19 08:53:51 +00:00
/*
* Copyright ( C ) by Olivier Goffart < ogoffart @ woboq . com >
*
* This library is free software ; you can redistribute it and / or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2.1 of the License , or ( at your option ) any later version .
*
* This library is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Lesser General Public License for more details .
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
*/
# include "remotepermissions.h"
2024-03-25 21:06:11 +00:00
# include <QLoggingCategory>
2017-09-19 08:53:51 +00:00
# include <cstring>
namespace OCC {
2024-03-25 21:06:11 +00:00
Q_LOGGING_CATEGORY ( lcRemotePermissions , " nextcloud.sync.remotepermissions " , QtInfoMsg )
2017-09-19 08:53:51 +00:00
static const char letters [ ] = " WDNVCKRSMm " ;
template < typename Char >
void RemotePermissions : : fromArray ( const Char * p )
{
2018-08-15 10:16:46 +00:00
_value = notNullMask ;
2017-09-19 08:53:51 +00:00
if ( ! p )
return ;
while ( * p ) {
if ( auto res = std : : strchr ( letters , static_cast < char > ( * p ) ) )
_value | = ( 1 < < ( res - letters ) ) ;
+ + p ;
}
}
2018-08-15 10:16:46 +00:00
QByteArray RemotePermissions : : toDbValue ( ) const
2017-09-19 08:53:51 +00:00
{
QByteArray result ;
if ( isNull ( ) )
return result ;
result . reserve ( PermissionsCount ) ;
for ( uint i = 1 ; i < = PermissionsCount ; + + i ) {
if ( _value & ( 1 < < i ) )
result . append ( letters [ i ] ) ;
}
if ( result . isEmpty ( ) ) {
// Make sure it is not empty so we can differentiate null and empty permissions
result . append ( ' ' ) ;
}
return result ;
}
2020-07-27 08:44:51 +00:00
QString RemotePermissions : : toString ( ) const
2018-08-15 10:16:46 +00:00
{
2020-07-27 08:44:51 +00:00
return QString : : fromUtf8 ( toDbValue ( ) ) ;
2018-08-15 10:16:46 +00:00
}
RemotePermissions RemotePermissions : : fromDbValue ( const QByteArray & value )
{
if ( value . isEmpty ( ) )
2020-12-10 18:52:58 +00:00
return { } ;
2018-08-15 10:16:46 +00:00
RemotePermissions perm ;
perm . fromArray ( value . constData ( ) ) ;
return perm ;
}
2024-03-25 21:06:11 +00:00
template < typename T >
RemotePermissions RemotePermissions : : internalFromServerString ( const QString & value ,
const T & otherProperties ,
MountedPermissionAlgorithm algorithm )
2018-08-15 10:16:46 +00:00
{
RemotePermissions perm ;
perm . fromArray ( value . utf16 ( ) ) ;
2024-03-25 21:06:11 +00:00
if ( algorithm = = MountedPermissionAlgorithm : : WildGuessMountedSubProperty ) {
return perm ;
}
if ( ( otherProperties . contains ( QStringLiteral ( " is-mount-root " ) ) & & otherProperties . value ( QStringLiteral ( " is-mount-root " ) ) = = QStringLiteral ( " false " ) & & perm . hasPermission ( RemotePermissions : : IsMounted ) ) | |
( ! otherProperties . contains ( QStringLiteral ( " is-mount-root " ) ) & & perm . hasPermission ( RemotePermissions : : IsMounted ) ) ) {
/* All the entries in a external storage have 'M' in their permission. However, for all
purposes in the desktop client , we only need to know about the mount points .
So replace the ' M ' by a ' m ' for every sub entries in an external storage */
perm . unsetPermission ( RemotePermissions : : IsMounted ) ;
perm . setPermission ( RemotePermissions : : IsMountedSub ) ;
qCInfo ( lcRemotePermissions ( ) ) < < otherProperties . value ( QStringLiteral ( " permissions " ) ) < < " replacing M permissions by m for subfolders inside a group folder " ;
}
2018-08-15 10:16:46 +00:00
return perm ;
}
2024-03-25 21:06:11 +00:00
RemotePermissions RemotePermissions : : fromServerString ( const QString & value ,
MountedPermissionAlgorithm algorithm ,
const QMap < QString , QString > & otherProperties )
{
return internalFromServerString ( value , otherProperties , algorithm ) ;
}
RemotePermissions RemotePermissions : : fromServerString ( const QString & value ,
MountedPermissionAlgorithm algorithm ,
const QVariantMap & otherProperties )
{
return internalFromServerString ( value , otherProperties , algorithm ) ;
}
2017-09-19 08:53:51 +00:00
} // namespace OCC