3 EAPI int E_EVENT_FM_OP_REGISTRY_ADD = 0;
4 EAPI int E_EVENT_FM_OP_REGISTRY_DEL = 0;
5 EAPI int E_EVENT_FM_OP_REGISTRY_CHANGED = 0;
7 static Eina_Hash *_e_fm2_op_registry = NULL;
8 static unsigned int _e_fm2_init_count = 0;
10 typedef struct _E_Fm2_Op_Registry_Entry_Listener E_Fm2_Op_Registry_Entry_Listener;
11 typedef struct _E_Fm2_Op_Registry_Entry_Internal E_Fm2_Op_Registry_Entry_Internal;
13 struct _E_Fm2_Op_Registry_Entry_Listener
16 void (*cb)(void *data, const E_Fm2_Op_Registry_Entry *entry);
18 void (*free_data)(void *data);
21 struct _E_Fm2_Op_Registry_Entry_Internal
23 E_Fm2_Op_Registry_Entry entry;
24 Eina_Inlist *listeners;
26 Ecore_Event *changed_event;
30 _e_fm2_op_registry_entry_e_fm_deleted(void *data, Evas *evas __UNUSED__, Evas_Object *e_fm __UNUSED__, void *event __UNUSED__)
32 E_Fm2_Op_Registry_Entry *entry = data;
35 e_fm2_op_registry_entry_changed(entry);
39 _e_fm2_op_registry_entry_e_fm_monitor_start(const E_Fm2_Op_Registry_Entry *entry)
41 if (!entry->e_fm) return;
42 evas_object_event_callback_add
43 (entry->e_fm, EVAS_CALLBACK_DEL,
44 _e_fm2_op_registry_entry_e_fm_deleted, entry);
48 _e_fm2_op_registry_entry_e_fm_monitor_stop(const E_Fm2_Op_Registry_Entry *entry)
50 if (!entry->e_fm) return;
51 evas_object_event_callback_del_full
52 (entry->e_fm, EVAS_CALLBACK_DEL,
53 _e_fm2_op_registry_entry_e_fm_deleted, entry);
56 static inline E_Fm2_Op_Registry_Entry_Internal *
57 _e_fm2_op_registry_entry_internal_get(const E_Fm2_Op_Registry_Entry *entry)
59 return (E_Fm2_Op_Registry_Entry_Internal *)entry;
63 _e_fm2_op_registry_entry_internal_free(E_Fm2_Op_Registry_Entry_Internal *e)
65 _e_fm2_op_registry_entry_e_fm_monitor_stop(&(e->entry));
69 E_Fm2_Op_Registry_Entry_Listener *listener = (void *)e->listeners;
70 e->listeners = eina_inlist_remove(e->listeners, e->listeners);
72 if (listener->free_data) listener->free_data(listener->data);
76 eina_stringshare_del(e->entry.src);
77 eina_stringshare_del(e->entry.dst);
82 _e_fm2_op_registry_entry_internal_unref(E_Fm2_Op_Registry_Entry_Internal *e)
84 if (e->references < 1)
88 if (e->references > 0)
91 _e_fm2_op_registry_entry_internal_free(e);
96 _e_fm2_op_registry_entry_internal_ref(E_Fm2_Op_Registry_Entry_Internal *e)
103 _e_fm2_op_registry_entry_listeners_call(const E_Fm2_Op_Registry_Entry_Internal *e)
105 E_Fm2_Op_Registry_Entry_Listener *listener;
108 if (eina_inlist_count(e->listeners) < 1) return;
110 EINA_INLIST_FOREACH_SAFE (e->listeners, l, listener)
111 listener->cb(listener->data, &e->entry);
115 _e_fm2_op_registry_entry_internal_unref_on_event(void *data, void *event __UNUSED__)
117 E_Fm2_Op_Registry_Entry_Internal *e = data;
118 _e_fm2_op_registry_entry_internal_unref(e);
122 _e_fm2_op_registry_entry_internal_event(E_Fm2_Op_Registry_Entry_Internal *e, int event_type)
124 _e_fm2_op_registry_entry_internal_ref(e);
125 ecore_event_add(event_type, &(e->entry),
126 _e_fm2_op_registry_entry_internal_unref_on_event, e);
130 e_fm2_op_registry_entry_add(int id, Evas_Object *e_fm, E_Fm_Op_Type op, E_Fm2_Op_Registry_Abort_Func abrt)
132 E_Fm2_Op_Registry_Entry_Internal *e;
134 e = E_NEW(E_Fm2_Op_Registry_Entry_Internal, 1);
138 e->entry.e_fm = e_fm;
139 e->entry.start_time = ecore_loop_time_get();
141 e->entry.status = E_FM2_OP_STATUS_IN_PROGRESS;
142 e->entry.func.abort = abrt;
145 if (!eina_hash_add(_e_fm2_op_registry, &id, e))
151 _e_fm2_op_registry_entry_e_fm_monitor_start(&(e->entry));
152 _e_fm2_op_registry_entry_internal_event(e, E_EVENT_FM_OP_REGISTRY_ADD);
158 e_fm2_op_registry_entry_del(int id)
160 E_Fm2_Op_Registry_Entry_Internal *e;
162 e = eina_hash_find(_e_fm2_op_registry, &id);
164 eina_hash_del_by_key(_e_fm2_op_registry, &id);
166 _e_fm2_op_registry_entry_internal_event(e, E_EVENT_FM_OP_REGISTRY_DEL);
167 _e_fm2_op_registry_entry_internal_unref(e);
173 _e_fm2_op_registry_entry_internal_unref_on_changed_event(void *data, void *event __UNUSED__)
175 E_Fm2_Op_Registry_Entry_Internal *e = data;
176 e->changed_event = NULL;
177 _e_fm2_op_registry_entry_internal_unref(e);
181 e_fm2_op_registry_entry_changed(const E_Fm2_Op_Registry_Entry *entry)
183 E_Fm2_Op_Registry_Entry_Internal *e;
186 e = _e_fm2_op_registry_entry_internal_get(entry);
188 _e_fm2_op_registry_entry_listeners_call(e);
190 if (e->changed_event) return;
191 _e_fm2_op_registry_entry_internal_ref(e);
192 e->changed_event = ecore_event_add
193 (E_EVENT_FM_OP_REGISTRY_CHANGED, &(e->entry),
194 _e_fm2_op_registry_entry_internal_unref_on_changed_event, e);
198 * Set the new e_fm for this operation.
200 * Use this call instead of directly setting in order to have the
201 * object to be monitored, when it is gone, the pointer will be made
204 * @note: it will not call any listener or add any event, please use
205 * e_fm2_op_registry_entry_changed().
208 e_fm2_op_registry_entry_e_fm_set(E_Fm2_Op_Registry_Entry *entry, Evas_Object *e_fm)
211 _e_fm2_op_registry_entry_e_fm_monitor_stop(entry);
213 _e_fm2_op_registry_entry_e_fm_monitor_start(entry);
217 * Set the new files for this operation.
219 * Use this call instead of directly setting in order to have
220 * stringshare references right.
222 * @note: it will not call any listener or add any event, please use
223 * e_fm2_op_registry_entry_changed().
226 e_fm2_op_registry_entry_files_set(E_Fm2_Op_Registry_Entry *entry, const char *src, const char *dst)
230 eina_stringshare_replace(&entry->src, src);
231 eina_stringshare_replace(&entry->dst, dst);
235 * Adds a reference to given entry.
237 * @return: new number of references after operation or -1 on error.
240 e_fm2_op_registry_entry_ref(E_Fm2_Op_Registry_Entry *entry)
242 E_Fm2_Op_Registry_Entry_Internal *e;
244 if (!entry) return -1;
246 e = _e_fm2_op_registry_entry_internal_get(entry);
247 return _e_fm2_op_registry_entry_internal_ref(e);
251 * Releases a reference to given entry.
253 * @return: new number of references after operation or -1 on error,
254 * if 0 the entry was freed and pointer is then invalid.
257 e_fm2_op_registry_entry_unref(E_Fm2_Op_Registry_Entry *entry)
259 E_Fm2_Op_Registry_Entry_Internal *e;
261 if (!entry) return -1;
263 e = _e_fm2_op_registry_entry_internal_get(entry);
264 return _e_fm2_op_registry_entry_internal_unref(e);
268 * Returns the X window associated to this operation.
270 * This will handle all bureaucracy to get X window based on e_fm evas
273 * @return: 0 if no window, window identifier otherwise.
276 e_fm2_op_registry_entry_xwin_get(const E_Fm2_Op_Registry_Entry *entry)
281 if (!entry) return 0;
282 if (!entry->e_fm) return 0;
284 e = evas_object_evas_get(entry->e_fm);
287 ee = evas_data_attach_get(e);
290 return (Ecore_X_Window)(long)ecore_evas_window_get(ee);
294 * Discover entry based on its identifier.
296 * @note: does not increment reference.
298 EAPI E_Fm2_Op_Registry_Entry *
299 e_fm2_op_registry_entry_get(int id)
301 return eina_hash_find(_e_fm2_op_registry, &id);
305 * Adds a function to be called when entry changes.
307 * When entry changes any attribute this function will be called.
309 * @param: entry entry to operate on.
310 * @param: cb function to callback on changes.
311 * @param: data extra data to give to @p cb
312 * @param: free_data function to call when listener is removed, entry
313 * is deleted or any error occur in this function and listener
316 * @note: does not increment reference.
317 * @note: on errors, @p free_data will be called.
320 e_fm2_op_registry_entry_listener_add(E_Fm2_Op_Registry_Entry *entry, void (*cb)(void *data, const E_Fm2_Op_Registry_Entry *entry), const void *data, void (*free_data)(void *data))
322 E_Fm2_Op_Registry_Entry_Internal *e;
323 E_Fm2_Op_Registry_Entry_Listener *listener;
326 if ((!entry) || (!cb))
328 if (free_data) free_data((void *)data);
332 listener = malloc(sizeof(*listener));
335 if (free_data) free_data((void *)data);
339 listener->data = (void *)data;
340 listener->free_data = free_data;
342 e = _e_fm2_op_registry_entry_internal_get(entry);
343 e->listeners = eina_inlist_append(e->listeners, EINA_INLIST_GET(listener));
344 err = eina_error_get();
347 printf("could not add listener: %s\n", eina_error_msg_get(err));
348 if (free_data) free_data((void *)data);
355 * Removes the function to be called when entry changes.
357 * @param: entry entry to operate on.
358 * @param: cb function to callback on changes.
359 * @param: data extra data to give to @p cb
361 * @note: does not decrement reference.
362 * @see: e_fm2_op_registry_entry_listener_add()
365 e_fm2_op_registry_entry_listener_del(E_Fm2_Op_Registry_Entry *entry, void (*cb)(void *data, const E_Fm2_Op_Registry_Entry *entry), const void *data)
367 E_Fm2_Op_Registry_Entry_Internal *e;
368 E_Fm2_Op_Registry_Entry_Listener *l;
370 if ((!entry) || (!cb)) return;
371 e = _e_fm2_op_registry_entry_internal_get(entry);
373 EINA_INLIST_FOREACH(e->listeners, l)
374 if ((l->cb == cb) && (l->data == data))
376 e->listeners = eina_inlist_remove(e->listeners, EINA_INLIST_GET(l));
377 if (l->free_data) l->free_data(l->data);
384 * Returns an iterator over all the entries in the fm operations registry.
386 * @warning: this iterator is just valid until new entries are added
387 * or removed (usually happens from main loop). This is because
388 * when system is back to main loop it can report new events and
389 * operations can be added or removed from this registry. In other
390 * words, it is fine to call this function, immediately walk the
391 * iterator and do something, then free the iterator. You can use
392 * it to create a shadow list if you wish.
394 * @see e_fm2_op_registry_get_all()
397 e_fm2_op_registry_iterator_new(void)
399 return eina_hash_iterator_data_new(_e_fm2_op_registry);
403 * Returns a shadow list with all entries in the registry.
405 * All entries will have references incremented, so you must free the
406 * list with e_fm2_op_registry_get_all_free() to free the list and
407 * release these references.
409 * @note: List is unsorted!
410 * @note: if you need a simple, immediate walk, use
411 * e_fm2_op_registry_iterator_new()
414 e_fm2_op_registry_get_all(void)
418 E_Fm2_Op_Registry_Entry_Internal *e;
421 it = eina_hash_iterator_data_new(_e_fm2_op_registry);
422 EINA_ITERATOR_FOREACH(it, e)
424 _e_fm2_op_registry_entry_internal_ref(e);
425 list = eina_list_append(list, &(e->entry));
427 eina_iterator_free(it);
433 e_fm2_op_registry_get_all_free(Eina_List *list)
435 E_Fm2_Op_Registry_Entry *entry;
436 EINA_LIST_FREE(list, entry)
437 e_fm2_op_registry_entry_unref(entry);
441 e_fm2_op_registry_is_empty(void)
443 return eina_hash_population(_e_fm2_op_registry) == 0;
447 e_fm2_op_registry_count(void)
449 return eina_hash_population(_e_fm2_op_registry);
453 e_fm2_op_registry_init(void)
456 if (_e_fm2_init_count > 1) return _e_fm2_init_count;
458 _e_fm2_op_registry = eina_hash_int32_new(NULL);
459 if (!_e_fm2_op_registry)
461 _e_fm2_init_count = 0;
465 if (E_EVENT_FM_OP_REGISTRY_ADD == 0)
466 E_EVENT_FM_OP_REGISTRY_ADD = ecore_event_type_new();
467 if (E_EVENT_FM_OP_REGISTRY_DEL == 0)
468 E_EVENT_FM_OP_REGISTRY_DEL = ecore_event_type_new();
469 if (E_EVENT_FM_OP_REGISTRY_CHANGED == 0)
470 E_EVENT_FM_OP_REGISTRY_CHANGED = ecore_event_type_new();
476 e_fm2_op_registry_shutdown(void)
478 if (_e_fm2_init_count == 0) return 0;
480 if (_e_fm2_init_count > 0) return _e_fm2_init_count;
482 eina_hash_free(_e_fm2_op_registry);
483 _e_fm2_op_registry = NULL;
489 e_fm2_op_registry_entry_abort(E_Fm2_Op_Registry_Entry *entry)
493 if (entry->func.abort)
494 entry->func.abort(entry);