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