2.5 filebrowser WIP commit
[blender-staging.git] / source / blender / editors / space_file / filelist.c
index c52f2bc01326c422c6e65248c9dc9c0ec3d562eb..2ab43e59e6678a76ac411a8f82e89fd9f2318095 100644 (file)
@@ -114,8 +114,6 @@ typedef struct FileList
        int numfiltered;
        char dir[FILE_MAX];
        short type;
-       short ipotype;
-       struct BlendHandle *libfiledata;
        int has_func;
        short prv_w;
        short prv_h;
@@ -127,20 +125,6 @@ typedef struct FileList
        ListBase threads;
 } FileList;
 
-int BIF_groupname_to_code(char *group)
-{
-       char buf[32];
-       char *lslash;
-       
-       BLI_strncpy(buf, group, 31);
-       lslash= BLI_last_slash(buf);
-       if (lslash)
-               lslash[0]= '\0';
-
-       return BLO_idcode_from_name(buf);
-}
-
-
 #define SPECIAL_IMG_SIZE 48
 #define SPECIAL_IMG_ROWS 4
 #define SPECIAL_IMG_COLS 4
@@ -155,7 +139,8 @@ int BIF_groupname_to_code(char *group)
 #define SPECIAL_IMG_TEXTFILE 7
 #define SPECIAL_IMG_FONTFILE 8
 #define SPECIAL_IMG_UNKNOWNFILE 9
-#define SPECIAL_IMG_MAX SPECIAL_IMG_UNKNOWNFILE + 1
+#define SPECIAL_IMG_LOADING 10
+#define SPECIAL_IMG_MAX SPECIAL_IMG_LOADING + 1
 
 static ImBuf* gSpecialFileImages[SPECIAL_IMG_MAX];
 
@@ -185,6 +170,7 @@ static int compare_name(const void *a1, const void *a2)
        if( strcmp(entry1->relname, ".")==0 ) return (-1);
        if( strcmp(entry2->relname, ".")==0 ) return (1);
        if( strcmp(entry1->relname, "..")==0 ) return (-1);
+       if( strcmp(entry2->relname, "..")==0 ) return (1);
        
        return (BLI_strcasecmp(entry1->relname,entry2->relname));
 }
@@ -212,6 +198,7 @@ static int compare_date(const void *a1, const void *a2)
        if( strcmp(entry1->relname, ".")==0 ) return (-1);
        if( strcmp(entry2->relname, ".")==0 ) return (1);
        if( strcmp(entry1->relname, "..")==0 ) return (-1);
+       if( strcmp(entry2->relname, "..")==0 ) return (1);
        
        if ( entry1->s.st_mtime < entry2->s.st_mtime) return 1;
        if ( entry1->s.st_mtime > entry2->s.st_mtime) return -1;
@@ -242,6 +229,7 @@ static int compare_size(const void *a1, const void *a2)
        if( strcmp(entry1->relname, ".")==0 ) return (-1);
        if( strcmp(entry2->relname, ".")==0 ) return (1);
        if( strcmp(entry1->relname, "..")==0 ) return (-1);
+       if( strcmp(entry2->relname, "..")==0 ) return (1);
        
        if ( entry1->s.st_size < entry2->s.st_size) return 1;
        if ( entry1->s.st_size > entry2->s.st_size) return -1;
@@ -279,7 +267,7 @@ static int compare_extension(const void *a1, const void *a2) {
        if( strcmp(entry1->relname, ".")==0 ) return (-1);
        if( strcmp(entry2->relname, ".")==0 ) return (1);
        if( strcmp(entry1->relname, "..")==0 ) return (-1);
-       if( strcmp(entry2->relname, "..")==0 ) return (-1);
+       if( strcmp(entry2->relname, "..")==0 ) return (1);
        
        return (BLI_strcasecmp(sufix1, sufix2));
 }
@@ -293,11 +281,6 @@ void filelist_filter(FileList* filelist)
        if (!filelist->filelist)
                return;
        
