summaryrefslogtreecommitdiff
path: root/equivhandler.c
blob: a742150c42d91b45d6ee5b44235934fba2e5e367 (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
/*
 * equivhandler.cpp
 *
 *  Created on: 19.5.2012
 *      Author: d.petrovski
 */

#include "equivhandler.h"
#include "setupeepg.h"
#include "log.h"
#include "util.h"

#include <string>

using namespace util;

multimap<string, string> cEquivHandler::equiChanMap;
long cEquivHandler::equiChanFileTime = 0;

cEquivHandler::cEquivHandler()
{
  loadEquivalentChannelMap();
}

cEquivHandler::~cEquivHandler()
{
  // TODO Auto-generated destructor stub
}

void cEquivHandler::loadEquivalentChannelMap (void)
{
  char Buffer[1024];
  char *Line;
  FILE *File;
  string FileName = string(cSetupEEPG::getInstance()->getConfDir()) + "/" + EEPG_FILE_EQUIV;
  multimap<string,string>::iterator it,it2;
  pair<multimap<string,string>::iterator,multimap<string,string>::iterator> ret;

  //Test if file is changed and reload
  struct stat st;
  if (stat(FileName.c_str(), &st)) {
      LogE(0, prep("Error obtaining stats for '%s' "), FileName.c_str());
      return;
  }

  if (equiChanMap.size() > 0 &&  equiChanFileTime == st.st_mtim.tv_nsec)
    return;
  else
    equiChanMap.clear();


  File = fopen (FileName.c_str(), "r");
  if (File) {
    memset (Buffer, 0, sizeof (Buffer));
    char origChanID[256];
    char equiChanID[256];
    char source[256];
    int nid = 0;
    int tid = 0;
    int sid = 0;
    int rid = 0;
    while ((Line = fgets (Buffer, sizeof (Buffer), File)) != NULL) {
      Line = compactspace (skipspace (stripspace (Line)));
      if (!isempty (Line)) {
        if (sscanf (Line, "%[^ ] %[^ ] %[^\n]\n", origChanID, equiChanID, source) == 3) {
          if (origChanID[0] != '#' && origChanID[0] != ';') {
            nid = 0;
            tid = 0;
            sid = 0;
            rid = 0;
            if (sscanf (origChanID, "%[^-]-%i -%i -%i ", source, &nid, &tid, &sid) == 4)
              if (sscanf (equiChanID, "%[^-]-%i -%i -%i ", source, &nid, &tid, &sid) == 4) {
                if (sscanf (origChanID, "%[^-]-%i -%i -%i -%i ", source, &nid, &tid, &sid, &rid) != 5) {
                  rid = 0;
                }
                tChannelID OriginalChID = tChannelID (cSource::FromString (source), nid, tid, sid, rid);
                bool found = false;
                //int i = 0;
                cChannel *OriginalChannel = Channels.GetByChannelID (OriginalChID, false);
                if (!OriginalChannel) {
                  LogI(2, prep("Warning, not found epg channel \'%s\' in channels.conf. Equivalency is assumed to be valid, but perhaps you should check the entry in the equivalents file"), origChanID); //TODO: skip this ing?
                  continue;
                }
                if (sscanf (equiChanID, "%[^-]-%i -%i -%i ", source, &nid, &tid, &sid) == 4) {
                  if (sscanf (equiChanID, "%[^-]-%i -%i -%i -%i ", source, &nid, &tid, &sid, &rid)
                    != 5) {
                    rid = 0;
                  }
                  tChannelID EquivChID = tChannelID (cSource::FromString (source), nid, tid, sid, rid);
                  cChannel *EquivChannel = Channels.GetByChannelID (EquivChID, false); //TODO use valid function?
                  if (EquivChannel) {
                    ret = equiChanMap.equal_range(*OriginalChID.ToString());
                    for (it=ret.first; it!=ret.second; ++it)
                      if ((*it).second ==  *OriginalChID.ToString()) {
                        found = true;
                        break;
                      }

                    if (!found) {
                      string origCh(*OriginalChID.ToString());
                      string equiCh(*EquivChID.ToString());
                      equiChanMap.insert(pair<string,string>(origCh.c_str(),equiCh.c_str()));
                      LogD(4, prep("Found %s equivalent to %s. origCh %s"), *EquivChID.ToString(), *OriginalChID.ToString(), origCh.c_str());
                      for ( it2=equiChanMap.begin() ; it2 != equiChanMap.end(); it2++ )
                        LogD(3, prep("Original ID %s <-> Equivalent ID %s"), (*it2).first.c_str(), it2->second.c_str());
                    }
                  } else
                    LogI(0, prep("Warning, not found equivalent channel \'%s\' in channels.conf"), equiChanID);
                }
              }   //if scanf string1
          }   //if string1
        }     //if scanf
      }    //if isempty
    }      //while
    fclose (File);
    equiChanFileTime = st.st_mtim.tv_nsec;
    LogD(2, prep("Loaded %i equivalents."), equiChanMap.size());
    for ( it2=equiChanMap.begin() ; it2 != equiChanMap.end(); it2++ )
      LogD(2, prep("Original ID %s <-> Equivalent ID %s"), (*it2).first.c_str(), it2->second.c_str());
  }  //if file
}

void cEquivHandler::updateEquivalent(cSchedules * Schedules, tChannelID channelID, cEvent *pEvent){
  multimap<string,string>::iterator it;
  pair<multimap<string,string>::iterator,multimap<string,string>::iterator> ret;

  LogD(2, prep("Start updateEquivalent %s"), *channelID.ToString());

  ret = equiChanMap.equal_range(*channelID.ToString());
  for (it=ret.first; it!=ret.second; ++it) {
    LogD(2, prep("equivalent channel exists"));
    tChannelID equChannelID (tChannelID::FromString((*it).second.c_str()));
    cChannel *equChannel = GetChannelByID (equChannelID, false);
    if (equChannel) {
      LogD(2, prep("found Equivalent channel %s"), *equChannelID.ToString());
      cSchedule *pSchedule = (cSchedule *) Schedules->GetSchedule (equChannel, true);
      cEvent *pEqvEvent = (cEvent *) pSchedule->GetEvent (pEvent->EventID(), pEvent->StartTime());
      if (pEqvEvent) {
        LogD(3, prep("equivalent event exists"));
        if (pEqvEvent == pEvent) {
          LogD(3, prep("equal event exists"));

        } else {
          LogD(2, prep("remove equivalent"));
          pSchedule->DelEvent(pEqvEvent);
          cEvent* newEvent = new cEvent (pEvent->EventID());
          cloneEvent(pEvent, newEvent);

          pSchedule->AddEvent(newEvent);

        }

      } else {
        LogD(3, prep("equivalent event does not exist"));
        cEvent* newEvent = new cEvent (pEvent->EventID());
        cloneEvent(pEvent, newEvent);

        pSchedule->AddEvent(newEvent);

      }
    }
  }
}

void cEquivHandler::updateEquivalent(tChannelID channelID, cEvent *pEvent){
  multimap<string,string>::iterator it;
  pair<multimap<string,string>::iterator,multimap<string,string>::iterator> ret;

  LogD(3, prep("Start updateEquivalent %s"), *channelID.ToString());

  ret = equiChanMap.equal_range(*channelID.ToString());
  for (it=ret.first; it!=ret.second; ++it) {
    LogD(3, prep("equivalent channel exists"));
    tChannelID equChannelID (tChannelID::FromString((*it).second.c_str()));
    cEvent* newEvent = new cEvent (pEvent->EventID());
    cloneEvent(pEvent, newEvent);

    AddEvent(newEvent, equChannelID);
  }
}


void cEquivHandler::sortEquivalents(tChannelID channelID, cSchedules* Schedules)
{
  multimap<string, string>::iterator it;
  pair < multimap < string, string > ::iterator, multimap < string, string
      > ::iterator > ret;
  LogD(2, prep("sortEquivalents for channel %s count: %d"), *channelID.ToString(), cEquivHandler::getEquiChanMap().count(*channelID.ToString()));
  
  ret = equiChanMap.equal_range(*channelID.ToString());
  for (it = ret.first; it != ret.second; ++it)
  {
    LogD(3, prep("equivalent channel exists"));
    tChannelID equChannelID(tChannelID::FromString((*it).second.c_str()));
    cChannel* pChannel = GetChannelByID(equChannelID, false);
    if (pChannel)
    {
      LogD(2, prep("found Equivalent channel %s"), *equChannelID.ToString());
      cSchedule* pSchedule = (cSchedule *) Schedules->GetSchedule(pChannel, true);

      pSchedule->Sort();
      Schedules->SetModified(pSchedule);
    }
  }
}

void cEquivHandler::cloneEvent(cEvent *Source, cEvent *Dest) {

  Dest->SetEventID(Source->EventID());
  Dest->SetTableID(Source->TableID());
  Dest->SetVersion(Source->Version());
  Dest->SetRunningStatus(Source->RunningStatus());
  Dest->SetTitle(Source->Title());
  Dest->SetShortText(Source->ShortText());
  Dest->SetDescription(Source->Description());
  cComponents *components = new cComponents();
  if (Source->Components()) {
     for (int i = 0; i < Source->Components()->NumComponents(); ++i)
         components->SetComponent(i, Source->Components()->Component(i)->ToString());
     }
  Dest->SetComponents(components);
  uchar contents[MaxEventContents];
  for (int i = 0; i < MaxEventContents; ++i)
      contents[i] = Source->Contents(i);
  Dest->SetContents(contents);
  Dest->SetParentalRating(Source->ParentalRating());
  Dest->SetStartTime(Source->StartTime());
  Dest->SetDuration(Source->Duration());
  Dest->SetVps(Source->Vps());
  if (Source->Seen())
     Dest->SetSeen();
}