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

void
_ecdb_drive_set_capabilities(Ecdb_Drive_Info *drive,
                                 E_Hal_Device_Get_All_Properties_Return *ret)
{
   Eina_List *write_speeds, *l;
   char *val;
   int i;

   /* Write speeds */
   write_speeds = e_hal_property_strlist_get(ret, "storage.cdrom.write_speeds",
                                          NULL);
   drive->write_speeds = calloc(eina_list_count(write_speeds) + 1, sizeof(int));
   drive->write_speeds[0] = eina_list_count(write_speeds) + 1;

   i = 1;
   EINA_LIST_FOREACH(write_speeds, l, val)
   {
      drive->write_speeds[i] = atoi(val);
      i++;
   }
}

int
ecdb_update_drive_info(Ecdb_Drive_Info *drive,
                                    E_Hal_Device_Get_All_Properties_Return *ret)
{
   /* Assume that the physical hardware hasn't changed */
   FREE(drive->write_speeds);
   _ecdb_drive_set_capabilities(drive, ret);

   return TRUE;
}

int
ecdb_aquire_drive_info(E_Hal_Device_Get_All_Properties_Return *ret,
                                    char *mnt, char *udi)
{
   Ecdb_Drive_Info *drive;

   drive = calloc(1, sizeof(Ecdb_Drive_Info));
   if (!drive)
   {
      return FALSE;
   }

   /* General Info */
   drive->vendor = e_hal_property_string_get(ret, "storage.vendor", NULL);
   drive->product = e_hal_property_string_get(ret, "storage.model", NULL);
   drive->revision = e_hal_property_string_get(ret,
                                          "storage.firmware_revision", NULL);
   drive->location = mnt;
   drive->udi = udi;
   
   /* Drive Capabilities */
   drive->write_cdr = e_hal_property_bool_get(ret, "storage.cdrom.cdr", NULL);
   drive->write_cdrw = e_hal_property_bool_get(ret, "storage.cdrom.cdrw", NULL);
   drive->write_dvdr = e_hal_property_bool_get(ret, "storage.cdrom.dvdr", NULL);
   drive->write_dvdrw = e_hal_property_bool_get(ret,
                                          "storage.cdrom.dvdrw", NULL);
   drive->write_dvdram = e_hal_property_bool_get(ret,
                                          "storage.cdrom.dvdram", NULL);
   drive->write_dvdplusrw = e_hal_property_bool_get(ret,
                                          "storage.cdrom.dvdplusrw", NULL);
   drive->write_dvdplusrwdl = e_hal_property_bool_get(ret,
                                          "storage.cdrom.dvdplusrwdl", NULL);
   drive->write_dvdplusrdl = e_hal_property_bool_get(ret,
                                          "storage.cdrom.dvdplusrdl", NULL);
   drive->write_bdr = e_hal_property_bool_get(ret, "storage.cdrom.bdr", NULL);
   drive->write_bdre = e_hal_property_bool_get(ret, "storage.cdrom.bdre", NULL);
   drive->write_hddvdr = e_hal_property_bool_get(ret,
                                          "storage.cdrom.hddvdr", NULL);
   drive->write_hddvdrw = e_hal_property_bool_get(ret,
                                          "storage.cdrom.hddvdrw", NULL);
   drive->support_multisession = e_hal_property_bool_get(ret,
                                          "storage.cdrom.support_multisession",
                                          NULL);
   /* Update generic write speeds */
   _ecdb_drive_set_capabilities(drive, ret);

   em->drives = eina_list_append(em->drives, drive);
   
   return TRUE;
}

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

   EINA_LIST_FREE(list, info)
   {
      if (!info)
         continue;

      FREE(info->write_speeds);
      FREE(info->vendor);
      FREE(info->product);
      FREE(info->revision);
      FREE(info->location);
      FREE(info->udi);
      FREE(info->type);
      free(info);
   }
}

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

   em->drives = eina_list_nth_list(em->drives, 0);
   EINA_LIST_FOREACH(em->drives, l, drive)
   {
      /* Leave these as printfs, they shouldn't go the stderr */
      printf("Vendor: %s, Product: %s, Revision: %s, "
            "Location: %s\n",
            drive->vendor, drive->product,
            drive->revision, drive->location);
      printf("Write CD-R: %d, Write CD-RW: %d\n",
            drive->write_cdr,
            drive->write_cdrw);
      printf("Write DVD-R: %d, Write DVD-RW: %d, Write DVD-RAM: %d\n",
            drive->write_dvdr,
            drive->write_dvdrw,
            drive->write_dvdram);
      printf("Write DVD+R: %d, Write DVD+RW: %d, Write DVD+RWDL: %d\n",
            drive->write_dvdplusr,
            drive->write_dvdplusrw,
            drive->write_dvdplusrwdl);
      printf("Write DVD+DL: %d\n",
            drive->write_dvdplusrdl);
      printf("Write BR-R: %d, Write BR-RW: %d\n",
            drive->write_bdr,
            drive->write_bdre);
      printf("Write HDDVD-R: %d, Write HDDVD-RW: %d\n",
            drive->write_hddvdr,
            drive->write_hddvdrw);
      printf("Status: %d, Type: %s, Capacity: %llu\n",
            drive->status,
            drive->type,
            drive->capacity);

      j = drive->write_speeds[0];

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

int
ecdb_aquire_drive(Ecdb_Project *proj)
{
   char adr[BURN_DRIVE_ADR_LEN];

   if (burn_drive_convert_fs_adr(proj->drive->location, adr) <= 0)
   {
      EINA_ERROR_PWARN("Error: Address doesn't provide cd burner!\n");
      return FALSE;
   }

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

int
ecdb_set_project_drive(Ecdb_Project *proj, unsigned int idx)
{
   Ecdb_Drive_Info *info;
   
   info = eina_list_nth(em->drives, idx);
   if (!info)
   {
      EINA_ERROR_PERR("Drive index does not exist!\n");
      return FALSE;
   }

   proj->drive = info;
   return TRUE;
}

