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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
|
/*
* timer.c: Web video plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
* $Id$
*/
#include <string.h>
#include <errno.h>
#include <libxml/parser.h>
#include "timer.h"
#include "request.h"
#include "common.h"
#include "download.h"
#include "config.h"
#include "filedownloader.h"
// --- cWebviTimer -----------------------------------------------
cWebviTimer::cWebviTimer(int ID, const char *title,
const char *ref, cWebviTimerManager *manager,
time_t last, int interval, bool success,
const char *errmsg)
: id(ID), title(title ? strdup(title) : strdup("???")),
reference(ref ? strdup(ref) : NULL), lastUpdate(last),
interval(interval), running(false), lastSucceeded(success),
lastError(errmsg ? strdup(errmsg) : NULL),
parent(manager)
{
}
cWebviTimer::~cWebviTimer() {
if(title)
free(title);
if (reference)
free(reference);
if (lastError)
free(lastError);
}
void cWebviTimer::SetTitle(const char *newTitle) {
if (title)
free(title);
title = newTitle ? strdup(newTitle) : strdup("???");
parent->SetModified();
}
void cWebviTimer::SetInterval(int interval) {
if (interval < MIN_TIMER_INTERVAL)
this->interval = MIN_TIMER_INTERVAL;
else
this->interval = interval;
parent->SetModified();
}
int cWebviTimer::GetInterval() const {
return interval;
}
time_t cWebviTimer::NextUpdate() const {
int delta = interval;
// Retry again soon if the last try failed
if (!lastSucceeded && delta > RETRY_TIMER_INTERVAL)
delta = RETRY_TIMER_INTERVAL;
return lastUpdate + delta;
}
void cWebviTimer::Execute() {
if (running) {
debug("previous instance of this timer is still running");
return;
}
info("Executing timer \"%s\"", title);
running = true;
cTimerRequest *req = new cTimerRequest(id, reference);
req->SetTimer(this);
cWebviThread::Instance().AddRequest(req);
lastUpdate = time(NULL);
SetError(NULL);
parent->SetModified();
activeStreams.Clear();
}
void cWebviTimer::SetError(const char *errmsg) {
bool oldSuccess = lastSucceeded;
if (lastError)
free(lastError);
lastError = NULL;
if (errmsg) {
lastSucceeded = false;
lastError = strdup(errmsg);
} else {
lastSucceeded = true;
}
if (oldSuccess != lastSucceeded)
parent->SetModified();
}
const char *cWebviTimer::LastError() const {
return lastError ? lastError : "";
}
void cWebviTimer::DownloadStreams(const char *menuxml, cProgressVector& summaries) {
if (!menuxml) {
SetError("xml == NULL");
return;
}
xmlDocPtr doc = xmlParseMemory(menuxml, strlen(menuxml));
if (!doc) {
xmlErrorPtr xmlerr = xmlGetLastError();
if (xmlerr)
error("libxml error: %s", xmlerr->message);
SetError(xmlerr->message);
return;
}
xmlNodePtr node = xmlDocGetRootElement(doc);
if (node)
node = node->xmlChildrenNode;
while (node) {
if (!xmlStrcmp(node->name, BAD_CAST "link")) {
xmlNodePtr node2 = node->children;
while(node2) {
if (!xmlStrcmp(node2->name, BAD_CAST "stream")) {
xmlChar *streamref = xmlNodeListGetString(doc, node2->xmlChildrenNode, 1);
const char *ref = (const char *)streamref;
if (parent->AlreadyDownloaded(ref)) {
debug("timer: %s has already been downloaded", ref);
} else if (*ref) {
info("timer: downloading %s", ref);
activeStreams.Append(strdup(ref));
cFileDownloadRequest *req = \
new cFileDownloadRequest(REQ_ID_TIMER, ref,
summaries.NewDownload());
req->SetTimer(this);
cWebviThread::Instance().AddRequest(req);
}
xmlFree(streamref);
}
node2 = node2->next;
}
}
node = node->next;
}
xmlFreeDoc(doc);
if (activeStreams.Size() == 0) {
running = false;
}
}
void cWebviTimer::CheckFailed(const char *errmsg) {
SetError(errmsg);
running = false;
}
void cWebviTimer::RequestFinished(const char *ref, const char *errmsg) {
if (errmsg && !lastError)
SetError(errmsg);
if (ref) {
if (!errmsg && parent)
parent->MarkDownloaded(ref);
int i = activeStreams.Find(ref);
if (i != -1) {
free(activeStreams[i]);
activeStreams.Remove(i);
}
}
if (activeStreams.Size() == 0) {
info("timer \"%s\" done", title);
running = false;
} else {
debug("timer %s is still downloading %d streams", reference, activeStreams.Size());
}
}
// --- cWebviTimerManager ----------------------------------------
cWebviTimerManager::cWebviTimerManager()
: nextID(1), modified(false), disableSaving(false), convertTemplatePaths(false)
{
}
cWebviTimerManager &cWebviTimerManager::Instance() {
static cWebviTimerManager instance;
return instance;
}
void cWebviTimerManager::LoadTimers(FILE *f) {
cReadLine rl;
long lastRefresh;
int interval;
int success;
char *ref;
const char *ver;
const char *title;
const char *errmsg;
int n, i;
ver = rl.Read(f);
if (strcmp(ver, "# WVTIMER1") != 0 &&
strncmp(ver, "# WVTIMER1/", 11) != 0) {
error("Can't load timers. Unknown format: %s", ver);
disableSaving = true;
return;
}
convertTemplatePaths = (strcmp(ver, "# WVTIMER1") == 0);
i = 1;
while (true) {
n = fscanf(f, "%ld %d %d %ms", &lastRefresh, &interval, &success, &ref);
if (n != 4) {
if (n != EOF) {
error("Error while reading webvi timers file");
} else if (ferror(f)) {
LOG_ERROR_STR("webvi timers file");
}
break;
}
if (convertTemplatePaths) {
char *newref = UpgradedTemplatePath(ref);
if (newref) {
free(ref);
ref = newref;
}
}
title = rl.Read(f);
title = title ? skipspace(title) : "???";
errmsg = success ? NULL : "";
info("timer %d: title %s", i++, title);
debug(" ref %s, lastRefresh %ld, interval %d", ref, lastRefresh, interval);
timers.Add(new cWebviTimer(nextID++, title, ref, this,
(time_t)lastRefresh, interval,
success, errmsg));
free(ref);
}
}
void cWebviTimerManager::LoadHistory(FILE *f) {
cReadLine rl;
char *line;
while ((line = rl.Read(f)))
refHistory.Append(strdup(line));
debug("loaded history: len = %d", refHistory.Size());
}
void cWebviTimerManager::SaveTimers(FILE *f, const char *version) {
// Format: space separated field in this order:
// lastUpdate interval lastSucceeded reference title
fprintf(f, "# WVTIMER1/%s\n", version);
cWebviTimer *t = timers.First();
while (t) {
if (fprintf(f, "%ld %d %d %s %s\n",
t->LastUpdate(), t->GetInterval(), t->Success(),
t->GetReference(), t->GetTitle()) < 0) {
error("Failed to save timer data!");
}
t = timers.Next(t);
}
}
void cWebviTimerManager::SaveHistory(FILE *f) {
int size = refHistory.Size();
int first;
if (size <= MAX_TIMER_HISTORY_SIZE)
first = 0;
else
first = size - MAX_TIMER_HISTORY_SIZE;
for (int i=first; i<size; i++) {
const char *ref = refHistory[i];
if (fwrite(ref, strlen(ref), 1, f) != 1 ||
fwrite("\n", 1, 1, f) != 1) {
error("Error while writing timer history");
break;
}
}
}
char *cWebviTimerManager::UpgradedTemplatePath(char *ref) {
// template names changed in 0.4.0
const char *templateNameMap[10][2] = \
{{"wvt:///youtube/", "wvt:///www.youtube.com/"},
{"wvt:///svtplay/", "wvt:///svtplay.se/"},
{"wvt:///moontv/", "wvt:///moontv.fi/"},
{"wvt:///metacafe/", "wvt:///www.metacafe.com/"},
{"wvt:///vimeo/", "wvt:///www.vimeo.com/"},
{"wvt:///katsomo/", "wvt:///www.katsomo.fi/"},
{"wvt:///ruutufi/", "wvt:///www.ruutu.fi/"},
{"wvt:///google/", "wvt:///video.google.com/"},
{"wvt:///yleareena/", "wvt:///areena.yle.fi/"}};
for (int i=0; i<10; i++) {
int oldlen = strlen(templateNameMap[i][0]);
if (strncmp(ref, templateNameMap[i][0], oldlen) == 0) {
int newlen = strlen(templateNameMap[i][1]) + strlen(ref);
char *newref = (char *)malloc((newlen+1)*sizeof(char));
strcpy(newref, templateNameMap[i][1]);
strcat(newref, ref+oldlen);
return newref;
}
}
return NULL;
}
void cWebviTimerManager::ConvertTimerHistoryTemplates() {
for (int i=0; i<refHistory.Size(); i++) {
char *oldref = refHistory[i];
char *newref = UpgradedTemplatePath(oldref);
if (!newref)
continue;
refHistory[i] = newref;
free(oldref);
}
modified = true;
}
bool cWebviTimerManager::Load(const char *path) {
FILE *f;
bool ok = true;
cString timersname = AddDirectory(path, "timers.dat");
f = fopen(timersname, "r");
if (f) {
debug("loading webvi timers from %s", (const char *)timersname);
LoadTimers(f);
fclose(f);
} else {
if (errno != ENOENT)
LOG_ERROR_STR("Can't load webvi timers");
ok = false;
}
cString historyname = AddDirectory(path, "timers.hst");
f = fopen(historyname, "r");
if (f) {
debug("loading webvi history from %s", (const char *)historyname);
LoadHistory(f);
fclose(f);
} else {
if (errno != ENOENT)
LOG_ERROR_STR("Can't load webvi timer history");
ok = false;
}
if (convertTemplatePaths)
ConvertTimerHistoryTemplates();
return ok;
}
bool cWebviTimerManager::Save(const char *path, const char *version) {
FILE *f;
bool ok = true;
if (!modified)
return true;
if (disableSaving) {
error("Not saving timers because the file format is unknown.");
return false;
}
cString timersname = AddDirectory(path, "timers.dat");
f = fopen(timersname, "w");
if (f) {
debug("saving webvi timers to %s", (const char *)timersname);
SaveTimers(f, version);
fclose(f);
} else {
LOG_ERROR_STR("Can't save webvi timers");
ok = false;
}
cString historyname = AddDirectory(path, "timers.hst");
f = fopen(historyname, "w");
if (f) {
debug("saving webvi timer history to %s", (const char *)historyname);
SaveHistory(f);
fclose(f);
} else {
LOG_ERROR_STR("Can't save webvi timer history");
ok = false;
}
modified = !ok;
return ok;
}
void cWebviTimerManager::Update() {
cWebviTimer *timer = timers.First();
if (!timer)
return;
time_t now = time(NULL);
#ifdef DEBUG
char timestr[25];
strftime(timestr, 25, "%x %X", localtime(&now));
debug("Running webvi timers update at %s", timestr);
#endif
while (timer) {
if (timer->NextUpdate() < now) {
debug("%d. %s: launching now",
timer->GetID(), timer->GetTitle());
timer->Execute();
} else {
#ifdef DEBUG
time_t next = timer->NextUpdate();
strftime(timestr, 25, "%x %X", localtime(&next));
debug("%d. %s: next update at %s",
timer->GetID(), timer->GetTitle(), timestr);
#endif
}
timer = timers.Next(timer);
}
}
cWebviTimer *cWebviTimerManager::GetByID(int id) const {
cWebviTimer *timer = timers.First();
while (timer) {
if (timer->GetID() == id)
return timer;
timer = timers.Next(timer);
}
return NULL;
}
cWebviTimer *cWebviTimerManager::Create(const char *title,
const char *ref,
bool getExisting) {
cWebviTimer *t;
if (!ref)
return NULL;
if (getExisting) {
t = timers.First();
while (t) {
if (strcmp(t->GetReference(), ref) == 0) {
return t;
}
t = timers.Next(t);
}
}
t = new cWebviTimer(nextID++, title, ref, this);
timers.Add(t);
modified = true;
return t;
}
void cWebviTimerManager::Remove(cWebviTimer *timer) {
timers.Del(timer);
modified = true;
}
void cWebviTimerManager::MarkDownloaded(const char *ref) {
if (!ref)
return;
if (refHistory.Find(ref) == -1) {
refHistory.Append(strdup(ref));
modified = true;
}
}
bool cWebviTimerManager::AlreadyDownloaded(const char *ref) {
return refHistory.Find(ref) != -1;
}
|