File indexing completed on 2024-05-05 04:51:38

0001 /*
0002     SPDX-FileCopyrightText: 2010 Michal Malek <michalm@jabster.pl>
0003     SPDX-FileCopyrightText: 1998-2010 Sebastian Trueg <trueg@k3b.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "k3bthememodel.h"
0009 #include "k3bthememanager.h"
0010 
0011 #include <KLocalizedString>
0012 #include <KIO/DeleteJob>
0013 
0014 #include <QFile>
0015 #include <QUrl>
0016 
0017 namespace K3b {
0018 
0019 ThemeModel::ThemeModel( ThemeManager* themeManager, QObject* parent )
0020     : QAbstractTableModel( parent ),
0021       m_themeManager( themeManager )
0022 {
0023 }
0024 
0025 
0026 ThemeModel::~ThemeModel()
0027 {
0028 }
0029 
0030 
0031 void ThemeModel::reload()
0032 {
0033     beginResetModel();
0034     m_themeManager->loadThemes();
0035     endResetModel();
0036 }
0037 
0038 
0039 Theme* ThemeModel::themeForIndex( const QModelIndex& index ) const
0040 {
0041     if( index.isValid() && index.row() >= 0 && index.row() < m_themeManager->themes().size() )
0042         return m_themeManager->themes().at( index.row() );
0043     else
0044         return 0;
0045 }
0046 
0047 
0048 QModelIndex ThemeModel::indexForTheme( Theme* theme, int column ) const
0049 {
0050     int i = m_themeManager->themes().indexOf( theme );
0051     if( i >= 0 && i < m_themeManager->themes().size() )
0052         return index( i, column );
0053     else
0054         return QModelIndex();
0055 }
0056 
0057 
0058 QVariant ThemeModel::data( const QModelIndex& index, int role ) const
0059 {
0060     if( Theme* theme = themeForIndex( index ) ) {
0061         if( Qt::DisplayRole == role ) {
0062             switch( index.column() ) {
0063                 case ThemeColumn: return theme->name();
0064                 case AuthorColumn: return theme->author();
0065                 case VersionColumn: return theme->version();
0066                 case CommentColumn: return theme->comment();
0067                 default: break;
0068             }
0069         }
0070     }
0071     return QVariant();
0072 }
0073 
0074 
0075 int ThemeModel::columnCount( const QModelIndex& /*parent*/ ) const
0076 {
0077     return NumColumns;
0078 }
0079 
0080 
0081 int ThemeModel::rowCount( const QModelIndex& parent ) const
0082 {
0083     if( parent.isValid() )
0084         return 0;
0085     else
0086         return m_themeManager->themes().size();
0087 }
0088 
0089 
0090 QVariant ThemeModel::headerData( int section, Qt::Orientation orientation, int role ) const
0091 {
0092     if( orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
0093         switch( section ) {
0094             case ThemeColumn: return i18n( "Theme" );
0095             case AuthorColumn: return i18n( "Author" );
0096             case VersionColumn: return i18n( "Version" );
0097             case CommentColumn: return i18n( "Comment" );
0098             default: return QVariant();
0099         }
0100     }
0101     return QVariant();
0102 }
0103 
0104 
0105 bool ThemeModel::removeRows( int row, int count, const QModelIndex& parent )
0106 {
0107     if( !parent.isValid() ) {
0108         beginRemoveRows( parent, row, row+count-1 );
0109         for( int i = 0; i < count; ++i ) {
0110             if( row >= 0 && row < m_themeManager->themes().size() ) {
0111                 QScopedPointer<Theme> theme(m_themeManager->themes().takeAt(row));
0112                 QString path = theme->path();
0113 
0114                 // delete k3b.theme file to avoid it to get loaded
0115                 QFile::remove( path + "/k3b.theme" );
0116 
0117                 // delete the theme data itself
0118                 KIO::del( QUrl::fromLocalFile( path ), KIO::HideProgressInfo );
0119             }
0120         }
0121         endRemoveRows();
0122         return true;
0123     }
0124     else {
0125         return false;
0126     }
0127 }
0128 
0129 } // namespace K3b
0130 
0131 #include "moc_k3bthememodel.cpp"