1 /*
2 SPDX-FileCopyrightText: 2010 Michal Malek <michalm@jabster.pl>
3 SPDX-FileCopyrightText: 1998-2010 Sebastian Trueg <trueg@k3b.org>
4
5 SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8 #include "k3bthememodel.h"
9 #include "k3bthememanager.h"
10
11 #include <KLocalizedString>
12 #include <KIO/DeleteJob>
13
14 #include <QFile>
15 #include <QUrl>
16
17 namespace K3b {
18
ThemeModel(ThemeManager * themeManager,QObject * parent)19 ThemeModel::ThemeModel( ThemeManager* themeManager, QObject* parent )
20 : QAbstractTableModel( parent ),
21 m_themeManager( themeManager )
22 {
23 }
24
25
~ThemeModel()26 ThemeModel::~ThemeModel()
27 {
28 }
29
30
reload()31 void ThemeModel::reload()
32 {
33 beginResetModel();
34 m_themeManager->loadThemes();
35 endResetModel();
36 }
37
38
themeForIndex(const QModelIndex & index) const39 Theme* ThemeModel::themeForIndex( const QModelIndex& index ) const
40 {
41 if( index.isValid() && index.row() >= 0 && index.row() < m_themeManager->themes().size() )
42 return m_themeManager->themes().at( index.row() );
43 else
44 return 0;
45 }
46
47
indexForTheme(Theme * theme,int column) const48 QModelIndex ThemeModel::indexForTheme( Theme* theme, int column ) const
49 {
50 int i = m_themeManager->themes().indexOf( theme );
51 if( i >= 0 && i < m_themeManager->themes().size() )
52 return index( i, column );
53 else
54 return QModelIndex();
55 }
56
57
data(const QModelIndex & index,int role) const58 QVariant ThemeModel::data( const QModelIndex& index, int role ) const
59 {
60 if( Theme* theme = themeForIndex( index ) ) {
61 if( Qt::DisplayRole == role ) {
62 switch( index.column() ) {
63 case ThemeColumn: return theme->name();
64 case AuthorColumn: return theme->author();
65 case VersionColumn: return theme->version();
66 case CommentColumn: return theme->comment();
67 default: break;
68 }
69 }
70 }
71 return QVariant();
72 }
73
74
columnCount(const QModelIndex &) const75 int ThemeModel::columnCount( const QModelIndex& /*parent*/ ) const
76 {
77 return NumColumns;
78 }
79
80
rowCount(const QModelIndex & parent) const81 int ThemeModel::rowCount( const QModelIndex& parent ) const
82 {
83 if( parent.isValid() )
84 return 0;
85 else
86 return m_themeManager->themes().size();
87 }
88
89
headerData(int section,Qt::Orientation orientation,int role) const90 QVariant ThemeModel::headerData( int section, Qt::Orientation orientation, int role ) const
91 {
92 if( orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
93 switch( section ) {
94 case ThemeColumn: return i18n( "Theme" );
95 case AuthorColumn: return i18n( "Author" );
96 case VersionColumn: return i18n( "Version" );
97 case CommentColumn: return i18n( "Comment" );
98 default: return QVariant();
99 }
100 }
101 return QVariant();
102 }
103
104
removeRows(int row,int count,const QModelIndex & parent)105 bool ThemeModel::removeRows( int row, int count, const QModelIndex& parent )
106 {
107 if( !parent.isValid() ) {
108 beginRemoveRows( parent, row, row+count-1 );
109 for( int i = 0; i < count; ++i ) {
110 if( row >= 0 && row < m_themeManager->themes().size() ) {
111 QScopedPointer<Theme> theme(m_themeManager->themes().takeAt(row));
112 QString path = theme->path();
113
114 // delete k3b.theme file to avoid it to get loaded
115 QFile::remove( path + "/k3b.theme" );
116
117 // delete the theme data itself
118 KIO::del( QUrl::fromLocalFile( path ), KIO::HideProgressInfo );
119 }
120 }
121 endRemoveRows();
122 return true;
123 }
124 else {
125 return false;
126 }
127 }
128
129 } // namespace K3b
130
131
132