db7a1b04b866d9675a2dbb3cd30690439062dcf6
[qwerkisync] / EventParsers / VMGEntities / VCalendar.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 "VCalendar.h"
20
21 #include "Attachment.h"
22 #include "Factory.h"
23 #include "EventTypes/SMS.h"
24 #include "VBody.h"
25 #include "VEnvelope.h"
26 #include "VMessage.h"
27
28 #include <QDebug>
29 #include <QDir>
30 #include <QTextStream>
31 #include <QTemporaryFile>
32
33 #include <glib.h>
34
35 #include <rtcom-eventlogger/eventlogger.h>
36
37 #include <typeinfo>
38
39 using namespace EventParsers::VMGEntities;
40
41 VCalendar::VCalendar(const Settings &settings, const SMSEntity* parent) :
42         SMSEntity(settings, parent), m_Version(1.0)
43 {
44 }
45
46 //VCalendar::VCalendar(QTextStream& stream)
47 //{
48 //}
49
50 VCalendar::VCalendar(const Settings &settings, const SMSEntity* parent, float version) :
51         SMSEntity(settings, parent), m_Version(version)
52 {
53 }
54
55 VCalendar::~VCalendar()
56 {
57 }
58
59 void VCalendar::Write(QTextStream &stream, const EventTypes::SMS &event)
60 {
61         //stream << "BEGIN:" << getTagName() << "\n";
62
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";
66
67         //stream << "END:" << getTagName() << "\n";
68 }
69
70 bool VCalendar::Read(const QString &initialLine, QTextStream &stream, EventTypes::SMS &event)
71 {
72         bool hasEnded(false);
73         float version(0);
74         bool isTopLevel(typeid(*getParent()) == typeid(VMessage));
75         bool isSender(typeid(*getParent()) == typeid(VEnvelope));
76         Attachment *vCalendarAttachment;
77
78         if(isAttachment())
79         {
80                 vCalendarAttachment = new Attachment(
81                         (QDir::tempPath() + "/attachment-" + QString::number(event.Timestamp().toTime_t()) + "-" + QString::number(event.Attachments().count()) + ".vcalendar").toUtf8(),
82                         "text/x-vcalendar");
83         }
84
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:"))
88         {
89                 if(lineData != QString("BEGIN:") + getTagName())
90                 {
91                         qDebug() << "Invalid stream";
92                         return false;
93                 }
94                 else if(!isAttachment())
95                 {
96                         // ...discard this line
97                         lineData = stream.readLine();
98                 }
99         }
100
101         do
102         {
103                 if(isAttachment())
104                 {
105                         vCalendarAttachment->Stream() << lineData << endl;
106
107                         if(lineData.startsWith("END:"))
108                         {
109                                 if(lineData == QString("END:VTODO")
110                                         || lineData == QString("END:VEVENT"))
111                                 {
112                                         // Ignore these. We treat these nested structures as content
113                                 }
114                                 else if(lineData != QString("END:") + getTagName())
115                                 {
116                                         qDebug() << getTagName() << " parser mismatch error" << lineData;
117                                         break;
118                                 }
119                                 else
120                                 {
121                                         // Save attachment
122                                         event.Attachments().append(vCalendarAttachment);
123
124                                         hasEnded = true;
125                                         break;
126                                 }
127                         }
128                 }
129                 else
130                 {
131                         if(lineData.startsWith("VERSION:"))
132                         {
133                                 version = lineData.mid(lineData.indexOf(":")+1).toFloat();
134                         }
135                         else if(lineData.startsWith("BEGIN:"))
136                         {
137                                 iReader* reader = Factory::Instantiate(CurrentSettings(), lineData, this);
138                                 bool valid(NULL != reader && reader->Read(lineData, stream, event));
139                                 delete reader;
140
141                                 // Quit processing if the nested content is not valid
142                                 if(!valid)
143                                         return valid;
144                         }
145                         else if(lineData.startsWith("END:"))
146                         {
147                                 if(lineData != QString("END:") + getTagName())
148                                 {
149                                         qDebug() << getTagName() << " parser mismatch error" << lineData;
150                                         break;
151                                 }
152                                 else
153                                 {
154                                         hasEnded = true;
155                                         break;
156                                 }
157                         }
158                 }
159
160                 lineData = stream.readLine();
161         }while(!hasEnded && !stream.atEnd());
162
163         if(hasEnded)
164         {
165
166         }
167
168         return true;
169 }