2 * @defgroup Store Elementary Store
4 * Store is an abstracting API that is intended to farm off fetching of data
5 * to threads running asynchronously from the mainloop that actually fetch
6 * data needed for a genlist (or possibly future other widgets) so scrolling
7 * never blocks waiting on IO (though normally this should be the users
8 * job - if using genlist, to ensure all data genlist needs is in memory at
9 * the time it needs it, and if it isn't to queue and defer a fetch and let
10 * genlist know later when its ready. Store actually does this and implements
11 * the infrastructure of this, leaving the actual fetch and convert up to
12 * functions provided by the user).
14 * It is possible for store to run inline without a thread, but this is
15 * highly inadvisable. you can disable this with:
17 * elm_store_fetch_thread_set(store, EINA_FALSE);
19 * Store works first by creating a store, setting up functions to list items
20 * and fetch items. Currently the only store type supported is the
21 * filesystem store, which will list the files inside a directory (not
22 * recursively) and then hand each file it finds (the file path) to the
23 * list function for evaluation.
25 * The list function may look at filename, may open the file or do
26 * anything it likes to determine something about the file. Either it
27 * filters it out (returns EINA_FALSE) and it is discarded or it
28 * returns EINA_TRUE and also provides a "sort id" which is a string
29 * store uses to figure out sorting. This string could be the filename, or
30 * some data based on its contents. The strings are sorted alphabetically
31 * like any normal ASCII strings, with case being important. As this listing
32 * function runs in a thread, it can do blocking IO and parsing without
33 * hurting the fluidity of the main loop and GUI. The list function also
34 * returns information on how to map fields in the source file to elements
35 * of the genlist item. For example, how the fetcher reads the private
36 * data struct of the user (what memory offset in the struct the data is at)
37 * and what type is there (it's a label of some sort, an icon, or with a
38 * custom mapping function that figures it out itself and creates the
39 * content needed for the genlist item).
41 * Store then uses this sort id to build (over time) a sorted list of items
42 * that then map 1:1 to genlist items. When these items are visible and
43 * need content, Store calls the fetch function per item, which is responsible
44 * for fetching the data from the given item and returning data to store
45 * so it can map this to some item content. This function also runs in a
46 * thread, and thus can do blocking IO work to later return the data. Sorting
47 * is optional and can be enabled or disabled too.
49 * When items are no longer needed, store will cal the unfetch function to
50 * free data in memory about that item that is no longer needed. This function
51 * is called in the mainloop and is expected to take minimal or almost no time
52 * to simply free up memory resources.
57 typedef struct _Elm_Store Elm_Store; /**< A store object */
58 typedef struct _Elm_Store_Item Elm_Store_Item; /**< A handle of a store item passed to store fetch/unfetch functions */
59 typedef struct _Elm_Store_Item_Info Elm_Store_Item_Info; /**< Basic information about a store item - always cast into a specific type like Elm_Store_Item_Info_Filesystem */
60 typedef struct _Elm_Store_Item_Info_Filesystem Elm_Store_Item_Info_Filesystem; /**< Filesystem specific information about a store item */
61 typedef struct _Elm_Store_Item_Mapping Elm_Store_Item_Mapping; /**< A basic way of telling Store how to take your return data (string, or something else from your struct) and convert it into something genlist can use */
62 typedef struct _Elm_Store_Item_Mapping_Empty Elm_Store_Item_Mapping_Empty; /**< An empty piece of mapping information. Useful for String labels as they get used directly */
63 typedef struct _Elm_Store_Item_Mapping_Icon Elm_Store_Item_Mapping_Icon; /***< The data being mapped at the given address is an icon, so use these properties for finding it */
64 typedef struct _Elm_Store_Item_Mapping_Photo Elm_Store_Item_Mapping_Photo; /**< The data is a photo, so use these parameters to find it */
65 typedef struct _Elm_Store_Item_Mapping_Custom Elm_Store_Item_Mapping_Custom; /**> The item needs a custom mapping which means calling a function and returning a string from it, as opposed to a static lookup. It should not be allocated, and should live in a buffer in memory that survives the return of this function if its a label, or an allocated icon object if its an icon needed etc. */
67 typedef Eina_Bool (*Elm_Store_Item_List_Cb)(void *data, Elm_Store_Item_Info *info); /**< Function to call for listing an item */
68 typedef void (*Elm_Store_Item_Fetch_Cb)(void *data, Elm_Store_Item *sti); /**< Function to call to fetch item data */
69 typedef void (*Elm_Store_Item_Unfetch_Cb)(void *data, Elm_Store_Item *sti); /**< Function to cal lto un-fetch (free) an item */
70 typedef void *(*Elm_Store_Item_Mapping_Cb)(void *data, Elm_Store_Item *sti, const char *part); /**< Custom mapping function to call */
74 ELM_STORE_ITEM_MAPPING_NONE = 0,
75 ELM_STORE_ITEM_MAPPING_LABEL, /**< const char * -> label */
76 ELM_STORE_ITEM_MAPPING_STATE, /**< Eina_Bool -> state */
77 ELM_STORE_ITEM_MAPPING_ICON, /**< char * -> icon path */
78 ELM_STORE_ITEM_MAPPING_PHOTO, /**< char * -> photo path */
79 ELM_STORE_ITEM_MAPPING_CUSTOM, /**< item->custom(it->data, it, part) -> void * (-> any) */
80 ELM_STORE_ITEM_MAPPING_LAST
81 } Elm_Store_Item_Mapping_Type;
83 struct _Elm_Store_Item_Mapping_Icon
85 int w, h; /**< The desired icon size in addition to the file path returned from the mapping */
86 Elm_Icon_Lookup_Order lookup_order; /**< The order in which to find the icon */
87 Eina_Bool standard_name : 1; /**< Use a standard name to find it (EINA_TRUE) or not */
88 Eina_Bool no_scale : 1; /**< EINA_TRUE is you don't want the icon scaled */
89 Eina_Bool smooth : 1; /**< EINA_TRUE if icon is to be smooth scaled */
90 Eina_Bool scale_up : 1; /**< EINA_TRUE if scaling up is allowed */
91 Eina_Bool scale_down : 1; /**< EINA_TRUE if scaling down is allowed */
94 struct _Elm_Store_Item_Mapping_Empty
96 Eina_Bool dummy; /**< dummy entry - set to anything you like */
99 struct _Elm_Store_Item_Mapping_Photo
101 int size; /**< Photo size to use (see elm_photo_add()) with the given photo path */
104 struct _Elm_Store_Item_Mapping_Custom
106 Elm_Store_Item_Mapping_Cb func; /**< The function called to do the custom mapping and return it */
109 struct _Elm_Store_Item_Mapping
111 Elm_Store_Item_Mapping_Type type; /**< what kind of mapping is this */
112 const char *part; /**< what part name in the genlist item is this filling in */
113 int offset; /**< offset in memory (in bytes) relative to base of structure for item data where the data for the mapping lives */
116 Elm_Store_Item_Mapping_Empty empty;
117 Elm_Store_Item_Mapping_Icon icon;
118 Elm_Store_Item_Mapping_Photo photo;
119 Elm_Store_Item_Mapping_Custom custom;
120 // add more types here
121 } details; /**< Allowed to be one of these possible mapping types */
124 struct _Elm_Store_Item_Info
126 Elm_Genlist_Item_Class *item_class; /**< The genlist item class that should be used for the item that has been listed */
127 const Elm_Store_Item_Mapping *mapping; /**< What kind of mappings do we use for the fields of this item to fill in the genlist item. Terminate array pointed to here with ELM_STORE_ITEM_MAPPING_END */
128 void *data; /**< Pointer to pass to struct data in memory if its already there, of not, NULL */
129 char *sort_id; /**< Sort ID string (strduped()) to know how to wort items, or NULL, if you don't care */
132 struct _Elm_Store_Item_Info_Filesystem
134 Elm_Store_Item_Info base; /**< Base information about an item */
135 char *path; /**< Extra information specific to the filesystem store */
138 #define ELM_STORE_ITEM_MAPPING_END { ELM_STORE_ITEM_MAPPING_NONE, NULL, 0, { .empty = { EINA_TRUE } } } /**< Use this to end a list of mappings */
139 #define ELM_STORE_ITEM_MAPPING_OFFSET(st, it) offsetof(st, it) /**< Use this to get the offset in bytes in memory for where the data for the mapping lives relative to the item data (a private struct pointed to owned by the user */
142 * Create a new store object
144 * This creates a new store object to then configure so it works.
146 * @return A new store object, or NULL if creation fails
148 EAPI Elm_Store *elm_store_filesystem_new(void);
150 * Free the store object and all items it manages
152 * This frees the given @p st store and all the items it manages. It will
153 * clear the List that it populated, but otherwise leave it alone. It will
154 * cancel background threads (and may have to wait for them to complete a
155 * pending operation to do this).
157 * @param st The store to free
159 EAPI void elm_store_free(Elm_Store *st);
162 * Set the path to the directory to scan for a filesystem store
164 * This sets the directory (@p dir) to scan and begins scanning in the
165 * the background in threads (or not if threading is disabled with
166 * elm_store_fetch_thread_set()). Note that Listing is always done in a thread
167 * but fetching may not be if disabled here. This should be the last thing
168 * called after fetch, list and unfetch functions are set, as well as target
169 * genlist etc. You also should not change the directory once set. If you
170 * need a new directory scanned, create a new store.
172 * @param st The store to modify
173 * @param dir A string giving the path to the directory to scan
175 EAPI void elm_store_filesystem_directory_set(Elm_Store *st, const char *dir);
178 * Get the directory set on a filesystem store
180 * This gets the directory set by elm_store_filesystem_directory_set(). This
181 * string returned will be valid until elm_store_filesystem_directory_set()
182 * changes it or until the store is freed with elm_store_free().
184 * @return A string with the path set, or NULL if none set.
186 EAPI const char *elm_store_filesystem_directory_get(const Elm_Store *st);
189 * Get the path of a specific store item
191 * This returns the full path of a store item. This string is valid only
192 * during the list function set by elm_store_list_func_set() or during the
193 * fetch function set by elm_store_fetch_func_set() or during the unfetch
194 * function set by elm_store_unfetch_func_set().
196 * @param sti The store item to get the path from
197 * @return A full path in a string or NULL if none available
199 EAPI const char *elm_store_item_filesystem_path_get(const Elm_Store_Item *sti);
202 * Set the target genlist to fill in from the store
204 * This tells the store the target genlist to use to fill in content from
205 * the store. Once a store starts "going" via elm_store_filesystem_directory_set()
206 * The target should never be changed again.
208 * @param st The store to do the filling.
209 * @param obj The genlist object to fill in and control the content of from the store.
211 EAPI void elm_store_target_genlist_set(Elm_Store *st, Evas_Object *obj);
214 * Set the maximum number of items that are not visible to keep cached
216 * Store may keep some items around for caching purposes that cannot be seen,
217 * so this controls the maximum number. The default is 128, but may change
218 * at any point in time in the future.
220 * @param st The store to modify
221 * @param max The number of items to keep (should be greater than or equal to 0)
223 EAPI void elm_store_cache_set(Elm_Store *st, int max);
226 * Get the maximum number if items to cache
228 * This returns the number of items at most to cache.
230 * @param st The store to query
231 * @return The maximum number of items to cache (>= 0)
232 * @see elm_store_cache_set()
234 EAPI int elm_store_cache_get(const Elm_Store *st);
237 * Set the function used to deal with listing of items
239 * This function is called per item that is found so it can examine the item
240 * and discard it (return EINA_FALSE to discard, or EINA_TRUE to accept), and
241 * work out some sorting ID (that may be filename or anything else based on
242 * content). This function is always called from a thread.
244 * @param st The store to set the function of
245 * @param func The function to be called
246 * @param data the data pointer to be passed to the @p func function when called
248 EAPI void elm_store_list_func_set(Elm_Store *st, Elm_Store_Item_List_Cb func, const void *data);
251 * Set the function used to deal with fetching of items
253 * This function is called per item that needs data to be fetched when it
254 * becomes visible and such data is needed. This function is normally run
255 * from a thread (unless elm_store_fetch_thread_set() disables this). The
256 * fetch function is to read data from the source and fill a structure
257 * allocated for this item with fields and then rely on the mapping setup
258 * to tell Store how to take a field in the structure and apply it to a
261 * @param st The store to set the function of
262 * @param func The function to be called
263 * @param data the data pointer to be passed to the @p func function when called
265 EAPI void elm_store_fetch_func_set(Elm_Store *st, Elm_Store_Item_Fetch_Cb func, const void *data);
268 * Set the function used to free the structure allocated for the item
270 * This function is called per item when it is not needed in memory anymore
271 * and should free the structure allocated in and filled in the function set
272 * by elm_store_fetch_func_set().
274 * @param st The store to set the function of
275 * @param func The function to be called
276 * @param data the data pointer to be passed to the @p func function when called
278 EAPI void elm_store_unfetch_func_set(Elm_Store *st, Elm_Store_Item_Unfetch_Cb func, const void *data);
281 * Enable or disable fetching in a thread for Store
283 * @param st The store to modify
284 * @param use_thread EINA_TRUE to use a thread to fetch, EINA_FALSE don't use a thread.
286 EAPI void elm_store_fetch_thread_set(Elm_Store *st, Eina_Bool use_thread);
289 * Get the thread enabled fetching option for Store
291 * @return The state set currently for the store.
292 * @see elm_store_fetch_thread_set()
294 EAPI Eina_Bool elm_store_fetch_thread_get(const Elm_Store *st);
297 * Set if items are to be sorted or not.
299 * By default items are not sorted, but read "in order" as they are found. If
300 * you want to sort, your list function set by elm_store_list_func_set() must
301 * provide a sort ID to sort by, and then Store will take care of sorting when
302 * it inserts items. You should set this up before you begin listing items
303 * in the store and then never change it again.
305 * @param st The store to modify
306 * @param sorted EINA_TRUE if we are to sort, EINA_FALSE if not.
308 EAPI void elm_store_sorted_set(Elm_Store *st, Eina_Bool sorted);
311 * Get the sorting flag
313 * Get the sorted flag as set by elm_store_sorted_set().
315 * @param st The store to query
316 * @return EINA_TRUE if sorted, EINA_FALSE if not.
318 EAPI Eina_Bool elm_store_sorted_get(const Elm_Store *st);
321 * Set the item data holding item fields to map to item values in genlist
323 * Once you decode an item, allocate a structure for it and fill the structure,
324 * you should set the item data with this function (eg in the fetch function).
325 * This item pointer is the base offset to use when mapping fields to item
326 * values. Once you unfetch, store will handle NULLing the data pointer for you.
328 * @param sti The store item to set the data pointer of
329 * @param data The data pointer to set.
331 EAPI void elm_store_item_data_set(Elm_Store_Item *sti, void *data);
336 * This gets the data pointer set by elm_store_item_data_set().
338 * @param sti The store item to query
339 * @return The data pointer set on the item
341 EAPI void *elm_store_item_data_get(Elm_Store_Item *sti);
344 * Fetch the store than a store item belongs to
346 * This fetches the store object that owns the store item.
348 * @param sti The store item to query
349 * @return The store the item belongs to
351 EAPI const Elm_Store *elm_store_item_store_get(const Elm_Store_Item *sti);
354 * Fetch the genlist item that this store item controls
356 * @param sti The store item to query
357 * @return The genlist object item handle controlled by this store item
359 EAPI const Elm_Object_Item *elm_store_item_genlist_item_get(const Elm_Store_Item *sti);