summaryrefslogtreecommitdiff
path: root/block.c
blob: abf54fa101026ae2c7499d11f445c551ea301b75 (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
/**
 * block.c: A plugin for the Video Disk Recorder
 *
 * based on taste.c v 1.1.1.1 2006/02/26 14:11:02 by lordjaxom
 *
 * See the README file for copyright and contact information.
 *
 * version by Midas
 *
 */

#include <getopt.h>
#include <vdr/plugin.h>

#include "status.h"
#include "event.h"
#include "setup.h"
#include "config.h"
#include "i18n.h"
#include "control.h"
#include "common.h"
#include <vdr/interface.h>

static const char *VERSION        = "0.0.4+201008090412";
static const char *DESCRIPTION    = trNOOP("Block unwanted shows by EPG title");
static const char *MAINMENUENTRY  = trNOOP("(De)Block broadcast");

static int channelnumber=0;
#ifdef LOGGING
  char *channel_groupsep_string=(char*)"";  
  char *has_been_checked_string=(char*)"";
  bool temp_replaying_recording=false;
#endif


class cPluginBlock : public cPlugin {
private:
  cStatusBlock *mStatus;

public:
  cPluginBlock(void);
  virtual ~cPluginBlock();
  virtual const char *Version(void) { return VERSION; }
  virtual const char *Description(void) { return tr(DESCRIPTION); }
  virtual bool Initialize(void);
  virtual bool Start(void);
  virtual const char *MainMenuEntry(void) { return SetupBlock.HideMenuEntry ? NULL : tr(MAINMENUENTRY); }
  virtual cOsdObject *MainMenuAction(void);
  virtual cMenuSetupPage *SetupMenu(void);
  virtual bool SetupParse(const char *Name, const char *Value);
  virtual void MainThreadHook(void);
  //virtual bool ProcessArgs(int argc, char *argv[]);
  };

cPluginBlock::cPluginBlock(void):
    cPlugin(),
    mStatus(NULL)
{
  cEventBlock::LastTitle=(char*)"block_dummy_title1";
}

cPluginBlock::~cPluginBlock()
{
  delete mStatus;
}

bool cPluginBlock::Initialize(void)
{
  return EventsBlock.Load(AddDirectory(cPlugin::ConfigDirectory(), "block.conf"), true, false);
}

bool cPluginBlock::Start(void)
{
#if VDRVERSNUM < 10507 
  RegisterI18n(Phrases);
#endif 
  mStatus = new cStatusBlock();
  return true;
}

cOsdObject *cPluginBlock::MainMenuAction(void)
{
  const cChannel *channel = Channels.GetByNumber(cDevice::CurrentChannel());
  if (channel != NULL && !channel->GroupSep()) { 
    cSchedulesLock schedLock;
    const cSchedules *scheds = cSchedules::Schedules(schedLock);
    if (scheds == NULL)
      return NULL;

    const cSchedule *sched = scheds->GetSchedule(channel->GetChannelID());
    if (sched == NULL)
      return NULL;

    const cEvent *present = sched->GetPresentEvent();
    
    if (EventsBlock.Acceptable(present->Title()))
    {
     EventsBlock.Add(new cEventBlock(present->Title()));
     EventsBlock.Sort();
     EventsBlock.Save();
    }
    else
    {
     int index=0;
     const cEventBlock* blockeventpointer = EventsBlock.First();
     while (blockeventpointer != NULL)
     {
      if (strcmp(blockeventpointer->Pattern(),present->Title())==0)
       break;
      index+=1;
      blockeventpointer=EventsBlock.Next(blockeventpointer);
     }      
     cEventBlock *event=EventsBlock.Get(index);
     if (event!=NULL)
     {
      if (Interface->Confirm(tr("Delete keyword?")))
       EventsBlock.Del(event);
       EventsBlock.Save();
     }
    }
    
    cEventBlock::LastTitle="block_dummy_title2";
  }
  return NULL;
}

cMenuSetupPage *cPluginBlock::SetupMenu(void)
{
  return new cMenuSetupBlock();
}

bool cPluginBlock::SetupParse(const char *Name, const char *Value)
{
  return SetupBlock.Parse(Name, Value);
}

void cPluginBlock::MainThreadHook()
{

  if (cSetupBlock::DetectionMethod!=1) return;//other detection method active in setup

  if (cEventBlock::ReplayingRecording) //no block events on the underlying channel processed - user watches recording
  {
#ifdef LOGGING
   if (!temp_replaying_recording)
   {
    temp_replaying_recording=true;
    dsyslog("plugin-block: doing nothing because user watches recording");
   }
#endif   
   return;
  }
#ifdef LOGGING
  else
  {
   if (temp_replaying_recording)
   {
    temp_replaying_recording=false;
    dsyslog("plugin-block: replay of recording ended. Resuming detection mode.");
   }
   return;
  }
#endif      

  channelnumber=cDevice::PrimaryDevice()->CurrentChannel();
  if (channelnumber==0) 
  {
#ifdef LOGGING
   dsyslog("plugin-block: Channel number is 0 => Channel switch on primary device");
#endif
   return; //switch in progress
  }

  const cChannel *channel=Channels.GetByNumber(channelnumber);

  if (channel != NULL && !channel->GroupSep())
  {
#ifdef LOGGING
    char *temp_string;
    asprintf(&temp_string,"channel: %d channel->GroupSep(): %d", channel->Number(), channel->GroupSep());
    if (strcmp(temp_string,channel_groupsep_string)!=0)
    {
     dsyslog("plugin-block: %s",temp_string);
     channel_groupsep_string=temp_string;
    }
#endif
    cSchedulesLock schedLock;
    const cSchedules *scheds = cSchedules::Schedules(schedLock);
            
    if (scheds == NULL)
    {
#ifdef LOGGING
      dsyslog("plugin-block: doing nothing because scheds==NULL");             
#endif      
      return;
    }
                                    
    const cSchedule *sched = scheds->GetSchedule(channel->GetChannelID());
    if (sched == NULL)
    {
     char *dummy;
     asprintf(&dummy, "%jd", (intmax_t)time_ms());
     dsyslog("plugin-block: no EPG data - using dummy for LastTitle: %s",dummy);
     cEventBlock::LastTitle=(char*)dummy;
#ifdef LOGGING    
     dsyslog("plugin-block: doing nothing because sched==NULL");
#endif
     return;
    }
                                                                
    const cEvent *present = sched->GetPresentEvent();
    const cEvent *follow  = sched->GetFollowingEvent();
                                                                        
    if (present == NULL)
    {
#ifdef LOGGING
      dsyslog("plugin-block: doing nothing because present==NULL");
#endif
      return;
    }
                        
    //TODO: check if isrequested is still necessary
//    if (!cControlBlock::IsRequested() && !EventsBlock.Acceptable(present->Title()))
    const char* title=present->Title();
    if (strcmp(title,"")==0)
    {
     char *dummy;
     asprintf(&dummy, "%jd", (intmax_t)time_ms());
     dsyslog("plugin-block: no current EPG title - using dummy for LastTitle: %s",title);
    }
  
    if (strcmp(title,cEventBlock::LastTitle)==0) 
    {
#ifdef LOGGING
     char *temp_string;
     asprintf(&temp_string, "'%s' has already been checked",title);
     if (strcmp(temp_string,has_been_checked_string)!=0)
     {
      dsyslog("plugin-block: %s",temp_string);
     has_been_checked_string=temp_string;
     }
#endif     
     return; //current show has already been checked
    }
#ifdef LOGGING
    dsyslog("plugin-block: new EPG title detected: '%s' - comparing with '%s'",title, cEventBlock::LastTitle);
#endif    
    cEventBlock::LastTitle=(char*)title;
    if (!EventsBlock.Acceptable(title))
    {
     isyslog("plugin-block: channel %d blocked", channelnumber);
     cControl::Launch(new cControlBlock(channel, present, follow));
    }
  }
  
}                                                                                                                                                

/*
bool cPluginBlock::ProcessArgs (int argc, char *argv[])
{
  static const char short_options[] = "p:";
  
  static const struct option long_options[] =
  {
    { "ParentalGuidance", required_argument, NULL, 'p' },
    {NULL}
  };
  
  int c;
  for (c=0;c<argc;c++)
  {
    dsyslog("plugin-block-DEV: argv[%d]=%s",c,argv[c]);
  }
  c=0;
  while ((c = getopt_long(argc,argv,short_options,long_options,NULL))!=-1)
  {
    switch (c)
    {
      case 'p': cSetupBlock::ParentalGuidance=atoi(optarg);
                //dsyslog("plugin-block-DEV: ParentalGuidance set to %s by cl argument.",optarg);
                break;
      default:  //dsyslog("plugin-block-DEV: unrecognized command line option %s",optarg );
                break;//usually return false, but parental guidance could also be set in setup.conf or not at all!
    }
  }
  return true;
}
*/
                                                                                                                                                                                                                                                                                                
VDRPLUGINCREATOR(cPluginBlock); // Don't touch this!