-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlist.cpp
76 lines (64 loc) · 2.08 KB
/
playlist.cpp
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
#include <QDir>
#include <QEventLoop>
#include <QStandardPaths>
#include "playlist.hpp"
#include "playlistselector.hpp"
Playlist::Playlist(QWidget *parent)
: QObject(parent)
, m_parent(parent)
{
m_configFile = QString("%1%2%3%4%5")
.arg(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation),
QDir::separator(), PROGRAM_NAME, QDir::separator(), PROGRAM_NAME".ini");
m_settings = new QSettings(m_configFile, QSettings::IniFormat, this);
}
std::map<QString, QString> Playlist::openPlayList()
{
std::map<QString, QString> playlist;
auto playlistNames = m_settings->childGroups();
QEventLoop loop;
PlaylistSelector selector(playlistNames);
connect(&selector, &PlaylistSelector::closed, &loop, &QEventLoop::quit);
selector.show();
loop.exec();
auto playlistName = selector.getSelection();
if (playlistName.isEmpty()) {
return {};
}
m_settings->beginGroup(playlistName[0]); /* PlaylistSelector will return a QStringList with just one QString */
for (auto key : m_settings->childKeys()) {
playlist[key] = m_settings->value(key).toString();
}
m_settings->endGroup();
return playlist;
}
int Playlist::removePlaylists()
{
int removed {};
auto playlistNames = m_settings->childGroups();
QEventLoop loop;
PlaylistSelector selector(playlistNames, QAbstractItemView::MultiSelection);
connect(&selector, &PlaylistSelector::closed, &loop, &QEventLoop::quit);
selector.show();
loop.exec();
auto selection = selector.getSelection();
if (selection.isEmpty()) {
goto exit;
}
for (auto group : selection) {
m_settings->beginGroup(group);
m_settings->remove("");
m_settings->endGroup();
++removed;
}
exit:
return removed;
}
void Playlist::savePlayList(QString playlistName, std::map<QString, QString> songs)
{
m_settings->beginGroup(playlistName);
for (const auto [songName, filePath] : songs) {
m_settings->setValue(songName, filePath);
}
m_settings->endGroup();
}