Added parsing of CSV-formatted Symbian event logs
[qwerkisync] / EventParsers / CSVSymbianEventLogParser.cpp
index 8d38ffe..d4eebdb 100644 (file)
@@ -17,6 +17,8 @@
  */
 
 #include "CSVSymbianEventLogParser.h"
+#include "EventTypes/PhoneCall.h"
+#include "Settings.h"
 
 #include <QDebug>
 
@@ -24,6 +26,8 @@
 #include <QString>
 #include <QStringList>
 
+#include <stdexcept>
+
 using namespace EventParsers;
 
 class SortByValueDesc
@@ -35,7 +39,14 @@ public:
        }
 };
 
-iEventParser *CSVSymbianEventLogParser::IsValid(QFile &eventFile)
+const QString ExtractString(const QString &originalString)
+{
+       QRegExp content("^[\"\']?(\\w*)?[\"\']?$");
+       content.indexIn(originalString.trimmed());
+       return content.cap(1);
+}
+
+iEventParser *CSVSymbianEventLogParser::IsValid(const Settings &currentSettings, QFile &eventFile)
 {
        qDebug() << "Checking if a CSV call log file...";
 
@@ -80,18 +91,17 @@ iEventParser *CSVSymbianEventLogParser::IsValid(QFile &eventFile)
                // Check we have the essential fields we need, and grab their
                // column ordering
                QStringList requiredHeadings;
-               requiredHeadings << "etype" << "etime" << "remote"
-                                                << "direction" << "duration" << "number";
+               requiredHeadings << "etype" << "etime" << "remote" << "direction"
+                                                << "duration" << "number" << "data";
 
                EventParsers::CSVSymbianEventLogParser::ColumnIndicesHash headingPositions;
                headingPositions.reserve(requiredHeadings.count());
 
-               QStringList headings(QString(firstLineContent).split(delim));
+               QStringList headings(QString(firstLineContent).split(delim, QString::KeepEmptyParts, Qt::CaseSensitive));
+               int numColumnsPerRecord(headings.count());
                for(QStringList::size_type i(0); i < headings.count(); ++i)
                {
-                       QRegExp content("^[\"\']?(\\w*)?[\"\']?$");
-                       content.indexIn(headings.value(i).trimmed());
-                       QString heading(content.cap(1));
+                       QString heading(ExtractString(headings.value(i)));
                        qDebug() << headings.value(i) << " : " << heading;
 
                        // Check over the required headings
@@ -107,20 +117,89 @@ iEventParser *CSVSymbianEventLogParser::IsValid(QFile &eventFile)
 
                // If we found all of the required headings, continue
                if(requiredHeadings.count() == 0)
-               {
-                       return new EventParsers::CSVSymbianEventLogParser(eventFile.fileName(), headingPositions);
-               }
+                       return new EventParsers::CSVSymbianEventLogParser(currentSettings, eventFile.fileName(), delim, numColumnsPerRecord, headingPositions);
        }
 
        return NULL;
 }
 
-CSVSymbianEventLogParser::CSVSymbianEventLogParser(const QString &filename, const ColumnIndicesHash &columns)
+CSVSymbianEventLogParser::CSVSymbianEventLogParser(const Settings &settings, const QString &filename, const char delimiter, const int numColumnsPerRecord, const ColumnIndicesHash &headingIndices)
+       : m_Settings(settings), m_Delimiter(delimiter), m_NumColumnsPerRecord(numColumnsPerRecord), m_HeadingIndices(headingIndices)
 {
 }
 
 EventTypes::EventFromFileList CSVSymbianEventLogParser::ParseFile(QFile &eventFile, const QList<uint> &recordsToReturn)
 {
        qDebug() << "CSV Parsing NYI!";
-       return EventTypes::EventFromFileList();
+       EventTypes::EventFromFileList fileEvents;
+       //return fileEvents;
+
+       QSet<uint> recordsToReturnSet(QSet<uint>::fromList(recordsToReturn));
+
+       uint lineNumber(0);
+       uint recordNumber(0);
+       eventFile.seek(0);
+
+       // Read the first line
+       QByteArray firstLineContent(eventFile.readLine());
+       QStringList firstLineValues(QString(firstLineContent).split(m_Delimiter));
+       if(firstLineValues.count() != m_NumColumnsPerRecord)
+               throw new std::runtime_error(QString("Unexpected number of columns (%1, expected %2) on line %3 of %4")
+                       .arg(firstLineValues.count())
+                        .arg(m_NumColumnsPerRecord)
+                        .arg(lineNumber)
+                        .arg(eventFile.fileName()).toStdString());
+       ++lineNumber;
+
+       // Read the main body of the file
+       while(!eventFile.atEnd())
+       {
+               QStringList lineValues(QString(eventFile.readLine()).split(m_Delimiter));
+               ++lineNumber;
+               // Make sure we have enough columns (i.e. handle newlines in values)
+               while(lineValues.count() < m_NumColumnsPerRecord)
+               {
+                       lineValues.append(QString(eventFile.readLine()).split(m_Delimiter));
+                       ++lineNumber;
+               }
+
+               if(recordsToReturnSet.count() == 0 || recordsToReturnSet.contains(recordNumber))
+               {
+                       bool bOK(false);
+                       int eType(lineValues.at(m_HeadingIndices.value("etype")).toUInt(&bOK));
+                       // We're only interested in phone calls
+                       if(bOK && eType == 0)
+                       {
+                               qDebug() << "Parsing event from line #" << lineNumber << ". Values: " << lineValues;
+
+                               QDateTime eTime(QDateTime::fromString(lineValues.at(m_HeadingIndices.value("etime")), "dd/MM/yyyy hh:mm:ss"));
+                               EventTypes::PhoneCall::eDestination direction(lineValues.at(m_HeadingIndices.value("direction")) == "0"
+                                       ? EventTypes::PhoneCall::INCOMING
+                                       : EventTypes::PhoneCall::OUTGOING);
+                               int duration(lineValues.at(m_HeadingIndices.value("duration")).toInt(&bOK));
+                               if(!bOK)
+                               {
+                                       qDebug() << QString("Unable to parse '%1' as a duration. Skipping record.")
+                                                                       .arg(lineValues.at(m_HeadingIndices.value("duration")));
+                                       continue;
+                               }
+                               QString number(ExtractString(lineValues.at(m_HeadingIndices.value("number"))));
+                               QString data(ExtractString(lineValues.at(m_HeadingIndices.value("data"))));
+
+                               QSharedPointer<EventTypes::iEvent> newEvent(new EventTypes::PhoneCall(
+                                       CurrentSettings(),
+                                       direction,
+                                       eTime,
+                                       number,
+                                       duration));
+                               fileEvents.append(EventTypes::EventFromFile(newEvent, recordNumber));
+                       }
+               }
+
+               ++recordNumber;
+       }
+
+       qDebug() << QString("File pos: %1, bAvail: %2, canReadLine: %3").arg(eventFile.pos()).arg(eventFile.bytesAvailable()).arg(eventFile.canReadLine());
+       qDebug() << fileEvents.count() << " events loaded from file";
+       return fileEvents;
 }