#ifndef GLOBAL_DECL
#include "global.h"     // Global data types and variables
#endif

#ifndef BROWSER_DECL
#include "browser.h"
#endif


browser::browser()  // Constructor
{
  intLine = 0;
  intLevel = 0;
  strcpy(menu_title[0], "Home Folder");
  pad_dash_to_eol(menu_title[0]);
  build_menu(HOME_DIR);  // Build the root menu
  update = 1;
}

int browser::get_path(char* strPath)
{
  int n;

  strcpy(strPath, HOME_DIR);
  if (intLevel > 0)
  {
    for(n=1;n<=intLevel;n++)
    {
      strcat(strPath, "/");
      strcat(strPath, menu_title[n]);
    }
  }
  return(0);
}

int browser::go_up(void)
{
  if (intLine > 0)
  {
    intLine--;
    update = 1;
  }
  return(0);
}

int browser::go_down(void)
{
  if (intLine < intLineCount-1)
  {
    intLine++;
    update = 1;
  }
  return(0);
}

int browser::leave_subdir(void)
{
  char strPath[500];

  if (intLevel > 0)
  {
    intLevel--;
    intLine = 0;
    // Rebuild the menu
    get_path(strPath);
    build_menu(strPath);
    update = 1;
  }
  return(0);
}

int browser::is_file(void)
{
  if ((menu_text[intLine][0] == '>') || (menu_text[intLine][0] == '='))
    return(0);
  else
    return(1);
}

int browser::is_directory(void)
{
  if (menu_text[intLine][0] == '>')
    return(1);
  else
    return(0);
}

int browser::is_special(void)
{
  if (menu_text[intLine][0] == '=')
    return(1);
  else
    return(0);
}

int browser::get_current(char* strCurrent)
{
  strcpy(strCurrent, menu_text[intLine]);
  return(0);
}

int browser::get_length(void)
{
  return strlen(menu_text[intLine]);
}

int browser::enter_subdir(void)
{
  char  strPath[500];
  char* strTemp;

  /* Enter subdirectory */
  strTemp = &menu_text[intLine][2];
  intLevel++;
  intLine = 0;
  strcpy(menu_title[intLevel], strTemp);
  // Rebuild the menu
  get_path(strPath);
  build_menu(strPath);
  update = 1;
  return(0);
}

int browser::display_menu(int intScrollPos)
{
  char strTemp[500];

  if (update == 1)
  {
    update = 0;
    send_text(menu_title[intLevel], 1, 0);
    if (is_mp3(menu_text[intLine]))
    {
      strcpy(strTemp, menu_text[intLine]);
      strTemp[strlen(menu_text[intLine]) - 4] = '\0';
      send_text(strTemp, 2, intScrollPos);
    }
    else
    {
      send_text(menu_text[intLine], 2, intScrollPos);
    }
  }

  return(0);
}

int browser::build_menu(char* strDirectory)
{
  int           has_files = 0;
  DIR           *dir_p;
  struct dirent *dir_entry_p;
  struct stat   stat_p;
  int           menu_line = 0;
  char          strFullFname[500];

  /* Open the directory           */
  dir_p = opendir(strDirectory);

  #if DIRDEBUG
  printf("\n\nEntering Directory Path: %s", strDirectory);
  #endif

  /************ LIST SUB-DIRECTORIES ************/

  /* read each entry until NULL.          */
  while( NULL != (dir_entry_p = readdir(dir_p)))
  {
    /* print the name of the directories held in
    * this directory entry.                */

    strcpy(strFullFname, strDirectory);
    strcat(strFullFname, "/");
    strcat(strFullFname, dir_entry_p->d_name);
    stat (strFullFname, &stat_p);

    #if DIRDEBUG
    printf("\n  - File Scanned: %s", dir_entry_p->d_name);
    #endif

    if ((S_ISDIR(stat_p.st_mode)) && (dir_entry_p->d_name[0] != '.'))
    {
      #if DIRDEBUG
      printf("  Directory");
      #endif

      strcpy(menu_text[menu_line], "> ");
      strcat(menu_text[menu_line], dir_entry_p->d_name);
      menu_line++;
    }
    else if ((!S_ISDIR(stat_p.st_mode)) && (dir_entry_p->d_name[0] != '.'))
    {
      #if DIRDEBUG
      printf("  File");
      #endif
      if (is_mp3(dir_entry_p->d_name))
      {
        has_files = 1;
        #if DIRDEBUG
        printf(" + Is an mp3");
        #endif
      }
    }
  }
  /* Tidy up.                             */
  closedir(dir_p);

  if (has_files == 1)
  {
    /************ LIST COMMANDS ************/
    strcpy(menu_text[menu_line], "= Ordered Play");
    menu_line++;
    strcpy(menu_text[menu_line], "= Random Play");
    menu_line++;
    /************ LIST FILES ************/
    dir_p = opendir(strDirectory);
    /* read each entry until NULL.          */
    while( NULL != (dir_entry_p = readdir(dir_p)))
    {
      /* print the name of the file held in
      * this directory entry.                */
      strcpy(strFullFname, strDirectory);
      strcat(strFullFname, "/");
      strcat(strFullFname, dir_entry_p->d_name);
      stat (strFullFname, &stat_p);

      if (S_ISREG(stat_p.st_mode))  // check if it is a file
      {
        if (is_mp3(dir_entry_p->d_name))  // check if it is an mp3
        {
          strcpy(menu_text[menu_line], dir_entry_p->d_name);
          menu_line++;
        }
      }
    }
    closedir(dir_p);  // Tidy up
  }
  intLineCount = menu_line;
  return(0);
}


syntax highlighted by Code2HTML, v. 0.9.1