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"
22 #include "DBBackends/RtcomEventLogger.h"
23 #include "EventParsers/VMGEntities/VMessage.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 &SMS::DB() const
44 return DBBackends::RtcomEventLogger(CurrentSettings(), *this);
47 SMS::SMS(const Settings &settings) :
54 foreach(QSharedPointer<Attachment> attachment, m_Attachments)
60 SMS::SMS(const Settings &settings, const RTComElEvent &event, const QList<RTComElAttachment*> attachments) :
64 setIsRead(event.fld_is_read);
65 setDestination(event.fld_outgoing ? SENT : INBOX);
66 setTimestamp(QDateTime::fromTime_t(event.fld_start_time).toUTC());
67 setTel(QString::fromUtf8(event.fld_remote_uid));
68 if(Tel().indexOf("0") == 0)
69 setTel(QString(Tel()).replace(QRegExp("^0"), "+44"));
70 //setContents(QString(event.fld_free_text).replace("\n", QChar(0x2029)));
71 setContents(QString::fromUtf8(event.fld_free_text));
73 // We directly access the m_Attachments member variable here rather than the
74 // accessor as the accessor is const
75 if(attachments.count() > 0)
76 foreach(RTComElAttachment *attachment, attachments)
77 m_Attachments.append(new Attachment(*attachment));
80 const uint SMS::HashCode() const
82 // qDebug() << Timestamp().toUTC().toTime_t() << ", " << qHash(Tel()) << ", " << qHash(Destination()) << ", " << qHash(Contents()) << ", " << Attachments().HashCode();
84 // foreach(QChar c, Contents().toUtf8())
86 // qDebug() << c.unicode();
90 Timestamp().toUTC().toTime_t() ^
92 qHash(Destination()) ^
94 Attachments().HashCode();
97 RTComElEvent * SMS::toRTComEvent(const NumberToNameLookup &numberToNameLookup) const
99 QString groupId((Tel().length() < 7 || Tel().indexOf(QRegExp("[:alpha:]+")) > -1)
103 RTComElEvent *event(rtcom_el_event_new());
104 memset(event, 0, sizeof(RTComElEvent));
106 RTCOM_EL_EVENT_SET_FIELD (event, service, g_strdup("RTCOM_EL_SERVICE_SMS"));
107 RTCOM_EL_EVENT_SET_FIELD (event, event_type, g_strdup("RTCOM_EL_EVENTTYPE_SMS_MESSAGE"));
108 RTCOM_EL_EVENT_SET_FIELD (event, storage_time, Timestamp().toUTC().toTime_t());
109 RTCOM_EL_EVENT_SET_FIELD (event, start_time, Timestamp().toUTC().toTime_t());
110 RTCOM_EL_EVENT_SET_FIELD (event, end_time, Timestamp().toUTC().toTime_t());
111 RTCOM_EL_EVENT_SET_FIELD (event, is_read, IsRead() ? 1 : 0);
112 RTCOM_EL_EVENT_SET_FIELD (event, outgoing, Destination() == SMS::SENT ? 1 : 0);
113 RTCOM_EL_EVENT_SET_FIELD (event, local_uid, g_strdup("ring/tel/ring"));
114 //RTCOM_EL_EVENT_SET_FIELD (&event, local_name, g_strdup("<SelfHandle>"));
115 RTCOM_EL_EVENT_SET_FIELD (event, remote_uid, g_strdup(Tel().toUtf8()));
116 //RTCOM_EL_EVENT_SET_FIELD (&event, remote_name, g_strdup(QString::number(numberToNameLookup.ContactDetails().value(Tel()).second).toUtf8()));
117 RTCOM_EL_EVENT_SET_FIELD (event, remote_ebook_uid, g_strdup(QString::number(numberToNameLookup.ContactDetails().value(Tel()).first).toUtf8()));
118 RTCOM_EL_EVENT_SET_FIELD (event, group_uid, g_strdup(groupId.toUtf8()));
119 //RTCOM_EL_EVENT_SET_FIELD (event, free_text, g_strdup(Contents().replace(0x2029, "\n").toUtf8()));
120 RTCOM_EL_EVENT_SET_FIELD (event, free_text, g_strdup(Contents().toUtf8()));
125 void SMS::Export(const QString &baseDirectory) const
127 // Build the path and ensure it exists...
128 QString eventFilename(baseDirectory);
129 eventFilename += Destination() == EventTypes::SMS::SENT ? "/Sent/" : "/Inbox/";
130 eventFilename += QString::number(Timestamp().toUTC().date().year()) + "/";
131 QDir().mkpath(eventFilename);
133 // ...then build the filename and open it.
134 eventFilename += QString::number(Timestamp().toUTC().toTime_t()) + ".vmg";
135 QFile data(eventFilename);
136 if (data.open(QFile::WriteOnly | QFile::Truncate))
138 QTextStream stream(&data);
140 QTextCodec *oldCodec = stream.codec();
141 stream.setAutoDetectUnicode(false);
142 stream.setCodec("UTF-16LE");
144 EventParsers::VMGEntities::VMessage writer(NULL, 1.1);
145 writer.Write(stream, *this);
147 //stream.setCodec(oldCodec);
152 fileTimes.modtime = Timestamp().toUTC().toTime_t();
153 utime(eventFilename.toAscii(), &fileTimes);
157 QDebug operator<<(QDebug dbg, SMS& event)
159 dbg.nospace() << "\tFolder:\t\t" << (event.Destination() == SMS::SENT ? "Sent" : "Inbox") << "\n";
160 dbg.nospace() << "\tTimestamp:\t" << event.Timestamp().toUTC() << "\n";
161 dbg.nospace() << "\tRemote-Tel:\t" << event.Tel() << "\n";
162 //dbg.nospace() << "\tremote-name:\t" << event.fld_remote_name << "\n";
163 dbg.nospace() << "\tIs-read:\t\t" << (event.IsRead() ? "true" : "false") << "\n";
164 dbg.nospace() << "\tContents:\t" << event.Contents() << "\n";