-       if ( ( (filelist->type == FILE_LOADLIB) &&  filelist_islibrary(filelist, dir, group)) 
-               || (filelist->type == FILE_MAIN) ) {
-               filelist->filter = 0;
-       }
-
        if (!filelist->filter) {
                if (filelist->fidx) {
                        MEM_freeN(filelist->fidx);
@@ -380,7 +363,6 @@ struct FileList*    filelist_new()
        p->filelist = 0;
        p->numfiles = 0;
        p->dir[0] = '\0';
-       p->libfiledata = 0;
        p->type = 0;
        p->has_func = 0;
        p->filter = 0;
@@ -394,8 +376,6 @@ struct FileList*    filelist_copy(struct FileList* filelist)
        p->filelist = NULL;
        p->fidx = NULL;
        p->type = filelist->type;
-       p->ipotype = filelist->ipotype;
-       p->has_func = filelist->has_func;
 
        return p;
 }
@@ -437,18 +417,6 @@ void filelist_free(struct FileList* filelist)
        filelist->numfiltered =0;
 }
 
-void filelist_freelib(struct FileList* filelist)
-{
-       if(filelist->libfiledata)       
-               BLO_blendhandle_close(filelist->libfiledata);
-       filelist->libfiledata= 0;
-}
-
-struct BlendHandle *filelist_lib(struct FileList* filelist)
-{
-       return filelist->libfiledata;
-}
-
 int    filelist_numfiles(struct FileList* filelist)
 {
        return filelist->numfiltered;
@@ -599,34 +567,47 @@ struct ImBuf * filelist_getimage(struct FileList* filelist, int index)
        fidx = filelist->fidx[index];
        ibuf = filelist->filelist[fidx].image;
 
-       if (ibuf == NULL) {
-               struct direntry *file = &filelist->filelist[fidx];
-               if (file->type & S_IFDIR) {
+       return ibuf;
+}
+
+struct ImBuf * filelist_geticon(struct FileList* filelist, int index)
+{
+       ImBuf* ibuf= NULL;
+       struct direntry *file= NULL;
+       int fidx = 0;   
+       if ( (index < 0) || (index >= filelist->numfiltered) ) {
+               return NULL;
+       }
+       fidx = filelist->fidx[index];
+       file = &filelist->filelist[fidx];
+       if (file->type & S_IFDIR) {
                        if ( strcmp(filelist->filelist[fidx].relname, "..") == 0) {
                                ibuf = gSpecialFileImages[SPECIAL_IMG_PARENT];
                        } else if  ( strcmp(filelist->filelist[fidx].relname, ".") == 0) {
                                ibuf = gSpecialFileImages[SPECIAL_IMG_REFRESH];
                        } else {
-                               ibuf = gSpecialFileImages[SPECIAL_IMG_FOLDER];
+               ibuf = gSpecialFileImages[SPECIAL_IMG_FOLDER];
                        }
-               } else {
-                       ibuf = gSpecialFileImages[SPECIAL_IMG_UNKNOWNFILE];
-               }
+       } else {
+               ibuf = gSpecialFileImages[SPECIAL_IMG_UNKNOWNFILE];
+       }
 
-               if (file->flags & BLENDERFILE) {
-                       ibuf = gSpecialFileImages[SPECIAL_IMG_BLENDFILE];
-               } else if ( (file->flags & MOVIEFILE) || (file->flags & MOVIEFILE_ICON) ) {
-                       ibuf = gSpecialFileImages[SPECIAL_IMG_MOVIEFILE];
-               } else if (file->flags & SOUNDFILE) {
-                       ibuf = gSpecialFileImages[SPECIAL_IMG_SOUNDFILE];
-               } else if (file->flags & PYSCRIPTFILE) {
-                       ibuf = gSpecialFileImages[SPECIAL_IMG_PYTHONFILE];
-               } else if (file->flags & FTFONTFILE) {
-                       ibuf = gSpecialFileImages[SPECIAL_IMG_FONTFILE];
-               } else if (file->flags & TEXTFILE) {
-                       ibuf = gSpecialFileImages[SPECIAL_IMG_TEXTFILE];
-               } 
+       if (file->flags & BLENDERFILE) {
+               ibuf = gSpecialFileImages[SPECIAL_IMG_BLENDFILE];
+       } else if ( (file->flags & MOVIEFILE) || (file->flags & MOVIEFILE_ICON) ) {
+               ibuf = gSpecialFileImages[SPECIAL_IMG_MOVIEFILE];
+       } else if (file->flags & SOUNDFILE) {
+               ibuf = gSpecialFileImages[SPECIAL_IMG_SOUNDFILE];
+       } else if (file->flags & PYSCRIPTFILE) {
+               ibuf = gSpecialFileImages[SPECIAL_IMG_PYTHONFILE];
+       } else if (file->flags & FTFONTFILE) {
+               ibuf = gSpecialFileImages[SPECIAL_IMG_FONTFILE];
+       } else if (file->flags & TEXTFILE) {
+               ibuf = gSpecialFileImages[SPECIAL_IMG_TEXTFILE];
+       } else if (file->flags & IMAGEFILE) {
+               ibuf = gSpecialFileImages[SPECIAL_IMG_LOADING];
        }
+
        return ibuf;
 }
 
@@ -693,31 +674,18 @@ void filelist_readdir(struct FileList* filelist)
        filelist->fidx = 0;
        filelist->filelist = 0;
 
-       if(filelist->type==FILE_MAIN) {
-               filelist_from_main(filelist);
-               finished = 1;
-       } else if(filelist->type==FILE_LOADLIB) {
-               BLI_cleanup_dir(G.sce, filelist->dir);
-               filelist_from_library(filelist);
-               if(filelist->libfiledata) {
-                       finished = 1;
-               }
-       }
-
-       if (!finished) {
-               BLI_getwdN(wdir);        
-               
-               BLI_cleanup_dir(G.sce, filelist->dir);
-               BLI_hide_dot_files(filelist->hide_dot);
-               filelist->numfiles = BLI_getdir(filelist->dir, &(filelist->filelist));
+       BLI_getwdN(wdir);        
+       
+       BLI_cleanup_dir(G.sce, filelist->dir);
+       BLI_hide_dot_files(filelist->hide_dot);
+       filelist->numfiles = BLI_getdir(filelist->dir, &(filelist->filelist));
 
-               chdir(wdir);
-               filelist_setfiletypes(filelist, G.have_quicktime);
-               filelist_filter(filelist);
-               
-               if (!filelist->threads.first) {
-                       BLI_init_threads(&filelist->threads, exec_loadimages, 2);
-               }
+       chdir(wdir);
+       filelist_setfiletypes(filelist, G.have_quicktime);
+       filelist_filter(filelist);
+       
+       if (!filelist->threads.first) {
+               BLI_init_threads(&filelist->threads, exec_loadimages, 2);
        }
 
        filelist->maxnamelen = 0;
@@ -885,296 +853,6 @@ void filelist_swapselect(struct FileList* filelist)
        }
 }
 
