Renamed some setters for consistency.
[qwerkisync] / EventParsers / VMGEntities / VCard.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 "VCard.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 VCard::VCard(const Settings &settings, const SMSEntity* parent) :
42         SMSEntity(settings, parent), m_Version(2.1), m_Target(VCARD_LOCAL)
43 {
44 }
45
46 //VCard::VCard(QTextStream& stream)
47 //{
48 //}
49
50 VCard::VCard(const Settings &settings, const SMSEntity* parent, float version, eTarget target) :
51         SMSEntity(settings, parent), m_Version(version), m_Target(target)
52 {
53 }
54
55 VCard::~VCard()
56 {
57 }
58
59 void VCard::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 VCard::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 *vCardAttachment;
77
78         if(isAttachment())
79         {
80                 vCardAttachment = new Attachment(
81                         (QDir::tempPath() + "/attachment-" + QString::number(event.Timestamp().toTime_t()) + "-" + QString::number(event.Attachments().count()) + ".vcard").toUtf8(),
82                         "text/x-vcard");
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                         vCardAttachment->Stream() << lineData << endl;
106
107                         if(lineData.startsWith("END:"))
108                         {
109                                 if(lineData != QString("END:") + getTagName())
110                                 {
111                                         qDebug() << getTagName() << " parser mismatch error" << lineData;
112                                         break;
113                                 }
114                                 else
115                                 {
116                                         // Save attachment
117                                         event.Attachments().append(vCardAttachment);
118
119                                         hasEnded = true;
120                                         break;
121                                 }
122                         }
123                 }
124                 else
125                 {
126                         if(lineData.startsWith("VERSION:"))
127                         {
128                                 version = lineData.mid(lineData.indexOf(":")+1).toFloat();
129                         }
130                         else if(lineData.startsWith("N:"))
131                         {
132                                 if((isTopLevel && !event.Destination() == EventTypes::SMS::SENT) || (isSender && event.Destination() == EventTypes::SMS::SENT))
133                                 {
134                                         //QString name = lineData.mid(lineData.indexOf(":") + 1);
135                                         //gchar* g_name = g_strdup(name.toUtf8());
136                                         //event.fld_remote_name = g_name;
137                                 }
138                                 else if(isTopLevel)
139                                 {
140                                         //event.fld_local_name = g_strdup("<SelfHandle>");
141                                 }
142                         }
143                         else if(lineData.startsWith("TEL:"))
144                         {
145                                 if((isTopLevel && !event.Destination() == EventTypes::SMS::SENT) || (isSender && event.Destination() == EventTypes::SMS::SENT))
146                                 {
147                                         QString tel = lineData.mid(lineData.indexOf(":") + 1);
148
149                                         if(tel.indexOf("0") == 0)
150                                                 tel = tel.replace(QRegExp("^0"), "+44");
151
152                                         gchar* g_tel = g_strdup(tel.toUtf8());
153                                         event.Tel(g_tel);
154
155 //                                      if(tel.length() < 7 || tel.indexOf(QRegExp("[:alpha:]+")) > -1)
156 //                                              event.fld_group_uid = g_tel;
157 //                                      else
158 //                                              event.fld_group_uid = g_strdup(tel.right(7).toUtf8());
159                                 }
160                                 else if(isTopLevel)
161                                 {
162                                         //event.fld_local_uid = g_strdup("ring/tel/ring");
163                                 }
164                         }
165                         else if(lineData.startsWith("BEGIN:"))
166                         {
167                                 iReader* reader = Factory::Instantiate(CurrentSettings(), lineData, this);
168                                 bool valid(NULL != reader && reader->Read(lineData, stream, event));
169                                 delete reader;
170
171                                 // Quit processing if the nested content is not valid
172                                 if(!valid)
173                                         return valid;
174                         }
175                         else if(lineData.startsWith("END:"))
176                         {
177                                 if(lineData != QString("END:") + getTagName())
178                                 {
179                                         qDebug() << getTagName() << " parser mismatch error" << lineData;
180                                         break;
181                                 }
182                                 else
183                                 {
184                                         hasEnded = true;
185                                         break;
186                                 }
187                         }
188                 }
189
190                 lineData = stream.readLine();
191         }while(!hasEnded && !stream.atEnd());
192
193         if(hasEnded)
194         {
195
196         }
197
198         return true;
199 }