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 "RtcomEventLogger.h"
21 #include "EventProcessors/iEventProcessor.h"
22 #include "EventTypes/iEvent.h"
23 #include "EventTypes/SMS.h"
28 #include <uuid/uuid.h>
30 #include <rtcom-eventlogger/event.h>
31 #include <rtcom-eventlogger/eventlogger.h>
33 using namespace DBBackends;
35 QDebug operator<<(QDebug, RTComElEvent &);
36 QDebug operator<<(QDebug, RTComElAttachment &);
37 QDebug operator<<(QDebug, GList &);
38 QDebug operator<<(QDebug, QList<RTComElAttachment*> &);
40 RtcomEventLogger::RtcomEventLogger(const Settings &settings) :
43 RTComEl *el(rtcom_el_new());
46 // Grab the service IDs we want to work with
47 m_ServiceIDs.insert(SERVICE_ID_CALL, rtcom_el_get_service_id(el, "RTCOM_EL_SERVICE_CALL"));
48 m_ServiceIDs.insert(SERVICE_ID_CHAT, rtcom_el_get_service_id(el, "RTCOM_EL_SERVICE_CHAT"));
49 m_ServiceIDs.insert(SERVICE_ID_SMS, rtcom_el_get_service_id(el, "RTCOM_EL_SERVICE_SMS"));
50 m_ServiceIDs.insert(SERVICE_ID_MMS, rtcom_el_get_service_id(el, "RTCOM_EL_SERVICE_MMS"));
52 // Remove any service IDs that weren't found
53 foreach(ServiceID serviceID, m_ServiceIDs.keys())
54 if(m_ServiceIDs.value(serviceID) == -1)
55 m_ServiceIDs.remove(serviceID);
60 qDebug() << "Failed to create event logger.";
63 RtcomEventLogger::RtcomEventLogger(const Settings &settings, const EventTypes::RtcomEvent &event) :
68 void RtcomEventLogger::Process(EventProcessors::iEventProcessor &processor)
70 // Initialise the event logger
71 RTComEl *el = rtcom_el_new();
75 RTComElQuery *query = rtcom_el_query_new(el);
78 bool incoming = CurrentSettings().ShouldProcess( Settings::TYPE_RECIEVED, Settings::EVENTTYPE_SMS);
79 bool outgoing = CurrentSettings().ShouldProcess( Settings::TYPE_SENT, Settings::EVENTTYPE_SMS);
82 bool prepared = false;
83 if(incoming && outgoing)
85 prepared = rtcom_el_query_prepare(query,
87 m_ServiceIDs.value(SERVICE_ID_SMS),
94 prepared = rtcom_el_query_prepare(query,
96 m_ServiceIDs.value(SERVICE_ID_SMS),
106 qDebug() << "SQL:\n" << rtcom_el_query_get_sql(query);
110 RTComElIter *it = rtcom_el_get_events(el, query);
113 if(rtcom_el_iter_first(it))
116 qDebug() << "Getting event count...";
117 while(rtcom_el_iter_next(it))
120 // Reset the iterator and grab the actual values
121 qDebug() << "Resetting iterator...";
123 it = rtcom_el_get_events(el, query);
126 if(rtcom_el_iter_first(it))
129 qDebug() << "Getting events...";
133 qDebug() << "Event #" << idx;
136 memset(&revent, 0, sizeof(revent));
138 if(rtcom_el_iter_get_values (
140 "id", &revent.fld_id,
141 "service-id", &revent.fld_service_id,
142 "start-time", &revent.fld_start_time,
143 "end-time", &revent.fld_end_time,
144 "local-uid", &revent.fld_local_uid,
145 "local-name", &revent.fld_local_name,
146 "remote-uid", &revent.fld_remote_uid,
147 "remote-name", &revent.fld_remote_name,
148 "is-read", &revent.fld_is_read,
149 "outgoing", &revent.fld_outgoing,
150 "free-text", &revent.fld_free_text,
155 QList<RTComElAttachment *> rattachments;
156 RTComElAttachIter *at_it = rtcom_el_iter_get_attachments(it);
159 qDebug() << "Attachments OK";
160 if(rtcom_el_attach_iter_first(at_it))
162 qDebug() << "Getting events...";
166 rattachments.append(rtcom_el_attach_iter_get(at_it));
167 qDebug() << "Attachment ID #" << rattachments.last()->id << endl;
168 qDebug() << "desc: " << rattachments.last()->desc << endl;
169 qDebug() << "path: " << rattachments.last()->path << endl;
170 }while(rtcom_el_attach_iter_next(at_it));
174 EventTypes::iEvent *const newEvent(CreateEvent(revent, rattachments));
175 processor.Process(*newEvent);
178 processor.EmitEventProcessed(idx, eventCount);
181 rtcom_el_event_free_contents(&revent);
183 while(rtcom_el_iter_next(it));
184 qDebug() << "...all events retrieved.";
188 qDebug() << "Failed to reset iterator";
191 qDebug() << "Failed to start iterator";
194 qDebug() << "Failed to get iterator. Do you have any events?";
197 qDebug() << "Failed to prepare the query.";
199 g_object_unref(query);
202 qDebug() << "Failed to create query.";
207 qDebug() << "Failed to create event logger.";
210 EventTypes::iEvent *const RtcomEventLogger::CreateEvent(RTComElEvent &revent, QList<RTComElAttachment*> &rattachments)
212 //if(m_ServiceIDs.contains(SERVICE_ID_CALL) && revent.fld_service_id == m_ServiceIDs.value(SERVICE_ID_CALL))
213 // return new EventTypes::Call(CurrentSettings(), revent, rattachments);
215 //if(m_ServiceIDs.contains(SERVICE_ID_CHAT) && revent.fld_service_id == m_ServiceIDs.value(SERVICE_ID_CHAT))
216 // return new EventTypes::Chat(CurrentSettings(), revent, rattachments);
218 if(m_ServiceIDs.contains(SERVICE_ID_SMS) && revent.fld_service_id == m_ServiceIDs.value(SERVICE_ID_SMS))
219 return new EventTypes::SMS(CurrentSettings(), revent, rattachments);
221 //if(m_ServiceIDs.contains(SERVICE_ID_MMS) && revent.fld_service_id == m_ServiceIDs.value(SERVICE_ID_MMS))
222 // return new EventTypes::MMS(CurrentSettings(), revent, rattachments);
227 void RtcomEventLogger::Insert(EventTypes::iEvent &event, const NumberToNameLookup &numberToNameLookup)
229 if(EventTypes::RtcomEvent *rtcomEvent = dynamic_cast<EventTypes::RtcomEvent *>(&event))
231 const uint UUID_STR_LEN(36);
234 _RTComEl *el(rtcom_el_new());
237 // Convert our objects into RTCom structs
238 RTComElEvent *revent(rtcomEvent->toRTComEvent(numberToNameLookup));
239 GList *rattachments(event.Attachments().toRTComAttachments());
243 // Generate the headers for the event
244 GHashTable *rheaders(g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free));
246 char key[UUID_STR_LEN + 1];
247 uuid_generate_random(uuid);
248 uuid_unparse(uuid, key);
249 g_hash_table_insert(rheaders, g_strdup ("message-token"), key);
250 qDebug() << "headers: " << rheaders;
252 qDebug() << "Inserting event:";
254 qDebug() << *rattachments;
257 newEventID = rtcom_el_add_event_full(el, revent, rheaders, rattachments, &error);
258 qDebug() << "new id: " << newEventID;
261 qDebug() << "err: " << error->message;
265 // Release the attachments
266 g_list_foreach (rattachments, (GFunc) rtcom_el_free_attachment, NULL);
267 g_list_free (rattachments);
269 rtcom_el_event_free_contents(revent);
270 rtcom_el_event_free(revent);
273 qDebug() << "Unable to initalise eventlogger for insertion.";
281 QDebug operator<<(QDebug dbg, RTComElEvent &event)
283 dbg.nospace() << "\tid:\t\t" << event.fld_id << "\n";
284 dbg.nospace() << "\tFolder:\t\t" << (event.fld_outgoing ? "Sent" : "Inbox") << "\n";
285 dbg.nospace() << "\tstart-time:\t" << QDateTime::fromTime_t(event.fld_start_time) << "\n";
286 dbg.nospace() << "\tend-time:\t\t" << QDateTime::fromTime_t(event.fld_end_time) << "\n";
287 //dbg.nospace() << "\tlocal-uid:\t" << event.fld_local_uid << "\n";
288 //dbg.nospace() << "\tlocal-name:\t" << event.fld_local_name << "\n";
289 dbg.nospace() << "\tremote-uid:\t" << event.fld_remote_uid << "\n";
290 dbg.nospace() << "\tremote-name:\t" << event.fld_remote_name << "\n";
291 dbg.nospace() << "\tis-read:\t\t" << (event.fld_is_read ? "true" : "false") << "\n";
292 dbg.nospace() << "\tfree-text:\t" << event.fld_free_text << "\n";
293 dbg.nospace() << "\tgroup-uid:\t" << event.fld_group_uid << "\n";
298 QDebug operator<<(QDebug dbg, RTComElAttachment &attachment)
300 dbg.nospace() << "Event-id:\t" << attachment.event_id << "\n";
301 dbg.nospace() << "Path:\t" << attachment.path << "\n";
302 dbg.nospace() << "Desc:\t" << attachment.desc << "\n";
307 QDebug operator<<(QDebug dbg, GList &attachments)
309 dbg.nospace() << "Attachments" << "\n";
311 for (GList *attachment(&attachments); NULL != attachment; attachment = attachment->next)
313 qDebug() << *(RTComElAttachment*)attachment->data;
316 dbg.nospace() << "\n";
321 QDebug operator<<(QDebug dbg, QList<RTComElAttachment *> &attachments)
323 dbg.nospace() << "Attachments" << "\n";
325 foreach(RTComElAttachment *attachment, attachments)
326 dbg.nospace() << *attachment << "\n";
328 dbg.nospace() << "\n";