aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/e_exehist.c
blob: 57d2d93ae265390066402d400d6e101f7b7a2537 (plain) (blame)
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
/*
 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
 */
#include "e.h"

/* currently default bind is alt+` buf alt+space has been suggested */

/* local subsystem functions */
typedef struct _E_Exehist E_Exehist;
typedef struct _E_Exehist_Item E_Exehist_Item;

struct _E_Exehist
{
   Evas_List *history;
};

struct _E_Exehist_Item
{
   char         *exe;
   char         *launch_method;
   double        exetime;
};

static void _e_exehist_unload_queue(void);
static void _e_exehist_load(void);
static void _e_exehist_clear(void);
static void _e_exehist_unload(void);
static void _e_exehist_limit(void);
static int _e_exehist_cb_unload(void *data);

/* local subsystem globals */
static E_Config_DD *_e_exehist_config_edd = NULL;
static E_Config_DD *_e_exehist_config_item_edd = NULL;
static E_Exehist *_e_exehist = NULL;
static Ecore_Timer *_e_exehist_unload_timer = NULL;

/* externally accessible functions */
EAPI int
e_exehist_init(void)
{
   _e_exehist_config_item_edd = E_CONFIG_DD_NEW("E_Exehist_Item", E_Exehist_Item);
#undef T
#undef D
#define T E_Exehist_Item
#define D _e_exehist_config_item_edd
   E_CONFIG_VAL(D, T, exe, STR);
   E_CONFIG_VAL(D, T, launch_method, STR);
   E_CONFIG_VAL(D, T, exetime, DOUBLE);
   
   _e_exehist_config_edd = E_CONFIG_DD_NEW("E_Exehist", E_Exehist);
#undef T
#undef D
#define T E_Exehist
#define D _e_exehist_config_edd
   E_CONFIG_LIST(D, T, history, _e_exehist_config_item_edd);
   return 1;
}

EAPI int
e_exehist_shutdown(void)
{
   _e_exehist_unload();
   E_CONFIG_DD_FREE(_e_exehist_config_item_edd);
   E_CONFIG_DD_FREE(_e_exehist_config_edd);
   return 1;
}

EAPI void
e_exehist_add(const char *launch_method, const char *exe)
{
   E_Exehist_Item *ei;

   _e_exehist_load();
   if (!_e_exehist) return;
   ei = E_NEW(E_Exehist_Item, 1);
   if (!ei) return;
   ei->launch_method = evas_stringshare_add(launch_method);
   ei->exe = evas_stringshare_add(exe);
   ei->exetime = ecore_time_get();
   _e_exehist->history = evas_list_append(_e_exehist->history, ei);
   _e_exehist_limit();
   e_config_domain_save("exehist", _e_exehist_config_edd, _e_exehist);
   _e_exehist_unload_queue();
}

EAPI void
e_exehist_clear(void)
{
   _e_exehist_load();
   if (!_e_exehist) return;
   _e_exehist_clear();
   e_config_domain_save("exehist", _e_exehist_config_edd, _e_exehist);
   _e_exehist_unload_queue();
}

EAPI int
e_exehist_popularity_get(const char *exe)
{
   Evas_List *l;
   int count = 0;
   
   _e_exehist_load();
   if (!_e_exehist) return 0;
   for (l = _e_exehist->history; l; l = l->next)
     {
	E_Exehist_Item *ei;
	
	ei = l->data;
	if ((ei->exe) && (!strcmp(exe, ei->exe))) count++;
     }
   return count;
}

EAPI double
e_exehist_newest_run_get(const char *exe)
{
   Evas_List *l;
   
   _e_exehist_load();
   if (!_e_exehist) return 0.0;
   for (l = evas_list_last(_e_exehist->history); l; l = l->prev)
     {
	E_Exehist_Item *ei;
	
	ei = l->data;
	if ((ei->exe) && (!strcmp(exe, ei->exe))) return ei->exetime;
     }
   return 0.0;
}

/* local subsystem functions */
static void
_e_exehist_unload_queue(void)
{
   if (_e_exehist_unload_timer) ecore_timer_del(_e_exehist_unload_timer);
   _e_exehist_unload_timer = ecore_timer_add(2.0, _e_exehist_cb_unload, NULL);
}

static void
_e_exehist_load(void)
{
   if (!_e_exehist)
     _e_exehist = e_config_domain_load("exehist", _e_exehist_config_edd);
   if (!_e_exehist)
     _e_exehist = E_NEW(E_Exehist, 1);
}

static void
_e_exehist_clear(void)
{
   if (_e_exehist)
     {
	while (_e_exehist->history)
	  {
	     E_Exehist_Item *ei;
	     
	     ei = _e_exehist->history->data;
	     if (ei->exe) evas_stringshare_del(ei->exe);
	     if (ei->launch_method) evas_stringshare_del(ei->launch_method);
	     free(ei);
	     _e_exehist->history = evas_list_remove_list(_e_exehist->history, _e_exehist->history);
	  }
     }
}

static void
_e_exehist_unload(void)
{
   _e_exehist_clear();
   E_FREE(_e_exehist);
}

static void
_e_exehist_limit(void)
{
   /* go from first item in hist on and either delete all items before a
    * specific timestamp, or if the list count > limit then delete items
    */
}

static int
_e_exehist_cb_unload(void *data)
{
   _e_exehist_unload();
   _e_exehist_unload_timer = NULL;
   return 0;
}