-int filelist_islibrary(struct FileList* filelist, char* dir, char* group)
-{
-        /* return ok when a blenderfile, in dir is the filename,
-        * in group the type of libdata
-        */
-       int len;
-       char *fd;
-       
-       strcpy(dir, filelist->dir);
-       len= strlen(dir);
-       if(len<7) return 0;
-       if( dir[len-1] != '/' && dir[len-1] != '\\') return 0;
-               
-       group[0]= 0;
-       dir[len-1]= 0;
-
-       /* Find the last slash */
-       fd= (strrchr(dir, '/')>strrchr(dir, '\\'))?strrchr(dir, '/'):strrchr(dir, '\\');
-
-       if(fd==0) return 0;
-       *fd= 0;
-       if(BLO_has_bfile_extension(fd+1)) {
-               *fd= '/';
-       }
-       else {
-               char *gp = fd+1; // in case we have a .blend file, gp points to the group
-
-               /* Find the last slash */
-               fd= (strrchr(dir, '/')>strrchr(dir, '\\'))?strrchr(dir, '/'):strrchr(dir, '\\');
-               if (!fd || !BLO_has_bfile_extension(fd+1)) return 0;
-
-               /* now we know that we are in a blend file and it is safe to 
-                  assume that gp actually points to a group */
-               BLI_strncpy(group, gp, GROUP_MAX);
-       }
-       return 1;
-}
-
-void filelist_from_library(struct FileList* filelist)
-{
-       LinkNode *l, *names, *previews;
-       struct ImBuf* ima;
-       int ok, i, nnames, idcode;
-       char filename[FILE_MAXDIR+FILE_MAXFILE];
-       char dir[FILE_MAX], group[GROUP_MAX];   
-       
-       filelist->type = FILE_LOADLIB;
-
-       /* name test */
-       ok= filelist_islibrary(filelist, dir, group);
-       if (!ok) {
-               /* free */
-               if(filelist->libfiledata) BLO_blendhandle_close(filelist->libfiledata);
-               filelist->libfiledata= 0;
-               return;
-       }
-       
-       BLI_strncpy(filename, G.sce, sizeof(filename)); // G.sce = last file loaded, for UI
-
-       /* there we go */
-       /* for the time being only read filedata when libfiledata==0 */
-       if (filelist->libfiledata==0) {
-               filelist->libfiledata= BLO_blendhandle_from_file(dir);
-               if(filelist->libfiledata==0) return;
-       }
-       
-       idcode= BIF_groupname_to_code(group);
-
-               // memory for strings is passed into filelist[i].relname
-               // and free'd in freefilelist
-       previews = NULL;
-       if (idcode) {
-               previews= BLO_blendhandle_get_previews(filelist->libfiledata, idcode);
-               names= BLO_blendhandle_get_datablock_names(filelist->libfiledata, idcode);
-               /* ugh, no rewind, need to reopen */
-               BLO_blendhandle_close(filelist->libfiledata);
-               filelist->libfiledata= BLO_blendhandle_from_file(dir);
-               
-       } else {
-               names= BLO_blendhandle_get_linkable_groups(filelist->libfiledata);
-       }
-       
-       nnames= BLI_linklist_length(names);
-
-       filelist->numfiles= nnames + 2;
-       filelist->filelist= malloc(filelist->numfiles * sizeof(*filelist->filelist));
-       memset(filelist->filelist, 0, filelist->numfiles * sizeof(*filelist->filelist));
-
-       filelist->filelist[0].relname= BLI_strdup(".");
-       filelist->filelist[0].type |= S_IFDIR;
-       filelist->filelist[1].relname= BLI_strdup("..");
-       filelist->filelist[1].type |= S_IFDIR;
-               
-       for (i=0, l= names; i<nnames; i++, l= l->next) {
-               char *blockname= l->link;
-
-               filelist->filelist[i + 2].relname= BLI_strdup(blockname);
-               if (!idcode)
-                       filelist->filelist[i + 2].type |= S_IFDIR;
-       }
-       
-       if(previews) {
-               for (i=0, l= previews; i<nnames; i++, l= l->next) {
-                       PreviewImage *img= l->link;
-                       
-                       if (img) {
-                               unsigned int w = img->w[PREVIEW_MIPMAP_LARGE];
-                               unsigned int h = img->h[PREVIEW_MIPMAP_LARGE];
-                               unsigned int *rect = img->rect[PREVIEW_MIPMAP_LARGE];
-
-                               /* first allocate imbuf for copying preview into it */
-                               if (w > 0 && h > 0 && rect) {
-                                       ima = IMB_allocImBuf(w, h, 32, IB_rect, 0);
-                                       memcpy(ima->rect, rect, w*h*sizeof(unsigned int));
-                                       filelist->filelist[i + 2].image = ima;
-                                       filelist->filelist[i + 2].flags = IMAGEFILE;
-                               }
-                       }
-               }
-       }
-
-       BLI_linklist_free(names, free);
-       if (previews) BLI_linklist_free(previews, (void(*)(void*)) MEM_freeN);
-
-       filelist_sort(filelist, FILE_SORTALPHA);
-
-       BLI_strncpy(G.sce, filename, sizeof(filename)); // prevent G.sce to change
-
-       filelist->filter = 0;
-       filelist_filter(filelist);
-}
-
-void filelist_append_library(struct FileList *filelist, char *dir, char *file, short flag, int idcode, struct Main *mainvar, struct Scene* scene)
-{
-       // XXX todo: replace NULL with op->reports
-       BLO_library_append(&filelist->libfiledata, filelist->filelist, filelist->numfiles, dir, file, flag, idcode, mainvar, scene, NULL);
-}
-
-void filelist_from_main(struct FileList *filelist)
-{
-       ID *id;
-       struct direntry *files, *firstlib = NULL;
-       ListBase *lb;
-       int a, fake, idcode, ok, totlib, totbl;
-       
-       filelist->type = FILE_MAIN;
-
-       if(filelist->dir[0]=='/') filelist->dir[0]= 0;
-       
-       if(filelist->dir[0]) {
-               idcode= BIF_groupname_to_code(filelist->dir);
-               if(idcode==0) filelist->dir[0]= 0;
-       }
-       
-       if( filelist->dir[0]==0) {
-               
-               /* make directories */
-               filelist->numfiles= 23;
-               filelist->filelist= (struct direntry *)malloc(filelist->numfiles * sizeof(struct direntry));
-               
-               for(a=0; a<filelist->numfiles; a++) {
-                       memset( &(filelist->filelist[a]), 0 , sizeof(struct direntry));
-                       filelist->filelist[a].type |= S_IFDIR;
-               }
-               
-               filelist->filelist[0].relname= BLI_strdup("..");
-               filelist->filelist[1].relname= BLI_strdup(".");
-               filelist->filelist[2].relname= BLI_strdup("Scene");
-               filelist->filelist[3].relname= BLI_strdup("Object");
-               filelist->filelist[4].relname= BLI_strdup("Mesh");
-               filelist->filelist[5].relname= BLI_strdup("Curve");
-               filelist->filelist[6].relname= BLI_strdup("Metaball");
-               filelist->filelist[7].relname= BLI_strdup("Material");
-               filelist->filelist[8].relname= BLI_strdup("Texture");
-               filelist->filelist[9].relname= BLI_strdup("Image");
-               filelist->filelist[10].relname= BLI_strdup("Ika");
-               filelist->filelist[11].relname= BLI_strdup("Wave");
-               filelist->filelist[12].relname= BLI_strdup("Lattice");
-               filelist->filelist[13].relname= BLI_strdup("Lamp");
-               filelist->filelist[14].relname= BLI_strdup("Camera");
-               filelist->filelist[15].relname= BLI_strdup("Ipo");
-               filelist->filelist[16].relname= BLI_strdup("World");
-               filelist->filelist[17].relname= BLI_strdup("Screen");
-               filelist->filelist[18].relname= BLI_strdup("VFont");
-               filelist->filelist[19].relname= BLI_strdup("Text");
-               filelist->filelist[20].relname= BLI_strdup("Armature");
-               filelist->filelist[21].relname= BLI_strdup("Action");
-               filelist->filelist[22].relname= BLI_strdup("NodeTree");
-               filelist_sort(filelist, FILE_SORTALPHA);
-       }
-       else {
-
-               /* make files */
-               idcode= BIF_groupname_to_code(filelist->dir);
-               
-               lb= wich_libbase(G.main, idcode );
-               if(lb==0) return;
-               
-               id= lb->first;
-               filelist->numfiles= 0;
-               while(id) {
-                       
-                       if(filelist->has_func && idcode==ID_IP) {
-                               if(filelist->ipotype== ((Ipo *)id)->blocktype) filelist->numfiles++;
-                       }
-                       else if (!filelist->hide_dot || id->name[2] != '.') {
-                               filelist->numfiles++;
-                       }
-                       
-                       id= id->next;
-               }
-               
-               if(!filelist->has_func) filelist->numfiles+= 2;
-               filelist->filelist= (struct direntry *)malloc(filelist->numfiles * sizeof(struct direntry));
-               
-               files = filelist->filelist;
-               
-               if(!filelist->has_func) {
-                       memset( &(filelist->filelist[0]), 0 , sizeof(struct direntry));
-                       filelist->filelist[0].relname= BLI_strdup(".");
-                       filelist->filelist[0].type |= S_IFDIR;
-                       memset( &(filelist->filelist[1]), 0 , sizeof(struct direntry));
-                       filelist->filelist[1].relname= BLI_strdup("..");
-                       filelist->filelist[1].type |= S_IFDIR;
-               
-                       files+= 2;
-               }
-               
-               id= lb->first;
-               totlib= totbl= 0;
-               
-               while(id) {
-                       
-                       ok= 0;
-                       if(filelist->has_func && idcode==ID_IP) {
-                               if(filelist->ipotype== ((Ipo *)id)->blocktype) ok= 1;
-                       }
-                       else ok= 1;
-                       
-                       if(ok) {
-                               /* TODO: hide dot files - elubie */
-                               memset( files, 0 , sizeof(struct direntry));
-                               if(id->lib==NULL)
-                                       files->relname= BLI_strdup(id->name+2);
-                               else {
-                                       files->relname= MEM_mallocN(FILE_MAXDIR+FILE_MAXFILE+32, "filename for lib");
-                                       sprintf(files->relname, "%s | %s", id->lib->name, id->name+2);
-                               }
-                               /* files->type |= S_IFDIR; */
-                               if(!filelist->has_func) { /* F4 DATA BROWSE */
-                                       if(idcode==ID_OB) {
-                                               if( ((Object *)id)->flag & SELECT) files->flags |= ACTIVE;
-                                       }
-                                       else if(idcode==ID_SCE) {
-                                               if( ((Scene *)id)->r.scemode & R_BG_RENDER) files->flags |= ACTIVE;
-                                       }                                       
-                               }
-                               files->nr= totbl+1;
-                               files->poin= id;
-                               fake= id->flag & LIB_FAKEUSER;
-                               if(idcode == ID_MA || idcode == ID_TE || idcode == ID_LA || idcode == ID_WO || idcode == ID_IM) {
-                                       files->flags |= IMAGEFILE;
-                               }
-                               if(id->lib && fake) sprintf(files->extra, "LF %d", id->us);
-                               else if(id->lib) sprintf(files->extra, "L    %d", id->us);
-                               else if(fake) sprintf(files->extra, "F    %d", id->us);
-                               else sprintf(files->extra, "      %d", id->us);
-                               
-                               if(id->lib) {
-                                       if(totlib==0) firstlib= files;
-                                       totlib++;
-                               }
-                               
-                               files++;
-                               totbl++;
-                       }
-                       
-                       id= id->next;
-               }
-               
-               /* only qsort of library blocks */
-               if(totlib>1) {
-                       qsort(firstlib, totlib, sizeof(struct direntry), compare_name);
-               }
-       }
-       filelist->filter = 0;
-       filelist_filter(filelist);
-}
-
-
 void filelist_settype(struct FileList* filelist, int type)
 {
        filelist->type = type;
@@ -1210,15 +888,3 @@ void filelist_sort(struct FileList* filelist, short sort)
        }
        filelist_filter(filelist);
 }
-
-
-void filelist_setipotype(struct FileList* filelist, short ipotype)
-{
-       filelist->ipotype = ipotype;
-}
-
-void filelist_hasfunc(struct FileList* filelist, int has_func)
-{
-       filelist->has_func = has_func;
-}
-