-
Notifications
You must be signed in to change notification settings - Fork 1
/
resources.cpp
211 lines (181 loc) · 6.58 KB
/
resources.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
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
#include "resources.h"
#include <QDir>
#include <QJsonDocument>
#include <QJsonArray>
using SCMap = std::pair<std::vector<QString>, std::vector<Color>>;
Resources::Resources(QObject *parent) : QObject(parent) {
player.setPlaylist(&playlist);
// the order matters: readPlayinglist() first, readSetting() next
try {
readSonglist("songlist.json");
} catch (Exception const&) {
}
try {
readPlayinglist("playinglist.json");
} catch (Exception const&) {
}
refreshPlayinglist();
try {
readSetting("setting.json");
} catch (Exception const&) { // fallback to default
translator = Translator();
themeColor = QString("Black");
}
}
void Resources::refreshPlayinglist() {
QList<QMediaContent> pl;
for (const QString &f : playingList) {
pl.push_back(QUrl::fromLocalFile(f));
}
playlist.clear();
playlist.addMedia(pl);
}
void Resources::readPlayinglist(QString filename) {
QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
throw FatalError("Resources::readSonglist()");
}
QJsonParseError parseErr;
QJsonObject playinglistObj = QJsonDocument::fromJson(file.readAll(), &parseErr).object();
file.close();
if (parseErr.error != QJsonParseError::NoError) {
throw JsonOpenError("Resources::readSonglist()");
}
QJsonArray playinglistAry = playinglistObj.value("playinglist").toArray();
for (const QJsonValue& v : playinglistAry) {
playingList.push_back(v.toString());
}
}
void Resources::writePlayinglist(QString filename) {
QFile file(filename);
if (!file.open(QIODevice::WriteOnly)) {
throw FatalError("Resource::writeSonglist()");
}
QTextStream out(&file);
QJsonObject obj;
obj.insert("playinglist", QJsonArray::fromStringList(playingList));
QJsonDocument doc(obj);
out << doc.toJson();
file.close();
}
void Resources::readSonglist(QString filename) {
QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
throw FatalError("Resources::readSonglist()");
}
QJsonParseError parseErr;
QJsonObject songlistObj = QJsonDocument::fromJson(file.readAll(), &parseErr).object();
file.close();
if (parseErr.error != QJsonParseError::NoError) {
throw JsonOpenError("Resources::readSonglist()");
}
QStringList names = songlistObj.keys();
for (const QString &n : names) {
songlistNames.push_back(n);
}
for (const QString &n : songlistNames) {
QJsonArray ary = songlistObj[n].toArray();
songlists.push_back({});
for (const QJsonValue& v : ary) {
songlists.back().push_back(v.toString());
}
}
}
void Resources::writeSonglist(QString filename) {
QFile file(filename);
if (!file.open(QIODevice::WriteOnly)) {
throw FatalError("Resource::writeSonglist()");
}
QTextStream out(&file);
QJsonObject obj;
for (int i = 0; i < songlistNames.size(); i++) {
obj.insert(songlistNames.at(i), QJsonArray::fromStringList(songlists.at(i)));
}
QJsonDocument doc(obj);
out << doc.toJson();
file.close();
}
void Resources::readSetting(QString filename) {
QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
throw FatalError("Resources::readSetting()");
}
QJsonParseError parseErr;
QJsonObject settingObj = QJsonDocument::fromJson(file.readAll(), &parseErr).object();
file.close();
if (parseErr.error != QJsonParseError::NoError) {
throw JsonOpenError("Resources::readSetting()");
}
QString colorStr = settingObj.value("theme").toString();
themeColor = colorStr;
QString langStr = settingObj.value("language").toString();
translator = Translator(langStr.toStdString());
int volume = settingObj.value("volume").toInt(0);
player.setVolume(volume);
QMediaPlaylist::PlaybackMode mode =
static_cast<QMediaPlaylist::PlaybackMode>(
settingObj.value("play back mode").toInt(0));
playlist.setPlaybackMode(mode);
QString playingSongLoc = settingObj.value("current song location").toString();
auto iter = std::find(playingList.cbegin(), playingList.cend(), playingSongLoc);
if (iter != playingList.cend()) {
// not good for list
playlist.setCurrentIndex(std::distance(playingList.cbegin(), iter));
}
}
void Resources::writeSetting(QString filename) {
QFile file(filename);
if (!file.open(QIODevice::WriteOnly)) {
throw FatalError("Resource::writeSetting()");
}
QTextStream out(&file);
QJsonObject obj;
obj.insert("theme", QJsonValue(themeColor));
QString langStr = translator.isEnglish() ? "English" : "Chinese";
obj.insert("language", QJsonValue(langStr));
obj.insert("volume", QJsonValue(player.volume()));
obj.insert("play back mode", QJsonValue(static_cast<int>(playlist.playbackMode())));
obj.insert("current song location", QJsonValue(
playlist.currentMedia().canonicalUrl().toLocalFile()));
QJsonDocument doc(obj);
out << doc.toJson();
file.close();
}
Color Resources::parseColor(QString const& colorStr) {
static const SCMap scmap = std::make_pair<std::vector<QString>, std::vector<Color>>(
{"Black", "Red", "Green", "Yellow", "Blue", "Magenta", "Cyan", "White"},
{Color(NC::White, NC::Black),
Color(NC::White, NC::Red),
Color(NC::White, NC::Green),
Color(NC::White, NC::Yellow),
Color(NC::White, NC::Blue),
Color(NC::White, NC::Magenta),
Color(NC::White, NC::Cyan),
Color(NC::Black, NC::White)});
auto& keys = scmap.first;
auto& values = scmap.second;
auto iter = std::find(keys.begin(), keys.end(), colorStr);
if (iter == keys.end()) {
throw FatalError("Resources::parseColor()");
}
return values.at(std::distance(keys.begin(), iter));
}
Color Resources::parseHighlight(const QString &colorStr) {
static const SCMap highlightmap = std::make_pair<std::vector<QString>, std::vector<Color>>(
{"Black", "Red", "Green", "Yellow", "Blue", "Magenta", "Cyan", "White"},
{Color(NC::Cyan, NC::Black),
Color(NC::Black, NC::Red),
Color(NC::Yellow, NC::Green),
Color(NC::Cyan, NC::Yellow),
Color(NC::Yellow, NC::Blue),
Color(NC::Yellow, NC::Magenta),
Color(NC::Yellow, NC::Cyan),
Color(NC::White, NC::White)});
auto& keys = highlightmap.first;
auto& values = highlightmap.second;
auto iter = std::find(keys.begin(), keys.end(), colorStr);
if (iter == keys.end()) {
throw FatalError("Resources::parseColor()");
}
return values.at(std::distance(keys.begin(), iter));
}