findepgentry.cpp

C/C++

No description

Guest

Download Edit

// Find an existing epg entry by eventid
// If no entry exists with the given eventid we'll search
// entries with similiar start time ( starttime/2 <> 10 minutes )
DvbSharedEpgEntry DvbEpgModel::findExistingEntry(DvbEpgEntry &newEntry)
{
DvbSharedEpgEntry existingEntry;
existingEntry = eventIds.value(QPair<DvbSharedChannel, int>(newEntry.channel, newEntry.eventid));
if(existingEntry.isValid()) return existingEntry;
DvbEpgEntry fakeEntry(newEntry.channel);
int newDurSecs = QTime(0,0).secsTo(newEntry.duration);
int allowedDiff = 0;
int durSecs = 0;
QDateTime endTime;
QDateTime newEndTime = newEntry.begin.addSecs(newDurSecs);
//Log("DATA: ") << durSecs << " " << allowedDiff;
for (ConstIdIterator it = entryIds.lowerBound(DvbEpgEntryId(&fakeEntry));
it != entryIds.constEnd(); ++it) {
const DvbSharedEpgEntry &exEntry = *it;
if (!exEntry.isValid() || newEntry.channel != exEntry->channel ) {
continue;
}
durSecs = QTime(0,0).secsTo(exEntry->duration);
endTime = exEntry->begin.addSecs(durSecs);
allowedDiff = (10*60 > (durSecs/2)) ? (durSecs / 2): 10*60; // allowed start/stop difference
// Check if events collide
// if new entry starts after existing entry and existing and ends before newentry starts
if(newEntry.begin.toMSecsSinceEpoch() > exEntry->begin.toMSecsSinceEpoch() && endTime.toMSecsSinceEpoch() <= newEntry.begin.toMSecsSinceEpoch()) continue;
// if existing entry starts after new entry and new ends before existing entry starts
if(exEntry->begin.toMSecsSinceEpoch() > newEntry.begin.toMSecsSinceEpoch() && newEndTime.toMSecsSinceEpoch() <= exEntry->begin.toMSecsSinceEpoch()) continue;
Log("DATA -> : ") << newEntry.begin.toMSecsSinceEpoch() << " " << exEntry->begin.toMSecsSinceEpoch() << " " << (abs(newEntry.begin.toMSecsSinceEpoch() - exEntry->begin.toMSecsSinceEpoch()) / 1000 ) << " " << allowedDiff << " " << durSecs;
// Check if newentry starttime is more than 10 minutes away from old starttime
if((abs(newEntry.begin.toMSecsSinceEpoch() - exEntry->begin.toMSecsSinceEpoch()) / 1000) > allowedDiff ||
// Check if new duration is more than 10 minutes away from old duration
abs(newDurSecs - durSecs) > allowedDiff) continue;
return exEntry;
}
//DvbSharedEpgEntry sharedFakeEntry(&fakeEntry);
//return sharedFakeEntry; // return an invalid entry
return DvbSharedEpgEntry();
}