Early out parsing if we're only processing incoming OR outgoing.
[qwerkisync] / EventParsers / CSVSymbianEventLogParser.cpp
index d4eebdb..43142f1 100644 (file)
 #include <stdexcept>
 
 using namespace EventParsers;
+using EventTypes::PhoneCall;
 
 class SortByValueDesc
 {
 public:
-       inline bool operator()(const QPair<char, uint> &a, const QPair<char, uint> &b) const
+       inline bool operator()(const QPair<QChar, uint> &a, const QPair<QChar, uint> &b) const
        {
                return b.second < a.second;
        }
@@ -41,7 +42,7 @@ public:
 
 const QString ExtractString(const QString &originalString)
 {
-       QRegExp content("^[\"\']?(\\w*)?[\"\']?$");
+       QRegExp content("^[\"\']?(.*)?[\"\']?$");
        content.indexIn(originalString.trimmed());
        return content.cap(1);
 }
@@ -50,30 +51,32 @@ iEventParser *CSVSymbianEventLogParser::IsValid(const Settings &currentSettings,
 {
        qDebug() << "Checking if a CSV call log file...";
 
-       QByteArray firstLineContent(eventFile.readLine());
+       QTextStream stream(&eventFile);
+
+       QString firstLineContent(stream.readLine());
        eventFile.seek(0);
        if(firstLineContent.length() > 0)
        {
                // Count the non-alphanumeric characters used
-               QHash<char, uint> counts;
-               foreach(char c, firstLineContent)
+               QHash<QChar, uint> counts;
+               foreach(const QChar c, firstLineContent)
                        ++counts[c];
 
-               QList<QPair<char, uint> > orderedCounts;
+               QList<QPair<QChar, uint> > orderedCounts;
                orderedCounts.reserve(counts.size());
-               foreach(char c, counts.keys())
+               foreach(const QChar c, counts.keys())
                        if(!QChar(c).isLetterOrNumber())
-                               orderedCounts.append(QPair<char, uint>(c, counts.value(c)));
+                               orderedCounts.append(QPair<QChar, uint>(c, counts.value(c)));
 
                qSort(orderedCounts.begin(), orderedCounts.end(), SortByValueDesc());
 
                // Work around Q_FOREACH macro limitation when dealing with
                // multi-typed templates (comma issue)
-               typedef QPair<char, uint> bodge;
+               typedef QPair<QChar, uint> bodge;
                foreach(bodge count, orderedCounts)
                        qDebug() << count.first << " = " << count.second;
 
-               char delim;
+               QChar delim;
                // No-one would be mad enough to use quotation marks or apostrophes
                // as their delimiter,but just in case, check the second most
                // frequent character is present thr right number of times for
@@ -123,7 +126,7 @@ iEventParser *CSVSymbianEventLogParser::IsValid(const Settings &currentSettings,
        return NULL;
 }
 
-CSVSymbianEventLogParser::CSVSymbianEventLogParser(const Settings &settings, const QString &filename, const char delimiter, const int numColumnsPerRecord, const ColumnIndicesHash &headingIndices)
+CSVSymbianEventLogParser::CSVSymbianEventLogParser(const Settings &settings, const QString &filename, const QChar delimiter, const int numColumnsPerRecord, const ColumnIndicesHash &headingIndices)
        : m_Settings(settings), m_Delimiter(delimiter), m_NumColumnsPerRecord(numColumnsPerRecord), m_HeadingIndices(headingIndices)
 {
 }
@@ -141,25 +144,26 @@ EventTypes::EventFromFileList CSVSymbianEventLogParser::ParseFile(QFile &eventFi
        eventFile.seek(0);
 
        // Read the first line
-       QByteArray firstLineContent(eventFile.readLine());
+       QTextStream stream(&eventFile);
+       QString firstLineContent(stream.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());
+                       .arg(m_NumColumnsPerRecord)
+                       .arg(lineNumber)
+                       .arg(eventFile.fileName()).toStdString());
        ++lineNumber;
 
        // Read the main body of the file
-       while(!eventFile.atEnd())
+       while(!stream.atEnd())
        {
-               QStringList lineValues(QString(eventFile.readLine()).split(m_Delimiter));
+               QStringList lineValues(QString(stream.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));
+                       lineValues.append(QString(stream.readLine()).split(m_Delimiter));
                        ++lineNumber;
                }
 
@@ -172,27 +176,38 @@ EventTypes::EventFromFileList CSVSymbianEventLogParser::ParseFile(QFile &eventFi
                        {
                                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)
+                               QDateTime eTime(QDateTime::fromString(lineValues.at(m_HeadingIndices.value("etime")), "dd/MM/yyyy h:mm:ss ap"));
+                               Settings::eDirection direction(lineValues.at(m_HeadingIndices.value("direction")) == "0"
+                                       ? Settings::INCOMING
+                                       : Settings::OUTGOING);
+
+                               // We only care about the requested directions...
+                               if(CurrentSettings().ShouldProcess(direction, EventTypes::EVENT_TYPE_CALL))
                                {
-                                       qDebug() << QString("Unable to parse '%1' as a duration. Skipping record.")
-                                                                       .arg(lineValues.at(m_HeadingIndices.value("duration")));
-                                       continue;
+                                       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"))));
+                                       // TODO: Not currently used...but almost certainly contains SIP call data
+                                       QString data(ExtractString(lineValues.at(m_HeadingIndices.value("data"))));
+
+                                       if(number.trimmed().length() == 0)
+                                       {
+                                               qDebug() << "Empty tel!";
+                                       }
+
+                                       QSharedPointer<EventTypes::iEvent> newEvent(new PhoneCall(
+                                               CurrentSettings(),
+                                               direction,
+                                               eTime,
+                                               number,
+                                               duration));
+                                       fileEvents.append(EventTypes::EventFromFile(newEvent, recordNumber));
                                }
-                               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));
                        }
                }