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.

1109 lines
29 KiB

#include "e.h"
typedef struct _E_Smart_Data E_Smart_Data;
typedef struct _Cache_Item Cache_Item;
typedef struct _Cache Cache;
struct _E_Smart_Data
{
Evas_Coord x, y, w, h;
Evas_Object *obj;
Evas_Object *eventarea;
const char *fdo;
const char *url;
const char *tmp_file;
Ecore_Con_Url *con_url;
Ecore_Event_Handler *url_hand;
Ecore_Timer *guessing_animation;
Ecore_Timer *timer, *fdo_reload_timer;
double last_resize;
int size;
int frame, frame_count;
int tmp_fd;
unsigned char fill_inside E_BITFIELD;
unsigned char scale_up E_BITFIELD;
unsigned char preload E_BITFIELD;
unsigned char loading E_BITFIELD;
unsigned char animated E_BITFIELD;
unsigned char invalid E_BITFIELD;
unsigned char edje E_BITFIELD;
};
struct _Cache_Item
{
unsigned int timestamp;
Evas_Object *icon, *obj;
const char *id;
Eina_List *objs;
};
struct _Cache
{
Eina_Hash *hash;
char *file;
Eet_File *ef;
Ecore_Timer *timer;
Eina_List *load_queue;
};
/* local subsystem functions */
static void _e_icon_smart_reconfigure(E_Smart_Data *sd);
static void _e_icon_smart_init(void);
static void _e_icon_smart_add(Evas_Object *obj);
static void _e_icon_smart_del(Evas_Object *obj);
static void _e_icon_smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y);
static void _e_icon_smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h);
static void _e_icon_smart_show(Evas_Object *obj);
static void _e_icon_smart_hide(Evas_Object *obj);
static void _e_icon_smart_color_set(Evas_Object *obj, int r, int g, int b, int a);
static void _e_icon_smart_clip_set(Evas_Object *obj, Evas_Object *clip);
static void _e_icon_smart_clip_unset(Evas_Object *obj);
static void _e_icon_obj_prepare(Evas_Object *obj, E_Smart_Data *sd);
static void _e_icon_preloaded(void *data, Evas *e, Evas_Object *obj, void *event_info);
/* local subsystem globals */
static Evas_Smart *_e_smart = NULL;
EINTERN int
e_icon_init(void)
{
return 1;
}
EINTERN int
e_icon_shutdown(void)
{
return 1;
}
/* externally accessible functions */
E_API Evas_Object *
e_icon_add(Evas *evas)
{
_e_icon_smart_init();
return evas_object_smart_add(evas, _e_smart);
}
static void
_e_icon_obj_prepare(Evas_Object *obj, E_Smart_Data *sd)
{
if (!sd->obj) return;
if (!sd->edje)
{
Evas_Object *pclip;
pclip = evas_object_clip_get(sd->obj);
evas_object_del(sd->obj);
sd->obj = evas_object_image_add(evas_object_evas_get(obj));
if (!sd->animated)
evas_object_image_scale_hint_set(sd->obj,
EVAS_IMAGE_SCALE_HINT_STATIC);
evas_object_smart_member_add(sd->obj, obj);
evas_object_stack_below(sd->obj, sd->eventarea);
evas_object_event_callback_add(sd->obj, EVAS_CALLBACK_IMAGE_PRELOADED,
_e_icon_preloaded, obj);
evas_object_clip_set(sd->obj, pclip);
}
}
static Eina_Bool
_frame_anim(void *data)
{
E_Smart_Data *sd = data;
double t;
int fr;
sd->frame++;
fr = (sd->frame % (sd->frame_count)) + 1;
evas_object_image_animated_frame_set(sd->obj, fr);
t = evas_object_image_animated_frame_duration_get(sd->obj, fr, 0);
sd->timer = ecore_timer_loop_add(t, _frame_anim, sd);
return EINA_FALSE;
}
static int
_handle_anim(E_Smart_Data *sd)
{
double t;
if (sd->timer) ecore_timer_del(sd->timer);
sd->timer = NULL;
sd->frame = 0;
sd->frame_count = 0;
if (!evas_object_image_animated_get(sd->obj)) return 0;
sd->frame_count = evas_object_image_animated_frame_count_get(sd->obj);
if (sd->frame_count < 2) return 0;
if (!sd->invalid) evas_object_show(sd->obj);
t = evas_object_image_animated_frame_duration_get(sd->obj, sd->frame, 0);
sd->timer = ecore_timer_loop_add(t, _frame_anim, sd);
return 1;
}
static Eina_Bool
_url_complete_cb(void *data, int type EINA_UNUSED, void *event_info)
{
Evas_Object *obj = data;
E_Smart_Data *sd = evas_object_smart_data_get(obj);
Ecore_Con_Event_Url_Complete *ev = event_info;
if (!sd) return EINA_TRUE;
if (ev->url_con != sd->con_url) return EINA_TRUE;
e_icon_file_set(obj, sd->tmp_file);
return EINA_FALSE;
}
E_API void
e_icon_file_set(Evas_Object *obj, const char *file)
{
E_Smart_Data *sd;
int len;
if (!file) return;
if (evas_object_smart_smart_get(obj) != _e_smart) SMARTERR();
if (!(sd = evas_object_smart_data_get(obj))) return;
len = strlen(file);
if ((len > 4) && (!strcasecmp(file + len - 4, ".edj")))
{
e_icon_file_edje_set(obj, file, "icon");
return;
}
if ((sd->url) && (!strcmp(sd->url, file))) return;
/* smart code here */
sd->edje = EINA_FALSE;
_e_icon_obj_prepare(obj, sd);
/* FIXME: 64x64 - unhappy about this. use icon size */
sd->loading = 0;
if (sd->fdo)
{
eina_stringshare_del(sd->fdo);
sd->fdo = NULL;
}
if (sd->url)
{
eina_stringshare_del(sd->url);
sd->url = NULL;
}
if (sd->con_url)
{
ecore_con_url_free(sd->con_url);
sd->con_url = NULL;
}
if (sd->tmp_file)
{
if (!!strcmp(file, sd->tmp_file))
{
// if we set a file that is not the tmp file - delete tmp file
ecore_file_unlink(sd->tmp_file);
eina_stringshare_del(sd->tmp_file);
sd->tmp_file = NULL;
}
}
if (sd->tmp_fd >= 0)
{
close(sd->tmp_fd);
sd->tmp_fd = -1;
}
if (sd->url_hand)
{
ecore_event_handler_del(sd->url_hand);
sd->url_hand = NULL;
}
if (sd->timer) ecore_timer_del(sd->timer);
sd->timer = NULL;
if (sd->guessing_animation) ecore_timer_del(sd->guessing_animation);
sd->guessing_animation = NULL;
sd->frame = 0;
sd->frame_count = 0;
sd->invalid = 0;
sd->edje = EINA_FALSE;
if (sd->size != 0)
evas_object_image_load_size_set(sd->obj, sd->size, sd->size);
if (sd->preload) evas_object_hide(sd->obj);
if (sd->preload)
evas_object_image_load_head_skip_set(sd->obj, EINA_TRUE);
if ((!strncasecmp(file, "http://", 7)) ||
(!strncasecmp(file, "https://", 8)))
{
char buf[4096];
snprintf(buf, sizeof(buf), "/tmp/.e-icon-tmp-XXXXXX");
sd->tmp_fd = mkstemp(buf);
if (sd->tmp_fd >= 0)
{
sd->url = eina_stringshare_add(file);
sd->tmp_file = eina_stringshare_add(buf);
sd->con_url = ecore_con_url_new(file);
ecore_con_url_fd_set(sd->con_url, sd->tmp_fd);
if (!ecore_con_url_get(sd->con_url))
{
ecore_file_unlink(buf);
close(sd->tmp_fd);
ecore_con_url_free(sd->con_url);
sd->tmp_fd = -1;
sd->con_url = NULL;
}
else
{
sd->url_hand = ecore_event_handler_add
(ECORE_CON_EVENT_URL_COMPLETE, _url_complete_cb, obj);
}
}
return;
}
else
evas_object_image_file_set(sd->obj, file, NULL);
// if (evas_object_image_load_error_get(sd->obj) != EVAS_LOAD_ERROR_NONE)
// return EINA_FALSE;
if (!_handle_anim(sd))
{
if (sd->preload)
{
sd->loading = 1;
evas_object_image_preload(sd->obj, EINA_FALSE);
}
else if (evas_object_visible_get(obj))
{
evas_object_show(sd->obj);
}
}
_e_icon_smart_reconfigure(sd);
}
E_API void
e_icon_file_key_set(Evas_Object *obj, const char *file, const char *key)
{
E_Smart_Data *sd;
if (evas_object_smart_smart_get(obj) != _e_smart) SMARTERR();
if (!(sd = evas_object_smart_data_get(obj))) return;
/* smart code here */
sd->loading = 0;
if (sd->fdo)
{
eina_stringshare_del(sd->fdo);
sd->fdo = NULL;
}
if (sd->url)
{
eina_stringshare_del(sd->url);
sd->url = NULL;
}
if (sd->con_url)
{
ecore_con_url_free(sd->con_url);
sd->con_url = NULL;
}
if (sd->tmp_file)
{
ecore_file_unlink(sd->tmp_file);
eina_stringshare_del(sd->tmp_file);
sd->tmp_file = NULL;
}
if (sd->tmp_fd >= 0)
{
close(sd->tmp_fd);
sd->tmp_fd = -1;
}
if (sd->url_hand)
{
ecore_event_handler_del(sd->url_hand);
sd->url_hand = NULL;
}
if (sd->timer) ecore_timer_del(sd->timer);
sd->timer = NULL;
if (sd->guessing_animation) ecore_timer_del(sd->guessing_animation);
sd->guessing_animation = NULL;
sd->frame = 0;
sd->frame_count = 0;
sd->invalid = 0;
sd->edje = EINA_FALSE;
_e_icon_obj_prepare(obj, sd);
if (sd->size != 0)
evas_object_image_load_size_set(sd->obj, sd->size, sd->size);
if (sd->preload) evas_object_hide(sd->obj);
if (sd->preload)
evas_object_image_load_head_skip_set(sd->obj, EINA_TRUE);
evas_object_image_file_set(sd->obj, file, key);
// if (evas_object_image_load_error_get(sd->obj) != EVAS_LOAD_ERROR_NONE)
// return EINA_FALSE;
if (!_handle_anim(sd))
{
if (sd->preload)
{
sd->loading = 1;
evas_object_image_preload(sd->obj, EINA_FALSE);
}
else if (evas_object_visible_get(obj))
evas_object_show(sd->obj);
}
_e_icon_smart_reconfigure(sd);
}
E_API void
e_icon_edje_object_set(Evas_Object *obj, Evas_Object *edje)
{
E_Smart_Data *sd;
if (evas_object_smart_smart_get(obj) != _e_smart) SMARTERR();
if (!(sd = evas_object_smart_data_get(obj))) return;
/* smart code here */
if (sd->obj) evas_object_del(sd->obj);
sd->loading = 0;
if (sd->fdo)
{
eina_stringshare_del(sd->fdo);
sd->fdo = NULL;
}
if (sd->url)
{
eina_stringshare_del(sd->url);
sd->url = NULL;
}
if (sd->con_url)
{
ecore_con_url_free(sd->con_url);
sd->con_url = NULL;
}
if (sd->tmp_file)
{
ecore_file_unlink(sd->tmp_file);
eina_stringshare_del(sd->tmp_file);
sd->tmp_file = NULL;
}
if (sd->tmp_fd >= 0)
{
close(sd->tmp_fd);
sd->tmp_fd = -1;
}
if (sd->url_hand)
{
ecore_event_handler_del(sd->url_hand);
sd->url_hand = NULL;
}
if (sd->timer) ecore_timer_del(sd->timer);
sd->timer = NULL;
if (sd->guessing_animation) ecore_timer_del(sd->guessing_animation);
sd->guessing_animation = NULL;
sd->frame = 0;
sd->frame_count = 0;
sd->invalid = 0;
sd->edje = EINA_TRUE;
sd->obj = edje;
if (evas_object_visible_get(obj)) evas_object_show(sd->obj);
evas_object_smart_member_add(sd->obj, obj);
_e_icon_smart_reconfigure(sd);
}
E_API Evas_Object *
e_icon_edje_get(Evas_Object *obj)
{
E_Smart_Data *sd;
if (evas_object_smart_smart_get(obj) != _e_smart) SMARTERR(NULL);
if (!(sd = evas_object_smart_data_get(obj)))
return NULL;
return sd->edje ? sd->obj : NULL;
}
E_API void
e_icon_file_edje_set(Evas_Object *obj, const char *file, const char *part)
{
E_Smart_Data *sd;
if (evas_object_smart_smart_get(obj) != _e_smart) SMARTERR();
if (!(sd = evas_object_smart_data_get(obj))) return;
/* smart code here */
if (sd->obj) evas_object_del(sd->obj);
sd->loading = 0;
if (sd->fdo)
{
eina_stringshare_del(sd->fdo);
sd->fdo = NULL;
}
if (sd->url)
{
eina_stringshare_del(sd->url);
sd->url = NULL;
}
if (sd->con_url)
{
ecore_con_url_free(sd->con_url);
sd->con_url = NULL;
}
if (sd->tmp_file)
{
ecore_file_unlink(sd->tmp_file);
eina_stringshare_del(sd->tmp_file);
sd->tmp_file = NULL;
}
if (sd->tmp_fd >= 0)
{
close(sd->tmp_fd);
sd->tmp_fd = -1;
}
if (sd->url_hand)
{
ecore_event_handler_del(sd->url_hand);
sd->url_hand = NULL;
}
if (sd->timer) ecore_timer_del(sd->timer);
sd->timer = NULL;
if (sd->guessing_animation) ecore_timer_del(sd->guessing_animation);
sd->guessing_animation = NULL;
sd->frame = 0;
sd->frame_count = 0;
sd->invalid = 0;
sd->edje = EINA_TRUE;
sd->obj = edje_object_add(evas_object_evas_get(obj));
edje_object_file_set(sd->obj, file, part);
// if (edje_object_load_error_get(sd->obj) != EDJE_LOAD_ERROR_NONE)
// return EINA_FALSE;
evas_object_smart_member_add(sd->obj, obj);
evas_object_stack_below(sd->obj, sd->eventarea);
if (evas_object_visible_get(obj)) evas_object_show(sd->obj);
_e_icon_smart_reconfigure(sd);
}
E_API void
e_icon_fdo_icon_set(Evas_Object *obj, const char *icon)
{
E_Smart_Data *sd;
const char *path;
int len;
if (!icon) return;
if (evas_object_smart_smart_get(obj) != _e_smart) SMARTERR();
if (icon[0] == '/')
{
e_icon_file_set(obj, icon);
return;
}
if (!(sd = evas_object_smart_data_get(obj))) return;
if (sd->timer) ecore_timer_del(sd->timer);
sd->timer = NULL;
if (sd->guessing_animation) ecore_timer_del(sd->guessing_animation);
sd->guessing_animation = NULL;
sd->frame = 0;
sd->frame_count = 0;
sd->invalid = 0;
sd->edje = EINA_FALSE;
eina_stringshare_replace(&sd->fdo, icon);
if (!sd->fdo) return;
path = efreet_icon_path_find(e_config->icon_theme, sd->fdo, sd->size);
if (!path)
{
if (e_util_strcmp(e_config->icon_theme, "hicolor"))
path = efreet_icon_path_find("hicolor", sd->fdo, sd->size);
if (!path) return;
}
len = strlen(icon);
if ((len > 4) && (!strcasecmp(icon + len - 4, ".edj")))
{
e_icon_file_edje_set(obj, path, "icon");
return;
}
/* smart code here */
_e_icon_obj_prepare(obj, sd);
sd->loading = 0;
if (sd->size != 0)
evas_object_image_load_size_set(sd->obj, sd->size, sd->size);
if (sd->preload) evas_object_hide(sd->obj);
if (sd->preload)
evas_object_image_load_head_skip_set(sd->obj, EINA_TRUE);
evas_object_image_file_set(sd->obj, path, NULL);
// if (evas_object_image_load_error_get(sd->obj) != EVAS_LOAD_ERROR_NONE)
// return EINA_FALSE;
if (sd->preload)
{
sd->loading = 1;
evas_object_image_preload(sd->obj, 0);
}
else if (evas_object_visible_get(obj))
evas_object_show(sd->obj);
_e_icon_smart_reconfigure(sd);
return;
}
E_API void
e_icon_image_object_set(Evas_Object *obj, Evas_Object *o)
{
E_Smart_Data *sd;
const char *str;
if (evas_object_smart_smart_get(obj) != _e_smart) SMARTERR();
if (!(sd = evas_object_smart_data_get(obj))) return;
str = evas_object_type_get(o);
if ((!str) || strcmp(str, "image"))
CRI(EINA_COLOR_RED "******************\ntrying to set an image object of type '%s'! this is not what you want!\n******************\n"EINA_COLOR_RESET, str);
if (sd->timer) ecore_timer_del(sd->timer);
sd->timer = NULL;
if (sd->guessing_animation) ecore_timer_del(sd->guessing_animation);
sd->guessing_animation = NULL;
sd->frame = 0;
sd->frame_count = 0;
sd->invalid = 0;
sd->edje = EINA_FALSE;
/* smart code here */
if (sd->obj) evas_object_del(sd->obj);
sd->loading = 0;
sd->obj = o;
evas_object_smart_member_add(sd->obj, obj);
evas_object_stack_below(sd->obj, sd->eventarea);
if (evas_object_visible_get(obj)) evas_object_show(sd->obj);
_handle_anim(sd);
_e_icon_smart_reconfigure(sd);
}
E_API Eina_Bool
e_icon_file_get(const Evas_Object *obj, const char **file, const char **group)
{
E_Smart_Data *sd;
if (evas_object_smart_smart_get(obj) != _e_smart) SMARTERR(EINA_FALSE);
if ((!file) && (!group)) return EINA_FALSE;
if (file) *file = NULL;
if (group) *group = NULL;
if (!(sd = evas_object_smart_data_get(obj))) return EINA_FALSE;
if (sd->edje)
{
edje_object_file_get(sd->obj, file, group);
return file || group;
}
evas_object_image_file_get(sd->obj, file, group);
return file || group;
}
E_API void
e_icon_smooth_scale_set(Evas_Object *obj, Eina_Bool smooth)
{
E_Smart_Data *sd;
if (evas_object_smart_smart_get(obj) != _e_smart) SMARTERRNR();
if (!(sd = evas_object_smart_data_get(obj))) return;
if (sd->edje) return;
evas_object_image_smooth_scale_set(sd->obj, smooth);
}
E_API Eina_Bool
e_icon_smooth_scale_get(const Evas_Object *obj)
{
E_Smart_Data *sd;
if (evas_object_smart_smart_get(obj) != _e_smart) SMARTERR(0);
if (!(sd = evas_object_smart_data_get(obj))) return EINA_FALSE;
if (sd->edje)
return EINA_FALSE;
return evas_object_image_smooth_scale_get(sd->obj);
}
E_API void
e_icon_alpha_set(Evas_Object *obj, Eina_Bool alpha)
{
E_Smart_Data *sd;
if (evas_object_smart_smart_get(obj) != _e_smart) SMARTERRNR();
if (!(sd = evas_object_smart_data_get(obj))) return;
if (sd->edje) return;
evas_object_image_alpha_set(sd->obj, alpha);
}
E_API Eina_Bool
e_icon_alpha_get(const Evas_Object *obj)
{
E_Smart_Data *sd;
if (evas_object_smart_smart_get(obj) != _e_smart) SMARTERR(0);
if (!(sd = evas_object_smart_data_get(obj))) return EINA_FALSE;
if (sd->edje) return EINA_FALSE;
return evas_object_image_alpha_get(sd->obj);
}
E_API void
e_icon_preload_set(Evas_Object *obj, Eina_Bool preload)
{
E_Smart_Data *sd;
if (evas_object_smart_smart_get(obj) != _e_smart) SMARTERRNR();
if (!(sd = evas_object_smart_data_get(obj))) return;