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, const int durationInSeconds, const bool isMissedCall, const AttachmentCollection &attachments) :
79 m_Settings(settings), m_Destination(destination), m_Timestamp(timestamp), m_Tel(tel),
80 m_DurationInSeconds(durationInSeconds), m_IsMissedCall(isMissedCall), 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"));
126 else if(IsMissedCall())
127 RTCOM_EL_EVENT_SET_FIELD (event, event_type, g_strdup("RTCOM_EL_EVENTTYPE_CALL_MISSED"));
129 RTCOM_EL_EVENT_SET_FIELD (event, event_type, g_strdup("RTCOM_EL_EVENTTYPE_CALL"));
130 RTCOM_EL_EVENT_SET_FIELD (event, start_time, Timestamp().toUTC().toTime_t());
131 RTCOM_EL_EVENT_SET_FIELD (event, end_time, Timestamp().addSecs(DurationInSeconds()).toUTC().toTime_t());
132 RTCOM_EL_EVENT_SET_FIELD (event, storage_time, Timestamp().addSecs(DurationInSeconds()).toUTC().toTime_t());
133 //RTCOM_EL_EVENT_SET_FIELD (event, is_read, 0);
134 RTCOM_EL_EVENT_SET_FIELD (event, outgoing, Destination() == Settings::OUTGOING ? 1 : 0);
135 //if(local_uid) // Voicemail local_uid transform needed here
136 RTCOM_EL_EVENT_SET_FIELD (event, local_uid, g_strdup("ring/tel/ring"));
137 //RTCOM_EL_EVENT_SET_FIELD (&event, local_name, g_strdup("<SelfHandle>"));
138 RTCOM_EL_EVENT_SET_FIELD (event, remote_uid, g_strdup(Tel().toUtf8()));
139 //RTCOM_EL_EVENT_SET_FIELD (&event, remote_name, g_strdup(QString::number(numberToNameLookup.ContactDetails().value(Tel()).second).toUtf8()));
140 RTCOM_EL_EVENT_SET_FIELD (event, remote_ebook_uid, g_strdup(QString::number(numberToNameLookup.ContactDetails().value(Tel()).first).toUtf8()));
141 RTCOM_EL_EVENT_SET_FIELD (event, group_uid, g_strdup(groupId.toUtf8()));
146 void PhoneCall::WriteCSVSymbian(QTextStream &stream, const ColumnIndicesByIndexHash &headerIndices, const QChar delimiter, const NumberToNameLookup &numberToNameLookup, SymbianEventLogStrings &strings) const
148 // 0|05/09/2007 11:25:12 am|1||||||<name>|3|8|0|0|<number>|||Unrecognized||
149 for(uint columnIndex(0); columnIndex < (uint)headerIndices.count(); ++columnIndex)
151 const QString &heading(headerIndices.value(columnIndex));
152 if("etype" == heading.toLower())
154 stream << "0"; // Phone calls are type '0'
156 else if("etime" == heading.toLower())
158 stream << Timestamp().toUTC().toString("dd/MM/yyyy h:mm:ss ap");
160 else if("remote" == heading.toLower())
162 stream << numberToNameLookup.ContactDetails().value(Tel()).second;
164 else if("direction" == heading.toLower())
168 if(!strings.contains("Missed Call"))
169 strings.insert("Missed Call", strings.count());
170 stream << strings.value("Missed Call");
172 else if(Settings::OUTGOING == Destination())
174 if(!strings.contains("Outgoing"))
175 strings.insert("Outgoing", strings.count());
176 stream << strings.value("Outgoing");
178 else if (Settings::INCOMING == Destination())
180 if(!strings.contains("Incoming"))
181 strings.insert("Incoming", strings.count());
182 stream << strings.value("Incoming");
185 else if("duration" == heading.toLower())
187 stream << DurationInSeconds();
189 else if("dtype" == heading.toLower())
191 stream << "1"; // 1 seems to be KLogDurationValid
193 else if("status" == heading.toLower())
195 stream << "0"; // Always '0' for phone calls.
197 else if("subject" == heading.toLower())
199 // Subject seems to be ignored - this makes sense, but I
200 // suspect that non-zero values are a bug that I can't duplicate...
203 else if("number" == heading.toLower())
207 else if("data" == heading.toLower())
209 // Event-specfic data - but not supported by DBU-SCV tool
210 // Hex editing the DBU suggests there is SIP account info in here...
211 // ...along the lines of:
212 // "VOIP.URL=sip:<remote#>@<remote domain>.MA=sip:<my#>@<my domain>"
213 stream << "Unrecognised";
217 // Don't print anything. Makes it obvious which fields we've
221 if(columnIndex < (uint)headerIndices.count() - 1)
228 QDebug operator<<(QDebug dbg, PhoneCall& event)
230 dbg.nospace() << "\tHash:\t\t" << event.HashCode() << "\n";
231 dbg.nospace() << "\tDirection:\t\t" << (event.Destination() == Settings::OUTGOING ? "Made" : "Recieved") << "\n";
232 dbg.nospace() << "\tTimestamp:\t" << event.Timestamp().toUTC() << "\n";
233 dbg.nospace() << "\tDuration:\t\t" << event.DurationInSeconds() << " seconds\n";
234 dbg.nospace() << "\tRemote-Tel:\t" << event.Tel() << "\n";
235 //dbg.nospace() << "\tremote-name:\t" << event.fld_remote_name << "\n";