2 * Copyright (C) 2011, Jamie Thompson
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.
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.
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/>.
21 #include "EventTypes/SMS.h"
26 #include "VEnvelope.h"
29 #include <QTextStream>
33 using namespace EventParsers::VMGEntities;
35 VMessage::VMessage(const Settings &settings, const SMSEntity *parent) :
36 SMSEntity(settings, parent), m_Version(1.1)
40 //VMessage::VMessage(QTextStream& stream)
44 VMessage::VMessage(const Settings &settings, const SMSEntity *parent, float version) :
45 SMSEntity(settings, parent), m_Version(version)
53 bool VMessage::Read(const QString &initialLine, QTextStream &stream, EventTypes::SMS &event)
55 if(getParent() != NULL)
57 qDebug() << "Messages cannot be nested.";
64 // Stream may or may not have a 'BEGIN' present. Swallow it if it's ours.
65 QString lineData(initialLine.length() > 0 ? initialLine : stream.readLine());
66 if(lineData.startsWith("BEGIN:"))
68 if(lineData != QString("BEGIN:") + getTagName())
70 qDebug() << "Invalid stream";
73 else // ...discard this line
74 lineData = stream.readLine();
79 if(lineData.startsWith("VERSION:"))
81 version = lineData.mid(lineData.indexOf(":")+1).toFloat();
83 else if(lineData.startsWith("X-IRMC-STATUS:"))
85 bool isRead(lineData.mid(lineData.indexOf(":") + 1) == "READ");
86 event.setIsRead(isRead);
88 else if(lineData.startsWith("X-IRMC-BOX:"))
90 QString box = lineData.mid(lineData.indexOf(":") + 1);
91 bool isOutgoing(box == "SENT");
92 if (isOutgoing == false && box != "INBOX")
94 qDebug() << "Unexpected box: " << box;
97 event.setDestination(isOutgoing ? EventTypes::SMS::SENT : EventTypes::SMS::INBOX);
99 else if(lineData.startsWith("X-NOK-DT:"))
101 QDateTime timestamp(QDateTime::fromString(
102 lineData.mid(lineData.indexOf(":") + 1),
103 "yyyyMMddThhmmssZ"));
104 timestamp.setTimeSpec(Qt::UTC);
105 event.setTimestamp(timestamp);
107 else if(lineData.startsWith("BEGIN:"))
109 iReader* reader = Factory::Instantiate(CurrentSettings(), lineData, this);
110 bool valid(NULL != reader && reader->Read(lineData, stream, event));
113 // Quit processing if the nested content is not valid
117 else if(lineData.startsWith("END:"))
119 if(lineData != "END:VMSG")
121 qDebug() << getTagName() << " parser mismatch error" << lineData;
131 lineData = stream.readLine();
132 }while(!hasEnded && !stream.atEnd());
137 void VMessage::Write(QTextStream &stream, const EventTypes::SMS &event)
139 stream << "BEGIN:VMSG\n";
140 stream << "VERSION:" << m_Version << "\n";
141 stream << "X-IRMC-STATUS:" << ( event.IsRead() ? "READ" : "") << "\n";
142 stream << "X-IRMC-BOX:" << ( event.Destination() == EventTypes::SMS::SENT ? "SENT" : "INBOX") << "\n";
143 stream << "X-NOK-DT:" << event.Timestamp().toUTC().toString("yyyyMMddThhmmssZ") << "\n";
145 VEnvelope msgEnvelope(CurrentSettings(), this);
147 // Add in the empty vcard for outgoing messages
148 if(event.Destination() == EventTypes::SMS::SENT)
150 VCard(CurrentSettings(), this, 2.1, VCard::VCARD_LOCAL).Write(stream, event);
152 msgEnvelope.getContentWriters().append(new VCard(CurrentSettings(), &msgEnvelope, 2.1, VCard::VCARD_REMOTE));
154 VEnvelope *bodyEnvelope = new VEnvelope(CurrentSettings(), this);
155 bodyEnvelope->getContentWriters().append(new VBody(CurrentSettings(), bodyEnvelope));
156 msgEnvelope.getContentWriters().append(bodyEnvelope);
160 VCard(CurrentSettings(), this, 2.1, VCard::VCARD_REMOTE).Write(stream, event);
162 msgEnvelope.getContentWriters().append(new VBody(CurrentSettings(), &msgEnvelope));
165 msgEnvelope.Write(stream, event);
167 stream << "END:VMSG\n";