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/>.
19 #include "VCalendar.h"
21 #include "Attachment.h"
23 #include "EventTypes/SMS.h"
25 #include "VEnvelope.h"
30 #include <QTextStream>
31 #include <QTemporaryFile>
35 #include <rtcom-eventlogger/eventlogger.h>
39 using namespace EventParsers::VMGEntities;
41 VCalendar::VCalendar(const Settings &settings, const SMSEntity* parent) :
42 SMSEntity(settings, parent), m_Version(1.0)
46 //VCalendar::VCalendar(QTextStream& stream)
50 VCalendar::VCalendar(const Settings &settings, const SMSEntity* parent, float version) :
51 SMSEntity(settings, parent), m_Version(version)
55 VCalendar::~VCalendar()
59 void VCalendar::Write(QTextStream &stream, const EventTypes::SMS &event)
61 //stream << "BEGIN:" << getTagName() << "\n";
63 //stream << "VERSION:" << m_Version << "\n";
64 //stream << "N:" << (m_Target == VCARD_LOCAL ? "" : event.Name()) << "\n";
65 //stream << "TEL:" << (m_Target == VCARD_LOCAL ? "" : event.Tel()) << "\n";
67 //stream << "END:" << getTagName() << "\n";
70 bool VCalendar::Read(const QString &initialLine, QTextStream &stream, EventTypes::SMS &event)
74 bool isTopLevel(typeid(*getParent()) == typeid(VMessage));
75 bool isSender(typeid(*getParent()) == typeid(VEnvelope));
76 Attachment *vCalendarAttachment;
80 vCalendarAttachment = new Attachment(
81 (QDir::tempPath() + "/attachment-" + QString::number(event.Timestamp().toTime_t()) + "-" + QString::number(event.Attachments().count()) + ".vcalendar").toUtf8(),
85 // Stream may or may not have a 'BEGIN' present. Swallow it if it's ours.
86 QString lineData(initialLine.length() > 0 ? initialLine : stream.readLine());
87 if(lineData.startsWith("BEGIN:"))
89 if(lineData != QString("BEGIN:") + getTagName())
91 qDebug() << "Invalid stream";
94 else if(!isAttachment())
96 // ...discard this line
97 lineData = stream.readLine();
105 vCalendarAttachment->Stream() << lineData << endl;
107 if(lineData.startsWith("END:"))
109 if(lineData == QString("END:VTODO")
110 || lineData == QString("END:VEVENT"))
112 // Ignore these. We treat these nested structures as content
114 else if(lineData != QString("END:") + getTagName())
116 qDebug() << getTagName() << " parser mismatch error" << lineData;
122 event.Attachments().append(vCalendarAttachment);
131 if(lineData.startsWith("VERSION:"))
133 version = lineData.mid(lineData.indexOf(":")+1).toFloat();
135 else if(lineData.startsWith("BEGIN:"))
137 iReader* reader = Factory::Instantiate(CurrentSettings(), lineData, this);
138 bool valid(NULL != reader && reader->Read(lineData, stream, event));
141 // Quit processing if the nested content is not valid
145 else if(lineData.startsWith("END:"))
147 if(lineData != QString("END:") + getTagName())
149 qDebug() << getTagName() << " parser mismatch error" << lineData;
160 lineData = stream.readLine();
161 }while(!hasEnded && !stream.atEnd());