Remove Orphan events to be safe
[timedsilencer] / switchingeventlist.cpp
index 088aa72..18111a4 100644 (file)
@@ -93,6 +93,7 @@ void SwitchingEventList::editEvent(QModelIndex index) {
 void SwitchingEventList::loadSavedEvents() {
   qDebug("Loading saved events");
   QSettings settings("TimedSilencer", "TimedSilencer");
+  QList<long> known_cookies;
   QHash<QString, QVariant> events = settings.value("events").toHash();
   // Check for < v0.6 settings
   if(!settings.value("from_time", QTime()).toTime().isNull()) {
@@ -109,6 +110,7 @@ void SwitchingEventList::loadSavedEvents() {
     long to_cookie = settings.value("unsilencing_cookie", 0).toLongLong();
     if(to_cookie > 0)
       pe.alarmd_cookies << to_cookie;
+    known_cookies << pe.alarmd_cookies;
     if(to_cookie <= 0 || from_cookie <= 0)
       pe.activated = false;
     events.insert(pe.getID(), pe.save());
@@ -121,6 +123,7 @@ void SwitchingEventList::loadSavedEvents() {
   bool settings_change = false;
   foreach(QVariant e, events.values()) {
     ProfileEvent *pe = ProfileEvent::load(e);
+    known_cookies << pe->alarmd_cookies;
     // Check if still active
     if(pe->activated && !AlarmdBackend::checkIfStillActive(pe)) {
       qDebug("An existing profile switching event is no longer active, updating its status");
@@ -135,6 +138,8 @@ void SwitchingEventList::loadSavedEvents() {
     // Clean up
     delete pe;
   }
+  // Delete possible orphan events
+  AlarmdBackend::deleteOrphanEvents(known_cookies);
   if(settings_change)
     settings.setValue("events", events);
 }