Addressing some compilation warnings:
[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 // TODO: Support writing out Calendar entires sent via SMS
60 void VCalendar::Write(QTextStream &/*stream*/, const EventTypes::SMS &/*event*/, const NumberToNameLookup &/*numberToNameLookup*/)
61 {
62         //stream << "BEGIN:" << getTagName() << "\n";
63
64         //stream << "VERSION:" << m_Version << "\n";
65         //stream << "N:" << (m_Target == VCARD_LOCAL ? "" : event.Name()) << "\n";
66         //stream << "TEL:" << (m_Target == VCARD_LOCAL ? "" : event.Tel()) << "\n";
67
68         //stream << "END:" << getTagName() << "\n";
69 }
70
71 bool VCalendar::Read(const QString &initialLine, QTextStream &stream, EventTypes::SMS &event)
72 {
73         bool hasEnded(false);
74         float version(0);
75         //bool isTopLevel(typeid(*getParent()) == typeid(VMessage)); Not used.
76         //bool isSender(typeid(*getParent()) == typeid(VEnvelope)); Not used.
77         Attachment *vCalendarAttachment;
78
79         if(isAttachment())
80         {
81                 vCalendarAttachment = new Attachment(
82                         (QDir::tempPath() + "/attachment-" + QString::number(event.Timestamp().toTime_t()) + "-" + QString::number(event.Attachments().count()) + ".vcalendar").toUtf8(),
83                         "text/x-vcalendar");
84         }
85
86         // Stream may or may not have a 'BEGIN' present. Swallow it if it's ours.
87         QString lineData(initialLine.length() > 0 ? initialLine : stream.readLine());
88         if(lineData.startsWith("BEGIN:"))
89         {
90                 if(lineData != QString("BEGIN:") + getTagName())
91                 {
92                         qDebug() << "Invalid stream";
93                         return false;
94                 }
95                 else if(!isAttachment())
96                 {
97                         // ...discard this line
98                         lineData = stream.readLine();
99                 }
100         }
101
102         do
103         {
104                 if(isAttachment())
105                 {
106                         vCalendarAttachment->Stream() << lineData << endl;
107
108                         if(lineData.startsWith("END:"))
109                         {
110                                 if(lineData == QString("END:VTODO")
111                                         || lineData == QString("END:VEVENT"))
112                                 {
113                                         // Ignore these. We treat these nested structures as content
114                                 }
115                                 else if(lineData != QString("END:") + getTagName())
116                                 {
117                                         qDebug() << getTagName() << " parser mismatch error" << lineData;
118                                         break;
119                                 }
120                                 else
121                                 {
122                                         // Save attachment
123                                         event.Attachments().append(vCalendarAttachment);
124
125                                         hasEnded = true;
126                                         break;
127                                 }
128                         }
129                 }
130                 else
131                 {
132                         if(lineData.startsWith("VERSION:"))
133                         {
134                                 version = lineData.mid(lineData.indexOf(":")+1).toFloat();
135                         }
136                         else if(lineData.startsWith("BEGIN:"))
137                         {
138                                 iReader* reader = Factory::Instantiate(CurrentSettings(), lineData, this);
139                                 bool valid(NULL != reader && reader->Read(lineData, stream, event));
140                                 delete reader;
141
142                                 // Quit processing if the nested content is not valid
143                                 if(!valid)
144                                         return valid;
145                         }
146                         else if(lineData.startsWith("END:"))
147                         {
148                                 if(lineData != QString("END:") + getTagName())
149                                 {
150                                         qDebug() << getTagName() << " parser mismatch error" << lineData;
151                                         break;
152                                 }
153                                 else
154                                 {
155                                         hasEnded = true;
156                                         break;
157                                 }
158                         }
159                 }
160
161                 lineData = stream.readLine();
162         }while(!hasEnded && !stream.atEnd());
163
164         if(hasEnded)
165         {
166
167         }
168
169         return true;
170 }