Early out parsing if we're only processing incoming OR outgoing.
[qwerkisync] / EventParsers / VMGEntities / VEnvelope.cpp
1 /*
2  * Copyright (C) 2011, Jamie Thompson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License as published by the Free Software Foundation; either
7  * version 3 of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; If not, see
16  * <http://www.gnu.org/licenses/>.
17  */
18
19 #include "VEnvelope.h"
20
21 #include "Factory.h"
22 #include "VBody.h"
23
24 #include <QTextStream>
25
26 #include <QDebug>
27
28 using namespace EventParsers::VMGEntities;
29
30 VEnvelope::VEnvelope(const Settings &settings, const SMSEntity* parent) :
31         SMSEntity(settings, parent)
32 {
33 }
34
35 //VEnvelope::VEnvelope(QTextStream& stream)
36 //{
37 //}
38
39 VEnvelope::~VEnvelope()
40 {
41         foreach(iWriter *contentWriter, m_ContentWriters)
42                 delete contentWriter;
43
44         m_ContentWriters.empty();
45 }
46
47 void VEnvelope::Write(QTextStream &stream, const EventTypes::SMS &event)
48 {
49         stream << "BEGIN:" << getTagName() << "\n";
50
51         foreach(iWriter *contentWriter, m_ContentWriters)
52                 contentWriter->Write(stream, event);
53
54         stream << "END:" << getTagName() << "\n";
55 }
56
57 bool VEnvelope::Read(const QString &initialLine, QTextStream &stream, EventTypes::SMS &event)
58 {
59         bool hasEnded(false);
60
61         // Stream may or may not have a 'BEGIN' present. Swallow it if it's ours.
62         QString lineData(initialLine.length() > 0 ? initialLine : stream.readLine());
63         if(lineData.startsWith("BEGIN:"))
64         {
65                 if(lineData != QString("BEGIN:") + getTagName())
66                 {
67                         qDebug() << "Invalid stream";
68                         return false;
69                 }
70                 else // ...discard this line
71                         lineData = stream.readLine();
72         }
73
74         do
75         {
76                 if(lineData.startsWith("BEGIN:"))
77                 {
78                         iReader* reader = Factory::Instantiate(CurrentSettings(), lineData, this);
79                         bool valid(NULL != reader && reader->Read(lineData, stream, event));
80                         delete reader;
81
82                         // Quit processing if the nested content is not valid
83                         if(!valid)
84                                 return valid;
85                 }
86                 else if(lineData.startsWith("END:"))
87                 {
88                         if(lineData != QString("END:") + getTagName())
89                         {
90                                 qDebug() << getTagName() << " parser mismatch error" << lineData;
91                                 return false;
92                         }
93                         else
94                         {
95                                 hasEnded = true;
96                                 break;
97                         }
98                 }
99
100                 lineData = stream.readLine();
101         }while(!hasEnded && !stream.atEnd());
102
103         if(hasEnded)
104         {
105                 //event.fld_is_read = isRead;
106                 //event.fld_outgoing = isOutgoing;
107                 //event.fld_start_time = timestamp.toUTC().toTime_t();
108         }
109
110         return true;
111 }