2016-08-25 00:45:32 +02:00
# include "./syncthingdirectorymodel.h"
2017-02-20 18:03:20 +01:00
# include "./syncthingicons.h"
2016-11-08 19:42:50 +01:00
# include "./colors.h"
2016-09-30 23:55:25 +02:00
# include "../connector/syncthingconnection.h"
# include "../connector/utils.h"
2016-09-01 16:34:30 +02:00
2016-10-05 23:17:18 +02:00
# include <QStringBuilder>
2016-09-01 16:34:30 +02:00
using namespace ChronoUtilities ;
2016-08-25 00:45:32 +02:00
namespace Data {
SyncthingDirectoryModel : : SyncthingDirectoryModel ( SyncthingConnection & connection , QObject * parent ) :
2016-11-02 21:05:32 +01:00
SyncthingModel ( connection , parent ) ,
2017-02-20 18:03:20 +01:00
m_dirs ( connection . dirInfo ( ) )
2016-08-25 00:45:32 +02:00
{
connect ( & m_connection , & SyncthingConnection : : newConfig , this , & SyncthingDirectoryModel : : newConfig ) ;
connect ( & m_connection , & SyncthingConnection : : newDirs , this , & SyncthingDirectoryModel : : newDirs ) ;
connect ( & m_connection , & SyncthingConnection : : dirStatusChanged , this , & SyncthingDirectoryModel : : dirStatusChanged ) ;
}
/*!
* \ brief Returns the directory info for the spcified \ a index . The returned object is not persistent .
*/
const SyncthingDir * SyncthingDirectoryModel : : dirInfo ( const QModelIndex & index ) const
{
2016-09-06 22:55:49 +02:00
return ( index . parent ( ) . isValid ( ) ? dirInfo ( index . parent ( ) ) : ( static_cast < size_t > ( index . row ( ) ) < m_dirs . size ( ) ? & m_dirs [ static_cast < size_t > ( index . row ( ) ) ] : nullptr ) ) ;
2016-08-25 00:45:32 +02:00
}
QModelIndex SyncthingDirectoryModel : : index ( int row , int column , const QModelIndex & parent ) const
{
if ( ! parent . isValid ( ) ) {
// top-level: all dir labels/IDs
if ( row < rowCount ( parent ) ) {
2016-09-21 21:09:12 +02:00
return createIndex ( row , column , static_cast < quintptr > ( - 1 ) ) ;
2016-08-25 00:45:32 +02:00
}
} else if ( ! parent . parent ( ) . isValid ( ) ) {
// dir-level: dir attributes
if ( row < rowCount ( parent ) ) {
2016-09-21 21:09:12 +02:00
return createIndex ( row , column , static_cast < quintptr > ( parent . row ( ) ) ) ;
2016-08-25 00:45:32 +02:00
}
}
return QModelIndex ( ) ;
}
QModelIndex SyncthingDirectoryModel : : parent ( const QModelIndex & child ) const
{
2016-09-21 21:09:12 +02:00
return child . internalId ( ) ! = static_cast < quintptr > ( - 1 ) ? index ( static_cast < int > ( child . internalId ( ) ) , 0 , QModelIndex ( ) ) : QModelIndex ( ) ;
2016-08-25 00:45:32 +02:00
}
QVariant SyncthingDirectoryModel : : headerData ( int section , Qt : : Orientation orientation , int role ) const
{
switch ( orientation ) {
case Qt : : Horizontal :
switch ( role ) {
case Qt : : DisplayRole :
switch ( section ) {
case 0 : return tr ( " ID " ) ;
case 1 : return tr ( " Status " ) ;
}
break ;
default :
;
}
break ;
default :
;
}
return QVariant ( ) ;
}
QVariant SyncthingDirectoryModel : : data ( const QModelIndex & index , int role ) const
{
if ( index . isValid ( ) ) {
if ( index . parent ( ) . isValid ( ) ) {
// dir attributes
2016-09-06 22:55:49 +02:00
if ( static_cast < size_t > ( index . parent ( ) . row ( ) ) < m_dirs . size ( ) ) {
2016-08-25 00:45:32 +02:00
switch ( role ) {
case Qt : : DisplayRole :
case Qt : : EditRole :
switch ( index . column ( ) ) {
case 0 : // attribute names
switch ( index . row ( ) ) {
case 0 : return tr ( " ID " ) ;
case 1 : return tr ( " Path " ) ;
case 2 : return tr ( " Devices " ) ;
case 3 : return tr ( " Read-only " ) ;
case 4 : return tr ( " Rescan interval " ) ;
2016-09-01 16:34:30 +02:00
case 5 : return tr ( " Last scan " ) ;
case 6 : return tr ( " Last file " ) ;
2016-10-04 23:42:17 +02:00
case 7 : return tr ( " Errors " ) ;
2016-08-25 00:45:32 +02:00
}
break ;
case 1 : // attribute values
2016-09-21 21:09:12 +02:00
const SyncthingDir & dir = m_dirs [ static_cast < size_t > ( index . parent ( ) . row ( ) ) ] ;
2016-08-25 00:45:32 +02:00
switch ( index . row ( ) ) {
case 0 : return dir . id ;
case 1 : return dir . path ;
case 2 : return dir . devices . join ( QStringLiteral ( " , " ) ) ;
case 3 : return dir . readOnly ? tr ( " yes " ) : tr ( " no " ) ;
2016-09-03 19:39:43 +02:00
case 4 : return QString : : fromLatin1 ( TimeSpan : : fromSeconds ( dir . rescanInterval ) . toString ( TimeSpanOutputFormat : : WithMeasures , true ) . data ( ) ) ;
2016-09-01 16:34:30 +02:00
case 5 : return dir . lastScanTime . isNull ( ) ? tr ( " unknown " ) : QString : : fromLatin1 ( dir . lastScanTime . toString ( DateTimeOutputFormat : : DateAndTime , true ) . data ( ) ) ;
case 6 : return dir . lastFileName . isEmpty ( ) ? tr ( " unknown " ) : dir . lastFileName ;
2016-10-04 23:42:17 +02:00
case 7 : return dir . errors . empty ( ) ? tr ( " none " ) : tr ( " %1 item(s) out of sync " , nullptr , static_cast < int > ( dir . errors . size ( ) ) ) . arg ( dir . errors . size ( ) ) ;
2016-08-25 00:45:32 +02:00
}
break ;
}
break ;
2016-09-01 16:34:30 +02:00
case Qt : : ForegroundRole :
switch ( index . column ( ) ) {
case 1 :
2016-09-21 21:09:12 +02:00
const SyncthingDir & dir = m_dirs [ static_cast < size_t > ( index . parent ( ) . row ( ) ) ] ;
2016-09-01 16:34:30 +02:00
switch ( index . row ( ) ) {
case 5 :
if ( dir . lastScanTime . isNull ( ) ) {
2016-11-08 19:42:50 +01:00
return Colors : : gray ( m_brightColors ) ;
2016-09-01 16:34:30 +02:00
}
break ;
case 6 :
2016-12-22 23:43:35 +01:00
return dir . lastFileName . isEmpty ( ) ? Colors : : gray ( m_brightColors ) : ( dir . lastFileDeleted ? Colors : : red ( m_brightColors ) : QVariant ( ) ) ;
2016-10-05 22:33:10 +02:00
case 7 :
2016-11-08 19:42:50 +01:00
return dir . errors . empty ( ) ? Colors : : gray ( m_brightColors ) : Colors : : red ( m_brightColors ) ;
2016-09-01 16:34:30 +02:00
}
}
break ;
case Qt : : ToolTipRole :
switch ( index . column ( ) ) {
case 1 :
2016-09-21 21:09:12 +02:00
const SyncthingDir & dir = m_dirs [ static_cast < size_t > ( index . parent ( ) . row ( ) ) ] ;
2016-09-01 16:34:30 +02:00
switch ( index . row ( ) ) {
case 5 :
if ( ! dir . lastScanTime . isNull ( ) ) {
return agoString ( dir . lastScanTime ) ;
}
break ;
case 6 :
if ( ! dir . lastFileTime . isNull ( ) ) {
if ( dir . lastFileDeleted ) {
return tr ( " Deleted at %1 " ) . arg ( QString : : fromLatin1 ( dir . lastFileTime . toString ( DateTimeOutputFormat : : DateAndTime , true ) . data ( ) ) ) ;
} else {
return tr ( " Updated at %1 " ) . arg ( QString : : fromLatin1 ( dir . lastFileTime . toString ( DateTimeOutputFormat : : DateAndTime , true ) . data ( ) ) ) ;
}
}
break ;
2016-10-05 23:17:18 +02:00
case 7 :
if ( ! dir . errors . empty ( ) ) {
QStringList errors ;
errors . reserve ( static_cast < int > ( dir . errors . size ( ) ) ) ;
for ( const auto & error : dir . errors ) {
errors < < error . path ;
}
2017-03-22 16:25:28 +01:00
return QVariant ( QStringLiteral ( " <b> " ) % tr ( " Failed items " ) % QStringLiteral ( " </b><ul><li> " ) % errors . join ( QStringLiteral ( " </li><li> " ) ) % QStringLiteral ( " </li></ul> " ) % tr ( " Click for details " ) ) ;
2016-10-05 23:17:18 +02:00
}
2016-09-01 16:34:30 +02:00
}
}
2016-08-25 00:45:32 +02:00
default :
;
}
}
2016-09-06 22:55:49 +02:00
} else if ( static_cast < size_t > ( index . row ( ) ) < m_dirs . size ( ) ) {
2016-08-25 00:45:32 +02:00
// dir IDs and status
2016-09-06 22:55:49 +02:00
const SyncthingDir & dir = m_dirs [ static_cast < size_t > ( index . row ( ) ) ] ;
2016-08-25 00:45:32 +02:00
switch ( role ) {
case Qt : : DisplayRole :
case Qt : : EditRole :
switch ( index . column ( ) ) {
case 0 : return dir . label . isEmpty ( ) ? dir . id : dir . label ;
case 1 :
2017-02-20 21:00:18 +01:00
if ( dir . paused & & dir . status ! = SyncthingDirStatus : : OutOfSync ) {
return tr ( " Paused " ) ;
} else {
switch ( dir . status ) {
case SyncthingDirStatus : : Unknown : return tr ( " Unknown status " ) ;
case SyncthingDirStatus : : Unshared : return tr ( " Unshared " ) ;
case SyncthingDirStatus : : Idle : return tr ( " Idle " ) ;
case SyncthingDirStatus : : Scanning : return dir . progressPercentage > 0 ? tr ( " Scanning (%1 %) " ) . arg ( dir . progressPercentage ) : tr ( " Scanning " ) ;
case SyncthingDirStatus : : Synchronizing : return dir . progressPercentage > 0 ? tr ( " Synchronizing (%1 %) " ) . arg ( dir . progressPercentage ) : tr ( " Synchronizing " ) ;
case SyncthingDirStatus : : OutOfSync : return tr ( " Out of sync " ) ;
}
2016-08-25 00:45:32 +02:00
}
break ;
}
break ;
case Qt : : DecorationRole :
switch ( index . column ( ) ) {
case 0 :
2017-02-20 21:00:18 +01:00
if ( dir . paused & & dir . status ! = SyncthingDirStatus : : OutOfSync ) {
return statusIcons ( ) . pause ;
} else {
switch ( dir . status ) {
case SyncthingDirStatus : : Unknown :
case SyncthingDirStatus : : Unshared : return statusIcons ( ) . disconnected ;
case SyncthingDirStatus : : Idle : return statusIcons ( ) . idling ;
case SyncthingDirStatus : : Scanning : return statusIcons ( ) . scanninig ;
case SyncthingDirStatus : : Synchronizing : return statusIcons ( ) . sync ;
case SyncthingDirStatus : : OutOfSync : return statusIcons ( ) . error ;
}
2016-08-25 00:45:32 +02:00
}
break ;
}
break ;
case Qt : : TextAlignmentRole :
switch ( index . column ( ) ) {
case 0 : break ;
case 1 : return static_cast < int > ( Qt : : AlignRight | Qt : : AlignVCenter ) ;
}
break ;
case Qt : : ForegroundRole :
switch ( index . column ( ) ) {
case 0 : break ;
case 1 :
2017-02-20 21:00:18 +01:00
if ( dir . paused & & dir . status ! = SyncthingDirStatus : : OutOfSync ) {
break ;
} else {
switch ( dir . status ) {
case SyncthingDirStatus : : Unknown : break ;
case SyncthingDirStatus : : Idle : return Colors : : green ( m_brightColors ) ;
case SyncthingDirStatus : : Unshared : return Colors : : orange ( m_brightColors ) ;
case SyncthingDirStatus : : Scanning :
case SyncthingDirStatus : : Synchronizing : return Colors : : blue ( m_brightColors ) ;
case SyncthingDirStatus : : OutOfSync : return Colors : : red ( m_brightColors ) ;
}
2016-08-25 00:45:32 +02:00
}
break ;
}
break ;
2017-02-23 14:59:00 +01:00
case DirectoryStatus :
return static_cast < int > ( dir . status ) ;
case DirectoryPaused :
return dir . paused ;
2016-08-25 00:45:32 +02:00
default :
;
}
}
}
return QVariant ( ) ;
}
bool SyncthingDirectoryModel : : setData ( const QModelIndex & index , const QVariant & value , int role )
{
2016-09-06 22:55:49 +02:00
Q_UNUSED ( index ) Q_UNUSED ( value ) Q_UNUSED ( role )
2016-08-25 00:45:32 +02:00
return false ;
}
int SyncthingDirectoryModel : : rowCount ( const QModelIndex & parent ) const
{
if ( ! parent . isValid ( ) ) {
2016-09-06 22:55:49 +02:00
return static_cast < int > ( m_dirs . size ( ) ) ;
2016-08-25 00:45:32 +02:00
} else if ( ! parent . parent ( ) . isValid ( ) ) {
2016-10-05 22:33:10 +02:00
return 8 ;
2016-08-25 00:45:32 +02:00
} else {
return 0 ;
}
}
int SyncthingDirectoryModel : : columnCount ( const QModelIndex & parent ) const
{
if ( ! parent . isValid ( ) ) {
return 2 ; // label/ID, status/buttons
} else if ( ! parent . parent ( ) . isValid ( ) ) {
return 2 ; // field name and value
} else {
return 0 ;
}
}
void SyncthingDirectoryModel : : newConfig ( )
{
beginResetModel ( ) ;
}
void SyncthingDirectoryModel : : newDirs ( )
{
endResetModel ( ) ;
}
void SyncthingDirectoryModel : : dirStatusChanged ( const SyncthingDir & , int index )
{
const QModelIndex modelIndex1 ( this - > index ( index , 0 , QModelIndex ( ) ) ) ;
emit dataChanged ( modelIndex1 , modelIndex1 , QVector < int > ( ) < < Qt : : DecorationRole ) ;
const QModelIndex modelIndex2 ( this - > index ( index , 1 , QModelIndex ( ) ) ) ;
emit dataChanged ( modelIndex2 , modelIndex2 , QVector < int > ( ) < < Qt : : DisplayRole < < Qt : : ForegroundRole ) ;
2016-10-05 22:33:10 +02:00
emit dataChanged ( this - > index ( 0 , 1 , modelIndex1 ) , this - > index ( 7 , 1 , modelIndex1 ) , QVector < int > ( ) < < Qt : : DisplayRole ) ;
2016-08-25 00:45:32 +02:00
}
} // namespace Data