FIX: [#37199] .ogg file is not recognized as a movie in File Browser
[blender.git] / source / blender / editors / space_file / filelist.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2007 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_file/filelist.c
29  *  \ingroup spfile
30  */
31
32
33 /* global includes */
34
35 #include <stdlib.h>
36 #include <math.h>
37 #include <string.h>
38
39 #ifndef WIN32
40 #include <unistd.h>
41 #else
42 #include <io.h>
43 #include <direct.h>
44 #endif   
45 #include "MEM_guardedalloc.h"
46
47 #include "BLI_blenlib.h"
48 #include "BLI_linklist.h"
49 #include "BLI_threads.h"
50 #include "BLI_utildefines.h"
51 #include "BLI_fileops_types.h"
52
53 #ifdef WIN32
54 #  include "BLI_winstuff.h"
55 #endif
56
57 #include "BKE_context.h"
58 #include "BKE_global.h"
59 #include "BKE_library.h"
60 #include "BKE_icons.h"
61 #include "BKE_main.h"
62 #include "BKE_report.h"
63 #include "BLO_readfile.h"
64 #include "BKE_idcode.h"
65
66 #include "DNA_space_types.h"
67
68 #include "ED_fileselect.h"
69 #include "ED_datafiles.h"
70
71 #include "IMB_imbuf.h"
72 #include "IMB_imbuf_types.h"
73 #include "IMB_thumbs.h"
74
75 #include "PIL_time.h"
76
77 #include "WM_api.h"
78 #include "WM_types.h"
79
80 #include "UI_resources.h"
81
82 #include "filelist.h"
83
84 /* max length of library group name within filesel */
85 #define GROUP_MAX 32
86
87 struct FileList;
88
89 typedef struct FileImage {
90         struct FileImage *next, *prev;
91         char path[FILE_MAX];
92         unsigned int flags;
93         int index;
94         short done;
95         ImBuf *img;
96 } FileImage;
97
98 typedef struct ThumbnailJob {
99         ListBase loadimages;
100         short *stop;
101         short *do_update;
102         struct FileList *filelist;
103         ReportList reports;
104 } ThumbnailJob;
105
106 typedef struct FileList {
107         struct direntry *filelist;
108         int *fidx;
109         int numfiles;
110         int numfiltered;
111         char dir[FILE_MAX];
112         short prv_w;
113         short prv_h;
114         short hide_dot;
115         unsigned int filter;
116         char filter_glob[64];
117         short changed;
118
119         struct BlendHandle *libfiledata;
120         short hide_parent;
121
122         void (*readf)(struct FileList *);
123         int (*filterf)(struct direntry *file, const char *dir, unsigned int filter, short hide_dot);
124
125 } FileList;
126
127 typedef struct FolderList {
128         struct FolderList *next, *prev;
129         char *foldername;
130 } FolderList;
131
132 #define SPECIAL_IMG_SIZE 48
133 #define SPECIAL_IMG_ROWS 4
134 #define SPECIAL_IMG_COLS 4
135
136 #define SPECIAL_IMG_FOLDER 0
137 #define SPECIAL_IMG_PARENT 1
138 #define SPECIAL_IMG_REFRESH 2
139 #define SPECIAL_IMG_BLENDFILE 3
140 #define SPECIAL_IMG_SOUNDFILE 4
141 #define SPECIAL_IMG_MOVIEFILE 5
142 #define SPECIAL_IMG_PYTHONFILE 6
143 #define SPECIAL_IMG_TEXTFILE 7
144 #define SPECIAL_IMG_FONTFILE 8
145 #define SPECIAL_IMG_UNKNOWNFILE 9
146 #define SPECIAL_IMG_LOADING 10
147 #define SPECIAL_IMG_BACKUP 11
148 #define SPECIAL_IMG_MAX SPECIAL_IMG_BACKUP + 1
149
150 static ImBuf *gSpecialFileImages[SPECIAL_IMG_MAX];
151
152
153 /* ******************* SORT ******************* */
154
155 static bool compare_is_directory(const struct direntry *entry)
156 {
157         /* for library browse .blend files may be treated as directories, but
158          * for sorting purposes they should be considered regular files */
159         if (S_ISDIR(entry->type))
160                 return !(entry->flags & (BLENDERFILE | BLENDERFILE_BACKUP));
161         
162         return false;
163 }
164
165 static int compare_name(const void *a1, const void *a2)
166 {
167         const struct direntry *entry1 = a1, *entry2 = a2;
168
169         /* type is equal to stat.st_mode */
170
171         if (compare_is_directory(entry1)) {
172                 if (compare_is_directory(entry2) == 0) return (-1);
173         }
174         else {
175                 if (compare_is_directory(entry2)) return (1);
176         }
177         if (S_ISREG(entry1->type)) {
178                 if (S_ISREG(entry2->type) == 0) return (-1);
179         }
180         else {
181                 if (S_ISREG(entry2->type)) return (1);
182         }
183         if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
184         if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
185         
186         /* make sure "." and ".." are always first */
187         if (strcmp(entry1->relname, ".") == 0) return (-1);
188         if (strcmp(entry2->relname, ".") == 0) return (1);
189         if (strcmp(entry1->relname, "..") == 0) return (-1);
190         if (strcmp(entry2->relname, "..") == 0) return (1);
191         
192         return (BLI_natstrcmp(entry1->relname, entry2->relname));
193 }
194
195 static int compare_date(const void *a1, const void *a2) 
196 {
197         const struct direntry *entry1 = a1, *entry2 = a2;
198         
199         /* type is equal to stat.st_mode */
200
201         if (compare_is_directory(entry1)) {
202                 if (compare_is_directory(entry2) == 0) return (-1);
203         }
204         else {
205                 if (compare_is_directory(entry2)) return (1);
206         }
207         if (S_ISREG(entry1->type)) {
208                 if (S_ISREG(entry2->type) == 0) return (-1);
209         }
210         else {
211                 if (S_ISREG(entry2->type)) return (1);
212         }
213         if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
214         if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
215
216         /* make sure "." and ".." are always first */
217         if (strcmp(entry1->relname, ".") == 0) return (-1);
218         if (strcmp(entry2->relname, ".") == 0) return (1);
219         if (strcmp(entry1->relname, "..") == 0) return (-1);
220         if (strcmp(entry2->relname, "..") == 0) return (1);
221         
222         if (entry1->s.st_mtime < entry2->s.st_mtime) return 1;
223         if (entry1->s.st_mtime > entry2->s.st_mtime) return -1;
224         
225         else return BLI_natstrcmp(entry1->relname, entry2->relname);
226 }
227
228 static int compare_size(const void *a1, const void *a2) 
229 {
230         const struct direntry *entry1 = a1, *entry2 = a2;
231
232         /* type is equal to stat.st_mode */
233
234         if (compare_is_directory(entry1)) {
235                 if (compare_is_directory(entry2) == 0) return (-1);
236         }
237         else {
238                 if (compare_is_directory(entry2)) return (1);
239         }
240         if (S_ISREG(entry1->type)) {
241                 if (S_ISREG(entry2->type) == 0) return (-1);
242         }
243         else {
244                 if (S_ISREG(entry2->type)) return (1);
245         }
246         if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
247         if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
248
249         /* make sure "." and ".." are always first */
250         if (strcmp(entry1->relname, ".") == 0) return (-1);
251         if (strcmp(entry2->relname, ".") == 0) return (1);
252         if (strcmp(entry1->relname, "..") == 0) return (-1);
253         if (strcmp(entry2->relname, "..") == 0) return (1);
254         
255         if (entry1->s.st_size < entry2->s.st_size) return 1;
256         if (entry1->s.st_size > entry2->s.st_size) return -1;
257         else return BLI_natstrcmp(entry1->relname, entry2->relname);
258 }
259
260 static int compare_extension(const void *a1, const void *a2)
261 {
262         const struct direntry *entry1 = a1, *entry2 = a2;
263         const char *sufix1, *sufix2;
264         const char *nil = "";
265
266         if (!(sufix1 = strstr(entry1->relname, ".blend.gz")))
267                 sufix1 = strrchr(entry1->relname, '.');
268         if (!(sufix2 = strstr(entry2->relname, ".blend.gz")))
269                 sufix2 = strrchr(entry2->relname, '.');
270         if (!sufix1) sufix1 = nil;
271         if (!sufix2) sufix2 = nil;
272
273         /* type is equal to stat.st_mode */
274
275         if (compare_is_directory(entry1)) {
276                 if (compare_is_directory(entry2) == 0) return (-1);
277         }
278         else {
279                 if (compare_is_directory(entry2)) return (1);
280         }
281         if (S_ISREG(entry1->type)) {
282                 if (S_ISREG(entry2->type) == 0) return (-1);
283         }
284         else {
285                 if (S_ISREG(entry2->type)) return (1);
286         }
287         if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
288         if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
289         
290         /* make sure "." and ".." are always first */
291         if (strcmp(entry1->relname, ".") == 0) return (-1);
292         if (strcmp(entry2->relname, ".") == 0) return (1);
293         if (strcmp(entry1->relname, "..") == 0) return (-1);
294         if (strcmp(entry2->relname, "..") == 0) return (1);
295         
296         return (BLI_strcasecmp(sufix1, sufix2));
297 }
298
299 static int is_hidden_file(const char *filename, short hide_dot)
300 {
301         int is_hidden = 0;
302
303         if (hide_dot) {
304                 if (filename[0] == '.' && filename[1] != '.' && filename[1] != 0) {
305                         is_hidden = 1; /* ignore .file */
306                 }
307                 else if (((filename[0] == '.') && (filename[1] == 0) )) {
308                         is_hidden = 1; /* ignore . */
309                 }
310                 else {
311                         int len = strlen(filename);
312                         if ( (len > 0) && (filename[len - 1] == '~') ) {
313                                 is_hidden = 1;  /* ignore file~ */
314                         }
315                 }
316         }
317         else {
318                 if (((filename[0] == '.') && (filename[1] == 0) )) {
319                         is_hidden = 1; /* ignore . */
320                 }
321         }
322         return is_hidden;
323 }
324
325 static int is_filtered_file(struct direntry *file, const char *UNUSED(dir), unsigned int filter, short hide_dot)
326 {
327         int is_filtered = 0;
328         if (filter) {
329                 if (file->flags & filter) {
330                         is_filtered = 1;
331                 }
332                 else if (file->type & S_IFDIR) {
333                         if (filter & FOLDERFILE) {
334                                 is_filtered = 1;
335                         }
336                 }
337         }
338         else {
339                 is_filtered = 1;
340         }
341         return is_filtered && !is_hidden_file(file->relname, hide_dot);
342 }
343
344 static int is_filtered_lib(struct direntry *file, const char *dir, unsigned int filter, short hide_dot)
345 {
346         int is_filtered = 0;
347         char tdir[FILE_MAX], tgroup[GROUP_MAX];
348         if (BLO_is_a_library(dir, tdir, tgroup)) {
349                 is_filtered = !is_hidden_file(file->relname, hide_dot);
350         }
351         else {
352                 is_filtered = is_filtered_file(file, dir, filter, hide_dot);
353         }
354         return is_filtered;
355 }
356
357 static int is_filtered_main(struct direntry *file, const char *UNUSED(dir), unsigned int UNUSED(filter), short hide_dot)
358 {
359         return !is_hidden_file(file->relname, hide_dot);
360 }
361
362 void filelist_filter(FileList *filelist)
363 {
364         int num_filtered = 0;
365         int i, j;
366
367         if (!filelist->filelist)
368                 return;
369
370         /* How many files are left after filter ? */
371         for (i = 0; i < filelist->numfiles; ++i) {
372                 struct direntry *file = &filelist->filelist[i];
373                 if (filelist->filterf(file, filelist->dir, filelist->filter, filelist->hide_dot) ) {
374                         num_filtered++;
375                 }
376         }
377         
378         if (filelist->fidx) {
379                 MEM_freeN(filelist->fidx);
380                 filelist->fidx = NULL;
381         }
382         filelist->fidx = (int *)MEM_callocN(num_filtered * sizeof(int), "filteridx");
383         filelist->numfiltered = num_filtered;
384
385         for (i = 0, j = 0; i < filelist->numfiles; ++i) {
386                 struct direntry *file = &filelist->filelist[i];
387                 if (filelist->filterf(file, filelist->dir, filelist->filter, filelist->hide_dot) ) {
388                         filelist->fidx[j++] = i;
389                 }
390         }
391 }
392
393 void filelist_init_icons(void)
394 {
395         short x, y, k;
396         ImBuf *bbuf;
397         ImBuf *ibuf;
398
399         BLI_assert(G.background == FALSE);
400
401 #ifdef WITH_HEADLESS
402         bbuf = NULL;
403 #else
404         bbuf = IMB_ibImageFromMemory((unsigned char *)datatoc_prvicons_png, datatoc_prvicons_png_size, IB_rect, NULL, "<splash>");
405 #endif
406         if (bbuf) {
407                 for (y = 0; y < SPECIAL_IMG_ROWS; y++) {
408                         for (x = 0; x < SPECIAL_IMG_COLS; x++) {
409                                 int tile = SPECIAL_IMG_COLS * y + x;
410                                 if (tile < SPECIAL_IMG_MAX) {
411                                         ibuf = IMB_allocImBuf(SPECIAL_IMG_SIZE, SPECIAL_IMG_SIZE, 32, IB_rect);
412                                         for (k = 0; k < SPECIAL_IMG_SIZE; k++) {
413                                                 memcpy(&ibuf->rect[k * SPECIAL_IMG_SIZE], &bbuf->rect[(k + y * SPECIAL_IMG_SIZE) * SPECIAL_IMG_SIZE * SPECIAL_IMG_COLS + x * SPECIAL_IMG_SIZE], SPECIAL_IMG_SIZE * sizeof(int));
414                                         }
415                                         gSpecialFileImages[tile] = ibuf;
416                                 }
417                         }
418                 }
419                 IMB_freeImBuf(bbuf);
420         }
421 }
422
423 void filelist_free_icons(void)
424 {
425         int i;
426
427         BLI_assert(G.background == FALSE);
428
429         for (i = 0; i < SPECIAL_IMG_MAX; ++i) {
430                 IMB_freeImBuf(gSpecialFileImages[i]);
431                 gSpecialFileImages[i] = NULL;
432         }
433 }
434
435 /* -----------------FOLDERLIST (previous/next) -------------- */
436 ListBase *folderlist_new(void)
437 {
438         ListBase *p = MEM_callocN(sizeof(ListBase), "folderlist");
439         return p;
440 }
441
442 void folderlist_popdir(struct ListBase *folderlist, char *dir)
443 {
444         const char *prev_dir;
445         struct FolderList *folder;
446         folder = folderlist->last;
447
448         if (folder) {
449                 /* remove the current directory */
450                 MEM_freeN(folder->foldername);
451                 BLI_freelinkN(folderlist, folder);
452
453                 folder = folderlist->last;
454                 if (folder) {
455                         prev_dir = folder->foldername;
456                         BLI_strncpy(dir, prev_dir, FILE_MAXDIR);
457                 }
458         }
459         /* delete the folder next or use setdir directly before PREVIOUS OP */
460 }
461
462 void folderlist_pushdir(ListBase *folderlist, const char *dir)
463 {
464         struct FolderList *folder, *previous_folder;
465         previous_folder = folderlist->last;
466
467         /* check if already exists */
468         if (previous_folder && previous_folder->foldername) {
469                 if (BLI_path_cmp(previous_folder->foldername, dir) == 0) {
470                         return;
471                 }
472         }
473
474         /* create next folder element */
475         folder = (FolderList *)MEM_mallocN(sizeof(FolderList), "FolderList");
476         folder->foldername = BLI_strdup(dir);
477
478         /* add it to the end of the list */
479         BLI_addtail(folderlist, folder);
480 }
481
482 int folderlist_clear_next(struct SpaceFile *sfile)
483 {
484         struct FolderList *folder;
485
486         /* if there is no folder_next there is nothing we can clear */
487         if (!sfile->folders_next)
488                 return 0;
489
490         /* if previous_folder, next_folder or refresh_folder operators are executed it doesn't clear folder_next */
491         folder = sfile->folders_prev->last;
492         if ((!folder) || (BLI_path_cmp(folder->foldername, sfile->params->dir) == 0))
493                 return 0;
494
495         /* eventually clear flist->folders_next */
496         return 1;
497 }
498
499 /* not listbase itself */
500 void folderlist_free(ListBase *folderlist)
501 {
502         if (folderlist) {
503                 FolderList *folder;
504                 for (folder = folderlist->first; folder; folder = folder->next)
505                         MEM_freeN(folder->foldername);
506                 BLI_freelistN(folderlist);
507         }
508 }
509
510 ListBase *folderlist_duplicate(ListBase *folderlist)
511 {
512         
513         if (folderlist) {
514                 ListBase *folderlistn = MEM_callocN(sizeof(ListBase), "copy folderlist");
515                 FolderList *folder;
516                 
517                 BLI_duplicatelist(folderlistn, folderlist);
518                 
519                 for (folder = folderlistn->first; folder; folder = folder->next) {
520                         folder->foldername = MEM_dupallocN(folder->foldername);
521                 }
522                 return folderlistn;
523         }
524         return NULL;
525 }
526
527
528 static void filelist_read_main(struct FileList *filelist);
529 static void filelist_read_library(struct FileList *filelist);
530 static void filelist_read_dir(struct FileList *filelist);
531
532 /* ------------------FILELIST------------------------ */
533 FileList *filelist_new(short type)
534 {
535         FileList *p = MEM_callocN(sizeof(FileList), "filelist");
536         switch (type) {
537                 case FILE_MAIN:
538                         p->readf = filelist_read_main;
539                         p->filterf = is_filtered_main;
540                         break;
541                 case FILE_LOADLIB:
542                         p->readf = filelist_read_library;
543                         p->filterf = is_filtered_lib;
544                         break;
545                 default:
546                         p->readf = filelist_read_dir;
547                         p->filterf = is_filtered_file;
548                         break;
549
550         }
551         return p;
552 }
553
554
555 void filelist_free(struct FileList *filelist)
556 {
557         if (!filelist) {
558                 printf("Attempting to delete empty filelist.\n");
559                 return;
560         }
561         
562         if (filelist->fidx) {
563                 MEM_freeN(filelist->fidx);
564                 filelist->fidx = NULL;
565         }
566
567         BLI_free_filelist(filelist->filelist, filelist->numfiles);
568         filelist->numfiles = 0;
569         filelist->filelist = NULL;
570         filelist->filter = 0;
571         filelist->filter_glob[0] = '\0';
572         filelist->numfiltered = 0;
573         filelist->hide_dot = 0;
574 }
575
576 void filelist_freelib(struct FileList *filelist)
577 {
578         if (filelist->libfiledata)
579                 BLO_blendhandle_close(filelist->libfiledata);
580         filelist->libfiledata = NULL;
581 }
582
583 BlendHandle *filelist_lib(struct FileList *filelist)
584 {
585         return filelist->libfiledata;
586 }
587
588 int filelist_numfiles(struct FileList *filelist)
589 {
590         return filelist->numfiltered;
591 }
592
593 const char *filelist_dir(struct FileList *filelist)
594 {
595         return filelist->dir;
596 }
597
598 void filelist_setdir(struct FileList *filelist, const char *dir)
599 {
600         BLI_strncpy(filelist->dir, dir, sizeof(filelist->dir));
601 }
602
603 void filelist_imgsize(struct FileList *filelist, short w, short h)
604 {
605         filelist->prv_w = w;
606         filelist->prv_h = h;
607 }
608
609 short filelist_changed(struct FileList *filelist)
610 {
611         return filelist->changed;
612 }
613
614 ImBuf *filelist_getimage(struct FileList *filelist, int index)
615 {
616         ImBuf *ibuf = NULL;
617         int fidx = 0;
618
619         BLI_assert(G.background == FALSE);
620
621         if ( (index < 0) || (index >= filelist->numfiltered) ) {
622                 return NULL;
623         }
624         fidx = filelist->fidx[index];
625         ibuf = filelist->filelist[fidx].image;
626
627         return ibuf;
628 }
629
630 ImBuf *filelist_geticon(struct FileList *filelist, int index)
631 {
632         ImBuf *ibuf = NULL;
633         struct direntry *file = NULL;
634         int fidx = 0;
635
636         BLI_assert(G.background == FALSE);
637
638         if ( (index < 0) || (index >= filelist->numfiltered) ) {
639                 return NULL;
640         }
641         fidx = filelist->fidx[index];
642         file = &filelist->filelist[fidx];
643         if (file->type & S_IFDIR) {
644                 if (strcmp(filelist->filelist[fidx].relname, "..") == 0) {
645                         ibuf = gSpecialFileImages[SPECIAL_IMG_PARENT];
646                 }
647                 else if (strcmp(filelist->filelist[fidx].relname, ".") == 0) {
648                         ibuf = gSpecialFileImages[SPECIAL_IMG_REFRESH];
649                 }
650                 else {
651                         ibuf = gSpecialFileImages[SPECIAL_IMG_FOLDER];
652                 }
653         }
654         else {
655                 ibuf = gSpecialFileImages[SPECIAL_IMG_UNKNOWNFILE];
656         }
657
658         if (file->flags & BLENDERFILE) {
659                 ibuf = gSpecialFileImages[SPECIAL_IMG_BLENDFILE];
660         }
661         else if ( (file->flags & MOVIEFILE) || (file->flags & MOVIEFILE_ICON) ) {
662                 ibuf = gSpecialFileImages[SPECIAL_IMG_MOVIEFILE];
663         }
664         else if (file->flags & SOUNDFILE) {
665                 ibuf = gSpecialFileImages[SPECIAL_IMG_SOUNDFILE];
666         }
667         else if (file->flags & PYSCRIPTFILE) {
668                 ibuf = gSpecialFileImages[SPECIAL_IMG_PYTHONFILE];
669         }
670         else if (file->flags & FTFONTFILE) {
671                 ibuf = gSpecialFileImages[SPECIAL_IMG_FONTFILE];
672         }
673         else if (file->flags & TEXTFILE) {
674                 ibuf = gSpecialFileImages[SPECIAL_IMG_TEXTFILE];
675         }
676         else if (file->flags & IMAGEFILE) {
677                 ibuf = gSpecialFileImages[SPECIAL_IMG_LOADING];
678         }
679         else if (file->flags & BLENDERFILE_BACKUP) {
680                 ibuf = gSpecialFileImages[SPECIAL_IMG_BACKUP];
681         }
682
683         return ibuf;
684 }
685
686 struct direntry *filelist_file(struct FileList *filelist, int index)
687 {
688         int fidx = 0;
689         
690         if ( (index < 0) || (index >= filelist->numfiltered) ) {
691                 return NULL;
692         }
693         fidx = filelist->fidx[index];
694
695         return &filelist->filelist[fidx];
696 }
697
698 int filelist_find(struct FileList *filelist, const char *filename)
699 {
700         int index = -1;
701         int i;
702         int fidx = -1;
703         
704         if (!filelist->fidx) 
705                 return fidx;
706
707         
708         for (i = 0; i < filelist->numfiles; ++i) {
709                 if (strcmp(filelist->filelist[i].relname, filename) == 0) {  /* not dealing with user input so don't need BLI_path_cmp */
710                         index = i;
711                         break;
712                 }
713         }
714
715         for (i = 0; i < filelist->numfiltered; ++i) {
716                 if (filelist->fidx[i] == index) {
717                         fidx = i;
718                         break;
719                 }
720         }
721         return fidx;
722 }
723
724 void filelist_hidedot(struct FileList *filelist, short hide)
725 {
726         filelist->hide_dot = hide;
727 }
728
729 void filelist_setfilter(struct FileList *filelist, unsigned int filter)
730 {
731         filelist->filter = filter;
732 }
733
734 void filelist_setfilter_types(struct FileList *filelist, const char *filter_glob)
735 {
736         BLI_strncpy(filelist->filter_glob, filter_glob, sizeof(filelist->filter_glob));
737 }
738
739 /* would recognize .blend as well */
740 static int file_is_blend_backup(const char *str)
741 {
742         short a, b;
743         int retval = 0;
744         
745         a = strlen(str);
746         b = 7;
747         
748         if (a == 0 || b >= a) {
749                 /* pass */
750         }
751         else {
752                 char *loc;
753                 
754                 if (a > b + 1)
755                         b++;
756                 
757                 /* allow .blend1 .blend2 .blend32 */
758                 loc = BLI_strcasestr(str + a - b, ".blend");
759                 
760                 if (loc)
761                         retval = 1;
762         }
763         
764         return (retval);
765 }
766
767 static int path_extension_type(const char *path)
768 {
769         if (BLO_has_bfile_extension(path)) {
770                 return BLENDERFILE;
771         }
772         else if (file_is_blend_backup(path)) {
773                 return BLENDERFILE_BACKUP;
774         }
775         else if (BLI_testextensie(path, ".py")) {
776                 return PYSCRIPTFILE;
777         }
778         else if (BLI_testextensie(path, ".txt")  ||
779                  BLI_testextensie(path, ".glsl") ||
780                  BLI_testextensie(path, ".osl")  ||
781                  BLI_testextensie(path, ".data"))
782         {
783                 return TEXTFILE;
784         }
785         else if (BLI_testextensie(path, ".ttf") ||
786                  BLI_testextensie(path, ".ttc") ||
787                  BLI_testextensie(path, ".pfb") ||
788                  BLI_testextensie(path, ".otf") ||
789                  BLI_testextensie(path, ".otc"))
790         {
791                 return FTFONTFILE;
792         }
793         else if (BLI_testextensie(path, ".btx")) {
794                 return BTXFILE;
795         }
796         else if (BLI_testextensie(path, ".dae")) {
797                 return COLLADAFILE;
798         }
799         else if (BLI_testextensie_array(path, imb_ext_image) ||
800                  (G.have_quicktime && BLI_testextensie_array(path, imb_ext_image_qt)))
801         {
802                 return IMAGEFILE;
803         }
804         else if (BLI_testextensie(path, ".ogg")) {
805                 if (IMB_isanim(path)) {
806                         return MOVIEFILE;
807                 } else {
808                         return SOUNDFILE;
809                 }
810         }
811         else if (BLI_testextensie_array(path, imb_ext_movie)) {
812                 return MOVIEFILE;
813         }
814         else if (BLI_testextensie_array(path, imb_ext_audio)) {
815                 return SOUNDFILE;
816         }
817         return 0;
818 }
819
820 static int file_extension_type(const char *dir, const char *relname)
821 {
822         char path[FILE_MAX];
823         BLI_join_dirfile(path, sizeof(path), dir, relname);
824         return path_extension_type(path);
825 }
826
827 int ED_file_extension_icon(const char *path)
828 {
829         int type = path_extension_type(path);
830         
831         if (type == BLENDERFILE)
832                 return ICON_FILE_BLEND;
833         else if (type == BLENDERFILE_BACKUP)
834                 return ICON_FILE_BACKUP;
835         else if (type == IMAGEFILE)
836                 return ICON_FILE_IMAGE;
837         else if (type == MOVIEFILE)
838                 return ICON_FILE_MOVIE;
839         else if (type == PYSCRIPTFILE)
840                 return ICON_FILE_SCRIPT;
841         else if (type == SOUNDFILE)
842                 return ICON_FILE_SOUND;
843         else if (type == FTFONTFILE)
844                 return ICON_FILE_FONT;
845         else if (type == BTXFILE)
846                 return ICON_FILE_BLANK;
847         else if (type == COLLADAFILE)
848                 return ICON_FILE_BLANK;
849         else if (type == TEXTFILE)
850                 return ICON_FILE_TEXT;
851         
852         return ICON_FILE_BLANK;
853 }
854
855 static void filelist_setfiletypes(struct FileList *filelist)
856 {
857         struct direntry *file;
858         int num;
859         
860         file = filelist->filelist;
861         
862         for (num = 0; num < filelist->numfiles; num++, file++) {
863                 file->type = file->s.st_mode;  /* restore the mess below */
864                 
865                 /* Don't check extensions for directories */ 
866                 if (file->type & S_IFDIR) {
867                         continue;
868                 }
869                 file->flags = file_extension_type(filelist->dir, file->relname);
870                 
871                 if (filelist->filter_glob[0] &&
872                     BLI_testextensie_glob(file->relname, filelist->filter_glob))
873                 {
874                         file->flags = OPERATORFILE;
875                 }
876                 
877         }
878 }
879
880 static void filelist_read_dir(struct FileList *filelist)
881 {
882         if (!filelist) return;
883
884         filelist->fidx = NULL;
885         filelist->filelist = NULL;
886
887         BLI_cleanup_dir(G.main->name, filelist->dir);
888         filelist->numfiles = BLI_dir_contents(filelist->dir, &(filelist->filelist));
889
890         filelist_setfiletypes(filelist);
891         filelist_filter(filelist);
892 }
893
894 static void filelist_read_main(struct FileList *filelist)
895 {
896         if (!filelist) return;
897         filelist_from_main(filelist);
898 }
899
900 static void filelist_read_library(struct FileList *filelist)
901 {
902         if (!filelist) return;
903         BLI_cleanup_dir(G.main->name, filelist->dir);
904         filelist_from_library(filelist);
905         if (!filelist->libfiledata) {
906                 int num;
907                 struct direntry *file;
908
909                 BLI_make_exist(filelist->dir);
910                 filelist_read_dir(filelist);
911                 file = filelist->filelist;
912                 for (num = 0; num < filelist->numfiles; num++, file++) {
913                         if (BLO_has_bfile_extension(file->relname)) {
914                                 char name[FILE_MAX];
915
916                                 BLI_join_dirfile(name, sizeof(name), filelist->dir, file->relname);
917
918                                 /* prevent current file being used as acceptable dir */
919                                 if (BLI_path_cmp(G.main->name, name) != 0) {
920                                         file->type &= ~S_IFMT;
921                                         file->type |= S_IFDIR;
922                                 }
923                         }
924                 }
925         }
926 }
927
928 void filelist_readdir(struct FileList *filelist)
929 {
930         filelist->readf(filelist);
931 }
932
933 int filelist_empty(struct FileList *filelist)
934 {       
935         return filelist->filelist == NULL;
936 }
937
938 void filelist_parent(struct FileList *filelist)
939 {
940         BLI_parent_dir(filelist->dir);
941         BLI_make_exist(filelist->dir);
942         filelist_readdir(filelist);
943 }
944
945 void filelist_select_file(struct FileList *filelist, int index, FileSelType select, unsigned int flag, FileCheckType check)
946 {
947         struct direntry *file = filelist_file(filelist, index);
948         if (file != NULL) {
949                 int check_ok = 0; 
950                 switch (check) {
951                         case CHECK_DIRS:
952                                 check_ok = S_ISDIR(file->type);
953                                 break;
954                         case CHECK_ALL:
955                                 check_ok = 1;
956                                 break;
957                         case CHECK_FILES:
958                         default:
959                                 check_ok = !S_ISDIR(file->type);
960                                 break;
961                 }
962                 if (check_ok) {
963                         switch (select) {
964                                 case FILE_SEL_REMOVE:
965                                         file->selflag &= ~flag;
966                                         break;
967                                 case FILE_SEL_ADD:
968                                         file->selflag |= flag;
969                                         break;
970                                 case FILE_SEL_TOGGLE:
971                                         file->selflag ^= flag;
972                                         break;
973                         }
974                 }
975         }
976 }
977
978 void filelist_select(struct FileList *filelist, FileSelection *sel, FileSelType select, unsigned int flag, FileCheckType check)
979 {
980         /* select all valid files between first and last indicated */
981         if ( (sel->first >= 0) && (sel->first < filelist->numfiltered) && (sel->last >= 0) && (sel->last < filelist->numfiltered) ) {
982                 int current_file;
983                 for (current_file = sel->first; current_file <= sel->last; current_file++) {
984                         filelist_select_file(filelist, current_file, select, flag, check);
985                 }
986         }
987 }
988
989 int filelist_is_selected(struct FileList *filelist, int index, FileCheckType check)
990 {
991         struct direntry *file = filelist_file(filelist, index);
992         if (!file) {
993                 return 0;
994         }
995         switch (check) {
996                 case CHECK_DIRS:
997                         return S_ISDIR(file->type) && (file->selflag & SELECTED_FILE);
998                 case CHECK_FILES:
999                         return S_ISREG(file->type) && (file->selflag & SELECTED_FILE);
1000                 case CHECK_ALL:
1001                 default:
1002                         return (file->selflag & SELECTED_FILE);
1003         }
1004 }
1005
1006 void filelist_sort(struct FileList *filelist, short sort)
1007 {
1008         switch (sort) {
1009                 case FILE_SORT_ALPHA:
1010                         qsort(filelist->filelist, filelist->numfiles, sizeof(struct direntry), compare_name);
1011                         break;
1012                 case FILE_SORT_TIME:
1013                         qsort(filelist->filelist, filelist->numfiles, sizeof(struct direntry), compare_date);
1014                         break;
1015                 case FILE_SORT_SIZE:
1016                         qsort(filelist->filelist, filelist->numfiles, sizeof(struct direntry), compare_size);
1017                         break;
1018                 case FILE_SORT_EXTENSION:
1019                         qsort(filelist->filelist, filelist->numfiles, sizeof(struct direntry), compare_extension);
1020                         break;
1021         }
1022
1023         filelist_filter(filelist);
1024 }
1025
1026
1027 int filelist_islibrary(struct FileList *filelist, char *dir, char *group)
1028 {
1029         return BLO_is_a_library(filelist->dir, dir, group);
1030 }
1031
1032 static int groupname_to_code(const char *group)
1033 {
1034         char buf[32];
1035         char *lslash;
1036         
1037         BLI_strncpy(buf, group, sizeof(buf));
1038         lslash = (char *)BLI_last_slash(buf);
1039         if (lslash)
1040                 lslash[0] = '\0';
1041
1042         return BKE_idcode_from_name(buf);
1043 }
1044  
1045 void filelist_from_library(struct FileList *filelist)
1046 {
1047         LinkNode *l, *names, *previews;
1048         struct ImBuf *ima;
1049         int ok, i, nprevs, nnames, idcode;
1050         char filename[FILE_MAX];
1051         char dir[FILE_MAX], group[GROUP_MAX];
1052         
1053         /* name test */
1054         ok = filelist_islibrary(filelist, dir, group);
1055         if (!ok) {
1056                 /* free */
1057                 if (filelist->libfiledata) BLO_blendhandle_close(filelist->libfiledata);
1058                 filelist->libfiledata = NULL;
1059                 return;
1060         }
1061         
1062         BLI_strncpy(filename, G.main->name, sizeof(filename));
1063
1064         /* there we go */
1065         /* for the time being only read filedata when libfiledata==0 */
1066         if (filelist->libfiledata == NULL) {
1067                 filelist->libfiledata = BLO_blendhandle_from_file(dir, NULL);
1068                 if (filelist->libfiledata == NULL) return;
1069         }
1070         
1071         idcode = groupname_to_code(group);
1072
1073         /* memory for strings is passed into filelist[i].relname
1074          * and freed in freefilelist */
1075         if (idcode) {
1076                 previews = BLO_blendhandle_get_previews(filelist->libfiledata, idcode, &nprevs);
1077                 names = BLO_blendhandle_get_datablock_names(filelist->libfiledata, idcode, &nnames);
1078                 /* ugh, no rewind, need to reopen */
1079                 BLO_blendhandle_close(filelist->libfiledata);
1080                 filelist->libfiledata = BLO_blendhandle_from_file(dir, NULL);
1081                 
1082         }
1083         else {
1084                 previews = NULL;
1085                 nprevs = 0;
1086                 names = BLO_blendhandle_get_linkable_groups(filelist->libfiledata);
1087                 nnames = BLI_linklist_length(names);
1088         }
1089
1090         filelist->numfiles = nnames + 1;
1091         filelist->filelist = malloc(filelist->numfiles * sizeof(*filelist->filelist));
1092         memset(filelist->filelist, 0, filelist->numfiles * sizeof(*filelist->filelist));
1093
1094         filelist->filelist[0].relname = BLI_strdup("..");
1095         filelist->filelist[0].type |= S_IFDIR;
1096                 
1097         for (i = 0, l = names; i < nnames; i++, l = l->next) {
1098                 char *blockname = l->link;
1099
1100                 filelist->filelist[i + 1].relname = BLI_strdup(blockname);
1101                 if (idcode) {
1102                         filelist->filelist[i + 1].type |= S_IFREG;
1103                 }
1104                 else {
1105                         filelist->filelist[i + 1].type |= S_IFDIR;
1106                 }
1107         }
1108         
1109         if (previews && (nnames != nprevs)) {
1110                 printf("filelist_from_library: error, found %d items, %d previews\n", nnames, nprevs);
1111         }
1112         else if (previews) {
1113                 for (i = 0, l = previews; i < nnames; i++, l = l->next) {
1114                         PreviewImage *img = l->link;
1115                         
1116                         if (img) {
1117                                 unsigned int w = img->w[ICON_SIZE_PREVIEW];
1118                                 unsigned int h = img->h[ICON_SIZE_PREVIEW];
1119                                 unsigned int *rect = img->rect[ICON_SIZE_PREVIEW];
1120
1121                                 /* first allocate imbuf for copying preview into it */
1122                                 if (w > 0 && h > 0 && rect) {
1123                                         ima = IMB_allocImBuf(w, h, 32, IB_rect);
1124                                         memcpy(ima->rect, rect, w * h * sizeof(unsigned int));
1125                                         filelist->filelist[i + 1].image = ima;
1126                                         filelist->filelist[i + 1].flags = IMAGEFILE;
1127                                 }
1128                         }
1129                 }
1130         }
1131
1132         BLI_linklist_free(names, free);
1133         if (previews) BLI_linklist_free(previews, BKE_previewimg_freefunc);
1134
1135         filelist_sort(filelist, FILE_SORT_ALPHA);
1136
1137         BLI_strncpy(G.main->name, filename, sizeof(filename));  /* prevent G.main->name to change */
1138
1139         filelist->filter = 0;
1140         filelist_filter(filelist);
1141 }
1142
1143 void filelist_hideparent(struct FileList *filelist, short hide)
1144 {
1145         filelist->hide_parent = hide;
1146 }
1147
1148 void filelist_from_main(struct FileList *filelist)
1149 {
1150         ID *id;
1151         struct direntry *files, *firstlib = NULL;
1152         ListBase *lb;
1153         int a, fake, idcode, ok, totlib, totbl;
1154         
1155         // filelist->type = FILE_MAIN; // XXXXX TODO: add modes to filebrowser
1156
1157         if (filelist->dir[0] == '/') filelist->dir[0] = 0;
1158         
1159         if (filelist->dir[0]) {
1160                 idcode = groupname_to_code(filelist->dir);
1161                 if (idcode == 0) filelist->dir[0] = 0;
1162         }
1163         
1164         if (filelist->dir[0] == 0) {
1165                 
1166                 /* make directories */
1167 #ifdef WITH_FREESTYLE
1168                 filelist->numfiles = 25;
1169 #else
1170                 filelist->numfiles = 24;
1171 #endif
1172                 filelist->filelist = (struct direntry *)malloc(filelist->numfiles * sizeof(struct direntry));
1173                 
1174                 for (a = 0; a < filelist->numfiles; a++) {
1175                         memset(&(filelist->filelist[a]), 0, sizeof(struct direntry));
1176                         filelist->filelist[a].type |= S_IFDIR;
1177                 }
1178                 
1179                 filelist->filelist[0].relname = BLI_strdup("..");
1180                 filelist->filelist[2].relname = BLI_strdup("Scene");
1181                 filelist->filelist[3].relname = BLI_strdup("Object");
1182                 filelist->filelist[4].relname = BLI_strdup("Mesh");
1183                 filelist->filelist[5].relname = BLI_strdup("Curve");
1184                 filelist->filelist[6].relname = BLI_strdup("Metaball");
1185                 filelist->filelist[7].relname = BLI_strdup("Material");
1186                 filelist->filelist[8].relname = BLI_strdup("Texture");
1187                 filelist->filelist[9].relname = BLI_strdup("Image");
1188                 filelist->filelist[10].relname = BLI_strdup("Ika");
1189                 filelist->filelist[11].relname = BLI_strdup("Wave");
1190                 filelist->filelist[12].relname = BLI_strdup("Lattice");
1191                 filelist->filelist[13].relname = BLI_strdup("Lamp");
1192                 filelist->filelist[14].relname = BLI_strdup("Camera");
1193                 filelist->filelist[15].relname = BLI_strdup("Ipo");
1194                 filelist->filelist[16].relname = BLI_strdup("World");
1195                 filelist->filelist[17].relname = BLI_strdup("Screen");
1196                 filelist->filelist[18].relname = BLI_strdup("VFont");
1197                 filelist->filelist[19].relname = BLI_strdup("Text");
1198                 filelist->filelist[20].relname = BLI_strdup("Armature");
1199                 filelist->filelist[21].relname = BLI_strdup("Action");
1200                 filelist->filelist[22].relname = BLI_strdup("NodeTree");
1201                 filelist->filelist[23].relname = BLI_strdup("Speaker");
1202 #ifdef WITH_FREESTYLE
1203                 filelist->filelist[24].relname = BLI_strdup("FreestyleLineStyle");
1204 #endif
1205                 filelist_sort(filelist, FILE_SORT_ALPHA);
1206         }
1207         else {
1208
1209                 /* make files */
1210                 idcode = groupname_to_code(filelist->dir);
1211                 
1212                 lb = which_libbase(G.main, idcode);
1213                 if (lb == NULL) return;
1214                 
1215                 id = lb->first;
1216                 filelist->numfiles = 0;
1217                 while (id) {
1218                         if (!filelist->hide_dot || id->name[2] != '.') {
1219                                 filelist->numfiles++;
1220                         }
1221                         
1222                         id = id->next;
1223                 }
1224                 
1225                 /* XXXXX TODO: if databrowse F4 or append/link filelist->hide_parent has to be set */
1226                 if (!filelist->hide_parent) filelist->numfiles += 1;
1227                 filelist->filelist = filelist->numfiles > 0 ? (struct direntry *)malloc(filelist->numfiles * sizeof(struct direntry)) : NULL;
1228
1229                 files = filelist->filelist;
1230                 
1231                 if (!filelist->hide_parent) {
1232                         memset(&(filelist->filelist[0]), 0, sizeof(struct direntry));
1233                         filelist->filelist[0].relname = BLI_strdup("..");
1234                         filelist->filelist[0].type |= S_IFDIR;
1235                 
1236                         files++;
1237                 }
1238                 
1239                 id = lb->first;
1240                 totlib = totbl = 0;
1241                 
1242                 while (id) {
1243                         ok = 1;
1244                         if (ok) {
1245                                 if (!filelist->hide_dot || id->name[2] != '.') {
1246                                         memset(files, 0, sizeof(struct direntry));
1247                                         if (id->lib == NULL) {
1248                                                 files->relname = BLI_strdup(id->name + 2);
1249                                         }
1250                                         else {
1251                                                 files->relname = MEM_mallocN(FILE_MAX + (MAX_ID_NAME - 2),     "filename for lib");
1252                                                 BLI_snprintf(files->relname, FILE_MAX + (MAX_ID_NAME - 2) + 3, "%s | %s", id->lib->name, id->name + 2);
1253                                         }
1254                                         files->type |= S_IFREG;
1255 #if 0               /* XXXXX TODO show the selection status of the objects */
1256                                         if (!filelist->has_func) { /* F4 DATA BROWSE */
1257                                                 if (idcode == ID_OB) {
1258                                                         if ( ((Object *)id)->flag & SELECT) files->selflag |= SELECTED_FILE;
1259                                                 }
1260                                                 else if (idcode == ID_SCE) {
1261                                                         if ( ((Scene *)id)->r.scemode & R_BG_RENDER) files->selflag |= SELECTED_FILE;
1262                                                 }
1263                                         }
1264 #endif
1265                                         files->nr = totbl + 1;
1266                                         files->poin = id;
1267                                         fake = id->flag & LIB_FAKEUSER;
1268                                         if (idcode == ID_MA || idcode == ID_TE || idcode == ID_LA || idcode == ID_WO || idcode == ID_IM) {
1269                                                 files->flags |= IMAGEFILE;
1270                                         }
1271                                         if      (id->lib && fake) BLI_snprintf(files->extra, sizeof(files->extra), "LF %d",    id->us);
1272                                         else if (id->lib)         BLI_snprintf(files->extra, sizeof(files->extra), "L    %d",  id->us);
1273                                         else if (fake)            BLI_snprintf(files->extra, sizeof(files->extra), "F    %d",  id->us);
1274                                         else                      BLI_snprintf(files->extra, sizeof(files->extra), "      %d", id->us);
1275                                         
1276                                         if (id->lib) {
1277                                                 if (totlib == 0) firstlib = files;
1278                                                 totlib++;
1279                                         }
1280                                         
1281                                         files++;
1282                                 }
1283                                 totbl++;
1284                         }
1285                         
1286                         id = id->next;
1287                 }
1288                 
1289                 /* only qsort of library blocks */
1290                 if (totlib > 1) {
1291                         qsort(firstlib, totlib, sizeof(struct direntry), compare_name);
1292                 }
1293         }
1294         filelist->filter = 0;
1295         filelist_filter(filelist);
1296 }
1297
1298 static void thumbnail_joblist_free(ThumbnailJob *tj)
1299 {
1300         FileImage *limg = tj->loadimages.first;
1301         
1302         /* free the images not yet copied to the filelist -> these will get freed with the filelist */
1303         for (; limg; limg = limg->next) {
1304                 if ((limg->img) && (!limg->done)) {
1305                         IMB_freeImBuf(limg->img);
1306                 }
1307         }
1308         BLI_freelistN(&tj->loadimages);
1309 }
1310
1311 static void thumbnails_startjob(void *tjv, short *stop, short *do_update, float *UNUSED(progress))
1312 {
1313         ThumbnailJob *tj = tjv;
1314         FileImage *limg = tj->loadimages.first;
1315
1316         tj->stop = stop;
1317         tj->do_update = do_update;
1318
1319         while ( (*stop == 0) && (limg) ) {
1320                 if (limg->flags & IMAGEFILE) {
1321                         limg->img = IMB_thumb_manage(limg->path, THB_NORMAL, THB_SOURCE_IMAGE);
1322                 }
1323                 else if (limg->flags & (BLENDERFILE | BLENDERFILE_BACKUP)) {
1324                         limg->img = IMB_thumb_manage(limg->path, THB_NORMAL, THB_SOURCE_BLEND);
1325                 }
1326                 else if (limg->flags & MOVIEFILE) {
1327                         limg->img = IMB_thumb_manage(limg->path, THB_NORMAL, THB_SOURCE_MOVIE);
1328                         if (!limg->img) {
1329                                 /* remember that file can't be loaded via IMB_open_anim */
1330                                 limg->flags &= ~MOVIEFILE;
1331                                 limg->flags |= MOVIEFILE_ICON;
1332                         }
1333                 }
1334                 *do_update = TRUE;
1335                 PIL_sleep_ms(10);
1336                 limg = limg->next;
1337         }
1338 }
1339
1340 static void thumbnails_update(void *tjv)
1341 {
1342         ThumbnailJob *tj = tjv;
1343
1344         if (tj->filelist && tj->filelist->filelist) {
1345                 FileImage *limg = tj->loadimages.first;
1346                 while (limg) {
1347                         if (!limg->done && limg->img) {
1348                                 tj->filelist->filelist[limg->index].image = limg->img;
1349                                 /* update flag for movie files where thumbnail can't be created */
1350                                 if (limg->flags & MOVIEFILE_ICON) {
1351                                         tj->filelist->filelist[limg->index].flags &= ~MOVIEFILE;
1352                                         tj->filelist->filelist[limg->index].flags |= MOVIEFILE_ICON;
1353                                 }
1354                                 limg->done = TRUE;
1355                         }
1356                         limg = limg->next;
1357                 }
1358         }
1359 }
1360
1361 static void thumbnails_free(void *tjv)
1362 {
1363         ThumbnailJob *tj = tjv;
1364         thumbnail_joblist_free(tj);
1365         MEM_freeN(tj);
1366 }
1367
1368
1369 void thumbnails_start(FileList *filelist, const bContext *C)
1370 {
1371         wmJob *wm_job;
1372         ThumbnailJob *tj;
1373         int idx;
1374         
1375         /* prepare job data */
1376         tj = MEM_callocN(sizeof(ThumbnailJob), "thumbnails\n");
1377         tj->filelist = filelist;
1378         for (idx = 0; idx < filelist->numfiles; idx++) {
1379                 if (!filelist->filelist[idx].image) {
1380                         if ( (filelist->filelist[idx].flags & (IMAGEFILE | MOVIEFILE | BLENDERFILE | BLENDERFILE_BACKUP)) ) {
1381                                 FileImage *limg = MEM_callocN(sizeof(FileImage), "loadimage");
1382                                 BLI_strncpy(limg->path, filelist->filelist[idx].path, FILE_MAX);
1383                                 limg->index = idx;
1384                                 limg->flags = filelist->filelist[idx].flags;
1385                                 BLI_addtail(&tj->loadimages, limg);
1386                         }
1387                 }
1388         }
1389
1390         BKE_reports_init(&tj->reports, RPT_PRINT);
1391
1392         /* setup job */
1393         wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), filelist, "Thumbnails",
1394                              0, WM_JOB_TYPE_FILESEL_THUMBNAIL);
1395         WM_jobs_customdata_set(wm_job, tj, thumbnails_free);
1396         WM_jobs_timer(wm_job, 0.5, NC_WINDOW, NC_WINDOW);
1397         WM_jobs_callbacks(wm_job, thumbnails_startjob, NULL, thumbnails_update, NULL);
1398
1399         /* start the job */
1400         WM_jobs_start(CTX_wm_manager(C), wm_job);
1401 }
1402
1403 void thumbnails_stop(wmWindowManager *wm, FileList *filelist)
1404 {
1405         WM_jobs_kill(wm, filelist, NULL);
1406 }
1407
1408 int thumbnails_running(wmWindowManager *wm, FileList *filelist)
1409 {
1410         return WM_jobs_test(wm, filelist, WM_JOB_TYPE_FILESEL_THUMBNAIL);
1411 }