summaryrefslogtreecommitdiff
path: root/src/modules/teamwork/e_mod_main.c
blob: ed9d742cf77041a0bbd2c4d08b83d8bb6b909d7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#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;
}