micro-optimization, avoid checking is_power_of_2_i once in power_of_2_max_i
[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
768 static int file_extension_type(const char *relname)
769 {
770         if (BLO_has_bfile_extension(relname)) {
771                 return BLENDERFILE;
772         }
773         else if (file_is_blend_backup(relname)) {
774                 return BLENDERFILE_BACKUP;
775         }
776         else if (BLI_testextensie(relname, ".py")) {
777                 return PYSCRIPTFILE;
778         }
779         else if (BLI_testextensie(relname, ".txt")  ||
780                  BLI_testextensie(relname, ".glsl") ||
781                  BLI_testextensie(relname, ".osl")  ||
782                  BLI_testextensie(relname, ".data"))
783         {
784                 return TEXTFILE;
785         }
786         else if (BLI_testextensie(relname, ".ttf") ||
787                  BLI_testextensie(relname, ".ttc") ||
788                  BLI_testextensie(relname, ".pfb") ||
789                  BLI_testextensie(relname, ".otf") ||
790                  BLI_testextensie(relname, ".otc"))
791         {
792                 return FTFONTFILE;
793         }
794         else if (BLI_testextensie(relname, ".btx")) {
795                 return BTXFILE;
796         }
797         else if (BLI_testextensie(relname, ".dae")) {
798                 return COLLADAFILE;
799         }
800         else if (BLI_testextensie_array(relname, imb_ext_image) ||
801                  (G.have_quicktime && BLI_testextensie_array(relname, imb_ext_image_qt)))
802         {
803                 return IMAGEFILE;
804         }
805         else if (BLI_testextensie_array(relname, imb_ext_movie)) {
806                 return MOVIEFILE;
807         }
808         else if (BLI_testextensie_array(relname, imb_ext_audio)) {
809                 return SOUNDFILE;
810         }
811         return 0;
812 }
813
814 int ED_file_extension_icon(const char *relname)
815 {
816         int type = file_extension_type(relname);
817         
818         if (type == BLENDERFILE)
819                 return ICON_FILE_BLEND;
820         else if (type == BLENDERFILE_BACKUP)
821                 return ICON_FILE_BACKUP;
822         else if (type == IMAGEFILE)
823                 return ICON_FILE_IMAGE;
824         else if (type == MOVIEFILE)
825                 return ICON_FILE_MOVIE;
826         else if (type == PYSCRIPTFILE)
827                 return ICON_FILE_SCRIPT;
828         else if (type == SOUNDFILE)
829                 return ICON_FILE_SOUND;
830         else if (type == FTFONTFILE)
831                 return ICON_FILE_FONT;
832         else if (type == BTXFILE)
833                 return ICON_FILE_BLANK;
834         else if (type == COLLADAFILE)
835                 return ICON_FILE_BLANK;
836         else if (type == TEXTFILE)
837                 return ICON_FILE_TEXT;
838         
839         return ICON_FILE_BLANK;
840 }
841
842 static void filelist_setfiletypes(struct FileList *filelist)
843 {
844         struct direntry *file;
845         int num;
846         
847         file = filelist->filelist;
848         
849         for (num = 0; num < filelist->numfiles; num++, file++) {
850                 file->type = file->s.st_mode;  /* restore the mess below */
851                 
852                 /* Don't check extensions for directories */ 
853                 if (file->type & S_IFDIR) {
854                         continue;
855                 }
856                 file->flags = file_extension_type(file->relname);
857                 
858                 if (filelist->filter_glob[0] &&
859                     BLI_testextensie_glob(file->relname, filelist->filter_glob))
860                 {
861                         file->flags = OPERATORFILE;
862                 }
863                 
864         }
865 }
866
867 static void filelist_read_dir(struct FileList *filelist)
868 {
869         if (!filelist) return;
870
871         filelist->fidx = NULL;
872         filelist->filelist = NULL;
873
874         BLI_cleanup_dir(G.main->name, filelist->dir);
875         filelist->numfiles = BLI_dir_contents(filelist->dir, &(filelist->filelist));
876
877         filelist_setfiletypes(filelist);
878         filelist_filter(filelist);
879 }
880
881 static void filelist_read_main(struct FileList *filelist)
882 {
883         if (!filelist) return;
884         filelist_from_main(filelist);
885 }
886
887 static void filelist_read_library(struct FileList *filelist)
888 {
889         if (!filelist) return;
890         BLI_cleanup_dir(G.main->name, filelist->dir);
891         filelist_from_library(filelist);
892         if (!filelist->libfiledata) {
893                 int num;
894                 struct direntry *file;
895
896                 BLI_make_exist(filelist->dir);
897                 filelist_read_dir(filelist);
898                 file = filelist->filelist;
899                 for (num = 0; num < filelist->numfiles; num++, file++) {
900                         if (BLO_has_bfile_extension(file->relname)) {
901                                 char name[FILE_MAX];
902
903                                 BLI_join_dirfile(name, sizeof(name), filelist->dir, file->relname);
904
905                                 /* prevent current file being used as acceptable dir */
906                                 if (BLI_path_cmp(G.main->name, name) != 0) {
907                                         file->type &= ~S_IFMT;
908                                         file->type |= S_IFDIR;
909                                 }
910                         }
911                 }
912         }
913 }
914
915 void filelist_readdir(struct FileList *filelist)
916 {
917         filelist->readf(filelist);
918 }
919
920 int filelist_empty(struct FileList *filelist)
921 {       
922         return filelist->filelist == NULL;
923 }
924
925 void filelist_parent(struct FileList *filelist)
926 {
927         BLI_parent_dir(filelist->dir);
928         BLI_make_exist(filelist->dir);
929         filelist_readdir(filelist);
930 }
931
932 void filelist_select_file(struct FileList *filelist, int index, FileSelType select, unsigned int flag, FileCheckType check)
933 {
934         struct direntry *file = filelist_file(filelist, index);
935         if (file != NULL) {
936                 int check_ok = 0; 
937                 switch (check) {
938                         case CHECK_DIRS:
939                                 check_ok = S_ISDIR(file->type);
940                                 break;
941                         case CHECK_ALL:
942                                 check_ok = 1;
943                                 break;
944                         case CHECK_FILES:
945                         default:
946                                 check_ok = !S_ISDIR(file->type);
947                                 break;
948                 }
949                 if (check_ok) {
950                         switch (select) {
951                                 case FILE_SEL_REMOVE:
952                                         file->selflag &= ~flag;
953                                         break;
954                                 case FILE_SEL_ADD:
955                                         file->selflag |= flag;
956                                         break;
957                                 case FILE_SEL_TOGGLE:
958                                         file->selflag ^= flag;
959                                         break;
960                         }
961                 }
962         }
963 }
964
965 void filelist_select(struct FileList *filelist, FileSelection *sel, FileSelType select, unsigned int flag, FileCheckType check)
966 {
967         /* select all valid files between first and last indicated */
968         if ( (sel->first >= 0) && (sel->first < filelist->numfiltered) && (sel->last >= 0) && (sel->last < filelist->numfiltered) ) {
969                 int current_file;
970                 for (current_file = sel->first; current_file <= sel->last; current_file++) {
971                         filelist_select_file(filelist, current_file, select, flag, check);
972                 }
973         }
974 }
975
976 int filelist_is_selected(struct FileList *filelist, int index, FileCheckType check)
977 {
978         struct direntry *file = filelist_file(filelist, index);
979         if (!file) {
980                 return 0;
981         }
982         switch (check) {
983                 case CHECK_DIRS:
984                         return S_ISDIR(file->type) && (file->selflag & SELECTED_FILE);
985                 case CHECK_FILES:
986                         return S_ISREG(file->type) && (file->selflag & SELECTED_FILE);
987                 case CHECK_ALL:
988                 default:
989                         return (file->selflag & SELECTED_FILE);
990         }
991 }
992
993 void filelist_sort(struct FileList *filelist, short sort)
994 {
995         switch (sort) {
996                 case FILE_SORT_ALPHA:
997                         qsort(filelist->filelist, filelist->numfiles, sizeof(struct direntry), compare_name);
998                         break;
999                 case FILE_SORT_TIME:
1000                         qsort(filelist->filelist, filelist->numfiles, sizeof(struct direntry), compare_date);
1001                         break;
1002                 case FILE_SORT_SIZE:
1003                         qsort(filelist->filelist, filelist->numfiles, sizeof(struct direntry), compare_size);
1004                         break;
1005                 case FILE_SORT_EXTENSION:
1006                         qsort(filelist->filelist, filelist->numfiles, sizeof(struct direntry), compare_extension);
1007                         break;
1008         }
1009
1010         filelist_filter(filelist);
1011 }
1012
1013
1014 int filelist_islibrary(struct FileList *filelist, char *dir, char *group)
1015 {
1016         return BLO_is_a_library(filelist->dir, dir, group);
1017 }
1018
1019 static int groupname_to_code(const char *group)
1020 {
1021         char buf[32];
1022         char *lslash;
1023         
1024         BLI_strncpy(buf, group, sizeof(buf));
1025         lslash = (char *)BLI_last_slash(buf);
1026         if (lslash)
1027                 lslash[0] = '\0';
1028
1029         return BKE_idcode_from_name(buf);
1030 }
1031  
1032 void filelist_from_library(struct FileList *filelist)
1033 {
1034         LinkNode *l, *names, *previews;
1035         struct ImBuf *ima;
1036         int ok, i, nprevs, nnames, idcode;
1037         char filename[FILE_MAX];
1038         char dir[FILE_MAX], group[GROUP_MAX];
1039         
1040         /* name test */
1041         ok = filelist_islibrary(filelist, dir, group);
1042         if (!ok) {
1043                 /* free */
1044                 if (filelist->libfiledata) BLO_blendhandle_close(filelist->libfiledata);
1045                 filelist->libfiledata = NULL;
1046                 return;
1047         }
1048         
1049         BLI_strncpy(filename, G.main->name, sizeof(filename));
1050
1051         /* there we go */
1052         /* for the time being only read filedata when libfiledata==0 */
1053         if (filelist->libfiledata == NULL) {
1054                 filelist->libfiledata = BLO_blendhandle_from_file(dir, NULL);
1055                 if (filelist->libfiledata == NULL) return;
1056         }
1057         
1058         idcode = groupname_to_code(group);
1059
1060         /* memory for strings is passed into filelist[i].relname
1061          * and freed in freefilelist */
1062         if (idcode) {
1063                 previews = BLO_blendhandle_get_previews(filelist->libfiledata, idcode, &nprevs);
1064                 names = BLO_blendhandle_get_datablock_names(filelist->libfiledata, idcode, &nnames);
1065                 /* ugh, no rewind, need to reopen */
1066                 BLO_blendhandle_close(filelist->libfiledata);
1067                 filelist->libfiledata = BLO_blendhandle_from_file(dir, NULL);
1068                 
1069         }
1070         else {
1071                 previews = NULL;
1072                 nprevs = 0;
1073                 names = BLO_blendhandle_get_linkable_groups(filelist->libfiledata);
1074                 nnames = BLI_linklist_length(names);
1075         }
1076
1077         filelist->numfiles = nnames + 1;
1078         filelist->filelist = malloc(filelist->numfiles * sizeof(*filelist->filelist));
1079         memset(filelist->filelist, 0, filelist->numfiles * sizeof(*filelist->filelist));
1080
1081         filelist->filelist[0].relname = BLI_strdup("..");
1082         filelist->filelist[0].type |= S_IFDIR;
1083                 
1084         for (i = 0, l = names; i < nnames; i++, l = l->next) {
1085                 char *blockname = l->link;
1086
1087                 filelist->filelist[i + 1].relname = BLI_strdup(blockname);
1088                 if (idcode) {
1089                         filelist->filelist[i + 1].type |= S_IFREG;
1090                 }
1091                 else {
1092                         filelist->filelist[i + 1].type |= S_IFDIR;
1093                 }
1094         }
1095         
1096         if (previews && (nnames != nprevs)) {
1097                 printf("filelist_from_library: error, found %d items, %d previews\n", nnames, nprevs);
1098         }
1099         else if (previews) {
1100                 for (i = 0, l = previews; i < nnames; i++, l = l->next) {
1101                         PreviewImage *img = l->link;
1102                         
1103                         if (img) {
1104                                 unsigned int w = img->w[ICON_SIZE_PREVIEW];
1105                                 unsigned int h = img->h[ICON_SIZE_PREVIEW];
1106                                 unsigned int *rect = img->rect[ICON_SIZE_PREVIEW];
1107
1108                                 /* first allocate imbuf for copying preview into it */
1109                                 if (w > 0 && h > 0 && rect) {
1110                                         ima = IMB_allocImBuf(w, h, 32, IB_rect);
1111                                         memcpy(ima->rect, rect, w * h * sizeof(unsigned int));
1112                                         filelist->filelist[i + 1].image = ima;
1113                                         filelist->filelist[i + 1].flags = IMAGEFILE;
1114                                 }
1115                         }
1116                 }
1117         }
1118
1119         BLI_linklist_free(names, free);
1120         if (previews) BLI_linklist_free(previews, BKE_previewimg_freefunc);
1121
1122         filelist_sort(filelist, FILE_SORT_ALPHA);
1123
1124         BLI_strncpy(G.main->name, filename, sizeof(filename));  /* prevent G.main->name to change */
1125
1126         filelist->filter = 0;
1127         filelist_filter(filelist);
1128 }
1129
1130 void filelist_hideparent(struct FileList *filelist, short hide)
1131 {
1132         filelist->hide_parent = hide;
1133 }
1134
1135 void filelist_from_main(struct FileList *filelist)
1136 {
1137         ID *id;
1138         struct direntry *files, *firstlib = NULL;
1139         ListBase *lb;
1140         int a, fake, idcode, ok, totlib, totbl;
1141         
1142         // filelist->type = FILE_MAIN; // XXXXX TODO: add modes to filebrowser
1143
1144         if (filelist->dir[0] == '/') filelist->dir[0] = 0;
1145         
1146         if (filelist->dir[0]) {
1147                 idcode = groupname_to_code(filelist->dir);
1148                 if (idcode == 0) filelist->dir[0] = 0;
1149         }
1150         
1151         if (filelist->dir[0] == 0) {
1152                 
1153                 /* make directories */
1154 #ifdef WITH_FREESTYLE
1155                 filelist->numfiles = 25;
1156 #else
1157                 filelist->numfiles = 24;
1158 #endif
1159                 filelist->filelist = (struct direntry *)malloc(filelist->numfiles * sizeof(struct direntry));
1160                 
1161                 for (a = 0; a < filelist->numfiles; a++) {
1162                         memset(&(filelist->filelist[a]), 0, sizeof(struct direntry));
1163                         filelist->filelist[a].type |= S_IFDIR;
1164                 }
1165                 
1166                 filelist->filelist[0].relname = BLI_strdup("..");
1167                 filelist->filelist[2].relname = BLI_strdup("Scene");
1168                 filelist->filelist[3].relname = BLI_strdup("Object");
1169                 filelist->filelist[4].relname = BLI_strdup("Mesh");
1170                 filelist->filelist[5].relname = BLI_strdup("Curve");
1171                 filelist->filelist[6].relname = BLI_strdup("Metaball");
1172                 filelist->filelist[7].relname = BLI_strdup("Material");
1173                 filelist->filelist[8].relname = BLI_strdup("Texture");
1174                 filelist->filelist[9].relname = BLI_strdup("Image");
1175                 filelist->filelist[10].relname = BLI_strdup("Ika");
1176                 filelist->filelist[11].relname = BLI_strdup("Wave");
1177                 filelist->filelist[12].relname = BLI_strdup("Lattice");
1178                 filelist->filelist[13].relname = BLI_strdup("Lamp");
1179                 filelist->filelist[14].relname = BLI_strdup("Camera");
1180                 filelist->filelist[15].relname = BLI_strdup("Ipo");
1181                 filelist->filelist[16].relname = BLI_strdup("World");
1182                 filelist->filelist[17].relname = BLI_strdup("Screen");
1183                 filelist->filelist[18].relname = BLI_strdup("VFont");
1184                 filelist->filelist[19].relname = BLI_strdup("Text");
1185                 filelist->filelist[20].relname = BLI_strdup("Armature");
1186                 filelist->filelist[21].relname = BLI_strdup("Action");
1187                 filelist->filelist[22].relname = BLI_strdup("NodeTree");
1188                 filelist->filelist[23].relname = BLI_strdup("Speaker");
1189 #ifdef WITH_FREESTYLE
1190                 filelist->filelist[24].relname = BLI_strdup("FreestyleLineStyle");
1191 #endif
1192                 filelist_sort(filelist, FILE_SORT_ALPHA);
1193         }
1194         else {
1195
1196                 /* make files */
1197                 idcode = groupname_to_code(filelist->dir);
1198                 
1199                 lb = which_libbase(G.main, idcode);
1200                 if (lb == NULL) return;
1201                 
1202                 id = lb->first;
1203                 filelist->numfiles = 0;
1204                 while (id) {
1205                         if (!filelist->hide_dot || id->name[2] != '.') {
1206                                 filelist->numfiles++;
1207                         }
1208                         
1209                         id = id->next;
1210                 }
1211                 
1212                 /* XXXXX TODO: if databrowse F4 or append/link filelist->hide_parent has to be set */
1213                 if (!filelist->hide_parent) filelist->numfiles += 1;
1214                 filelist->filelist = filelist->numfiles > 0 ? (struct direntry *)malloc(filelist->numfiles * sizeof(struct direntry)) : NULL;
1215
1216                 files = filelist->filelist;
1217                 
1218                 if (!filelist->hide_parent) {
1219                         memset(&(filelist->filelist[0]), 0, sizeof(struct direntry));
1220                         filelist->filelist[0].relname = BLI_strdup("..");
1221                         filelist->filelist[0].type |= S_IFDIR;
1222                 
1223                         files++;
1224                 }
1225                 
1226                 id = lb->first;
1227                 totlib = totbl = 0;
1228                 
1229                 while (id) {
1230                         ok = 1;
1231                         if (ok) {
1232                                 if (!filelist->hide_dot || id->name[2] != '.') {
1233                                         memset(files, 0, sizeof(struct direntry));
1234                                         if (id->lib == NULL) {
1235                                                 files->relname = BLI_strdup(id->name + 2);
1236                                         }
1237                                         else {
1238                                                 files->relname = MEM_mallocN(FILE_MAX + (MAX_ID_NAME - 2),     "filename for lib");
1239                                                 BLI_snprintf(files->relname, FILE_MAX + (MAX_ID_NAME - 2) + 3, "%s | %s", id->lib->name, id->name + 2);
1240                                         }
1241                                         files->type |= S_IFREG;
1242 #if 0               /* XXXXX TODO show the selection status of the objects */
1243                                         if (!filelist->has_func) { /* F4 DATA BROWSE */
1244                                                 if (idcode == ID_OB) {
1245                                                         if ( ((Object *)id)->flag & SELECT) files->selflag |= SELECTED_FILE;
1246                                                 }
1247                                                 else if (idcode == ID_SCE) {
1248                                                         if ( ((Scene *)id)->r.scemode & R_BG_RENDER) files->selflag |= SELECTED_FILE;
1249                                                 }
1250                                         }
1251 #endif
1252                                         files->nr = totbl + 1;
1253                                         files->poin = id;
1254                                         fake = id->flag & LIB_FAKEUSER;
1255                                         if (idcode == ID_MA || idcode == ID_TE || idcode == ID_LA || idcode == ID_WO || idcode == ID_IM) {
1256                                                 files->flags |= IMAGEFILE;
1257                                         }
1258                                         if      (id->lib && fake) BLI_snprintf(files->extra, sizeof(files->extra), "LF %d",    id->us);
1259                                         else if (id->lib)         BLI_snprintf(files->extra, sizeof(files->extra), "L    %d",  id->us);
1260                                         else if (fake)            BLI_snprintf(files->extra, sizeof(files->extra), "F    %d",  id->us);
1261                                         else                      BLI_snprintf(files->extra, sizeof(files->extra), "      %d", id->us);
1262                                         
1263                                         if (id->lib) {
1264                                                 if (totlib == 0) firstlib = files;
1265                                                 totlib++;
1266                                         }
1267                                         
1268                                         files++;
1269                                 }
1270                                 totbl++;
1271                         }
1272                         
1273                         id = id->next;
1274                 }
1275                 
1276                 /* only qsort of library blocks */
1277                 if (totlib > 1) {
1278                         qsort(firstlib, totlib, sizeof(struct direntry), compare_name);
1279                 }
1280         }
1281         filelist->filter = 0;
1282         filelist_filter(filelist);
1283 }
1284
1285 static void thumbnail_joblist_free(ThumbnailJob *tj)
1286 {
1287         FileImage *limg = tj->loadimages.first;
1288         
1289         /* free the images not yet copied to the filelist -> these will get freed with the filelist */
1290         for (; limg; limg = limg->next) {
1291                 if ((limg->img) && (!limg->done)) {
1292                         IMB_freeImBuf(limg->img);
1293                 }
1294         }
1295         BLI_freelistN(&tj->loadimages);
1296 }
1297
1298 static void thumbnails_startjob(void *tjv, short *stop, short *do_update, float *UNUSED(progress))
1299 {
1300         ThumbnailJob *tj = tjv;
1301         FileImage *limg = tj->loadimages.first;
1302
1303         tj->stop = stop;
1304         tj->do_update = do_update;
1305
1306         while ( (*stop == 0) && (limg) ) {
1307                 if (limg->flags & IMAGEFILE) {
1308                         limg->img = IMB_thumb_manage(limg->path, THB_NORMAL, THB_SOURCE_IMAGE);
1309                 }
1310                 else if (limg->flags & (BLENDERFILE | BLENDERFILE_BACKUP)) {
1311                         limg->img = IMB_thumb_manage(limg->path, THB_NORMAL, THB_SOURCE_BLEND);
1312                 }
1313                 else if (limg->flags & MOVIEFILE) {
1314                         limg->img = IMB_thumb_manage(limg->path, THB_NORMAL, THB_SOURCE_MOVIE);
1315                         if (!limg->img) {
1316                                 /* remember that file can't be loaded via IMB_open_anim */
1317                                 limg->flags &= ~MOVIEFILE;
1318                                 limg->flags |= MOVIEFILE_ICON;
1319                         }
1320                 }
1321                 *do_update = TRUE;
1322                 PIL_sleep_ms(10);
1323                 limg = limg->next;
1324         }
1325 }
1326
1327 static void thumbnails_update(void *tjv)
1328 {
1329         ThumbnailJob *tj = tjv;
1330
1331         if (tj->filelist && tj->filelist->filelist) {
1332                 FileImage *limg = tj->loadimages.first;
1333                 while (limg) {
1334                         if (!limg->done && limg->img) {
1335                                 tj->filelist->filelist[limg->index].image = limg->img;
1336                                 /* update flag for movie files where thumbnail can't be created */
1337                                 if (limg->flags & MOVIEFILE_ICON) {
1338                                         tj->filelist->filelist[limg->index].flags &= ~MOVIEFILE;
1339                                         tj->filelist->filelist[limg->index].flags |= MOVIEFILE_ICON;
1340                                 }
1341                                 limg->done = TRUE;
1342                         }
1343                         limg = limg->next;
1344                 }
1345         }
1346 }
1347
1348 static void thumbnails_free(void *tjv)
1349 {
1350         ThumbnailJob *tj = tjv;
1351         thumbnail_joblist_free(tj);
1352         MEM_freeN(tj);
1353 }
1354
1355
1356 void thumbnails_start(FileList *filelist, const bContext *C)
1357 {
1358         wmJob *wm_job;
1359         ThumbnailJob *tj;
1360         int idx;
1361         
1362         /* prepare job data */
1363         tj = MEM_callocN(sizeof(ThumbnailJob), "thumbnails\n");
1364         tj->filelist = filelist;
1365         for (idx = 0; idx < filelist->numfiles; idx++) {
1366                 if (!filelist->filelist[idx].image) {
1367                         if ( (filelist->filelist[idx].flags & (IMAGEFILE | MOVIEFILE | BLENDERFILE | BLENDERFILE_BACKUP)) ) {
1368                                 FileImage *limg = MEM_callocN(sizeof(FileImage), "loadimage");
1369                                 BLI_strncpy(limg->path, filelist->filelist[idx].path, FILE_MAX);
1370                                 limg->index = idx;
1371                                 limg->flags = filelist->filelist[idx].flags;
1372                                 BLI_addtail(&tj->loadimages, limg);
1373                         }
1374                 }
1375         }
1376
1377         BKE_reports_init(&tj->reports, RPT_PRINT);
1378
1379         /* setup job */
1380         wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), filelist, "Thumbnails",
1381                              0, WM_JOB_TYPE_FILESEL_THUMBNAIL);
1382         WM_jobs_customdata_set(wm_job, tj, thumbnails_free);
1383         WM_jobs_timer(wm_job, 0.5, NC_WINDOW, NC_WINDOW);
1384         WM_jobs_callbacks(wm_job, thumbnails_startjob, NULL, thumbnails_update, NULL);
1385
1386         /* start the job */
1387         WM_jobs_start(CTX_wm_manager(C), wm_job);
1388 }
1389
1390 void thumbnails_stop(wmWindowManager *wm, FileList *filelist)
1391 {
1392         WM_jobs_kill(wm, filelist, NULL);
1393 }
1394
1395 int thumbnails_running(wmWindowManager *wm, FileList *filelist)
1396 {
1397         return WM_jobs_test(wm, filelist, WM_JOB_TYPE_FILESEL_THUMBNAIL);
1398 }