summaryrefslogtreecommitdiff
path: root/menu_myedittimer.c
blob: c727501483788e5b0e5d31a398ac4d4c0af78cba (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
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
/*                                                                  -*- c++ -*-
Copyright (C) 2004-2012 Christian Wieninger

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html

The author can be reached at cwieninger@gmx.de

The project's page is at http://winni.vdr-developer.org/epgsearch
*/

#include <vector>
#include "menu_myedittimer.h"
#include "epgsearchcfg.h"
#include "epgsearchcats.h"
#include "timer_thread.h"
#include "menu_dirselect.h"
#include "menu_searchedit.h"
#include "epgsearchtools.h"
#include "recstatus.h"
#include "uservars.h"
#include "menu_deftimercheckmethod.h"
#include "timerstatus.h"
#include <math.h>

const char *cMenuMyEditTimer::CheckModes[3];

cMenuMyEditTimer::cMenuMyEditTimer(cTimer *Timer, bool New, const cEvent* Event, const cChannel* forcechannel)
:cOsdMenu(trVDR("Edit timer"), 14)
{
#if VDRVERSNUM >= 10728
  SetMenuCategory(mcTimer);
#endif
    CheckModes[0] = tr("no check");
    CheckModes[UPD_CHDUR] = tr("by channel and time");
    CheckModes[UPD_EVENTID] = tr("by event ID");

    strcpy(file, "");
    strcpy(directory, "");
    UserDefDaysOfWeek = 0;
    checkmode = 0;
    if (Timer)
    {
	timer = Timer;
	event = Event;
	flags = Timer->Flags();
	day = Timer->Day();
	weekdays = Timer->WeekDays();

	start = Timer->Start();
	stop = Timer->Stop();
	priority = Timer->Priority();
	lifetime = Timer->Lifetime();
	strcpy(file, Timer->File());
	channel = Timer->Channel()->Number();
#ifdef USE_PINPLUGIN
	fskProtection = Timer->FskProtection();
#endif
	if (forcechannel)
	    channel = forcechannel->Number();
	SplitFile();

	addIfConfirmed = New;
	Set();
	SetHelp(addIfConfirmed?NULL:trVDR("Button$Delete"), NULL, NULL, NULL);
    }
  Timers.IncBeingEdited();
}

void cMenuMyEditTimer::SplitFile()
{
  char* tmp = strrchr(file, '~');
  if (tmp) // split file in real file and directory
    {
      if (event && !isempty(event->ShortText()))
	{
	  cString eventFile = cString::sprintf("%s~%s", event->Title(), event->ShortText());
	  char* tmp2 = strstr(file, eventFile);
	  if (tmp2) // file contains title and subtitle
	    {
	      if (tmp2 > file)
		{
		  *(tmp2-1) = 0;
		  strcpy(directory, file);
		  strcpy(file, tmp2);
		}
	      else
		*directory = 0;
	    }
	  else
	    {
	      *tmp = 0;
	      strcpy(directory, file);
	      strcpy(file, tmp+1);
	    }
	}
      else
	{
	  *tmp = 0;
	  strcpy(directory, file);
	  strcpy(file, tmp+1);
	}
    }
  else
    *directory = 0;
}

void cMenuMyEditTimer::Set()
{
    int current = Current();
    Clear();

    cSearchExt* search = TriggeredFromSearchTimer(timer);

    Add(new cMenuEditStrItem( trVDR("File"), file, MaxFileName, trVDR(FileNameChars)));
    Add(new cMenuEditStrItem( tr("Directory"), directory, MaxFileName, tr(AllowedChars)));
    Add(new cMenuEditBitItem( trVDR("Active"),       &flags, tfActive));
#ifdef USE_PINPLUGIN
    if (cOsd::pinValid) Add(new cMenuEditChanItem(tr("Channel"), &channel));
    else {
      cString buf = cString::sprintf("%s\t%s", tr("Channel"), Channels.GetByNumber(channel)->Name());
      Add(new cOsdItem(buf));
    }
#else
    Add(new cMenuEditChanItem(trVDR("Channel"),      &channel));
#endif
    Add(new cMenuEditDateItem(trVDR("Day"),          &day, &weekdays));

    if (!IsSingleEvent())
	Add(new cMenuEditDateItem(trVDR("First day"), &day));

    Add(new cMenuEditTimeItem(trVDR("Start"),        &start));
    Add(new cMenuEditTimeItem(trVDR("Stop"),         &stop));
    Add(new cMenuEditBitItem( trVDR("VPS"),          &flags, tfVps));
    Add(new cMenuEditIntItem( trVDR("Priority"),     &priority, 0, MAXPRIORITY));
    Add(new cMenuEditIntItem( trVDR("Lifetime"),     &lifetime, 0, MAXLIFETIME));
#ifdef USE_PINPLUGIN
    if (cOsd::pinValid || !fskProtection) Add(new cMenuEditBoolItem(tr("Childlock"),&fskProtection));
    else {
      cString buf = cString::sprintf("%s\t%s", tr("Childlock"), fskProtection ? trVDR("yes") : trVDR("no"));
      Add(new cOsdItem(buf));
    }
#endif

    if (search)
    {
	cMenuEditStrItem* searchtimerItem = new cMenuEditStrItem( tr("Search timer"), search->search, MaxFileName, tr(AllowedChars));
	searchtimerItem->SetSelectable(false);
	Add(searchtimerItem);
    }
    else if (IsSingleEvent() && event)
    {
	checkmode = DefTimerCheckModes.GetMode(Channels.GetByNumber(channel));
	char* checkmodeAux = GetAuxValue(timer, "update");
	if (checkmodeAux)
	{
	    checkmode = atoi(checkmodeAux);
	    free(checkmodeAux);
	}
	Add(new cMenuEditStraItem(tr("Timer check"), &checkmode, 3, CheckModes));
    }

    int deviceNr = gl_recStatusMonitor->TimerRecDevice(timer);
    if (deviceNr > 0)
    {
	cOsdItem* pInfoItem = new cOsdItem("");
	pInfoItem->SetSelectable(false);
	Add(pInfoItem);
	cString info = cString::sprintf("%s: %d/%d", tr("recording with device"), deviceNr, cDevice::NumDevices());
	pInfoItem = new cOsdItem(info);
	pInfoItem->SetSelectable(false);
	Add(pInfoItem);
    }

    if (current > -1)
    {
	SetCurrent(Get(current));
        RefreshCurrent();
    }
    Display();
}

cMenuMyEditTimer::~cMenuMyEditTimer()
{
  if (timer && addIfConfirmed)
     delete timer; // apparently it wasn't confirmed
  Timers.DecBeingEdited();
}

void cMenuMyEditTimer::HandleSubtitle()
{
    const char* ItemText = Get(Current())->Text();
    if (strstr(ItemText, trVDR("File")) != ItemText)
	return;
    if (InEditMode(ItemText, trVDR("File"), file))
	return;

    if (!event || (event && !event->ShortText()))
	return;
    char* tmp = strchr(file, '~');
    if (tmp)
    {
	*tmp = 0;
	SetHelp(addIfConfirmed?NULL:trVDR("Button$Delete"), NULL, trVDR("Button$Reset"), tr("Button$With subtitle"));
    }
    else
    {
	strcat(file, "~");
	strncat(file, event->ShortText(), MaxFileName - strlen(file) - 1);
	SetHelp(addIfConfirmed?NULL:trVDR("Button$Delete"), NULL, trVDR("Button$Reset"), tr("Button$Without subtitle"));
    }
    RefreshCurrent();
    Display();
}

bool cMenuMyEditTimer::IsSingleEvent(void) const
{
  return !weekdays;
}

eOSState cMenuMyEditTimer::DeleteTimer()
{
    // Check if this timer is active:
    if (timer && !addIfConfirmed) {
	if (Interface->Confirm(trVDR("Delete timer?"))) {
	    if (timer->Recording()) {
		if (Interface->Confirm(trVDR("Timer still recording - really delete?"))) {
		    timer->Skip();
		    cRecordControls::Process(time(NULL));
		}
		else
		    return osContinue;
	    }
	    LogFile.iSysLog("deleting timer %s", *timer->ToDescr());
	    Timers.Del(timer);
	    gl_timerStatusMonitor->SetConflictCheckAdvised();
	    Timers.SetModified();
	    return osBack;
        }
    }
    return osContinue;
}

eOSState cMenuMyEditTimer::ProcessKey(eKeys Key)
{
    bool bWasSingleEvent = IsSingleEvent();

    eOSState state = cOsdMenu::ProcessKey(Key);

    if (bWasSingleEvent != IsSingleEvent())
    {
	Set();
	Display();
    }

    int iOnDirectoryItem = 0;
    int iOnFileItem = 0;
    int iOnDayItem = 0;
    const char* ItemText = Get(Current())->Text();
    if (!HasSubMenu() && ItemText && strlen(ItemText)>0)
    {
	if (strstr(ItemText, trVDR("Day")) == ItemText)
	{
	    if (!IsSingleEvent())
	    {
		SetHelp(trVDR("Button$Edit"));
		iOnDayItem = 1;
	    }
	    else
		SetHelp(addIfConfirmed?NULL:trVDR("Button$Delete"), NULL, NULL, NULL);
	}
	else if (strstr(ItemText, tr("Directory")) == ItemText)
	{
	    if (!InEditMode(ItemText, tr("Directory"), directory))
		SetHelp(addIfConfirmed?NULL:trVDR("Button$Delete"), NULL, trVDR("Button$Reset"), tr("Button$Select"));
	    iOnDirectoryItem = 1;
	}
	else if (strstr(ItemText, trVDR("File")) == ItemText)
	{
	    if (!InEditMode(ItemText, trVDR("File"), file))
	    {
		if (event && event->ShortText())
		{
		    if (strchr(file, '~'))
			SetHelp(addIfConfirmed?NULL:trVDR("Button$Delete"), NULL, trVDR("Button$Reset"), tr("Button$Without subtitle"));
		    else
			SetHelp(addIfConfirmed?NULL:trVDR("Button$Delete"), NULL, trVDR("Button$Reset"), tr("Button$With subtitle"));
		}
		else
		    SetHelp(addIfConfirmed?NULL:trVDR("Button$Delete"), NULL, trVDR("Button$Reset"), NULL);
	    }
	    iOnFileItem = 1;
	}
	else
	    SetHelp(addIfConfirmed?NULL:trVDR("Button$Delete"), NULL, NULL, NULL);
    }

    if ((Key == kYellow) && ((iOnDirectoryItem && !InEditMode(ItemText, tr("Directory"), directory)) ||
			     (iOnFileItem && !InEditMode(ItemText, trVDR("File"), file))))
    {
	if (iOnDirectoryItem)
	    strcpy(directory, "");
	if (iOnFileItem && event)
	    strn0cpy(file, event->Title(),sizeof(file));
	RefreshCurrent();
	Display();
    }

    if (state == osUnknown)
    {
	switch (Key)
	{
	    case kOk:
	    {
		cChannel *ch = Channels.GetByNumber(channel);
		if (!ch)
		{
		    Skins.Message(mtError, tr("*** Invalid Channel ***"));
		    break;
		}

		string fullaux = "";
        string aux = "";
		if (timer && timer->Aux())
		    fullaux = timer->Aux();

		if (event && IsSingleEvent())
		{
		    time_t startTime = 0, stopTime = 0;;
		    int begin  = cTimer::TimeToInt(start); // seconds from midnight
		    int length = cTimer::TimeToInt(stop) - begin;
		    if (length < 0)
			length += SECSINDAY;
		    startTime = cTimer::SetTime(day, begin);
		    stopTime = startTime + length;
                    // calculate margins
		    int bstart = event->StartTime() - startTime;
		    int bstop = stopTime - event->EndTime();

		    char* epgsearchaux = GetAuxValue(timer, "epgsearch");
		    if (epgsearchaux)
		    {
			aux = epgsearchaux;
			free(epgsearchaux);
		    }
		    aux = UpdateAuxValue(aux, "channel", NumToString(ch->Number()) + " - " + CHANNELNAME(ch));
		    aux = UpdateAuxValue(aux, "update", checkmode);
		    aux = UpdateAuxValue(aux, "eventid", event->EventID());
		    aux = UpdateAuxValue(aux, "bstart", bstart);
		    aux = UpdateAuxValue(aux, "bstop", bstop);
		    if (addIfConfirmed) // do not update start and stop time in aux if this is an existing timer, we need this to detect manual editing
		      {
			aux = UpdateAuxValue(aux, "start", startTime);
			aux = UpdateAuxValue(aux, "stop", stopTime);
		      }
		    fullaux = UpdateAuxValue(fullaux, "epgsearch", aux);
		}

#ifdef USE_PINPLUGIN
        aux = "";
        aux = UpdateAuxValue(aux, "protected", fskProtection ? "yes" : "no");
        fullaux = UpdateAuxValue(fullaux, "pin-plugin", aux);
#endif

		char* tmpFile = strdup(file);
		strreplace(tmpFile, ':', '|');
		char* tmpDir = strdup(directory);
		strreplace(tmpDir, ':', '|');
		if (strlen(tmpFile) == 0)
		{
		    free(tmpFile);
		    tmpFile = strdup(CHANNELNAME(ch));
		}

                if (timer)
                {
                    cString cmdbuf;
                    cmdbuf = cString::sprintf("%d:%d:%s:%04d:%04d:%d:%d:%s%s%s:%s",
                       flags,
                       ch->Number(),
                       PRINTDAY(day, weekdays, true),
                       start,
                       stop,
                       priority,
                       lifetime,
                       strlen(tmpDir)>0?tmpDir:"",
                       (strlen(tmpDir)>0 && strlen(tmpFile)>0)?"~":"",
                       tmpFile,
                       fullaux.c_str());

                    timer->Parse(cmdbuf);

                    free(tmpFile);
                    free(tmpDir);

                    if (addIfConfirmed)
                      Timers.Add(timer);
                    timer->SetEventFromSchedule();
                    timer->Matches();
                    gl_timerStatusMonitor->SetConflictCheckAdvised();
                    Timers.SetModified();
                    addIfConfirmed = false;
                } else {
		    free(tmpFile);
		    free(tmpDir);
		}
	    }
	    return osBack;
	    case kRed:
		if (HasSubMenu())
		    return osContinue;
		else
		    if (iOnDayItem)
			return AddSubMenu(new cMenuEditDaysOfWeek(&weekdays,1,false));
		    else
			return DeleteTimer();
		break;
	    case kGreen:
	    case kYellow:
		return osContinue;
	    case kBlue:
		if (HasSubMenu())
		    return osContinue;
		if (iOnDirectoryItem && !InEditMode(ItemText, tr("Directory"), directory))
		    state = AddSubMenu(new cMenuDirSelect(directory));
		if (iOnFileItem)
		{
		    HandleSubtitle();
		    return osContinue;
		}
		break;
	    default: break;
	}
    }

    if (Key != kNone && !HasSubMenu())
    {
	if (iOnDirectoryItem && !InEditMode(ItemText, tr("Directory"), directory))
	    ReplaceDirVars();
    }
    return state;
}

void cMenuMyEditTimer::ReplaceDirVars()
{
    if (!strchr(directory,'%') || !event)
	return;

    cVarExpr varExpr(directory);
    strcpy(directory, varExpr.Evaluate(event).c_str());
    if (strchr(directory, '%') != NULL) // only set directory to new value if all categories could have been replaced
	*directory = 0;
    if (varExpr.DependsOnVar("%title%", event) || varExpr.DependsOnVar("%subtitle%", event))
    {
	strcpy(file, directory);
	*directory = 0;
	SplitFile();
    }

    RefreshCurrent();
    // update title too
    if (Current() > 0)
	Get(Current()-1)->Set();
    Display();
    return;
}