filelist.c - culled some compiler warnings.
[blender.git] / source / blender / src / 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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
31 /* global includes */
32
33 #include <stdlib.h>
34 #include <math.h>
35 #include <string.h>
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
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
53 #ifdef WIN32
54 #include "BLI_winstuff.h"
55 #endif
56
57 #include "BKE_utildefines.h"
58 #include "BKE_global.h"
59 #include "BIF_filelist.h"
60 #include "BKE_library.h"
61 #include "BKE_global.h"
62 #include "BKE_main.h"
63 #include "BLO_readfile.h"
64
65 #include "DNA_space_types.h"
66 #include "DNA_ipo_types.h"
67 #include "DNA_ID.h"
68 #include "DNA_object_types.h"
69 #include "DNA_lamp_types.h"
70 #include "DNA_material_types.h"
71 #include "DNA_texture_types.h"
72 #include "DNA_world_types.h"
73 #include "DNA_scene_types.h"
74 #include "DNA_userdef_types.h"
75
76 #include "IMB_imbuf.h"
77 #include "IMB_imbuf_types.h"
78 #include "IMB_thumbs.h"
79
80 #include "PIL_time.h"
81
82 #include "datatoc.h"
83
84 /* Elubie: VERY, really very ugly and evil! Remove asap!!! */
85 /* for state of file */
86 #define ACTIVE                          2
87
88 /* max length of library group name within filesel */
89 #define GROUP_MAX 32
90
91 typedef struct FileList
92 {
93         struct direntry *filelist;
94         int *fidx;
95
96         int numfiles;
97         int numfiltered;
98         char dir[FILE_MAXDIR];
99         short type;
100         short ipotype;
101         struct BlendHandle *libfiledata;
102         int has_func;
103         short prv_w;
104         short prv_h;
105         short hide_dot;
106         unsigned int filter;
107 } FileList;
108
109 int BIF_groupname_to_code(char *group)
110 {
111         char buf[32];
112         char *lslash;
113         
114         BLI_strncpy(buf, group, 31);
115         lslash= BLI_last_slash(buf);
116         if (lslash)
117                 lslash[0]= '\0';
118
119         return BLO_idcode_from_name(buf);
120 }
121
122
123 #define SPECIAL_IMG_SIZE 48
124 #define SPECIAL_IMG_ROWS 4
125 #define SPECIAL_IMG_COLS 4
126
127 #define SPECIAL_IMG_FOLDER 0
128 #define SPECIAL_IMG_PARENT 1
129 #define SPECIAL_IMG_REFRESH 2
130 #define SPECIAL_IMG_BLENDFILE 3
131 #define SPECIAL_IMG_SOUNDFILE 4
132 #define SPECIAL_IMG_MOVIEFILE 5
133 #define SPECIAL_IMG_PYTHONFILE 6
134 #define SPECIAL_IMG_TEXTFILE 7
135 #define SPECIAL_IMG_FONTFILE 8
136 #define SPECIAL_IMG_UNKNOWNFILE 9
137 #define SPECIAL_IMG_MAX SPECIAL_IMG_UNKNOWNFILE + 1
138
139 static ImBuf* gSpecialFileImages[SPECIAL_IMG_MAX];
140
141
142 /* ******************* SORT ******************* */
143
144 static int compare_name(const void *a1, const void *a2)
145 {
146         const struct direntry *entry1=a1, *entry2=a2;
147
148         /* type is is equal to stat.st_mode */
149
150         if (S_ISDIR(entry1->type)){
151                 if (S_ISDIR(entry2->type)==0) return (-1);
152         } else{
153                 if (S_ISDIR(entry2->type)) return (1);
154         }
155         if (S_ISREG(entry1->type)){
156                 if (S_ISREG(entry2->type)==0) return (-1);
157         } else{
158                 if (S_ISREG(entry2->type)) return (1);
159         }
160         if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
161         if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
162         
163         /* make sure "." and ".." are always first */
164         if( strcmp(entry1->relname, ".")==0 ) return (-1);
165         if( strcmp(entry2->relname, ".")==0 ) return (1);
166         if( strcmp(entry1->relname, "..")==0 ) return (-1);
167         
168         return (BLI_strcasecmp(entry1->relname,entry2->relname));
169 }
170
171 static int compare_date(const void *a1, const void *a2) 
172 {
173         const struct direntry *entry1=a1, *entry2=a2;
174         
175         /* type is equal to stat.st_mode */
176
177         if (S_ISDIR(entry1->type)){
178                 if (S_ISDIR(entry2->type)==0) return (-1);
179         } else{
180                 if (S_ISDIR(entry2->type)) return (1);
181         }
182         if (S_ISREG(entry1->type)){
183                 if (S_ISREG(entry2->type)==0) return (-1);
184         } else{
185                 if (S_ISREG(entry2->type)) return (1);
186         }
187         if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
188         if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
189
190         /* make sure "." and ".." are always first */
191         if( strcmp(entry1->relname, ".")==0 ) return (-1);
192         if( strcmp(entry2->relname, ".")==0 ) return (1);
193         if( strcmp(entry1->relname, "..")==0 ) return (-1);
194         
195         if ( entry1->s.st_mtime < entry2->s.st_mtime) return 1;
196         if ( entry1->s.st_mtime > entry2->s.st_mtime) return -1;
197         
198         else return BLI_strcasecmp(entry1->relname,entry2->relname);
199 }
200
201 static int compare_size(const void *a1, const void *a2) 
202 {
203         const struct direntry *entry1=a1, *entry2=a2;
204
205         /* type is equal to stat.st_mode */
206
207         if (S_ISDIR(entry1->type)){
208                 if (S_ISDIR(entry2->type)==0) return (-1);
209         } else{
210                 if (S_ISDIR(entry2->type)) return (1);
211         }
212         if (S_ISREG(entry1->type)){
213                 if (S_ISREG(entry2->type)==0) return (-1);
214         } else{
215                 if (S_ISREG(entry2->type)) return (1);
216         }
217         if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
218         if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
219
220         /* make sure "." and ".." are always first */
221         if( strcmp(entry1->relname, ".")==0 ) return (-1);
222         if( strcmp(entry2->relname, ".")==0 ) return (1);
223         if( strcmp(entry1->relname, "..")==0 ) return (-1);
224         
225         if ( entry1->s.st_size < entry2->s.st_size) return 1;
226         if ( entry1->s.st_size > entry2->s.st_size) return -1;
227         else return BLI_strcasecmp(entry1->relname,entry2->relname);
228 }
229
230 static int compare_extension(const void *a1, const void *a2) {
231         const struct direntry *entry1=a1, *entry2=a2;
232         char *sufix1, *sufix2;
233         char *nil="";
234
235         if (!(sufix1= strstr (entry1->relname, ".blend.gz"))) 
236                 sufix1= strrchr (entry1->relname, '.');
237         if (!(sufix2= strstr (entry2->relname, ".blend.gz")))
238                 sufix2= strrchr (entry2->relname, '.');
239         if (!sufix1) sufix1= nil;
240         if (!sufix2) sufix2= nil;
241
242         /* type is is equal to stat.st_mode */
243
244         if (S_ISDIR(entry1->type)){
245                 if (S_ISDIR(entry2->type)==0) return (-1);
246         } else{
247                 if (S_ISDIR(entry2->type)) return (1);
248         }
249         if (S_ISREG(entry1->type)){
250                 if (S_ISREG(entry2->type)==0) return (-1);
251         } else{
252                 if (S_ISREG(entry2->type)) return (1);
253         }
254         if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
255         if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
256         
257         /* make sure "." and ".." are always first */
258         if( strcmp(entry1->relname, ".")==0 ) return (-1);
259         if( strcmp(entry2->relname, ".")==0 ) return (1);
260         if( strcmp(entry1->relname, "..")==0 ) return (-1);
261         if( strcmp(entry2->relname, "..")==0 ) return (-1);
262         
263         return (BLI_strcasecmp(sufix1, sufix2));
264 }
265
266 void BIF_filelist_filter(FileList* filelist)
267 {
268         int num_filtered = 0;
269         int i, j;
270
271         if (!filelist->filelist)
272                 return;
273         
274         if (!filelist->filter) {
275                 if (filelist->fidx) {
276                         MEM_freeN(filelist->fidx);
277                         filelist->fidx = NULL;
278                 }
279                 filelist->fidx = (int *)MEM_callocN(filelist->numfiles*sizeof(int), "filteridx");
280                 for (i = 0; i < filelist->numfiles; ++i) {
281                         filelist->fidx[i] = i;
282                 }
283                 filelist->numfiltered = filelist->numfiles;
284                 return;
285         }
286
287         // How many files are left after filter ?
288         for (i = 0; i < filelist->numfiles; ++i) {
289                 if (filelist->filelist[i].flags & filelist->filter) {
290                         num_filtered++;
291                 } 
292                 else if (filelist->filelist[i].type & S_IFDIR) {
293                         if (filelist->filter & FOLDERFILE) {
294                                 num_filtered++;
295                         }
296                 }               
297         }
298         
299         if (filelist->fidx) {
300                         MEM_freeN(filelist->fidx);
301                         filelist->fidx = NULL;
302         }
303         filelist->fidx = (int *)MEM_callocN(num_filtered*sizeof(int), "filteridx");
304         filelist->numfiltered = num_filtered;
305
306         for (i = 0, j=0; i < filelist->numfiles; ++i) {
307                 if (filelist->filelist[i].flags & filelist->filter) {
308                         filelist->fidx[j++] = i;
309                 }
310                 else if (filelist->filelist[i].type & S_IFDIR) {
311                         if (filelist->filter & FOLDERFILE) {
312                                 filelist->fidx[j++] = i;
313                         }
314                 }  
315         }
316 }
317
318 void BIF_filelist_init_icons()
319 {
320         short x, y, k;
321         ImBuf *bbuf;
322         ImBuf *ibuf;
323         bbuf = IMB_ibImageFromMemory((int *)datatoc_prvicons, datatoc_prvicons_size, IB_rect);
324         if (bbuf) {
325                 for (y=0; y<SPECIAL_IMG_ROWS; y++) {
326                         for (x=0; x<SPECIAL_IMG_COLS; x++) {
327                                 int tile = SPECIAL_IMG_COLS*y + x; 
328                                 if (tile < SPECIAL_IMG_MAX) {
329                                         ibuf = IMB_allocImBuf(SPECIAL_IMG_SIZE, SPECIAL_IMG_SIZE, 32, IB_rect, 0);
330                                         for (k=0; k<SPECIAL_IMG_SIZE; k++) {
331                                                 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));
332                                         }
333                                         gSpecialFileImages[tile] = ibuf;
334                                 }
335                         }
336                 }
337                 IMB_freeImBuf(bbuf);
338         }
339
340 }
341
342 void BIF_filelist_free_icons()
343 {
344         int i;
345         for (i=0; i < SPECIAL_IMG_MAX; ++i) {
346                 IMB_freeImBuf(gSpecialFileImages[i]);
347                 gSpecialFileImages[i] = NULL;
348         }
349 }
350
351 struct FileList*        BIF_filelist_new()
352 {
353         FileList* p = MEM_callocN( sizeof(FileList), "filelist" );
354         p->filelist = 0;
355         p->numfiles = 0;
356         p->dir[0] = '\0';
357         p->libfiledata = 0;
358         p->type = 0;
359         p->has_func = 0;
360         p->filter = 0;
361         return p;
362 }
363
364 struct FileList*        BIF_filelist_copy(struct FileList* filelist)
365 {
366         FileList* p = BIF_filelist_new();
367         BLI_strncpy(p->dir, filelist->dir, FILE_MAXDIR);
368         p->filelist = NULL;
369         p->fidx = NULL;
370         p->type = filelist->type;
371         p->ipotype = filelist->ipotype;
372         p->has_func = filelist->has_func;
373
374         return p;
375 }
376
377 void BIF_filelist_free(struct FileList* filelist)
378 {
379         int i;
380
381         if (!filelist) {
382                 printf("Attemtping to delete empty filelist.\n");
383                 return;
384         }
385
386         if (filelist->fidx) {
387                 MEM_freeN(filelist->fidx);
388                 filelist->fidx = NULL;
389         }
390
391         for (i = 0; i < filelist->numfiles; ++i) {
392                 if (filelist->filelist[i].image) {                      
393                         IMB_freeImBuf(filelist->filelist[i].image);
394                 }
395                 filelist->filelist[i].image = 0;
396                 if (filelist->filelist[i].relname)
397                         MEM_freeN(filelist->filelist[i].relname);
398                 filelist->filelist[i].relname = 0;
399                 if (filelist->filelist[i].string)
400                         MEM_freeN(filelist->filelist[i].string);
401                 filelist->filelist[i].string = 0;
402         }
403         
404         filelist->numfiles = 0;
405         free(filelist->filelist);
406         filelist->filelist = 0; 
407         filelist->filter = 0;
408         filelist->numfiltered =0;
409 }
410
411 void BIF_filelist_freelib(struct FileList* filelist)
412 {
413         if(filelist->libfiledata)       
414                 BLO_blendhandle_close(filelist->libfiledata);
415         filelist->libfiledata= 0;
416 }
417
418 struct BlendHandle *BIF_filelist_lib(struct FileList* filelist)
419 {
420         return filelist->libfiledata;
421 }
422
423 int     BIF_filelist_numfiles(struct FileList* filelist)
424 {
425         return filelist->numfiltered;
426 }
427
428 const char * BIF_filelist_dir(struct FileList* filelist)
429 {
430         return filelist->dir;
431 }
432
433 void BIF_filelist_setdir(struct FileList* filelist, const char *dir)
434 {
435         BLI_strncpy(filelist->dir, dir, FILE_MAXDIR);
436 }
437
438 void BIF_filelist_appenddir(struct FileList* filelist, const char *relname)
439 {
440         strcat(filelist->dir, relname); 
441         BLI_cleanup_dir(G.sce, filelist->dir);
442 }
443
444 void BIF_filelist_imgsize(struct FileList* filelist, short w, short h)
445 {
446         filelist->prv_w = w;
447         filelist->prv_h = h;
448 }
449
450 void BIF_filelist_loadimage(struct FileList* filelist, int index)
451 {
452         ImBuf *imb = NULL;
453         int imgwidth = filelist->prv_w;
454         int imgheight = filelist->prv_h;
455         short ex, ey, dx, dy;
456         float scaledx, scaledy;
457         int fidx = 0;
458         
459         if ( (index < 0) || (index >= filelist->numfiltered) ) {
460                 return;
461         }
462         fidx = filelist->fidx[index];
463
464         if (!filelist->filelist[fidx].image)
465         {
466                 if (filelist->type != FILE_MAIN)
467                 {
468                         if ( filelist->filelist[fidx].flags & IMAGEFILE ) {                             
469                                 imb = IMB_thumb_manage(filelist->dir, filelist->filelist[fidx].relname, THB_NORMAL, THB_SOURCE_IMAGE);
470                         } else if ( filelist->filelist[fidx].flags & MOVIEFILE ) {                              
471                                 imb = IMB_thumb_manage(filelist->dir, filelist->filelist[fidx].relname, THB_NORMAL, THB_SOURCE_MOVIE);
472                                 if (!imb) {
473                                         /* remember that file can't be loaded via IMB_open_anim */
474                                         filelist->filelist[fidx].flags &= ~MOVIEFILE;
475                                         filelist->filelist[fidx].flags |= MOVIEFILE_ICON;
476                                 }
477                         }
478                         if (imb) {
479                                 if (imb->x > imb->y) {
480                                         scaledx = (float)imgwidth;
481                                         scaledy =  ( (float)imb->y/(float)imb->x )*imgwidth;
482                                 }
483                                 else {
484                                         scaledy = imgheight;
485                                         scaledx =  ( (float)imb->x/(float)imb->y )*imgheight;
486                                 }
487                                 ex = (short)scaledx;
488                                 ey = (short)scaledy;
489                                 
490                                 dx = imgwidth - ex;
491                                 dy = imgheight - ey;
492                                 
493                                 IMB_scaleImBuf(imb, ex, ey);
494
495                         } 
496                         filelist->filelist[fidx].image = imb;
497                         
498                 }
499         }
500 }
501
502 struct ImBuf * BIF_filelist_getimage(struct FileList* filelist, int index)
503 {
504         ImBuf* ibuf = NULL;
505         int fidx = 0;   
506         if ( (index < 0) || (index >= filelist->numfiltered) ) {
507                 return NULL;
508         }
509         fidx = filelist->fidx[index];
510         ibuf = filelist->filelist[fidx].image;
511
512         if (ibuf == NULL) {
513                 struct direntry *file = &filelist->filelist[fidx];
514                 if (file->type & S_IFDIR) {
515                         if ( strcmp(filelist->filelist[fidx].relname, "..") == 0) {
516                                 ibuf = gSpecialFileImages[SPECIAL_IMG_PARENT];
517                         } else if  ( strcmp(filelist->filelist[fidx].relname, ".") == 0) {
518                                 ibuf = gSpecialFileImages[SPECIAL_IMG_REFRESH];
519                         } else {
520                                 ibuf = gSpecialFileImages[SPECIAL_IMG_FOLDER];
521                         }
522                 } else {
523                         ibuf = gSpecialFileImages[SPECIAL_IMG_UNKNOWNFILE];
524                 }
525
526                 if (file->flags & BLENDERFILE) {
527                         ibuf = gSpecialFileImages[SPECIAL_IMG_BLENDFILE];
528                 } else if ( (file->flags & MOVIEFILE) || (file->flags & MOVIEFILE_ICON) ) {
529                         ibuf = gSpecialFileImages[SPECIAL_IMG_MOVIEFILE];
530                 } else if (file->flags & SOUNDFILE) {
531                         ibuf = gSpecialFileImages[SPECIAL_IMG_SOUNDFILE];
532                 } else if (file->flags & PYSCRIPTFILE) {
533                         ibuf = gSpecialFileImages[SPECIAL_IMG_PYTHONFILE];
534                 } else if (file->flags & FTFONTFILE) {
535                         ibuf = gSpecialFileImages[SPECIAL_IMG_FONTFILE];
536                 } else if (file->flags & TEXTFILE) {
537                         ibuf = gSpecialFileImages[SPECIAL_IMG_TEXTFILE];
538                 } 
539         }
540         return ibuf;
541 }
542
543 struct direntry * BIF_filelist_file(struct FileList* filelist, int index)
544 {
545         int fidx = 0;
546         
547         if ( (index < 0) || (index >= filelist->numfiltered) ) {
548                 return NULL;
549         }
550         fidx = filelist->fidx[index];
551
552         return &filelist->filelist[fidx];
553 }
554
555 int BIF_filelist_find(struct FileList* filelist, char *file)
556 {
557         int index = -1;
558         int i;
559         int fidx = -1;
560         
561         if (!filelist->fidx) 
562                 return fidx;
563
564         
565         for (i = 0; i < filelist->numfiles; ++i) {
566                 if ( strcmp(filelist->filelist[i].relname, file) == 0) {
567                         index = i;
568                         break;
569                 }
570         }
571
572         for (i = 0; i < filelist->numfiltered; ++i) {
573                 if (filelist->fidx[i] == index) {
574                         fidx = i;
575                         break;
576                 }
577         }
578         return fidx;
579 }
580
581 void BIF_filelist_hidedot(struct FileList* filelist, short hide)
582 {
583         filelist->hide_dot = hide;
584 }
585
586 void BIF_filelist_setfilter(struct FileList* filelist, unsigned int filter)
587 {
588         filelist->filter = filter;
589 }
590
591 void BIF_filelist_readdir(struct FileList* filelist)
592 {
593         char wdir[FILE_MAXDIR];
594         int finished = 0;
595
596         if (!filelist) return;
597         filelist->fidx = 0;
598         filelist->filelist = 0;
599
600         if(filelist->type==FILE_MAIN) {
601                 BIF_filelist_from_main(filelist);
602                 finished = 1;
603         } else if(filelist->type==FILE_LOADLIB) {
604                 BLI_cleanup_dir(G.sce, filelist->dir);
605                 BIF_filelist_from_library(filelist);
606                 if(filelist->libfiledata) {
607                         finished = 1;
608                 }
609         }
610
611         if (!finished) {
612                 BLI_getwdN(wdir);        
613                 
614                 BLI_cleanup_dir(G.sce, filelist->dir);
615                 BLI_hide_dot_files(filelist->hide_dot);
616                 filelist->numfiles = BLI_getdir(filelist->dir, &(filelist->filelist));
617
618                 chdir(wdir);
619                 BIF_filelist_setfiletypes(filelist, G.have_quicktime);
620                 BIF_filelist_filter(filelist);
621
622         }
623 }
624
625 int BIF_filelist_empty(struct FileList* filelist)
626 {       
627         return filelist->filelist == 0;
628 }
629
630 void BIF_filelist_parent(struct FileList* filelist)
631 {
632 #ifdef WIN32
633         char c = '\\';
634 #else
635         char c = '/';
636 #endif
637         char *dir = filelist->dir;
638         size_t len = strlen(dir);
639         
640         while( (len > 0) && (dir[len-1] == c)  )
641         {
642                 --len;
643                 dir[len] = '\0';
644         }
645         while ( (len > 0) && (dir[len-1] != c) )
646         {
647                 --len;
648                 dir[len] = '\0';
649         }
650         if (len == 0)
651         {
652                 dir[0] = c; dir[1] = '\0';
653         }
654 #ifdef WIN32
655         strcat(filelist->dir, "\\");
656 #else
657         strcat(filelist->dir, "/");
658 #endif
659         
660         BLI_cleanup_dir(G.sce, filelist->dir);
661         BLI_make_exist(filelist->dir);
662         BIF_filelist_readdir(filelist);
663 }
664
665 void BIF_filelist_setfiletypes(struct FileList* filelist, short has_quicktime)
666 {
667         struct direntry *file;
668         int num;
669
670         file= filelist->filelist;
671
672         for(num=0; num<filelist->numfiles; num++, file++) {
673                 file->flags= 0;
674                 file->type= file->s.st_mode;    /* restore the mess below */ 
675
676                         /* Don't check extensions for directories */ 
677                 if (file->type & S_IFDIR)
678                         continue;
679                                 
680                 
681                 
682                 if(BLO_has_bfile_extension(file->relname)) {
683                         file->flags |= BLENDERFILE;
684                         if(filelist->type==FILE_LOADLIB) {              
685                                 char name[FILE_MAXDIR+FILE_MAXFILE];
686                                 BLI_strncpy(name, filelist->dir, sizeof(name));
687                                 strcat(name, file->relname);
688                                 
689                                 /* prevent current file being used as acceptable dir */
690                                 if (BLI_streq(G.main->name, name)==0) {
691                                         file->type &= ~S_IFMT;
692                                         file->type |= S_IFDIR;
693                                 }
694                         }
695                 } else if(BLI_testextensie(file->relname, ".py")) {
696                                 file->flags |= PYSCRIPTFILE;
697                 } else if(BLI_testextensie(file->relname, ".txt")) {
698                                 file->flags |= TEXTFILE;
699                 } else if( BLI_testextensie(file->relname, ".ttf")
700                                         || BLI_testextensie(file->relname, ".ttc")
701                                         || BLI_testextensie(file->relname, ".pfb")
702                                         || BLI_testextensie(file->relname, ".otf")
703                                         || BLI_testextensie(file->relname, ".otc")) {
704                                 file->flags |= FTFONTFILE;                      
705                 } else if (has_quicktime){
706                         if(             BLI_testextensie(file->relname, ".jpg")
707                                 ||      BLI_testextensie(file->relname, ".jpeg")
708                                 ||      BLI_testextensie(file->relname, ".tga")
709                                 ||      BLI_testextensie(file->relname, ".rgb")
710                                 ||      BLI_testextensie(file->relname, ".bmp")
711                                 ||      BLI_testextensie(file->relname, ".png")
712                                 ||      BLI_testextensie(file->relname, ".iff")
713                                 ||      BLI_testextensie(file->relname, ".lbm")
714                                 ||      BLI_testextensie(file->relname, ".gif")
715                                 ||      BLI_testextensie(file->relname, ".psd")
716                                 ||      BLI_testextensie(file->relname, ".tif")
717                                 ||      BLI_testextensie(file->relname, ".tiff")
718                                 ||      BLI_testextensie(file->relname, ".pct")
719                                 ||      BLI_testextensie(file->relname, ".pict")
720                                 ||      BLI_testextensie(file->relname, ".pntg") //macpaint
721                                 ||      BLI_testextensie(file->relname, ".qtif")
722                                 ||      BLI_testextensie(file->relname, ".sgi")
723 #ifdef WITH_OPENEXR
724                                 ||      BLI_testextensie(file->relname, ".exr")
725 #endif
726                             ) {
727                                 file->flags |= IMAGEFILE;                       
728                         }
729                         else if(BLI_testextensie(file->relname, ".avi")
730                                 ||      BLI_testextensie(file->relname, ".flc")
731                                 ||      BLI_testextensie(file->relname, ".mov")
732                                 ||      BLI_testextensie(file->relname, ".movie")
733                                 ||      BLI_testextensie(file->relname, ".mp4")
734                                 ||      BLI_testextensie(file->relname, ".m4v")
735                                 ||      BLI_testextensie(file->relname, ".mv")) {
736                                 file->flags |= MOVIEFILE;                       
737                         }
738                         else if(BLI_testextensie(file->relname, ".wav")) {
739                                 file->flags |= SOUNDFILE;
740                         }
741                 } else { // no quicktime
742                         if(BLI_testextensie(file->relname, ".jpg")
743                                 ||      BLI_testextensie(file->relname, ".tga")
744                                 ||      BLI_testextensie(file->relname, ".rgb")
745                                 ||      BLI_testextensie(file->relname, ".bmp")
746                                 ||      BLI_testextensie(file->relname, ".png")
747                                 ||      BLI_testextensie(file->relname, ".iff")
748                                 ||      BLI_testextensie(file->relname, ".tif")
749                                 ||      BLI_testextensie(file->relname, ".tiff")
750 #ifdef WITH_OPENEXR
751                                 ||      BLI_testextensie(file->relname, ".exr")
752 #endif
753                                 ||      BLI_testextensie(file->relname, ".lbm")
754                                 ||      BLI_testextensie(file->relname, ".sgi")) {
755                                 file->flags |= IMAGEFILE;                       
756                         }
757                         else if(BLI_testextensie(file->relname, ".avi")
758                                 ||      BLI_testextensie(file->relname, ".mp4")
759                                 ||      BLI_testextensie(file->relname, ".mv")) {
760                                 file->flags |= MOVIEFILE;                       
761                         }
762                         else if(BLI_testextensie(file->relname, ".wav")) {
763                                 file->flags |= SOUNDFILE;
764                         }
765                 }
766         }
767 }
768
769 void BIF_filelist_swapselect(struct FileList* filelist)
770 {
771         struct direntry *file;
772         int num, act= 0;
773         
774         file= filelist->filelist;
775         for(num=0; num<filelist->numfiles; num++, file++) {
776                 if(file->flags & ACTIVE) {
777                         act= 1;
778                         break;
779                 }
780         }
781         file= filelist->filelist+2;
782         for(num=2; num<filelist->numfiles; num++, file++) {
783                 if(act) file->flags &= ~ACTIVE;
784                 else file->flags |= ACTIVE;
785         }
786 }
787
788 int BIF_filelist_islibrary(struct FileList* filelist, char* dir, char* group)
789 {
790          /* return ok when a blenderfile, in dir is the filename,
791          * in group the type of libdata
792          */
793         int len;
794         char *fd;
795         
796         strcpy(dir, filelist->dir);
797         len= strlen(dir);
798         if(len<7) return 0;
799         if( dir[len-1] != '/' && dir[len-1] != '\\') return 0;
800                 
801         group[0]= 0;
802         dir[len-1]= 0;
803
804         /* Find the last slash */
805         fd= (strrchr(dir, '/')>strrchr(dir, '\\'))?strrchr(dir, '/'):strrchr(dir, '\\');
806
807         if(fd==0) return 0;
808         *fd= 0;
809         if(BLO_has_bfile_extension(fd+1)) {
810                 *fd= '/';
811         }
812         else {
813                 char *gp = fd+1; // in case we have a .blend file, gp points to the group
814
815                 /* Find the last slash */
816                 fd= (strrchr(dir, '/')>strrchr(dir, '\\'))?strrchr(dir, '/'):strrchr(dir, '\\');
817                 if (!fd || !BLO_has_bfile_extension(fd+1)) return 0;
818
819                 /* now we know that we are in a blend file and it is safe to 
820                    assume that gp actually points to a group */
821                 BLI_strncpy(group, gp, GROUP_MAX);
822         }
823         return 1;
824 }
825
826 void BIF_filelist_from_library(struct FileList* filelist)
827 {
828         LinkNode *l, *names, *previews;
829         struct ImBuf* ima;
830         int ok, i, nnames, idcode;
831         char filename[FILE_MAXDIR+FILE_MAXFILE];
832         char dir[FILE_MAXDIR], group[GROUP_MAX];        
833         
834         filelist->type = FILE_LOADLIB;
835
836         /* name test */
837         ok= BIF_filelist_islibrary(filelist, dir, group);
838         if (!ok) {
839                 /* free */
840                 if(filelist->libfiledata) BLO_blendhandle_close(filelist->libfiledata);
841                 filelist->libfiledata= 0;
842                 return;
843         }
844         
845         BLI_strncpy(filename, G.sce, sizeof(filename)); // G.sce = last file loaded, for UI
846
847         /* there we go */
848         /* for the time being only read filedata when libfiledata==0 */
849         if (filelist->libfiledata==0) {
850                 filelist->libfiledata= BLO_blendhandle_from_file(dir);
851                 if(filelist->libfiledata==0) return;
852         }
853         
854         idcode= BIF_groupname_to_code(group);
855
856                 // memory for strings is passed into filelist[i].relname
857                 // and free'd in freefilelist
858         previews = NULL;
859         if (idcode) {
860                 previews= BLO_blendhandle_get_previews(filelist->libfiledata, idcode);
861                 names= BLO_blendhandle_get_datablock_names(filelist->libfiledata, idcode);
862                 /* ugh, no rewind, need to reopen */
863                 BLO_blendhandle_close(filelist->libfiledata);
864                 filelist->libfiledata= BLO_blendhandle_from_file(dir);
865                 
866         } else {
867                 names= BLO_blendhandle_get_linkable_groups(filelist->libfiledata);
868         }
869         
870         nnames= BLI_linklist_length(names);
871
872         filelist->numfiles= nnames + 2;
873         filelist->filelist= malloc(filelist->numfiles * sizeof(*filelist->filelist));
874         memset(filelist->filelist, 0, filelist->numfiles * sizeof(*filelist->filelist));
875
876         filelist->filelist[0].relname= BLI_strdup(".");
877         filelist->filelist[0].type |= S_IFDIR;
878         filelist->filelist[1].relname= BLI_strdup("..");
879         filelist->filelist[1].type |= S_IFDIR;
880                 
881         for (i=0, l= names; i<nnames; i++, l= l->next) {
882                 char *blockname= l->link;
883
884                 filelist->filelist[i + 2].relname= BLI_strdup(blockname);
885                 if (!idcode)
886                         filelist->filelist[i + 2].type |= S_IFDIR;
887         }
888         
889         if(previews) {
890                 for (i=0, l= previews; i<nnames; i++, l= l->next) {
891                         PreviewImage *img= l->link;
892                         
893                         if (img) {
894                                 unsigned int w = img->w[PREVIEW_MIPMAP_LARGE];
895                                 unsigned int h = img->h[PREVIEW_MIPMAP_LARGE];
896                                 unsigned int *rect = img->rect[PREVIEW_MIPMAP_LARGE];
897
898                                 /* first allocate imbuf for copying preview into it */
899                                 if (w > 0 && h > 0 && rect) {
900                                         ima = IMB_allocImBuf(w, h, 32, IB_rect, 0);
901                                         memcpy(ima->rect, rect, w*h*sizeof(unsigned int));
902                                         filelist->filelist[i + 2].image = ima;
903                                         filelist->filelist[i + 2].flags = IMAGEFILE;
904                                 }
905                         }
906                 }
907         }
908
909         BLI_linklist_free(names, free);
910         if (previews) BLI_linklist_free(previews, (void(*)(void*)) MEM_freeN);
911
912         BIF_filelist_sort(filelist, FILE_SORTALPHA);
913
914         BLI_strncpy(G.sce, filename, sizeof(filename)); // prevent G.sce to change
915 }
916
917 void BIF_filelist_append_library(struct FileList *filelist, char *dir, char *file, short flag, int idcode)
918 {
919         BLO_library_append_(&filelist->libfiledata, filelist->filelist, filelist->numfiles, dir, file, flag, idcode);
920 }
921
922 void BIF_filelist_from_main(struct FileList *filelist)
923 {
924         ID *id;
925         struct direntry *files, *firstlib = NULL;
926         ListBase *lb;
927         int a, fake, idcode, ok, totlib, totbl;
928         
929         filelist->type = FILE_MAIN;
930
931         if(filelist->dir[0]=='/') filelist->dir[0]= 0;
932         
933         if(filelist->dir[0]) {
934                 idcode= BIF_groupname_to_code(filelist->dir);
935                 if(idcode==0) filelist->dir[0]= 0;
936         }
937         
938         if( filelist->dir[0]==0) {
939                 
940                 /* make directories */
941                 filelist->numfiles= 23;
942                 filelist->filelist= (struct direntry *)malloc(filelist->numfiles * sizeof(struct direntry));
943                 
944                 for(a=0; a<filelist->numfiles; a++) {
945                         memset( &(filelist->filelist[a]), 0 , sizeof(struct direntry));
946                         filelist->filelist[a].type |= S_IFDIR;
947                 }
948                 
949                 filelist->filelist[0].relname= BLI_strdup("..");
950                 filelist->filelist[1].relname= BLI_strdup(".");
951                 filelist->filelist[2].relname= BLI_strdup("Scene");
952                 filelist->filelist[3].relname= BLI_strdup("Object");
953                 filelist->filelist[4].relname= BLI_strdup("Mesh");
954                 filelist->filelist[5].relname= BLI_strdup("Curve");
955                 filelist->filelist[6].relname= BLI_strdup("Metaball");
956                 filelist->filelist[7].relname= BLI_strdup("Material");
957                 filelist->filelist[8].relname= BLI_strdup("Texture");
958                 filelist->filelist[9].relname= BLI_strdup("Image");
959                 filelist->filelist[10].relname= BLI_strdup("Ika");
960                 filelist->filelist[11].relname= BLI_strdup("Wave");
961                 filelist->filelist[12].relname= BLI_strdup("Lattice");
962                 filelist->filelist[13].relname= BLI_strdup("Lamp");
963                 filelist->filelist[14].relname= BLI_strdup("Camera");
964                 filelist->filelist[15].relname= BLI_strdup("Ipo");
965                 filelist->filelist[16].relname= BLI_strdup("World");
966                 filelist->filelist[17].relname= BLI_strdup("Screen");
967                 filelist->filelist[18].relname= BLI_strdup("VFont");
968                 filelist->filelist[19].relname= BLI_strdup("Text");
969                 filelist->filelist[20].relname= BLI_strdup("Armature");
970                 filelist->filelist[21].relname= BLI_strdup("Action");
971                 filelist->filelist[22].relname= BLI_strdup("NodeTree");
972                 BIF_filelist_sort(filelist, FILE_SORTALPHA);
973         }
974         else {
975
976                 /* make files */
977                 idcode= BIF_groupname_to_code(filelist->dir);
978                 
979                 lb= wich_libbase(G.main, idcode );
980                 if(lb==0) return;
981                 
982                 id= lb->first;
983                 filelist->numfiles= 0;
984                 while(id) {
985                         
986                         if(filelist->has_func && idcode==ID_IP) {
987                                 if(filelist->ipotype== ((Ipo *)id)->blocktype) filelist->numfiles++;
988                         }
989                         else if (!filelist->hide_dot || id->name[2] != '.') {
990                                 filelist->numfiles++;
991                         }
992                         
993                         id= id->next;
994                 }
995                 
996                 if(!filelist->has_func) filelist->numfiles+= 2;
997                 filelist->filelist= (struct direntry *)malloc(filelist->numfiles * sizeof(struct direntry));
998                 
999                 files = filelist->filelist;
1000                 
1001                 if(!filelist->has_func) {
1002                         memset( &(filelist->filelist[0]), 0 , sizeof(struct direntry));
1003                         filelist->filelist[0].relname= BLI_strdup(".");
1004                         filelist->filelist[0].type |= S_IFDIR;
1005                         memset( &(filelist->filelist[1]), 0 , sizeof(struct direntry));
1006                         filelist->filelist[1].relname= BLI_strdup("..");
1007                         filelist->filelist[1].type |= S_IFDIR;
1008                 
1009                         files+= 2;
1010                 }
1011                 
1012                 id= lb->first;
1013                 totlib= totbl= 0;
1014                 
1015                 while(id) {
1016                         
1017                         ok= 0;
1018                         if(filelist->has_func && idcode==ID_IP) {
1019                                 if(filelist->ipotype== ((Ipo *)id)->blocktype) ok= 1;
1020                         }
1021                         else ok= 1;
1022                         
1023                         if(ok) {
1024                                 /* TODO: hide dot files - elubie */
1025                                 memset( files, 0 , sizeof(struct direntry));
1026                                 if(id->lib==NULL)
1027                                         files->relname= BLI_strdup(id->name+2);
1028                                 else {
1029                                         files->relname= MEM_mallocN(FILE_MAXDIR+FILE_MAXFILE+32, "filename for lib");
1030                                         sprintf(files->relname, "%s | %s", id->lib->name, id->name+2);
1031                                 }
1032                                 /* files->type |= S_IFDIR; */
1033                                 if(!filelist->has_func) { /* F4 DATA BROWSE */
1034                                         if(idcode==ID_OB) {
1035                                                 if( ((Object *)id)->flag & SELECT) files->flags |= ACTIVE;
1036                                         }
1037                                         else if(idcode==ID_SCE) {
1038                                                 if( ((Scene *)id)->r.scemode & R_BG_RENDER) files->flags |= ACTIVE;
1039                                         }                                       
1040                                 }
1041                                 files->nr= totbl+1;
1042                                 files->poin= id;
1043                                 fake= id->flag & LIB_FAKEUSER;
1044                                 if(idcode == ID_MA || idcode == ID_TE || idcode == ID_LA || idcode == ID_WO || idcode == ID_IM) {
1045                                         files->flags |= IMAGEFILE;
1046                                 }
1047                                 if(id->lib && fake) sprintf(files->extra, "LF %d", id->us);
1048                                 else if(id->lib) sprintf(files->extra, "L    %d", id->us);
1049                                 else if(fake) sprintf(files->extra, "F    %d", id->us);
1050                                 else sprintf(files->extra, "      %d", id->us);
1051                                 
1052                                 if(id->lib) {
1053                                         if(totlib==0) firstlib= files;
1054                                         totlib++;
1055                                 }
1056                                 
1057                                 files++;
1058                                 totbl++;
1059                         }
1060                         
1061                         id= id->next;
1062                 }
1063                 
1064                 /* only qsort of library blocks */
1065                 if(totlib>1) {
1066                         qsort(firstlib, totlib, sizeof(struct direntry), compare_name);
1067                 }
1068         }
1069 }
1070
1071
1072 void BIF_filelist_settype(struct FileList* filelist, int type)
1073 {
1074         filelist->type = type;
1075 }
1076
1077 short BIF_filelist_gettype(struct FileList* filelist)
1078 {
1079         return filelist->type;
1080 }
1081
1082 void BIF_filelist_sort(struct FileList* filelist, short sort)
1083 {
1084         struct direntry *file;
1085         int num;/*  , act= 0; */
1086
1087         switch(sort) {
1088         case FILE_SORTALPHA:
1089                 qsort(filelist->filelist, filelist->numfiles, sizeof(struct direntry), compare_name);   
1090                 break;
1091         case FILE_SORTDATE:
1092                 qsort(filelist->filelist, filelist->numfiles, sizeof(struct direntry), compare_date);   
1093                 break;
1094         case FILE_SORTSIZE:
1095                 qsort(filelist->filelist, filelist->numfiles, sizeof(struct direntry), compare_size);   
1096                 break;
1097         case FILE_SORTEXTENS:
1098                 qsort(filelist->filelist, filelist->numfiles, sizeof(struct direntry), compare_extension);      
1099         }
1100
1101         file= filelist->filelist;
1102         for(num=0; num<filelist->numfiles; num++, file++) {
1103                 file->flags &= ~HILITE;
1104         }
1105         BIF_filelist_filter(filelist);
1106 }
1107
1108
1109 void BIF_filelist_setipotype(struct FileList* filelist, short ipotype)
1110 {
1111         filelist->ipotype = ipotype;
1112 }
1113
1114 void BIF_filelist_hasfunc(struct FileList* filelist, int has_func)
1115 {
1116         filelist->has_func = has_func;
1117 }
1118