/* vim: set sw=3 ts=3 sts=3 expandtab: */
#include "ecdb.h"

int
ecdb_aquire_drive_info(void)
{
   int ret, i;
   unsigned int drive_num;
   struct burn_drive_info *drives_current;
   struct burn_speed_descriptor *speeds;

   ret = 0;
   if (em->drives)
   {
      ecdb_drive_info_list_free(em->drives);
   }

   while (!ret)
   {
      ret = burn_drive_scan(&drives_current, &drive_num);
   }

   for (i = 0; i < drive_num; i++)
   {
      Ecdb_Drive_Info *drive;

      drive = calloc(1, sizeof(Ecdb_Drive_Info));

      if (!drive)
      {
         return FALSE;
      }

      drive->product = eina_stringshare_add(drives_current[i].product);
      drive->vendor = eina_stringshare_add(drives_current[i].vendor);
      drive->revision = eina_stringshare_add(drives_current[i].revision);
      drive->location = strdup(drives_current[i].location);
      drive->read_dvdram = drives_current[i].read_dvdram;
      drive->read_dvdr = drives_current[i].read_dvdr;
      drive->read_dvdrom = drives_current[i].read_dvdrom;
      drive->read_cdr = drives_current[i].read_cdr;
      drive->read_cdrw = drives_current[i].read_cdrw;
      drive->write_dvdram = drives_current[i].write_dvdram;
      drive->write_dvdr = drives_current[i].write_dvdr;
      drive->write_cdr = drives_current[i].write_cdr;
      drive->write_simulate = drives_current[i].write_simulate;

      burn_drive_get_speedlist(drives_current[i].drive, &speeds);

      drive->profile_name = eina_stringshare_add(speeds->profile_name);
      drive->profile_loaded = speeds->profile_loaded;

      if (burn_drive_grab(drives_current[i].drive, 1))
      {
         drive->capacity = burn_disc_available_space(drives_current[i].drive,
                                                                        NULL);
         burn_drive_release(drives_current[i].drive, 0);
      }

      while (speeds->next)
      {
         i++;
         speeds = speeds->next;
      }

      drive->read_speeds = calloc(i + 2, sizeof(int));
      drive->write_speeds = calloc(i + 2, sizeof(int));

      drive->read_speeds[0] = i + 1;
      drive->write_speeds[0] = i + 1;

      for (ret = 1; ret <= i; ret++)
      {
         drive->write_speeds[ret] = speeds->write_speed;
         drive->read_speeds[ret] = speeds->read_speed;
         speeds = speeds->prev;
      }

      burn_drive_free_speedlist(&speeds);
      em->drives = eina_list_append(em->drives, drive);
   }

   burn_drive_info_free(drives_current);

   return TRUE;
}

void
ecdb_drive_info_list_free(Eina_List *list)
{
   Ecdb_Drive_Info *info;
   Eina_List *l;

   EINA_LIST_FOREACH(list, l, info)
   {
      FREE(info->read_speeds);
      FREE(info->write_speeds);
      FREE(info->location);
      eina_stringshare_del(info->vendor);
      eina_stringshare_del(info->product);
      eina_stringshare_del(info->revision);
      eina_stringshare_del(info->profile_name);
      FREE(info);
   }

   list = eina_list_free(list);
}

void
ecdb_print_drive_info(void)
{
   Eina_List *l;
   Ecdb_Drive_Info *drive;
   int j, i, h;

   em->drives = eina_list_nth_list(em->drives, 0);
   EINA_LIST_FOREACH(em->drives, l, drive)
   {
      printf("Vendor: %s, Product: %s, Revision: %s, "
            "Location: %s\n",
            drive->vendor, drive->product,
            drive->revision, drive->location);
      printf("Profile name: %s, Loaded: %d\n",
            drive->profile_name,
            drive->profile_loaded);
      printf("Read DVDRAM: %d, Read DVDR: %d, Read DVDROM: %d, "
            "Read CDR: %d, Read CDRW: %d\n",
            drive->read_dvdram,
            drive->read_dvdr,
            drive->read_dvdrom,
            drive->read_cdr,
            drive->read_cdrw);
      printf("Write DVDRAM: %d, Write DVDR: %d, Write CDR: %d, "
            "Write Simulate: %d\n",
            drive->write_dvdram,
            drive->write_dvdr,
            drive->write_cdr,
            drive->write_simulate);

      j = drive->read_speeds[0];

      for (i = 1; i < j; i ++)
      {
         if ((h = drive->write_speeds[i]))
         {
            printf("Write: %d\n", h);
         }
         if ((h = drive->read_speeds[i]))
         {
            printf("Read: %d\n", h);
         }
      }
   }
}

int
ecdb_aquire_drive(Ecdb_Project *proj, unsigned int idx)
{
   Ecdb_Drive_Info *info;
   char adr[BURN_DRIVE_ADR_LEN];

   info = eina_list_nth(em->drives, idx);
   if (burn_drive_convert_fs_adr(info->location, adr) <= 0)
   {
      printf("Error: Address doesn't provide cd burner!\n");
      return FALSE;
   }

   if (burn_drive_scan_and_grab(&info->tangible, adr, 1) > 0)
   {
      proj->drive = info;
      return TRUE;
   }
   else
   {
      info->tangible = NULL;
      return FALSE;
   }
}
   
