Updated packaging for new 0.0.2 release.
[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 "NumberToNameLookup.h"
25 #include "VBody.h"
26 #include "VEnvelope.h"
27 #include "VMessage.h"
28
29 #include <QDebug>
30 #include <QDir>
31 #include <QTextStream>
32 #include <QTemporaryFile>
33
34 #include <glib.h>
35
36 #include <rtcom-eventlogger/eventlogger.h>
37
38 #include <typeinfo>
39
40 using namespace EventParsers::VMGEntities;
41
42 VCard::VCard(const Settings &settings, const SMSEntity* parent) :
43         SMSEntity(settings, parent), m_Version(2.1), m_Target(VCARD_LOCAL)
44 {
45 }
46
47 //VCard::VCard(QTextStream& stream)
48 //{
49 //}
50
51 VCard::VCard(const Settings &settings, const SMSEntity* parent, float version, eTarget target) :
52         SMSEntity(settings, parent), m_Version(version), m_Target(target)
53 {
54 }
55
56 VCard::~VCard()
57 {
58 }
59
60 void VCard::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:";
66         if (m_Target != VCARD_LOCAL)
67         {
68                         qDebug() << event.Tel();
69                         qDebug() << numberToNameLookup.ContactDetails().count();
70                 if(numberToNameLookup.ContactDetails().contains(event.Tel()))
71                         stream << numberToNameLookup.ContactDetails().value(event.Tel()).second;
72         }
73         stream << "\n";
74         stream << "TEL:" << (m_Target == VCARD_LOCAL ? "" : event.Tel()) << "\n";
75
76         stream << "END:" << getTagName() << "\n";
77 }
78
79 bool VCard::Read(const QString &initialLine, QTextStream &stream, EventTypes::SMS &event)
80 {
81         bool hasEnded(false);
82         float version(0);
83         bool isTopLevel(typeid(*getParent()) == typeid(VMessage));
84         bool isSender(typeid(*getParent()) == typeid(VEnvelope));
85         Attachment *vCardAttachment;
86
87         if(isAttachment())
88         {
89                 vCardAttachment = new Attachment(
90                         (QDir::tempPath() + "/attachment-" + QString::number(event.Timestamp().toTime_t()) + "-" + QString::number(event.Attachments().count()) + ".vcard").toUtf8(),
91                         "text/x-vcard");
92         }
93
94         // Stream may or may not have a 'BEGIN' present. Swallow it if it's ours.
95         QString lineData(initialLine.length() > 0 ? initialLine : stream.readLine());
96         if(lineData.startsWith("BEGIN:"))
97         {
98                 if(lineData != QString("BEGIN:") + getTagName())
99                 {
100                         qDebug() << "Invalid stream";
101                         return false;
102                 }
103                 else if(!isAttachment())
104                 {
105                         // ...discard this line
106                         lineData = stream.readLine();
107                 }
108         }
109
110         do
111         {
112                 if(isAttachment())
113                 {
114                         vCardAttachment->Stream() << lineData << endl;
115
116                         if(lineData.startsWith("END:"))
117                         {
118                                 if(lineData != QString("END:") + getTagName())
119                                 {
120                                         qDebug() << getTagName() << " parser mismatch error" << lineData;
121                                         break;
122                                 }
123                                 else
124                                 {
125                                         // Save attachment
126                                         event.Attachments().append(vCardAttachment);
127
128                                         hasEnded = true;
129                                         break;
130                                 }
131                         }
132                 }
133                 else
134                 {
135                         if(lineData.startsWith("VERSION:"))
136                         {
137                                 version = lineData.mid(lineData.indexOf(":")+1).toFloat();
138                         }
139                         else if(lineData.startsWith("N:"))
140                         {
141                                 if((isTopLevel && !event.Destination() == EventTypes::SMS::SENT) || (isSender && event.Destination() == EventTypes::SMS::SENT))
142                                 {
143                                         //QString name = lineData.mid(lineData.indexOf(":") + 1);
144                                         //gchar* g_name = g_strdup(name.toUtf8());
145                                         //event.fld_remote_name = g_name;
146                                 }
147                                 else if(isTopLevel)
148                                 {
149                                         //event.fld_local_name = g_strdup("<SelfHandle>");
150                                 }
151                         }
152                         else if(lineData.startsWith("TEL:"))
153                         {
154                                 if((isTopLevel && !event.Destination() == EventTypes::SMS::SENT) || (isSender && event.Destination() == EventTypes::SMS::SENT))
155                                 {
156                                         QString tel = lineData.mid(lineData.indexOf(":") + 1);
157
158                                         if(tel.indexOf("0") == 0)
159                                                 tel = tel.replace(QRegExp("^0"), "+44");
160
161                                         gchar* g_tel = g_strdup(tel.toUtf8());
162                                         event.Tel(g_tel);
163
164 //                                      if(tel.length() < 7 || tel.indexOf(QRegExp("[:alpha:]+")) > -1)
165 //                                              event.fld_group_uid = g_tel;
166 //                                      else
167 //                                              event.fld_group_uid = g_strdup(tel.right(7).toUtf8());
168                                 }
169                                 else if(isTopLevel)
170                                 {
171                                         //event.fld_local_uid = g_strdup("ring/tel/ring");
172                                 }
173                         }
174                         else if(lineData.startsWith("BEGIN:"))
175                         {
176                                 iReader* reader = Factory::Instantiate(CurrentSettings(), lineData, this);
177                                 bool valid(NULL != reader && reader->Read(lineData, stream, event));
178                                 delete reader;
179
180                                 // Quit processing if the nested content is not valid
181                                 if(!valid)
182                                         return valid;
183                         }
184                         else if(lineData.startsWith("END:"))
185                         {
186                                 if(lineData != QString("END:") + getTagName())
187                                 {
188                                         qDebug() << getTagName() << " parser mismatch error" << lineData;
189                                         break;
190                                 }
191                                 else
192                                 {
193                                         hasEnded = true;
194                                         break;
195                                 }
196                         }
197                 }
198
199                 lineData = stream.readLine();
200         }while(!hasEnded && !stream.atEnd());
201
202         if(hasEnded)
203         {
204
205         }
206
207         return true;
208 }