1st attempt at an initial import.
[qwerkisync] / DBBackends / RtcomEventLogger.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 "RtcomEventLogger.h"
20
21 #include "EventProcessors/iEventProcessor.h"
22 #include "EventTypes/iEvent.h"
23 #include "EventTypes/SMS.h"
24 #include "Settings.h"
25
26 #include <QDebug>
27
28 #include <uuid/uuid.h>
29
30 #include <rtcom-eventlogger/event.h>
31 #include <rtcom-eventlogger/eventlogger.h>
32
33 using namespace DBBackends;
34
35 QDebug operator<<(QDebug, RTComElEvent &);
36 QDebug operator<<(QDebug, RTComElAttachment &);
37 QDebug operator<<(QDebug, GList &);
38 QDebug operator<<(QDebug, QList<RTComElAttachment*> &);
39
40 RtcomEventLogger::RtcomEventLogger(const Settings &settings) :
41         m_Settings(settings)
42 {
43         RTComEl *el(rtcom_el_new());
44         if(NULL != el)
45         {
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"));
51
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);
56
57                 g_object_unref(el);
58         }
59         else
60                 qDebug() << "Failed to create event logger.";
61 }
62
63 RtcomEventLogger::RtcomEventLogger(const Settings &settings, const EventTypes::RtcomEvent &event) :
64         m_Settings(settings)
65 {
66 }
67
68 void RtcomEventLogger::Process(EventProcessors::iEventProcessor &processor)
69 {
70         // Initialise the event logger
71         RTComEl *el = rtcom_el_new();
72         if(NULL != el)
73         {
74                 // Initialise a query
75                 RTComElQuery *query = rtcom_el_query_new(el);
76                 if(query != NULL)
77                 {
78                         bool incoming = CurrentSettings().ShouldProcess( Settings::TYPE_RECIEVED, Settings::EVENTTYPE_SMS);
79                         bool outgoing = CurrentSettings().ShouldProcess( Settings::TYPE_SENT, Settings::EVENTTYPE_SMS);
80
81                         // Prepare it...
82                         bool prepared = false;
83                         if(incoming && outgoing)
84                         {
85                                 prepared = rtcom_el_query_prepare(query,
86                                         "service-id",
87                                         m_ServiceIDs.value(SERVICE_ID_SMS),
88                                         RTCOM_EL_OP_EQUAL,
89
90                                         NULL);
91                         }
92                         else
93                         {
94                                 prepared = rtcom_el_query_prepare(query,
95                                         "service-id",
96                                         m_ServiceIDs.value(SERVICE_ID_SMS),
97                                         RTCOM_EL_OP_EQUAL,
98
99                                         "outgoing",
100                                         incoming ? 0 : 1,
101                                         RTCOM_EL_OP_EQUAL,
102
103                                         NULL);
104                         }
105
106                         qDebug() << "SQL:\n" << rtcom_el_query_get_sql(query);
107
108                         if(prepared)
109                         {
110                                 RTComElIter *it = rtcom_el_get_events(el, query);
111                                 if(it != NULL)
112                                 {
113                                         if(rtcom_el_iter_first(it))
114                                         {
115                                                 int eventCount = 0;
116                                                 qDebug() << "Getting event count...";
117                                                 while(rtcom_el_iter_next(it))
118                                                         ++eventCount;
119
120                                                 // Reset the iterator and grab the actual values
121                                                 qDebug() << "Resetting iterator...";
122                                                 g_object_unref(it);
123                                                 it = rtcom_el_get_events(el, query);
124                                                 if(it != NULL)
125                                                 {
126                                                         if(rtcom_el_iter_first(it))
127                                                         {
128                                                                 int idx = 0;
129                                                                 qDebug() << "Getting events...";
130                                                                 do
131                                                                 {
132                                                                         ++idx;
133                                                                         qDebug() << "Event #" << idx;
134
135                                                                         RTComElEvent revent;
136                                                                         memset(&revent, 0, sizeof(revent));
137
138                                                                         if(rtcom_el_iter_get_values (
139                                                                                 it,
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,
151                                                                                 NULL))
152                                                                         {
153                                                                                 qDebug() << revent;
154
155                                                                                 QList<RTComElAttachment *> rattachments;
156                                                                                 RTComElAttachIter *at_it = rtcom_el_iter_get_attachments(it);
157                                                                                 if(it != NULL)
158                                                                                 {
159                                                                                         qDebug() << "Attachments OK";
160                                                                                         if(rtcom_el_attach_iter_first(at_it))
161                                                                                         {
162                                                                                                 qDebug() << "Getting events...";
163
164                                                                                                 do
165                                                                                                 {
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));
171                                                                                         }
172                                                                                 }
173
174                                                                                 EventTypes::iEvent *const newEvent(CreateEvent(revent, rattachments));
175                                                                                 processor.Process(*newEvent);
176                                                                                 delete newEvent;
177
178                                                                                 processor.EmitEventProcessed(idx, eventCount);
179                                                                         }
180
181                                                                         rtcom_el_event_free_contents(&revent);
182                                                                 }
183                                                                 while(rtcom_el_iter_next(it));
184                                                                 qDebug() << "...all events retrieved.";
185                                                         }
186                                                 }
187                                                 else
188                                                         qDebug() << "Failed to reset iterator";
189                                         }
190                                         else
191                                                 qDebug() << "Failed to start iterator";
192                                 }
193                                 else
194                                         qDebug() << "Failed to get iterator. Do you have any events?";
195                         }
196                         else
197                                 qDebug() << "Failed to prepare the query.";
198
199                         g_object_unref(query);
200                 }
201                 else
202                         qDebug() << "Failed to create query.";
203
204                 g_object_unref(el);
205         }
206         else
207                 qDebug() << "Failed to create event logger.";
208 }
209
210 EventTypes::iEvent *const RtcomEventLogger::CreateEvent(RTComElEvent &revent, QList<RTComElAttachment*> &rattachments)
211 {
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);
214
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);
217
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);
220
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);
223
224         return NULL;
225 }
226
227 void RtcomEventLogger::Insert(EventTypes::iEvent &event, const NumberToNameLookup &numberToNameLookup)
228 {
229         if(EventTypes::RtcomEvent *rtcomEvent = dynamic_cast<EventTypes::RtcomEvent *>(&event))
230         {
231                 const uint UUID_STR_LEN(36);
232
233                 int newEventID(0);
234                 _RTComEl *el(rtcom_el_new());
235                 if(NULL != el)
236                 {
237                         // Convert our objects into RTCom structs
238                         RTComElEvent *revent(rtcomEvent->toRTComEvent(numberToNameLookup));
239                         GList *rattachments(event.Attachments().toRTComAttachments());
240
241                         GError *error(NULL);
242
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));
245                         uuid_t uuid;
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;
251
252                         qDebug() << "Inserting event:";
253                         qDebug() << *revent;
254                         qDebug() << *rattachments;
255
256                         // Add the event
257                         newEventID = rtcom_el_add_event_full(el, revent, rheaders, rattachments, &error);
258                         qDebug() << "new id: " << newEventID;
259                         if(error != NULL)
260                         {
261                                 qDebug() << "err: " << error->message;
262                                 g_error_free(error);
263                         }
264
265                         // Release the attachments
266                         g_list_foreach (rattachments, (GFunc) rtcom_el_free_attachment, NULL);
267                         g_list_free (rattachments);
268
269                         rtcom_el_event_free_contents(revent);
270                         rtcom_el_event_free(revent);
271                 }
272                 else
273                         qDebug() << "Unable to initalise eventlogger for insertion.";
274
275                 g_object_unref(el);
276         }
277
278         return;
279 }
280
281 QDebug operator<<(QDebug dbg, RTComElEvent &event)
282 {
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";
294
295         return dbg;
296 }
297
298 QDebug operator<<(QDebug dbg, RTComElAttachment &attachment)
299 {
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";
303
304         return dbg;
305 }
306
307 QDebug operator<<(QDebug dbg, GList &attachments)
308 {
309         dbg.nospace() << "Attachments" << "\n";
310
311         for (GList *attachment(&attachments); NULL != attachment; attachment = attachment->next)
312         {
313                 qDebug() << *(RTComElAttachment*)attachment->data;
314         }
315
316         dbg.nospace() << "\n";
317
318         return dbg;
319 }
320
321 QDebug operator<<(QDebug dbg, QList<RTComElAttachment *> &attachments)
322 {
323         dbg.nospace() << "Attachments" << "\n";
324
325         foreach(RTComElAttachment *attachment, attachments)
326                 dbg.nospace() << *attachment << "\n";
327
328         dbg.nospace() << "\n";
329
330         return dbg;
331 }