// Find an existing epg entry by eventid // If no entry exists with the given eventid we'll search // entries with similar start time ( +-10 minutes ) // TODO: Maybe implement levenshtein for title DvbSharedEpgEntry DvbEpgModel::findExistingEntry(DvbEpgEntry &newEntry) { DvbSharedEpgEntry existingEntry; existingEntry = eventIds.value(QPair(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 even collide // if new entry starts after existing entry and existing ends before new 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 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; } return DvbSharedEpgEntry(); // no match, return invalid entry }