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

int
ecdb_shutdown(void *data, int type, void *event)
{
   FREE(em->theme_path);
   FREE(em->engine);

   if (em->drives)
   {
      ecdb_drive_info_list_free(em->drives);
   }

   if (em->evas_dnd_candidates)
   {
      eina_list_free(em->evas_dnd_candidates);
   }

   if (em->ewl_dnd_candidates)
   {
      eina_list_free(em->ewl_dnd_candidates);
   }

   ecdb_hal_shutdown();

   FREE(em);
   
   if (!ecore_file_recursive_rm("/tmp/ecdb"))
   {
      EINA_ERROR_PWARN("Removal of temporary directory failed!\n");
   }

   ecore_main_loop_quit();
   return FALSE;
}

int
ecdb_burn_init(void)
{
   if (!burn_initialize())
   {
      return 0;
   }

   burn_msgs_set_severities("NEVER", "SORRY", "ecdb: ");
   burn_set_signal_handling("ecdb: ", NULL, 0);

   return 1;
}

int
ecdb_image_init(void)
{
   if (!iso_init())
   {
      return 0;
   }
   iso_set_msgs_severities("NEVER", "SORRY", "ecdb: ");

   return 1;
}

int
ecdb_match_keyword(const char *chk, const char *key, int len)
{
   int i;

   for (i = 0; i < len; i++)
   {
      if (chk[i] != key[i])
      {
         return TRUE;
      }
   }

   return FALSE;
}

char *
ecdb_strip_string(const char *strip)
{
   char *t1 = (char *)strip;

   do
   {
      if (*t1 == ' ')
      {
         t1++;
         return strdup(t1);
      }
   } while ((t1) && (t1++));

   return NULL;
}

char *
ecdb_strip_next_argument(const char *strip)
{
   char *t1 = (char *)strip, *t2;
   char *ret = NULL;
   int len = 0, space = FALSE;

   do
   {
      if (*t1 == ' ')
      {
         space = TRUE;
      }

      if ((*t1 != ' ') && (space == TRUE))
      {
         t2 = t1;

         /* Find length of string to copy */
         while ((*t2) && (*t2 != ' '))
         {
            len++;
            t2++;
         }

         /* Given no more args */
         if (!len)
         {
            return NULL;
         }
         else
         {
            len++;
         }

         /* Make a new string and copy everything over */
         ret = malloc(sizeof(char) * len);
         memcpy(ret, t1, len);
         return ret;
      }
   } while (*(t1++));

   return NULL;
}

int
ecdb_dnd_position(void *data, int type, void *event)
{
   Eina_List *l;
   Ecore_X_Event_Xdnd_Position *ev;
   Evas_Object *o;
   Ewl_Widget *ew;
   Ecore_X_Rectangle rect;
   Evas_Coord window_x, window_y, window_w, window_h, x, y, w, h;

   ev = event;
   if (ev->win != ecore_evas_software_x11_window_get(em->main_win_ee))
   {
      return 1;
   }

   ecore_evas_geometry_get(em->main_win_ee, &window_x, &window_y, &window_w,
                                                                  &window_h);
   em->evas_dnd_candidates = eina_list_nth_list(em->evas_dnd_candidates, 0);
   EINA_LIST_FOREACH(em->evas_dnd_candidates, l, o)
   {
      if (evas_object_visible_get(o))
      {
         evas_object_geometry_get(o, &x, &y, &w, &h);
         x += window_x;
         y += window_y;

         if ((ev->position.x >= x) && (ev->position.x <= (x + w))
               && (ev->position.y >= y) && (ev->position.y <= (y + h)))
         {
            rect.x = x;
            rect.y = y;
            rect.width = w;
            rect.height = h;
            em->evas_drop_object = o;
            em->ewl_drop_object = NULL;
            ecore_x_dnd_send_status(1, 1, rect, ECORE_X_DND_ACTION_PRIVATE);
            return 1;
         }
      }
   }

   em->ewl_dnd_candidates = eina_list_nth_list(em->ewl_dnd_candidates, 0);
   EINA_LIST_FOREACH(em->ewl_dnd_candidates, l, ew)
   {
      ewl_object_current_geometry_get(EWL_OBJECT(ew), &x, &y, &w, &h);
      x += window_x;
      y += window_y;

      if ((ev->position.x >= x) && (ev->position.x <= (x + w))
            && (ev->position.y >= y) && (ev->position.y <= (y + h)))
      {
         rect.x = x;
         rect.y = y;
         rect.width = w;
         rect.height = h;
         em->ewl_drop_object = ew;
         em->evas_drop_object = NULL;
         ecore_x_dnd_send_status(1, 1, rect, ECORE_X_DND_ACTION_PRIVATE);
         return 1;
      }
   }

   rect.x = window_x;
   rect.y = window_y;
   rect.width = window_w;
   rect.height = window_h;
   ecore_x_dnd_send_status(0, 1, rect, ECORE_X_DND_ACTION_PRIVATE);

   return 1;
}

int
ecdb_dnd_drop(void *data, int type, void *event)
{
   if ((em->evas_drop_object) || (em->ewl_drop_object))
   {
      ecore_x_selection_xdnd_request
         (ecore_evas_software_x11_window_get(em->main_win_ee),
         "text/uri-list");
   }
   return 1;
}

int
ecdb_dnd_selection(void *data, int type, void *event)
{
   Ecore_X_Event_Selection_Notify *ev;
   Ecore_X_Selection_Data *d;
   Ecore_X_Selection_Data_Files *files;
   void (*call_func)(Evas_Object *, Ecore_X_Selection_Data_Files *) = NULL;

   ev = event;
   if (((!em->evas_drop_object) && (!em->ewl_drop_object)) ||
         (ev->selection != ECORE_X_SELECTION_XDND) || (!(files = ev->data)) ||
         (!(d = ev->data)) || (files->num_files <= 0))
   {
      ecore_x_dnd_send_finished();
      return 1;
   }

   if (em->evas_drop_object)
   {
      call_func = evas_object_data_get(em->evas_drop_object, "dnd_call_func");
      if (call_func != NULL)
      {
         call_func(em->evas_drop_object, files);
      }
   }
   else if (em->ewl_drop_object)
   {
      Ewl_Event_Dnd_Data_Received ewl_ev;

      ewl_ev.type = ev->target;
      ewl_ev.data = files->files;
      ewl_ev.len = files->num_files;
      ewl_ev.format = d->format;
      ewl_callback_call_with_event_data(em->ewl_drop_object,
                                 EWL_CALLBACK_DND_DATA_RECEIVED, &ewl_ev);
   }

   em->evas_drop_object = NULL;
   em->ewl_drop_object = NULL;
   ecore_x_dnd_send_finished();
   return 1;
}


// For use with the entry group, where only one file is valid.  We take the
// first one and ignore the rest (if any)
void
ecdb_dnd_entry_dnd_set(Evas_Object *o, Ecore_X_Selection_Data_Files *files)
{
   Efreet_Uri *uri;

   if ((files) && (files->files))
   {
      uri = efreet_uri_decode(files->files[0]);
      ecdb_entry_text_set(o, uri->path);
      efreet_uri_free(uri);
   }
}

int
ecdb_speed_convert(Ecdb_Drive_Info *drive, int sel)
{
   int count, num_speeds, i;

   count = 1;
   sel++;
   num_speeds = drive->write_speeds[0];
   for (i = 0; i < num_speeds; i++)
   {
      if (drive->write_speeds[i] <= 0)
         continue;
      if (count == sel)
         return drive->write_speeds[count];
      count++;
   }

   return -1;
}

