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/>.
19 #include "PhoneCall.h"
21 #include "Attachment.h"
22 #include "DBBackends/RtcomEventLogger.h"
23 #include "EventParsers/CSVSymbianEventLogParser.h"
24 #include "NumberToNameLookup.h"
33 #include <QTextStream>
37 #include <rtcom-eventlogger/event.h>
38 #include <rtcom-eventlogger/eventlogger-attach-iter.h>
40 using namespace EventTypes;
42 const DBBackends::iDBBackend &PhoneCall::DB() const
44 return DBBackends::RtcomEventLogger(CurrentSettings(), *this);
47 PhoneCall::PhoneCall(const Settings &settings) :
50 qDebug() << "Created new default Phone Call: " << *this;
53 PhoneCall::~PhoneCall()
55 foreach(QSharedPointer<Attachment> attachment, m_Attachments)
61 PhoneCall::PhoneCall(const Settings &settings, const RTComElEvent &event, const QList<RTComElAttachment*> attachments) :
64 Destination(event.fld_outgoing ? Settings::OUTGOING : Settings::INCOMING);
65 Timestamp(QDateTime::fromTime_t(event.fld_start_time).toUTC());
66 DurationInSeconds(QDateTime::fromTime_t(event.fld_start_time).toUTC().secsTo(QDateTime::fromTime_t(event.fld_end_time).toUTC()));
67 Tel(event.fld_remote_uid);
68 if(Tel().indexOf("0") == 0)
69 Tel(QString(Tel()).replace(QRegExp("^0"), "+44"));
71 if(attachments.count() > 0)
72 foreach(RTComElAttachment *attachment, attachments)
73 Attachments().append(new Attachment(*attachment));
75 qDebug() << "Created new Phone Call from RtCom:\n" << *this;
78 PhoneCall::PhoneCall(const Settings &settings, const Settings::eDirection destination, const QDateTime ×tamp, const QString &tel, int durationInSeconds, const AttachmentCollection &attachments) :
79 m_Settings(settings), m_Destination(destination), m_Timestamp(timestamp), m_Tel(tel),
80 m_DurationInSeconds(durationInSeconds), m_Attachments(attachments)
82 if(Tel().indexOf("0") == 0)
83 Tel(QString(Tel()).replace(QRegExp("^0"), "+44"));
85 // TODO: Copy attachments.
86 // if(attachments.count() > 0)
87 // foreach(const QSharedPointer<Attachment *> &attachment, attachments)
88 // Attachments().append(attachment);
90 qDebug() << "Created new Phone Call: " << *this;
94 const uint PhoneCall::HashCode() const
96 // qDebug() << Timestamp().toUTC().toTime_t() << ", " << qHash(Tel()) << ", " << qHash(Destination()) << ", " << qHash(Contents()) << ", " << Attachments().HashCode();
98 // foreach(QChar c, Contents().toUtf8())
100 // qDebug() << c.unicode();
104 Timestamp().toUTC().toTime_t() ^
105 DurationInSeconds() ^
107 qHash(Destination()) ^
108 Attachments().HashCode();
111 RTComElEvent * PhoneCall::toRTComEvent(const NumberToNameLookup &numberToNameLookup) const
113 QList<QString> voiceMailList;
114 voiceMailList << "+447945353070"; // T-Mobile UK Voicemail
116 QString groupId((Tel().length() < 7 || Tel().indexOf(QRegExp("[:alpha:]+")) > -1)
120 RTComElEvent *event(rtcom_el_event_new());
121 memset(event, 0, sizeof(RTComElEvent));
123 RTCOM_EL_EVENT_SET_FIELD (event, service, g_strdup("RTCOM_EL_SERVICE_CALL"));
124 if(voiceMailList.contains(Tel()))
125 RTCOM_EL_EVENT_SET_FIELD (event, event_type, g_strdup("RTCOM_EL_EVENTTYPE_CALL_VOICEMAIL"));
127 RTCOM_EL_EVENT_SET_FIELD (event, event_type, g_strdup("RTCOM_EL_EVENTTYPE_CALL"));
128 RTCOM_EL_EVENT_SET_FIELD (event, start_time, Timestamp().toUTC().toTime_t());
129 RTCOM_EL_EVENT_SET_FIELD (event, end_time, Timestamp().addSecs(DurationInSeconds()).toUTC().toTime_t());
130 RTCOM_EL_EVENT_SET_FIELD (event, storage_time, Timestamp().addSecs(DurationInSeconds()).toUTC().toTime_t());
131 //RTCOM_EL_EVENT_SET_FIELD (event, is_read, 0);
132 RTCOM_EL_EVENT_SET_FIELD (event, outgoing, Destination() == Settings::OUTGOING ? 1 : 0);
133 //if(local_uid) // Voicemail local_uid transform needed here
134 RTCOM_EL_EVENT_SET_FIELD (event, local_uid, g_strdup("ring/tel/ring"));
135 //RTCOM_EL_EVENT_SET_FIELD (&event, local_name, g_strdup("<SelfHandle>"));
136 RTCOM_EL_EVENT_SET_FIELD (event, remote_uid, g_strdup(Tel().toUtf8()));
137 //RTCOM_EL_EVENT_SET_FIELD (&event, remote_name, g_strdup(QString::number(numberToNameLookup.ContactDetails().value(Tel()).second).toUtf8()));
138 RTCOM_EL_EVENT_SET_FIELD (event, remote_ebook_uid, g_strdup(QString::number(numberToNameLookup.ContactDetails().value(Tel()).first).toUtf8()));
139 RTCOM_EL_EVENT_SET_FIELD (event, group_uid, g_strdup(groupId.toUtf8()));
144 void PhoneCall::Export(const QString &baseDirectory, const NumberToNameLookup &numberToNameLookup) const
146 // // Build the path and ensure it exists...
147 // QString eventFilename(baseDirectory);
148 // eventFilename += Destination() == EventTypes::PhoneCall::OUTGOING ? "/Sent/" : "/Inbox/";
149 // eventFilename += QString::number(Timestamp().toUTC().date().year()) + "/";
150 // QDir().mkpath(eventFilename);
152 // // ...then build the filename and open it.
153 // eventFilename += QString::number(Timestamp().toUTC().toTime_t()) + ".vmg";
154 // QFile data(eventFilename);
155 // if (data.open(QFile::WriteOnly | QFile::Truncate))
157 // QTextStream stream(&data);
159 // QTextCodec *oldCodec = stream.codec();
160 // stream.setAutoDetectUnicode(false);
161 // stream.setCodec("UTF-16LE");
163 // EventParsers::VMGEntities::VMessage writer(NULL, 1.1);
164 // writer.Write(stream, *this);
165 ////stream << "Test";
166 // //stream.setCodec(oldCodec);
170 // utimbuf fileTimes;
171 // fileTimes.modtime = Timestamp().toUTC().toTime_t();
172 // utime(eventFilename.toAscii(), &fileTimes);
176 QDebug operator<<(QDebug dbg, PhoneCall& event)
178 dbg.nospace() << "\tHash:\t\t" << event.HashCode() << "\n";
179 dbg.nospace() << "\tDirection:\t\t" << (event.Destination() == Settings::OUTGOING ? "Made" : "Recieved") << "\n";
180 dbg.nospace() << "\tTimestamp:\t" << event.Timestamp().toUTC() << "\n";
181 dbg.nospace() << "\tDuration:\t\t" << event.DurationInSeconds() << " seconds\n";
182 dbg.nospace() << "\tRemote-Tel:\t" << event.Tel() << "\n";
183 //dbg.nospace() << "\tremote-name:\t" << event.fld_remote_name << "\n";