Repository navigation
Expand file tree
/
Copy pathbookmark_automatic.cpp
More file actions
958 lines (739 loc) · 24.5 KB
/
Copy pathbookmark_automatic.cpp
File metadata and controls
958 lines (739 loc) · 24.5 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
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
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
#include "stdafx.h"
#include "SDK/playback_control.h"
#include "SDK/playlist.h"
#include "bookmark_core.h"
#include "bookmark_store.h"
#include "bookmark_automatic.h"
#include "bookmark_list_control.h"
#include "bookmark_preferences.h"
#include "utils.h"
#include "radio_filter_titleformat_hook.h"
using namespace glb;
namespace fltr = filters;
void bookmark_automatic::updateDummyTime() {
dummy.set_rt_time(playback_control::get()->playback_get_position());
if (is_cfg_LapseEnabled()) {
if (m_updatePlaylistLapseStart == DBL_MAX) {
m_updatePlaylistLapseStart = dummy.get_time();
}
m_updatePlaylistLapse = dummy.get_time() - m_updatePlaylistLapseStart;
}
if (m_updating || m_user_reset_after_current) {
bool b_data_srv_available = false;
pfc::string8 playing_playlist_name;
size_t playing_playlist_index;
if (dummy.need_playlist && !dummy.playlist.get_length())
{
size_t index_item;
bool bItemLoc = playlist_manager_v5::get()->get_playing_item_location(&playing_playlist_index, &index_item);
bool bPlaylist = false;
if (bItemLoc) {
FB2K_console_print_v("<update time> - Item location playlist index: ", playing_playlist_index);
bPlaylist = playlist_manager_v5::get()->playlist_get_name(playing_playlist_index, playing_playlist_name);
if (bPlaylist) {
FB2K_console_print_v("<update time> - Item location playlist name: ", playing_playlist_name);
}
}
else {
FB2K_console_print_v("<update time> - Unknown item location ", dummy.path);
//increase retries
++dummy.need_loc_retries;
}
b_data_srv_available = bItemLoc && bPlaylist;
}
else {
//..
}
bool bcan_autosave_newtrack = cfg_autosave_newtrack.get();
bcan_autosave_newtrack &= cfg_autosave_radio_newtrack.get() || !dummy.isRadio();
bool bexpent_retries = dummy.need_playlist && dummy.need_loc_retries > LOC_RETRIES;
//dummy.need_playlist could maybe be used in combination to m_updating_playlist ? updating meaning implying retries or delayed ?
bool b_write_store = false;
if (b_data_srv_available || !dummy.need_playlist || bexpent_retries) {
// loc available
if (!dummy.need_playlist) {
if (is_cfg_LapseEnabled()) {
//reduced verbose logs
if (m_updatePlaylistLapse < 3 || (atoi(cfg_lapse.get_value() - m_updatePlaylistLapse < 1 ) {
FB2K_console_print_v("Track details ready, checking delay... ", dummy.desc);
}
}
else {
FB2K_console_print_v("Track details done. ", dummy.get_desc());
}
}
if (!m_user_reset_after_current && is_cfg_LapseEnabled()) {
if(m_updatePlaylistLapse < get_cfg_lapse()) {
//do not wait for lapse
if (restored_dummy.path.get_length()) {
bool bradio_restored = isRestoredRadioDummy(dummy);
bool bdummy_restored = isRestoredDummy(dummy);
if (bradio_restored || bdummy_restored) {
ResetRestoredDummy();
cancelUpdating();
}
}
//fix empty playlist manually bookmarking before delay expires
if (dummy.need_playlist) {
//todo: remove m_updating from updateDummy()
updateDummy(nullptr);
if (bcan_autosave_newtrack) {
m_updating = true;
}
}
//
return;
//
}
else {
if (m_updatePlaylistLapseStart != DBL_MAX) {
dummy.set_rt_time(m_updatePlaylistLapseStart);
}
}
}
m_user_reset_after_current = false;
if (bcan_autosave_newtrack) {
// AUTO - CREATE
if (b_data_srv_available && dummy.need_playlist) {
updateDummy(nullptr);
if (is_cfg_LapseEnabled()) {
if (m_updatePlaylistLapseStart != DBL_MAX) {
dummy.set_rt_time(m_updatePlaylistLapseStart);
}
}
}
bool bres = upgradeDummy(nullptr, g_guiLists);
m_updatePlaylistLapseStart = DBL_MAX;
m_updating = dummy.need_playlist = false;
//
return;
//
}
}
else {
// loc not available
// check lapse first, retries may be > LOC_RETRIES for non-playlist items (queued, ...)
bool blapse_enabled_completed = is_cfg_LapseEnabled() && m_updatePlaylistLapse > get_cfg_lapse();
bool bexpent_retries = dummy.need_playlist && dummy.need_loc_retries > LOC_RETRIES;
if (blapse_enabled_completed || (!is_cfg_LapseEnabled() && dummy.need_loc_retries > LOC_RETRIES)) {
FB2K_console_print_v(PFC_string_formatter() << "<update time>: " << (blapse_enabled_completed ? "delayed" : "too many retries"));
if (!dummy.need_playlist) {
FB2K_console_print_v("delayed, queued, too many retries, details... ", dummy.get_desc());
}
if (bcan_autosave_newtrack) {
if (is_cfg_LapseEnabled()) {
if (m_updatePlaylistLapseStart != DBL_MAX) {
dummy.set_rt_time(m_updatePlaylistLapseStart);
}
}
// AUTO - CREATE
bool bres = upgradeDummy(nullptr, g_guiLists);
if (!dummy.need_playlist || bexpent_retries) {
m_updatePlaylistLapseStart = DBL_MAX;
m_updating = dummy.need_playlist = false;
}
//
return;
//
}
}
else {
// retry orphans
return;
//
}
}
// no delays, paused bookmarking ...
if (cfg_autosave_on_quit.get()) {
if (dummy.need_playlist) {
updateDummy(nullptr);
}
}
else {
//fix empty playlist, still need it for manual bookmarks
if (dummy.need_playlist) {
updateDummy(nullptr);
}
}
m_updating = dummy.need_playlist = false;
}
}
bool bookmark_automatic::fetchHelloRadioStationName(pfc::string8& out) {
if (!checkDummyIsRadio())
{
return false;
}
if (cfg_autosave_radio_comment.get()) {
//station
pfc::string8 station;
metadb_handle_ptr mhp;
if (playback_control::get()->get_now_playing(mhp)) {
metadb_info_container::ptr pmic;
mhp->get_info_ref(pmic);
if (pmic.get_ptr()) {
size_t pos = pmic->info().meta_find("title");
if (pos != SIZE_MAX) {
station = pmic->info().meta_get("title", 0);
}
}
}
if (station.get_length()) {
out = filters::autoFixEncoding(station.c_str()).c_str();
return true;
}
else {
if (m_ptfo_title.is_empty()) {
static_api_ptr_t<titleformat_compiler>()->compile_safe_ex(m_ptfo_title, "%title%");
}
pfc::string_formatter title;
if (playback_control::get()->playback_format_title(NULL, title, m_ptfo_title, NULL, playback_control::display_level_titles)) {
if (!title.equals("?")) {
out = title;
return true;
}
}
}
}
return false;
}
//Full update
void bookmark_automatic::updateDummy(const metadb_handle_ptr p_pmh_now_playing/*bool threaded*/) {
metadb_handle_ptr dbHandle_item;
if (core_api::is_main_thread()) {
playback_control::get()->get_now_playing(dbHandle_item);
}
else {
dbHandle_item = p_pmh_now_playing;
}
if (dbHandle_item.get_ptr()) {
bool b_done = false;
pfc::string_formatter songDesc;
titleformat_object::ptr desc_format;
static_api_ptr_t<titleformat_compiler>()->compile_safe_ex(desc_format, cfg_desc_format.get_value().c_str());
pfc::string8 songPath = dbHandle_item->get_path();
if (checkDummyIsRadio(songPath)) {
if (dummy.get_dyna() && !dummy.get_fdn().get_length()) {
//station
pfc::string8 station;
if (dbHandle_item.get_ptr()) {
//Station
file_info_impl fi;
dbHandle_item->get_info(fi);
size_t pos = fi.meta_find("title");
if (pos != SIZE_MAX) {
station = fi.meta_get("title", 0);
station = filters::autoFixEncoding(station.c_str()).c_str();
}
}
bool is_scoop = station.toLower().has_prefix("scoop");
pfc::string8 title;
titleformat_object::ptr tfo_fdn;
static_api_ptr_t<titleformat_compiler>()->compile_safe_ex(tfo_fdn, "%title%");
if (core_api::is_main_thread()) {
b_done = playback_control::get()->playback_format_title(NULL, title, tfo_fdn, NULL, playback_control::display_level_all);
}
else {
b_done = dbHandle_item->format_title(NULL, title, tfo_fdn, NULL);
}
if (title.startsWith("<?xml")) {
FB2K_console_print_v("Track check-in ", "(xml)");
}
else {
FB2K_console_print_v("Track check-in ", title);
}
bool piped = filters::is_dyna_double_pipe(dummy.get_fdn());
bool custom = false;
pfc::string8 artist;
static_api_ptr_t<titleformat_compiler>()->compile_safe_ex(tfo_fdn, "%artist%");
if (core_api::is_main_thread()) {
b_done = playback_control::get()->playback_format_title(NULL, artist, tfo_fdn, NULL, playback_control::display_level_all);
}
else {
b_done = dbHandle_item->format_title(NULL, artist, tfo_fdn, NULL);
}
if (is_scoop) { pfc::swap_t(artist, title); }
if (!artist.equals("?")) {
if (piped) {
dummy.set_fdn(PFC_string_formatter() << title << " - " << artist);
}
else {
custom = true;
if (title.get_length()) {
dummy.set_fdn(PFC_string_formatter() << title << "~" << artist);
}
else {
if (dummy.get_fdn().startsWith("<?xml")) {
FB2K_console_print_v("Track check-in ", "(xml)");
}
else {
FB2K_console_print_v("Track check-in ", dummy.get_fdn(), ", skipping artist ", artist);;
}
}
}
}
else {
dummy.set_fdn(title);
}
pfc::string8 album;
static_api_ptr_t<titleformat_compiler>()->compile_safe_ex(tfo_fdn, "%album%");
if (core_api::is_main_thread()) {
b_done = playback_control::get()->playback_format_title(NULL, album, tfo_fdn, NULL, playback_control::display_level_all);
}
else {
b_done = dbHandle_item->format_title(NULL, album, tfo_fdn, NULL);
}
if (custom) {
dummy.set_fdn(PFC_string_formatter() << dummy.get_fdn() << "~" << (album.equals("?") ? "" : album));
}
else {
}
pfc::string8 year;
static_api_ptr_t<titleformat_compiler>()->compile_safe_ex(tfo_fdn, "%year%");
if (core_api::is_main_thread()) {
b_done = playback_control::get()->playback_format_title(NULL, year, tfo_fdn, NULL, playback_control::display_level_all);
}
else {
b_done = dbHandle_item->format_title(NULL, year, tfo_fdn, NULL);
}
if (custom) {
dummy.set_fdn(PFC_string_formatter() << dummy.get_fdn() << "~" << (year.equals("?") ? "" : year) << "~vbm");
}
else {
}
}
//fb2k provides hook for new songs
if (core_api::is_main_thread()) {
b_done = playback_control::get()->playback_format_title(NULL, songDesc, desc_format, NULL, playback_control::display_level_all);
}
else {
b_done = dbHandle_item->format_title(NULL, songDesc, desc_format, NULL);
}
radio_filter_titleformat_hook ra_hook;
std::vector<pfc::string8>vfilters;
fltr::get_filters(cfg_txt_filter.get_value(), vfilters);
ra_hook.setData(dummy.get_fdn(), vfilters);
fltr::radio_nfo_type rnt;
fltr::get_radio_nfo(dummy.get_fdn(), rnt);
size_t pri_pos = fltr::parse_radio_info(rnt/*songDesc*/, &ra_hook, songDesc, cfg_desc_format.get_value());
if (cfg_autosave_radio_comment.get() && rnt.xml_station.get_length()) {
dummy.set_comment(rnt.xml_station);
}
//
}
else {
b_done = dbHandle_item->format_title(NULL, songDesc, desc_format, NULL);
}
if (!b_done) {
songDesc << "Could not generate description.";
}
pfc::string8 playing_playlist_name;
GUID guid_playing_playlist = pfc::guid_null;
auto pl_man = playlist_manager_v5::get();
bool playlist_available = false;
size_t index_item;
size_t index_playlist;
auto check_available = pl_man->get_playing_item_location(&index_playlist, &index_item);
if (check_available) {
bool bres = pl_man->get_playing_item_location(&index_playlist, &index_item);
playlist_available = pl_man->playlist_find_item(index_playlist, dbHandle_item, index_item);
}
if (!check_available || playlist_available) {
b_done &= pl_man->get_playing_item_location(&index_playlist, &index_item);
b_done &= pl_man->playlist_get_name(index_playlist, playing_playlist_name);
if (b_done) {
guid_playing_playlist = pl_man->playlist_get_guid(index_playlist);
}
else {
//..
}
}
else {
//todo: radio station without playlist ???
if (dummy.isRadio(songPath) && !dummy.get_desc().get_length() && !dummy.get_dyna()) {
b_done = false;
}
else {
b_done = true;
}
}
if (core_api::is_main_thread()) {
//monitor and auto-creation
m_updating = !b_done;
m_updating &= dummy.need_loc_retries <= LOC_RETRIES;
dummy.set_time(playback_control::get()->playback_get_position());
dummy.set_current_date();
}
dummy.path = songPath;
if (!dummy.isRadio()) {
dummy.set_desc(songDesc);
}
else {
if (!dummy.get_desc().get_length()) {
dummy.set_desc(songDesc);
}
else {
if (dummy.get_dyna()) {
dummy.set_desc(songDesc);
}
}
}
dummy.subsong = dbHandle_item->get_subsong_index();
if (playlist_available) {
dummy.playlist = playing_playlist_name;
dummy.guid_playlist = guid_playing_playlist;
dummy.need_playlist = !playlist_available;
}
//dyna
pfc::string8 station_name;
if (!dummy.get_comment().get_length() && fetchHelloRadioStationName(station_name)) {
dummy.set_comment(station_name);
}
if (core_api::is_main_thread()) {
if (m_updating) {
++dummy.need_loc_retries;
}
}
}
else {
if (!core_api::is_shutting_down()) {
dummy.reset();
}
}
}
bool bookmark_automatic::CheckAutoPlaylistFilter() {
if (cfg_autosave_filter_newtrack.get()) {
//Obtain individual names in the filter
std::vector<std::string> allowedPlaylists;
std::stringstream ss(cfg_autosave_newtrack_playlists.get_value().c_str());
std::string token;
while (std::getline(ss, token, ',')) {
allowedPlaylists.push_back(token);
}
//replace chars in the current playlist names
pfc::string8 dummyPlaylist = dummy.playlist.c_str();
dummyPlaylist.replace_char(',', '.');
auto find_it = std::find(allowedPlaylists.begin(), allowedPlaylists.end(), dummyPlaylist.c_str());
if (find_it == allowedPlaylists.end()) {
// nothing to do
return false;
}
}
return true;
}
bool bookmark_automatic::CheckRadioFilter(pfc::string8 p_song_desc, const pfc::string8 p_csvfilters, const pfc::string8 p_tf_filter) {
bool bres = true;
radio_filter_titleformat_hook ra_hook;
std::vector<pfc::string8>vfilters;
pfc::string8 songDesc = p_song_desc.get_length() ? p_song_desc : dummy.get_fdn();
if (!songDesc.get_length()) {
//
FB2K_console_print_v("Store skipping empty fdn, info: ", dummy.get_desc());
return false;
//
}
fltr::get_filters(p_csvfilters, vfilters);
ra_hook.setData(dummy.get_fdn(), vfilters);
pfc::string8 flt_in_csv;
std::vector<pfc::string8>vfields;
std::pair<size_t, size_t> primary_sig = fltr::get_radio_info_sigfields(songDesc, vfields);
if (!vfields.size()) vfields.push_back(songDesc);
std::pair<size_t, size_t> pres = fltr::filters_in_fields(vfields, vfilters);
if (pres == std::pair(SIZE_MAX, SIZE_MAX)) {
FB2K_console_print_v("RF allowed");
}
else if (pres.first <= fltr::kMinRadioFields) {
FB2K_console_print_v("RF blocked, skipping bm. Filter: ", vfilters[pres.second], ", primary field: ", vfields[pres.first]);
//todo: testing TF, should return.
bres = false;
}
else {
if (pres.second < SIZE_MAX) {
FB2K_console_print_v("RF allowed, bm sec. passed. Filter: ", vfilters[pres.second], ", field: ", vfields[pres.first]);
}
else {
FB2K_console_print_v("RF allowed, bm passed.");
}
}
FB2K_console_print_v("RF running TF filter...");
pfc::string_formatter sf_filter;
pfc::string8 filter_res;
bool b_done;
if (vfields.size() > fltr::kMinRadioFields) {
//todo: it is running get_radio_info_sigfields again
fltr::radio_nfo_type rnt{ songDesc, "", primary_sig, vfields};
size_t ires = fltr::parse_radio_info(rnt, &ra_hook, filter_res, p_tf_filter.c_str());
b_done = ires != SIZE_MAX;
}
else {
titleformat_object::ptr tfo_filter;
static_api_ptr_t<titleformat_compiler>()->compile_safe_ex(tfo_filter, p_tf_filter.c_str());
b_done = playback_control::get()->playback_format_title(&ra_hook, filter_res, tfo_filter, NULL, playback_control::display_level_all);
}
if (b_done)
{
if (pfc::string_is_numeric(filter_res)) {
if (atoi(filter_res) != 0) {
FB2K_console_print_v("RF TF blocked, skipping bm. Num. val > 0 : ", filter_res);
bres &= false;
}
else {
FB2K_console_print_v("RF TF allowed. Num. val: ", filter_res);
}
}
else {
pfc::string8 tmpstr;
if (std::find_if(vfilters.begin(), vfilters.end(), [filter_res](const pfc::string8 s)
{ return s.equals(filter_res); }) != vfilters.end()) {
FB2K_console_print_v("RF TF blocked with MATCH in filters, skipping bm. Filter: ", filter_res);
bres &= false;
}
}
}
else {
FB2K_console_print_v("RF TF error running expression.");
}
return bres;
}
bool bookmark_automatic::upgradeDummy(const metadb_handle_ptr p_pmh_now_playing, std::list< dlg::CListControlBookmark*> guiLists) {
if (dummy.isRadio()) {
if (!CheckRadioFilter()) {
return false;
}
}
bool bcan_retry = dummy.need_playlist && dummy.need_loc_retries < LOC_RETRIES;
bool blapse_completed = !is_cfg_LapseEnabled() || m_updatePlaylistLapse > get_cfg_lapse();
if (bcan_retry && !blapse_completed) {
return false;
}
const std::vector<bookmark_t>& masterList = g_store.GetMasterList();
FB2K_console_print_v("Checking store.");
size_t old_size = masterList.size();
if (dummy.get_desc().length() == 0) {
// nothing to do
FB2K_console_print_v("Skip save, no dummy description.");
return false;
}
metadb_handle_ptr track_bm;
metadb_handle_ptr track_current;
//not playing on_quit - is shutting down
bool bnowPlaying = false;
if (core_api::is_main_thread()) {
bnowPlaying = playback_control_v3::get()->get_now_playing(track_current);
}
else {
if (p_pmh_now_playing.get_ptr()) {
bnowPlaying = true;
track_current = p_pmh_now_playing;
}
else {
//..
}
}
if (bnowPlaying) {
track_bm = track_current;
}
else {
auto metadb_ptr = metadb::get();
track_bm = metadb_ptr->handle_create(dummy.path.c_str(), dummy.subsong);
}
auto track_subsong = bnowPlaying ? track_current->get_subsong_index() : track_bm->get_subsong_index();
auto track_length = track_bm->get_length();
bool bsamepath = pfc::string8(track_bm->get_path()).equals(dummy.path);
bsamepath &= track_subsong == dummy.subsong;
//todo: unify with local files which are rejected by duplicated criteria
bool bradio_restored = isRestoredRadioDummy(dummy);
bool bdummy_restored = isRestoredDummy(dummy);
if (!core_api::is_shutting_down()) {
if (bsamepath && (bradio_restored || bdummy_restored)) {
ResetRestoredDummy();
FB2K_console_print_v("Skipping, bm was being restored");
return false;
}
else {
//..
}
}
if (!CheckAutoPlaylistFilter()) {
FB2K_console_print_v("Filter is active and did not match, do not store a bookmark.");
//
return false;
//
}
if (is_cfg_Bookmarking()) {
bool allowed_duplicates = is_cfg_Dupli_Enabled();
bool realloc_prev_duplicate = is_cfg_Dupli_Remove_Prev();
size_t radio_signa_len = SIZE_MAX;
if (dummy.isRadio()) {
//todo:
radio_filter_titleformat_hook ra_hook;
std::vector<pfc::string8>vfilters;
fltr::get_filters( cfg_txt_filter.get_value(), vfilters);
ra_hook.setData(dummy.get_fdn(), vfilters);
//
fltr::radio_nfo_type rnt;
fltr::get_radio_nfo(dummy.get_fdn(), rnt);
pfc::string8 buff;
radio_signa_len = fltr::parse_radio_info(rnt, &ra_hook, buff, cfg_desc_format.get_value().c_str());
dummy.set_desc(buff);
//xml
dummy.set_fdn(rnt.radio_info);
}
size_t dup_ndx = SIZE_MAX;
for (auto rit = std::rbegin(masterList); rit != std::rend(masterList); ++rit) {
//rev. more renames
bool brev_start = dummy.get_time() < 2 * kMin_Lapse;
bool brev_time = abs(rit->get_time() - dummy.get_time()) <= 2 * kMin_Lapse;
bool brev_path_guid_subsong = rit->path.equals(dummy.path) && pfc::guid_equal(rit->guid_playlist, dummy.guid_playlist);
brev_path_guid_subsong = brev_path_guid_subsong && rit->subsong == dummy.subsong;
bool brev_desc_or_radio = !dummy.isRadio() || (rit->get_desc().equals(dummy.get_desc()));
bool bradio_same_desc_any_time = dummy.isRadio() && brev_path_guid_subsong;
//sig
if (radio_signa_len != SIZE_MAX && rit->get_desc().get_length() >= radio_signa_len) {
bradio_same_desc_any_time &= rit->get_desc().subString(0, radio_signa_len).equals(dummy.get_desc().subString(0, radio_signa_len));
}
else {
bradio_same_desc_any_time &= dummy.isRadio() && brev_path_guid_subsong && rit->get_desc().equals(dummy.get_desc());
}
//
if (dummy.need_playlist || (brev_time && brev_path_guid_subsong && brev_desc_or_radio) || bradio_same_desc_any_time) {
if (allowed_duplicates && brev_start) {
if (realloc_prev_duplicate) {
dup_ndx = std::distance(std::rbegin(masterList), rit);
dup_ndx = masterList.size() - dup_ndx - 1;
break;
}
}
else {
if (!dummy.need_playlist && (brev_time && brev_path_guid_subsong)) {
FB2K_console_print_v("Skipping duplicated bookmark: ", dummy.path);
}
bool bexpent_retries = dummy.need_playlist && dummy.need_loc_retries > LOC_RETRIES;
if (!bexpent_retries) {
FB2K_console_print_v("Nothing to do.");
// nothing to do
return false;
}
}
}
}
if (dup_ndx != SIZE_MAX) {
bit_array_bittable changeMask(bit_array_false(), masterList.size());
changeMask.set(dup_ndx, true);
g_store.Remove(changeMask, std::function<void()>([]() {}));
}
if (core_api::is_shutting_down() && masterList.size()) {
FB2K_console_print_v("Shutting down.");
//..
}
else {
dummy.need_playlist = false;
if (bookmark_store::get_no_refresh()) {
return false;
}
g_store.AddItem(std::move(bookmark_t(dummy)),
//callback
std::function<void()>([this, guiLists]() {
g_store.Write();
FB2K_console_print_v("Dummy stored");
fb2k::inMainThread([this, guiLists]() {
//UI
bool bscroll_list = cfg_autosave_focus_newtrack.get();
refresh_ui(bscroll_list, bscroll_list, guiLists);
});
}));
return true;
}
}
else {
FB2K_console_print_v("Auto-Bookmarking is paused");
}
return g_store.Size() != old_size;
}
void bookmark_automatic::ResetRestoredDummy() {
restored_dummy.reset();
}
void bookmark_automatic::ResetRestoredDummyTime() {
restored_dummy.set_time(0.0);
}
void bookmark_automatic::SetRestoredDummy(bookmark_t& bm) {
restored_dummy = bookmark_t(bm);
}
bool bookmark_automatic::isRestoredDummy(const bookmark_t& bm) {
if (bm.isRadio()) return false;
if (restored_dummy.get_time() || pfc::guid_equal(restored_dummy.guid_playlist, bm.guid_playlist) &&
(restored_dummy.path.equals(bm.path)) && abs(restored_dummy.get_time() - bm.get_time()) <= kRestoredLapse) {
return true;
}
return false;
}
bool bookmark_automatic::isRestoredRadioDummy(const bookmark_t& bm) {
if (!bm.isRadio()) return false;
if (pfc::guid_equal(restored_dummy.guid_playlist, bm.guid_playlist) &&
(restored_dummy.path.equals(bm.path)) && restored_dummy.get_desc().equals(bm.get_desc())) {
return true;
}
return false;
}
void bookmark_automatic::checkDeletedRestoredDummy(const bit_array& mask, size_t count) {
auto f = mask.find_first(true, 0, count);
if (f >= count) {
ResetRestoredDummy();
}
else {
for (auto n = f; n < count; n = mask.find_next(true, n, count)) {
bookmark_t tmpbm = g_store.GetItem(n);
if (isRestoredDummy(tmpbm)) {
ResetRestoredDummy();
break;
}
}
}
}
void bookmark_automatic::refresh_ui(bool bselect, bool bensure_visible, std::list< dlg::CListControlBookmark*> guiLists) {
fb2k::inMainThread([bselect, bensure_visible, guiLists]() {
for (auto it = guiLists.begin(); it != guiLists.end(); ++it) {
dlg::CListControlBookmark* lc = *it;
if (lc->TableEdit_IsActive()) {
lc->TableEdit_Abort(false);
}
size_t item = lc->GetItemCount() - 1;
if (lc->GetSortOrder()) {
item = 0;
}
if (bselect) {
lc->SelectNone();
}
lc->OnItemsInserted(item, 1, bselect);
if (bselect || bensure_visible) {
lc->EnsureItemVisible(item, false);
}
if (bselect) {
lc->SetFocusItem(item);
}
}
});
}
void bookmark_automatic::delete_item_ui(size_t index, std::list< dlg::CListControlBookmark*> guiLists) {
fb2k::inMainThread([index, guiLists]() {
for (auto it = guiLists.begin(); it != guiLists.end(); ++it) {
bit_array_bittable changeMask(bit_array_false(), g_primaryGuiList->GetItemCount());
size_t new_pos = index;
if ((*it)->GetSortOrder()) {
if (index >= (*it)->GetItemCount()) {
new_pos = 0;
}
else {
new_pos = (*it)->GetItemCount() - index;
}
}
auto isel = (*it)->GetSingleSel();
if ((*it)->GetSingleSel() == new_pos) {
if ((*it)->TableEdit_IsActive()) {
(*it)->TableEdit_Abort(false);
}
}
(*it)->SelectNone();
(*it)->OnItemRemoved(new_pos);
}
});
}