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"
25 #include "VEnvelope.h"
30 #include <QTextStream>
31 #include <QTemporaryFile>
35 #include <rtcom-eventlogger/eventlogger.h>
39 using namespace EventParsers::VMGEntities;
41 VCard::VCard(const SMSEntity* parent) :
42 SMSEntity(parent), m_Version(2.1), m_Target(VCARD_LOCAL)
46 //VCard::VCard(QTextStream& stream)
50 VCard::VCard(const SMSEntity* parent, float version, eTarget target) :
51 SMSEntity(parent), m_Version(version), m_Target(target)
59 void VCard::Write(QTextStream &stream, const EventTypes::SMS &event)
61 stream << "BEGIN:" << getTagName() << "\n";
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";
67 stream << "END:" << getTagName() << "\n";
70 bool VCard::Read(const QString &initialLine, QTextStream &stream, EventTypes::SMS &event)
74 bool isTopLevel(typeid(*getParent()) == typeid(VMessage));
75 bool isSender(typeid(*getParent()) == typeid(VEnvelope));
76 Attachment *vCardAttachment;
80 vCardAttachment = new Attachment(
81 (QDir::tempPath() + "/attachment-" + QString::number(event.Timestamp().toTime_t()) + "-" + QString::number(event.Attachments().count()) + ".vcard").toUtf8(),
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:"))
89 if(lineData != QString("BEGIN:") + getTagName())
91 qDebug() << "Invalid stream";
94 else if(!isAttachment())
96 // ...discard this line
97 lineData = stream.readLine();
105 vCardAttachment->Stream() << lineData << endl;
107 if(lineData.startsWith("END:"))
109 if(lineData != QString("END:") + getTagName())
111 qDebug() << getTagName() << " parser mismatch error" << lineData;
117 event.Attachments().append(vCardAttachment);
126 if(lineData.startsWith("VERSION:"))
128 version = lineData.mid(lineData.indexOf(":")+1).toFloat();
130 else if(lineData.startsWith("N:"))
132 if((isTopLevel && !event.Destination() == EventTypes::SMS::SENT) || (isSender && event.Destination() == EventTypes::SMS::SENT))
134 //QString name = lineData.mid(lineData.indexOf(":") + 1);
135 //gchar* g_name = g_strdup(name.toUtf8());
136 //event.fld_remote_name = g_name;
140 //event.fld_local_name = g_strdup("<SelfHandle>");
143 else if(lineData.startsWith("TEL:"))
145 if((isTopLevel && !event.Destination() == EventTypes::SMS::SENT) || (isSender && event.Destination() == EventTypes::SMS::SENT))
147 QString tel = lineData.mid(lineData.indexOf(":") + 1);
149 if(tel.indexOf("0") == 0)
150 tel = tel.replace(QRegExp("^0"), "+44");
152 gchar* g_tel = g_strdup(tel.toUtf8());
155 // if(tel.length() < 7 || tel.indexOf(QRegExp("[:alpha:]+")) > -1)
156 // event.fld_group_uid = g_tel;
158 // event.fld_group_uid = g_strdup(tel.right(7).toUtf8());
162 //event.fld_local_uid = g_strdup("ring/tel/ring");
165 else if(lineData.startsWith("BEGIN:"))
167 iReader* reader = Factory::Instantiate(lineData, this);
168 bool valid(NULL != reader && reader->Read(lineData, stream, event));
171 // Quit processing if the nested content is not valid
175 else if(lineData.startsWith("END:"))
177 if(lineData != QString("END:") + getTagName())
179 qDebug() << getTagName() << " parser mismatch error" << lineData;
190 lineData = stream.readLine();
191 }while(!hasEnded && !stream.atEnd());