File: menu_commands.c

package info (click to toggle)
vdr-plugin-epgsearch 2.2.0+git20170817-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 2,788 kB
  • sloc: ansic: 23,630; perl: 1,330; sh: 634; makefile: 226
file content (332 lines) | stat: -rw-r--r-- 10,117 bytes parent folder | download
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
/*                                                                  -*- c++ -*-
Copyright (C) 2004-2013 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_commands.h"
#include <vdr/interface.h>
#include <vdr/menu.h>
#include <vdr/plugin.h>
#include "menu_searchresults.h"
#include "menu_myedittimer.h"
#include "menu_search.h"
#include "menu_searchedit.h"
#include "epgsearchcfg.h"
#include "recdone.h"
#include "epgsearchtools.h"
#include "switchtimer.h"
#include "switchtimer_thread.h"
#include "blacklist.h"
#include "menu_blacklistedit.h"
#include <vdr/menu.h>
#include "menu_deftimercheckmethod.h"

// --- cMenuSearchCommands ---------------------------------------------------------

cMenuSearchCommands::cMenuSearchCommands(const char *Title, const cEvent* Event, bool DirectCall, cSearchExt* Search)
:cOsdMenu(Title)
{
  SetMenuCategory(mcCommand);

   directCall = DirectCall;
   SetHasHotkeys();
   LoadCommands();

   search = Search;
   event = Event;
   Add(new cOsdItem(hk(tr("Repeats"))));
   Add(new cOsdItem(hk(trVDR("Button$Record"))));
   Add(new cOsdItem(hk(trVDR("Button$Switch"))));
   Add(new cOsdItem(hk(tr("Create search"))));
   Add(new cOsdItem(hk(tr("Search in recordings"))));
   Add(new cOsdItem(hk(tr("Mark as 'already recorded'?"))));
   Add(new cOsdItem(hk(tr("Add/Remove to/from switch list?"))));
   Add(new cOsdItem(hk(tr("Create blacklist"))));
   for (cCommand *command = commands.First(); command; command = commands.Next(command))
      Add(new cOsdItem(hk(command->Title())));
   if (event)
   {
     cString szTitle = cString::sprintf("%s: %s", tr("EPG Commands"), event->Title());
     SetTitle(szTitle);
   }
}

cMenuSearchCommands::~cMenuSearchCommands()
{
}

void cMenuSearchCommands::LoadCommands()
{
  const char* szLanguageCode = I18nLanguageCode(I18nCurrentLanguage());

   char *pstrSearchToken, *pptr;
   char *pstrSearch=strdup(szLanguageCode);
   pstrSearchToken=strtok_r(pstrSearch, ",", &pptr);
   bool bLoaded = false;
   while(pstrSearchToken) // may contain multiple code, e.g. 'ger,deu'
   {
     cString cmdFile = cString::sprintf("%s-%s.conf", ADDDIR(CONFIGDIR, "epgsearchcmds"), pstrSearchToken);
      if (access(cmdFile, F_OK) == 0)
      {
         commands.Load(cmdFile, true);
         bLoaded = true;
         break;
      }
      pstrSearchToken=strtok_r(NULL, ",", &pptr);
   }
   if (!bLoaded)
      commands.Load(AddDirectory(CONFIGDIR, "epgsearchcmds.conf"), true);
   free(pstrSearch);
}

eOSState cMenuSearchCommands::Switch(void)
{
   LOCK_CHANNELS_READ;
   const cChannel *channel = Channels->GetByChannelID(event->ChannelID(), true, true);
   if (channel && cDevice::PrimaryDevice()->SwitchChannel(channel, true))
      return osEnd;
   else
   {
     INFO(trVDR("Can't switch channel!"));
     return osContinue;
   }
}

eOSState cMenuSearchCommands::ExtendedSearch(void)
{
   return AddSubMenu(new cMenuEPGSearchExt());
}

eOSState cMenuSearchCommands::Record(void)
{
   if (!event) return osContinue;

   eTimerMatch timerMatch = tmNone;
   LOCK_TIMERS_WRITE;
   Timers->SetExplicitModify();
   cTimer* timer = Timers->GetMatch(event, &timerMatch);
   if (timerMatch == tmFull)
   {
      if (EPGSearchConfig.useVDRTimerEditMenu)
         return AddSubMenu(new cMenuEditTimer(timer));
      else
         return AddSubMenu(new cMenuMyEditTimer(timer, false, event, timer->Channel()));
   }

   timer = new cTimer(event);
   PrepareTimerFile(event, timer);
   cTimer *t = Timers->GetTimer(timer);

   if (EPGSearchConfig.onePressTimerCreation == 0 || t || (!t && event->StartTime() - (Setup.MarginStart+2) * 60 < time(NULL)))
   {
      if (t)
      {
         delete timer;
         timer = t;
      }
      if (EPGSearchConfig.useVDRTimerEditMenu)
         return AddSubMenu(new cMenuEditTimer(timer, !t));
      else
         return AddSubMenu(new cMenuMyEditTimer(timer, !t, event));
   }
   else
   {
      string fullaux = "";
      string aux = "";
      if (event)
      {
         int bstart = event->StartTime() - timer->StartTime();
         int bstop = timer->StopTime() - event->EndTime();
         int checkmode = DefTimerCheckModes.GetMode(timer->Channel());
         aux = UpdateAuxValue(aux, "channel", NumToString(timer->Channel()->Number()) + " - " + CHANNELNAME(timer->Channel()));
         aux = UpdateAuxValue(aux, "update", checkmode);
         aux = UpdateAuxValue(aux, "eventid", event->EventID());
         aux = UpdateAuxValue(aux, "bstart", bstart);
         aux = UpdateAuxValue(aux, "bstop", bstop);
         fullaux = UpdateAuxValue(fullaux, "epgsearch", aux);
      }
#ifdef USE_PINPLUGIN
      aux = "";
      aux = UpdateAuxValue(aux, "protected", timer->FskProtection() ? "yes" : "no");
      fullaux = UpdateAuxValue(fullaux, "pin-plugin", aux);
#endif

      SetAux(timer, fullaux);
      if (Setup.SVDRPPeering && *Setup.SVDRPDefaultHost)
         timer->SetRemote(Setup.SVDRPDefaultHost);
      Timers->Add(timer);
      timer->Matches();
      Timers->SetModified();
      if (!HandleRemoteTimerModifications(timer)) {
         ERROR(tr("Epgsearch: RemoteTimerModifications failed"));
		 Timers->Del(timer);
      }
			else
      return osBack;
   }
   return osContinue;
}

eOSState cMenuSearchCommands::MarkAsRecorded(void)
{
   if (!event) return osContinue;
   if (!Interface->Confirm(tr("Mark as 'already recorded'?")))
      return osContinue;
   cTimer* dummyTimer = new cTimer(event);
   cMutexLock RecsDoneLock(&RecsDone);
   RecsDone.Add(new cRecDone(dummyTimer, event, search));
   RecsDone.Save();
   delete dummyTimer;
   return osBack;
}

eOSState cMenuSearchCommands::AddToSwitchList(void)
{
   if (!event) return osContinue;

   time_t now = time(NULL);
   if (now >= event->StartTime())
   {
     ERROR(tr("Already running!"));
      return osBack;
   }
   cSwitchTimer* switchTimer = SwitchTimers.InSwitchList(event);
   if (!switchTimer)
   {
      if (!Interface->Confirm(tr("Add to switch list?")))
	     return osContinue;
      cMutexLock SwitchTimersLock(&SwitchTimers);
      SwitchTimers.Add(new cSwitchTimer(event));
      SwitchTimers.Save();
      cSwitchTimerThread::Init(); // asure the thread is running
   }
   else
   {
      if (!Interface->Confirm(tr("Delete from switch list?")))
	     return osContinue;
      cMutexLock SwitchTimersLock(&SwitchTimers);
      SwitchTimers.Del(switchTimer);
      SwitchTimers.Save();
      if (SwitchTimers.Count() == 0)
	     cSwitchTimerThread::Exit();
   }
   return osBack;
}

eOSState cMenuSearchCommands::CreateSearchTimer(void)
{
   if (!event) return osContinue;

   cSearchExt* pNew = new cSearchExt;
   strcpy(pNew->search, event->Title());
   LOCK_CHANNELS_READ;
   pNew->channelMin = pNew->channelMax = Channels->GetByChannelID(event->ChannelID());
   return AddSubMenu(new cMenuEditSearchExt(pNew, true, false, true));
}

eOSState cMenuSearchCommands::CreateBlacklist(void)
{
   if (!event) return osContinue;

   cBlacklist* pNew = new cBlacklist;
   strcpy(pNew->search, event->Title());
   return AddSubMenu(new cMenuBlacklistEdit(pNew, true));
}

eOSState cMenuSearchCommands::Execute(void)
{
   int current = Current();
   if (current <= 7)
   {
      if (current == 0)
         return AddSubMenu(new cMenuSearchResultsForQuery(event->Title(), true));
      if (current == 1)
         return Record();
      if (current == 2)
         return Switch();
      if (current == 3)
         return CreateSearchTimer();
      if (current == 4)
         return AddSubMenu(new cMenuSearchResultsForRecs(event->Title()));
      if (current == 5)
         return MarkAsRecorded();
      if (current == 6)
         return AddToSwitchList();
      if (current == 7)
         return CreateBlacklist();
   }

   cCommand *command = commands.Get(current-8);
   if (command) {
      cString buffer;
      bool confirmed = true;
      if (command->Confirm()) {
	buffer = cString::sprintf("%s?", command->Title());
	confirmed = Interface->Confirm(buffer);
      }
      if (confirmed) {
	buffer = cString::sprintf("%s...", command->Title());
	Skins.Message(mtStatus, buffer);

	{
	LOCK_CHANNELS_READ;
	buffer = cString::sprintf("'%s' %ld %ld %d '%s' '%s'",
				  EscapeString(event->Title()).c_str(),
				  event->StartTime(),
				  event->EndTime(),
				  ChannelNrFromEvent(event),
				  EscapeString(Channels->GetByChannelID(event->ChannelID(), true, true)->Name()).c_str(),
				  EscapeString(event->ShortText()?event->ShortText():"").c_str());
	}
	const char *Result = command->Execute(buffer);
	Skins.Message(mtStatus, NULL);
	if (Result)
	  return AddSubMenu(new cMenuText(command->Title(), Result, fontFix));
	return osBack;
      }
   }
   return osContinue;
}

eOSState cMenuSearchCommands::ProcessKey(eKeys Key)
{
   bool hadSubmenu = HasSubMenu();
   eOSState state = cOsdMenu::ProcessKey(Key);

   // jump back to calling menu, if a command was called directly with key '1' .. '9'
   if (directCall && hadSubmenu && !HasSubMenu())
      return osBack;

   if (state == osUnknown) {
      switch (Key) {
         case kGreen:
         case kYellow:
         case kBlue:
            return osContinue;
         case k1...k9:
         case kOk:
            return Execute();
         default:   break;
      }
   }
   return state;
}