-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlist.cpp
903 lines (779 loc) · 25.3 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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
#include "playlist.h"
#include "signalhandler.h"
#include "sound.h"
#include "fileoperations.h"
#include "tinyxml2.h"
#include <fstream>
#include <VFSTools.h>
#include <VFSSystemPaths.h>
#include <sstream>
#include <map>
extern GtkBuilder *builder;
static std::map<std::string, std::list<song> > g_mPlaylists;
std::string stripcarriageret(std::string s) //For some reason, some files contain carriage returns, and Linux doesn't like them.
{
size_t pos = s.find('\r');
while(pos != std::string::npos)
{
s.erase(pos, 1); //Replace carriage returns
pos = s.find('\r');
}
return s;
}
//Remove all comments after delim is encountered on this line
void removecomment(std::string& s, char delim)
{
size_t pos = s.find(delim);
if(pos != std::string::npos)
s.erase(pos);
s = stripcarriageret(s);
}
void strip_leading_whitespace(std::string& s)
{
while(s[0] < '!') s.erase(0,1);
}
std::set<std::string> get_playlisttypes_supported()
{
std::set<std::string> sList;
sList.insert("m3u");
sList.insert("m3u8");
sList.insert("asx");
sList.insert("pls");
sList.insert("wpl");
sList.insert("xspf");
sList.insert("xml");
sList.insert("kiss");
return sList;
}
std::list<song> playlist_load(std::string sFilename)
{
std::list<song> ret;
if(sFilename.find(".m3u", sFilename.size() - 4) != std::string::npos ||
sFilename.find(".m3u8", sFilename.size() - 5) != std::string::npos)
ret = fill_out(playlist_load_M3U(sFilename), ttvfs::StripLastPath(sFilename));
else if(sFilename.find(".asx", sFilename.size() - 4) != std::string::npos)
ret = fill_out(playlist_load_ASX(sFilename), ttvfs::StripLastPath(sFilename));
else if(sFilename.find(".pls", sFilename.size() - 4) != std::string::npos)
ret = fill_out(playlist_load_PLS(sFilename), ttvfs::StripLastPath(sFilename));
else if(sFilename.find(".wpl", sFilename.size() - 4) != std::string::npos)
ret = fill_out(playlist_load_WPL(sFilename), ttvfs::StripLastPath(sFilename));
else if(sFilename.find(".xspf", sFilename.size() - 5) != std::string::npos)
ret = fill_out(playlist_load_XSPF(sFilename), ttvfs::StripLastPath(sFilename));
else if(sFilename.find(".xml", sFilename.size() - 4) != std::string::npos)
ret = fill_out(playlist_load_iTunes(sFilename), ttvfs::StripLastPath(sFilename));
else if(sFilename.find(".kiss", sFilename.size() - 5) != std::string::npos)
ret = convert_to_global(playlist_load_kissme(sFilename), ttvfs::StripLastPath(sFilename));
else
std::cout << "Playlist file format " << sFilename.substr(sFilename.size() - 3) << " unsupported." << std::endl;
return ret;
}
std::list<song> playlist_load_M3U(std::string sFilename)
{
std::list<song> ret;
std::ifstream infile(sFilename.c_str());
if(infile.fail())
{
std::cout << "Error: file " << sFilename << " does not exist." << std::endl;
return ret;
}
while(!infile.fail() && !infile.eof())
{
std::string s;
getline(infile, s);
removecomment(s, '#'); //Skip over m3u comments; we'll load all track data ourselves
if(s.size())
{
song so;
so.filename = s;
ret.push_back(so);
}
}
infile.close();
return ret;
}
std::list<song> playlist_load_ASX(std::string sFilename)
{
std::list<song> ret;
tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument;
int iErr = doc->LoadFile(sFilename.c_str());
if(iErr != tinyxml2::XML_NO_ERROR)
{
std::cout << "Error parsing XML file " << sFilename << ": Error " << iErr << std::endl;
delete doc;
return ret;
}
//Grab root element
tinyxml2::XMLElement* root = doc->RootElement();
if(root == NULL)
{
std::cout << "Error: Root element NULL in XML file " << sFilename << std::endl;
delete doc;
return ret;
}
//Read entries
for(tinyxml2::XMLElement* entry = root->FirstChildElement("entry"); entry != NULL; entry = entry->NextSiblingElement("entry"))
{
tinyxml2::XMLElement* ref = entry->FirstChildElement("ref");
if(ref != NULL)
{
const char* href = ref->Attribute("href");
if(href != NULL)
{
std::string s = href;
song so;
so.filename = s;
ret.push_back(so);
}
}
}
delete doc;
return ret;
}
std::list<song> playlist_load_PLS(std::string sFilename)
{
std::list<song> ret;
std::ifstream playlistFile(sFilename.c_str());
int num = 0;
if(!playlistFile.fail())
{
std::string s;
//Header starts with a [playlist] line
while(!s.size() && !playlistFile.fail())
{
getline(playlistFile,s);
removecomment(s, ';');
strip_leading_whitespace(s);
}
if(s != "[playlist]")
{
playlistFile.close();
return ret; //malformed
}
s.clear();
//Next up is a line that tells us how many items
while(!s.size() && !playlistFile.fail())
{
getline(playlistFile,s);
removecomment(s, ';');
strip_leading_whitespace(s);
}
if(s.find("NumberOfEntries") == std::string::npos)
{
playlistFile.close();
return ret; //malformed if this missing
}
s.erase(0, 15); //Strip off "NumberOfEntries"
strip_leading_whitespace(s); //Strip any whitespace
s.erase(0,1); //Erase '=' character
strip_leading_whitespace(s); //Strip any whitespace
std::istringstream iss(s);
iss >> num;
}
int cur = 0;
while(!playlistFile.fail() && !playlistFile.eof() && cur < num)
{
std::string s;
getline(playlistFile, s);
removecomment(s, ';');
strip_leading_whitespace(s);
if(s.size())
{
cur++;
std::ostringstream oss;
oss << "File" << cur;
if(s.find(oss.str())) //"FileX" isn't the beginning of this line; ignore
{
cur--;
continue;
}
s.erase(0, oss.str().size()); //Strip off "FileX"
strip_leading_whitespace(s); //Strip any whitespace
s.erase(0,1); //Erase '=' character
strip_leading_whitespace(s); //Strip any whitespace
//Done; add file
song so;
so.filename = s;
ret.push_back(so);
}
}
playlistFile.close();
return ret;
}
std::list<song> playlist_load_WPL(std::string sFilename)
{
std::list<song> ret;
tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument;
int iErr = doc->LoadFile(sFilename.c_str());
if(iErr != tinyxml2::XML_NO_ERROR)
{
std::cout << "Error parsing XML file " << sFilename << ": Error " << iErr << std::endl;
delete doc;
return ret;
}
//Grab root element
tinyxml2::XMLElement* root = doc->RootElement();
if(root == NULL)
{
std::cout << "Error: Root element NULL in XML file " << sFilename << std::endl;
delete doc;
return ret;
}
//Read entries
for(tinyxml2::XMLElement* body = root->FirstChildElement("body"); body != NULL; body = body->NextSiblingElement("body"))
{
for(tinyxml2::XMLElement* seq = body->FirstChildElement("seq"); seq != NULL; seq = seq->NextSiblingElement("seq"))
{
for(tinyxml2::XMLElement* media = seq->FirstChildElement("media"); media != NULL; media = media->NextSiblingElement("media"))
{
const char* src = media->Attribute("src");
if(src != NULL)
{
song so;
so.filename = src;
ret.push_back(so);
}
}
}
}
delete doc;
return ret;
}
std::list<song> playlist_load_XSPF(std::string sFilename)
{
std::list<song> ret;
tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument;
int iErr = doc->LoadFile(sFilename.c_str());
if(iErr != tinyxml2::XML_NO_ERROR)
{
std::cout << "Error parsing XML file " << sFilename << ": Error " << iErr << std::endl;
delete doc;
return ret;
}
//Grab root element
tinyxml2::XMLElement* root = doc->RootElement();
if(root == NULL)
{
std::cout << "Error: Root element NULL in XML file " << sFilename << std::endl;
delete doc;
return ret;
}
//Read entries
for(tinyxml2::XMLElement* tracklist = root->FirstChildElement("trackList"); tracklist != NULL; tracklist = tracklist->NextSiblingElement("trackList"))
{
for(tinyxml2::XMLElement* track = tracklist->FirstChildElement("track"); track != NULL; track = track->NextSiblingElement("track"))
{
tinyxml2::XMLElement* location = track->FirstChildElement("location");
if(location != NULL)
{
const char* src = location->GetText();
if(src != NULL)
{
song so;
so.filename = src;
ret.push_back(so);
}
}
}
}
delete doc;
return ret;
}
//I considered briefly how I would know that a particular .xml file would be in this exact format. I then realized that
//Apple programmers are the only people stupid enough to actually name an XML-based playlist format with a .xml file
//extension. Problem avoided.
//
//Of course, a user may be that stupid as well, but the resulting playlist would just be blank, so no worries.
std::list<song> playlist_load_iTunes(std::string sFilename)
{
std::list<song> ret;
tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument;
int iErr = doc->LoadFile(sFilename.c_str());
if(iErr != tinyxml2::XML_NO_ERROR)
{
std::cout << "Error parsing XML file " << sFilename << ": Error " << iErr << std::endl;
delete doc;
return ret;
}
//Grab root element
tinyxml2::XMLElement* root = doc->RootElement();
if(root == NULL)
{
std::cout << "Error: Root element NULL in XML file " << sFilename << std::endl;
delete doc;
return ret;
}
//Am I being too harsh about Apple programmers? Oh, wait, what's this? Three nested XML elements, all with the same name?
//Nah, I'm not being too harsh.
//Oh, and in case you think this is just an export format, take a look at your "Music/iTunes/iTunes Music Library.xml". Open
//it in an editor and watch just how often it's modified. Truncate it to 0 bytes, set it to read-only, and watch how iTunes
//merrily continues along as if nothing is wrong. Now ask yourself just what these people are thinking.
for(tinyxml2::XMLElement* dict = root->FirstChildElement("dict"); dict != NULL; dict = dict->NextSiblingElement("dict"))
{
for(tinyxml2::XMLElement* dict2 = dict->FirstChildElement("dict"); dict2 != NULL; dict2 = dict2->NextSiblingElement("dict"))
{
for(tinyxml2::XMLElement* dict3 = dict2->FirstChildElement("dict"); dict3 != NULL; dict3 = dict3->NextSiblingElement("dict"))
{
//Now we have to dig through a bajillion "key"s here to get the location...
for(tinyxml2::XMLElement* key = dict3->FirstChildElement("key"); key != NULL; key = key->NextSiblingElement("key"))
{
const char* name = key->GetText();
if(name != NULL && std::string(name) == "Location") //Key with name "Location" should have the path next
{
//Found the right thing; next XML element should be "string" with the actual location
//(Seriously, have they heard of XML attributes?)
tinyxml2::XMLElement* string = key->NextSiblingElement("string");
if(string != NULL)
{
const char* cPath = string->GetText();
if(cPath != NULL)
{
song so;
so.filename = cPath;
ret.push_back(so);
break; //Stop searching through this lowest-level dict
}
}
}
}
}
}
}
delete doc;
return ret; //Worst. XML format. Ever.
}
std::list<song> playlist_load_kissme(std::string sFilename)
{
std::list<song> ret;
/*std::ifstream playlistFile(sFilename.c_str());
while(!playlistFile.fail() && !playlistFile.eof())
{
std::string s;
getline(playlistFile, s);
if(s.size())
{
ret.push_back(song_get_tags(s));
}
}
playlistFile.close();*/
tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument;
int iErr = doc->LoadFile(sFilename.c_str());
if(iErr != tinyxml2::XML_NO_ERROR)
{
std::cout << "Error parsing XML file " << sFilename << ": Error " << iErr << std::endl;
delete doc;
return ret;
}
//Grab root element
tinyxml2::XMLElement* root = doc->RootElement();
if(root == NULL)
{
std::cout << "Error: Root element NULL in XML file " << sFilename << std::endl;
delete doc;
return ret;
}
for(tinyxml2::XMLElement* songelem = root->FirstChildElement("song"); songelem != NULL; songelem = songelem->NextSiblingElement("song"))
{
song s;
s.track = 0;
s.length = 0;
s.error = false;
const char* cFilename = songelem->Attribute("file");
const char* cTitle = songelem->Attribute("title");
const char* cArtist = songelem->Attribute("artist");
const char* cAlbum = songelem->Attribute("album");
if(cFilename == NULL) continue;
s.filename = cFilename;
if(cTitle != NULL)
s.title = cTitle;
else
s.title = ttvfs::StripFileExtension(ttvfs::PathToFileName(cFilename)); //At bare minimum, display filename as title
if(cArtist != NULL)
s.artist = cArtist;
if(cAlbum != NULL)
s.album = cAlbum;
songelem->QueryUnsignedAttribute("track", &s.track);
songelem->QueryIntAttribute("length", &s.length);
ret.push_back(s);
}
delete doc;
return ret;
}
void playlist_save_kissme(std::string sFilename, std::list<song> sFiles)
{
/*std::ofstream playlistFile(sFilename.c_str());
if(playlistFile.fail()) return;
for(std::list<song>::iterator i = sFiles.begin(); i != sFiles.end(); i++)
playlistFile << i->filename << std::endl;
playlistFile.close();*/
//Create XML
tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument;
tinyxml2::XMLElement* root = doc->NewElement("playlist");
doc->InsertFirstChild(root);
//Save songs
for(std::list<song>::iterator i = sFiles.begin(); i != sFiles.end(); i++)
{
tinyxml2::XMLElement* songelem = doc->NewElement("song");
root->InsertEndChild(songelem);
//Populate song with important metadata
songelem->SetAttribute("file", i->filename.c_str());
songelem->SetAttribute("title", i->title.c_str());
songelem->SetAttribute("artist", i->artist.c_str());
songelem->SetAttribute("album", i->album.c_str());
songelem->SetAttribute("track", i->track);
songelem->SetAttribute("length", i->length);
}
//Done
doc->SaveFile(sFilename.c_str());
delete doc;
}
void playlist_save_M3U(std::string sFilename, std::string sName)
{
std::list<song> sFiles = g_mPlaylists[sName];
std::ofstream pl(sFilename.c_str());
if(pl.fail())
{
std::cout << "Error creating playlist file " << sFilename << std::endl;
return;
}
pl << "#EXTM3U" << std::endl; //Write header
for(std::list<song>::iterator i = sFiles.begin(); i != sFiles.end(); i++)
{
pl << "#EXTINF:" << i->length << "," << i->title << " - " << i->artist << std::endl; //Write metadata
pl << i->filename << std::endl; //Write filename
}
pl.close();
}
void save_config()
{
std::string sConfigFilename = ttvfs::GetAppDir("kissme") + "/kissme.last";
gint root_x, root_y;
gint width, height;
bool maximized = get_window_maximized();
get_window_position(&root_x, &root_y);
get_window_size(&width, &height);
//Create XML document
tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument;
tinyxml2::XMLElement* root = doc->NewElement("config");
doc->InsertFirstChild(root);
//Save window data
tinyxml2::XMLElement* window = doc->NewElement("window");
root->InsertEndChild(window);
window->SetAttribute("maximized", maximized);
window->SetAttribute("posx", root_x);
window->SetAttribute("posy", root_y);
window->SetAttribute("width", width);
window->SetAttribute("height", height);
//Save audio data stuff
tinyxml2::XMLElement* audio = doc->NewElement("audio");
root->InsertEndChild(audio);
audio->SetAttribute("volume", getVolume());
audio->SetAttribute("repeat", get_repeat_mode());
//Done
doc->SaveFile(sConfigFilename.c_str());
delete doc;
}
void load_config() //Silently fail if config isn't here already
{
std::string sConfigFilename = ttvfs::GetAppDir("kissme") + "/kissme.last";
//Open config file
tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument;
int iErr = doc->LoadFile(sConfigFilename.c_str());
if(iErr != tinyxml2::XML_NO_ERROR)
{
delete doc;
return;
}
//Grab root element
tinyxml2::XMLElement* root = doc->RootElement();
if(root == NULL)
{
delete doc;
return;
}
//Load window
GtkWindow* w = GTK_WINDOW(gtk_builder_get_object(builder, "window1"));
tinyxml2::XMLElement* window = root->FirstChildElement("window");
if(window != NULL)
{
int width, height, posx, posy;
bool maximized = false;
window->QueryBoolAttribute("maximized", &maximized);
if(maximized)
gtk_window_maximize(w);
else
{
if(window->QueryIntAttribute("posx", &posx) == tinyxml2::XML_NO_ERROR &&
window->QueryIntAttribute("posy", &posy) == tinyxml2::XML_NO_ERROR)
gtk_window_move(w, posx, posy);
if(window->QueryIntAttribute("width", &width) == tinyxml2::XML_NO_ERROR &&
window->QueryIntAttribute("height", &height) == tinyxml2::XML_NO_ERROR)
gtk_window_resize(w, width, height);
}
}
//Load audio config
tinyxml2::XMLElement* audio = root->FirstChildElement("audio");
if(audio != NULL)
{
//volume
float fVolume = 1.0;
audio->QueryFloatAttribute("volume", &fVolume);
setVolume(fVolume);
//Update GUI to reflect this
gtk_scale_button_set_value(GTK_SCALE_BUTTON(gtk_builder_get_object(builder, "volumebutton1")), fVolume);
//Repeat
int iRepeat = REPEAT_NONE;
audio->QueryIntAttribute("repeat", &iRepeat);
set_repeat_mode(iRepeat);
}
delete doc;
}
//Load all playlists in user dir
void load_playlists()
{
ttvfs::StringList lFiles;
ttvfs::GetFileList(ttvfs::GetAppDir("kissme").c_str(), lFiles);
//Loop through all files in user dir (where we keep playlists)
for(ttvfs::StringList::iterator i = lFiles.begin(); i != lFiles.end(); i++)
{
if(i->find(".kiss", i->size() - 5) != std::string::npos) //Playlist file
{
std::list<song> sFiles = playlist_load_kissme(ttvfs::GetAppDir("kissme") + "/" + *i);
std::string sListName = ttvfs::StripFileExtension(*i);
//Add playlist to our manager
playlist_add(sListName, sFiles);
//Add new playlist to our view
GtkTreeIter iter;
GtkListStore* playlists = GTK_LIST_STORE(gtk_builder_get_object(builder, "Playlists"));
gtk_list_store_append(playlists, &iter);
GValue a = G_VALUE_INIT;
g_value_init (&a, G_TYPE_STRING);
g_value_set_static_string (&a, sListName.c_str());
gtk_list_store_set_value(playlists, &iter, 0, &a);
}
}
resort_playlist_pane(); //Make sure these are in alphabetical order
}
std::list<song> convert_to_global(std::list<song> sFilenames, std::string sPath)
{
std::set<std::string> lFileFilters = get_filetypes_supported();
for(std::list<song>::iterator i = sFilenames.begin(); i != sFilenames.end(); i++)
{
std::string s = ttvfs::FixPath(convert_to_path(i->filename));
if(!ttvfs::IsDirectory(ttvfs::StripLastPath(s).c_str())) //Relative path
i->filename.assign(ttvfs::FixPath(sPath + '/' + s));
else //Absolute path
i->filename.assign(s);
//Make sure this file type is supported
size_t pos = i->filename.find_last_of('.');
if(pos != std::string::npos && pos != i->filename.length()-1)
{
std::string substr = i->filename.substr(pos+1); //Get filename extension of song
if(!lFileFilters.count(to_lowercase(substr)))
{
i = sFilenames.erase(i); //Remove from list if it doesn't pass our filter
i--;
}
}
else
{
i = sFilenames.erase(i);
i--;
}
}
return sFilenames;
}
std::string convert_to_path(std::string sURI)
{
if(sURI.find("file://") != 0) return sURI; //Already formatted fine
sURI.erase(0, 7); //Erase "file://" part
if(sURI.find("localhost") == 0)
sURI.erase(0, 9); //Erase "localhost" part
sURI.erase(0,1); //Erase backslash
//Now, parse through, and replace escape sequences with proper value
for(size_t pos = sURI.find('%', 0); pos != std::string::npos; pos = sURI.find('%', pos))
{
//Get the two-digit hex value of this sequence
std::istringstream iss(sURI.substr(pos+1, 2));
int hex;
iss >> std::hex >> hex;
//Replace this portion of the string with this sequence
sURI.replace(pos, 3, 1, (char)hex);
pos++; //Skip over this character for next search, in case we just placed a '%' character
}
return sURI;
}
void playlist_play(std::string sName)
{
clear_now_playing();
std::list<song> sList = g_mPlaylists[sName];
for(std::list<song>::iterator i = sList.begin(); i != sList.end(); i++)
add_to_playlist(*i);
update_playlist_time();
}
void playlist_add(std::string sName, std::list<song> sSongs)
{
g_mPlaylists[sName] = sSongs;
}
void save()
{
save_config();
save_cur_playlist(NULL);
}
void load()
{
load_playlists();
load_config();
}
gboolean save_cur_playlist(gpointer data)
{
std::list<song> playlist = get_cur_playlist();
//Get current playlist filename
std::string sName = ttvfs::GetAppDir("kissme") + "/last";
std::string sPaneName = "last";
//Find selection
GtkTreeIter iter;
GtkTreeModel* tree_model = GTK_TREE_MODEL(gtk_builder_get_object(builder, "Playlists"));
if(gtk_tree_selection_get_selected(GTK_TREE_SELECTION(gtk_builder_get_object(builder, "selectedplaylists")), &tree_model, &iter))
{
GValue value = G_VALUE_INIT;
gtk_tree_model_get_value(tree_model, &iter, 0, &value);
const gchar* text = g_value_get_string(&value);
if(text != NULL)
{
sName = ttvfs::GetAppDir("kissme") + "/" + text;
sPaneName = text;
}
g_value_unset(&value);
}
sName += ".kiss";
if(!playlist.size()) return true; //Skip empty files
//Update our internal list
g_mPlaylists[sPaneName] = playlist;
//Save the playlist
playlist_save_kissme(sName, playlist);
return true;
}
void save_cur_playlist(std::string sName)
{
if(!sName.size()) return;
std::list<song> playlist = get_cur_playlist();
if(!playlist.size()) return;
//Get current playlist filename
std::string sFileName = ttvfs::GetAppDir("kissme") + "/" + sName + ".kiss";
//Update our internal list
g_mPlaylists[sName] = playlist;
//Save the playlist
playlist_save_kissme(sFileName, playlist);
}
std::list<song> fill_out(std::list<song> songs, std::string sPath)
{
songs = convert_to_global(songs, sPath);
for(std::list<song>::iterator i = songs.begin(); i != songs.end(); i++)
*i = song_get_tags(i->filename);
return songs;
}
//Blindly stolen from http://www.cplusplus.com/reference/list/list/sort/ . Thanks, guys!
bool compare_nocase (const std::string& first, const std::string& second)
{
unsigned int i=0;
while ( (i<first.length()) && (i<second.length()) )
{
if (tolower(first[i])<tolower(second[i])) return true;
else if (tolower(first[i])>tolower(second[i])) return false;
++i;
}
return ( first.length() < second.length() );
}
void resort_playlist_pane()
{
GtkListStore* playlists = GTK_LIST_STORE(gtk_builder_get_object(builder, "Playlists"));
GtkTreeView* view = GTK_TREE_VIEW(gtk_builder_get_object(builder, "treeview1"));
GtkTreePath* path = NULL;
gtk_list_store_clear(playlists);
//Grab the current playlists in a list (because maps sort case sensitive)
std::list<std::string> sPlaylists;
for(std::map<std::string, std::list<song> >::iterator i = g_mPlaylists.begin(); i != g_mPlaylists.end(); i++)
{
sPlaylists.push_back(i->first);
}
//Sort this new list
sPlaylists.sort(compare_nocase);
for(std::list<std::string>::iterator i = sPlaylists.begin(); i != sPlaylists.end(); i++)
{
//Add new playlist to our view
GtkTreeIter iter;
gtk_list_store_append(playlists, &iter);
GValue a = G_VALUE_INIT;
g_value_init (&a, G_TYPE_STRING);
g_value_set_static_string (&a, i->c_str());
gtk_list_store_set_value(playlists, &iter, 0, &a);
//Test and see if this is the currently-selected playlist
if(*i == playlist_currently_viewing())
path = gtk_tree_model_get_path(gtk_tree_view_get_model(view), &iter);
}
//Select current playlist
if(path != NULL)
{
gtk_tree_selection_select_path(gtk_tree_view_get_selection(view), path);
gtk_tree_path_free(path);
}
}
//Because Windows is stupid and case-insensitive and it makes me look bad otherwise
bool are_equal(std::string first, std::string second)
{
unsigned int i = 0;
while((i < first.length()) && (i < second.length()))
{
if(tolower(first[i]) != tolower(second[i])) return false;
++i;
}
return(first.length() == second.length());
}
bool is_playlist(std::string sName)
{
for(std::map<std::string, std::list<song> >::iterator i = g_mPlaylists.begin(); i != g_mPlaylists.end(); i++)
{
if(are_equal(sName, i->first))
return true;
}
return false;
}
void delete_playlist(std::string sName)
{
g_mPlaylists.erase(sName); //Remove playlist from our list of playlists
std::string sFilename = ttvfs::GetAppDir("kissme") + "/" + sName + ".kiss";
remove(sFilename.c_str()); //Remove playlist file, if it's there
//Playlist pane removal is handled by code elsewhere
}
void rename_playlist(std::string sOldName, std::string sNewName)
{
g_mPlaylists[sNewName] = g_mPlaylists[sOldName]; //Copy over our playlist
g_mPlaylists.erase(g_mPlaylists.find(sOldName)); //Remove old playlist
std::string sOldFilename = ttvfs::GetAppDir("kissme") + "/" + sOldName + ".kiss";
std::string sNewFilename = ttvfs::GetAppDir("kissme") + "/" + sNewName + ".kiss";
rename(sOldFilename.c_str(), sNewFilename.c_str());
}
void playlist_add_commandline(int argc, char** argv)
{
bool bPlay = false;
std::set<std::string> lFileFilters = get_filetypes_supported();
for(int i = 1; i < argc; i++)
{
std::string s = argv[i];
size_t pos = s.find_last_of('.');
if(pos != std::string::npos && pos != s.length()-1)
{
std::string substr = s.substr(pos+1); //Get filename extension of song
if(!lFileFilters.count(to_lowercase(substr)))
continue;
}
else
continue;
add_to_playlist(s);
bPlay = true;
}
if(bPlay)
{
button_play_clicked(NULL, NULL); //Start playing
}
}