1 /*
2    SPDX-FileCopyrightText: 2018 (c) Matthieu Gallien <matthieu_gallien@yahoo.fr>
3 
4    SPDX-License-Identifier: LGPL-3.0-or-later
5  */
6 
7 #include "viewsmodel.h"
8 
9 #include "viewslistdata.h"
10 
11 #include <KI18n/KLocalizedString>
12 
13 class ViewsModelPrivate
14 {
15 
16 public:
17 
18     ViewsListData *mViewsData = nullptr;
19 
20     ElisaUtils::PlayListEntryType mEmbeddedCategory = ElisaUtils::Unknown;
21 
22     QMap<ElisaUtils::PlayListEntryType, QUrl> mDefaultIcons = {{ElisaUtils::Album, QUrl{QStringLiteral("image://icon/view-media-album-cover")}},
23                                                                {ElisaUtils::Artist, QUrl{QStringLiteral("image://icon/view-media-artist")}},
24                                                                {ElisaUtils::Genre, QUrl{QStringLiteral("image://icon/view-media-genre")}},};
25 };
26 
ViewsModel(QObject * parent)27 ViewsModel::ViewsModel(QObject *parent)
28     : QAbstractListModel(parent), d(std::make_unique<ViewsModelPrivate>())
29 {
30 }
31 
32 ViewsModel::~ViewsModel() = default;
33 
roleNames() const34 QHash<int, QByteArray> ViewsModel::roleNames() const
35 {
36     auto result = QAbstractListModel::roleNames();
37 
38     result[ImageNameRole] = "image";
39     result[DatabaseIdRole] = "databaseId";
40     result[UseSecondTitleRole] = "useSecondTitle";
41     result[SecondTitleRole] = "secondTitle";
42     result[EntryCategoryRole] = "entryCategory";
43 
44     return result;
45 }
46 
flags(const QModelIndex & index) const47 Qt::ItemFlags ViewsModel::flags(const QModelIndex &index) const
48 {
49     if (!index.isValid()) {
50         return Qt::NoItemFlags;
51     }
52 
53     return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
54 }
55 
rowCount(const QModelIndex & parent) const56 int ViewsModel::rowCount(const QModelIndex &parent) const
57 {
58     if (parent.isValid()) {
59         return 0;
60     }
61 
62     if (!d->mViewsData) {
63         return 0;
64     }
65 
66     return d->mViewsData->count();
67 }
68 
data(const QModelIndex & index,int role) const69 QVariant ViewsModel::data(const QModelIndex &index, int role) const
70 {
71     auto result = QVariant{};
72 
73     if (!d->mViewsData) {
74         return result;
75     }
76 
77     if (!index.isValid()) {
78         return result;
79     }
80 
81     switch(role)
82     {
83     case Qt::DisplayRole:
84         result = d->mViewsData->title(index.row());
85         break;
86     case ColumnRoles::ImageNameRole:
87         result = d->mViewsData->iconUrl(index.row());
88         break;
89     case ColumnRoles::DatabaseIdRole:
90         result = d->mViewsData->databaseId(index.row());
91         break;
92     case ColumnRoles::SecondTitleRole:
93         result = d->mViewsData->secondTitle(index.row());
94         break;
95     case ColumnRoles::UseSecondTitleRole:
96         result = d->mViewsData->useSecondTitle(index.row());
97         break;
98     case ColumnRoles::EntryCategoryRole:
99         if (d->mViewsData->defaultEntry(index.row())) {
100             result = QStringLiteral("default");
101         } else {
102             switch (d->mViewsData->embeddedCategory())
103             {
104             case ElisaUtils::Album:
105                 result = i18nc("Title of the view of all albums", "Albums");
106                 break;
107             case ElisaUtils::Genre:
108                 result = i18nc("Title of the view of all genres", "Genres");
109                 break;
110             case ElisaUtils::Artist:
111                 result = i18nc("Title of the view of all artists", "Artists");
112                 break;
113             case ElisaUtils::Radio:
114             case ElisaUtils::Track:
115             case ElisaUtils::Unknown:
116             case ElisaUtils::Composer:
117             case ElisaUtils::FileName:
118             case ElisaUtils::Lyricist:
119             case ElisaUtils::Container:
120                 break;
121             }
122         }
123 
124         break;
125     }
126 
127     return result;
128 }
129 
index(int row,int column,const QModelIndex & parent) const130 QModelIndex ViewsModel::index(int row, int column, const QModelIndex &parent) const
131 {
132     auto result = QModelIndex();
133 
134     if (column != 0) {
135         return result;
136     }
137 
138     if (parent.isValid()) {
139         return result;
140     }
141 
142     result = createIndex(row, column);
143 
144     return result;
145 }
146 
parent(const QModelIndex & child) const147 QModelIndex ViewsModel::parent(const QModelIndex &child) const
148 {
149     Q_UNUSED(child)
150 
151     auto result = QModelIndex();
152 
153     return result;
154 }
155 
viewsData() const156 ViewsListData *ViewsModel::viewsData() const
157 {
158     return d->mViewsData;
159 }
160 
setViewsData(ViewsListData * viewsData)161 void ViewsModel::setViewsData(ViewsListData *viewsData)
162 {
163     if (d->mViewsData == viewsData) {
164         return;
165     }
166 
167     if (viewsData) {
168         beginResetModel();
169     }
170 
171     d->mViewsData = viewsData;
172     Q_EMIT viewsDataChanged();
173 
174     if (d->mViewsData) {
175         connect(d->mViewsData, &ViewsListData::dataAboutToBeAdded,
176                 this, &ViewsModel::dataAboutToBeAdded);
177         connect(d->mViewsData, &ViewsListData::dataAdded,
178                 this, &ViewsModel::dataAdded);
179         connect(d->mViewsData, &ViewsListData::dataAboutToBeRemoved,
180                 this, &ViewsModel::dataAboutToBeRemoved);
181         connect(d->mViewsData, &ViewsListData::dataRemoved,
182                 this, &ViewsModel::dataRemoved);
183         connect(d->mViewsData, &ViewsListData::dataModified,
184                 this, &ViewsModel::dataModified);
185         connect(d->mViewsData, &ViewsListData::dataAboutToBeReset,
186                 this, &ViewsModel::dataAboutToBeReset);
187         connect(d->mViewsData, &ViewsListData::dataReset,
188                 this, &ViewsModel::dataReset);
189 
190         endResetModel();
191     }
192 }
193 
dataAboutToBeAdded(int startIndex,int lastIndex)194 void ViewsModel::dataAboutToBeAdded(int startIndex, int lastIndex)
195 {
196     beginInsertRows({}, startIndex, lastIndex);
197 }
198 
dataAdded()199 void ViewsModel::dataAdded()
200 {
201     endInsertRows();
202 }
203 
dataAboutToBeRemoved(int startIndex,int lastIndex)204 void ViewsModel::dataAboutToBeRemoved(int startIndex, int lastIndex)
205 {
206     beginRemoveRows({}, startIndex, lastIndex);
207 }
208 
dataRemoved()209 void ViewsModel::dataRemoved()
210 {
211     endRemoveRows();
212 }
213 
dataModified(int currentIndex)214 void ViewsModel::dataModified(int currentIndex)
215 {
216     Q_EMIT dataChanged(index(currentIndex, 0), index(currentIndex, 0));
217 }
218 
dataAboutToBeReset()219 void ViewsModel::dataAboutToBeReset()
220 {
221     beginResetModel();
222 }
223 
dataReset()224 void ViewsModel::dataReset()
225 {
226     endResetModel();
227 }
228 
229 
230 #include "moc_viewsmodel.cpp"
231