summaryrefslogtreecommitdiff
path: root/src/bin/e_update.c
blob: fe77066b620bef2e6ab002dff659e04dbc209b5d (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "e.h"

static Ecore_Con_Url *url_up = NULL;
static Eina_List *handlers = NULL;
static Ecore_Timer *update_timer = NULL;
static E_Dialog *dialog = NULL;
static char *machid = NULL;

static void
_update_done(void)
{
   if (url_up)
     {
        ecore_con_url_free(url_up);
        url_up = NULL;
     }
}

static void
_delete_cb(void *obj __UNUSED__)
{
   dialog = NULL;
}

static void
_ok_cb(void *data __UNUSED__, E_Dialog *dia __UNUSED__)
{
   e_object_del(E_OBJECT(dialog));
   if (e_config->update.later > 0)
     {
        e_config->update.later = 0;
        e_config_save_queue();
     }
}

static void
_bother_me_later_cb(void *data __UNUSED__, E_Dialog *dia __UNUSED__)
{
   e_object_del(E_OBJECT(dialog));
   // 5 * 5 * 1hr === about 1 day limit, so bother-me later will wait
   // a day in between botherings. botherings reset on e start or restart
   if (e_config->update.later < 5)
     {
        e_config->update.later++;
        e_config_save_queue();
     }
}

static void
_never_tell_me_cb(void *data __UNUSED__, E_Dialog *dia __UNUSED__)
{
   if (update_timer) ecore_timer_del(update_timer);
   update_timer = NULL;
   e_object_del(E_OBJECT(dialog));
   e_config->update.check = 0;
   e_config->update.later = 0;
   e_config_save_queue();
}

static void
_new_version(const char *ver)
{
   char text[2048];

   if (dialog) return;

   dialog = e_dialog_new(NULL, "E", "_update_available");

   e_object_del_attach_func_set(E_OBJECT(dialog), _delete_cb);
   e_dialog_button_add(dialog, _("OK"), NULL,
                       _ok_cb, NULL);
   e_dialog_button_add(dialog, _("Bother me later"), NULL,
                       _bother_me_later_cb, NULL);
   e_dialog_button_add(dialog, _("Never tell me"), NULL,
                       _never_tell_me_cb, NULL);
   e_dialog_button_focus_num(dialog, 1);
   e_dialog_title_set(dialog, _("Update Notice"));
   e_dialog_icon_set(dialog, "dialog-warning", 64);

   snprintf(text, sizeof(text),
            _("Your enlightenment version is<br>"
              "not the current latest release.<br>"
              "The latest version is:<br>"
              "<br>"
              "%s<br>"
              "<br>"
              "Please visit www.enlightenment.org<br>"
              "or update your system packages<br>"
              "to get a new version."), ver);
   e_dialog_text_set(dialog, text);
   e_win_centered_set(dialog->win, 1);
   e_dialog_show(dialog);
}

static Eina_Bool
_upload_data_cb(void *data __UNUSED__, int ev_type __UNUSED__, void *event)
{
   Ecore_Con_Event_Url_Data *ev = event;
   if (ev->url_con != url_up) return EINA_TRUE;
   if (ev->size > 0)
     {
        char *txt = alloca(ev->size + 1);

        memcpy(txt, ev->data, ev->size);
        txt[ev->size] = 0;

        if (!strncmp(txt, "OK", 2))
          {
          }
        else if (!strncmp(txt, "OLD", 3))
          {
             char *ver = strchr(txt, ' ');
             if (ver)
               {
                  ver++;
                  _new_version(ver);
               }
          }
     }
   return EINA_FALSE;
}

static Eina_Bool
_upload_progress_cb(void *data __UNUSED__, int ev_type __UNUSED__, void *event)
{
   Ecore_Con_Event_Url_Progress *ev = event;
   if (ev->url_con != url_up) return EINA_TRUE;
   return EINA_FALSE;
}

static Eina_Bool
_upload_complete_cb(void *data __UNUSED__, int ev_type __UNUSED__, void *event)
{
   Ecore_Con_Event_Url_Complete *ev = event;
   if (ev->url_con != url_up) return EINA_TRUE;
   if (ev->status != 200)
     {
        _update_done();
        return EINA_FALSE;
     }
   _update_done();
   return EINA_FALSE;
}

static void
_update_machid_get(void)
{
   FILE *f;
   char buf[4096], *c;
   size_t len;

   f = fopen("/etc/machine-id", "r");
   if (!f) f = fopen("/var/lib/dbus/machine-id", "r");
   if (!f)
     {
        e_user_dir_concat_static(buf, ".machid");
        f = fopen(buf, "r");
     }
   if (f)
     {
        len = fread(buf, 1, sizeof(buf) - 1, f);
        if (len > 10)
          {
             buf[len] = 0;
             for (c = buf; *c; c++)
               {
                  if (!isalnum(*c))
                    {
                       *c = 0;
                       break;
                    }
               }
             machid = strdup(buf);
             fclose(f);
             return;
          }
        fclose(f);
     }

   // generate ID
   e_user_dir_concat_static(buf, ".machid");
   f = fopen(buf, "w");
   if (f)
     {
        double t;
        fwrite("GEN-", 4, 1, f);
        t = ecore_time_unix_get();
        fprintf(f, "%1.16f-%i-%i\n", t, rand(), rand());
        fclose(f);
        _update_machid_get();
        return;
     }
   // this just is all a wash - just use this
   machid = strdup("NOIDEAWHATTHEIDOFTHISMACHINEIS");
}

static void
_update_post_generate(char *buf, int size)
{
   if (!machid) _update_machid_get();
   if (!machid) return;
   snprintf(buf, size,
            "CLIENT %s\n"
            "UPDATE enlightenment %s",
            machid, VERSION);
}

static void
_update_check(void)
{
   char buf[1024];

   if (url_up) _update_done();
   url_up = ecore_con_url_new("http://www.enlightenment.org/update.php");
   if (url_up)
     {
        _update_post_generate(buf, sizeof(buf));
        ecore_con_url_http_version_set(url_up, ECORE_CON_URL_HTTP_VERSION_1_0);
        ecore_con_url_post(url_up, buf, strlen(buf), "text/plain");
     }
   else
     _update_done();
}

static Eina_Bool
_update_timeout_cb(void *data)
{
   double t = 3600.0; // base minimum betwene checks -> 1hr min
   int later = e_config->update.later;

   if (e_config->update.check) _update_check();
   if (update_timer) ecore_timer_del(update_timer);
   if (later > 0)
     {
        later++;
        t *= (later * later);
     }
   update_timer = ecore_timer_add(t, _update_timeout_cb, data);
   return EINA_FALSE;
}

EINTERN int
e_update_init(void)
{
   if (ecore_con_url_init())
     {
        handlers = eina_list_append
            (handlers, ecore_event_handler_add
              (ECORE_CON_EVENT_URL_DATA, _upload_data_cb, NULL));
        handlers = eina_list_append
            (handlers, ecore_event_handler_add
              (ECORE_CON_EVENT_URL_PROGRESS, _upload_progress_cb, NULL));
        handlers = eina_list_append
            (handlers, ecore_event_handler_add
              (ECORE_CON_EVENT_URL_COMPLETE, _upload_complete_cb, NULL));
        if (e_config->update.check)
          {
             e_config->update.check = 0;
             _update_timeout_cb(NULL);
             e_config->update.check = 1;
          }
     }
   return 1;
}

EINTERN int
e_update_shutdown(void)
{
   if (handlers)
     {
        _update_done();
        ecore_con_url_shutdown();
     }
   if (update_timer)
     {
        ecore_timer_del(update_timer);
        update_timer = NULL;
     }
   if (dialog) e_object_del(E_OBJECT(dialog));
   dialog = NULL;
   _update_done();
   if (machid)
     {
        free(machid);
        machid = NULL;
     }
   return 1;
}