ephoto/src/bin/ephoto_thumb_browser.c

304 lines
7.4 KiB
C

#include "ephoto.h"
#define ZOOM_MAX 512
#define ZOOM_MIN 128
#define ZOOM_STEP 32
#define TODO_ITEM_MIN_BATCH 16
typedef struct _Ephoto_Thumb_Browser Ephoto_Thumb_Browser;
struct _Ephoto_Thumb_Browser
{
Ephoto *ephoto;
Ephoto_Entry *entry;
Evas_Object *grid;
Eio_File *ls;
Eina_List *todo_items;
Eina_List *grid_items;
Eina_List *handlers;
struct {
Ecore_Animator *todo_items;
} animator;
Eina_Bool layout_deleted : 1;
};
static void
_todo_items_free(Ephoto_Thumb_Browser *tb)
{
eina_list_free(tb->todo_items);
tb->todo_items = NULL;
}
static void
_grid_items_free(Ephoto_Thumb_Browser *tb)
{
eina_list_free(tb->grid_items);
tb->grid_items = NULL;
}
static char *
_ephoto_thumb_item_label_get(void *data, Evas_Object *obj __UNUSED__, const char *part __UNUSED__)
{
Ephoto_Entry *e = data;
return strdup(e->label);
}
static Evas_Object *
_ephoto_thumb_file_icon_get(void *data, Evas_Object *obj, const char *part __UNUSED__)
{
Ephoto_Entry *e = data;
return ephoto_thumb_add(e->ephoto, obj, e->path);
}
static void
_ephoto_thumb_item_del(void *data __UNUSED__, Evas_Object *obj __UNUSED__)
{
/* FIXME: the entry is already freed when changing directories
* One solution is to take care of this cleaning when manually removing
* some grid items
Ephoto_Entry *e = data;
e->item = NULL;
*/
}
static const Elm_Gengrid_Item_Class _ephoto_thumb_file_class = {
"thumb",
{
_ephoto_thumb_item_label_get,
_ephoto_thumb_file_icon_get,
NULL,
_ephoto_thumb_item_del
}
};
static int
_entry_cmp(const void *pa, const void *pb)
{
const Ephoto_Entry *a = pa, *b = pb;
int ret, s;
s = strcmp(a->basename, b->basename);
if (s > 0)
ret = 1;
else if (s < 0)
ret = -1;
else
ret = 0;
return ret;
}
static void
_entry_item_add(Ephoto_Thumb_Browser *tb, Ephoto_Entry *e)
{
const Elm_Gengrid_Item_Class *ic;
ic = &_ephoto_thumb_file_class;
if (!tb->grid_items)
{
e->item = elm_gengrid_item_append(tb->grid, ic, e, NULL, NULL);
tb->grid_items = eina_list_append(tb->grid_items, e);
}
else
{
int near_cmp;
Ephoto_Entry *near_entry;
Elm_Gengrid_Item *near_item;
Eina_List *near_node = eina_list_search_sorted_near_list
(tb->grid_items, _entry_cmp, e, &near_cmp);
near_entry = near_node->data;
near_item = near_entry->item;
if (near_cmp < 0)
{
e->item = elm_gengrid_item_insert_after
(tb->grid, ic, e, near_item, NULL, NULL);
tb->grid_items = eina_list_append_relative_list
(tb->grid_items, e, near_node);
}
else
{
e->item = elm_gengrid_item_insert_before
(tb->grid, ic, e, near_item, NULL, NULL);
tb->grid_items = eina_list_prepend_relative_list
(tb->grid_items, e, near_node);
}
}
if (e->item)
elm_gengrid_item_data_set(e->item, e);
else
{
ERR("could not add item to grid: path '%s'", e->path);
ephoto_entry_free(e);
return;
}
}
static Eina_Bool
_todo_items_process(void *data)
{
Ephoto_Thumb_Browser *tb = data;
Ephoto_Entry *entry;
if ((tb->ls) && (eina_list_count(tb->todo_items) < TODO_ITEM_MIN_BATCH))
return EINA_TRUE;
tb->animator.todo_items = NULL;
EINA_LIST_FREE(tb->todo_items, entry)
_entry_item_add(tb, entry);
return EINA_FALSE;
}
static void
_ephoto_thumb_selected(void *data, Evas_Object *o __UNUSED__, void *event_info)
{
Ephoto_Thumb_Browser *tb = data;
Elm_Gengrid_Item *it = event_info;
Ephoto_Entry *e = elm_gengrid_item_data_get(it);
elm_gengrid_item_selected_set(it, EINA_FALSE);
ephoto_promote_single_browser(tb->ephoto, e);
}
static void
_zoom_set(Ephoto_Thumb_Browser *tb, int zoom)
{
if (zoom > ZOOM_MAX) zoom = ZOOM_MAX;
else if (zoom < ZOOM_MIN) zoom = ZOOM_MIN;
ephoto_thumb_size_set(tb->ephoto, zoom);
elm_gengrid_item_size_set(tb->grid, zoom, zoom);
}
static void
_grid_del(void *data, Evas *e __UNUSED__, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
{
Ephoto_Thumb_Browser *tb = data;
Ecore_Event_Handler *handler;
_todo_items_free(tb);
_grid_items_free(tb);
EINA_LIST_FREE(tb->handlers, handler)
ecore_event_handler_del(handler);
if (tb->animator.todo_items)
{
ecore_animator_del(tb->animator.todo_items);
tb->animator.todo_items = NULL;
}
if (tb->ls)
{
tb->layout_deleted = EINA_TRUE;
eio_file_cancel(tb->ls);
return;
}
free(tb);
}
static Eina_Bool
_ephoto_thumb_populate_start(void *data, int type __UNUSED__, void *event __UNUSED__)
{
Ephoto_Thumb_Browser *tb = data;
_todo_items_free(tb);
_grid_items_free(tb);
elm_gengrid_clear(tb->grid);
return ECORE_CALLBACK_PASS_ON;
}
static Eina_Bool
_ephoto_thumb_populate_end(void *data, int type __UNUSED__, void *event __UNUSED__)
{
Ephoto_Thumb_Browser *tb = data;
tb->ls = NULL;
if (tb->layout_deleted)
{
free(tb);
return ECORE_CALLBACK_PASS_ON;
}
return ECORE_CALLBACK_PASS_ON;
}
static Eina_Bool
_ephoto_thumb_populate_error(void *data __UNUSED__, int type __UNUSED__, void *event __UNUSED__)
{
return ECORE_CALLBACK_PASS_ON;
}
static Eina_Bool
_ephoto_thumb_entry_create(void *data, int type __UNUSED__, void *event)
{
Ephoto_Thumb_Browser *tb = data;
Ephoto_Event_Entry_Create *ev = event;
Ephoto_Entry *e;
e = ev->entry;
tb->todo_items = eina_list_append(tb->todo_items, e);
if (!tb->animator.todo_items)
tb->animator.todo_items = ecore_animator_add(_todo_items_process, tb);
return ECORE_CALLBACK_PASS_ON;
}
Evas_Object *
ephoto_thumb_browser_add(Ephoto *ephoto, Evas_Object *parent)
{
Ephoto_Thumb_Browser *tb;
tb = calloc(1, sizeof(Ephoto_Thumb_Browser));
EINA_SAFETY_ON_NULL_GOTO(tb, error);
tb->ephoto = ephoto;
tb->grid = elm_gengrid_add(parent);
EINA_SAFETY_ON_NULL_GOTO(tb->grid, error);
elm_gengrid_align_set(tb->grid, 0.5, 0.5);
elm_gengrid_bounce_set(tb->grid, EINA_FALSE, EINA_TRUE);
evas_object_size_hint_align_set
(tb->grid, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_size_hint_weight_set
(tb->grid, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_smart_callback_add
(tb->grid, "selected", _ephoto_thumb_selected, tb);
_zoom_set(tb, tb->ephoto->config->thumb_size);
evas_object_event_callback_add(tb->grid, EVAS_CALLBACK_DEL, _grid_del, tb);
evas_object_data_set(tb->grid, "thumb_browser", tb);
evas_object_show(tb->grid);
tb->handlers = eina_list_append
(tb->handlers, ecore_event_handler_add
(EPHOTO_EVENT_POPULATE_START, _ephoto_thumb_populate_start, tb));
tb->handlers = eina_list_append
(tb->handlers, ecore_event_handler_add
(EPHOTO_EVENT_POPULATE_END, _ephoto_thumb_populate_end, tb));
tb->handlers = eina_list_append
(tb->handlers, ecore_event_handler_add
(EPHOTO_EVENT_POPULATE_ERROR, _ephoto_thumb_populate_error, tb));
tb->handlers = eina_list_append
(tb->handlers, ecore_event_handler_add
(EPHOTO_EVENT_ENTRY_CREATE_THUMB, _ephoto_thumb_entry_create, tb));
return tb->grid;
error:
evas_object_del(tb->grid);
return NULL;
}
void
ephoto_thumb_browser_entry_set(Evas_Object *obj, Ephoto_Entry *entry)
{
Ephoto_Thumb_Browser *tb = evas_object_data_get(obj, "thumb_browser");
tb->entry = entry;
}