Slight changes to error handling during RtcomEventLogger insertions to better handle...
[qwerkisync] / DBBackends / RtcomEventLogger.cpp
index 7957baf..20daa23 100644 (file)
@@ -23,6 +23,7 @@
 #include "EventTypes/iEvent.h"
 #include "EventTypes/PhoneCall.h"
 #include "EventTypes/SMS.h"
+#include "RtcomEventLoggerComponents/TriggerDisabler.h"
 #include "Settings.h"
 
 #include <QDebug>
@@ -53,7 +54,7 @@ QDebug operator<<(QDebug, GList &);
 QDebug operator<<(QDebug, QList<RTComElAttachment*> &);
 
 RtcomEventLogger::RtcomEventLogger(const Settings &settings) :
-       m_Settings(settings), mk_DBPath("/.rtcom-eventlogger/el-v1.db")
+       m_Settings(settings)
 {
        RTComEl *el(rtcom_el_new());
        if(NULL != el)
@@ -76,7 +77,7 @@ RtcomEventLogger::RtcomEventLogger(const Settings &settings) :
 }
 
 RtcomEventLogger::RtcomEventLogger(const Settings &settings, const EventTypes::RtcomEvent &event) :
-       m_Settings(settings), mk_DBPath("/.rtcom-eventlogger/el-v1.db")
+       m_Settings(settings)
 {
 }
 
@@ -99,8 +100,8 @@ void RtcomEventLogger::ProcessService(EventProcessors::iEventProcessor &processo
 {
        RTComEl *el_nonconst(const_cast<RTComEl *>(&el));
 
-       bool incoming = CurrentSettings().ShouldProcess( Settings::TYPE_RECIEVED, service);
-       bool outgoing = CurrentSettings().ShouldProcess( Settings::TYPE_SENT, service);
+       bool incoming = CurrentSettings().ShouldProcess( Settings::INCOMING, service);
+       bool outgoing = CurrentSettings().ShouldProcess( Settings::OUTGOING, service);
 
        if(incoming || outgoing)
        {
@@ -171,7 +172,7 @@ void RtcomEventLogger::ProcessService(EventProcessors::iEventProcessor &processo
 
                                                                                QList<RTComElAttachment *> rattachments;
                                                                                RTComElAttachIter *at_it = rtcom_el_iter_get_attachments(it);
-                                                                               if(it != NULL)
+                                                                               if(at_it != NULL)
                                                                                {
                                                                                        qDebug() << "Attachments OK";
                                                                                        if(rtcom_el_attach_iter_first(at_it))
@@ -239,6 +240,11 @@ EventTypes::iEvent *const RtcomEventLogger::CreateEvent(RTComElEvent &revent, QL
        return NULL;
 }
 
+void RtcomEventLogger::PreInsert()
+{
+       m_TriggerDisabler = new RtcomEventLoggerComponents::TriggerDisabler(CurrentSettings());
+}
+
 void RtcomEventLogger::Insert(EventTypes::iEvent &event, const NumberToNameLookup &numberToNameLookup)
 {
        if(EventTypes::RtcomEvent *rtcomEvent = dynamic_cast<EventTypes::RtcomEvent *>(&event))
@@ -270,13 +276,13 @@ void RtcomEventLogger::Insert(EventTypes::iEvent &event, const NumberToNameLooku
                        // Add the event
                        QDateTime startTime(QDateTime::currentDateTimeUtc());
                        int newEventID(-1);
+                       int currentBackoffInMillisecs(5);
                        while(((newEventID = rtcom_el_add_event_full(el, revent, rheaders, rattachments, &error)) == -1)
-                                 && startTime.msecsTo(QDateTime::currentDateTimeUtc()) < 1000)
+                                 && startTime.msecsTo(QDateTime::currentDateTimeUtc()) < 10000)
                        {
                                if(error != NULL)
                                {
                                        qDebug() << "err: " << error->message;
-                                       qDebug() << *revent << "\n";
                                        g_error_free(error);
                                        error = NULL;
                                }
@@ -286,18 +292,24 @@ void RtcomEventLogger::Insert(EventTypes::iEvent &event, const NumberToNameLooku
                                mutex.lock();
 
                                QWaitCondition waitCondition;
-                               waitCondition.wait(&mutex, 5);
+                               waitCondition.wait(&mutex, currentBackoffInMillisecs);
 
                                mutex.unlock();
+
+                               // Exponential backoff...
+                               currentBackoffInMillisecs *= 2;
                        }
 
                        if(-1 == newEventID)
                        {
+                               qDebug() << "Unable to insert event due to error.";
+                               qDebug() << *revent << "\n";
+                       }
+                       else
+                       {
                                qDebug() << "new id: " << newEventID;
                                InsertedIDs().append(newEventID);
                        }
-                       else
-                               qDebug() << "Unable to insert event due to error.";
 
                        // Release the attachments
                        g_list_foreach (rattachments, (GFunc) rtcom_el_free_attachment, NULL);
@@ -320,6 +332,8 @@ void RtcomEventLogger::PostInsert()
        // Reorder the DB IDs as Nokia are guilty of both premature
        // optimisation as well as closed source UIs...
        Reindex();
+
+       delete m_TriggerDisabler;
 }
 
 void RtcomEventLogger::ClearInsertedIDs()
@@ -332,10 +346,10 @@ void RtcomEventLogger::ClearInsertedIDs()
 void RtcomEventLogger::Reindex()
 {
        // Set up the database connection...
-       QSqlDatabase db(QSqlDatabase::addDatabase( "QSQLITE" ));
+       QSqlDatabase db(QSqlDatabase::addDatabase("QSQLITE"));
 
-       db.setDatabaseName( QDir::homePath() + mk_DBPath );
-       if ( ! db.open() )
+       db.setDatabaseName(CurrentSettings().DBPath());
+       if(!db.open())
        {
                throw std::runtime_error("Cannot open database: Unable to establish database connection");
        }