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/>.
21 #include "Attachment.h"
23 #include "EventTypes/SMS.h"
24 #include "NumberToNameLookup.h"
26 #include "VEnvelope.h"
31 #include <QTextStream>
32 #include <QTemporaryFile>
36 #include <rtcom-eventlogger/eventlogger.h>
40 using namespace EventParsers::VMGEntities;
42 VCard::VCard(const Settings &settings, const SMSEntity* parent) :
43 SMSEntity(settings, parent), m_Version(2.1), m_Target(VCARD_LOCAL)
47 //VCard::VCard(QTextStream& stream)
51 VCard::VCard(const Settings &settings, const SMSEntity* parent, float version, eTarget target) :
52 SMSEntity(settings, parent), m_Version(version), m_Target(target)
60 void VCard::Write(QTextStream &stream, const EventTypes::SMS &event, const NumberToNameLookup &numberToNameLookup)
62 stream << "BEGIN:" << getTagName() << "\n";
64 stream << "VERSION:" << m_Version << "\n";
66 if (m_Target != VCARD_LOCAL)
68 qDebug() << event.Tel();
69 qDebug() << numberToNameLookup.ContactDetails().count();
70 if(numberToNameLookup.ContactDetails().contains(event.Tel()))
71 stream << numberToNameLookup.ContactDetails().value(event.Tel()).second;
74 stream << "TEL:" << (m_Target == VCARD_LOCAL ? "" : event.Tel()) << "\n";
76 stream << "END:" << getTagName() << "\n";
79 bool VCard::Read(const QString &initialLine, QTextStream &stream, EventTypes::SMS &event)
83 bool isTopLevel(typeid(*getParent()) == typeid(VMessage));
84 bool isSender(typeid(*getParent()) == typeid(VEnvelope));
85 Attachment *vCardAttachment;
89 vCardAttachment = new Attachment(
90 (QDir::tempPath() + "/attachment-" + QString::number(event.Timestamp().toTime_t()) + "-" + QString::number(event.Attachments().count()) + ".vcard").toUtf8(),
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:"))
98 if(lineData != QString("BEGIN:") + getTagName())
100 qDebug() << "Invalid stream";
103 else if(!isAttachment())
105 // ...discard this line
106 lineData = stream.readLine();
114 vCardAttachment->Stream() << lineData << endl;
116 if(lineData.startsWith("END:"))
118 if(lineData != QString("END:") + getTagName())
120 qDebug() << getTagName() << " parser mismatch error" << lineData;
126 event.Attachments().append(vCardAttachment);
135 if(lineData.startsWith("VERSION:"))
137 version = lineData.mid(lineData.indexOf(":")+1).toFloat();
139 else if(lineData.startsWith("N:"))
141 if((isTopLevel && !event.Destination() == EventTypes::SMS::SENT) || (isSender && event.Destination() == EventTypes::SMS::SENT))
143 //QString name = lineData.mid(lineData.indexOf(":") + 1);
144 //gchar* g_name = g_strdup(name.toUtf8());
145 //event.fld_remote_name = g_name;
149 //event.fld_local_name = g_strdup("<SelfHandle>");
152 else if(lineData.startsWith("TEL:"))
154 if((isTopLevel && !event.Destination() == EventTypes::SMS::SENT) || (isSender && event.Destination() == EventTypes::SMS::SENT))
156 QString tel = lineData.mid(lineData.indexOf(":") + 1);
158 if(tel.indexOf("0") == 0)
159 tel = tel.replace(QRegExp("^0"), "+44");
161 gchar* g_tel = g_strdup(tel.toUtf8());
164 // if(tel.length() < 7 || tel.indexOf(QRegExp("[:alpha:]+")) > -1)
165 // event.fld_group_uid = g_tel;
167 // event.fld_group_uid = g_strdup(tel.right(7).toUtf8());
171 //event.fld_local_uid = g_strdup("ring/tel/ring");
174 else if(lineData.startsWith("BEGIN:"))
176 iReader* reader = Factory::Instantiate(CurrentSettings(), lineData, this);
177 bool valid(NULL != reader && reader->Read(lineData, stream, event));
180 // Quit processing if the nested content is not valid
184 else if(lineData.startsWith("END:"))
186 if(lineData != QString("END:") + getTagName())
188 qDebug() << getTagName() << " parser mismatch error" << lineData;
199 lineData = stream.readLine();
200 }while(!hasEnded && !stream.atEnd());