File indexing completed on 2024-07-07 14:11:59

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2010 Dennis Nienhüser <nienhueser@kde.org>
0004 //
0005 
0006 #include "MonavMapsModel.h"
0007 
0008 #include <QDate>
0009 
0010 namespace Marble
0011 {
0012 
0013 void MonavMapsModel::deleteMapFiles( int row )
0014 {
0015     if ( row >= 0 && row < m_data.size() ) {
0016         m_data.at( row ).remove();
0017         beginRemoveRows( QModelIndex(), row, row );
0018         m_data.remove( row );
0019         endRemoveRows();
0020     }
0021 }
0022 
0023 void MonavMapsModel::setInstallableVersions( const QMap<QString, QString> &remoteMaps )
0024 {
0025     m_remoteMaps = remoteMaps;
0026     beginResetModel();
0027     endResetModel();
0028 }
0029 
0030 MonavMapsModel::MonavMapsModel( const QVector<MonavMap> &data, QObject * parent ) :
0031     QAbstractTableModel( parent ), m_data( data )
0032 {
0033     std::sort( m_data.begin(), m_data.end(), &MonavMap::nameLessThan );
0034 }
0035 
0036 QVariant MonavMapsModel::headerData ( int section, Qt::Orientation orientation, int role ) const
0037 {
0038     if ( orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
0039         switch( section ) {
0040         case 0:
0041             return tr( "Name" );
0042         case 1:
0043             return tr( "Transport" );
0044         case 2:
0045             return tr( "Size" );
0046         case 3:
0047             return tr( "Update" );
0048         case 4:
0049             return tr( "Delete" );
0050         case 5:
0051             return tr( "Date" );
0052         }
0053     }
0054 
0055     return QVariant();
0056 }
0057 
0058 QVariant MonavMapsModel::data ( const QModelIndex & index, int role ) const
0059 {
0060     if ( index.isValid() && role == Qt::DisplayRole ) {
0061         int const row = index.row();
0062         if ( row >= 0 && row < m_data.size() )
0063             switch( index.column() ) {
0064             case 0:
0065                 return m_data.at( row ).name();
0066             case 1:
0067                 return m_data.at( row ).transport();
0068             case 2:
0069                 return QString( "%1 MB" ).arg( 1 + m_data.at( row ).size() / 1024 / 1024 );
0070             case 3: {
0071                 QString payload = m_data.at( row ).payload();
0072                 payload = payload.mid(payload.lastIndexOf(QLatin1Char('/')) + 1);
0073                 if ( m_remoteMaps.contains( payload ) ) {
0074                     QDate remote = QDate::fromString( m_remoteMaps[payload], "MM/dd/yy" );
0075                     QDate local = QDate::fromString( m_data.at( row ).date(), "MM/dd/yy" );
0076                     return remote > local;
0077                 }
0078 
0079                 return false;
0080             }
0081             case 4:
0082                 return QFileInfo( m_data.at( row ).directory().absolutePath() ).isWritable();
0083             case 5:
0084                 {
0085                     QDate date = QDate::fromString( m_data.at( row ).date(), "MM/dd/yy" );
0086                     if ( date.year() < 2000 ) {
0087                         // Qt interprets 11 as 1911
0088                         date.setDate( date.year() + 100, date.month(), date.day() );
0089                     }
0090                     return date.toString( Qt::SystemLocaleShortDate );
0091                 }
0092             }
0093     }
0094 
0095     return QVariant();
0096 }
0097 
0098 int MonavMapsModel::rowCount ( const QModelIndex & parent ) const
0099 {
0100     return parent.isValid() ? 0 : m_data.size();
0101 }
0102 
0103 int MonavMapsModel::columnCount ( const QModelIndex & parent ) const
0104 {
0105     return parent.isValid() ? 0 : 6;
0106 }
0107 
0108 QString MonavMapsModel::payload( int index ) const
0109 {
0110     if ( index >= 0 && index <= m_data.size() ) {
0111         return m_data.at( index ).payload();
0112     }
0113 
0114     return QString();
0115 }
0116 
0117 }
0118 
0119 #include "moc_MonavMapsModel.cpp"