1st attempt at an initial import.
[qwerkisync] / EventTypes / PhoneCall.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 "PhoneCall.h"
20
21 #include "Attachment.h"
22 #include "DBBackends/RtcomEventLogger.h"
23 #include "EventParsers/CSVSymbianEventLogParser.h"
24 #include "NumberToNameLookup.h"
25 #include "Settings.h"
26
27 #include <QDateTime>
28 #include <QDir>
29 #include <QFile>
30 #include <QList>
31 #include <QRegExp>
32 #include <QString>
33 #include <QTextStream>
34
35 #include <utime.h>
36
37 #include <rtcom-eventlogger/event.h>
38 #include <rtcom-eventlogger/eventlogger-attach-iter.h>
39
40 using namespace EventTypes;
41
42 const DBBackends::iDBBackend &PhoneCall::DB() const
43 {
44         return DBBackends::RtcomEventLogger(CurrentSettings(), *this);
45 }
46
47 PhoneCall::PhoneCall(const Settings &settings) :
48         m_Settings(settings)
49 {
50 }
51
52 PhoneCall::~PhoneCall()
53 {
54         foreach(QSharedPointer<Attachment> attachment, m_Attachments)
55         {
56                 attachment.clear();
57         }
58 }
59
60 PhoneCall::PhoneCall(const Settings &settings, const RTComElEvent &event, const QList<RTComElAttachment*> attachments) :
61         m_Settings(settings)
62 {
63         setDestination(event.fld_outgoing ? OUTGOING : INCOMING);
64         setTimestamp(QDateTime::fromTime_t(event.fld_start_time).toUTC());
65         setDurationInSeconds(QDateTime::fromTime_t(event.fld_start_time).toUTC().secsTo(QDateTime::fromTime_t(event.fld_end_time).toUTC()));
66         setTel(event.fld_remote_uid);
67         if(Tel().indexOf("0") == 0)
68                 setTel(QString(Tel()).replace(QRegExp("^0"), "+44"));
69
70         // We directly access the m_Attachments member variable here rather than the
71         // accessor as the accessor is const
72         if(attachments.count() > 0)
73                 foreach(RTComElAttachment *attachment, attachments)
74                         Attachments().append(new Attachment(*attachment));
75 }
76 #include <QDebug>
77 const uint PhoneCall::HashCode() const
78 {
79 //      qDebug() << Timestamp().toUTC().toTime_t() << ", " << qHash(Tel()) << ", " << qHash(Destination()) << ", " << qHash(Contents()) << ", " << Attachments().HashCode();
80
81 //      foreach(QChar c, Contents().toUtf8())
82 //      {
83 //              qDebug() << c.unicode();
84 //      }
85
86         return
87                 Timestamp().toUTC().toTime_t() ^
88                 DurationInSeconds() ^
89                 qHash(Tel()) ^
90                 qHash(Destination()) ^
91                 Attachments().HashCode();
92 }
93
94 RTComElEvent * PhoneCall::toRTComEvent(const NumberToNameLookup &numberToNameLookup) const
95 {
96         QList<QString> voiceMailList;
97
98         QString groupId((Tel().length() < 7 || Tel().indexOf(QRegExp("[:alpha:]+")) > -1)
99                 ? Tel()
100                 : Tel().right(7));
101
102         RTComElEvent *event(rtcom_el_event_new());
103         memset(event, 0, sizeof(RTComElEvent));
104
105         RTCOM_EL_EVENT_SET_FIELD (event, service, g_strdup("RTCOM_EL_SERVICE_CALL"));
106         if(voiceMailList.contains(Tel()))
107                 RTCOM_EL_EVENT_SET_FIELD (event, event_type, g_strdup("RTCOM_EL_EVENTTYPE_CALL_VOICEMAIL"));
108         else
109                 RTCOM_EL_EVENT_SET_FIELD (event, event_type, g_strdup("RTCOM_EL_EVENTTYPE_CALL"));
110         RTCOM_EL_EVENT_SET_FIELD (event, storage_time, Timestamp().toUTC().toTime_t());
111         RTCOM_EL_EVENT_SET_FIELD (event, start_time, Timestamp().toUTC().toTime_t());
112         RTCOM_EL_EVENT_SET_FIELD (event, end_time, Timestamp().toUTC().toTime_t());
113         //RTCOM_EL_EVENT_SET_FIELD (event, is_read, 0);
114         RTCOM_EL_EVENT_SET_FIELD (event, outgoing, Destination() == PhoneCall::OUTGOING ? 1 : 0);
115         RTCOM_EL_EVENT_SET_FIELD (event, local_uid, g_strdup("ring/tel/ring"));
116         //RTCOM_EL_EVENT_SET_FIELD (&event, local_name, g_strdup("<SelfHandle>"));
117         RTCOM_EL_EVENT_SET_FIELD (event, remote_uid, g_strdup(Tel().toUtf8()));
118         //RTCOM_EL_EVENT_SET_FIELD (&event, remote_name, g_strdup(QString::number(numberToNameLookup.ContactDetails().value(Tel()).second).toUtf8()));
119         RTCOM_EL_EVENT_SET_FIELD (event, remote_ebook_uid, g_strdup(QString::number(numberToNameLookup.ContactDetails().value(Tel()).first).toUtf8()));
120         RTCOM_EL_EVENT_SET_FIELD (event, group_uid, g_strdup(groupId.toUtf8()));
121         //RTCOM_EL_EVENT_SET_FIELD (event, free_text, g_strdup(Contents().replace(0x2029, "\n").toUtf8()));
122         //RTCOM_EL_EVENT_SET_FIELD (event, free_text, g_strdup(Contents().toUtf8()));
123
124         return event;
125 }
126
127 void PhoneCall::Export(const QString &baseDirectory) const
128 {
129 //      // Build the path and ensure it exists...
130 //      QString eventFilename(baseDirectory);
131 //      eventFilename += Destination() == EventTypes::PhoneCall::OUTGOING ? "/Sent/" : "/Inbox/";
132 //      eventFilename += QString::number(Timestamp().toUTC().date().year()) + "/";
133 //      QDir().mkpath(eventFilename);
134
135 //      // ...then build the filename and open it.
136 //      eventFilename += QString::number(Timestamp().toUTC().toTime_t()) + ".vmg";
137 //      QFile data(eventFilename);
138 //      if (data.open(QFile::WriteOnly | QFile::Truncate))
139 //      {
140 //              QTextStream stream(&data);
141
142 //              QTextCodec *oldCodec = stream.codec();
143 //              stream.setAutoDetectUnicode(false);
144 //              stream.setCodec("UTF-16LE");
145
146 //              EventParsers::VMGEntities::VMessage writer(NULL, 1.1);
147 //              writer.Write(stream, *this);
148 ////stream << "Test";
149 //              //stream.setCodec(oldCodec);
150 //              stream.flush();
151 //              data.close();
152
153 //              utimbuf fileTimes;
154 //              fileTimes.modtime = Timestamp().toUTC().toTime_t();
155 //              utime(eventFilename.toAscii(), &fileTimes);
156 //      }
157 }
158
159 QDebug operator<<(QDebug dbg, PhoneCall& event)
160 {
161         dbg.nospace() << "\tFolder:\t\t" << (event.Destination() == PhoneCall::OUTGOING ? "Sent" : "Inbox") << "\n";
162         dbg.nospace() << "\tTimestamp:\t" << event.Timestamp().toUTC() << "\n";
163         dbg.nospace() << "\tDuration:\t" << event.DurationInSeconds() << "\n";
164         dbg.nospace() << "\tRemote-Tel:\t" << event.Tel() << "\n";
165         //dbg.nospace() << "\tremote-name:\t" << event.fld_remote_name << "\n";
166
167         return dbg;
168 }