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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
|
#include "../include/QuickMedia.hpp"
#include "../plugins/Manganelo.hpp"
#include "../plugins/Mangadex.hpp"
#include "../plugins/Youtube.hpp"
#include "../plugins/Pornhub.hpp"
#include "../plugins/Fourchan.hpp"
#include "../include/Scale.hpp"
#include "../include/Program.h"
#include "../include/VideoPlayer.hpp"
#include "../include/StringUtils.hpp"
#include "../include/GoogleCaptcha.hpp"
#include "../include/Notification.hpp"
#include <cppcodec/base64_rfc4648.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Text.hpp>
#include <SFML/Window/Event.hpp>
#include <json/reader.h>
#include <json/writer.h>
#include <assert.h>
#include <cmath>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <signal.h>
static const sf::Color back_color(30, 32, 34);
static const int DOUBLE_CLICK_TIME = 500;
static const std::string fourchan_google_captcha_api_key = "6Ldp2bsSAAAAAAJ5uyx_lx34lJeEpTLVkP5k04qc";
// Prevent writing to broken pipe from exiting the program
static void sigpipe_handler(int unused) {
}
static int x_error_handler(Display *display, XErrorEvent *event) {
return 0;
}
static int x_io_error_handler(Display *display) {
return 0;
}
namespace QuickMedia {
Program::Program() :
window(sf::VideoMode(800, 600), "QuickMedia"),
window_size(800, 600),
body(nullptr),
current_plugin(nullptr),
current_page(Page::SEARCH_SUGGESTION),
image_index(0)
{
window.setVerticalSyncEnabled(true);
if(!font.loadFromFile("../../../fonts/Lato-Regular.ttf")) {
fprintf(stderr, "Failed to load font: Lato-Regular.ttf\n");
abort();
}
if(!bold_font.loadFromFile("../../../fonts/Lato-Bold.ttf")) {
fprintf(stderr, "Failed to load font: Lato-Bold.ttf\n");
abort();
}
body = new Body(this, font, bold_font);
struct sigaction action;
action.sa_handler = sigpipe_handler;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
sigaction(SIGPIPE, &action, NULL);
XSetErrorHandler(x_error_handler);
XSetIOErrorHandler(x_io_error_handler);
}
Program::~Program() {
delete body;
delete current_plugin;
}
static SearchResult search_selected_suggestion(Body *input_body, Body *output_body, Plugin *plugin, std::string &selected_title, std::string &selected_url) {
BodyItem *selected_item = input_body->get_selected();
if(!selected_item)
return SearchResult::ERR;
selected_title = selected_item->title;
selected_url = selected_item->url;
output_body->clear_items();
SearchResult search_result = plugin->search(!selected_url.empty() ? selected_url : selected_title, output_body->items);
output_body->reset_selected();
return search_result;
}
static void usage() {
fprintf(stderr, "usage: QuickMedia <plugin> [--tor]\n");
fprintf(stderr, "OPTIONS:\n");
fprintf(stderr, "plugin The plugin to use. Should be either 4chan, manganelo, mangadex, pornhub or youtube\n");
fprintf(stderr, "--tor Use tor. Disabled by default\n");
fprintf(stderr, "EXAMPLES:\n");
fprintf(stderr, "QuickMedia manganelo\n");
fprintf(stderr, "QuickMedia youtube --tor\n");
}
static bool is_program_executable_by_name(const char *name) {
// TODO: Implement for Windows. Windows also uses semicolon instead of colon as a separator
char *env = getenv("PATH");
std::unordered_set<std::string> paths;
string_split(env, ':', [&paths](const char *str, size_t size) {
paths.insert(std::string(str, size));
return true;
});
for(const std::string &path_str : paths) {
Path path(path_str);
path.join(name);
if(get_file_type(path) == FileType::REGULAR)
return true;
}
return false;
}
int Program::run(int argc, char **argv) {
if(argc < 2) {
usage();
return -1;
}
current_plugin = nullptr;
std::string plugin_logo_path;
bool use_tor = false;
for(int i = 1; i < argc; ++i) {
if(!current_plugin) {
if(strcmp(argv[i], "manganelo") == 0) {
current_plugin = new Manganelo();
plugin_logo_path = "../../../images/manganelo_logo.png";
} else if(strcmp(argv[i], "mangadex") == 0) {
current_plugin = new Mangadex();
plugin_logo_path = "../../../images/mangadex_logo.png";
} else if(strcmp(argv[i], "youtube") == 0) {
current_plugin = new Youtube();
plugin_logo_path = "../../../images/yt_logo_rgb_dark_small.png";
} else if(strcmp(argv[i], "pornhub") == 0) {
current_plugin = new Pornhub();
plugin_logo_path = "../../../images/pornhub_logo.png";
} else if(strcmp(argv[i], "4chan") == 0) {
current_plugin = new Fourchan();
plugin_logo_path = "../../../images/4chan_logo.png";
}
}
if(strcmp(argv[i], "--tor") == 0) {
use_tor = true;
}
}
if(!current_plugin) {
usage();
return -1;
}
if(use_tor && !is_program_executable_by_name("torsocks")) {
fprintf(stderr, "torsocks needs to be installed (and accessible from PATH environment variable) when using the --tor option\n");
return -2;
}
current_plugin->use_tor = use_tor;
if(!plugin_logo_path.empty()) {
if(!plugin_logo.loadFromFile(plugin_logo_path)) {
fprintf(stderr, "Failed to load plugin logo, path: %s\n", plugin_logo_path.c_str());
return -2;
}
plugin_logo.generateMipmap();
plugin_logo.setSmooth(true);
}
search_bar = std::make_unique<SearchBar>(font, plugin_logo);
search_bar->text_autosearch_delay = current_plugin->get_search_delay();
while(window.isOpen()) {
switch(current_page) {
case Page::EXIT:
window.close();
break;
case Page::SEARCH_SUGGESTION:
body->draw_thumbnails = current_plugin->search_suggestions_has_thumbnails();
search_suggestion_page();
break;
#if 0
case Page::SEARCH_RESULT:
body->draw_thumbnails = current_plugin->search_results_has_thumbnails();
search_result_page();
break;
#endif
case Page::VIDEO_CONTENT:
body->draw_thumbnails = false;
video_content_page();
break;
case Page::EPISODE_LIST:
body->draw_thumbnails = false;
episode_list_page();
break;
case Page::IMAGES: {
body->draw_thumbnails = false;
window.setKeyRepeatEnabled(false);
window.setFramerateLimit(4);
image_page();
window.setFramerateLimit(0);
window.setKeyRepeatEnabled(true);
break;
}
case Page::CONTENT_LIST: {
body->draw_thumbnails = true;
content_list_page();
break;
}
case Page::CONTENT_DETAILS: {
body->draw_thumbnails = true;
content_details_page();
break;
}
case Page::IMAGE_BOARD_THREAD_LIST: {
body->draw_thumbnails = true;
image_board_thread_list_page();
break;
}
case Page::IMAGE_BOARD_THREAD: {
body->draw_thumbnails = true;
image_board_thread_page();
break;
}
default:
fprintf(stderr, "Page not implemented: %d\n", current_page);
window.close();
break;
}
}
return 0;
}
void Program::base_event_handler(sf::Event &event, Page previous_page, bool handle_keypress, bool clear_on_escape, bool handle_searchbar) {
if (event.type == sf::Event::Closed) {
current_page = Page::EXIT;
} else if(event.type == sf::Event::Resized) {
window_size.x = event.size.width;
window_size.y = event.size.height;
sf::FloatRect visible_area(0, 0, window_size.x, window_size.y);
window.setView(sf::View(visible_area));
} else if(handle_keypress && event.type == sf::Event::KeyPressed) {
if(event.key.code == sf::Keyboard::Up) {
body->select_previous_item();
} else if(event.key.code == sf::Keyboard::Down) {
body->select_next_item();
} else if(event.key.code == sf::Keyboard::Escape) {
current_page = previous_page;
if(clear_on_escape) {
body->clear_items();
body->reset_selected();
search_bar->clear();
}
}
} else if(handle_searchbar && event.type == sf::Event::TextEntered) {
search_bar->onTextEntered(event.text.unicode);
}
}
static std::string base64_encode(const std::string &data) {
return cppcodec::base64_rfc4648::encode(data);
}
static std::string base64_decode(const std::string &data) {
return cppcodec::base64_rfc4648::decode<std::string>(data);
}
static bool read_file_as_json(const Path &storage_path, Json::Value &result) {
std::string file_content;
if(file_get_content(storage_path, file_content) != 0)
return false;
Json::CharReaderBuilder json_builder;
std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader());
std::string json_errors;
if(!json_reader->parse(file_content.data(), file_content.data() + file_content.size(), &result, &json_errors)) {
fprintf(stderr, "Failed to read json, error: %s\n", json_errors.c_str());
return false;
}
return true;
}
static bool save_manga_progress_json(const Path &path, const Json::Value &json) {
Json::StreamWriterBuilder json_builder;
return file_overwrite(path, Json::writeString(json_builder, json)) == 0;
}
enum class SearchSuggestionTab {
ALL,
HISTORY
};
void Program::search_suggestion_page() {
std::string update_search_text;
bool search_running = false;
Body history_body(this, font, bold_font);
const float tab_text_size = 18.0f;
const float tab_height = tab_text_size + 10.0f;
sf::Text all_tab_text("All", font, tab_text_size);
sf::Text history_tab_text("History", font, tab_text_size);
struct Tab {
Body *body;
SearchSuggestionTab tab;
sf::Text *text;
};
std::array<Tab, 2> tabs = { Tab{body, SearchSuggestionTab::ALL, &all_tab_text}, Tab{&history_body, SearchSuggestionTab::HISTORY, &history_tab_text} };
int selected_tab = 0;
// TOOD: Make generic, instead of checking for plugin
if(current_plugin->is_manga()) {
Path content_storage_dir = get_storage_dir().join(current_plugin->name);
if(create_directory_recursive(content_storage_dir) != 0) {
show_notification("Storage", "Failed to create directory: " + content_storage_dir.data, Urgency::CRITICAL);
exit(1);
}
Path credentials_storage_dir = get_storage_dir().join("credentials");
if(create_directory_recursive(credentials_storage_dir) != 0) {
show_notification("Storage", "Failed to create directory: " + credentials_storage_dir.data, Urgency::CRITICAL);
exit(1);
}
// TODO: Make asynchronous
// TODO: Make this also work for mangadex. Would require storing both id and name of the manga
for_files_in_dir_sort_last_modified(content_storage_dir, [&history_body, this](const std::filesystem::path &filepath) {
Path fullpath(filepath.c_str());
Json::Value body;
if(!read_file_as_json(fullpath, body)) {
fprintf(stderr, "Failed to read json file: %s\n", fullpath.data.c_str());
return true;
}
auto filename = filepath.filename();
const Json::Value &manga_name = body["name"];
if(!filename.empty() && manga_name.isString()) {
// TODO: Add thumbnail
auto body_item = std::make_unique<BodyItem>(manga_name.asString());
if(current_plugin->name == "manganelo")
body_item->url = "https://manganelo.com/manga/" + base64_decode(filename.string());
else if(current_plugin->name == "mangadex")
body_item->url = "https://mangadex.org/title/" + base64_decode(filename.string());
history_body.items.push_back(std::move(body_item));
}
return true;
});
}
search_bar->onTextUpdateCallback = [&update_search_text, this, &tabs, &selected_tab](const std::string &text) {
if(tabs[selected_tab].body == body)
update_search_text = text;
else {
tabs[selected_tab].body->filter_search_fuzzy(text);
tabs[selected_tab].body->clamp_selection();
}
};
search_bar->onTextSubmitCallback = [this, &tabs, &selected_tab](const std::string &text) -> bool {
Page next_page = current_plugin->get_page_after_search();
// TODO: This shouldn't be done if search_selected_suggestion fails
if(search_selected_suggestion(tabs[selected_tab].body, body, current_plugin, content_title, content_url) != SearchResult::OK) {
show_notification("Search", "Search failed!", Urgency::CRITICAL);
return false;
}
if(next_page == Page::EPISODE_LIST && current_plugin->is_manga()) {
Manga *manga_plugin = static_cast<Manga*>(current_plugin);
if(content_url.empty()) {
show_notification("Manga", "Url is missing for manga!", Urgency::CRITICAL);
return false;
}
Path content_storage_dir = get_storage_dir().join(current_plugin->name);
std::string manga_id;
if(!manga_plugin->extract_id_from_url(content_url, manga_id))
return false;
manga_id_base64 = base64_encode(manga_id);
content_storage_file = content_storage_dir.join(manga_id_base64);
content_storage_json.clear();
content_storage_json["name"] = content_title;
FileType file_type = get_file_type(content_storage_file);
if(file_type == FileType::REGULAR)
read_file_as_json(content_storage_file, content_storage_json);
} else if(next_page == Page::VIDEO_CONTENT) {
watched_videos.clear();
if(content_url.empty())
next_page = Page::SEARCH_RESULT;
else {
page_stack.push(Page::SEARCH_SUGGESTION);
}
} else if(next_page == Page::CONTENT_LIST) {
content_list_url = content_url;
} else if(next_page == Page::IMAGE_BOARD_THREAD_LIST) {
image_board_thread_list_url = content_url;
}
current_page = next_page;
return true;
};
PluginResult front_page_result = current_plugin->get_front_page(body->items);
body->clamp_selection();
sf::Vector2f body_pos;
sf::Vector2f body_size;
bool redraw = true;
sf::Event event;
const sf::Color tab_selected_color(0, 85, 119);
const sf::Color tab_unselected_color(43, 45, 47);
sf::RectangleShape tab_spacing_rect(sf::Vector2f(0.0f, 0.0f));
tab_spacing_rect.setFillColor(tab_unselected_color);
const float tab_spacer_height = 0.0f;
sf::RectangleShape tab_drop_shadow;
tab_drop_shadow.setFillColor(sf::Color(23, 25, 27));
while (current_page == Page::SEARCH_SUGGESTION) {
while (window.pollEvent(event)) {
base_event_handler(event, Page::EXIT, false);
if(event.type == sf::Event::Resized || event.type == sf::Event::GainedFocus)
redraw = true;
else if(event.type == sf::Event::KeyPressed) {
if(event.key.code == sf::Keyboard::Up) {
tabs[selected_tab].body->select_previous_item();
} else if(event.key.code == sf::Keyboard::Down) {
tabs[selected_tab].body->select_next_item();
} else if(event.key.code == sf::Keyboard::Escape) {
current_page = Page::EXIT;
} else if(event.key.code == sf::Keyboard::Left) {
selected_tab = std::max(0, selected_tab - 1);
search_bar->clear();
} else if(event.key.code == sf::Keyboard::Right) {
selected_tab = std::min((int)tabs.size() - 1, selected_tab + 1);
search_bar->clear();
}
}
}
if(redraw) {
redraw = false;
search_bar->onWindowResize(window_size);
float body_padding_horizontal = 50.0f;
float body_padding_vertical = tab_spacer_height + tab_height + 50.0f;
float body_width = window_size.x - body_padding_horizontal * 2.0f;
if(body_width < 400) {
body_width = window_size.x;
body_padding_horizontal = 0.0f;
}
float search_bottom = search_bar->getBottomWithoutShadow();
body_pos = sf::Vector2f(body_padding_horizontal, search_bottom + body_padding_vertical);
body_size = sf::Vector2f(body_width, window_size.y - search_bottom - body_padding_vertical);
}
search_bar->update();
if(!update_search_text.empty() && !search_running) {
search_suggestion_future = std::async(std::launch::async, [this, update_search_text]() {
BodyItems result;
if(current_plugin->update_search_suggestions(update_search_text, result) != SuggestionResult::OK) {
show_notification("Search", "Search failed!", Urgency::CRITICAL);
}
return result;
});
update_search_text.clear();
search_running = true;
}
if(search_running && search_suggestion_future.valid() && search_suggestion_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready) {
if(update_search_text.empty()) {
body->items = search_suggestion_future.get();
body->clamp_selection();
} else {
search_suggestion_future.get();
}
search_running = false;
}
window.clear(back_color);
{
tab_spacing_rect.setPosition(0.0f, search_bar->getBottomWithoutShadow());
tab_spacing_rect.setSize(sf::Vector2f(window_size.x, tab_spacer_height));
window.draw(tab_spacing_rect);
tab_drop_shadow.setSize(sf::Vector2f(window_size.x, 5.0f));
tab_drop_shadow.setPosition(0.0f, std::floor(search_bar->getBottomWithoutShadow() + tab_height));
window.draw(tab_drop_shadow);
const float width_per_tab = window_size.x / tabs.size();
const float tab_y = tab_spacer_height + std::floor(search_bar->getBottomWithoutShadow() + tab_height * 0.5f - (tab_text_size + 5.0f) * 0.5f);
sf::RectangleShape tab_background(sf::Vector2f(std::floor(width_per_tab), tab_height));
int i = 0;
for(Tab &tab : tabs) {
if(i == selected_tab) {
tab.body->draw(window, body_pos, body_size);
tab_background.setFillColor(tab_selected_color);
} else {
tab_background.setFillColor(tab_unselected_color);
}
tab_background.setPosition(std::floor(i * width_per_tab), tab_spacer_height + std::floor(search_bar->getBottomWithoutShadow()));
window.draw(tab_background);
const float center = (i * width_per_tab) + (width_per_tab * 0.5f);
tab.text->setPosition(std::floor(center - tab.text->getLocalBounds().width * 0.5f), tab_y);
window.draw(*tab.text);
++i;
}
}
search_bar->draw(window, false);
window.display();
}
}
void Program::search_result_page() {
assert(false);
#if 0
search_bar->onTextUpdateCallback = [this](const std::string &text) {
body->filter_search_fuzzy(text);
body->clamp_selection();
};
search_bar->onTextSubmitCallback = [this](const std::string &text) {
BodyItem *selected_item = body->get_selected();
if(!selected_item)
return;
video_url = selected_item->url;
current_page = Page::VIDEO_CONTENT;
};
sf::Vector2f body_pos;
sf::Vector2f body_size;
bool redraw = true;
sf::Event event;
while (current_page == Page::SEARCH_RESULT) {
while (window.pollEvent(event)) {
base_event_handler(event, Page::SEARCH_SUGGESTION);
if(event.type == sf::Event::Resized || event.type == sf::Event::GainedFocus)
redraw = true;
}
if(redraw) {
redraw = false;
search_bar->onWindowResize(window_size);
float body_padding_horizontal = 50.0f;
float body_padding_vertical = 50.0f;
float body_width = window_size.x - body_padding_horizontal * 2.0f;
if(body_width < 400) {
body_width = window_size.x;
body_padding_horizontal = 0.0f;
}
float search_bottom = search_bar->getBottom();
body_pos = sf::Vector2f(body_padding_horizontal, search_bottom + body_padding_vertical);
body_size = sf::Vector2f(body_width, window_size.y - search_bottom - body_padding_vertical);
}
search_bar->update();
window.clear(back_color);
body->draw(window, body_pos, body_size);
search_bar->draw(window);
window.display();
}
#endif
}
struct XDisplayScope {
XDisplayScope(Display *_display) : display(_display) {
}
~XDisplayScope() {
if(display)
XCloseDisplay(display);
}
Display *display;
};
enum class WindowFullscreenState {
UNSET,
SET,
TOGGLE
};
static bool window_set_fullscreen(Display *display, Window window, WindowFullscreenState state) {
Atom wm_state_atom = XInternAtom(display, "_NET_WM_STATE", False);
Atom wm_state_fullscreen_atom = XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", False);
if(wm_state_atom == False || wm_state_fullscreen_atom == False) {
fprintf(stderr, "Failed to fullscreen window. The window manager doesn't support fullscreening windows.\n");
return false;
}
XEvent xev;
xev.type = ClientMessage;
xev.xclient.window = window;
xev.xclient.message_type = wm_state_atom;
xev.xclient.format = 32;
xev.xclient.data.l[0] = (int)state;
xev.xclient.data.l[1] = wm_state_fullscreen_atom;
xev.xclient.data.l[2] = 0;
xev.xclient.data.l[3] = 1;
xev.xclient.data.l[4] = 0;
if(!XSendEvent(display, XDefaultRootWindow(display), 0, SubstructureRedirectMask | SubstructureNotifyMask, &xev)) {
fprintf(stderr, "Failed to fullscreen window\n");
return false;
}
XFlush(display);
return true;
}
void Program::video_content_page() {
search_bar->onTextUpdateCallback = nullptr;
search_bar->onTextSubmitCallback = nullptr;
Page previous_page = pop_page_stack();
Display* disp = XOpenDisplay(NULL);
if (!disp)
throw std::runtime_error("Failed to open display to X11 server");
XDisplayScope display_scope(disp);
std::unique_ptr<sf::RenderWindow> video_player_ui_window;
auto on_window_create = [disp, &video_player_ui_window](sf::WindowHandle video_player_window) {
int screen = DefaultScreen(disp);
Window ui_window = XCreateWindow(disp, RootWindow(disp, screen),
0, 0, 1, 1, 0,
DefaultDepth(disp, screen),
InputOutput,
DefaultVisual(disp, screen),
0, NULL);
XReparentWindow(disp, ui_window, video_player_window, 0, 0);
XMapWindow(disp, ui_window);
XFlush(disp);
video_player_ui_window = std::make_unique<sf::RenderWindow>(ui_window);
video_player_ui_window->setVerticalSyncEnabled(true);
};
bool ui_resize = true;
bool seekable = false;
std::unique_ptr<VideoPlayer> video_player;
auto load_video_error_check = [this, &video_player, previous_page]() {
watched_videos.insert(content_url);
VideoPlayer::Error err = video_player->load_video(content_url.c_str(), window.getSystemHandle());
if(err != VideoPlayer::Error::OK) {
std::string err_msg = "Failed to play url: ";
err_msg += content_url;
show_notification("Video player", err_msg.c_str(), Urgency::CRITICAL);
current_page = previous_page;
}
};
video_player = std::make_unique<VideoPlayer>(current_plugin->use_tor, [this, &video_player, &seekable, &load_video_error_check, previous_page](const char *event_name) {
bool end_of_file = false;
if(strcmp(event_name, "pause") == 0) {
double time_remaining = 0.0;
if(video_player->get_time_remaining(&time_remaining) == VideoPlayer::Error::OK && time_remaining <= 1.0)
end_of_file = true;
} else if(strcmp(event_name, "playback-restart") == 0) {
video_player->set_paused(false);
video_player->is_seekable(&seekable);
}
if(end_of_file) {
std::string new_video_url;
BodyItems related_media = current_plugin->get_related_media(content_url);
// Find video that hasn't been played before in this video session
for(auto it = related_media.begin(), end = related_media.end(); it != end; ++it) {
if(watched_videos.find((*it)->url) == watched_videos.end()) {
new_video_url = (*it)->url;
break;
}
}
// If there are no videos to play, then dont play any...
if(new_video_url.empty()) {
show_notification("Video player", "No more related videos to play");
current_page = previous_page;
return;
}
content_url = std::move(new_video_url);
load_video_error_check();
}
}, on_window_create);
load_video_error_check();
auto on_doubleclick = [this, disp]() {
window_set_fullscreen(disp, window.getSystemHandle(), WindowFullscreenState::TOGGLE);
};
sf::Clock ui_hide_timer;
bool ui_visible = true;
const int UI_HIDE_TIMEOUT = 4500;
sf::Clock time_since_last_left_click;
int left_click_counter;
sf::Event event;
sf::RectangleShape rect;
rect.setFillColor(sf::Color::Red);
sf::Clock get_progress_timer;
double progress = 0.0;
// Clear screen before playing video, to show a black screen instead of being frozen
// at the previous UI for a moment
window.clear();
window.display();
while (current_page == Page::VIDEO_CONTENT) {
while (window.pollEvent(event)) {
base_event_handler(event, previous_page, true, false, false);
if(event.type == sf::Event::Resized) {
if(video_player_ui_window)
ui_resize = true;
} else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space) {
if(video_player->toggle_pause() != VideoPlayer::Error::OK) {
fprintf(stderr, "Failed to toggle pause!\n");
}
} else if(event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) {
if(time_since_last_left_click.restart().asMilliseconds() <= DOUBLE_CLICK_TIME) {
if(++left_click_counter == 2) {
on_doubleclick();
left_click_counter = 0;
}
} else {
left_click_counter = 1;
}
} else if(event.type == sf::Event::MouseMoved) {
ui_hide_timer.restart();
if(!ui_visible) {
ui_visible = true;
video_player_ui_window->setVisible(true);
//window.setMouseCursorVisible(true);
}
}
}
if(video_player_ui_window) {
while(video_player_ui_window->pollEvent(event)) {
if(event.type == sf::Event::Resized) {
sf::FloatRect visible_area(0, 0, event.size.width, event.size.height);
video_player_ui_window->setView(sf::View(visible_area));
}
}
}
VideoPlayer::Error update_err = video_player->update();
if(update_err == VideoPlayer::Error::FAIL_TO_CONNECT_TIMEOUT) {
show_notification("Video player", "Failed to connect to mpv ipc after 5 seconds", Urgency::CRITICAL);
current_page = previous_page;
return;
} else if(update_err != VideoPlayer::Error::OK) {
show_notification("Video player", "Unexpected error while updating", Urgency::CRITICAL);
current_page = previous_page;
return;
}
// TODO: Show loading video animation
//window.clear();
//window.display();
if(video_player->is_connected() && get_progress_timer.getElapsedTime().asMilliseconds() >= 500) {
get_progress_timer.restart();
video_player->get_progress(&progress);
}
if(video_player_ui_window) {
if(!ui_visible) {
std::this_thread::sleep_for(std::chrono::milliseconds(50));
continue;
}
if(ui_hide_timer.getElapsedTime().asMilliseconds() > UI_HIDE_TIMEOUT) {
ui_visible = false;
video_player_ui_window->setVisible(false);
//window.setMouseCursorVisible(false);
}
const float ui_height = window_size.y * 0.025f;
if(ui_resize) {
ui_resize = false;
video_player_ui_window->setSize(sf::Vector2u(window_size.x, ui_height));
video_player_ui_window->setPosition(sf::Vector2i(0, window_size.y - ui_height));
}
// TODO: Make window transparent, so the ui overlay for the video has transparency
video_player_ui_window->clear(sf::Color(33, 33, 33));
rect.setSize(sf::Vector2f(window_size.x * progress, ui_height));
video_player_ui_window->draw(rect);
video_player_ui_window->display();
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
auto mouse_pos = sf::Mouse::getPosition(window);
if(mouse_pos.y >= window_size.y - ui_height && mouse_pos.y <= window_size.y) {
if(seekable)
video_player->set_progress((double)mouse_pos.x / (double)window_size.x);
else
fprintf(stderr, "Video is not seekable!\n"); // TODO: Show this to the user
}
}
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
}
video_player_ui_window.reset();
window_set_fullscreen(disp, window.getSystemHandle(), WindowFullscreenState::UNSET);
//window.setMouseCursorVisible(true);
}
enum class TrackMediaType {
RSS,
HTML
};
const char* track_media_type_string(TrackMediaType media_type) {
switch(media_type) {
case TrackMediaType::RSS:
return "rss";
case TrackMediaType::HTML:
return "html";
}
assert(false);
return "";
}
static int track_media(TrackMediaType media_type, const std::string &manga_title, const std::string &chapter_title, const std::string &url) {
const char *args[] = { "automedia.py", "add", track_media_type_string(media_type), url.data(), "--start-after", chapter_title.data(), "--name", manga_title.data(), nullptr };
return exec_program(args, nullptr, nullptr);
}
void Program::select_episode(BodyItem *item, bool start_from_beginning) {
images_url = item->url;
chapter_title = item->title;
image_index = 0;
current_page = Page::IMAGES;
if(start_from_beginning)
return;
const Json::Value &json_chapters = content_storage_json["chapters"];
if(json_chapters.isObject()) {
const Json::Value &json_chapter = json_chapters[chapter_title];
if(json_chapter.isObject()) {
const Json::Value ¤t = json_chapter["current"];
if(current.isNumeric())
image_index = current.asInt() - 1;
}
}
}
Page Program::pop_page_stack() {
if(!page_stack.empty()) {
Page previous_page = page_stack.top();
page_stack.pop();
return previous_page;
}
return Page::EXIT;
}
void Program::episode_list_page() {
search_bar->onTextUpdateCallback = [this](const std::string &text) {
body->filter_search_fuzzy(text);
body->select_first_item();
};
search_bar->onTextSubmitCallback = [this](const std::string &text) -> bool {
BodyItem *selected_item = body->get_selected();
if(!selected_item)
return false;
select_episode(selected_item, false);
return true;
};
const Json::Value &json_chapters = content_storage_json["chapters"];
sf::Vector2f body_pos;
sf::Vector2f body_size;
bool redraw = true;
sf::Event event;
while (current_page == Page::EPISODE_LIST) {
while (window.pollEvent(event)) {
base_event_handler(event, Page::SEARCH_SUGGESTION);
if(event.type == sf::Event::Resized || event.type == sf::Event::GainedFocus)
redraw = true;
else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::T && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl)) {
BodyItem *selected_item = body->get_selected();
if(selected_item) {
if(track_media(TrackMediaType::HTML, content_title, selected_item->title, content_url) == 0) {
show_notification("Media tracker", "You are now tracking \"" + content_title + "\" after \"" + selected_item->title + "\"");
} else {
show_notification("Media tracker", "Failed to track media \"" + content_title + "\", chapter: \"" + selected_item->title + "\"", Urgency::CRITICAL);
}
}
}
}
// TODO: This code is duplicated in many places. Handle it in one place.
if(redraw) {
redraw = false;
search_bar->onWindowResize(window_size);
float body_padding_horizontal = 50.0f;
float body_padding_vertical = 50.0f;
float body_width = window_size.x - body_padding_horizontal * 2.0f;
if(body_width < 400) {
body_width = window_size.x;
body_padding_horizontal = 0.0f;
}
float search_bottom = search_bar->getBottom();
body_pos = sf::Vector2f(body_padding_horizontal, search_bottom + body_padding_vertical);
body_size = sf::Vector2f(body_width, window_size.y - search_bottom - body_padding_vertical);
}
search_bar->update();
window.clear(back_color);
body->draw(window, body_pos, body_size, json_chapters);
search_bar->draw(window);
window.display();
}
}
// TODO: Optimize this somehow. One image alone uses more than 20mb ram! Total ram usage for viewing one image
// becomes 40mb (private memory, almost 100mb in total!) Unacceptable!
Program::LoadImageResult Program::load_image_by_index(int image_index, sf::Texture &image_texture, sf::String &error_message) {
Path image_path = content_cache_dir;
image_path.join(std::to_string(image_index + 1));
Path image_finished_path(image_path.data + ".finished");
if(get_file_type(image_finished_path) != FileType::FILE_NOT_FOUND && get_file_type(image_path) == FileType::REGULAR) {
std::string image_data;
if(file_get_content(image_path, image_data) == 0) {
if(image_texture.loadFromMemory(image_data.data(), image_data.size())) {
image_texture.setSmooth(true);
//image_texture.generateMipmap();
return LoadImageResult::OK;
} else {
error_message = std::string("Failed to load image for page ") + std::to_string(image_index + 1);
return LoadImageResult::FAILED;
}
} else {
show_notification("Manga", "Failed to load image for page " + std::to_string(image_index + 1) + ". Image filepath: " + image_path.data, Urgency::CRITICAL);
error_message = std::string("Failed to load image for page ") + std::to_string(image_index + 1);
return LoadImageResult::FAILED;
}
} else {
error_message = "Downloading page " + std::to_string(image_index + 1) + "...";
return LoadImageResult::DOWNLOAD_IN_PROGRESS;
}
}
void Program::download_chapter_images_if_needed(Manga *image_plugin) {
if(downloading_chapter_url == images_url)
return;
downloading_chapter_url = images_url;
if(image_download_future.valid()) {
image_download_cancel = true;
image_download_future.get();
image_download_cancel = false;
}
std::string chapter_url = images_url;
Path content_cache_dir_ = content_cache_dir;
image_download_future = std::async(std::launch::async, [chapter_url, image_plugin, content_cache_dir_, this]() {
// TODO: Download images in parallel
int page = 1;
image_plugin->for_each_page_in_chapter(chapter_url, [content_cache_dir_, &page, this](const std::string &url) {
if(image_download_cancel)
return false;
#if 0
size_t last_index = url.find_last_of('/');
if(last_index == std::string::npos || (int)url.size() - (int)last_index + 1 <= 0) {
show_notification("Manganelo", "Image url is in incorrect format, missing '/': " + url, Urgency::CRITICAL);
return false;
}
std::string image_filename = url.substr(last_index + 1);
Path image_filepath = content_cache_dir_;
image_filepath.join(image_filename);
#endif
// TODO: Save image with the file extension that url says it has? right now the file is saved without any extension
Path image_filepath = content_cache_dir_;
image_filepath.join(std::to_string(page++));
Path lockfile_path(image_filepath.data + ".finished");
if(get_file_type(lockfile_path) != FileType::FILE_NOT_FOUND)
return true;
std::string image_content;
if(download_to_string(url, image_content, {}, current_plugin->use_tor) != DownloadResult::OK || image_content.size() <= 255) {
if(current_plugin->name == "manganelo") {
bool try_backup_url = false;
std::string new_url = url;
if(string_replace_all(new_url, "s3.mkklcdnv3.com", "bu.mkklcdnbuv1.com") > 0) {
try_backup_url = true;
} else {
try_backup_url = (string_replace_all(new_url, "s41.mkklcdnv41.com", "bu.mkklcdnbuv1.com") > 0);
}
if(try_backup_url) {
image_content.clear();
if(download_to_string(new_url, image_content, {}, current_plugin->use_tor) != DownloadResult::OK || image_content.size() <= 255) {
show_notification("Manganelo", "Failed to download image: " + new_url, Urgency::CRITICAL);
return false;
}
} else {
show_notification("Manganelo", "Failed to download image: " + url, Urgency::CRITICAL);
return false;
}
} else {
show_notification("Manga", "Failed to download image: " + url, Urgency::CRITICAL);
return false;
}
}
if(file_overwrite(image_filepath, image_content) != 0) {
show_notification("Storage", "Failed to save image to file: " + image_filepath.data, Urgency::CRITICAL);
return false;
}
if(create_lock_file(lockfile_path) != 0) {
show_notification("Storage", "Failed to save image finished state to file: " + lockfile_path.data, Urgency::CRITICAL);
return false;
}
return true;
});
});
}
void Program::image_page() {
search_bar->onTextUpdateCallback = nullptr;
search_bar->onTextSubmitCallback = nullptr;
sf::Texture image_texture;
sf::Sprite image;
sf::Text error_message("", font, 30);
error_message.setFillColor(sf::Color::White);
assert(current_plugin->is_manga());
Manga *image_plugin = static_cast<Manga*>(current_plugin);
std::string image_data;
bool download_in_progress = false;
content_cache_dir = get_cache_dir().join(image_plugin->name).join(manga_id_base64).join(base64_encode(chapter_title));
if(create_directory_recursive(content_cache_dir) != 0) {
show_notification("Storage", "Failed to create directory: " + content_cache_dir.data, Urgency::CRITICAL);
current_page = Page::EPISODE_LIST;
return;
}
download_chapter_images_if_needed(image_plugin);
int num_images = 0;
if(image_plugin->get_number_of_images(images_url, num_images) != ImageResult::OK) {
show_notification("Plugin", "Failed to get number of images", Urgency::CRITICAL);
current_page = Page::EPISODE_LIST;
return;
}
image_index = std::min(image_index, num_images);
if(image_index < num_images) {
sf::String error_msg;
LoadImageResult load_image_result = load_image_by_index(image_index, image_texture, error_msg);
if(load_image_result == LoadImageResult::OK)
image.setTexture(image_texture, true);
else if(load_image_result == LoadImageResult::DOWNLOAD_IN_PROGRESS)
download_in_progress = true;
error_message.setString(error_msg);
} else if(image_index == num_images) {
error_message.setString("End of " + chapter_title);
}
Json::Value &json_chapters = content_storage_json["chapters"];
Json::Value json_chapter;
int latest_read = image_index + 1;
if(json_chapters.isObject()) {
json_chapter = json_chapters[chapter_title];
if(json_chapter.isObject()) {
const Json::Value ¤t = json_chapter["current"];
if(current.isNumeric())
latest_read = std::max(latest_read, current.asInt());
} else {
json_chapter = Json::Value(Json::objectValue);
}
} else {
json_chapters = Json::Value(Json::objectValue);
json_chapter = Json::Value(Json::objectValue);
}
json_chapter["current"] = std::min(latest_read, num_images);
json_chapter["total"] = num_images;
json_chapters[chapter_title] = json_chapter;
if(!save_manga_progress_json(content_storage_file, content_storage_json)) {
show_notification("Manga progress", "Failed to save manga progress", Urgency::CRITICAL);
}
bool error = !error_message.getString().isEmpty();
bool redraw = true;
sf::Event event;
sf::Text chapter_text(chapter_title + " | Page " + std::to_string(image_index + 1) + "/" + std::to_string(num_images), font, 14);
if(image_index == num_images)
chapter_text.setString(chapter_title + " | End");
chapter_text.setFillColor(sf::Color::White);
sf::RectangleShape chapter_text_background;
chapter_text_background.setFillColor(sf::Color(0, 0, 0, 150));
sf::Vector2u texture_size;
sf::Vector2f texture_size_f;
if(!error) {
texture_size = image.getTexture()->getSize();
texture_size_f = sf::Vector2f(texture_size.x, texture_size.y);
}
sf::Clock check_downloaded_timer;
const sf::Int32 check_downloaded_timeout_ms = 500;
// TODO: Show to user if a certain page is missing (by checking page name (number) and checking if some are skipped)
while (current_page == Page::IMAGES) {
while(window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
current_page = Page::EXIT;
} else if(event.type == sf::Event::Resized) {
window_size.x = event.size.width;
window_size.y = event.size.height;
sf::FloatRect visible_area(0, 0, window_size.x, window_size.y);
window.setView(sf::View(visible_area));
redraw = true;
} else if(event.type == sf::Event::GainedFocus) {
redraw = true;
} else if(event.type == sf::Event::KeyPressed) {
if(event.key.code == sf::Keyboard::Up) {
if(image_index > 0) {
--image_index;
return;
} else if(image_index == 0 && body->selected_item < (int)body->items.size() - 1) {
// TODO: Make this work if the list is sorted differently than from newest to oldest.
body->selected_item++;
select_episode(body->items[body->selected_item].get(), true);
image_index = 99999; // Start at the page that shows we are at the end of the chapter
return;
}
} else if(event.key.code == sf::Keyboard::Down) {
if(image_index < num_images) {
++image_index;
return;
} else if(image_index == num_images && body->selected_item > 0) {
// TODO: Make this work if the list is sorted differently than from newest to oldest.
body->selected_item--;
select_episode(body->items[body->selected_item].get(), true);
return;
}
} else if(event.key.code == sf::Keyboard::Escape) {
current_page = Page::EPISODE_LIST;
}
}
}
if(download_in_progress && check_downloaded_timer.getElapsedTime().asMilliseconds() >= check_downloaded_timeout_ms) {
sf::String error_msg;
LoadImageResult load_image_result = load_image_by_index(image_index, image_texture, error_msg);
if(load_image_result == LoadImageResult::OK) {
image.setTexture(image_texture, true);
download_in_progress = false;
error = false;
texture_size = image.getTexture()->getSize();
texture_size_f = sf::Vector2f(texture_size.x, texture_size.y);
} else if(load_image_result == LoadImageResult::FAILED) {
download_in_progress = false;
}
error_message.setString(error_msg);
redraw = true;
check_downloaded_timer.restart();
}
const float font_height = chapter_text.getCharacterSize() + 8.0f;
const float background_height = font_height + 6.0f;
sf::Vector2f content_size;
content_size.x = window_size.x;
content_size.y = window_size.y - background_height;
if(redraw) {
redraw = false;
if(error) {
auto bounds = error_message.getLocalBounds();
error_message.setPosition(std::floor(content_size.x * 0.5f - bounds.width * 0.5f), std::floor(content_size.y * 0.5f - bounds.height));
} else {
auto image_scale = get_ratio(texture_size_f, clamp_to_size(texture_size_f, content_size));
image.setScale(image_scale);
auto image_size = texture_size_f;
image_size.x *= image_scale.x;
image_size.y *= image_scale.y;
image.setPosition(std::floor(content_size.x * 0.5f - image_size.x * 0.5f), std::floor(content_size.y * 0.5f - image_size.y * 0.5f));
}
window.clear(back_color);
if(error) {
window.draw(error_message);
} else {
window.draw(image);
}
chapter_text_background.setSize(sf::Vector2f(window_size.x, background_height));
chapter_text_background.setPosition(0.0f, std::floor(window_size.y - background_height));
window.draw(chapter_text_background);
auto text_bounds = chapter_text.getLocalBounds();
chapter_text.setPosition(std::floor(window_size.x * 0.5f - text_bounds.width * 0.5f), std::floor(window_size.y - background_height * 0.5f - font_height * 0.5f));
window.draw(chapter_text);
}
window.display();
}
}
void Program::content_list_page() {
if(current_plugin->get_content_list(content_list_url, body->items) != PluginResult::OK) {
show_notification("Content list", "Failed to get content list for url: " + content_list_url, Urgency::CRITICAL);
current_page = Page::SEARCH_SUGGESTION;
return;
}
search_bar->onTextUpdateCallback = [this](const std::string &text) {
body->filter_search_fuzzy(text);
body->select_first_item();
};
search_bar->onTextSubmitCallback = [this](const std::string &text) -> bool {
BodyItem *selected_item = body->get_selected();
if(!selected_item)
return false;
content_episode = selected_item->title;
content_url = selected_item->url;
current_page = Page::CONTENT_DETAILS;
body->clear_items();
return true;
};
sf::Vector2f body_pos;
sf::Vector2f body_size;
bool redraw = true;
sf::Event event;
while (current_page == Page::CONTENT_LIST) {
while (window.pollEvent(event)) {
base_event_handler(event, Page::SEARCH_SUGGESTION);
if(event.type == sf::Event::Resized || event.type == sf::Event::GainedFocus)
redraw = true;
}
// TODO: This code is duplicated in many places. Handle it in one place.
if(redraw) {
redraw = false;
search_bar->onWindowResize(window_size);
float body_padding_horizontal = 50.0f;
float body_padding_vertical = 50.0f;
float body_width = window_size.x - body_padding_horizontal * 2.0f;
if(body_width < 400) {
body_width = window_size.x;
body_padding_horizontal = 0.0f;
}
float search_bottom = search_bar->getBottom();
body_pos = sf::Vector2f(body_padding_horizontal, search_bottom + body_padding_vertical);
body_size = sf::Vector2f(body_width, window_size.y - search_bottom - body_padding_vertical);
}
search_bar->update();
window.clear(back_color);
body->draw(window, body_pos, body_size);
search_bar->draw(window);
window.display();
}
}
void Program::content_details_page() {
if(current_plugin->get_content_details(content_list_url, content_url, body->items) != PluginResult::OK) {
show_notification("Content details", "Failed to get content details for url: " + content_url, Urgency::CRITICAL);
// TODO: This will return to an empty content list.
// Each page should have its own @Body so we can return to the last page and still have the data loaded
// however the cached images should be cleared.
current_page = Page::CONTENT_LIST;
return;
}
// Instead of using search bar to searching, use it for commenting.
// TODO: Have an option for the search bar to be multi-line.
search_bar->onTextUpdateCallback = nullptr;
search_bar->onTextSubmitCallback = [this](const std::string &text) -> bool {
if(text.empty())
return false;
return true;
};
sf::Vector2f body_pos;
sf::Vector2f body_size;
bool redraw = true;
sf::Event event;
while (current_page == Page::CONTENT_DETAILS) {
while (window.pollEvent(event)) {
base_event_handler(event, Page::CONTENT_LIST);
if(event.type == sf::Event::Resized || event.type == sf::Event::GainedFocus)
redraw = true;
}
// TODO: This code is duplicated in many places. Handle it in one place.
if(redraw) {
redraw = false;
search_bar->onWindowResize(window_size);
float body_padding_horizontal = 50.0f;
float body_padding_vertical = 50.0f;
float body_width = window_size.x - body_padding_horizontal * 2.0f;
if(body_width < 400) {
body_width = window_size.x;
body_padding_horizontal = 0.0f;
}
float search_bottom = search_bar->getBottom();
body_pos = sf::Vector2f(body_padding_horizontal, search_bottom + body_padding_vertical);
body_size = sf::Vector2f(body_width, window_size.y - search_bottom - body_padding_vertical);
}
search_bar->update();
window.clear(back_color);
body->draw(window, body_pos, body_size);
search_bar->draw(window);
window.display();
}
}
void Program::image_board_thread_list_page() {
assert(current_plugin->is_image_board());
ImageBoard *image_board = static_cast<ImageBoard*>(current_plugin);
if(image_board->get_threads(image_board_thread_list_url, body->items) != PluginResult::OK) {
show_notification("Content list", "Failed to get threads for url: " + image_board_thread_list_url, Urgency::CRITICAL);
current_page = Page::SEARCH_SUGGESTION;
return;
}
search_bar->onTextUpdateCallback = [this](const std::string &text) {
body->filter_search_fuzzy(text);
body->select_first_item();
};
search_bar->onTextSubmitCallback = [this](const std::string &text) -> bool {
BodyItem *selected_item = body->get_selected();
if(!selected_item)
return false;
content_episode = selected_item->title;
content_url = selected_item->url;
current_page = Page::IMAGE_BOARD_THREAD;
body->clear_items();
return true;
};
sf::Vector2f body_pos;
sf::Vector2f body_size;
bool redraw = true;
sf::Event event;
while (current_page == Page::IMAGE_BOARD_THREAD_LIST) {
while (window.pollEvent(event)) {
base_event_handler(event, Page::SEARCH_SUGGESTION);
if(event.type == sf::Event::Resized || event.type == sf::Event::GainedFocus)
redraw = true;
}
// TODO: This code is duplicated in many places. Handle it in one place.
if(redraw) {
redraw = false;
search_bar->onWindowResize(window_size);
float body_padding_horizontal = 50.0f;
float body_padding_vertical = 50.0f;
float body_width = window_size.x - body_padding_horizontal * 2.0f;
if(body_width < 400) {
body_width = window_size.x;
body_padding_horizontal = 0.0f;
}
float search_bottom = search_bar->getBottom();
body_pos = sf::Vector2f(body_padding_horizontal, search_bottom + body_padding_vertical);
body_size = sf::Vector2f(body_width, window_size.y - search_bottom - body_padding_vertical);
}
search_bar->update();
window.clear(back_color);
body->draw(window, body_pos, body_size);
search_bar->draw(window);
window.display();
}
}
static bool is_url_video(const std::string &url) {
return string_ends_with(url, ".webm") || string_ends_with(url, ".mp4") || string_ends_with(url, ".gif");
}
void Program::image_board_thread_page() {
assert(current_plugin->is_image_board());
// TODO: Support image board other than 4chan. To make this work, the captcha code needs to be changed
// to work with other captcha than google captcha
assert(current_plugin->name == "4chan");
ImageBoard *image_board = static_cast<ImageBoard*>(current_plugin);
if(image_board->get_thread_comments(image_board_thread_list_url, content_url, body->items) != PluginResult::OK) {
show_notification("Content details", "Failed to get content details for url: " + content_url, Urgency::CRITICAL);
// TODO: This will return to an empty content list.
// Each page should have its own @Body so we can return to the last page and still have the data loaded
// however the cached images should be cleared.
current_page = Page::IMAGE_BOARD_THREAD_LIST;
return;
}
const std::string &board = image_board_thread_list_url;
const std::string &thread = content_url;
// TODO: Instead of using stage here, use different pages for each stage
enum class NavigationStage {
VIEWING_COMMENTS,
REPLYING,
SOLVING_POST_CAPTCHA,
POSTING_SOLUTION,
POSTING_COMMENT,
VIEWING_ATTACHED_IMAGE
};
NavigationStage navigation_stage = NavigationStage::VIEWING_COMMENTS;
std::future<bool> captcha_request_future;
std::future<bool> captcha_post_solution_future;
std::future<bool> post_comment_future;
std::future<bool> load_image_future;
sf::Texture captcha_texture;
sf::Sprite captcha_sprite;
std::mutex captcha_image_mutex;
auto attached_image_texture = std::make_unique<sf::Texture>();
sf::Sprite attached_image_sprite;
std::mutex attachment_load_mutex;
GoogleCaptchaChallengeInfo challenge_info;
sf::Text challenge_description_text("", font, 24);
challenge_description_text.setFillColor(sf::Color::White);
const size_t captcha_num_columns = 3;
const size_t captcha_num_rows = 3;
std::array<bool, captcha_num_columns * captcha_num_rows> selected_captcha_images;
for(size_t i = 0; i < selected_captcha_images.size(); ++i) {
selected_captcha_images[i] = false;
}
sf::RectangleShape captcha_selection_rect;
captcha_selection_rect.setOutlineThickness(5.0f);
captcha_selection_rect.setOutlineColor(sf::Color(0, 85, 119));
// TODO: Draw only the outline instead of a transparent rectangle
captcha_selection_rect.setFillColor(sf::Color::Transparent);
// Valid for 2 minutes after solving a captcha
std::string captcha_post_id;
sf::Clock captcha_solved_time;
std::string comment_to_post;
// TODO: Show a white image with "Loading..." text while the captcha image is downloading
// TODO: Make this work with other sites than 4chan
auto request_google_captcha_image = [this, &captcha_texture, &captcha_image_mutex, &navigation_stage, &captcha_sprite, &challenge_description_text](GoogleCaptchaChallengeInfo &challenge_info) {
std::string payload_image_data;
DownloadResult download_image_result = download_to_string(challenge_info.payload_url, payload_image_data, {}, current_plugin->use_tor);
if(download_image_result == DownloadResult::OK) {
std::lock_guard<std::mutex> lock(captcha_image_mutex);
if(captcha_texture.loadFromMemory(payload_image_data.data(), payload_image_data.size())) {
captcha_texture.setSmooth(true);
captcha_sprite.setTexture(captcha_texture, true);
challenge_description_text.setString(challenge_info.description);
} else {
show_notification("Google captcha", "Failed to load downloaded captcha image", Urgency::CRITICAL);
navigation_stage = NavigationStage::VIEWING_COMMENTS;
}
} else {
show_notification("Google captcha", "Failed to download captcha image", Urgency::CRITICAL);
navigation_stage = NavigationStage::VIEWING_COMMENTS;
}
};
auto request_new_google_captcha_challenge = [this, &selected_captcha_images, &navigation_stage, &captcha_request_future, &request_google_captcha_image, &challenge_info]() {
fprintf(stderr, "Solving captcha!\n");
navigation_stage = NavigationStage::SOLVING_POST_CAPTCHA;
for(size_t i = 0; i < selected_captcha_images.size(); ++i) {
selected_captcha_images[i] = false;
}
const std::string referer = "https://boards.4chan.org/";
captcha_request_future = google_captcha_request_challenge(fourchan_google_captcha_api_key, referer,
[&navigation_stage, &request_google_captcha_image, &challenge_info](std::optional<GoogleCaptchaChallengeInfo> new_challenge_info) {
if(navigation_stage != NavigationStage::SOLVING_POST_CAPTCHA)
return;
if(new_challenge_info) {
challenge_info = new_challenge_info.value();
request_google_captcha_image(challenge_info);
} else {
show_notification("Google captcha", "Failed to get captcha challenge", Urgency::CRITICAL);
navigation_stage = NavigationStage::VIEWING_COMMENTS;
}
}, current_plugin->use_tor);
};
auto post_comment = [this, &navigation_stage, &image_board, &board, &thread, &captcha_post_id, &comment_to_post, &request_new_google_captcha_challenge]() {
navigation_stage = NavigationStage::POSTING_COMMENT;
PostResult post_result = image_board->post_comment(board, thread, captcha_post_id, comment_to_post);
if(post_result == PostResult::OK) {
show_notification(current_plugin->name, "Comment posted!");
navigation_stage = NavigationStage::VIEWING_COMMENTS;
// TODO: Append posted comment to the thread so the user can see their posted comment.
// TODO: Asynchronously update the thread periodically to show new comments.
} else if(post_result == PostResult::TRY_AGAIN) {
show_notification(current_plugin->name, "Error while posting, did the captcha expire? Please try again");
// TODO: Check if the response contains a new captcha instead of requesting a new one manually
request_new_google_captcha_challenge();
} else if(post_result == PostResult::BANNED) {
show_notification(current_plugin->name, "Failed to post comment because you are banned", Urgency::CRITICAL);
navigation_stage = NavigationStage::VIEWING_COMMENTS;
} else if(post_result == PostResult::ERR) {
show_notification(current_plugin->name, "Failed to post comment. Is " + current_plugin->name + " down or is your internet down?", Urgency::CRITICAL);
navigation_stage = NavigationStage::VIEWING_COMMENTS;
} else {
assert(false && "Unhandled post result");
show_notification(current_plugin->name, "Failed to post comment. Unknown error", Urgency::CRITICAL);
navigation_stage = NavigationStage::VIEWING_COMMENTS;
}
};
// Instead of using search bar to searching, use it for commenting.
// TODO: Have an option for the search bar to be multi-line.
search_bar->onTextUpdateCallback = nullptr;
search_bar->onTextSubmitCallback = [&post_comment_future, &navigation_stage, &request_new_google_captcha_challenge, &comment_to_post, &captcha_post_id, &captcha_solved_time, &post_comment](const std::string &text) -> bool {
if(text.empty())
return false;
assert(navigation_stage == NavigationStage::REPLYING);
comment_to_post = text;
if(!captcha_post_id.empty() && captcha_solved_time.getElapsedTime().asSeconds() < 120) {
post_comment_future = std::async(std::launch::async, [&post_comment]() -> bool {
post_comment();
return true;
});
} else {
request_new_google_captcha_challenge();
}
return true;
};
sf::Vector2f body_pos;
sf::Vector2f body_size;
bool redraw = true;
sf::Event event;
std::stack<int> comment_navigation_stack;
while (current_page == Page::IMAGE_BOARD_THREAD) {
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
current_page = Page::EXIT;
} else if(event.type == sf::Event::Resized) {
window_size.x = event.size.width;
window_size.y = event.size.height;
sf::FloatRect visible_area(0, 0, window_size.x, window_size.y);
window.setView(sf::View(visible_area));
}
if(event.type == sf::Event::Resized || event.type == sf::Event::GainedFocus)
redraw = true;
else if(event.type == sf::Event::KeyPressed && navigation_stage == NavigationStage::VIEWING_COMMENTS) {
if(event.key.code == sf::Keyboard::Up) {
body->select_previous_item();
} else if(event.key.code == sf::Keyboard::Down) {
body->select_next_item();
} else if(event.key.code == sf::Keyboard::Escape) {
current_page = Page::IMAGE_BOARD_THREAD_LIST;
body->clear_items();
body->reset_selected();
search_bar->clear();
} else if(event.key.code == sf::Keyboard::P) {
// TODO: Make this work when thumbnail is preview for a video/gif instead of a static image
BodyItem *selected_item = body->get_selected();
if(selected_item && !selected_item->attached_content_url.empty()) {
if(is_url_video(selected_item->attached_content_url)) {
page_stack.push(Page::IMAGE_BOARD_THREAD);
current_page = Page::VIDEO_CONTENT;
std::string prev_content_url = content_url;
content_url = selected_item->attached_content_url;
video_content_page();
content_url = std::move(prev_content_url);
} else {
navigation_stage = NavigationStage::VIEWING_ATTACHED_IMAGE;
load_image_future = std::async(std::launch::async, [this, &image_board, &attached_image_texture, &attached_image_sprite, &attachment_load_mutex]() -> bool {
BodyItem *selected_item = body->get_selected();
if(!selected_item || selected_item->attached_content_url.empty()) {
return false;
}
std::string image_data;
if(download_to_string(selected_item->attached_content_url, image_data, {}, image_board->use_tor) != DownloadResult::OK) {
show_notification(image_board->name, "Failed to download image: " + selected_item->attached_content_url, Urgency::CRITICAL);
return false;
}
std::lock_guard<std::mutex> lock(attachment_load_mutex);
if(!attached_image_texture->loadFromMemory(image_data.data(), image_data.size())) {
show_notification(image_board->name, "Failed to load image downloaded from url: " + selected_item->attached_content_url, Urgency::CRITICAL);
return false;
}
attached_image_texture->setSmooth(true);
attached_image_sprite.setTexture(*attached_image_texture, true);
return true;
});
}
}
}
BodyItem *selected_item = body->get_selected();
if(event.key.code == sf::Keyboard::Enter && selected_item && (comment_navigation_stack.empty() || body->selected_item != comment_navigation_stack.top()) && !selected_item->replies.empty()) {
for(auto &body_item : body->items) {
body_item->visible = false;
}
selected_item->visible = true;
for(size_t reply_index : selected_item->replies) {
body->items[reply_index]->visible = true;
}
comment_navigation_stack.push(body->selected_item);
} else if(event.key.code == sf::Keyboard::BackSpace && !comment_navigation_stack.empty()) {
size_t previous_selected = 0;
if(!comment_navigation_stack.empty()) {
previous_selected = comment_navigation_stack.top();
}
comment_navigation_stack.pop();
if(comment_navigation_stack.empty()) {
for(auto &body_item : body->items) {
body_item->visible = true;
}
body->selected_item = previous_selected;
} else {
for(auto &body_item : body->items) {
body_item->visible = false;
}
body->selected_item = previous_selected;
selected_item = body->items[comment_navigation_stack.top()].get();
selected_item->visible = true;
for(size_t reply_index : selected_item->replies) {
body->items[reply_index]->visible = true;
}
}
} else if(event.key.code == sf::Keyboard::C && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl) && selected_item) {
navigation_stage = NavigationStage::REPLYING;
} else if(event.key.code == sf::Keyboard::R && selected_item) {
std::string text_to_add = ">>" + selected_item->post_number;
if(search_bar->is_cursor_at_start_of_line())
text_to_add += '\n';
search_bar->append_text(text_to_add);
}
} else if(event.type == sf::Event::TextEntered && navigation_stage == NavigationStage::REPLYING) {
search_bar->onTextEntered(event.text.unicode);
}
if(event.type == sf::Event::KeyPressed && navigation_stage == NavigationStage::REPLYING) {
if(event.key.code == sf::Keyboard::Escape) {
//search_bar->clear();
navigation_stage = NavigationStage::VIEWING_COMMENTS;
}
}
if(event.type == sf::Event::KeyPressed && navigation_stage == NavigationStage::SOLVING_POST_CAPTCHA) {
int num = -1;
if(event.key.code >= sf::Keyboard::Num1 && event.key.code <= sf::Keyboard::Num9) {
num = event.key.code - sf::Keyboard::Num1;
} else if(event.key.code >= sf::Keyboard::Numpad1 && event.key.code <= sf::Keyboard::Numpad9) {
num = event.key.code - sf::Keyboard::Numpad1;
}
constexpr int select_map[9] = { 6, 7, 8, 3, 4, 5, 0, 1, 2 };
if(num != -1) {
int index = select_map[num];
selected_captcha_images[index] = !selected_captcha_images[index];
}
if(event.key.code == sf::Keyboard::Escape) {
navigation_stage = NavigationStage::VIEWING_COMMENTS;
} else if(event.key.code == sf::Keyboard::Enter) {
navigation_stage = NavigationStage::POSTING_SOLUTION;
captcha_post_solution_future = google_captcha_post_solution(fourchan_google_captcha_api_key, challenge_info.id, selected_captcha_images,
[&navigation_stage, &captcha_post_id, &captcha_solved_time, &selected_captcha_images, &challenge_info, &request_google_captcha_image, &post_comment](std::optional<std::string> new_captcha_post_id, std::optional<GoogleCaptchaChallengeInfo> new_challenge_info) {
if(navigation_stage != NavigationStage::POSTING_SOLUTION)
return;
if(new_captcha_post_id) {
captcha_post_id = new_captcha_post_id.value();
captcha_solved_time.restart();
post_comment();
} else if(new_challenge_info) {
show_notification("Google captcha", "Failed to solve captcha, please try again");
challenge_info = new_challenge_info.value();
navigation_stage = NavigationStage::SOLVING_POST_CAPTCHA;
for(size_t i = 0; i < selected_captcha_images.size(); ++i) {
selected_captcha_images[i] = false;
}
request_google_captcha_image(challenge_info);
}
}, current_plugin->use_tor);
}
}
if(event.type == sf::Event::KeyPressed && (navigation_stage == NavigationStage::POSTING_SOLUTION || navigation_stage == NavigationStage::POSTING_COMMENT)) {
if(event.key.code == sf::Keyboard::Escape) {
navigation_stage = NavigationStage::VIEWING_COMMENTS;
}
}
if(event.type == sf::Event::KeyPressed && navigation_stage == NavigationStage::VIEWING_ATTACHED_IMAGE) {
if(event.key.code == sf::Keyboard::Escape || event.key.code == sf::Keyboard::BackSpace) {
navigation_stage = NavigationStage::VIEWING_COMMENTS;
attached_image_texture.reset(new sf::Texture());
}
}
}
// TODO: This code is duplicated in many places. Handle it in one place.
if(redraw) {
redraw = false;
search_bar->onWindowResize(window_size);
float body_padding_horizontal = 50.0f;
float body_padding_vertical = 50.0f;
float body_width = window_size.x - body_padding_horizontal * 2.0f;
if(body_width < 400) {
body_width = window_size.x;
body_padding_horizontal = 0.0f;
}
float search_bottom = search_bar->getBottom();
body_pos = sf::Vector2f(body_padding_horizontal, search_bottom + body_padding_vertical);
body_size = sf::Vector2f(body_width, window_size.y - search_bottom - body_padding_vertical);
}
//search_bar->update();
window.clear(back_color);
if(navigation_stage == NavigationStage::SOLVING_POST_CAPTCHA) {
std::lock_guard<std::mutex> lock(captcha_image_mutex);
if(captcha_texture.getNativeHandle() != 0) {
const float challenge_description_height = challenge_description_text.getCharacterSize() + 10.0f;
sf::Vector2f content_size = window_size;
content_size.y -= challenge_description_height;
sf::Vector2u captcha_texture_size = captcha_texture.getSize();
sf::Vector2f captcha_texture_size_f(captcha_texture_size.x, captcha_texture_size.y);
auto image_scale = get_ratio(captcha_texture_size_f, clamp_to_size(captcha_texture_size_f, content_size));
captcha_sprite.setScale(image_scale);
auto image_size = captcha_texture_size_f;
image_size.x *= image_scale.x;
image_size.y *= image_scale.y;
captcha_sprite.setPosition(std::floor(content_size.x * 0.5f - image_size.x * 0.5f), std::floor(challenge_description_height + content_size.y * 0.5f - image_size.y * 0.5f));
window.draw(captcha_sprite);
challenge_description_text.setPosition(captcha_sprite.getPosition() + sf::Vector2f(image_size.x * 0.5f, 0.0f) - sf::Vector2f(challenge_description_text.getLocalBounds().width * 0.5f, challenge_description_height));
window.draw(challenge_description_text);
for(size_t column = 0; column < captcha_num_columns; ++column) {
for(size_t row = 0; row < captcha_num_rows; ++row) {
if(selected_captcha_images[column + captcha_num_columns * row]) {
captcha_selection_rect.setPosition(captcha_sprite.getPosition() + sf::Vector2f(image_size.x / captcha_num_columns * column, image_size.y / captcha_num_rows * row));
captcha_selection_rect.setSize(sf::Vector2f(image_size.x / captcha_num_columns, image_size.y / captcha_num_rows));
window.draw(captcha_selection_rect);
}
}
}
}
} else if(navigation_stage == NavigationStage::POSTING_SOLUTION) {
// TODO: Show "Posting..." when posting solution
} else if(navigation_stage == NavigationStage::POSTING_COMMENT) {
// TODO: Show "Posting..." when posting comment
} else if(navigation_stage == NavigationStage::VIEWING_ATTACHED_IMAGE) {
// TODO: Show a white image with the text "Downloading..." while the image is downloading and loading
std::lock_guard<std::mutex> lock(attachment_load_mutex);
if(attached_image_texture->getNativeHandle() != 0) {
auto content_size = window_size;
sf::Vector2u texture_size = attached_image_texture->getSize();
sf::Vector2f texture_size_f(texture_size.x, texture_size.y);
auto image_scale = get_ratio(texture_size_f, clamp_to_size(texture_size_f, content_size));
attached_image_sprite.setScale(image_scale);
auto image_size = texture_size_f;
image_size.x *= image_scale.x;
image_size.y *= image_scale.y;
attached_image_sprite.setPosition(std::floor(content_size.x * 0.5f - image_size.x * 0.5f), std::floor(content_size.y * 0.5f - image_size.y * 0.5f));
window.draw(attached_image_sprite);
} else {
sf::RectangleShape rect(sf::Vector2f(640.0f, 480.0f));
rect.setFillColor(sf::Color::White);
auto content_size = window_size;
auto rect_size = clamp_to_size(rect.getSize(), content_size);
rect.setSize(rect_size);
rect.setPosition(std::floor(content_size.x * 0.5f - rect_size.x * 0.5f), std::floor(content_size.y * 0.5f - rect_size.y * 0.5f));
window.draw(rect);
}
} else if(navigation_stage == NavigationStage::REPLYING) {
body->draw(window, body_pos, body_size);
search_bar->draw(window);
} else if(navigation_stage == NavigationStage::VIEWING_COMMENTS) {
body->draw(window, body_pos, body_size);
search_bar->draw(window);
}
window.display();
}
// TODO: Instead of waiting for them, kill them somehow
if(captcha_request_future.valid())
captcha_request_future.get();
if(captcha_post_solution_future.valid())
captcha_post_solution_future.get();
if(post_comment_future.valid())
post_comment_future.get();
if(load_image_future.valid())
load_image_future.get();
// Clear post that is still being written.
// TODO: A multiline text edit widget should be cleared instead of the search bar.
// TODO: This post should be saved for the thread. Each thread should have its own text edit widget,
// so you dont have to retype a post that was in the middle of being posted when returning.
search_bar->clear();
}
}
|