-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPlaylistController.cpp
More file actions
411 lines (341 loc) · 8.15 KB
/
PlaylistController.cpp
File metadata and controls
411 lines (341 loc) · 8.15 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "boost/filesystem.hpp"
#include "boost/filesystem/fstream.hpp"
#include "soci/soci/src/core/soci.h"
#include "soci/soci/src/backends/sqlite3/soci-sqlite3.h"
#include <algorithm>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include "PlaylistController.h"
#include "SettingsController.h"
using std::cout;
using std::map;
using std::string;
using std::vector;
using namespace soci;
using boost::filesystem::ofstream;
using boost::filesystem::ifstream;
using namespace boost::filesystem;
extern SettingsController sc;
PlaylistController::PlaylistController() {
}
void PlaylistController::addplaylist(Playlist& pl, const int pos) {
if(pos < 0) {
playlists.push_back(pl);
selection = begin() + offset;
dispoffset = size() - 1;
dispselection = begin() + dispoffset;
}
else if((size_t)pos < playlists.size()) {
playlists.insert(begin() + pos, pl);
selection = begin() + offset;
dispoffset = pos;
dispselection = begin() + dispoffset;
}
}
void PlaylistController::autoaddplaylist(path p) {
if(!exists(p))
return;
if(!is_directory(p))
return;
#if BOOST_FILESYSTEM_VERSION == 3
string n = p.filename().string();
#else
string n = p.filename();
#endif
if(n.empty())
return;
Playlist pl(n);
map<string,string> shows;
for(auto i = directory_iterator(p); i != directory_iterator(); ++i) {
#if BOOST_FILESYSTEM_VERSION == 3
shows[i->path().filename().string()] = i->path().string();
#else
shows[i->path().filename()] = i->path().string();
#endif
}
#if __GNUC__ <= 4 && __GNUC_MINOR__ < 6
for(auto i = shows.begin(); i != shows.end(); ++i) {
Show s((*i).second, (*i).first, EPISODE);
pl.add(s);
}
#else
for(auto &i: shows) {
Show s(i.second, i.first, EPISODE);
pl.add(s);
}
#endif
addplaylist(pl);
}
auto PlaylistController::begin() -> decltype(playlists.begin()) {
return playlists.begin();
}
auto PlaylistController::cbegin() -> decltype(playlists.cbegin()) {
return playlists.cbegin();
}
void PlaylistController::deleteselected() {
if (!playlists.empty()) {
playlists.erase(dispselection);
}
}
auto PlaylistController::end() -> decltype(playlists.end()) {
return playlists.end();
}
auto PlaylistController::cend() -> decltype(playlists.cend()) {
return playlists.cend();
}
bool PlaylistController::empty() {
return playlists.empty();
}
auto PlaylistController::getselection() -> decltype(selection) {
selection = begin() + offset;
return selection;
}
auto PlaylistController::getdispselection() -> decltype(selection) {
dispselection = begin() + dispoffset;
return dispselection;
}
size_t PlaylistController::getdispoffset() {
return dispoffset;
}
void PlaylistController::go() {
offset = dispoffset;
selection = dispselection;
}
void PlaylistController::go(decltype(selection) pos) {
//if(pos >= begin() && pos < end())
selection = pos;
}
void PlaylistController::offsetselection(size_t o) {
auto s = selection + o;
if(s >= begin() && s < end()) {
selection = s;
offset += o;
}
return;
}
void PlaylistController::offsetdispselection(size_t o) {
auto s = dispselection + o;
if(s >= begin() && s < end()) {
dispselection = s;
dispoffset += o;
}
return;
}
/**
Database loading and saving
*/
bool PlaylistController::loaddb() {
bool ret = true;
ifstream db;
db.open(sc.data);
size_t played, size;
unsigned int watched;
string name, type, file;
//TODO: Better error checking
while(db.good()) {
db >> played >> size;
db.ignore();
getline(db, name);
if(db.fail()) {
goto cleanup;
}
Playlist p(name);
for(size_t i = 0; i < size; ++i) {
db >> watched >> type;
db.ignore(100,'\n');
getline(db, name);
getline(db, file);
// The file was incorrectly formatted
if(db.fail()) {
ret = false;
goto cleanup;
}
Show s(file, name, Show::gettype(type), watched);
p.add(s);
}
Playlist pl(p.getname(),p);
addplaylist(pl);
}
cleanup:
selection = dispselection = begin();
offset = dispoffset = 0;
std::sort(begin(), end());
db.close();
return ret;
}
bool PlaylistController::savedb() {
ofstream db;
db.open(sc.data);
//TODO: Better error checking
#if __GNUC__ <= 4 && __GNUC_MINOR__ < 6
for(auto i = playlists.begin(); i < playlists.end(); ++i) {
(*i).printdetail(db);
db << '\n';
for(auto j = (*i).begin(); j < (*i).end(); ++j) {
(*j).printdetail(db);
db << '\n';
}
}
#else
for(Playlist& p : playlists) {
p.printdetail(db);
db << '\n';
for(Show& s : p) {
s.printdetail(db);
db << '\n';
}
}
#endif
db.close();
savedb_new();
return true;
}
bool PlaylistController::db_create(bool exists, session& db) {
/*
const double version = .2;
double v = 0;
indicator ind;
bool init = true, caught = false;
// TODO: remove exists once we drop old database stuff
if(exists) {
try {
db << "SELECT Number FROM Version", into(v, ind);
}
catch (const std::exception& e) {
init = true;
caught = true;
}
if(!caught && db.got_data()) {
switch(ind) {
case i_ok:
init = false;
break;
case i_null:
break;
case i_truncated:
break;
}
if(v < version) {
init = true;
}
}
}
else
init = true;
if(init) {
db << "CREATE TABLE IF NOT EXISTS Playlists ("
"Name TEXT PRIMARY KEY ASC NOT NULL"
")";
db << "CREATE TABLE IF NOT EXISTS Version ("
"Number REAL NOT NULL"
")";
db << "CREATE TABLE IF NOT EXISTS Shows ("
"File TEXT PRIMARY KEY ASC NOT NULL, "
"Name TEXT, "
"ID INT NOT NULL, "
"Watched INT DEFAULT 0, "
"Type TEXT NOT NULL DEFAULT EPISODE, "
"Playlist REFERENCES Playlists (Name) ON DELETE CASCADE ON UPDATE CASCADE"
")";
if(exists && !caught && v < version) {
db << "UPDATE Version SET Number = :version", use(version);
}
else
db << "INSERT INTO Version (Number) VALUES(:version)", use(version);
// TODO: remove this once we drop old database stuff
if(!exists) {
#if __GNUC__ <= 4 && __GNUC_MINOR__ < 6
int order = 0;
for(auto i = playlists.begin(); i < playlists.end(); ++i) {
db << "INSERT INTO Playlists VALUES(:NAME)", use(playlists[i]);
for(auto j = playlists[i].begin(); j < playlists[i].end(); ++j) {
db << "INSERT INTO Shows VALUES(:FILE,:NAME,:WATCHED,:TYPE,:PLAYLIST)",
use(*j), use((*j).getname(), "PLAYLIST");
}
}
#else
int order = 0;
for(Playlist& p : playlists) {
db << "INSERT INTO Playlists VALUES(:NAME)", use(p);
for(Show& s : p) {
db << "INSERT INTO Shows VALUES(:FILE,:NAME,:WATCHED,:TYPE,:PLAYLIST)",
use(s), use(p.getname(), "PLAYLIST");
}
}
#endif
}
db << "SELECT Number FROM Version", into(v, ind);
if(db.got_data()) {
switch(ind) {
case i_ok:
return true;
break;
case i_null:
return false;
break;
case i_truncated:
break;
}
}
}
return false;
*/
return true;
}
bool PlaylistController::savedb_new() {
/*
bool exists = boost::filesystem::exists(sc.db);
session db(sqlite3, sc.db.native());
// TODO: remove exists once we drop old database stuff
assert(db_create(exists, db));
#if __GNUC__ <= 4 && __GNUC_MINOR__ < 6
size_t size = playlists.size();
for(size_t i = 0; i < size; ++i) {
if(playlists[i].haschanged()) {
std::string name;
indicator ind;
db << "SELECT Name FROM Playlists WHERE Name == :OLDNAME", use(playlists[i]), into(name, ind);
if(ind == i_ok && db.got_data()) {
db << "UPDATE Playlists SET "
"Name=:NAME "
"WHERE Name == :OLDNAME", use(playlists[i]);
}
else {
db << "INSERT INTO Playlists VALUES(:NAME)", use(playlists[i]);
}
}
size_t shows_size = p.size();
for(size_t j = 0; j < shows_size; ++j) {
//s.printdetail(db);
//db << '\n';
}
}
#else
for(Playlist& p : playlists) {
if(p.haschanged()) {
std::string name;
indicator ind;
db << "SELECT Name FROM Playlists WHERE Name == :OLDNAME", use(p), into(name, ind);
if(ind == i_ok && db.got_data()) {
db << "UPDATE Playlists SET "
"Name=:NAME "
"WHERE Name == :OLDNAME", use(p);
}
else {
db << "INSERT INTO Playlists VALUES(:NAME)", use(p);
}
}
for(Show& s : p) {
//s.printdetail(db);
//db << '\n';
}
}
#endif
*/
return true;
}
size_t PlaylistController::size() { return playlists.size(); }