allow custom types to register a parse function
SVN revision: 28256
This commit is contained in:
parent
90d1031c0e
commit
bbd63d7f6b
|
@ -344,14 +344,24 @@ _cb_command(void *data, Efreet_Desktop *desktop, char *exec, int remaining)
|
||||||
free(exec);
|
free(exec);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void *
|
||||||
|
cb_type_parse(Efreet_Desktop *desktop, Efreet_Ini *ini)
|
||||||
|
{
|
||||||
|
const char *val;
|
||||||
|
val = efreet_ini_string_get(ini, "X-Test");
|
||||||
|
if (!val) return NULL;
|
||||||
|
return (void *)strdup(val);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
ef_cb_desktop_type_parse(void)
|
ef_cb_desktop_type_parse(void)
|
||||||
{
|
{
|
||||||
Efreet_Desktop *desktop;
|
Efreet_Desktop *desktop;
|
||||||
int ret = 1, my_type;
|
int ret = 1, my_type;
|
||||||
|
char *val;
|
||||||
|
|
||||||
/* add my custom desktop type to efreet */
|
/* add my custom desktop type to efreet */
|
||||||
my_type = efreet_desktop_type_add("My_Type");
|
my_type = efreet_desktop_type_add("My_Type", cb_type_parse, (Efreet_Desktop_Type_Free_Cb)free);
|
||||||
if (my_type <= EFREET_DESKTOP_TYPE_MAX)
|
if (my_type <= EFREET_DESKTOP_TYPE_MAX)
|
||||||
{
|
{
|
||||||
printf("Error adding desktop type.");
|
printf("Error adding desktop type.");
|
||||||
|
@ -371,6 +381,13 @@ ef_cb_desktop_type_parse(void)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val = (char *)efreet_desktop_type_data_get(desktop);
|
||||||
|
if (!val || strcmp(val, "Own key"))
|
||||||
|
{
|
||||||
|
printf("Invalid value of custom key (%s).\n", val);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,18 @@ static int efreet_desktop_command_file_id = 0;
|
||||||
|
|
||||||
static int init = 0;
|
static int init = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
* Information about custom types
|
||||||
|
*/
|
||||||
|
typedef struct Efreet_Desktop_Type_Info Efreet_Desktop_Type_Info;
|
||||||
|
struct Efreet_Desktop_Type_Info
|
||||||
|
{
|
||||||
|
char *type;
|
||||||
|
Efreet_Desktop_Type_Parse_Cb parse_func;
|
||||||
|
Efreet_Desktop_Type_Free_Cb free_func;
|
||||||
|
};
|
||||||
|
|
||||||
static Efreet_Desktop *efreet_desktop_new(const char *file);
|
static Efreet_Desktop *efreet_desktop_new(const char *file);
|
||||||
static Efreet_Desktop_Type efreet_desktop_type_parse(const char *type_str);
|
static Efreet_Desktop_Type efreet_desktop_type_parse(const char *type_str);
|
||||||
static Ecore_List *efreet_desktop_string_list_parse(const char *string);
|
static Ecore_List *efreet_desktop_string_list_parse(const char *string);
|
||||||
|
@ -83,6 +95,8 @@ static int efreet_desktop_cb_download_progress(void *data, const char *file,
|
||||||
static void efreet_desktop_exec_cb(void *data, Efreet_Desktop *desktop,
|
static void efreet_desktop_exec_cb(void *data, Efreet_Desktop *desktop,
|
||||||
char *exec, int remaining);
|
char *exec, int remaining);
|
||||||
|
|
||||||
|
static void efreet_desktop_type_info_free(Efreet_Desktop_Type_Info *info);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
* @return Returns > 0 on success or 0 on failure
|
* @return Returns > 0 on success or 0 on failure
|
||||||
|
@ -101,7 +115,7 @@ efreet_desktop_init(void)
|
||||||
ECORE_FREE_CB(efreet_desktop_free));
|
ECORE_FREE_CB(efreet_desktop_free));
|
||||||
|
|
||||||
efreet_desktop_types = ecore_list_new();
|
efreet_desktop_types = ecore_list_new();
|
||||||
ecore_list_set_free_cb(efreet_desktop_types, ECORE_FREE_CB(free));
|
ecore_list_set_free_cb(efreet_desktop_types, ECORE_FREE_CB(efreet_desktop_type_info_free));
|
||||||
|
|
||||||
return init;
|
return init;
|
||||||
}
|
}
|
||||||
|
@ -259,7 +273,11 @@ efreet_desktop_new(const char *file)
|
||||||
}
|
}
|
||||||
else if (desktop->type >= (EFREET_DESKTOP_TYPE_MAX + 1))
|
else if (desktop->type >= (EFREET_DESKTOP_TYPE_MAX + 1))
|
||||||
{
|
{
|
||||||
/* XXX which user fields should we be parsing? */
|
Efreet_Desktop_Type_Info *info;
|
||||||
|
info = ecore_list_goto_index(efreet_desktop_types,
|
||||||
|
(desktop->type - (EFREET_DESKTOP_TYPE_MAX + 1)));
|
||||||
|
if (info->parse_func)
|
||||||
|
desktop->type_data = info->parse_func(desktop, ini);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
error = 1;
|
error = 1;
|
||||||
|
@ -312,10 +330,11 @@ efreet_desktop_save(Efreet_Desktop *desktop)
|
||||||
}
|
}
|
||||||
else if (desktop->type > EFREET_DESKTOP_TYPE_MAX + 1)
|
else if (desktop->type > EFREET_DESKTOP_TYPE_MAX + 1)
|
||||||
{
|
{
|
||||||
ecore_list_goto_index(efreet_desktop_types,
|
Efreet_Desktop_Type_Info *info;
|
||||||
|
info = ecore_list_goto_index(efreet_desktop_types,
|
||||||
(desktop->type - (EFREET_DESKTOP_TYPE_MAX + 1)));
|
(desktop->type - (EFREET_DESKTOP_TYPE_MAX + 1)));
|
||||||
efreet_ini_string_set(ini, "Type",
|
if (info)
|
||||||
(char *)ecore_list_current(efreet_desktop_types));
|
efreet_ini_string_set(ini, "Type", info->type);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
ok = 0;
|
ok = 0;
|
||||||
|
@ -400,6 +419,15 @@ efreet_desktop_free(Efreet_Desktop *desktop)
|
||||||
|
|
||||||
IF_FREE_HASH(desktop->x);
|
IF_FREE_HASH(desktop->x);
|
||||||
|
|
||||||
|
if (desktop->type >= EFREET_DESKTOP_TYPE_MAX + 1 && desktop->type_data)
|
||||||
|
{
|
||||||
|
Efreet_Desktop_Type_Info *info;
|
||||||
|
info = ecore_list_goto_index(efreet_desktop_types,
|
||||||
|
(desktop->type - (EFREET_DESKTOP_TYPE_MAX + 1)));
|
||||||
|
if (info->free_func)
|
||||||
|
info->free_func(desktop->type_data);
|
||||||
|
}
|
||||||
|
|
||||||
FREE(desktop);
|
FREE(desktop);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -498,20 +526,54 @@ efreet_desktop_category_del(Efreet_Desktop *desktop, const char *category)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param type: The type to add to the list of matching types
|
* @param type: The type to add to the list of matching types
|
||||||
* @return Returns no value
|
* @param parse_func: a function to parse out custom fields
|
||||||
|
* @param free_func: a function to free data returned from @a parse_func
|
||||||
|
* @return Returns the id of the new type
|
||||||
* @brief Adds the given type to the list of types in the system
|
* @brief Adds the given type to the list of types in the system
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
efreet_desktop_type_add(const char *type)
|
efreet_desktop_type_add(const char *type, Efreet_Desktop_Type_Parse_Cb parse_func,
|
||||||
|
Efreet_Desktop_Type_Free_Cb free_func)
|
||||||
{
|
{
|
||||||
int id;
|
int id;
|
||||||
|
Efreet_Desktop_Type_Info *info;
|
||||||
|
|
||||||
|
info = NEW(Efreet_Desktop_Type_Info, 1);
|
||||||
|
if (!info) return 0;
|
||||||
|
|
||||||
|
info->type = strdup(type);
|
||||||
|
info->parse_func = parse_func;
|
||||||
|
info->free_func = free_func;
|
||||||
|
|
||||||
id = ecore_list_nodes(efreet_desktop_types);
|
id = ecore_list_nodes(efreet_desktop_types);
|
||||||
ecore_list_append(efreet_desktop_types, strdup(type));
|
ecore_list_append(efreet_desktop_types, info);
|
||||||
|
|
||||||
return (id + EFREET_DESKTOP_TYPE_MAX + 1);
|
return (id + EFREET_DESKTOP_TYPE_MAX + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
* @brief Free an Efreet Desktop_Type_Info struct
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
efreet_desktop_type_info_free(Efreet_Desktop_Type_Info *info)
|
||||||
|
{
|
||||||
|
if (!info) return;
|
||||||
|
IF_FREE(info->type);
|
||||||
|
free(info);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief get type specific data for custom desktop types
|
||||||
|
* @param desktop the desktop
|
||||||
|
* @return type specific data, or NULL if there is none
|
||||||
|
*/
|
||||||
|
void *
|
||||||
|
efreet_desktop_type_data_get(Efreet_Desktop *desktop)
|
||||||
|
{
|
||||||
|
return desktop->type_data;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
* @param type_str: the type as a string
|
* @param type_str: the type as a string
|
||||||
|
@ -521,7 +583,7 @@ efreet_desktop_type_add(const char *type)
|
||||||
static Efreet_Desktop_Type
|
static Efreet_Desktop_Type
|
||||||
efreet_desktop_type_parse(const char *type_str)
|
efreet_desktop_type_parse(const char *type_str)
|
||||||
{
|
{
|
||||||
char *str;
|
Efreet_Desktop_Type_Info *info;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
if (!type_str) return EFREET_DESKTOP_TYPE_UNKNOWN;
|
if (!type_str) return EFREET_DESKTOP_TYPE_UNKNOWN;
|
||||||
|
@ -535,9 +597,9 @@ efreet_desktop_type_parse(const char *type_str)
|
||||||
|
|
||||||
/* check the user added types */
|
/* check the user added types */
|
||||||
ecore_list_goto_first(efreet_desktop_types);
|
ecore_list_goto_first(efreet_desktop_types);
|
||||||
while ((str = ecore_list_next(efreet_desktop_types)))
|
while ((info = ecore_list_next(efreet_desktop_types)))
|
||||||
{
|
{
|
||||||
if (!strcmp(str, type_str))
|
if (!strcmp(info->type, type_str))
|
||||||
return (count + EFREET_DESKTOP_TYPE_MAX + 1);
|
return (count + EFREET_DESKTOP_TYPE_MAX + 1);
|
||||||
count ++;
|
count ++;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
#ifndef EFREET_DESKTOP_H
|
#ifndef EFREET_DESKTOP_H
|
||||||
#define EFREET_DESKTOP_H
|
#define EFREET_DESKTOP_H
|
||||||
|
|
||||||
|
#include "efreet_ini.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file efreet_desktop.h
|
* @file efreet_desktop.h
|
||||||
* @brief Contains the structures and methods used to support the
|
* @brief Contains the structures and methods used to support the
|
||||||
|
@ -44,6 +46,16 @@ typedef void (*Efreet_Desktop_Command_Cb) (void *data, Efreet_Desktop *desktop,
|
||||||
*/
|
*/
|
||||||
typedef int (*Efreet_Desktop_Progress_Cb) (void *data, Efreet_Desktop *desktop, char *uri, long int total, long int current);
|
typedef int (*Efreet_Desktop_Progress_Cb) (void *data, Efreet_Desktop *desktop, char *uri, long int total, long int current);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A callback used to parse data for custom types
|
||||||
|
*/
|
||||||
|
typedef void *(*Efreet_Desktop_Type_Parse_Cb) (Efreet_Desktop *desktop, Efreet_Ini *ini);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A callback used to free data for custom types
|
||||||
|
*/
|
||||||
|
typedef void *(*Efreet_Desktop_Type_Free_Cb) (void *data);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Efreet_Desktop
|
* Efreet_Desktop
|
||||||
* @brief a parsed representation of a .desktop file
|
* @brief a parsed representation of a .desktop file
|
||||||
|
@ -81,6 +93,7 @@ struct Efreet_Desktop
|
||||||
unsigned char startup_notify:1; /**< The starup notify settings of the app */
|
unsigned char startup_notify:1; /**< The starup notify settings of the app */
|
||||||
|
|
||||||
Ecore_Hash *x; /**< Keep track of all user extensions, keys that begin with X- */
|
Ecore_Hash *x; /**< Keep track of all user extensions, keys that begin with X- */
|
||||||
|
void *type_data; /**< Type specific data for custom types */
|
||||||
};
|
};
|
||||||
|
|
||||||
Efreet_Desktop *efreet_desktop_get(const char *file);
|
Efreet_Desktop *efreet_desktop_get(const char *file);
|
||||||
|
@ -111,7 +124,7 @@ void efreet_desktop_category_add(Efreet_Desktop *desktop,
|
||||||
int efreet_desktop_category_del(Efreet_Desktop *desktop,
|
int efreet_desktop_category_del(Efreet_Desktop *desktop,
|
||||||
const char *category);
|
const char *category);
|
||||||
|
|
||||||
int efreet_desktop_type_add(const char *type);
|
int efreet_desktop_type_add(const char *type, Efreet_Desktop_Type_Parse_Cb parse_func, Efreet_Desktop_Type_Free_Cb free_func);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @}
|
* @}
|
||||||
|
|
Loading…
Reference in New Issue