You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2021 lines
59 KiB

#include "ephoto.h"
#include <elm_interface_scrollable.h>
#define ZOOM_MAX 512
#define ZOOM_MIN 128
#define ZOOM_STEP 32
#define FILESEP "file://"
#define FILESEP_LEN sizeof(FILESEP) - 1
#define TODO_ITEM_MIN_BATCH 5
#define DRAG_TIMEOUT 0.3
#define ANIM_TIME 0.2
static Eina_Bool _5s_cancel = EINA_FALSE;
static Ecore_Timer *_5s_timeout = NULL;
typedef struct _Ephoto_Thumb_Browser Ephoto_Thumb_Browser;
struct _Ephoto_Thumb_Browser
{
Ephoto *ephoto;
Evas_Object *main;
Evas_Object *table;
Evas_Object *gridbox;
Evas_Object *grid;
Evas_Object *original_grid;
Evas_Object *nolabel;
Evas_Object *search;
Evas_Object *menu;
Elm_Object_Item *last_sel;
Ephoto_Sort sort;
Eio_File *ls;
Eina_Bool dirs_only;
Eina_Bool thumbs_only;
Eina_List *cut_items;
Eina_List *copy_items;
Eina_List *handlers;
Eina_List *todo_items;
Eina_List *entries;
Eina_List *searchentries;
double totsize;
double totsize_old;
int totimages;
int totimages_old;
Eina_Bool dragging;
Eina_Bool searching;
Eina_Bool processing;
struct
{
Ecore_Animator *todo_items;
int count;
int processed;
} animator;
Eina_Bool main_deleted:1;
};
/*Item Classes*/
static Elm_Gengrid_Item_Class _ephoto_thumb_file_class;
/*Main Callbacks*/
static void _ephoto_show_settings(void *data, Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED);
static void _ephoto_main_key_down(void *data, Evas *e EINA_UNUSED,
Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED);
static void _ephoto_main_del(void *data, Evas *e EINA_UNUSED,
Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED);
/*Thumb Pane Functions*/
static void _ephoto_thumb_activated(void *data, Evas_Object *obj EINA_UNUSED,
void *event_info);
static void _ephoto_thumb_zoom_set(Ephoto_Thumb_Browser *tb, int zoom);
static void _ephoto_thumb_search_go(void *data, Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED);
static void _ephoto_thumb_search_cancel(void *data, Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED);
static void _ephoto_thumb_search_start(void *data, Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED);
/*Common Callbacks*/
static void
_menu_dismissed_cb(void *data, Evas_Object *obj,
void *event_info EINA_UNUSED)
{
Ephoto_Thumb_Browser *tb = data;
evas_object_del(obj);
elm_object_focus_set(tb->main, EINA_TRUE);
}
static void
_menu_empty_cb(void *data, Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED)
{
Ephoto_Thumb_Browser *tb = data;
Eina_List *paths = NULL;
Elm_Object_Item *item;
Ephoto_Entry *file;
item = elm_gengrid_first_item_get(tb->grid);
while (item)
{
file = elm_object_item_data_get(item);
paths = eina_list_append(paths, strdup(file->path));
item = elm_gengrid_item_next_get(item);
}
if (eina_list_count(paths) <= 0)
return;
ephoto_file_empty_trash(tb->ephoto, paths);
}
static Eina_Bool
_5s_timeout_gone(void *data)
{
elm_drag_cancel(data);
_5s_timeout = NULL;
return ECORE_CALLBACK_CANCEL;
}
static void
_dnd_drag_start(void *data EINA_UNUSED, Evas_Object *obj)
{
Ephoto_Thumb_Browser *tb = evas_object_data_get(obj, "thumb_browser");
if (_5s_cancel)
_5s_timeout = ecore_timer_add(5.0, _5s_timeout_gone, obj);
elm_object_cursor_set(tb->main, ELM_CURSOR_HAND2);
ephoto_show_folders(tb->ephoto, EINA_FALSE);
tb->dragging = 1;
}
static void
_dnd_drag_done(void *data EINA_UNUSED, Evas_Object *obj,
Eina_Bool doaccept EINA_UNUSED)
{
Ephoto_Thumb_Browser *tb = evas_object_data_get(obj, "thumb_browser");
if (_5s_cancel)
{
ecore_timer_del(_5s_timeout);
_5s_timeout = NULL;
}
eina_list_free(data);
elm_object_cursor_unset(tb->main);
tb->dragging = 0;
return;
}
static const char *
_dnd_drag_data_build(Eina_List **items)
{
const char *drag_data = NULL;
if (*items)
{
Eina_List *l;
Elm_Object_Item *it;
Ephoto_Entry *e;
unsigned int len = 0;
EINA_LIST_FOREACH(*items, l, it)
{
e = elm_object_item_data_get(it);
if (e->path)
len += strlen(e->path);
}
drag_data =
malloc(len + eina_list_count(*items) * (FILESEP_LEN + 1) + 1);
strcpy((char *) drag_data, "");
EINA_LIST_FOREACH(*items, l, it)
{
e = elm_object_item_data_get(it);
if (e->path)
{
strcat((char *) drag_data, FILESEP);
strcat((char *) drag_data, e->path);
strcat((char *) drag_data, "\n");
}
}
}
return drag_data;
}
static Evas_Object *
_dnd_create_icon(void *data, Evas_Object *win, Evas_Coord *xoff,
Evas_Coord *yoff)
{
Evas_Object *icon = NULL;
Evas_Object *o = elm_object_item_part_content_get(data, "elm.swallow.icon");
if (o)
{
int xm, ym, w = 30, h = 30;
const char *f;
const char *g;
elm_image_file_get(o, &f, &g);
evas_pointer_canvas_xy_get(evas_object_evas_get(o), &xm, &ym);
if (xoff)
*xoff = xm - (w / 2);
if (yoff)
*yoff = ym - (h / 2);
icon = elm_icon_add(win);
elm_image_file_set(icon, f, g);
evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND,
EVAS_HINT_EXPAND);
if (xoff && yoff)
evas_object_move(icon, *xoff, *yoff);
evas_object_resize(icon, w, h);
}
return icon;
}
static const char *
_dnd_get_drag_data(Evas_Object *obj, Elm_Object_Item *it, Eina_List **items)
{
const char *drag_data = NULL;
*items = eina_list_clone(elm_gengrid_selected_items_get(obj));
if (it)
{
if (!elm_gengrid_item_selected_get(it))
*items = eina_list_append(*items, it);
}
drag_data = _dnd_drag_data_build(items);
return drag_data;
}
static Elm_Object_Item *
_dnd_item_get(Evas_Object *obj, Evas_Coord x, Evas_Coord y, int *xposret,
int *yposret)
{
Elm_Object_Item *item;
item = elm_gengrid_at_xy_item_get(obj, x, y, xposret, yposret);
return item;
}
static Eina_Bool
_dnd_item_data_get(Evas_Object *obj, Elm_Object_Item *it,
Elm_Drag_User_Info *info)
{
info->format = ELM_SEL_FORMAT_TARGETS;
info->createicon = _dnd_create_icon;
info->createdata = it;
info->dragstart = _dnd_drag_start;
info->icons = NULL;
info->dragdone = _dnd_drag_done;
info->data = _dnd_get_drag_data(obj, it, (Eina_List **) & info->donecbdata);
info->acceptdata = info->donecbdata;
if (info->data)
return EINA_TRUE;
else
return EINA_FALSE;
}
/*Thumb Pane Callbacks*/
static char *
_thumb_item_text_get(void *data, Evas_Object *obj EINA_UNUSED,
const char *part EINA_UNUSED)
{
Ephoto_Entry *e = data;
return strdup(e->label);
}
static Evas_Object *
_thumb_file_icon_get(void *data, Evas_Object *obj,
const char *part)
{
Ephoto_Entry *e = data;
Evas_Object *thumb = NULL;
if (strcmp(part, "elm.swallow.icon"))
return NULL;
if (e)
{
thumb = ephoto_thumb_add(e->ephoto, obj, e->path);
evas_object_show(thumb);
}
return thumb;
}
static void
_thumb_item_del(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED)
{
/* The entry is already freed when changing directories. */
}
static int
_entry_cmp_grid_alpha_asc(const void *pa, const void *pb)
{
const Ephoto_Entry *a, *b;
a = elm_object_item_data_get(pa);
b = elm_object_item_data_get(pb);
return strcasecmp(a->basename, b->basename);
}
static int
_entry_cmp_grid_alpha_desc(const void *pa, const void *pb)
{
const Ephoto_Entry *a, *b;
int i;
a = elm_object_item_data_get(pa);
b = elm_object_item_data_get(pb);
i = strcasecmp(a->basename, b->basename);
if (i < 0)
i = 1;
else if (i > 0)
i = -1;
return i;
}
static int
_entry_cmp_grid_mod_asc(const void *pa, const void *pb)
{
const Ephoto_Entry *a, *b;
long long moda, modb;
a = elm_object_item_data_get(pa);
b = elm_object_item_data_get(pb);
moda = ecore_file_mod_time(a->path);
modb = ecore_file_mod_time(b->path);
if (moda < modb)
return -1;
else if (moda > modb)
return 1;
else
return strcasecmp(a->basename, b->basename);
}
static int
_entry_cmp_grid_mod_desc(const void *pa, const void *pb)
{
const Ephoto_Entry *a, *b;
long long moda, modb;
a = elm_object_item_data_get(pa);
b = elm_object_item_data_get(pb);
moda = ecore_file_mod_time(a->path);
modb = ecore_file_mod_time(b->path);
if (moda < modb)
return 1;
else if (moda > modb)
return -1;
else
{
int i;
i = strcasecmp(a->basename, b->basename);
if (i < 0)
i = 1;
else if (i > 0)
i = -1;
return i;
}
}
static void
_sort_alpha_asc(void *data, Evas_Object *obj,
void *event_data EINA_UNUSED)
{
Ephoto_Thumb_Browser *tb = data;
Evas_Object *ic;
tb->sort = EPHOTO_SORT_ALPHABETICAL_ASCENDING;
tb->thumbs_only = 1;
tb->dirs_only = 0;
ic = elm_icon_add(obj);
elm_icon_standard_set(ic, "view-sort-ascending");
elm_object_part_content_set(obj, "icon", ic);
evas_object_show(ic);
ephoto_directory_set(tb->ephoto, tb->ephoto->config->directory,
NULL, tb->dirs_only, tb->thumbs_only);
}
static void
_sort_alpha_desc(void *data, Evas_Object *obj EINA_UNUSED,
void *event_data EINA_UNUSED)
{
Ephoto_Thumb_Browser *tb = data;
Evas_Object *ic;
tb->sort = EPHOTO_SORT_ALPHABETICAL_DESCENDING;
tb->thumbs_only = 1;
tb->dirs_only = 0;
ic = elm_icon_add(obj);
elm_icon_standard_set(ic, "view-sort-descending");
elm_object_part_content_set(obj, "icon", ic);
evas_object_show(ic);
ephoto_directory_set(tb->ephoto, tb->ephoto->config->directory,
NULL, tb->dirs_only, tb->thumbs_only);
}
static void
_sort_mod_asc(void *data, Evas_Object *obj EINA_UNUSED,
void *event_data EINA_UNUSED)
{
Ephoto_Thumb_Browser *tb = data;
Evas_Object *ic;
tb->sort = EPHOTO_SORT_MODTIME_ASCENDING;
tb->thumbs_only = 1;
tb->dirs_only = 0;
ic = elm_icon_add(obj);
elm_icon_standard_set(ic, "view-sort-ascending");
elm_object_part_content_set(obj, "icon", ic);
evas_object_show(ic);
ephoto_directory_set(tb->ephoto, tb->ephoto->config->directory,
NULL, tb->dirs_only, tb->thumbs_only);
}
static void
_sort_mod_desc(void *data, Evas_Object *obj EINA_UNUSED,
void *event_data EINA_UNUSED)
{
Ephoto_Thumb_Browser *tb = data;
Evas_Object *ic;
tb->sort = EPHOTO_SORT_MODTIME_DESCENDING;
tb->thumbs_only = 1;
tb->dirs_only = 0;
ic = elm_icon_add(obj);
elm_icon_standard_set(ic, "view-sort-descending");
elm_object_part_content_set(obj, "icon", ic);
evas_object_show(ic);
ephoto_directory_set(tb->ephoto, tb->ephoto->config->directory,
NULL, tb->dirs_only, tb->thumbs_only);
}
static void
_zoom_in(void *data, Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED)
{
Ephoto_Thumb_Browser *tb = data;
int zoom = tb->ephoto->config->thumb_size + ZOOM_STEP;
_ephoto_thumb_zoom_set(tb, zoom);
}
static void
_zoom_out(void *data, Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED)
{
Ephoto_Thumb_Browser *tb = data;
int zoom = tb->ephoto->config->thumb_size - ZOOM_STEP;
_ephoto_thumb_zoom_set(tb, zoom);
}
static void
_view_single(void *data, Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED)
{
Ephoto_Thumb_Browser *tb = data;
Elm_Object_Item *it = elm_gengrid_selected_item_get(tb->grid);
Ephoto_Entry *entry;
Eina_List *selected, *s;
Elm_Object_Item *item;
if (it)
entry = elm_object_item_data_get(it);
else
entry = eina_list_nth(tb->entries, 0);
selected =
eina_list_clone(elm_gengrid_selected_items_get(tb->grid));
if (eina_list_count(selected) <= 1 && tb->searchentries)
{
if (tb->ephoto->selentries)
eina_list_free(tb->ephoto->selentries);
tb->ephoto->selentries = NULL;
tb->ephoto->searchentries =
eina_list_clone(tb->searchentries);
}
else if (eina_list_count(selected) > 1)
{
EINA_LIST_FOREACH(selected, s, item)
{
tb->ephoto->selentries =
eina_list_append(tb->ephoto->selentries,
elm_object_item_data_get(item));
}
}
else
{
if (tb->ephoto->selentries)
eina_list_free(tb->ephoto->selentries);
if (tb->ephoto->searchentries)
eina_list_free(tb->ephoto->searchentries);
tb->ephoto->selentries = NULL;
tb->ephoto->searchentries = NULL;
}
if (entry)
{
evas_object_smart_callback_call(tb->main, "view", entry);
}
}
static void
_grid_menu_select_all_cb(void *data, Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED)
{
Ephoto_Thumb_Browser *tb = data;
Elm_Object_Item *item;
item = elm_gengrid_first_item_get(tb->grid);
while (item)
{
elm_gengrid_item_selected_set(item, EINA_TRUE);
item = elm_gengrid_item_next_get(item);
}
}
static void
_grid_menu_clear_cb(void *data, Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED)
{
Ephoto_Thumb_Browser *tb = data;
Elm_Object_Item *item;
item = elm_gengrid_first_item_get(tb->grid);
while (item)
{
elm_gengrid_item_selected_set(item, EINA_FALSE);
item = elm_gengrid_item_next_get(item);
}
}
static void
_grid_menu_cut_cb(void *data, Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED)
{
Ephoto_Thumb_Browser *tb = data;
Eina_List *selection =
eina_list_clone(elm_gengrid_selected_items_get(tb->grid));
Eina_List *f;
Elm_Object_Item *item;
Ephoto_Entry *file;
if (eina_list_count(selection) <= 0)
return;
if (tb->cut_items)
{
eina_list_free(tb->cut_items);
tb->cut_items = NULL;
}
if (tb->copy_items)
{
eina_list_free(tb->copy_items);
tb->copy_items = NULL;
}
EINA_LIST_FOREACH(selection, f, item)
{
file = elm_object_item_data_get(item);
tb->cut_items = eina_list_append(tb->cut_items, strdup(file->path));
}
eina_list_free(selection);
}
static void
_grid_menu_copy_cb(void *data, Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED)
{
Ephoto_Thumb_Browser *tb = data;
Eina_List *selection =
eina_list_clone(elm_gengrid_selected_items_get(tb->grid));
Eina_List *f;
Elm_Object_Item *item;
Ephoto_Entry *file;
if (eina_list_count(selection) <= 0)
return;
if (tb->cut_items)
{
eina_list_free(tb->cut_items);
tb->cut_items = NULL;
}
if (tb->copy_items)
{
eina_list_free(tb->copy_items);
tb->copy_items = NULL;
}
EINA_LIST_FOREACH(selection, f, item)
{
file = elm_object_item_data_get(item);
tb->copy_items = eina_list_append(tb->copy_items, strdup(file->path));
}
eina_list_free(selection);
}
static void
_grid_menu_paste_cb(void *data, Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED)
{
Ephoto_Thumb_Browser *tb = data;
if (eina_list_count(tb->cut_items) > 0)
{
ephoto_file_paste(tb->ephoto, eina_list_clone(tb->cut_items), EINA_FALSE,
tb->ephoto->config->directory);
eina_list_free(tb->cut_items);
tb->cut_items = NULL;
}
else if (eina_list_count(tb->copy_items) > 0)
{
ephoto_file_paste(tb->ephoto, eina_list_clone(tb->copy_items), EINA_TRUE,
tb->ephoto->config->directory);
eina_list_free(tb->copy_items);
tb->copy_items = NULL;
}
}
static void
_grid_menu_rename_cb(void *data, Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED)
{
Elm_Object_Item *item = data;
Ephoto_Thumb_Browser *tb = evas_object_data_get(item, "thumb_browser");
Ephoto_Entry *file;
file = elm_object_item_data_get(item);
ephoto_file_rename(tb->ephoto, file->path);
evas_object_data_del(item, "thumb_browser");
}
static void
_grid_menu_delete_cb(void *data, Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED)
{
Ephoto_Thumb_Browser *tb = data;
Eina_List *paths = NULL, *f;
Eina_List *selection =
eina_list_clone(elm_gengrid_selected_items_get(tb->grid));
Elm_Object_Item *item;
Ephoto_Entry *file;
if (eina_list_count(selection) <= 0)
return;
EINA_LIST_FOREACH(selection, f, item)
{
file = elm_object_item_data_get(item);
if (ecore_file_exists(file->path))
paths = eina_list_append(paths, strdup(file->path));
}
ephoto_file_delete(tb->ephoto, paths, EINA_FILE_REG);
eina_list_free(selection);
}
static void
_grid_mouse_up_cb(void *data, Evas *e EINA_UNUSED,
Evas_Object *obj EINA_UNUSED, void *event_info)
{
Ephoto_Thumb_Browser *tb = data;
Evas_Object *menu;
Elm_Object_Item *item;
Evas_Event_Mouse_Up *info = event_info;