1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
/***************************************************************************
* Copyright (C) 2020-2021 by Ilya Kotov *
* forkotov02@ya.ru *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QtDebug>
#include <qmmp/qmmp.h>
#include "librarymodel.h"
class LibraryTreeItem
{
public:
LibraryTreeItem() {}
~LibraryTreeItem()
{
clear();
}
void clear()
{
name.clear();
type = Qmmp::UNKNOWN;
parent = nullptr;
qDeleteAll(children);
children.clear();
}
QString name;
Qmmp::MetaData type = Qmmp::UNKNOWN;
QList<LibraryTreeItem *> children;
LibraryTreeItem *parent = nullptr;
};
LibraryModel::LibraryModel(QObject *parent) : QAbstractItemModel(parent)
{
m_rootItem = new LibraryTreeItem;
refresh();
}
LibraryModel::~LibraryModel()
{
delete m_rootItem;
}
bool LibraryModel::canFetchMore(const QModelIndex &parent) const
{
if(!parent.isValid())
return false;
LibraryTreeItem *parentItem = static_cast<LibraryTreeItem *>(parent.internalPointer());
if(parentItem == m_rootItem || parentItem->type == Qmmp::TITLE)
return false;
else
return parentItem->children.isEmpty();
}
void LibraryModel::fetchMore(const QModelIndex &parent)
{
if(!parent.isValid())
return;
LibraryTreeItem *parentItem = static_cast<LibraryTreeItem *>(parent.internalPointer());
QSqlDatabase db = QSqlDatabase::database("qmmp_library_1");
if(!db.isOpen())
return;
if(parentItem->type == Qmmp::ARTIST)
{
QSqlQuery query(db);
query.prepare("SELECT DISTINCT Album from track_library WHERE Artist = :artist");
query.bindValue(":artist", parentItem->name);
bool ok = query.exec();
if(!ok)
{
qWarning("Library: exec error: %s", qPrintable(query.lastError().text()));
return;
}
while(query.next())
{
LibraryTreeItem *item = new LibraryTreeItem;
item->name = query.value("Album").toString();
item->type = Qmmp::ALBUM;
item->parent = parentItem;
parentItem->children << item;
qDebug() << parentItem->name << item->name;
}
}
else if(parentItem->type == Qmmp::ALBUM)
{
QSqlQuery query(db);
query.prepare("SELECT Title from track_library WHERE Artist = :artist AND Album = :album");
query.bindValue(":artist", parentItem->parent->name);
query.bindValue(":album", parentItem->name);
bool ok = query.exec();
if(!ok)
{
qWarning("Library: exec error: %s", qPrintable(query.lastError().text()));
return;
}
while(query.next())
{
LibraryTreeItem *item = new LibraryTreeItem;
item->name = query.value("Title").toString();
item->type = Qmmp::TITLE;
item->parent = parentItem;
parentItem->children << item;
}
qDebug() << parentItem->children.count();
}
}
QVariant LibraryModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid() || role != Qt::DisplayRole)
return QVariant();
QString name = static_cast<LibraryTreeItem *>(index.internalPointer())->name;
return name.isEmpty() ? tr("Unknown") : name;
}
QModelIndex LibraryModel::parent(const QModelIndex &child) const
{
if(!child.isValid())
return QModelIndex();
LibraryTreeItem *childItem = static_cast<LibraryTreeItem *>(child.internalPointer());
LibraryTreeItem *parentItem = childItem->parent;
if(parentItem == m_rootItem || !parentItem || !parentItem->parent)
return QModelIndex();
return createIndex(parentItem->parent->children.indexOf(parentItem), 0, parentItem);
}
QModelIndex LibraryModel::index(int row, int column, const QModelIndex &parent) const
{
if(parent.isValid() && parent.column() != 0)
return QModelIndex();
LibraryTreeItem *parentItem = parent.isValid() ? static_cast<LibraryTreeItem *>(parent.internalPointer()) :
m_rootItem;
if(row >= 0 && row < parentItem->children.count())
return createIndex(row, column, parentItem->children.at(row));
else
return QModelIndex();
}
int LibraryModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 1;
}
int LibraryModel::rowCount(const QModelIndex &parent) const
{
if(!parent.isValid())
return m_rootItem->children.count();
LibraryTreeItem *parentItem = static_cast<LibraryTreeItem *>(parent.internalPointer());
if(parentItem->type == Qmmp::TITLE)
return 0;
else
return qMax(1, parentItem->children.count());
}
void LibraryModel::refresh()
{
beginResetModel();
m_rootItem->clear();
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "qmmp_library_1");
db.setDatabaseName(Qmmp::configDir() + "/" + "library.sqlite");
db.open();
if(!db.isOpen())
{
endResetModel();
return;
}
QSqlQuery query(db);
bool ok = query.exec("SELECT DISTINCT Artist from track_library");
if(!ok)
qWarning("Library: exec error: %s", qPrintable(query.lastError().text()));
while(query.next())
{
LibraryTreeItem *item = new LibraryTreeItem;
item->name = query.value("Artist").toString();
item->type = Qmmp::ARTIST;
item->parent = m_rootItem;
m_rootItem->children << item;
}
endResetModel();
}
|