forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_pl_thumbnail_download.c
968 lines (809 loc) · 29.2 KB
/
task_pl_thumbnail_download.c
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
/* RetroArch - A frontend for libretro.
* Copyright (C) 2011-2017 - Daniel De Matteis
* Copyright (C) 2014-2017 - Jean-André Santoni
* Copyright (C) 2016-2019 - Brad Parker
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <string/stdstring.h>
#include <file/file_path.h>
#include <net/net_http.h>
#include <streams/file_stream.h>
#include "tasks_internal.h"
#include "task_file_transfer.h"
#include "../configuration.h"
#include "../file_path_special.h"
#include "../playlist.h"
#include "../verbosity.h"
#ifdef RARCH_INTERNAL
#include "../gfx/gfx_thumbnail_path.h"
#ifdef HAVE_MENU
#include "../menu/menu_cbs.h"
#include "../menu/menu_driver.h"
#endif
#endif
enum pl_thumb_status
{
PL_THUMB_BEGIN = 0,
PL_THUMB_ITERATE_ENTRY,
PL_THUMB_ITERATE_TYPE,
PL_THUMB_END
};
enum pl_thumb_flags
{
PL_THUMB_FLAG_OVERWRITE = (1 << 0),
PL_THUMB_FLAG_RIGHT_THUMB_EXISTS = (1 << 1),
PL_THUMB_FLAG_LEFT_THUMB_EXISTS = (1 << 2),
PL_THUMB_FLAG_HTTP_TASK_COMPLETE = (1 << 3)
};
typedef struct pl_thumb_handle
{
char *system;
char *playlist_path;
char *dir_thumbnails;
playlist_t *playlist;
gfx_thumbnail_path_data_t *thumbnail_path_data;
retro_task_t *http_task;
playlist_config_t playlist_config; /* size_t alignment */
size_t list_size;
size_t list_index;
unsigned type_idx;
enum pl_thumb_status status;
enum playlist_thumbnail_name_flags name_flags;
uint8_t flags;
} pl_thumb_handle_t;
typedef struct pl_entry_id
{
char *playlist_path;
size_t idx;
} pl_entry_id_t;
/*********************/
/* Utility Functions */
/*********************/
/* Fetches the thumbnail subdirectory (Named_Snaps,
* Named_Titles, Named_Boxarts) corresponding to the
* specified 'type index' (1, 2, 3).
* Returns true if 'type index' is valid */
static bool gfx_thumbnail_get_sub_directory(
unsigned type_idx, const char **sub_directory)
{
if (!sub_directory)
return false;
switch (type_idx)
{
case 1:
*sub_directory = "Named_Snaps";
return true;
case 2:
*sub_directory = "Named_Titles";
return true;
case 3:
*sub_directory = "Named_Boxarts";
return true;
case 0:
default:
break;
}
return false;
}
/* Fetches current database name.
* Returns true if database name is valid. */
static void gfx_thumbnail_get_db_name(
gfx_thumbnail_path_data_t *path_data, const char **db_name)
{
if (!string_is_empty(path_data->content_db_name))
*db_name = path_data->content_db_name;
}
/* Fetches local and remote paths for current thumbnail
* of current type */
static bool get_thumbnail_paths(
pl_thumb_handle_t *pl_thumb,
char *path, size_t path_size,
char *url, size_t url_size)
{
char content_dir[PATH_MAX_LENGTH];
char tmp_buf[PATH_MAX_LENGTH];
size_t raw_url_len = sizeof(char) * 8192;
char *raw_url = NULL;
const char *system = NULL;
const char *db_name = NULL;
const char *img_name = NULL;
const char *sub_dir = NULL;
const char *system_name = NULL;
content_dir[0] = '\0';
if (!pl_thumb->thumbnail_path_data)
return false;
if (string_is_empty(pl_thumb->dir_thumbnails))
return false;
/* Extract required strings */
gfx_thumbnail_get_system( pl_thumb->thumbnail_path_data, &system);
gfx_thumbnail_get_db_name(pl_thumb->thumbnail_path_data, &db_name);
if (!gfx_thumbnail_get_img_name(pl_thumb->thumbnail_path_data, &img_name, pl_thumb->name_flags))
return false;
if (!gfx_thumbnail_get_sub_directory(pl_thumb->type_idx, &sub_dir))
return false;
/* Determine system name */
if (string_is_empty(db_name))
{
if (string_is_empty(system))
return false;
/* If this is a content history or favorites playlist
* then the current 'path_data->system' string is
* meaningless. In this case, we fall back to the
* content directory name */
if ( string_is_equal(system, "history")
|| string_is_equal(system, "favorites"))
{
if (!gfx_thumbnail_get_content_dir(
pl_thumb->thumbnail_path_data, content_dir, sizeof(content_dir)))
return false;
system_name = content_dir;
}
else
system_name = system;
}
else
system_name = db_name;
/* Generate local path */
fill_pathname_join_special(path, pl_thumb->dir_thumbnails,
system_name, path_size);
fill_pathname_join_special(tmp_buf, path, sub_dir, sizeof(tmp_buf));
fill_pathname_join_special(path, tmp_buf, img_name, path_size);
if (string_is_empty(path))
return false;
if (!(raw_url = (char*)malloc(8192 * sizeof(char))))
return false;
raw_url[0] = '\0';
/* Generate remote path */
snprintf(raw_url, raw_url_len, "%s/%s/%s/%s",
FILE_PATH_CORE_THUMBNAILS_URL,
system_name,
sub_dir,
img_name
);
if (string_is_empty(raw_url))
{
free(raw_url);
return false;
}
net_http_urlencode_full(url, raw_url, url_size);
free(raw_url);
return !string_is_empty(url);
}
/* Thumbnail download http task callback function
* > Writes thumbnail file to disk */
void cb_http_task_download_pl_thumbnail(
retro_task_t *task, void *task_data,
void *user_data, const char *err)
{
char output_dir[PATH_MAX_LENGTH];
http_transfer_data_t *data = (http_transfer_data_t*)task_data;
file_transfer_t *transf = (file_transfer_t*)user_data;
pl_thumb_handle_t *pl_thumb = NULL;
/* Update pl_thumb task status
* > Do this first, to minimise the risk of hanging
* the parent task in the event of an http error */
if (!transf)
goto finish;
if (!(pl_thumb = (pl_thumb_handle_t*)transf->user_data))
goto finish;
pl_thumb->flags |= PL_THUMB_FLAG_HTTP_TASK_COMPLETE;
/* Remaining sanity checks... */
if (!data || !data->data || string_is_empty(transf->path))
goto finish;
/* Skip if data can't be good */
if (data->status != 200)
{
err = "File not found.";
goto finish;
}
/* Create output directory, if required */
strlcpy(output_dir, transf->path, sizeof(output_dir));
path_basedir_wrapper(output_dir);
if (!path_mkdir(output_dir))
{
err = msg_hash_to_str(MSG_FAILED_TO_CREATE_THE_DIRECTORY);
goto finish;
}
/* Write thumbnail file to disk */
if (!filestream_write_file(transf->path, data->data, data->len))
{
err = "Write failed.";
goto finish;
}
finish:
if (!string_is_empty(err))
RARCH_ERR("[Thumbnail]: Download \"%s\" failed: %s\n",
(transf ? transf->path : "unknown"), err);
else
RARCH_LOG("[Thumbnail]: Download \"%s\".\n",
(transf ? transf->path : "unknown"));
if (transf)
free(transf);
}
/* Download thumbnail of the current type for the current
* playlist entry */
static void download_pl_thumbnail(pl_thumb_handle_t *pl_thumb)
{
char path[PATH_MAX_LENGTH];
char url[2048];
path[0] = '\0';
url[0] = '\0';
/* Check if paths are valid */
if (get_thumbnail_paths(pl_thumb, path, sizeof(path), url, sizeof(url)))
{
/* Only download missing thumbnails */
if (!path_is_valid(path) || (pl_thumb->flags & PL_THUMB_FLAG_OVERWRITE))
{
file_transfer_t *transf = (file_transfer_t*)malloc(sizeof(file_transfer_t));
if (!transf)
return; /* If this happens then everything is broken anyway... */
/* Initialise http task status */
pl_thumb->flags &= ~PL_THUMB_FLAG_HTTP_TASK_COMPLETE;
transf->enum_idx = MSG_UNKNOWN;
transf->path[0] = '\0';
/* Initialise file transfer */
transf->user_data = (void*)pl_thumb;
strlcpy(transf->path, path, sizeof(transf->path));
/* Note: We don't actually care if this fails since that
* just means the file is missing from the server, so it's
* not something we can handle here... */
/* ...if it does fail, however, we can immediately
* signal that the task is 'complete' */
if (!(pl_thumb->http_task = (retro_task_t*)task_push_http_transfer_file(
url, true, NULL, cb_http_task_download_pl_thumbnail, transf)))
pl_thumb->flags |= PL_THUMB_FLAG_HTTP_TASK_COMPLETE;
}
}
}
static void free_pl_thumb_handle(pl_thumb_handle_t *pl_thumb)
{
if (pl_thumb->system)
{
free(pl_thumb->system);
pl_thumb->system = NULL;
}
if (pl_thumb->playlist_path)
{
free(pl_thumb->playlist_path);
pl_thumb->playlist_path = NULL;
}
if (pl_thumb->dir_thumbnails)
{
free(pl_thumb->dir_thumbnails);
pl_thumb->dir_thumbnails = NULL;
}
if (pl_thumb->playlist)
{
playlist_free(pl_thumb->playlist);
pl_thumb->playlist = NULL;
}
if (pl_thumb->thumbnail_path_data)
{
free(pl_thumb->thumbnail_path_data);
pl_thumb->thumbnail_path_data = NULL;
}
free(pl_thumb);
pl_thumb = NULL;
}
/*******************************/
/* Playlist Thumbnail Download */
/*******************************/
static void task_pl_thumbnail_download_handler(retro_task_t *task)
{
pl_thumb_handle_t *pl_thumb = NULL;
enum playlist_thumbnail_name_flags next_flag = PLAYLIST_THUMBNAIL_FLAG_INVALID;
if (!task)
goto task_finished;
if (!(pl_thumb = (pl_thumb_handle_t*)task->state))
goto task_finished;
if (task_get_cancelled(task))
goto task_finished;
switch (pl_thumb->status)
{
case PL_THUMB_BEGIN:
/* Load playlist */
if (!path_is_valid(pl_thumb->playlist_config.path))
goto task_finished;
if (!(pl_thumb->playlist = playlist_init(&pl_thumb->playlist_config)))
goto task_finished;
pl_thumb->list_size = playlist_size(pl_thumb->playlist);
if (pl_thumb->list_size < 1)
goto task_finished;
/* Initialise thumbnail path data */
if (!(pl_thumb->thumbnail_path_data = gfx_thumbnail_path_init()))
goto task_finished;
if (!gfx_thumbnail_set_system(
pl_thumb->thumbnail_path_data,
pl_thumb->system, pl_thumb->playlist))
goto task_finished;
/* All good - can start iterating */
pl_thumb->status = PL_THUMB_ITERATE_ENTRY;
break;
case PL_THUMB_ITERATE_ENTRY:
/* Set current thumbnail content */
if (gfx_thumbnail_set_content_playlist(
pl_thumb->thumbnail_path_data, pl_thumb->playlist, pl_thumb->list_index))
{
const char *label = NULL;
/* Update progress display */
task_free_title(task);
if (gfx_thumbnail_get_label(pl_thumb->thumbnail_path_data, &label))
task_set_title(task, strdup(label));
else
task_set_title(task, strdup(""));
task_set_progress(task, (pl_thumb->list_index * 100) / pl_thumb->list_size);
/* Start iterating over thumbnail type */
pl_thumb->type_idx = 1;
pl_thumb->status = PL_THUMB_ITERATE_TYPE;
playlist_update_thumbnail_name_flag(pl_thumb->playlist, pl_thumb->list_index, PLAYLIST_THUMBNAIL_FLAG_FULL_NAME);
pl_thumb->name_flags = PLAYLIST_THUMBNAIL_FLAG_FULL_NAME;
}
else
{
/* Current playlist entry is broken - advance to
* the next one */
pl_thumb->list_index++;
if (pl_thumb->list_index >= pl_thumb->list_size)
pl_thumb->status = PL_THUMB_END;
}
break;
case PL_THUMB_ITERATE_TYPE:
/* Ensure that we only enqueue one transfer
* at a time... */
/* > If HTTP task is NULL, then it either finished
* or an error occurred - in either case,
* current task is 'complete' */
if (!pl_thumb->http_task)
pl_thumb->flags |= PL_THUMB_FLAG_HTTP_TASK_COMPLETE;
/* > Wait for task_push_http_transfer_file()
* callback to trigger */
else if (!(pl_thumb->flags & PL_THUMB_FLAG_HTTP_TASK_COMPLETE))
break;
pl_thumb->http_task = NULL;
/* Check whether all thumbnail types have been processed */
if (pl_thumb->type_idx > 3)
{
next_flag = playlist_get_next_thumbnail_name_flag(pl_thumb->playlist,pl_thumb->list_index);
if (next_flag == PLAYLIST_THUMBNAIL_FLAG_NONE) {
if (pl_thumb->playlist )
/* Time to move on to the next entry */
pl_thumb->list_index++;
if (pl_thumb->list_index < pl_thumb->list_size)
pl_thumb->status = PL_THUMB_ITERATE_ENTRY;
else
pl_thumb->status = PL_THUMB_END;
break;
} else {
/* Increment the name flag to cover the 3 supported naming conventions.
* Side-effect: all combinations will be tried (3x3 requests for 1 playlist entry)
* even if some files were already downloaded, but that may be useful if later on
* different view priorities are implemented. */
pl_thumb->type_idx = 1;
playlist_update_thumbnail_name_flag(pl_thumb->playlist, pl_thumb->list_index, next_flag);
pl_thumb->name_flags = next_flag;
}
}
/* Download current thumbnail */
if (pl_thumb)
download_pl_thumbnail(pl_thumb);
/* Increment thumbnail type */
pl_thumb->type_idx++;
break;
case PL_THUMB_END:
default:
task_set_progress(task, 100);
goto task_finished;
}
return;
task_finished:
if (task)
task_set_finished(task, true);
if (pl_thumb)
free_pl_thumb_handle(pl_thumb);
}
static bool task_pl_thumbnail_finder(retro_task_t *task, void *user_data)
{
if (task && user_data && task->handler == task_pl_thumbnail_download_handler)
{
pl_thumb_handle_t *pl_thumb = NULL;
if ((pl_thumb = (pl_thumb_handle_t*)task->state))
return string_is_equal((const char*)user_data, pl_thumb->playlist_config.path);
}
return false;
}
bool task_push_pl_thumbnail_download(
const char *system,
const playlist_config_t *playlist_config,
const char *dir_thumbnails)
{
task_finder_data_t find_data;
const char *playlist_file = NULL;
retro_task_t *task = task_init();
pl_thumb_handle_t *pl_thumb = (pl_thumb_handle_t*)calloc(1, sizeof(pl_thumb_handle_t));
/* Sanity check */
if (!playlist_config || !task || !pl_thumb)
goto error;
if ( string_is_empty(system)
|| string_is_empty(playlist_config->path)
|| string_is_empty(dir_thumbnails))
goto error;
playlist_file = path_basename_nocompression(
playlist_config->path);
if (string_is_empty(playlist_file))
goto error;
/* Only parse supported playlist types */
if (
string_ends_with_size(playlist_file, "_history.lpl",
strlen(playlist_file),
STRLEN_CONST("_history.lpl")
)
|| string_is_equal(playlist_file,
FILE_PATH_CONTENT_FAVORITES)
|| string_is_equal(system, "history")
|| string_is_equal(system, "favorites"))
goto error;
/* Concurrent download of thumbnails for the same
* playlist is not allowed */
find_data.func = task_pl_thumbnail_finder;
find_data.userdata = (void*)playlist_config->path;
if (task_queue_find(&find_data))
goto error;
/* Configure handle */
if (!playlist_config_copy(playlist_config, &pl_thumb->playlist_config))
goto error;
pl_thumb->system = strdup(system);
pl_thumb->playlist_path = NULL;
pl_thumb->dir_thumbnails = strdup(dir_thumbnails);
pl_thumb->playlist = NULL;
pl_thumb->thumbnail_path_data = NULL;
pl_thumb->http_task = NULL;
pl_thumb->list_size = 0;
pl_thumb->list_index = 0;
pl_thumb->type_idx = 1;
pl_thumb->status = PL_THUMB_BEGIN;
/* Configure task */
task->handler = task_pl_thumbnail_download_handler;
task->state = pl_thumb;
task->title = strdup(system);
task->alternative_look = true;
task->progress = 0;
task_queue_push(task);
return true;
error:
if (task)
{
free(task);
task = NULL;
}
if (pl_thumb)
{
free(pl_thumb);
pl_thumb = NULL;
}
return false;
}
/*************************************/
/* Playlist Entry Thumbnail Download */
/*************************************/
static void cb_task_pl_entry_thumbnail_refresh_menu(
retro_task_t *task, void *task_data,
void *user_data, const char *err)
{
#if defined(RARCH_INTERNAL) && defined(HAVE_MENU)
pl_thumb_handle_t *pl_thumb = NULL;
const char *thumbnail_path = NULL;
const char *left_thumbnail_path = NULL;
bool do_refresh = false;
playlist_t *current_playlist = playlist_get_cached();
menu_handle_t *menu = menu_state_get_ptr()->driver_data;
#ifdef HAVE_MATERIALUI
const char *menu_driver = menu_driver_ident();
#endif
if (!task)
return;
pl_thumb = (pl_thumb_handle_t*)task->state;
if (!pl_thumb || !pl_thumb->thumbnail_path_data)
return;
/* Only refresh if current playlist hasn't changed,
* and menu selection pointer is on the same entry
* (Note: this is crude, but it's sufficient to prevent
* 'refresh' from getting spammed when switching
* playlists or scrolling through one playlist at
* maximum speed with on demand downloads enabled)
* NOTE: GLUI requires special treatment, since
* it displays multiple thumbnails at a time... */
if (!current_playlist || !menu)
return;
if (string_is_empty(playlist_get_conf_path(current_playlist)))
return;
#ifdef HAVE_MATERIALUI
if (string_is_equal(menu_driver, "glui"))
{
if (!string_is_equal(pl_thumb->playlist_path,
playlist_get_conf_path(current_playlist)))
return;
}
else
#endif
{
if (!string_is_equal(pl_thumb->playlist_path,
playlist_get_conf_path(current_playlist)))
return;
}
/* Only refresh if left/right thumbnails did not exist
* when the task began, but do exist now
* (with the caveat that we must also refresh if existing
* files have been overwritten) */
if ( !(pl_thumb->flags & PL_THUMB_FLAG_RIGHT_THUMB_EXISTS)
||(pl_thumb->flags & PL_THUMB_FLAG_OVERWRITE))
if (gfx_thumbnail_update_path(pl_thumb->thumbnail_path_data, GFX_THUMBNAIL_RIGHT))
if (gfx_thumbnail_get_path(pl_thumb->thumbnail_path_data, GFX_THUMBNAIL_RIGHT, &thumbnail_path))
do_refresh = path_is_valid(thumbnail_path);
if (!do_refresh)
if ( !(pl_thumb->flags & PL_THUMB_FLAG_LEFT_THUMB_EXISTS)
|| (pl_thumb->flags & PL_THUMB_FLAG_OVERWRITE))
if (gfx_thumbnail_update_path(pl_thumb->thumbnail_path_data, GFX_THUMBNAIL_LEFT))
if (gfx_thumbnail_get_path(pl_thumb->thumbnail_path_data, GFX_THUMBNAIL_LEFT, &left_thumbnail_path))
do_refresh = path_is_valid(left_thumbnail_path);
if (do_refresh)
{
struct menu_state *menu_st = menu_state_get_ptr();
unsigned i = (unsigned)pl_thumb->list_index;
if ( menu_st->driver_ctx
&& menu_st->driver_ctx->refresh_thumbnail_image)
menu_st->driver_ctx->refresh_thumbnail_image(
menu_st->userdata, i);
}
#endif
}
static void task_pl_entry_thumbnail_free(retro_task_t *task)
{
pl_thumb_handle_t *pl_thumb = NULL;
if (task && (pl_thumb = (pl_thumb_handle_t*)task->state))
free_pl_thumb_handle(pl_thumb);
}
static void task_pl_entry_thumbnail_download_handler(retro_task_t *task)
{
pl_thumb_handle_t *pl_thumb = NULL;
if (!task)
return;
if (!(pl_thumb = (pl_thumb_handle_t*)task->state))
goto task_finished;
if (task_get_cancelled(task))
goto task_finished;
switch (pl_thumb->status)
{
case PL_THUMB_BEGIN:
{
const char *label = NULL;
const char *right_thumbnail_path = NULL;
const char *left_thumbnail_path = NULL;
/* Check whether current right/left thumbnails
* already exist (required for menu refresh callback) */
pl_thumb->flags &= ~PL_THUMB_FLAG_RIGHT_THUMB_EXISTS;
pl_thumb->flags &= ~PL_THUMB_FLAG_LEFT_THUMB_EXISTS;
if (gfx_thumbnail_update_path(
pl_thumb->thumbnail_path_data, GFX_THUMBNAIL_RIGHT))
{
if (gfx_thumbnail_get_path(
pl_thumb->thumbnail_path_data,
GFX_THUMBNAIL_RIGHT, &right_thumbnail_path))
{
if (path_is_valid(right_thumbnail_path))
pl_thumb->flags |= PL_THUMB_FLAG_RIGHT_THUMB_EXISTS;
}
}
if (gfx_thumbnail_update_path(
pl_thumb->thumbnail_path_data, GFX_THUMBNAIL_LEFT))
{
if (gfx_thumbnail_get_path(
pl_thumb->thumbnail_path_data,
GFX_THUMBNAIL_LEFT, &left_thumbnail_path))
{
if (path_is_valid(left_thumbnail_path))
pl_thumb->flags |= PL_THUMB_FLAG_LEFT_THUMB_EXISTS;
}
}
/* Set task title */
task_free_title(task);
if (gfx_thumbnail_get_label(
pl_thumb->thumbnail_path_data, &label))
task_set_title(task, strdup(label));
else
task_set_title(task, strdup(""));
task_set_progress(task, 0);
/* All good - can start iterating */
pl_thumb->status = PL_THUMB_ITERATE_TYPE;
}
break;
case PL_THUMB_ITERATE_TYPE:
{
/* Ensure that we only enqueue one transfer
* at a time... */
/* > If HTTP task is NULL, then it either finished
* or an error occurred - in either case,
* current task is 'complete' */
if (!pl_thumb->http_task)
pl_thumb->flags |= PL_THUMB_FLAG_HTTP_TASK_COMPLETE;
/* > Wait for task_push_http_transfer_file()
* callback to trigger */
else if (!(pl_thumb->flags & PL_THUMB_FLAG_HTTP_TASK_COMPLETE))
break;
pl_thumb->http_task = NULL;
/* Check whether all thumbnail types have been processed */
if (pl_thumb->type_idx > 3)
{
pl_thumb->status = PL_THUMB_END;
break;
}
/* Update progress */
task_set_progress(task, ((pl_thumb->type_idx - 1) * 100) / 3);
/* Download current thumbnail */
if (pl_thumb)
download_pl_thumbnail(pl_thumb);
/* Increment thumbnail type */
pl_thumb->type_idx++;
}
break;
case PL_THUMB_END:
default:
task_set_progress(task, 100);
goto task_finished;
}
return;
task_finished:
if (task)
task_set_finished(task, true);
}
static bool task_pl_entry_thumbnail_finder(retro_task_t *task, void *user_data)
{
pl_entry_id_t *entry_id = NULL;
if ( task
&& user_data
&& task->handler == task_pl_entry_thumbnail_download_handler
&& (entry_id = (pl_entry_id_t*)user_data))
{
pl_thumb_handle_t *pl_thumb = NULL;
if ((pl_thumb = (pl_thumb_handle_t*)task->state))
return (entry_id->idx == pl_thumb->list_index) &&
string_is_equal(entry_id->playlist_path, pl_thumb->playlist_path);
}
return false;
}
bool task_push_pl_entry_thumbnail_download(
const char *system,
playlist_t *playlist,
unsigned idx,
bool overwrite,
bool mute)
{
task_finder_data_t find_data;
settings_t *settings = config_get_ptr();
retro_task_t *task = task_init();
pl_thumb_handle_t *pl_thumb = (pl_thumb_handle_t*)calloc(1, sizeof(pl_thumb_handle_t));
pl_entry_id_t *entry_id = (pl_entry_id_t*)malloc(sizeof(pl_entry_id_t));
char *playlist_path = NULL;
gfx_thumbnail_path_data_t *
thumbnail_path_data = NULL;
const char *dir_thumbnails = NULL;
enum playlist_thumbnail_name_flags next_flag = PLAYLIST_THUMBNAIL_FLAG_INVALID;
/* Sanity check */
if (!settings || !task || !pl_thumb || !playlist || !entry_id)
goto error;
dir_thumbnails = settings->paths.directory_thumbnails;
if (string_is_empty(system) ||
string_is_empty(dir_thumbnails) ||
string_is_empty(playlist_get_conf_path(playlist)))
goto error;
if (idx >= playlist_size(playlist))
goto error;
/* Only parse supported playlist types */
if (string_ends_with_size(system, "_history",
strlen(system),
STRLEN_CONST("_history")
))
goto error;
/* Copy playlist path
* (required for task finder and menu refresh functionality) */
playlist_path = strdup(playlist_get_conf_path(playlist));
/* Concurrent download of thumbnails for the same
* playlist entry is not allowed */
entry_id->playlist_path = playlist_path;
entry_id->idx = idx;
find_data.func = task_pl_entry_thumbnail_finder;
find_data.userdata = (void*)entry_id;
if (task_queue_find(&find_data))
goto error;
free(entry_id);
entry_id = NULL;
/* Initialise thumbnail path data
* > Have to do this here rather than in the
* task handler to avoid thread race conditions */
thumbnail_path_data = gfx_thumbnail_path_init();
if (!thumbnail_path_data)
goto error;
if (!gfx_thumbnail_set_system(
thumbnail_path_data, system, playlist))
goto error;
if (!gfx_thumbnail_set_content_playlist(
thumbnail_path_data, playlist, idx))
goto error;
/* Apply flexible thumbnail naming: ROM file name - database name - short name */
next_flag = playlist_get_next_thumbnail_name_flag(playlist,idx);
playlist_update_thumbnail_name_flag(playlist, idx, next_flag);
if (next_flag == PLAYLIST_THUMBNAIL_FLAG_NONE) {
runloop_msg_queue_push(
msg_hash_to_str(MSG_NO_THUMBNAIL_DOWNLOAD_POSSIBLE),
1, 100, true,
NULL, MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
goto error;
}
/* Configure handle
* > Note: playlist_config is unused by this task */
pl_thumb->system = NULL;
pl_thumb->playlist_path = playlist_path;
pl_thumb->dir_thumbnails = strdup(dir_thumbnails);
pl_thumb->playlist = NULL;
pl_thumb->thumbnail_path_data = thumbnail_path_data;
pl_thumb->http_task = NULL;
pl_thumb->list_size = playlist_size(playlist);
pl_thumb->list_index = idx;
pl_thumb->type_idx = 1;
pl_thumb->name_flags = next_flag;
pl_thumb->status = PL_THUMB_BEGIN;
if (overwrite)
pl_thumb->flags = PL_THUMB_FLAG_OVERWRITE;
/* Configure task */
task->handler = task_pl_entry_thumbnail_download_handler;
task->state = pl_thumb;
task->title = strdup(system);
task->alternative_look = true;
task->mute = mute;
task->progress = 0;
task->callback = cb_task_pl_entry_thumbnail_refresh_menu;
task->cleanup = task_pl_entry_thumbnail_free;
task_queue_push(task);
return true;
error:
if (task)
{
free(task);
task = NULL;
}
if (pl_thumb)
{
free(pl_thumb);
pl_thumb = NULL;
}
if (entry_id)
{
free(entry_id);
entry_id = NULL;
}
if (playlist_path)
{
free(playlist_path);
playlist_path = NULL;
}
if (thumbnail_path_data)
{
free(thumbnail_path_data);
thumbnail_path_data = NULL;
}
return false;
}