enlightenment/src/modules/teamwork/e_mod_main.c

208 lines
6.9 KiB
C

#include "e_mod_main.h"
#include "sha1.h"
EINTERN int _e_teamwork_log_dom = -1;
EINTERN Mod *tw_mod = NULL;
EINTERN Teamwork_Config *tw_config = NULL;
EAPI E_Module_Api e_modapi = {E_MODULE_API_VERSION, "Teamwork"};
static E_Config_DD *conf_edd = NULL;
static E_Action *e_tw_toggle = NULL;
static const char _act_toggle[] = "tw_toggle";
static const char _e_tw_name[] = N_("Teamwork");
static const char _lbl_toggle[] = N_("Toggle Popup Visibility");
static const char *
_sha1_to_string(const unsigned char *hashout)
{
const char hextab[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
char sha1[41] = {0};
unsigned int i = 0;
for (i = 0; i < 20; i++)
{
sha1[2 * i] = hextab[(hashout[i] >> 4) & 0x0f];
sha1[2 * i + 1] = hextab[hashout[i] & 0x0f];
}
return eina_stringshare_add(sha1);
}
const char *
sha1_encode(const unsigned char *data, size_t len)
{
SHA_CTX2 ctx;
unsigned char hashout[20];
unsigned char *buf;
if (EINA_UNLIKELY(len > 65000))
buf = malloc(len);
else
buf = alloca(len);
EINA_SAFETY_ON_NULL_RETURN_VAL(buf, NULL);
memcpy(buf, data, len);
SHA1_Init2(&ctx);
SHA1_Update2(&ctx, buf, len);
SHA1_Final2(hashout, &ctx);
if (EINA_UNLIKELY(len > 65000)) free(buf);
return _sha1_to_string(hashout);
}
//////////////////////////////
static Teamwork_Config *
e_tw_config_new(void)
{
Teamwork_Config *cf;
cf = E_NEW(Teamwork_Config, 1);
cf->config_version = MOD_CONFIG_FILE_VERSION;
cf->allowed_media_size = 10; // 10 megabytes
cf->allowed_media_age = 3; // 3 days
cf->mouse_out_delay = 0.0;
cf->popup_size = 10.0;
cf->popup_opacity = 90.0;
return cf;
}
static E_Config_DD *
e_tw_config_dd_new(void)
{
conf_edd = E_CONFIG_DD_NEW("Teamwork_Config", Teamwork_Config);
#undef T
#undef D
#define T Teamwork_Config
#define D conf_edd
E_CONFIG_VAL(D, T, config_version, UINT);
E_CONFIG_VAL(D, T, disable_media_fetch, UCHAR);
E_CONFIG_VAL(D, T, allowed_media_size, LL);
E_CONFIG_VAL(D, T, allowed_media_age, INT);
E_CONFIG_VAL(D, T, mouse_out_delay, DOUBLE);
E_CONFIG_VAL(D, T, popup_size, DOUBLE);
E_CONFIG_VAL(D, T, popup_opacity, DOUBLE);
return conf_edd;
}
//////////////////////////////
static void
e_tw_act_toggle_cb(E_Object *obj EINA_UNUSED, const char *params EINA_UNUSED)
{
if (!tw_mod->pop) return;
if (tw_mod->sticky)
tw_hide(NULL);
tw_mod->sticky = !tw_mod->sticky;
}
//////////////////////////////
EAPI void *
e_modapi_init(E_Module *m)
{
char buf[PATH_MAX];
E_Configure_Option *co;
snprintf(buf, sizeof(buf), "%s/e-module-teamwork.edj", e_module_dir_get(m));
e_configure_registry_category_add("applications", 20, _("Apps"), NULL,
"preferences-applications");
e_configure_registry_item_add("applications/teamwork", 1, _("Teamwork"), NULL,
buf, e_int_config_teamwork_module);
tw_mod = E_NEW(Mod, 1);
tw_mod->module = m;
m->data = tw_mod;
conf_edd = e_tw_config_dd_new();
tw_config = e_config_domain_load("module.teamwork", conf_edd);
if (tw_config)
{
if (!e_util_module_config_check(_("Teamwork"), tw_config->config_version, MOD_CONFIG_FILE_VERSION))
E_FREE_FUNC(tw_config, free);
}
if (tw_config)
{
/* sanity checks */
tw_config->mouse_out_delay = E_CLAMP(tw_config->mouse_out_delay, 0.0, 5.0);
tw_config->popup_size = E_CLAMP(tw_config->popup_size, 10.0, 100.0);
tw_config->popup_opacity = E_CLAMP(tw_config->popup_opacity, 10.0, 100.0);
}
else
tw_config = e_tw_config_new();
tw_config->config_version = MOD_CONFIG_FILE_VERSION;
_e_teamwork_log_dom = eina_log_domain_register("teamwork", EINA_COLOR_ORANGE);
eina_log_domain_level_set("teamwork", EINA_LOG_LEVEL_DBG);
if (!e_tw_init())
{
e_modapi_shutdown(NULL);
return NULL;
}
e_tw_toggle = e_action_add(_act_toggle);
e_tw_toggle->func.go = e_tw_act_toggle_cb;
e_action_predef_name_set(_e_tw_name, _lbl_toggle, _act_toggle, NULL, NULL, 0);
e_configure_option_domain_current_set("teamwork");
E_CONFIGURE_OPTION_ADD_CUSTOM(co, "teamwork-settings", _("Teamwork settings panel"), _("teamwork"), _("applications"));
co->info = eina_stringshare_add("applications/teamwork");
E_CONFIGURE_OPTION_ICON(co, buf);
E_CONFIGURE_OPTION_ADD(co, BOOL, disable_media_fetch, tw_config, _("Disable Teamwork remote media fetching"), _("teamwork"));
E_CONFIGURE_OPTION_ADD(co, DOUBLE, allowed_media_size, tw_config, _("Maximum total size of Teamwork media to keep in RAM"), _("teamwork"), _("cache"));
E_CONFIGURE_OPTION_MINMAX_STEP_FMT(co, 0, 1024, 16, _("%4.0f MiB"));
E_CONFIGURE_OPTION_HELP(co, _("This option determines how much memory will be used to cache recent media for faster loading."));
E_CONFIGURE_OPTION_ADD(co, DOUBLE, allowed_media_age, tw_config, _("Maximum age for a disk-cached Teamwork media item"), _("teamwork"), _("cache"));
E_CONFIGURE_OPTION_MINMAX_STEP_FMT(co, -1, 180, 1, _("%3.0f Days"));
E_CONFIGURE_OPTION_HELP(co, _("This option determines how long media will remain in the disk cache before it is pruned.</ps>"
"Set to -1 to never delete media, or to 0 to never cache media on disk."));
E_CONFIGURE_OPTION_ADD(co, DOUBLE, mouse_out_delay, tw_config, _("Delay before closing a Teamwork popup on mouse-out"), _("teamwork"), _("mouse"));
E_CONFIGURE_OPTION_MINMAX_STEP_FMT(co, 0, 5, 0.5, _("%1.1f seconds"));
E_CONFIGURE_OPTION_ADD(co, DOUBLE, popup_size, tw_config, _("Maximum percentage of screen to use for Teamwork popups"), _("teamwork"), _("screen"));
E_CONFIGURE_OPTION_MINMAX_STEP_FMT(co, 10, 100, 1, _("%3.0f"));
E_CONFIGURE_OPTION_ADD(co, DOUBLE, popup_opacity, tw_config, _("Opacity to use for Teamwork popups"), _("teamwork"));
E_CONFIGURE_OPTION_MINMAX_STEP_FMT(co, 10, 100, 1, _("%3.0f"));
co->funcs[0].none = tw_popup_opacity_set;
e_configure_option_category_tag_add(_("applications"), _("teamwork"));
e_configure_option_category_tag_add(_("teamwork"), _("teamwork"));
e_configure_option_category_icon_set(_("teamwork"), buf);
return m;
}
EAPI int
e_modapi_shutdown(E_Module *m __UNUSED__)
{
e_tw_shutdown();
E_CONFIG_DD_FREE(conf_edd);
eina_log_domain_unregister(_e_teamwork_log_dom);
_e_teamwork_log_dom = -1;
e_configure_registry_item_del("applications/teamwork");
e_configure_registry_category_del("applications");
e_configure_option_domain_clear("teamwork");
e_configure_option_category_tag_del(_("teamwork"), _("teamwork"));
e_configure_option_category_tag_del(_("applications"), _("teamwork"));
e_action_predef_name_del(_e_tw_name, _lbl_toggle);
e_action_del(_act_toggle);
e_tw_toggle = NULL;
E_FREE(tw_config);
E_FREE(tw_mod);
return 1;
}
EAPI int
e_modapi_save(E_Module *m __UNUSED__)
{
e_config_domain_save("module.teamwork", conf_edd, tw_config);
return 1;
}