int AddFileIfItIsAPicture(char *thedirectory,char *subdir,wxString *filename,unsigned int count_only)
{
      if (filename->Length()<=4) { fprintf(stderr,"Filenames must be bigger than .jpg for example\n"); return 0; }

       //char filename_str[1024];
       //strncpy(filename_str,(const char*) filename->mb_str(wxConvUTF8),1024);
       //fprintf(stderr,"AddFileIfItIsAPicture called for %s , %s , %s \n",thedirectory,subdir,filename_str);

      wxString dir_and_filename; dir_and_filename.clear();
      dir_and_filename<<_U2(thedirectory);
      if (thedirectory[strlen(thedirectory)-1]!='/') { dir_and_filename<<_U2("/"); }
      dir_and_filename<<*filename;

      wxFileName fname(dir_and_filename);
      wxString extension=fname.GetExt();
      wxString fullname =fname.GetFullName();

      if (!fname.FileExists( )) { fprintf(stderr,"Not a file ..!\n"); }
          else
     // if (fname.DirExists( )) { fprintf(stderr," Not a directory ..!\n"); }
     //     else
      if ( ExtensionIsPicture(&extension) )
        {
          if (!count_only)
          {
            if ( pictures_count >= list_size )  { /*Our list cannot acommodate any more data error*/ return 0; }

            strncpy(list[pictures_count].filename,(const char*) fullname.mb_str(wxConvUTF8),256);
            if (subdir==0) { strncpy(list[pictures_count].subdir,"\0",1024);   } else
                           { strncpy(list[pictures_count].subdir,subdir,1024); }

            wxDateTime mod_time=fname.GetModificationTime();
            list[pictures_count].year=mod_time.GetYear();
            list[pictures_count].month=mod_time.GetMonth();
            list[pictures_count].day=mod_time.GetDay();
            list[pictures_count].day_of_year=mod_time.GetDayOfYear();
            list[pictures_count].hour=mod_time.GetHour();
            list[pictures_count].minute=mod_time.GetMinute();
            list[pictures_count].second=mod_time.GetSecond();

            if (ExtensionIsJPEGPicture(&extension)) { list[pictures_count].is_jpeg=1; } else
                                                    { list[pictures_count].is_jpeg=0; }

            wxULongLong size_long_long = fname.GetSize();
            list[pictures_count].filesize=(unsigned int) size_long_long.ToULong();

            ++pictures_count;
          }
          return 1;
        }
  return 0;
}
unsigned int GetDirectoryList(char * thedirectory,char *subdir,unsigned int space_to_allocate,unsigned int comp_func,unsigned int asc_desc,unsigned int randomized,unsigned int recursive)
{
    if (FORCE_STOP_LISTING) {
        fprintf(stderr,"Emergency stop listing\n");
        return 0;
    }

    unsigned int this_list_total_pictures_count=0;
    unsigned int count_only=0,bottom_level=0;
    if ( recursive <= 1 ) {
        bottom_level=1;
    }
    if (space_to_allocate==0) {
        count_only=1;
    }
    else if ((space_to_allocate!=0)&&(list==0)) {
        fprintf(stderr,"List not allocated for GetDirectoryList call\n");
        return 0;
    }

    //if (recursive>MAX_RECURSION) { fprintf(stderr,"Recursion has reached a depth of 100 levels , stopping it :P \n"); return 0; } else
    if (bottom_level)
    {
        gettimeofday(&start_time,0x0);
        strcpy(root_directory,thedirectory);
    }

    wxDir dir(_U2(thedirectory));
    if ( !dir.IsOpened() )  {
        fprintf(stderr,"Cannot open dir `%s`\n",thedirectory);
        return 0;
    }
    wxString dirname,filename,extension,fullname,filespec = wxT("*.*");
    int cont = dir.GetFirst(&filename,filespec,wxDIR_FILES|wxDIR_HIDDEN);
    while ( cont )
    {
        if ( AddFileIfItIsAPicture(thedirectory,subdir,&filename,count_only) ) {
            ++this_list_total_pictures_count;
        }
        /* WE CONTINUE THE LOOP UNTIL THE END!*/
        cont = dir.GetNext(&filename);
    }



    gettimeofday(&current_time,0x0);
    unsigned int curtime=timeval_diff2(&difference_time,&current_time,&start_time)/1000;
    //If recursion is enabled , and we timed out , lets ask the user if he wants to continue
    if ( (curtime>MAX_TIME_FOR_OPERATION_MS) && (recursive!=0) )
    {
        if (UserWantsToStop()) {
            /*To stop directory listing , we stop recursion here and now..! and set the force stop flag*/
            FORCE_STOP_LISTING=1;
            recursive=0;
        } else
        {
            //Compensate for the time lost waiting for user input.. :P
            gettimeofday(&current_time,0x0);
            curtime=(timeval_diff2(&difference_time,&current_time,&start_time)/1000 )-curtime;
            MAX_TIME_FOR_OPERATION_MS=curtime+=(MAX_TIME_FOR_OPERATION_MS*2);
        }
    }

    if (recursive>0) //Recursion Enabled
    {   /* RECURSIVE FUNCTIONALITY START*/
        filespec.clear();
        if ( recursive == 1 ) {
            fprintf(stderr,"Starting Recursive dir listing @ %s \n",thedirectory);
        }
        char recursive_dir[1024]= {0},new_subdir[1024]= {0},dirname_cstr[1024]= {0};
        wxDir dir2(_U2(thedirectory));
        if (dir2.IsOpened())
        {
            int cont = dir2.GetFirst(&dirname,filespec,wxDIR_DIRS);
            while ( cont )
            {
                strncpy(dirname_cstr,(const char*) dirname.mb_str(wxConvUTF8),1024);
                if ( ( strlen(dirname_cstr)==1 )&&(dirname_cstr[0]=='/') ) { } else if ( ( strlen(dirname_cstr)==1 )&&(dirname_cstr[0]=='.') ) { } else if ( ( strlen(dirname_cstr)==2 )&&(dirname_cstr[0]=='.')&&(dirname_cstr[1]=='.') ) { } else if ( strlen(dirname_cstr)>0 )
                {   //NO POINT IN RECURSING IN THE SAME (PRACTICLY) DIR :P
                    ensure_ending_with_slash(dirname_cstr);

                    //fprintf(stderr,"WAS The Directory is `%s\n` ",thedirectory);
                    //fprintf(stderr,"WAS Running Recursive (level %u) addition to dir `%s`\n",recursive,recursive_dir);
                    //fprintf(stderr,"WAS Subdir is `%s`\n",new_subdir);

                    sprintf(recursive_dir,"%s%s",thedirectory,dirname_cstr);

                    if ( subdir == 0 )
                    {
                        sprintf(new_subdir,"%s",dirname_cstr);
                    }
                    else
                    {
                        ensure_ending_with_slash(subdir);
                        sprintf(new_subdir,"%s%s",subdir,dirname_cstr);
                    }

                    //fprintf(stderr,"Running Recursive (level %u) addition to dir `%s`\n",recursive,recursive_dir);
                    //fprintf(stderr,"Subdir is `%s`\n",new_subdir);
                    unsigned int recursive_pictures_added=GetDirectoryList(recursive_dir,new_subdir,space_to_allocate,comp_func,asc_desc,randomized,recursive+1);
                    //fprintf(stderr," returned %u ( %u until now ) pictures\n",recursive_pictures_added,this_list_total_pictures_count);

                    this_list_total_pictures_count+=recursive_pictures_added;
                }
                /* WE CONTINUE THE LOOP UNTIL THE END!*/
                cont = dir2.GetNext(&dirname);
            }
        }
        if (bottom_level) {
            fprintf(stderr,"Recursive dir listing is done\n");
        }
        /* RECURSIVE FUNCTIONALITY END */
    }

    if (bottom_level)
    {
        gettimeofday(&current_time,0x0);
        last_list_time_ms=timeval_diff2(&difference_time,&current_time,&start_time)/1000;
        fprintf(stderr,"Getting directory list took %u ms\n",(unsigned int) last_list_time_ms);
    }

    if (count_only)
    {
        return this_list_total_pictures_count;
    } else
    {
        if ( (this_list_total_pictures_count>1)&&(bottom_level))
        {
            if (randomized)
            {
                fprintf(stderr,"Randomizing directory list.. ");
                RandomizeDirectoryListSorting(0,this_list_total_pictures_count);
            }   else
            {
                fprintf(stderr,"Sorting directory modification time.. ");
                SortDirectoryList(0,this_list_total_pictures_count,comp_func,asc_desc);
            }
            fprintf(stderr,"done\n");

            //PrintDirectoryList();
        }
    }

    return this_list_total_pictures_count;
}