style cleanup: comments
[blender.git] / source / blender / editors / space_file / filelist.c
index 288ff76..9d5f7fd 100644 (file)
@@ -50,7 +50,7 @@
 #include "BLI_utildefines.h"
 
 #ifdef WIN32
-#include "BLI_winstuff.h"
+#  include "BLI_winstuff.h"
 #endif
 
 #include "BKE_context.h"
@@ -98,12 +98,11 @@ typedef struct ThumbnailJob {
        ListBase loadimages;
        short *stop;
        short *do_update;
-       struct FileListfilelist;
+       struct FileList *filelist;
        ReportList reports;
 } ThumbnailJob;
 
-typedef struct FileList
-{
+typedef struct FileList {
        struct direntry *filelist;
        int *fidx;
        int numfiles;
@@ -120,12 +119,11 @@ typedef struct FileList
        short hide_parent;
 
        void (*readf)(struct FileList *);
-       int  (*filterf)(struct direntry* file, const char* dir, unsigned int filter, short hide_dot);
+       int (*filterf)(struct direntry *file, const char *dir, unsigned int filter, short hide_dot);
 
 } FileList;
 
-typedef struct FolderList
-{
+typedef struct FolderList {
        struct FolderList *next, *prev;
        char *foldername;
 } FolderList;
@@ -147,206 +145,220 @@ typedef struct FolderList
 #define SPECIAL_IMG_LOADING 10
 #define SPECIAL_IMG_MAX SPECIAL_IMG_LOADING + 1
 
-static ImBufgSpecialFileImages[SPECIAL_IMG_MAX];
+static ImBuf *gSpecialFileImages[SPECIAL_IMG_MAX];
 
 
 /* ******************* SORT ******************* */
 
 static int compare_name(const void *a1, const void *a2)
 {
-       const struct direntry *entry1=a1, *entry2=a2;
+       const struct direntry *entry1 = a1, *entry2 = a2;
 
        /* type is equal to stat.st_mode */
 
-       if (S_ISDIR(entry1->type)){
-               if (S_ISDIR(entry2->type)==0) return (-1);
-       } else{
+       if (S_ISDIR(entry1->type)) {
+               if (S_ISDIR(entry2->type) == 0) return (-1);
+       }
+       else {
                if (S_ISDIR(entry2->type)) return (1);
        }
-       if (S_ISREG(entry1->type)){
-               if (S_ISREG(entry2->type)==0) return (-1);
-       } else{
+       if (S_ISREG(entry1->type)) {
+               if (S_ISREG(entry2->type) == 0) return (-1);
+       }
+       else {
                if (S_ISREG(entry2->type)) return (1);
        }
        if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
        if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
        
        /* make sure "." and ".." are always first */
-       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(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_natstrcmp(entry1->relname,entry2->relname));
+       return (BLI_natstrcmp(entry1->relname, entry2->relname));
 }
 
 static int compare_date(const void *a1, const void *a2)        
 {
-       const struct direntry *entry1=a1, *entry2=a2;
+       const struct direntry *entry1 = a1, *entry2 = a2;
        
        /* type is equal to stat.st_mode */
 
-       if (S_ISDIR(entry1->type)){
-               if (S_ISDIR(entry2->type)==0) return (-1);
-       } else{
+       if (S_ISDIR(entry1->type)) {
+               if (S_ISDIR(entry2->type) == 0) return (-1);
+       }
+       else {
                if (S_ISDIR(entry2->type)) return (1);
        }
-       if (S_ISREG(entry1->type)){
-               if (S_ISREG(entry2->type)==0) return (-1);
-       } else{
+       if (S_ISREG(entry1->type)) {
+               if (S_ISREG(entry2->type) == 0) return (-1);
+       }
+       else {
                if (S_ISREG(entry2->type)) return (1);
        }
        if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
        if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
 
        /* make sure "." and ".." are always first */
-       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(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;
+       if (entry1->s.st_mtime < entry2->s.st_mtime) return 1;
+       if (entry1->s.st_mtime > entry2->s.st_mtime) return -1;
        
-       else return BLI_natstrcmp(entry1->relname,entry2->relname);
+       else return BLI_natstrcmp(entry1->relname, entry2->relname);
 }
 
 static int compare_size(const void *a1, const void *a2)        
 {
-       const struct direntry *entry1=a1, *entry2=a2;
+       const struct direntry *entry1 = a1, *entry2 = a2;
 
        /* type is equal to stat.st_mode */
 
-       if (S_ISDIR(entry1->type)){
-               if (S_ISDIR(entry2->type)==0) return (-1);
-       } else{
+       if (S_ISDIR(entry1->type)) {
+               if (S_ISDIR(entry2->type) == 0) return (-1);
+       }
+       else {
                if (S_ISDIR(entry2->type)) return (1);
        }
-       if (S_ISREG(entry1->type)){
-               if (S_ISREG(entry2->type)==0) return (-1);
-       } else{
+       if (S_ISREG(entry1->type)) {
+               if (S_ISREG(entry2->type) == 0) return (-1);
+       }
+       else {
                if (S_ISREG(entry2->type)) return (1);
        }
        if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
        if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
 
        /* make sure "." and ".." are always first */
-       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(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;
-       else return BLI_natstrcmp(entry1->relname,entry2->relname);
+       if (entry1->s.st_size < entry2->s.st_size) return 1;
+       if (entry1->s.st_size > entry2->s.st_size) return -1;
+       else return BLI_natstrcmp(entry1->relname, entry2->relname);
 }
 
 static int compare_extension(const void *a1, const void *a2)
 {
-       const struct direntry *entry1=a1, *entry2=a2;
+       const struct direntry *entry1 = a1, *entry2 = a2;
        const char *sufix1, *sufix2;
-       const char *nil="";
+       const char *nil = "";
 
-       if (!(sufix1= strstr (entry1->relname, ".blend.gz"))) 
-               sufix1= strrchr (entry1->relname, '.');
-       if (!(sufix2= strstr (entry2->relname, ".blend.gz")))
-               sufix2= strrchr (entry2->relname, '.');
-       if (!sufix1) sufix1= nil;
-       if (!sufix2) sufix2= nil;
+       if (!(sufix1 = strstr(entry1->relname, ".blend.gz")))
+               sufix1 = strrchr(entry1->relname, '.');
+       if (!(sufix2 = strstr(entry2->relname, ".blend.gz")))
+               sufix2 = strrchr(entry2->relname, '.');
+       if (!sufix1) sufix1 = nil;
+       if (!sufix2) sufix2 = nil;
 
        /* type is equal to stat.st_mode */
 
-       if (S_ISDIR(entry1->type)){
-               if (S_ISDIR(entry2->type)==0) return (-1);
-       } else{
+       if (S_ISDIR(entry1->type)) {
+               if (S_ISDIR(entry2->type) == 0) return (-1);
+       }
+       else {
                if (S_ISDIR(entry2->type)) return (1);
        }
-       if (S_ISREG(entry1->type)){
-               if (S_ISREG(entry2->type)==0) return (-1);
-       } else{
+       if (S_ISREG(entry1->type)) {
+               if (S_ISREG(entry2->type) == 0) return (-1);
+       }
+       else {
                if (S_ISREG(entry2->type)) return (1);
        }
        if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
        if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
        
        /* make sure "." and ".." are always first */
-       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(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(sufix1, sufix2));
 }
 
-static int is_hidden_file(const charfilename, short hide_dot)
+static int is_hidden_file(const char *filename, short hide_dot)
 {
-       int is_hidden=0;
+       int is_hidden = 0;
 
        if (hide_dot) {
-               if(filename[0]=='.' && filename[1]!='.' && filename[1]!=0) {
-                       is_hidden=1; /* ignore .file */
-               } else if (((filename[0] == '.') && (filename[1] == 0) )) {
-                       is_hidden=1; /* ignore . */
-               } else {
-                       int len=strlen(filename);
-                       if( (len>0) && (filename[len-1]=='~') ) {
-                               is_hidden=1;  /* ignore file~ */
+               if (filename[0] == '.' && filename[1] != '.' && filename[1] != 0) {
+                       is_hidden = 1; /* ignore .file */
+               }
+               else if (((filename[0] == '.') && (filename[1] == 0) )) {
+                       is_hidden = 1; /* ignore . */
+               }
+               else {
+                       int len = strlen(filename);
+                       if ( (len > 0) && (filename[len - 1] == '~') ) {
+                               is_hidden = 1;  /* ignore file~ */
                        }
                } 
-       } else {
+       }
+       else {
                if (((filename[0] == '.') && (filename[1] == 0) )) {
-                       is_hidden=1; /* ignore . */
+                       is_hidden = 1; /* ignore . */
                }
        }
        return is_hidden;
 }
 
-static int is_filtered_file(struct direntry* file, const char* UNUSED(dir), unsigned int filter, short hide_dot)
+static int is_filtered_file(struct direntry *file, const char *UNUSED(dir), unsigned int filter, short hide_dot)
 {
-       int is_filtered=0;
+       int is_filtered = 0;
        if (filter) {
                if (file->flags & filter) {
-                       is_filtered=1;
-               } else if (file->type & S_IFDIR) {
+                       is_filtered = 1;
+               }
+               else if (file->type & S_IFDIR) {
                        if (filter & FOLDERFILE) {
                                is_filtered = 1;
                        }
                }
-       } else {
+       }
+       else {
                is_filtered = 1;
        }
        return is_filtered && !is_hidden_file(file->relname, hide_dot);
 }
 
-static int is_filtered_lib(struct direntry* file, const char* dir, unsigned int filter, short hide_dot)
+static int is_filtered_lib(struct direntry *file, const char *dir, unsigned int filter, short hide_dot)
 {
-       int is_filtered=0;
+       int is_filtered = 0;
        char tdir[FILE_MAX], tgroup[GROUP_MAX];
        if (BLO_is_a_library(dir, tdir, tgroup)) {
                is_filtered = !is_hidden_file(file->relname, hide_dot);
-       } else {
+       }
+       else {
                is_filtered = is_filtered_file(file, dir, filter, hide_dot);
        }
        return is_filtered;
 }
 
-static int is_filtered_main(struct direntry* file, const char* UNUSED(dir), unsigned int UNUSED(filter), short hide_dot)
+static int is_filtered_main(struct direntry *file, const char *UNUSED(dir), unsigned int UNUSED(filter), short hide_dot)
 {
        return !is_hidden_file(file->relname, hide_dot);
 }
 
-void filelist_filter(FileListfilelist)
+void filelist_filter(FileList *filelist)
 {
        int num_filtered = 0;
        int i, j;
-       
+
        if (!filelist->filelist)
                return;
 
-       // How many files are left after filter ?
+       /* How many files are left after filter ? */
        for (i = 0; i < filelist->numfiles; ++i) {
                struct direntry *file = &filelist->filelist[i];
-               if ( filelist->filterf(file, filelist->dir, filelist->filter, filelist->hide_dot) ) {
+               if (filelist->filterf(file, filelist->dir, filelist->filter, filelist->hide_dot) ) {
                        num_filtered++;
                } 
        }
@@ -355,12 +367,12 @@ void filelist_filter(FileList* filelist)
                MEM_freeN(filelist->fidx);
                filelist->fidx = NULL;
        }
-       filelist->fidx = (int *)MEM_callocN(num_filtered*sizeof(int), "filteridx");
+       filelist->fidx = (int *)MEM_callocN(num_filtered * sizeof(int), "filteridx");
        filelist->numfiltered = num_filtered;
 
-       for (i = 0, j=0; i < filelist->numfiles; ++i) {
+       for (i = 0, j = 0; i < filelist->numfiles; ++i) {
                struct direntry *file = &filelist->filelist[i];
-               if ( filelist->filterf(file, filelist->dir, filelist->filter, filelist->hide_dot) ) {
+               if (filelist->filterf(file, filelist->dir, filelist->filter, filelist->hide_dot) ) {
                        filelist->fidx[j++] = i;
                }
        }
@@ -371,19 +383,22 @@ void filelist_init_icons(void)
        short x, y, k;
        ImBuf *bbuf;
        ImBuf *ibuf;
+
+       BLI_assert(G.background == FALSE);
+
 #ifdef WITH_HEADLESS
        bbuf = NULL;
 #else
-       bbuf = IMB_ibImageFromMemory((unsigned char*)datatoc_prvicons, datatoc_prvicons_size, IB_rect, "<splash>");
+       bbuf = IMB_ibImageFromMemory((unsigned char *)datatoc_prvicons_png, datatoc_prvicons_png_size, IB_rect, NULL, "<splash>");
 #endif
        if (bbuf) {
-               for (y=0; y<SPECIAL_IMG_ROWS; y++) {
-                       for (x=0; x<SPECIAL_IMG_COLS; x++) {
-                               int tile = SPECIAL_IMG_COLS*y + x; 
+               for (y = 0; y < SPECIAL_IMG_ROWS; y++) {
+                       for (x = 0; x < SPECIAL_IMG_COLS; x++) {
+                               int tile = SPECIAL_IMG_COLS * y + x;
                                if (tile < SPECIAL_IMG_MAX) {
                                        ibuf = IMB_allocImBuf(SPECIAL_IMG_SIZE, SPECIAL_IMG_SIZE, 32, IB_rect);
-                                       for (k=0; k<SPECIAL_IMG_SIZE; k++) {
-                                               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));
+                                       for (k = 0; k < SPECIAL_IMG_SIZE; k++) {
+                                               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));
                                        }
                                        gSpecialFileImages[tile] = ibuf;
                                }
@@ -396,59 +411,62 @@ void filelist_init_icons(void)
 void filelist_free_icons(void)
 {
        int i;
-       for (i=0; i < SPECIAL_IMG_MAX; ++i) {
+
+       BLI_assert(G.background == FALSE);
+
+       for (i = 0; i < SPECIAL_IMG_MAX; ++i) {
                IMB_freeImBuf(gSpecialFileImages[i]);
                gSpecialFileImages[i] = NULL;
        }
 }
 
-//-----------------FOLDERLIST (previous/next) --------------//
-struct ListBase* folderlist_new(void)
+/* -----------------FOLDERLIST (previous/next) -------------- */
+ListBase *folderlist_new(void)
 {
-       ListBase* p = MEM_callocN( sizeof(ListBase), "folderlist" );
+       ListBase *p = MEM_callocN(sizeof(ListBase), "folderlist");
        return p;
 }
 
-void folderlist_popdir(struct ListBasefolderlist, char *dir)
+void folderlist_popdir(struct ListBase *folderlist, char *dir)
 {
        const char *prev_dir;
        struct FolderList *folder;
        folder = folderlist->last;
 
-       if(folder){
-               // remove the current directory
+       if (folder) {
+               /* remove the current directory */
                MEM_freeN(folder->foldername);
                BLI_freelinkN(folderlist, folder);
 
                folder = folderlist->last;
-               if(folder){
+               if (folder) {
                        prev_dir = folder->foldername;
                        BLI_strncpy(dir, prev_dir, FILE_MAXDIR);
                }
        }
-       // delete the folder next or use setdir directly before PREVIOUS OP
+       /* delete the folder next or use setdir directly before PREVIOUS OP */
 }
 
-void folderlist_pushdir(ListBasefolderlist, const char *dir)
+void folderlist_pushdir(ListBase *folderlist, const char *dir)
 {
        struct FolderList *folder, *previous_folder;
        previous_folder = folderlist->last;
 
-       // check if already exists
-       if(previous_folder && previous_folder->foldername){
-               if(BLI_path_cmp(previous_folder->foldername, dir)==0){
+       /* check if already exists */
+       if (previous_folder && previous_folder->foldername) {
+               if (BLI_path_cmp(previous_folder->foldername, dir) == 0) {
                        return;
                }
        }
 
-       // create next folder element
-       folder = (FolderList*)MEM_mallocN(sizeof(FolderList),"FolderList");
-       folder->foldername = (char*)MEM_mallocN(sizeof(char)*(strlen(dir)+1), "foldername");
+       /* create next folder element */
+       folder = (FolderList *)MEM_mallocN(sizeof(FolderList), "FolderList");
+       folder->foldername = (char *)MEM_mallocN(sizeof(char) * (strlen(dir) + 1), "foldername");
        folder->foldername[0] = '\0';
 
        BLI_strncpy(folder->foldername, dir, FILE_MAXDIR);
 
-       // add it to the end of the list
+       /* add it to the end of the list */
        BLI_addtail(folderlist, folder);
 }
 
@@ -456,41 +474,41 @@ int folderlist_clear_next(struct SpaceFile *sfile)
 {
        struct FolderList *folder;
 
-       // if there is no folder_next there is nothing we can clear
+       /* if there is no folder_next there is nothing we can clear */
        if (!sfile->folders_next)
                return 0;
 
-       // if previous_folder, next_folder or refresh_folder operators are executed it doesn't clear folder_next
+       /* if previous_folder, next_folder or refresh_folder operators are executed it doesn't clear folder_next */
        folder = sfile->folders_prev->last;
-       if ((!folder) ||(BLI_path_cmp(folder->foldername, sfile->params->dir) == 0))
+       if ((!folder) || (BLI_path_cmp(folder->foldername, sfile->params->dir) == 0))
                return 0;
 
-       // eventually clear flist->folders_next
+       /* eventually clear flist->folders_next */
        return 1;
 }
 
 /* not listbase itself */
-void folderlist_free(ListBasefolderlist)
+void folderlist_free(ListBase *folderlist)
 {
-       if (folderlist){
+       if (folderlist) {
                FolderList *folder;
-               for(folder= folderlist->first; folder; folder= folder->next)
+               for (folder = folderlist->first; folder; folder = folder->next)
                        MEM_freeN(folder->foldername);
                BLI_freelistN(folderlist);
        }
 }
 
-ListBase *folderlist_duplicate(ListBasefolderlist)
+ListBase *folderlist_duplicate(ListBase *folderlist)
 {
        
        if (folderlist) {
-               ListBase *folderlistn= MEM_callocN(sizeof(ListBase), "copy folderlist");
+               ListBase *folderlistn = MEM_callocN(sizeof(ListBase), "copy folderlist");
                FolderList *folder;
                
                BLI_duplicatelist(folderlistn, folderlist);
                
-               for(folder= folderlistn->first; folder; folder= folder->next) {
-                       folder->foldername= MEM_dupallocN(folder->foldername);
+               for (folder = folderlistn->first; folder; folder = folder->next) {
+                       folder->foldername = MEM_dupallocN(folder->foldername);
                }
                return folderlistn;
        }
@@ -498,15 +516,15 @@ ListBase *folderlist_duplicate(ListBase* folderlist)
 }
 
 
-static void filelist_read_main(struct FileListfilelist);
-static void filelist_read_library(struct FileListfilelist);
-static void filelist_read_dir(struct FileListfilelist);
+static void filelist_read_main(struct FileList *filelist);
+static void filelist_read_library(struct FileList *filelist);
+static void filelist_read_dir(struct FileList *filelist);
 
-//------------------FILELIST------------------------//
-struct FileList*       filelist_new(short type)
+/* ------------------FILELIST------------------------ */
+FileList *filelist_new(short type)
 {
-       FileList* p = MEM_callocN( sizeof(FileList), "filelist" );
-       switch(type) {
+       FileList *p = MEM_callocN(sizeof(FileList), "filelist");
+       switch (type) {
                case FILE_MAIN:
                        p->readf = filelist_read_main;
                        p->filterf = is_filtered_main;
@@ -524,7 +542,7 @@ struct FileList*    filelist_new(short type)
 }
 
 
-void filelist_free(struct FileListfilelist)
+void filelist_free(struct FileList *filelist)
 {
        int i;
 
@@ -558,52 +576,55 @@ void filelist_free(struct FileList* filelist)
        filelist->filelist = NULL;      
        filelist->filter = 0;
        filelist->filter_glob[0] = '\0';
-       filelist->numfiltered =0;
-       filelist->hide_dot =0;
+       filelist->numfiltered = 0;
+       filelist->hide_dot = 0;
 }
 
-void filelist_freelib(struct FileListfilelist)
+void filelist_freelib(struct FileList *filelist)
 {
-       if(filelist->libfiledata)       
+       if (filelist->libfiledata)
                BLO_blendhandle_close(filelist->libfiledata);
-       filelist->libfiledata= NULL;
+       filelist->libfiledata = NULL;
 }
 
-struct BlendHandle *filelist_lib(struct FileList* filelist)
+BlendHandle *filelist_lib(struct FileList *filelist)
 {
        return filelist->libfiledata;
 }
 
-int    filelist_numfiles(struct FileList* filelist)
+int filelist_numfiles(struct FileList *filelist)
 {
        return filelist->numfiltered;
 }
 
-const char * filelist_dir(struct FileList* filelist)
+const char *filelist_dir(struct FileList *filelist)
 {
        return filelist->dir;
 }
 
-void filelist_setdir(struct FileListfilelist, const char *dir)
+void filelist_setdir(struct FileList *filelist, const char *dir)
 {
-       BLI_strncpy(filelist->dir, dir, FILE_MAX);
+       BLI_strncpy(filelist->dir, dir, sizeof(filelist->dir));
 }
 
-void filelist_imgsize(struct FileListfilelist, short w, short h)
+void filelist_imgsize(struct FileList *filelist, short w, short h)
 {
        filelist->prv_w = w;
        filelist->prv_h = h;
 }
 
-short filelist_changed(struct FileListfilelist)
+short filelist_changed(struct FileList *filelist)
 {
        return filelist->changed;
 }
 
-struct ImBuf * filelist_getimage(struct FileList* filelist, int index)
+ImBuf *filelist_getimage(struct FileList *filelist, int index)
 {
-       ImBuf* ibuf = NULL;
-       int fidx = 0;   
+       ImBuf *ibuf = NULL;
+       int fidx = 0;
+
+       BLI_assert(G.background == FALSE);
+
        if ( (index < 0) || (index >= filelist->numfiltered) ) {
                return NULL;
        }
@@ -613,48 +634,60 @@ struct ImBuf * filelist_getimage(struct FileList* filelist, int index)
        return ibuf;
 }
 
-struct ImBuf * filelist_geticon(struct FileList* filelist, int index)
+ImBuf *filelist_geticon(struct FileList *filelist, int index)
 {
-       ImBuf* ibuf= NULL;
-       struct direntry *file= NULL;
-       int fidx = 0;   
+       ImBuf *ibuf = NULL;
+       struct direntry *file = NULL;
+       int fidx = 0;
+
+       BLI_assert(G.background == FALSE);
+
        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) {
+               if (strcmp(filelist->filelist[fidx].relname, "..") == 0) {
                        ibuf = gSpecialFileImages[SPECIAL_IMG_PARENT];
-               } else if  ( strcmp(filelist->filelist[fidx].relname, ".") == 0) {
+               }
+               else if (strcmp(filelist->filelist[fidx].relname, ".") == 0) {
                        ibuf = gSpecialFileImages[SPECIAL_IMG_REFRESH];
-               } else {
+               }
+               else {
                        ibuf = gSpecialFileImages[SPECIAL_IMG_FOLDER];
                }
-       } else {
+       }
+       else {
                ibuf = gSpecialFileImages[SPECIAL_IMG_UNKNOWNFILE];
        }
 
        if (file->flags & BLENDERFILE) {
                ibuf = gSpecialFileImages[SPECIAL_IMG_BLENDFILE];
-       } else if ( (file->flags & MOVIEFILE) || (file->flags & MOVIEFILE_ICON) ) {
+       }
+       else if ( (file->flags & MOVIEFILE) || (file->flags & MOVIEFILE_ICON) ) {
                ibuf = gSpecialFileImages[SPECIAL_IMG_MOVIEFILE];
-       } else if (file->flags & SOUNDFILE) {
+       }
+       else if (file->flags & SOUNDFILE) {
                ibuf = gSpecialFileImages[SPECIAL_IMG_SOUNDFILE];
-       } else if (file->flags & PYSCRIPTFILE) {
+       }
+       else if (file->flags & PYSCRIPTFILE) {
                ibuf = gSpecialFileImages[SPECIAL_IMG_PYTHONFILE];
-       } else if (file->flags & FTFONTFILE) {
+       }
+       else if (file->flags & FTFONTFILE) {
                ibuf = gSpecialFileImages[SPECIAL_IMG_FONTFILE];
-       } else if (file->flags & TEXTFILE) {
+       }
+       else if (file->flags & TEXTFILE) {
                ibuf = gSpecialFileImages[SPECIAL_IMG_TEXTFILE];
-       } else if (file->flags & IMAGEFILE) {
+       }
+       else if (file->flags & IMAGEFILE) {
                ibuf = gSpecialFileImages[SPECIAL_IMG_LOADING];
        }
 
        return ibuf;
 }
 
-struct direntry * filelist_file(struct FileList* filelist, int index)
+struct direntry *filelist_file(struct FileList *filelist, int index)
 {
        int fidx = 0;
        
@@ -666,7 +699,7 @@ struct direntry * filelist_file(struct FileList* filelist, int index)
        return &filelist->filelist[fidx];
 }
 
-int filelist_find(struct FileListfilelist, const char *filename)
+int filelist_find(struct FileList *filelist, const char *filename)
 {
        int index = -1;
        int i;
@@ -677,7 +710,7 @@ int filelist_find(struct FileList* filelist, const char *filename)
 
        
        for (i = 0; i < filelist->numfiles; ++i) {
-               if ( strcmp(filelist->filelist[i].relname, filename) == 0) { /* not dealing with user input so dont need BLI_path_cmp */
+               if (strcmp(filelist->filelist[i].relname, filename) == 0) {  /* not dealing with user input so don't need BLI_path_cmp */
                        index = i;
                        break;
                }
@@ -692,17 +725,17 @@ int filelist_find(struct FileList* filelist, const char *filename)
        return fidx;
 }
 
-void filelist_hidedot(struct FileListfilelist, short hide)
+void filelist_hidedot(struct FileList *filelist, short hide)
 {
        filelist->hide_dot = hide;
 }
 
-void filelist_setfilter(struct FileListfilelist, unsigned int filter)
+void filelist_setfilter(struct FileList *filelist, unsigned int filter)
 {
        filelist->filter = filter;
 }
 
-void filelist_setfilter_types(struct FileListfilelist, const char *filter_glob)
+void filelist_setfilter_types(struct FileList *filelist, const char *filter_glob)
 {
        BLI_strncpy(filelist->filter_glob, filter_glob, sizeof(filelist->filter_glob));
 }
@@ -710,23 +743,25 @@ void filelist_setfilter_types(struct FileList* filelist, const char *filter_glob
 static int file_is_blend_backup(const char *str)
 {
        short a, b;
-       int retval= 0;
+       int retval = 0;
        
-       a= strlen(str);
-       b= 7;
+       a = strlen(str);
+       b = 7;
        
-       if(a==0 || b>=a);
+       if (a == 0 || b >= a) {
+               /* pass */
+       }
        else {
                char *loc;
                
-               if(a > b+1)
+               if (a > b + 1)
                        b++;
                
                /* allow .blend1 .blend2 .blend32 */
-               loc= BLI_strcasestr(str+a-b, ".blend");
+               loc = BLI_strcasestr(str + a - b, ".blend");
                
-               if(loc)
-                       retval= 1;
+               if (loc)
+                       retval = 1;
        }
        
        return (retval);
@@ -735,32 +770,44 @@ static int file_is_blend_backup(const char *str)
 
 static int file_extension_type(const char *relname)
 {
-       if(BLO_has_bfile_extension(relname)) {
+       if (BLO_has_bfile_extension(relname)) {
                return BLENDERFILE;
-       } else if(file_is_blend_backup(relname)) {
+       }
+       else if (file_is_blend_backup(relname)) {
                return BLENDERFILE_BACKUP;
-       } else if(BLI_testextensie(relname, ".py")) {
+       }
+       else if (BLI_testextensie(relname, ".py")) {
                return PYSCRIPTFILE;
-       } else if(BLI_testextensie(relname, ".txt")
-                         || BLI_testextensie(relname, ".glsl")
-                         || BLI_testextensie(relname, ".data")) {
+       }
+       else if (BLI_testextensie(relname, ".txt")  ||
+                BLI_testextensie(relname, ".glsl") ||
+                BLI_testextensie(relname, ".data"))
+       {
                return TEXTFILE;
-       } else if( BLI_testextensie(relname, ".ttf")
-                         || BLI_testextensie(relname, ".ttc")
-                         || BLI_testextensie(relname, ".pfb")
-                         || BLI_testextensie(relname, ".otf")
-                         || BLI_testextensie(relname, ".otc")) {
+       }
+       else if (BLI_testextensie(relname, ".ttf") ||
+                BLI_testextensie(relname, ".ttc") ||
+                BLI_testextensie(relname, ".pfb") ||
+                BLI_testextensie(relname, ".otf") ||
+                BLI_testextensie(relname, ".otc"))
+       {
                return FTFONTFILE;                      
-       } else if(BLI_testextensie(relname, ".btx")) {
+       }
+       else if (BLI_testextensie(relname, ".btx")) {
                return BTXFILE;
-       } else if(BLI_testextensie(relname, ".dae")) {
+       }
+       else if (BLI_testextensie(relname, ".dae")) {
                return COLLADAFILE;
-       } else if(BLI_testextensie_array(relname, imb_ext_image)
-                         || (G.have_quicktime && BLI_testextensie_array(relname, imb_ext_image_qt))) {
+       }
+       else if (BLI_testextensie_array(relname, imb_ext_image) ||
+                (G.have_quicktime && BLI_testextensie_array(relname, imb_ext_image_qt)))
+       {
                return IMAGEFILE;                       
-       } else if(BLI_testextensie_array(relname, imb_ext_movie)) {
+       }
+       else if (BLI_testextensie_array(relname, imb_ext_movie)) {
                return MOVIEFILE;                       
-       } else if(BLI_testextensie_array(relname, imb_ext_audio)) {
+       }
+       else if (BLI_testextensie_array(relname, imb_ext_audio)) {
                return SOUNDFILE;
        } 
        return 0;
@@ -768,37 +815,37 @@ static int file_extension_type(const char *relname)
 
 int ED_file_extension_icon(const char *relname)
 {
-       int type= file_extension_type(relname);
+       int type = file_extension_type(relname);
        
-       if (type == BLENDERFILE || type==BLENDERFILE_BACKUP)
+       if (type == BLENDERFILE || type == BLENDERFILE_BACKUP)
                return ICON_FILE_BLEND;
-       else if (type ==  IMAGEFILE)
+       else if (type == IMAGEFILE)
                return ICON_FILE_IMAGE;
-       else if (type ==  MOVIEFILE)
+       else if (type == MOVIEFILE)
                return ICON_FILE_MOVIE;
-       else if (type ==  PYSCRIPTFILE)
+       else if (type == PYSCRIPTFILE)
                return ICON_FILE_SCRIPT;
-       else if (type ==  SOUNDFILE) 
+       else if (type == SOUNDFILE)
                return ICON_FILE_SOUND;
-       else if (type ==  FTFONTFILE) 
+       else if (type == FTFONTFILE)
                return ICON_FILE_FONT;
-       else if (type ==  BTXFILE) 
+       else if (type == BTXFILE)
                return ICON_FILE_BLANK;
-       else if (type ==  COLLADAFILE) 
+       else if (type == COLLADAFILE)
                return ICON_FILE_BLANK;
        
        return ICON_FILE_BLANK;
 }
 
-static void filelist_setfiletypes(struct FileListfilelist)
+static void filelist_setfiletypes(struct FileList *filelist)
 {
        struct direntry *file;
        int num;
        
-       file= filelist->filelist;
+       file = filelist->filelist;
        
-       for(num=0; num<filelist->numfiles; num++, file++) {
-               file->type= file->s.st_mode;    /* restore the mess below */ 
+       for (num = 0; num < filelist->numfiles; num++, file++) {
+               file->type = file->s.st_mode;  /* restore the mess below */
                
                /* Don't check extensions for directories */ 
                if (file->type & S_IFDIR) {
@@ -806,57 +853,57 @@ static void filelist_setfiletypes(struct FileList* filelist)
                }
                file->flags = file_extension_type(file->relname);
                
-               if(filelist->filter_glob
-                  && BLI_testextensie_glob(file->relname, filelist->filter_glob)) {
-                       file->flags= OPERATORFILE;
+               if (filelist->filter_glob &&
+                   BLI_testextensie_glob(file->relname, filelist->filter_glob))
+               {
+                       file->flags = OPERATORFILE;
                }
                
        }
 }
 
-static void filelist_read_dir(struct FileListfilelist)
+static void filelist_read_dir(struct FileList *filelist)
 {
-       char wdir[FILE_MAX]= "";
+       char wdir[FILE_MAX] = "";
        if (!filelist) return;
 
        filelist->fidx = NULL;
        filelist->filelist = NULL;
 
-       BLI_current_working_dir(wdir, sizeof(wdir));     /* backup cwd to restore after */
+       BLI_current_working_dir(wdir, sizeof(wdir));  /* backup cwd to restore after */
 
        BLI_cleanup_dir(G.main->name, filelist->dir);
        filelist->numfiles = BLI_dir_contents(filelist->dir, &(filelist->filelist));
 
-       if(!chdir(wdir)) {} /* fix warning about not checking return value */
+       if (!chdir(wdir)) {} /* fix warning about not checking return value */
        filelist_setfiletypes(filelist);
        filelist_filter(filelist);
 }
 
-static void filelist_read_main(struct FileListfilelist)
+static void filelist_read_main(struct FileList *filelist)
 {
        if (!filelist) return;
        filelist_from_main(filelist);
 }
 
-static void filelist_read_library(struct FileListfilelist)
+static void filelist_read_library(struct FileList *filelist)
 {
        if (!filelist) return;
        BLI_cleanup_dir(G.main->name, filelist->dir);
        filelist_from_library(filelist);
-       if(!filelist->libfiledata) {
+       if (!filelist->libfiledata) {
                int num;
                struct direntry *file;
 
                BLI_make_exist(filelist->dir);
                filelist_read_dir(filelist);
                file = filelist->filelist;
-               for(num=0; num<filelist->numfiles; num++, file++) {
-                       if(BLO_has_bfile_extension(file->relname)) {
+               for (num = 0; num < filelist->numfiles; num++, file++) {
+                       if (BLO_has_bfile_extension(file->relname)) {
                                char name[FILE_MAX];
-                       
-                               BLI_strncpy(name, filelist->dir, sizeof(name));
-                               strcat(name, file->relname);
-                               
+
+                               BLI_join_dirfile(name, sizeof(name), filelist->dir, file->relname);
+
                                /* prevent current file being used as acceptable dir */
                                if (BLI_path_cmp(G.main->name, name) != 0) {
                                        file->type &= ~S_IFMT;
@@ -867,26 +914,26 @@ static void filelist_read_library(struct FileList* filelist)
        }
 }
 
-void filelist_readdir(struct FileListfilelist)
+void filelist_readdir(struct FileList *filelist)
 {
        filelist->readf(filelist);
 }
 
-int filelist_empty(struct FileListfilelist)
+int filelist_empty(struct FileList *filelist)
 {      
        return filelist->filelist == NULL;
 }
 
-void filelist_parent(struct FileListfilelist)
+void filelist_parent(struct FileList *filelist)
 {
        BLI_parent_dir(filelist->dir);
        BLI_make_exist(filelist->dir);
        filelist_readdir(filelist);
 }
 
-void filelist_select_file(struct FileListfilelist, int index, FileSelType select, unsigned int flag, FileCheckType check)
+void filelist_select_file(struct FileList *filelist, int index, FileSelType select, unsigned int flag, FileCheckType check)
 {
-       struct direntryfile = filelist_file(filelist, index);
+       struct direntry *file = filelist_file(filelist, index);
        if (file != NULL) {     
                int check_ok = 0; 
                switch (check) {
@@ -917,7 +964,7 @@ void filelist_select_file(struct FileList* filelist, int index, FileSelType sele
        }
 }
 
-void filelist_select(struct FileList* filelist, FileSelection* sel, FileSelType select, unsigned int flag, FileCheckType check)
+void filelist_select(struct FileList *filelist, FileSelection *sel, FileSelType select, unsigned int flag, FileCheckType check)
 {
        /* select all valid files between first and last indicated */
        if ( (sel->first >= 0) && (sel->first < filelist->numfiltered) && (sel->last >= 0) && (sel->last < filelist->numfiltered) ) {
@@ -928,9 +975,9 @@ void filelist_select(struct FileList* filelist, FileSelection* sel, FileSelType
        }
 }
 
-int    filelist_is_selected(struct FileList* filelist, int index, FileCheckType check)
+int filelist_is_selected(struct FileList *filelist, int index, FileCheckType check)
 {
-       struct direntryfile = filelist_file(filelist, index);
+       struct direntry *file = filelist_file(filelist, index);
        if (!file) {
                return 0;
        }
@@ -945,58 +992,58 @@ int       filelist_is_selected(struct FileList* filelist, int index, FileCheckType che
        }
 }
 
-void filelist_sort(struct FileListfilelist, short sort)
+void filelist_sort(struct FileList *filelist, short sort)
 {
-       switch(sort) {
-       case FILE_SORT_ALPHA:
-               qsort(filelist->filelist, filelist->numfiles, sizeof(struct direntry), compare_name);   
-               break;
-       case FILE_SORT_TIME:
-               qsort(filelist->filelist, filelist->numfiles, sizeof(struct direntry), compare_date);   
-               break;
-       case FILE_SORT_SIZE:
-               qsort(filelist->filelist, filelist->numfiles, sizeof(struct direntry), compare_size);   
-               break;
-       case FILE_SORT_EXTENSION:
-               qsort(filelist->filelist, filelist->numfiles, sizeof(struct direntry), compare_extension);      
+       switch (sort) {
+               case FILE_SORT_ALPHA:
+                       qsort(filelist->filelist, filelist->numfiles, sizeof(struct direntry), compare_name);
+                       break;
+               case FILE_SORT_TIME:
+                       qsort(filelist->filelist, filelist->numfiles, sizeof(struct direntry), compare_date);
+                       break;
+               case FILE_SORT_SIZE:
+                       qsort(filelist->filelist, filelist->numfiles, sizeof(struct direntry), compare_size);
+                       break;
+               case FILE_SORT_EXTENSION:
+                       qsort(filelist->filelist, filelist->numfiles, sizeof(struct direntry), compare_extension);
        }
 
        filelist_filter(filelist);
 }
 
 
-int filelist_islibrary(struct FileList* filelist, char* dir, char* group)
+int filelist_islibrary(struct FileList *filelist, char *dir, char *group)
 {
        return BLO_is_a_library(filelist->dir, dir, group);
 }
 
-static int groupname_to_code(char *group)
+static int groupname_to_code(const char *group)
 {
        char buf[32];
        char *lslash;
        
        BLI_strncpy(buf, group, sizeof(buf));
-       lslash= BLI_last_slash(buf);
+       lslash = BLI_last_slash(buf);
        if (lslash)
-               lslash[0]= '\0';
+               lslash[0] = '\0';
 
        return BKE_idcode_from_name(buf);
 }
  
-void filelist_from_library(struct FileListfilelist)
+void filelist_from_library(struct FileList *filelist)
 {
        LinkNode *l, *names, *previews;
-       struct ImBufima;
+       struct ImBuf *ima;
        int ok, i, nprevs, nnames, idcode;
        char filename[FILE_MAX];
        char dir[FILE_MAX], group[GROUP_MAX];   
        
        /* name test */
-       ok= filelist_islibrary(filelist, dir, group);
+       ok = filelist_islibrary(filelist, dir, group);
        if (!ok) {
                /* free */
-               if(filelist->libfiledata) BLO_blendhandle_close(filelist->libfiledata);
-               filelist->libfiledata= NULL;
+               if (filelist->libfiledata) BLO_blendhandle_close(filelist->libfiledata);
+               filelist->libfiledata = NULL;
                return;
        }
        
@@ -1005,52 +1052,54 @@ void filelist_from_library(struct FileList* filelist)
        /* there we go */
        /* for the time being only read filedata when libfiledata==0 */
        if (filelist->libfiledata == NULL) {
-               filelist->libfiledata= BLO_blendhandle_from_file(dir, NULL);
-               if(filelist->libfiledata == NULL) return;
+               filelist->libfiledata = BLO_blendhandle_from_file(dir, NULL);
+               if (filelist->libfiledata == NULL) return;
        }
        
-       idcode= groupname_to_code(group);
+       idcode = groupname_to_code(group);
 
        /* memory for strings is passed into filelist[i].relname
-        * and free'd in freefilelist */
+        * and freed in freefilelist */
        if (idcode) {
-               previews= BLO_blendhandle_get_previews(filelist->libfiledata, idcode, &nprevs);
-               names= BLO_blendhandle_get_datablock_names(filelist->libfiledata, idcode, &nnames);
+               previews = BLO_blendhandle_get_previews(filelist->libfiledata, idcode, &nprevs);
+               names = BLO_blendhandle_get_datablock_names(filelist->libfiledata, idcode, &nnames);
                /* ugh, no rewind, need to reopen */
                BLO_blendhandle_close(filelist->libfiledata);
-               filelist->libfiledata= BLO_blendhandle_from_file(dir, NULL);
+               filelist->libfiledata = BLO_blendhandle_from_file(dir, NULL);
                
-       } else {
-               previews= NULL;
-               nprevs= 0;
-               names= BLO_blendhandle_get_linkable_groups(filelist->libfiledata);
-               nnames= BLI_linklist_length(names);
+       }
+       else {
+               previews = NULL;
+               nprevs = 0;
+               names = BLO_blendhandle_get_linkable_groups(filelist->libfiledata);
+               nnames = BLI_linklist_length(names);
        }
 
-       filelist->numfiles= nnames + 1;
-       filelist->filelist= malloc(filelist->numfiles * sizeof(*filelist->filelist));
+       filelist->numfiles = nnames + 1;
+       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].relname = BLI_strdup("..");
        filelist->filelist[0].type |= S_IFDIR;
                
-       for (i=0, l= names; i<nnames; i++, l= l->next) {
-               char *blockname= l->link;
+       for (i = 0, l = names; i < nnames; i++, l = l->next) {
+               char *blockname = l->link;
 
-               filelist->filelist[i + 1].relname= BLI_strdup(blockname);
+               filelist->filelist[i + 1].relname = BLI_strdup(blockname);
                if (idcode) {
                        filelist->filelist[i + 1].type |= S_IFREG;
-               } else {
+               }
+               else {
                        filelist->filelist[i + 1].type |= S_IFDIR;
                }
        }
        
-       if(previews && (nnames != nprevs)) {
+       if (previews && (nnames != nprevs)) {
                printf("filelist_from_library: error, found %d items, %d previews\n", nnames, nprevs);
        }
-       else if(previews) {
-               for (i=0, l= previews; i<nnames; i++, l= l->next) {
-                       PreviewImage *img= l->link;
+       else if (previews) {
+               for (i = 0, l = previews; i < nnames; i++, l = l->next) {
+                       PreviewImage *img = l->link;
                        
                        if (img) {
                                unsigned int w = img->w[ICON_SIZE_PREVIEW];
@@ -1060,7 +1109,7 @@ void filelist_from_library(struct FileList* filelist)
                                /* first allocate imbuf for copying preview into it */
                                if (w > 0 && h > 0 && rect) {
                                        ima = IMB_allocImBuf(w, h, 32, IB_rect);
-                                       memcpy(ima->rect, rect, w*h*sizeof(unsigned int));
+                                       memcpy(ima->rect, rect, w * h * sizeof(unsigned int));
                                        filelist->filelist[i + 1].image = ima;
                                        filelist->filelist[i + 1].flags = IMAGEFILE;
                                }
@@ -1073,13 +1122,13 @@ void filelist_from_library(struct FileList* filelist)
 
        filelist_sort(filelist, FILE_SORT_ALPHA);
 
-       BLI_strncpy(G.main->name, filename, sizeof(filename));  // prevent G.main->name to change
+       BLI_strncpy(G.main->name, filename, sizeof(filename));  /* prevent G.main->name to change */
 
        filelist->filter = 0;
        filelist_filter(filelist);
 }
 
-void filelist_hideparent(struct FileListfilelist, short hide)
+void filelist_hideparent(struct FileList *filelist, short hide)
 {
        filelist->hide_parent = hide;
 }
@@ -1093,119 +1142,120 @@ void filelist_from_main(struct FileList *filelist)
        
        // filelist->type = FILE_MAIN; // XXXXX TODO: add modes to filebrowser
 
-       if(filelist->dir[0]=='/') filelist->dir[0]= 0;
+       if (filelist->dir[0] == '/') filelist->dir[0] = 0;
        
-       if(filelist->dir[0]) {
-               idcode= groupname_to_code(filelist->dir);
-               if(idcode==0) filelist->dir[0]= 0;
+       if (filelist->dir[0]) {
+               idcode = groupname_to_code(filelist->dir);
+               if (idcode == 0) filelist->dir[0] = 0;
        }
        
-       if( filelist->dir[0]==0) {
+       if (filelist->dir[0] == 0) {
                
                /* make directories */
-               filelist->numfiles= 24;
-               filelist->filelist= (struct direntry *)malloc(filelist->numfiles * sizeof(struct direntry));
+               filelist->numfiles = 24;
+               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));
+               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[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->filelist[23].relname= BLI_strdup("Speaker");
+               filelist->filelist[0].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->filelist[23].relname = BLI_strdup("Speaker");
                filelist_sort(filelist, FILE_SORT_ALPHA);
        }
        else {
 
                /* make files */
-               idcode= groupname_to_code(filelist->dir);
+               idcode = groupname_to_code(filelist->dir);
                
-               lb= which_libbase(G.main, idcode );
-               if(lb == NULL) return;
+               lb = which_libbase(G.main, idcode);
+               if (lb == NULL) return;
                
-               id= lb->first;
-               filelist->numfiles= 0;
-               while(id) {
+               id = lb->first;
+               filelist->numfiles = 0;
+               while (id) {
                        if (!filelist->hide_dot || id->name[2] != '.') {
                                filelist->numfiles++;
                        }
                        
-                       id= id->next;
+                       id = id->next;
                }
                
                /* XXXXX TODO: if databrowse F4 or append/link filelist->hide_parent has to be set */
-               if (!filelist->hide_parent) filelist->numfiles+= 1;
-               filelist->filelist= filelist->numfiles > 0 ? (struct direntry *)malloc(filelist->numfiles * sizeof(struct direntry)) : NULL;
+               if (!filelist->hide_parent) filelist->numfiles += 1;
+               filelist->filelist = filelist->numfiles > 0 ? (struct direntry *)malloc(filelist->numfiles * sizeof(struct direntry)) : NULL;
 
                files = filelist->filelist;
                
                if (!filelist->hide_parent) {
-                       memset( &(filelist->filelist[0]), 0 , sizeof(struct direntry));
-                       filelist->filelist[0].relname= BLI_strdup("..");
+                       memset(&(filelist->filelist[0]), 0, sizeof(struct direntry));
+                       filelist->filelist[0].relname = BLI_strdup("..");
                        filelist->filelist[0].type |= S_IFDIR;
                
                        files++;
                }
                
-               id= lb->first;
-               totlib= totbl= 0;
+               id = lb->first;
+               totlib = totbl = 0;
                
-               while(id) {
+               while (id) {
                        ok = 1;
-                       if(ok) {
+                       if (ok) {
                                if (!filelist->hide_dot || id->name[2] != '.') {
-                                       memset( files, 0 , sizeof(struct direntry));
-                                       if(id->lib==NULL)
-                                               files->relname= BLI_strdup(id->name+2);
+                                       memset(files, 0, sizeof(struct direntry));
+                                       if (id->lib == NULL) {
+                                               files->relname = BLI_strdup(id->name + 2);
+                                       }
                                        else {
-                                               files->relname= MEM_mallocN(FILE_MAX+32, "filename for lib");
-                                               sprintf(files->relname, "%s | %s", id->lib->name, id->name+2);
+                                               files->relname = MEM_mallocN(FILE_MAX + (MAX_ID_NAME - 2),     "filename for lib");
+                                               BLI_snprintf(files->relname, FILE_MAX + (MAX_ID_NAME - 2) + 3, "%s | %s", id->lib->name, id->name + 2);
                                        }
                                        files->type |= S_IFREG;
-#if 0                          // XXXXX TODO show the selection status of the objects
-                                       if(!filelist->has_func) { /* F4 DATA BROWSE */
-                                               if(idcode==ID_OB) {
-                                                       if( ((Object *)id)->flag & SELECT) files->selflag |= SELECTED_FILE;
+#if 0               /* XXXXX TODO show the selection status of the objects */
+                                       if (!filelist->has_func) { /* F4 DATA BROWSE */
+                                               if (idcode == ID_OB) {
+                                                       if ( ((Object *)id)->flag & SELECT) files->selflag |= SELECTED_FILE;
+                                               }
+                                               else if (idcode == ID_SCE) {
+                                                       if ( ((Scene *)id)->r.scemode & R_BG_RENDER) files->selflag |= SELECTED_FILE;
                                                }
-                                               else if(idcode==ID_SCE) {
-                                                       if( ((Scene *)id)->r.scemode & R_BG_RENDER) files->selflag |= SELECTED_FILE;
-                                               }                                       
                                        }
 #endif
-                                       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->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 && fake) BLI_snprintf(files->extra, sizeof(files->extra), "LF %d",    id->us);
+                                       else if (id->lib)         BLI_snprintf(files->extra, sizeof(files->extra), "L    %d",  id->us);
+                                       else if (fake)            BLI_snprintf(files->extra, sizeof(files->extra), "F    %d",  id->us);
+                                       else                      BLI_snprintf(files->extra, sizeof(files->extra), "      %d", id->us);
                                        
-                                       if(id->lib) {
-                                               if(totlib==0) firstlib= files;
+                                       if (id->lib) {
+                                               if (totlib == 0) firstlib = files;
                                                totlib++;
                                        }
                                        
@@ -1214,11 +1264,11 @@ void filelist_from_main(struct FileList *filelist)
                                totbl++;
                        }
                        
-                       id= id->next;
+                       id = id->next;
                }
                
                /* only qsort of library blocks */
-               if(totlib>1) {
+               if (totlib > 1) {
                        qsort(firstlib, totlib, sizeof(struct direntry), compare_name);
                }
        }
@@ -1228,10 +1278,10 @@ void filelist_from_main(struct FileList *filelist)
 
 static void thumbnail_joblist_free(ThumbnailJob *tj)
 {
-       FileImagelimg = tj->loadimages.first;
+       FileImage *limg = tj->loadimages.first;
        
        /* free the images not yet copied to the filelist -> these will get freed with the filelist */
-       for( ; limg; limg= limg->next) {
+       for (; limg; limg = limg->next) {
                if ((limg->img) && (!limg->done)) {
                        IMB_freeImBuf(limg->img);
                }
@@ -1241,26 +1291,28 @@ static void thumbnail_joblist_free(ThumbnailJob *tj)
 
 static void thumbnails_startjob(void *tjv, short *stop, short *do_update, float *UNUSED(progress))
 {
-       ThumbnailJob *tj= tjv;
-       FileImagelimg = tj->loadimages.first;
+       ThumbnailJob *tj = tjv;
+       FileImage *limg = tj->loadimages.first;
 
-       tj->stop= stop;
-       tj->do_update= do_update;
+       tj->stop = stop;
+       tj->do_update = do_update;
 
-       while ( (*stop==0) && (limg) ) {
-               if ( limg->flags & IMAGEFILE ) {
+       while ( (*stop == 0) && (limg) ) {
+               if (limg->flags & IMAGEFILE) {
                        limg->img = IMB_thumb_manage(limg->path, THB_NORMAL, THB_SOURCE_IMAGE);
-               } else if ( limg->flags & BLENDERFILE ) {
+               }
+               else if (limg->flags & BLENDERFILE) {
                        limg->img = IMB_thumb_manage(limg->path, THB_NORMAL, THB_SOURCE_BLEND);
-               } else if ( limg->flags & MOVIEFILE ) {
+               }
+               else if (limg->flags & MOVIEFILE) {
                        limg->img = IMB_thumb_manage(limg->path, THB_NORMAL, THB_SOURCE_MOVIE);
                        if (!limg->img) {
-                                       /* remember that file can't be loaded via IMB_open_anim */
-                                       limg->flags &= ~MOVIEFILE;
-                                       limg->flags |= MOVIEFILE_ICON;
-                               }
+                               /* remember that file can't be loaded via IMB_open_anim */
+                               limg->flags &= ~MOVIEFILE;
+                               limg->flags |= MOVIEFILE_ICON;
+                       }
                }
-               *do_update = 1;
+               *do_update = TRUE;
                PIL_sleep_ms(10);
                limg = limg->next;
        }
@@ -1268,10 +1320,10 @@ static void thumbnails_startjob(void *tjv, short *stop, short *do_update, float
 
 static void thumbnails_update(void *tjv)
 {
-       ThumbnailJob *tj= tjv;
+       ThumbnailJob *tj = tjv;
 
        if (tj->filelist && tj->filelist->filelist) {
-               FileImagelimg = tj->loadimages.first;
+               FileImage *limg = tj->loadimages.first;
                while (limg) {
                        if (!limg->done && limg->img) {
                                tj->filelist->filelist[limg->index].image = limg->img;
@@ -1280,7 +1332,7 @@ static void thumbnails_update(void *tjv)
                                        tj->filelist->filelist[limg->index].flags &= ~MOVIEFILE;
                                        tj->filelist->filelist[limg->index].flags |= MOVIEFILE_ICON;
                                }
-                               limg->done=1;
+                               limg->done = TRUE;
                        }
                        limg = limg->next;
                }
@@ -1289,28 +1341,28 @@ static void thumbnails_update(void *tjv)
 
 static void thumbnails_free(void *tjv)
 {
-       ThumbnailJob *tj= tjv;
+       ThumbnailJob *tj = tjv;
        thumbnail_joblist_free(tj);
        MEM_freeN(tj);
 }
 
 
-void thumbnails_start(struct FileList* filelist, const struct bContext* C)
+void thumbnails_start(struct FileList *filelist, const struct bContext *C)
 {
-       wmJob *steve;
+       wmJob *wm_job;
        ThumbnailJob *tj;
        int idx;
        
        /* prepare job data */
-       tj= MEM_callocN(sizeof(ThumbnailJob), "thumbnails\n");
+       tj = MEM_callocN(sizeof(ThumbnailJob), "thumbnails\n");
        tj->filelist = filelist;
-       for (idx = 0; idx < filelist->numfiles;idx++) {
+       for (idx = 0; idx < filelist->numfiles; idx++) {
                if (!filelist->filelist[idx].image) {
-                       if ( (filelist->filelist[idx].flags & (IMAGEFILE|MOVIEFILE|BLENDERFILE)) ) {
-                               FileImagelimg = MEM_callocN(sizeof(struct FileImage), "loadimage");
+                       if ( (filelist->filelist[idx].flags & (IMAGEFILE | MOVIEFILE | BLENDERFILE)) ) {
+                               FileImage *limg = MEM_callocN(sizeof(struct FileImage), "loadimage");
                                BLI_strncpy(limg->path, filelist->filelist[idx].path, FILE_MAX);
-                               limg->index= idx;
-                               limg->flags= filelist->filelist[idx].flags;
+                               limg->index = idx;
+                               limg->flags = filelist->filelist[idx].flags;
                                BLI_addtail(&tj->loadimages, limg);
                        }
                }
@@ -1319,21 +1371,22 @@ void thumbnails_start(struct FileList* filelist, const struct bContext* C)
        BKE_reports_init(&tj->reports, RPT_PRINT);
 
        /* setup job */
-       steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), filelist, "Thumbnails", 0);
-       WM_jobs_customdata(steve, tj, thumbnails_free);
-       WM_jobs_timer(steve, 0.5, NC_WINDOW, NC_WINDOW);
-       WM_jobs_callbacks(steve, thumbnails_startjob, NULL, thumbnails_update, NULL);
+       wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), filelist, "Thumbnails",
+                            0, WM_JOB_TYPE_FILESEL_THUMBNAIL);
+       WM_jobs_customdata_set(wm_job, tj, thumbnails_free);
+       WM_jobs_timer(wm_job, 0.5, NC_WINDOW, NC_WINDOW);
+       WM_jobs_callbacks(wm_job, thumbnails_startjob, NULL, thumbnails_update, NULL);
 
        /* start the job */
-       WM_jobs_start(CTX_wm_manager(C), steve);
+       WM_jobs_start(CTX_wm_manager(C), wm_job);
 }
 
-void thumbnails_stop(struct FileList* filelist, const struct bContext* C)
+void thumbnails_stop(struct FileList *filelist, const struct bContext *C)
 {
        WM_jobs_kill(CTX_wm_manager(C), filelist, NULL);
 }
 
-int thumbnails_running(struct FileList* filelist, const struct bContext* C)
+int thumbnails_running(struct FileList *filelist, const struct bContext *C)
 {
-       return WM_jobs_test(CTX_wm_manager(C), filelist);
+       return WM_jobs_test(CTX_wm_manager(C), filelist, WM_JOB_TYPE_FILESEL_THUMBNAIL);
 }