1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 2003-2007 Imendio AB
* Copyright (C) 2007-2008 Collabora Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authors: Xavier Claessens <xclaesse@gmail.com>
* Cosimo Alfarano <cosimo.alfarano@collabora.co.uk>
*/
#include "log-manager.h" // RO
#include "log-manager-priv.h" // W
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <gio/gio.h>
#include <glib/gstdio.h>
#include <telepathy-glib/interfaces.h>
#include <telepathy-glib/util.h>
#include <telepathy-logger/log-entry.h>
#include <telepathy-logger/log-store.h>
#include <telepathy-logger/log-store-empathy.h>
#include <telepathy-logger/datetime.h>
#include <telepathy-logger/util.h>
//#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
//#include <empathy-debug.h>
#define DEBUG g_debug
#define GET_PRIV(obj) TPL_GET_PRIV (obj, TplLogManager)
typedef struct
{
GList *stores;
} TplLogManagerPriv;
typedef void (*TplLogManagerFreeFunc) (gpointer * data);
typedef struct
{
TplLogManager *manager;
gpointer request;
TplLogManagerFreeFunc request_free;
TplLogManagerAsyncCallback cb;
gpointer user_data;
} TplLogManagerAsyncData;
typedef struct
{
TpAccount *account;
gchar *chat_id;
gboolean is_chatroom;
gchar *date;
guint num_messages;
TplLogMessageFilter filter;
gchar *search_text;
gpointer user_data;
gpointer logentry;
} TplLogManagerChatInfo;
G_DEFINE_TYPE (TplLogManager, tpl_log_manager, G_TYPE_OBJECT);
static TplLogManager *manager_singleton = NULL;
static void
log_manager_finalize (GObject * object)
{
TplLogManagerPriv *priv;
priv = GET_PRIV (object);
g_list_foreach (priv->stores, (GFunc) g_object_unref, NULL);
g_list_free (priv->stores);
}
/*
* - Singleton LogManager constructor -
* Initialises LogStores with LogStoreEmpathy instance
*/
static GObject *
log_manager_constructor (GType type,
guint n_props, GObjectConstructParam * props)
{
GObject *retval;
TplLogManagerPriv *priv;
if (manager_singleton)
{
retval = g_object_ref (manager_singleton);
}
else
{
retval = G_OBJECT_CLASS (tpl_log_manager_parent_class)->constructor
(type, n_props, props);
manager_singleton = TPL_LOG_MANAGER (retval);
g_object_add_weak_pointer (retval, (gpointer *) & manager_singleton);
priv = GET_PRIV (manager_singleton);
priv->stores = g_list_append (priv->stores,
g_object_new (TPL_TYPE_LOG_STORE_EMPATHY,
NULL));
}
return retval;
}
static void
tpl_log_manager_class_init (TplLogManagerClass * klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->constructor = log_manager_constructor;
object_class->finalize = log_manager_finalize;
g_type_class_add_private (object_class, sizeof (TplLogManagerPriv));
}
static void
tpl_log_manager_init (TplLogManager * manager)
{
TplLogManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
TPL_TYPE_LOG_MANAGER,
TplLogManagerPriv);
manager->priv = priv;
/* initialise thread support. It can be called just once, so check it already
* ON and call if if it's not.
* Threads are needed by Async APIs.
*/
if (!g_thread_supported ())
{
g_debug ("Initializing GThread");
g_thread_init (NULL);
}
else
g_debug ("GThread already initialized. Brilliant!");
}
TplLogManager *
tpl_log_manager_dup_singleton (void)
{
return g_object_new (TPL_TYPE_LOG_MANAGER, NULL);
}
/*
* @message: a TplLogEntry subclass
*/
gboolean
tpl_log_manager_add_message (TplLogManager * manager,
gpointer message, GError ** error)
{
TplLogManagerPriv *priv;
GList *l;
gboolean out = FALSE;
gboolean found = FALSE;
/* TODO: When multiple log stores appear with add_message implementations
* make this customisable. */
const gchar *add_store = "TpLogger";
g_return_val_if_fail (TPL_IS_LOG_MANAGER (manager), FALSE);
g_return_val_if_fail (TPL_IS_LOG_ENTRY (message), FALSE);
priv = GET_PRIV (manager);
for (l = priv->stores; l; l = g_list_next (l))
{
if (!tp_strdiff
(tpl_log_store_get_name (TPL_LOG_STORE (l->data)), add_store))
{
out = tpl_log_store_add_message (TPL_LOG_STORE (l->data),
message, error);
found = TRUE;
break;
}
}
if (!found)
DEBUG ("Failed to find chosen log store to write to.");
return out;
}
gboolean
tpl_log_manager_exists (TplLogManager * manager,
TpAccount * account,
const gchar * chat_id, gboolean chatroom)
{
GList *l;
TplLogManagerPriv *priv;
g_return_val_if_fail (TPL_IS_LOG_MANAGER (manager), FALSE);
g_return_val_if_fail (chat_id != NULL, FALSE);
priv = GET_PRIV (manager);
for (l = priv->stores; l; l = g_list_next (l))
{
if (tpl_log_store_exists (TPL_LOG_STORE (l->data),
account, chat_id, chatroom))
return TRUE;
}
return FALSE;
}
// returns a list of gchar dates
GList *
tpl_log_manager_get_dates (TplLogManager * manager,
TpAccount * account,
const gchar * chat_id, gboolean chatroom)
{
GList *l, *out = NULL;
TplLogManagerPriv *priv;
g_return_val_if_fail (TPL_IS_LOG_MANAGER (manager), NULL);
g_return_val_if_fail (chat_id != NULL, NULL);
priv = GET_PRIV (manager);
for (l = priv->stores; l; l = g_list_next (l))
{
TplLogStore *store = TPL_LOG_STORE (l->data);
GList *new;
/* Insert dates of each store in the out list. Keep the out list sorted
* and avoid to insert dups. */
new = tpl_log_store_get_dates (store, account, chat_id, chatroom);
while (new)
{
if (g_list_find_custom (out, new->data, (GCompareFunc) strcmp))
g_free (new->data);
else
out =
g_list_insert_sorted (out, new->data, (GCompareFunc) strcmp);
new = g_list_delete_link (new, new);
}
}
return out;
}
GList *
tpl_log_manager_get_messages_for_date (TplLogManager * manager,
TpAccount * account,
const gchar * chat_id,
gboolean chatroom, const gchar * date)
{
GList *l, *out = NULL;
TplLogManagerPriv *priv;
g_return_val_if_fail (TPL_IS_LOG_MANAGER (manager), NULL);
g_return_val_if_fail (chat_id != NULL, NULL);
priv = GET_PRIV (manager);
for (l = priv->stores; l; l = g_list_next (l))
{
TplLogStore *store = TPL_LOG_STORE (l->data);
out =
g_list_concat (out,
tpl_log_store_get_messages_for_date (store, account,
chat_id, chatroom,
date));
}
return out;
}
static gint
log_manager_message_date_cmp (gconstpointer a, gconstpointer b)
{
TplLogEntry *one = (TplLogEntry *) a;
TplLogEntry *two = (TplLogEntry *) b;
time_t one_time, two_time;
one_time = tpl_log_entry_get_timestamp (one);
two_time = tpl_log_entry_get_timestamp (two);
/* Return -1 of message1 is older than message2 */
return one_time < two_time ? -1 : one_time - two_time;
}
GList *
tpl_log_manager_get_filtered_messages (TplLogManager * manager,
TpAccount * account,
const gchar * chat_id,
gboolean chatroom,
guint num_messages,
TplLogMessageFilter filter,
gpointer user_data)
{
TplLogManagerPriv *priv;
GList *out = NULL;
GList *l;
guint i = 0;
g_return_val_if_fail (TPL_IS_LOG_MANAGER (manager), NULL);
g_return_val_if_fail (!TPL_STR_EMPTY (chat_id), NULL);
priv = GET_PRIV (manager);
/* Get num_messages from each log store and keep only the
* newest ones in the out list. Keep that list sorted: Older first. */
for (l = priv->stores; l; l = g_list_next (l))
{
TplLogStore *store = TPL_LOG_STORE (l->data);
GList *new;
new = tpl_log_store_get_filtered_messages (store, account, chat_id,
chatroom, num_messages,
filter, user_data);
while (new)
{
if (i < num_messages)
{
/* We have less message than needed so far. Keep this message */
out = g_list_insert_sorted (out, new->data,
(GCompareFunc)
log_manager_message_date_cmp);
i++;
}
else if (log_manager_message_date_cmp (new->data, out->data) > 0)
{
/* This message is newer than the oldest message we have in out
* list. Remove the head of out list and insert this message */
g_object_unref (out->data);
out = g_list_delete_link (out, out);
out = g_list_insert_sorted (out, new->data,
(GCompareFunc)
log_manager_message_date_cmp);
}
else
{
/* This message is older than the oldest message we have in out
* list. Drop it. */
g_object_unref (new->data);
}
new = g_list_delete_link (new, new);
}
}
return out;
}
GList *
tpl_log_manager_get_chats (TplLogManager * manager, TpAccount * account)
{
GList *l, *out = NULL;
TplLogManagerPriv *priv;
g_return_val_if_fail (TPL_IS_LOG_MANAGER (manager), NULL);
g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
priv = GET_PRIV (manager);
for (l = priv->stores; l; l = g_list_next (l))
{
TplLogStore *store = TPL_LOG_STORE (l->data);
out = g_list_concat (out, tpl_log_store_get_chats (store, account));
}
return out;
}
GList *
tpl_log_manager_search_in_identifier_chats_new (TplLogManager * manager,
TpAccount * account,
gchar const *identifier,
const gchar * text)
{
GList *l, *out = NULL;
TplLogManagerPriv *priv;
g_return_val_if_fail (TPL_IS_LOG_MANAGER (manager), NULL);
g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
g_return_val_if_fail (!TPL_STR_EMPTY (identifier), NULL);
g_return_val_if_fail (!TPL_STR_EMPTY (text), NULL);
priv = GET_PRIV (manager);
for (l = priv->stores; l; l = g_list_next (l))
{
TplLogStore *store = TPL_LOG_STORE (l->data);
out = g_list_concat (out,
tpl_log_store_search_in_identifier_chats_new
(store, account, identifier, text));
}
return out;
}
GList *
tpl_log_manager_search_new (TplLogManager * manager, const gchar * text)
{
GList *l, *out = NULL;
TplLogManagerPriv *priv;
g_return_val_if_fail (TPL_IS_LOG_MANAGER (manager), NULL);
g_return_val_if_fail (!TPL_STR_EMPTY (text), NULL);
priv = GET_PRIV (manager);
for (l = priv->stores; l; l = g_list_next (l))
{
TplLogStore *store = TPL_LOG_STORE (l->data);
out = g_list_concat (out, tpl_log_store_search_new (store, text));
}
return out;
}
void
tpl_log_manager_search_hit_free (TplLogSearchHit * hit)
{
if (hit->account != NULL)
g_object_unref (hit->account);
g_free (hit->date);
g_free (hit->filename);
g_free (hit->chat_id);
g_slice_free (TplLogSearchHit, hit);
}
void
tpl_log_manager_search_free (GList * hits)
{
GList *l;
for (l = hits; l; l = g_list_next (l))
{
tpl_log_manager_search_hit_free (l->data);
}
g_list_free (hits);
}
/* Format is just date, 20061201. */
gchar *
tpl_log_manager_get_date_readable (const gchar * date)
{
time_t t;
t = tpl_time_parse (date);
return tpl_time_to_string_local (t, "%a %d %b %Y");
}
/* Async */
static TplLogManagerAsyncData *
tpl_log_manager_async_data_new (void)
{
return g_slice_new (TplLogManagerAsyncData);
}
/*
static void
tpl_log_manager_async_data_free (TplLogManagerAsyncData * data)
{
g_return_if_fail (TPL_IS_LOG_MANAGER (data->manager));
g_return_if_fail (data->request && data->request_free);
g_object_unref (data->manager);
data->request_free (data->request);
g_free (data);
}
*/
static TplLogManagerChatInfo *
tpl_log_manager_chat_info_new (void)
{
return g_slice_new0 (TplLogManagerChatInfo);
}
static void
tpl_log_manager_chat_info_free (TplLogManagerChatInfo * data)
{
tpl_object_unref_if_not_null (data->account);
if (data->chat_id)
g_free (data->chat_id);
if (data->date)
g_free (data->date);
g_free (data);
}
static gpointer
_tpl_log_manager_async_operation_finish (TplLogManager * manager,
GAsyncResult * result,
TplLogManagerAsyncData * async_data)
{
GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
return g_simple_async_result_get_op_res_gpointer (simple);
}
static void
_tpl_log_manager_async_operation_cb (GObject * source_object,
GAsyncResult * result,
gpointer user_data)
{
TplLogManager *manager = TPL_LOG_MANAGER (source_object);
TplLogManagerAsyncData *async_data = (TplLogManagerAsyncData *) user_data;
gpointer retval;
retval =
_tpl_log_manager_async_operation_finish (manager, result, async_data);
if (async_data->cb)
{
async_data->cb (async_data->manager, retval, NULL,
async_data->user_data);
}
//tpl_log_manager_async_data_free(async_data);
}
/* wrapper around GIO's GSimpleAsync* */
static void
_tpl_log_manager_call_async_operation (TplLogManager * manager,
GSimpleAsyncThreadFunc
operation_thread_func,
TplLogManagerAsyncData * async_data,
TplLogManagerAsyncCallback callback)
{
GSimpleAsyncResult *simple;
simple = g_simple_async_result_new (G_OBJECT (manager),
_tpl_log_manager_async_operation_cb,
async_data,
_tpl_log_manager_async_operation_finish);
g_simple_async_result_run_in_thread (simple, operation_thread_func, 0,
NULL);
}
/* end of Async common function */
/* Start of add_message async implementation */
static void
_add_message_async_thread (GSimpleAsyncResult * simple, GObject * object,
GCancellable * cancellable)
{
TplLogManagerAsyncData *async_data;
TplLogManagerChatInfo *chat_info;
GError *error;
async_data = g_async_result_get_user_data (G_ASYNC_RESULT (simple));
chat_info = async_data->request;
tpl_log_manager_add_message (async_data->manager, chat_info->logentry,
&error);
if(error!=NULL) {
g_error("synchronous operation error: %s", error->message);
g_simple_async_result_set_from_error(simple, error);
g_clear_error(&error);
g_error_free(error);
} else
g_simple_async_result_set_op_res_gboolean (simple, TRUE);
}
void
tpl_log_manager_add_message_async (TplLogManager * manager,
gpointer message,
TplLogManagerAsyncCallback callback,
gpointer user_data,
GDestroyNotify destroy)
{
TplLogManagerChatInfo *chat_info = tpl_log_manager_chat_info_new ();
TplLogManagerAsyncData *async_data = tpl_log_manager_async_data_new ();
tpl_call_with_err_if_fail (TPL_IS_LOG_MANAGER (manager), manager,
TPL_LOG_MANAGER, FAILED,
"manager argument passed is not a TplManager instance",
callback, user_data);
tpl_call_with_err_if_fail (TPL_IS_LOG_ENTRY (message), manager,
TPL_LOG_MANAGER, FAILED,
"message argument passed is not a TplLogEntry instance",
callback, user_data);
/* TODO check account, chat_id within entry, after TplLogEntry refactoring
tpl_call_with_err_if_fail (TP_IS_ACCOUNT (account), manager,
TPL_LOG_MANAGER, FAILED,
"account argument is not a TpAccount instance",
callback, user_data);
tpl_call_with_err_if_fail (!TPL_STR_EMPTY (chat_id), manager,
TPL_LOG_MANAGER, FAILED,
"chat_id argument passed cannot be empty string or NULL ptr",
callback, user_data);
*/
chat_info->logentry = message;
g_object_ref (chat_info->logentry);
async_data->manager = manager;
g_object_ref (manager);
async_data->request = (gpointer) chat_info;
async_data->request_free =
(TplLogManagerFreeFunc) tpl_log_manager_chat_info_free;
async_data->cb = callback;
async_data->user_data = user_data;
_tpl_log_manager_call_async_operation (manager, _add_message_async_thread,
async_data, callback);
}
/* End of get_dates async implementation */
/* Start of get_dates async implementation */
static void
_get_dates_async_thread (GSimpleAsyncResult * simple, GObject * object,
GCancellable * cancellable)
{
TplLogManagerAsyncData *async_data;
TplLogManagerChatInfo *chat_info;
GList *lst = NULL;
async_data = g_async_result_get_user_data (G_ASYNC_RESULT (simple));
chat_info = async_data->request;
lst = tpl_log_manager_get_dates (async_data->manager,
chat_info->account, chat_info->chat_id,
chat_info->is_chatroom);
g_simple_async_result_set_op_res_gpointer (simple, lst, NULL); // TODO add destructor
}
void
tpl_log_manager_get_dates_async (TplLogManager * manager,
TpAccount * account,
const gchar * chat_id,
gboolean is_chatroom,
TplLogManagerAsyncCallback callback,
gpointer user_data, GDestroyNotify destroy)
{
TplLogManagerChatInfo *chat_info = tpl_log_manager_chat_info_new ();
TplLogManagerAsyncData *async_data = tpl_log_manager_async_data_new ();
tpl_call_with_err_if_fail (TPL_IS_LOG_MANAGER (manager), manager,
TPL_LOG_MANAGER, FAILED,
"manager argument passed is not a TplManager instance",
callback, user_data);
tpl_call_with_err_if_fail (TP_IS_ACCOUNT (account), manager,
TPL_LOG_MANAGER, FAILED,
"account argument is not a TpAccount instance",
callback, user_data);
tpl_call_with_err_if_fail (!TPL_STR_EMPTY (chat_id), manager,
TPL_LOG_MANAGER, FAILED,
"chat_id argument passed cannot be empty string or NULL ptr",
callback, user_data);
// TODO add check against manager, chat_info
// TODO add check against date!=NULL and call cb in case of error
chat_info->account = account;
g_object_ref (account);
chat_info->chat_id = g_strdup (chat_id);
chat_info->is_chatroom = is_chatroom;
async_data->manager = manager;
g_object_ref (manager);
async_data->request = (gpointer) chat_info;
async_data->request_free =
(TplLogManagerFreeFunc) tpl_log_manager_chat_info_free;
async_data->cb = callback;
async_data->user_data = user_data;
_tpl_log_manager_call_async_operation (manager, _get_dates_async_thread,
async_data, callback);
}
/* End of get_dates async implementation */
/* Start of get_messages_for_date async implementation */
static void
_get_messages_for_date_async_thread (GSimpleAsyncResult * simple,
GObject * object,
GCancellable * cancellable)
{
TplLogManagerAsyncData *async_data;
TplLogManagerChatInfo *chat_info;
GList *lst;
async_data = g_async_result_get_user_data (G_ASYNC_RESULT (simple));
chat_info = async_data->request;
lst = tpl_log_manager_get_messages_for_date (async_data->manager,
chat_info->account,
chat_info->chat_id,
chat_info->is_chatroom,
chat_info->date);
g_simple_async_result_set_op_res_gpointer (simple, lst, NULL); // TODO add destructor
}
void
tpl_log_manager_get_messages_for_date_async (TplLogManager * manager,
TpAccount * account,
const gchar * chat_id,
gboolean is_chatroom,
const gchar * date,
TplLogManagerAsyncCallback
callback, gpointer user_data,
GDestroyNotify destroy)
{
TplLogManagerChatInfo *chat_info = tpl_log_manager_chat_info_new ();
TplLogManagerAsyncData *async_data = tpl_log_manager_async_data_new ();
// TODO add check against manager, chat_info
// TODO add check against date!=NULL and call cb in case of error
tpl_call_with_err_if_fail (TPL_IS_LOG_MANAGER (manager), manager,
TPL_LOG_MANAGER, FAILED,
"manager argument passed is not a TplManager instance",
callback, user_data);
tpl_call_with_err_if_fail (TP_IS_ACCOUNT (account), manager,
TPL_LOG_MANAGER, FAILED,
"account argument is not a TpAccount instance",
callback, user_data);
tpl_call_with_err_if_fail (!TPL_STR_EMPTY (chat_id), manager,
TPL_LOG_MANAGER, FAILED,
"chat_id argument passed cannot be empty string or NULL ptr",
callback, user_data);
tpl_call_with_err_if_fail (!TPL_STR_EMPTY (date), manager,
TPL_LOG_MANAGER, FAILED,
"date argument passed cannot be empty string or NULL ptr",
callback, user_data);
chat_info->account = account;
g_object_ref (account);
chat_info->chat_id = g_strdup (chat_id);
chat_info->is_chatroom = is_chatroom;
chat_info->date = g_strdup(date);
async_data->manager = manager;
g_object_ref (manager);
async_data->request = (gpointer) chat_info;
async_data->request_free =
(TplLogManagerFreeFunc) tpl_log_manager_chat_info_free;
async_data->cb = callback;
async_data->user_data = user_data;
_tpl_log_manager_call_async_operation (manager,
_get_messages_for_date_async_thread,
async_data, callback);
}
/* End of get_messages_for_date async implementation */
/* Start of get_filtered_messages async implementation */
static void
_get_filtered_messages_thread (GSimpleAsyncResult * simple, GObject * object,
GCancellable * cancellable)
{
TplLogManagerAsyncData *async_data;
TplLogManagerChatInfo *chat_info;
GList *lst;
async_data = g_async_result_get_user_data (G_ASYNC_RESULT (simple));
chat_info = async_data->request;
lst = tpl_log_manager_get_filtered_messages (async_data->manager,
chat_info->account,
chat_info->chat_id,
chat_info->is_chatroom,
chat_info->num_messages,
chat_info->filter,
chat_info->user_data);
g_simple_async_result_set_op_res_gpointer (simple, lst, NULL); // TODO add destructor
}
void
tpl_log_manager_get_filtered_messages_async (TplLogManager * manager,
TpAccount * account,
const gchar * chat_id,
gboolean is_chatroom,
guint num_messages,
TplLogMessageFilter filter,
gpointer filter_user_data,
TplLogManagerAsyncCallback
callback, gpointer user_data,
GDestroyNotify destroy)
{
TplLogManagerChatInfo *chat_info = tpl_log_manager_chat_info_new ();
TplLogManagerAsyncData *async_data = tpl_log_manager_async_data_new ();
tpl_call_with_err_if_fail (TPL_IS_LOG_MANAGER (manager), manager,
TPL_LOG_MANAGER, FAILED,
"manager argument passed is not a TplManager instance",
callback, user_data);
tpl_call_with_err_if_fail (TP_IS_ACCOUNT (account), manager,
TPL_LOG_MANAGER, FAILED,
"account argument is not a TpAccount instance",
callback, user_data);
tpl_call_with_err_if_fail (!TPL_STR_EMPTY (chat_id), manager,
TPL_LOG_MANAGER, FAILED,
"chat_id argument passed cannot be empty string or NULL ptr",
callback, user_data);
tpl_call_with_err_if_fail ((num_messages > 0), manager,
TPL_LOG_MANAGER, FAILED,
"num_message argument passed needs to be greater than 0",
callback, user_data);
tpl_call_with_err_if_fail (filter != NULL, manager,
TPL_LOG_MANAGER, FAILED,
"filter function should be not NULL",
callback, user_data);
chat_info->account = account;
g_object_ref (account);
chat_info->chat_id = g_strdup (chat_id);
chat_info->is_chatroom = is_chatroom;
chat_info->num_messages = num_messages;
chat_info->filter = filter;
chat_info->user_data = filter_user_data;
async_data->manager = manager;
g_object_ref (manager);
async_data->request = (gpointer) chat_info;
async_data->request_free =
(TplLogManagerFreeFunc) tpl_log_manager_chat_info_free;
async_data->cb = callback;
async_data->user_data = user_data;
_tpl_log_manager_call_async_operation (manager,
_get_filtered_messages_thread,
async_data, callback);
}
/* End of get_filtered_messages async implementation */
/* Start of get_chats async implementation */
static void
_get_chats_thread (GSimpleAsyncResult * simple, GObject * object,
GCancellable * cancellable)
{
TplLogManagerAsyncData *async_data;
TplLogManagerChatInfo *chat_info;
GList *lst;
async_data = g_async_result_get_user_data (G_ASYNC_RESULT (simple));
chat_info = async_data->request;
lst = tpl_log_manager_get_chats (async_data->manager, chat_info->account);
g_simple_async_result_set_op_res_gpointer (simple, lst, NULL); // TODO add destructor
}
void
tpl_log_manager_get_chats_async (TplLogManager * manager,
TpAccount * account,
TplLogManagerAsyncCallback callback,
gpointer user_data, GDestroyNotify destroy)
{
TplLogManagerChatInfo *chat_info = tpl_log_manager_chat_info_new ();
TplLogManagerAsyncData *async_data = tpl_log_manager_async_data_new ();
tpl_call_with_err_if_fail (TPL_IS_LOG_MANAGER (manager), manager,
TPL_LOG_MANAGER, FAILED,
"manager argument is not a TplManager instance",
callback, user_data);
tpl_call_with_err_if_fail (TP_IS_ACCOUNT (account), manager,
TPL_LOG_MANAGER, FAILED,
"account argument is not a TpAccount instance",
callback, user_data);
chat_info->account = account;
g_object_ref (account);
async_data->manager = manager;
g_object_ref (manager);
async_data->request = (gpointer) chat_info;
async_data->request_free =
(TplLogManagerFreeFunc) tpl_log_manager_chat_info_free;
async_data->cb = callback;
async_data->user_data = user_data;
_tpl_log_manager_call_async_operation (manager,
_get_chats_thread,
async_data, callback);
}
/* End of get_filtered_messages async implementation */
/* Start of tpl_log_manager_search_in_identifier_chats_new async implementation */
static void
_search_in_identifier_chats_new_thread (GSimpleAsyncResult * simple,
GObject * object,
GCancellable * cancellable)
{
TplLogManagerAsyncData *async_data;
TplLogManagerChatInfo *chat_info;
GList *lst;
async_data = g_async_result_get_user_data (G_ASYNC_RESULT (simple));
chat_info = async_data->request;
lst = tpl_log_manager_search_in_identifier_chats_new (async_data->manager, chat_info->account,
chat_info->chat_id, chat_info->search_text);
// TODO add destructor
g_simple_async_result_set_op_res_gpointer (simple, lst, NULL);
}
void
tpl_log_manager_search_in_identifier_chats_new_async (TplLogManager *
manager,
TpAccount * account,
gchar const
*identifier,
const gchar * text,
TplLogManagerAsyncCallback callback,
gpointer user_data,
GDestroyNotify destroy)
{
TplLogManagerChatInfo *chat_info = tpl_log_manager_chat_info_new ();
TplLogManagerAsyncData *async_data = tpl_log_manager_async_data_new ();
tpl_call_with_err_if_fail (TPL_IS_LOG_MANAGER (manager), manager,
TPL_LOG_MANAGER, FAILED,
"manager argument is not a TplManager instance",
callback, user_data);
tpl_call_with_err_if_fail (TP_IS_ACCOUNT (account), manager,
TPL_LOG_MANAGER, FAILED,
"account argument is not a TpAccount instance",
callback, user_data);
chat_info->account = account;
g_object_ref (account);
chat_info->chat_id = g_strdup(identifier);
chat_info->search_text = g_strdup(text);
async_data->manager = manager;
g_object_ref (manager);
async_data->request = (gpointer) chat_info;
async_data->request_free =
(TplLogManagerFreeFunc) tpl_log_manager_chat_info_free;
async_data->cb = callback;
async_data->user_data = user_data;
_tpl_log_manager_call_async_operation (manager,
_search_in_identifier_chats_new_thread,
async_data, callback);
}
/* End of tpl_log_manager_search_in_identifier_chats_new async implementation */
/* Start of tpl_log_manager_search_new async implementation */
static void
_search_new_thread (GSimpleAsyncResult * simple,
GObject * object,
GCancellable * cancellable)
{
TplLogManagerAsyncData *async_data;
TplLogManagerChatInfo *chat_info;
GList *lst;
async_data = g_async_result_get_user_data (G_ASYNC_RESULT (simple));
chat_info = async_data->request;
lst = tpl_log_manager_search_new (async_data->manager, chat_info->search_text);
// TODO add destructor
g_simple_async_result_set_op_res_gpointer (simple, lst, NULL);
}
void
tpl_log_manager_search_new_async (TplLogManager * manager,
const gchar * text,
TplLogManagerAsyncCallback callback,
gpointer user_data,
GDestroyNotify destroy)
{
TplLogManagerChatInfo *chat_info = tpl_log_manager_chat_info_new ();
TplLogManagerAsyncData *async_data = tpl_log_manager_async_data_new ();
tpl_call_with_err_if_fail (TPL_IS_LOG_MANAGER (manager), manager,
TPL_LOG_MANAGER, FAILED,
"manager argument is not a TplManager instance",
callback, user_data);
chat_info->search_text = g_strdup(text);
async_data->manager = manager;
g_object_ref (manager);
async_data->request = (gpointer) chat_info;
async_data->request_free =
(TplLogManagerFreeFunc) tpl_log_manager_chat_info_free;
async_data->cb = callback;
async_data->user_data = user_data;
_tpl_log_manager_call_async_operation (manager,
_search_new_thread,
async_data, callback);
}
/* End of tpl_log_manager_search_new async implementation */
|