forked from voodoo-chile/Cambrian-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TApplicationBallotmaster.cpp
1022 lines (924 loc) · 33.5 KB
/
TApplicationBallotmaster.cpp
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
#ifndef PRECOMPILEDHEADERS_H
#include "PreCompiledHeaders.h"
#endif
#include "TApplicationBallotmaster.h"
#include "WLayoutBrowser.h"
// Callback to stop a running poll.
// When a poll is started and if the poll has a time length, thena timer is set to automatically stop the poll.
//
// INTERFACE NOTES
// Must have the same interface as PFn_TimerQueueCallback()
void
TimerQueueCallback_PollStop(PVPARAM pvEventBallotPoll)
{
Assert(pvEventBallotPoll != NULL);
((CEventBallotPoll *)pvEventBallotPoll)->StopPoll();
}
#if 0
void
DisplayApplicationBallotMaster()
{
TProfile * pProfile = NavigationTree_PGetSelectedTreeItemMatchingInterfaceTProfile();
if (pProfile == NULL)
return;
/*
TApplicationBallotmaster * pApplication = pProfile->PGetApplicationBallotmaster_NZ();
pApplication->TreeItemW_SelectWithinNavigationTree();
*/
CStr strPathApplication = "file:///" + pProfile->m_pConfigurationParent->SGetPathOfFileName("Apps/Ballotmaster/default.htm");
TBrowser * pBrowser = new TBrowser(pProfile);
pProfile->m_arraypaBrowsers.Add(PA_CHILD pBrowser);
pBrowser->SetIconNameAndUrl(eMenuAction_DisplayBallotMaster, "Ballots", strPathApplication);
pBrowser->TreeItemBrowser_DisplayWithinNavigationTree();
pBrowser->TreeItemW_SelectWithinNavigationTree();
}
#endif
CServiceBallotmaster::CServiceBallotmaster(TProfile * pProfileParent) : IService(pProfileParent), m_oVaultBallots(pProfileParent->PGetContactDummy_NZ())
{
}
// CServiceBallotmaster::IXmlExchange::XmlExchange()
void
CServiceBallotmaster::XmlExchange(INOUT CXmlExchanger * pXmlExchanger)
{
IService::XmlExchange(pXmlExchanger);
m_oVaultBallots.XmlExchange("Ballots", INOUT pXmlExchanger);
if (pXmlExchanger->m_fSerializing)
return;
// Initialize the timers for polls to automatically stop
TIMESTAMP tsNow = Timestamp_GetCurrentDateTime();
IEvent ** ppEventStop;
IEvent ** ppEvent = m_oVaultBallots.m_arraypaEvents.PrgpGetEventsStop(OUT &ppEventStop);
while (ppEvent != ppEventStop)
{
CEventBallotPoll * pEventBallotPoll = (CEventBallotPoll *)*ppEvent++;
Assert(pEventBallotPoll->EGetEventClass() == CEventBallotPoll::c_eEventClass);
if (pEventBallotPoll->m_tsStarted != d_ts_zNA && pEventBallotPoll->m_tsStopped == d_ts_zNA && pEventBallotPoll->m_cSecondsPollLength > 0)
{
TIMESTAMP tsStop = pEventBallotPoll->m_tsStarted + pEventBallotPoll->m_cSecondsPollLength * (TIMESTAMP_DELTA)d_ts_cSeconds;
TIMESTAMP_DELTA dtsStopping = tsStop - tsNow; // If this value is negative, it means the poll 'stopped' while SocietyPro was not running.
if (dtsStopping < 10 * d_ts_cMinutes)
dtsStopping = 10 * d_ts_cMinutes; // Make the poll has a minimum of 10 minutes to run so it may collect all pending votes before closing
MessageLog_AppendTextFormatSev(eSeverityNoise, "Poll ID $t which started on {tL} lasting $I seconds will automatically stop at {tL} in $I seconds ($T).\n", pEventBallotPoll->m_tsEventID, pEventBallotPoll->m_tsStarted, pEventBallotPoll->m_cSecondsPollLength, tsStop, (int)(dtsStopping / d_ts_cSeconds), dtsStopping);
TimerQueue_CallbackAdd(dtsStopping / d_ts_cSeconds, TimerQueueCallback_PollStop, pEventBallotPoll);
}
} // while
TimerQueue_DisplayToMessageLog();
} // XmlExchange()
// CServiceBallotmaster::IService::DetachFromObjectsAboutBeingDeleted()
void
CServiceBallotmaster::DetachFromObjectsAboutBeingDeleted()
{
}
CEventBallotPoll *
CServiceBallotmaster::PAllocateBallot(const IEventBallot * pEventBallotTemplate)
{
CEventBallotPoll * paEventBallot = new CEventBallotPoll;
paEventBallot->m_pVaultParent_NZ = &m_oVaultBallots;
if (pEventBallotTemplate != NULL)
{
MessageLog_AppendTextFormatSev(eSeverityNoise, "CServiceBallotmaster::PAllocateBallot() - Template $t\n", pEventBallotTemplate->m_tsEventID);
Assert(pEventBallotTemplate->m_pVaultParent_NZ != NULL);
CBinXcpStanzaEventCopier binXcpStanzaCopier(m_oVaultBallots.m_pParent);
binXcpStanzaCopier.EventCopy(IN pEventBallotTemplate, OUT paEventBallot);
paEventBallot->m_tsStarted = paEventBallot->m_tsStopped = d_zNA;
paEventBallot->m_uFlagsEvent |= IEvent::FE_kfEventDeleted;
}
m_oVaultBallots.m_arraypaEvents.Add(PA_CHILD paEventBallot);
// Assert(paEventBallot->m_uFlagsEvent & IEvent::FE_kfEventDeleted);
return paEventBallot;
}
// Make a copy of an existing ballot and add it to the Ballotmaster
void
CServiceBallotmaster::EventBallotAddAsTemplate(IEventBallot * pEventBallot)
{
Assert(pEventBallot != NULL);
(void)PAllocateBallot(pEventBallot);
}
void
TProfile::BallotMaster_EventBallotAddAsTemplate(IEventBallot * pEventBallot)
{
PGetServiceBallotmaster_NZ()->EventBallotAddAsTemplate(pEventBallot);
}
void
TProfile::BallotMaster_OnEventNewBallotReceived(CEventBallotReceived * pEventBallotReceived)
{
Assert(pEventBallotReceived->EGetEventClass() == CEventBallotReceived::c_eEventClass);
m_arraypEventsRecentBallots.AddEvent(pEventBallotReceived);
Dashboard_NewEventRelatedToBallot(pEventBallotReceived);
OJapiAppBallotmaster * pBallotmaster = OJapiAppBallotmaster::s_plistBallotmasters;
while (pBallotmaster != NULL)
{
pBallotmaster->OnEventBallotReceived(pEventBallotReceived);
pBallotmaster = pBallotmaster->m_pNext;
}
}
/*
void
TProfile::BallotMaster_onEventVoteReceived(CEventBallotSent * pEventBallotSent)
{
Assert(pEventBallotSent->EGetEventClass() == CEventBallotSent::c_eEventClass);
}
*/
///////////////////////////////////////////////////////////////////////////////////////////////////
OJapiBallot::OJapiBallot(IEventBallot * pEventBallot)
{
Assert(pEventBallot != NULL);
m_pEventBallot = pEventBallot;
}
QString
OJapiBallot::id() const
{
return Timestamp_ToStringBase85(m_pEventBallot->m_tsEventID);
}
QString
OJapiBallot::type() const
{
return (m_pEventBallot->EGetEventClass() == CEventBallotReceived::c_eEventClass) ? "ballot" : "poll";
}
// submit(), slot
// Submit a vote back to the sender of the poll.
// This method should be called only for received ballots to send the vote back to the poll sender.
bool
OJapiBallot::submit(const QString & sComment)
{
if (m_pEventBallot->EGetEventClass() == CEventBallotReceived::c_eEventClass)
{
CEventBallotReceived * pEventBallotReceived = (CEventBallotReceived *)m_pEventBallot;
// Calculate the bitmask from the selected choices
UINT_BALLOT_CHOICES ukmChoices = 0;
UINT iChoice = 0;
_CEventBallotChoice ** ppChoiceStop;
_CEventBallotChoice ** ppChoice = pEventBallotReceived->PrgpGetChoicesStop(OUT &ppChoiceStop);
while (ppChoice != ppChoiceStop)
{
_CEventBallotChoice * pChoice = *ppChoice++;
Assert(_CEventBallotChoice::F_kfIsSelected == 1);
ukmChoices |= (pChoice->m_uFlags & _CEventBallotChoice::F_kfIsSelected) << iChoice++;
}
MessageLog_AppendTextFormatSev(eSeverityNoise, "OJapiBallot::submit() - Event ID $t: $x $Q\n", m_pEventBallot->m_tsEventID, ukmChoices, &sComment);
pEventBallotReceived->SubmitVoteViaXospF(ukmChoices, sComment);
return true;
}
MessageLog_AppendTextFormatSev(eSeverityErrorWarning, "OJapiBallot::submit() - Event ID $t is not allowed to vote\n", m_pEventBallot->m_tsEventID);
return false;
}
QString
OJapiBallot::originator() const
{
if (m_pEventBallot->EGetEventClass() == CEventBallotReceived::c_eEventClass)
{
TContact * pContactSender = m_pEventBallot->PGetContactForReply_YZ();
if (pContactSender != NULL)
return pContactSender->TreeItem_SGetNameDisplay();
}
return m_pEventBallot->PGetProfile()->m_strNameProfile; // At the moment, the originator is the profile/role who sent the ballot
}
QString
OJapiBallot::status() const
{
Assert(m_pEventBallot != NULL);
const EEventClass eEventClass = m_pEventBallot->EGetEventClass();
if (eEventClass == CEventBallotReceived::c_eEventClass)
{
return ((((CEventBallotReceived *)m_pEventBallot)->m_ukmChoices) == 0) ? "unvoted" : "voted";
}
Assert(eEventClass == CEventBallotPoll::c_eEventClass);
if (eEventClass == CEventBallotPoll::c_eEventClass)
{
return ((CEventBallotPoll *)m_pEventBallot)->PszGetStatus();
}
MessageLog_AppendTextFormatCo(d_coBlue, "OJapiBallot::OJapiBallot() - returning an empty string for Event ID $t\n", m_pEventBallot->m_tsEventID);
return c_sEmpty;
}
PSZAC
CEventBallotPoll::PszGetStatus() const
{
if (m_tsStopped != d_ts_zNA)
return "stopped";
if (m_tsStarted != d_ts_zNA)
return "started";
return (m_uFlagsEvent & FE_kfEventDeleted) ? "unsaved" : "unstarted";
}
QString
OJapiBallot::title() const
{
return m_pEventBallot->m_strTitle;
}
void
OJapiBallot::title(const QString & sTitle)
{
m_pEventBallot->m_strTitle = sTitle;
}
QString
OJapiBallot::description() const
{
return m_pEventBallot->m_strDescription;
}
void
OJapiBallot::description(const QString & sDescription)
{
m_pEventBallot->m_strDescription = sDescription;
}
QVariantList
OJapiBallot::options() const
{
Assert(m_pEventBallot != NULL);
IEventBallot * pEvent = m_pEventBallot;
if (pEvent->EGetEventClass() == CEventBallotPoll::c_eEventClass)
{
// Fetching the options is a bit more complex because we also want the vote tally/counts.
// Therefore we have to see if there has been a ballot sent, and get the options from there.
pEvent = ((CEventBallotPoll *)pEvent)->PGetEventBallotSend_YZ();
if (pEvent == NULL)
pEvent = m_pEventBallot; // Use the poll template
}
return pEvent->LsGetChoices();
}
void
OJapiBallot::options(const QVariantList & lsOptions)
{
return m_pEventBallot->SetChoices(lsOptions);
}
bool
OJapiBallot::allowComments() const
{
return ((m_pEventBallot->m_uFlagsBallot & IEventBallot::FB_kfAllowNoComments) == 0);
}
void
OJapiBallot::allowComments(bool fAllowComments)
{
if (fAllowComments)
m_pEventBallot->m_uFlagsBallot &= ~IEventBallot::FB_kfAllowNoComments;
else
m_pEventBallot->m_uFlagsBallot |= IEventBallot::FB_kfAllowNoComments;
}
bool
OJapiBallot::allowMultipleChoices() const
{
return ((m_pEventBallot->m_uFlagsBallot & IEventBallot::FB_kfAllowMultipleChoices) != 0);
}
void
OJapiBallot::allowMultipleChoices(bool fAllowMultipleChoices)
{
if (fAllowMultipleChoices)
m_pEventBallot->m_uFlagsBallot |= IEventBallot::FB_kfAllowMultipleChoices;
else
m_pEventBallot->m_uFlagsBallot &= ~IEventBallot::FB_kfAllowMultipleChoices;
}
bool
OJapiBallot::isTemplate()
{
return ((m_pEventBallot->m_uFlagsBallot & IEventBallot::FB_kfBallotmasterTemplate) != 0);
}
void
OJapiBallot::isTemplate(bool fIsTemplate)
{
if (fIsTemplate)
m_pEventBallot->m_uFlagsBallot |= IEventBallot::FB_kfBallotmasterTemplate;
else
m_pEventBallot->m_uFlagsBallot &= ~IEventBallot::FB_kfBallotmasterTemplate;
}
bool
OJapiBallot::isSelected()
{
return ((m_pEventBallot->m_uFlagsBallot & IEventBallot::FB_kfBallotmasterTemplate) != 0);
}
void
OJapiBallot::isSelected(bool fIsSelected)
{
if (fIsSelected)
m_pEventBallot->m_uFlagsEvent |= IEvent::FE_kfEventSelected;
else
m_pEventBallot->m_uFlagsEvent &= ~IEvent::FE_kfEventSelected;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
OJapiPollCore::OJapiPollCore(CEventBallotPoll * pBallot) : OJapiBallot(pBallot)
{
Assert(pBallot != NULL);
Assert(pBallot->EGetEventClass() == CEventBallotPoll::c_eEventClass);
m_pBallot = pBallot;
}
int
OJapiPollCore::pollTimeLength() const
{
Assert(m_pBallot != NULL);
return m_pBallot->m_cSecondsPollLength;
}
void
OJapiPollCore::pollTimeLength(int cSeconds)
{
Assert(m_pBallot != NULL);
m_pBallot->m_cSecondsPollLength = cSeconds;
}
QString
OJapiPollCore::dismissText() const
{
Assert(m_pBallot != NULL);
return m_pBallot->m_strButtonDismiss;
}
void
OJapiPollCore::dismissText(const QString & sText)
{
Assert(m_pBallot != NULL);
m_pBallot->m_strButtonDismiss = sText;
}
QString
OJapiPollCore::submitText() const
{
Assert(m_pBallot != NULL);
return m_pBallot->m_strButtonSubmit;
}
void
OJapiPollCore::submitText(const QString & sText)
{
Assert(m_pBallot != NULL);
m_pBallot->m_strButtonSubmit = sText;
}
void
OJapiPollCore::pollTargetId(const CString & sTargetId)
{
Assert(m_pBallot != NULL);
m_pBallot->m_strTargetIdentifier = sTargetId;
Report((m_pBallot->m_uFlagsEvent & IEvent::FE_kfEventError) == 0); // I think this will fail, however it is good documentation
m_pBallot->m_uFlagsEvent &= ~IEvent::FE_kfEventError; // Remove any previous error
}
QString
OJapiPollCore::pollTargetId() const
{
Assert(m_pBallot != NULL);
return m_pBallot->m_strTargetIdentifier;
}
QDateTime
OJapiPollCore::dateStarted() const
{
Assert(m_pBallot != NULL);
return Timestamp_ToQDateTime(m_pBallot->m_tsStarted);
}
QDateTime
OJapiPollCore::dateStopped() const
{
Assert(m_pBallot != NULL);
return Timestamp_ToQDateTime(m_pBallot->m_tsStopped);
}
OJapiPollResultsStats::OJapiPollResultsStats(CEventBallotPoll * pBallot)
{
Assert(pBallot != NULL);
m_pBallot = pBallot;
InitToZeroes(OUT &m_statistics, sizeof(m_statistics));
}
///////////////////////////////////////////////////////////////////////////////////////////////////
OJapiPollOption::OJapiPollOption(_CEventBallotChoice * pChoice)
{
Assert(pChoice != NULL);
m_pChoice = pChoice;
}
QString
OJapiPollOption::text() const
{
return m_pChoice->m_strQuestion;
}
bool
OJapiPollOption::isSelected()
{
return ((m_pChoice->m_uFlags & _CEventBallotChoice::F_kfIsSelected) != 0);
}
void
OJapiPollOption::isSelected(bool fIsSelected)
{
if (fIsSelected)
m_pChoice->m_uFlags |= _CEventBallotChoice::F_kfIsSelected;
else
m_pChoice->m_uFlags &= ~_CEventBallotChoice::F_kfIsSelected;
}
int
OJapiPollOption::count() const
{
return m_pChoice->m_cVotes;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
OJapiPollResultsComment::OJapiPollResultsComment(_CEventBallotVote * pComment )
{
m_pComment = pComment;
}
QDateTime
OJapiPollResultsComment::date()
{
return Timestamp_ToQDateTime(m_pComment->m_tsVote);
}
QString
OJapiPollResultsComment::comment()
{
return m_pComment->m_strComment;
}
QString
OJapiPollResultsComment::name()
{
return m_pComment->m_pContact->TreeItem_SGetNameDisplay();
}
OJapiPollResults::OJapiPollResults(CEventBallotPoll * pBallot) : OJapiPollCore(pBallot), m_oStats(pBallot)
{
Assert(pBallot != NULL);
}
// Return a list of comments from those who voted
QVariant
OJapiPollResults::comments() const
{
//MessageLog_AppendTextFormatCo(d_coBlue, "OJapiPollResults::comments($t) m_pEventBallotSent=0x$p\n", &m_pBallot->m_tsEventID, m_pEventBallotSent);
QVariantList lvComments;
CEventBallotSent * pEventBallotSent = m_pBallot->PGetEventBallotSend_YZ();
if (pEventBallotSent != NULL)
{
_CEventBallotVote ** ppVoteStop;
_CEventBallotVote ** ppVote = pEventBallotSent->m_arraypaVotes.PrgpGetVotesStop(OUT &ppVoteStop);
while (ppVote != ppVoteStop)
{
_CEventBallotVote * pVote = *ppVote++;
//MessageLog_AppendTextFormatCo(d_coBlue, "OJapiPollResults::comments($t) - tsVote=$t, $S: $S\n", m_pBallot->m_tsEventID, pVote->m_tsVote, &pVote->m_pContact->m_strJidBare, &pVote->m_strComment);
if (pVote->m_strComment.FIsEmptyString())
continue;
#if 1
if (pVote->m_paoJapiPollResultsComment == NULL)
pVote->m_paoJapiPollResultsComment = new OJapiPollResultsComment(pVote);
lvComments.append(QVariant::fromValue(pVote->m_paoJapiPollResultsComment));
#else
lvComments.append(QVariant::fromValue(new OJapiPollResultsComment(pVote)));
#endif
}
}
return lvComments;
}
// Return an array of integers representing the number of votes per choice.
// This method is likely to be unused because the object OJapiPollOption has a method count().
QVariant
OJapiPollResults::counts() const
{
Assert(m_pBallot != NULL);
MessageLog_AppendTextFormatCo(d_coBlue, "OJapiPollResults::counts()\n");
QVariantList list;
CEventBallotSent * pEventBallotSent = m_pBallot->PGetEventBallotSend_YZ();
if (pEventBallotSent != NULL)
{
_CEventBallotChoice ** ppChoiceStop;
_CEventBallotChoice ** ppChoice = pEventBallotSent->PrgpGetChoicesStopWithTally(OUT &ppChoiceStop);
while (ppChoice != ppChoiceStop)
{
_CEventBallotChoice * pChoice = *ppChoice++;
list.append(pChoice->m_cVotes);
}
}
return list;
}
void
CEventBallotSent::CalculateStatistics(OUT SEventPollStatistics * pStatistics)
{
Assert(pStatistics != NULL);
InitToZeroes(OUT pStatistics, sizeof(*pStatistics));
pStatistics->cSent = 1; // The ballot was sent to an indivdiual
// To determine how many recipients received the ballot, we need to look at the vault.
TGroup * pGroup = (TGroup *)m_pVaultParent_NZ->m_pParent;
if (pGroup->EGetRuntimeClass() == RTI(TGroup))
{
pStatistics->cSent = pGroup->m_arraypaMembers.GetSize(); // TODO: If a new group member was added or removed, the code should look at the timestamps the poll started to include only those at the time of the poll
}
pStatistics->cResponded = m_arraypaVotes.GetSize();
pStatistics->cPending = pStatistics->cSent - pStatistics->cResponded;
}
// Return basic statistics about the poll
POJapiPollResultsStats
OJapiPollResults::stats()
{
Assert(m_pBallot != NULL);
CEventBallotSent * pEventBallotSent = m_pBallot->PGetEventBallotSend_YZ();
if (pEventBallotSent != NULL)
pEventBallotSent->CalculateStatistics(OUT &m_oStats.m_statistics);
return &m_oStats;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
OJapiPoll::OJapiPoll(CEventBallotPoll * pBallot) : OJapiPollCore(pBallot)
{
m_paoJapiPollResults = NULL;
}
OJapiPoll::~OJapiPoll()
{
MessageLog_AppendTextFormatSev(eSeverityErrorWarning, "OJapiPoll::~OJapiPoll() 0x$p\n", this);
delete m_paoJapiPollResults;
}
bool
OJapiPoll::save(/*const QString & sDebugContext*/)
{
m_pBallot->m_uFlagsEvent &= ~IEvent::FE_kfEventDeleted;
TProfile * pProfile = m_pBallot->PGetProfile();
pProfile->m_pConfigurationParent->XmlConfigurationSaveToFile(); // Force a save to make sure if the machine crashes, the poll have been saved
QString sStatus = status();
MessageLog_AppendTextFormatCo(d_coBlue, "OJapiPoll::save() - Poll ID $t, status=$Q\n", /*&sDebugContext,*/ m_pBallot->m_tsEventID, &sStatus);
// Notify each Ballotmaster app the poll has been saved
OJapiAppBallotmaster * pBallotmaster = OJapiAppBallotmaster::s_plistBallotmasters;
while (pBallotmaster != NULL)
{
if (pBallotmaster->PGetProfile() == pProfile)
{
MessageLog_AppendTextFormatCo(d_coBlue, "Ballotmaster 0x$p: emitting onPollSaved() - Poll ID $t\n", pBallotmaster, m_pBallot->m_tsEventID);
emit pBallotmaster->onPollSaved(this);
}
pBallotmaster = pBallotmaster->m_pNext;
}
return true;
}
void
OJapiPoll::destroy()
{
m_pBallot->m_uFlagsEvent |= IEvent::FE_kfEventDeleted;
OJapiAppBallotmaster * pBallotmaster = OJapiAppBallotmaster::s_plistBallotmasters;
while (pBallotmaster != NULL)
{
if (pBallotmaster->PGetProfile() == m_pBallot->PGetProfile())
{
MessageLog_AppendTextFormatCo(d_coBlue, "Ballotmaster 0x$p: emitting onPollSaved() - Poll ID $t\n", pBallotmaster, m_pBallot->m_tsEventID);
emit pBallotmaster->onPollDestroyed(this);
}
pBallotmaster = pBallotmaster->m_pNext;
}
}
// This method return a pointer to a group or a contact.
// The name of the method uses the word 'group' because most polls are sent to groups.
ITreeItemChatLogEvents *
CEventBallotPoll::PGetGroupTarget_YZ()
{
PSZUC pszGroupIdentifier = m_strTargetIdentifier;
Assert(pszGroupIdentifier != NULL);
TAccountXmpp * pAccount = PGetAccount();
ITreeItemChatLogEvents * pContactOrGroup = pAccount->Contact_PFindByJID(pszGroupIdentifier, eFindContact_zDefault);
if (pContactOrGroup == NULL)
pContactOrGroup = pAccount->Group_PFindByIdentifier_YZ(pszGroupIdentifier);
if (pContactOrGroup != NULL)
return pContactOrGroup;
if (*pszGroupIdentifier != '\0')
MessageLog_AppendTextFormatSev(eSeverityErrorWarning, "CEventBallotPoll::PGetGroupTarget_YZ() - Invalid pollTargetId '$s'\n", pszGroupIdentifier);
return NULL;
}
CEventBallotSent *
CEventBallotPoll::PGetEventBallotSend_YZ() CONST_MCC
{
if ((m_pEventBallotSent == NULL) && (m_uFlagsEvent & FE_kfEventError) == 0)
{
// Attempt to find the ballot
ITreeItemChatLogEvents * pGroup = PGetGroupTarget_YZ();
if (pGroup != NULL && m_tsStarted != d_ts_zNA)
{
m_pEventBallotSent = (CEventBallotSent *)pGroup->Vault_PGet_NZ()->PFindEventByID(m_tsStarted);
if (m_pEventBallotSent == NULL || m_pEventBallotSent->EGetEventClass() != CEventBallotSent::c_eEventClass)
{
MessageLog_AppendTextFormatSev(eSeverityWarningToErrorLog, "CEventBallotPoll::PGetEventBallotSend_YZ($t) - Unable to find m_pEventBallotSent matching timestamp $t\n", m_tsEventID, m_tsStarted);
m_pEventBallotSent = NULL;
m_uFlagsEvent |= FE_kfEventError;
}
}
}
return m_pEventBallotSent;
}
// Return true if the poll was successfully started.
bool
CEventBallotPoll::FStartPoll()
{
m_tsStopped = d_ts_zNA; // Clear this variable in case it is a resume of a poll
m_uFlagsBallot &= ~FB_kfStopAcceptingVotes;
if (m_tsStarted == d_ts_zNA)
{
Assert(m_pEventBallotSent == NULL);
ITreeItemChatLogEvents * pGroup = PGetGroupTarget_YZ();
if (pGroup == NULL)
return false;
m_tsStarted = Timestamp_GetCurrentDateTime();
// To send a poll, we need to clone the template and add it to the Chat Log of the group (or contact)
m_pEventBallotSent = new CEventBallotSent(IN &m_tsStarted); // Create a ballot with the same Event ID
m_pEventBallotSent->Event_InitFromDataOfEvent(this);
m_pEventBallotSent->m_uFlagsBallot |= FB_kfFromBallotmaster;
pGroup->Vault_AddEventToChatLogAndSendToContacts(PA_CHILD m_pEventBallotSent);
if (m_cSecondsPollLength > 0)
{
TimerQueue_CallbackAdd(m_cSecondsPollLength, TimerQueueCallback_PollStop, this);
//TimerQueue_DisplayToMessageLog();
}
}
return true;
}
void
CEventBallotPoll::StopPoll()
{
Assert(EGetEventClass() == c_eEventClass);
MessageLog_AppendTextFormatSev(eSeverityNoise, "Stopping Poll ID $t\n", m_tsEventID);
m_uFlagsBallot |= FB_kfStopAcceptingVotes;
if (PGetEventBallotSend_YZ() != NULL)
m_pEventBallotSent->m_uFlagsBallot |= FB_kfStopAcceptingVotes;
if (m_tsStarted != d_ts_zNA && m_tsStopped == d_ts_zNA)
{
// Stop the poll if it was started and not already stopped
m_tsStopped = Timestamp_GetCurrentDateTime();
// Notify each Ballotmaster about the poll stopped
OJapiAppBallotmaster * pBallotmaster = OJapiAppBallotmaster::s_plistBallotmasters;
while (pBallotmaster != NULL)
{
pBallotmaster->OnEventPollStopped(this);
pBallotmaster = pBallotmaster->m_pNext;
}
}
}
bool
OJapiPoll::start()
{
return m_pBallot->FStartPoll();
}
void
OJapiPoll::stop()
{
m_pBallot->StopPoll();
}
// Send the ballot to a group, or to a contact
// This method is for debugging
bool
OJapiPoll::send(const QString & sGroupId)
{
// In order to send the poll to the group, we need to find the TGroup object
CStr strGroupId = sGroupId;
TAccountXmpp * pAccount = m_pBallot->PGetAccount();
ITreeItemChatLogEvents * pContactOrGroup = pAccount->Contact_PFindByJID(strGroupId, eFindContact_zDefault);
if (pContactOrGroup == NULL)
pContactOrGroup = pAccount->Group_PFindByIdentifier_YZ(strGroupId);
MessageLog_AppendTextFormatCo(d_coBlack, "OJapiPoll::send($Q) - pContactOrGroup = 0x$p\n", &sGroupId, pContactOrGroup);
if (pContactOrGroup != NULL)
{
// To send the poll, we need to clone it and add it to the group (this code is a quite ugly at the moment, however it is for proof of concept)
CEventBallotPoll * paEventBallot = new CEventBallotPoll;
paEventBallot->m_pVaultParent_NZ = m_pBallot->m_pVaultParent_NZ;
CBinXcpStanzaEventCopier binXcpStanzaCopier(m_pBallot->m_pVaultParent_NZ->m_pParent);
binXcpStanzaCopier.EventCopy(IN m_pBallot, OUT paEventBallot);
paEventBallot->m_pVaultParent_NZ = NULL;
pContactOrGroup->Vault_AddEventToChatLogAndSendToContacts(PA_CHILD paEventBallot);
#if 0
paEventBallot->m_uFlagsBallot |= CEventBallotPoll::FB_kfStopAcceptingVotes; // Testing the stop()
stop();
//start();
#endif
return true;
}
return false;
}
void
OJapiAppBallotmaster::open()
{
LaunchApplication_Ballotmaster(NULL);
}
POJapiPollResults
OJapiPoll::getResults() CONST_MCC
{
if (m_paoJapiPollResults == NULL)
m_paoJapiPollResults = new OJapiPollResults(m_pBallot);
return m_paoJapiPollResults;
}
QVariantList OJapiPoll::listAttatchments()
{
QVariantList list;
CEventBallotAttatchment **ppBallotAttatchmentStop;
CEventBallotAttatchment **ppBallotAttatchment = m_pBallot->m_arraypaAtattchments.PrgpGetAttatchmentsStop(&ppBallotAttatchmentStop);
while ( ppBallotAttatchment != ppBallotAttatchmentStop)
{
CEventBallotAttatchment *pBallotAttatchment = *ppBallotAttatchment++;
list.append(QVariant::fromValue(pBallotAttatchment->POJapiGet()));
}
return list;
}
void OJapiPoll::addAttatchment(const QString &strName, const QString &strContentBase64, const QString strMimeType)
{
CEventBallotAttatchment *pAttatchment = m_pBallot->PAllocateNewAttatchment();
pAttatchment->m_strMimeType = strMimeType;
pAttatchment->m_strName = strName;
pAttatchment->m_binContent.BinAppendBinaryDataFromBase64(strContentBase64);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
OJapiAppBallotmaster * OJapiAppBallotmaster::s_plistBallotmasters; // Declare the static variable
OJapiAppBallotmaster::OJapiAppBallotmaster(OJapiCambrian * poCambrian, const SApplicationHtmlInfo *pApplicationInfo) : OJapiAppInfo(pApplicationInfo)
{
Assert(poCambrian != NULL);
m_pServiceBallotmaster = poCambrian->m_pProfile->PGetServiceBallotmaster_NZ();
Assert(m_pServiceBallotmaster->EGetRuntimeClass() == RTI_SZ(CServiceBallotmaster));
m_pNext = s_plistBallotmasters; // Insert the new objec at the beginning of the list
s_plistBallotmasters = this;
}
OJapiAppBallotmaster::~OJapiAppBallotmaster()
{
MessageLog_AppendTextFormatSev(eSeverityWarningToErrorLog, "OJapiAppBallotmaster::~OJapiAppBallotmaster() 0x$p\n", this); // For debugging, display in the Error Log when the object is destroyed (to make sure we have not missed it)
// Remove the Ballotmaster from the list
OJapiAppBallotmaster ** ppBallotmasterRemove = &s_plistBallotmasters;
OJapiAppBallotmaster * pBallotmaster = s_plistBallotmasters;
while (TRUE)
{
if (pBallotmaster == this)
{
*ppBallotmasterRemove = m_pNext;
break;
}
ppBallotmasterRemove = &pBallotmaster->m_pNext;
pBallotmaster = pBallotmaster->m_pNext;
Assert(pBallotmaster != NULL && "The OJapiAppBallotmaster should be in the list");
}
}
TProfile *
OJapiAppBallotmaster::PGetProfile() const
{
return m_pServiceBallotmaster->m_pProfileParent;
}
IEventBallot **
OJapiAppBallotmaster::PrgpGetEventsBallotsStop(OUT IEventBallot *** pppEventBallotStop) const
{
return (IEventBallot **)m_pServiceBallotmaster->m_pProfileParent->m_arraypEventsRecentBallots.PrgpGetEventsStopLast(OUT (IEvent ***)pppEventBallotStop);
}
CEventBallotReceived *
OJapiAppBallotmaster::PFindBallotByID(TIMESTAMP tsIdBallot) const
{
IEventBallot ** ppEventBallotStop;
IEventBallot ** ppEventBallot = PrgpGetEventsBallotsStop(OUT &ppEventBallotStop);
while (ppEventBallot != ppEventBallotStop)
{
CEventBallotReceived * pEventBallot = (CEventBallotReceived *)*ppEventBallot++;
if (pEventBallot->m_tsEventID == tsIdBallot && pEventBallot->EGetEventClass() == CEventBallotReceived::c_eEventClass)
return pEventBallot;
}
return NULL;
}
CEventBallotPoll *
OJapiAppBallotmaster::PFindPollByID(TIMESTAMP tsIdPoll) const
{
//MessageLog_AppendTextFormatSev(eSeverityNoise, "OJapiAppBallotmaster::PFindPollByID($t)\n", tsIdPoll);
IEvent ** ppEventStop;
IEvent ** ppEvent = m_pServiceBallotmaster->m_oVaultBallots.m_arraypaEvents.PrgpGetEventsStop(OUT &ppEventStop);
while (ppEvent != ppEventStop)
{
CEventBallotPoll * pEvent = (CEventBallotPoll *)*ppEvent++;
Assert(pEvent->EGetEventClass() == CEventBallotPoll::c_eEventClass);
if (pEvent->m_tsEventID == tsIdPoll && (pEvent->m_uFlagsEvent & IEvent::FE_kfEventDeleted) == 0)
{
//MessageLog_AppendTextFormatSev(eSeverityNoise, "OJapiAppBallotmaster::PFindPollByID($t) -> 0x$p $t\n", tsIdPoll, pEvent, pEvent->m_tsEventID);
return pEvent;
}
}
return NULL;
}
CEventBallotPoll *
OJapiAppBallotmaster::PFindPollByID(const QString & sIdPoll) const
{
return PFindPollByID(Timestamp_FromStringW_ML(sIdPoll));
}
CEventBallotPoll *
OJapiAppBallotmaster::PFindPollByTimeStarted(TIMESTAMP tsStarted) const
{
IEvent ** ppEventStop;
IEvent ** ppEvent = m_pServiceBallotmaster->m_oVaultBallots.m_arraypaEvents.PrgpGetEventsStop(OUT &ppEventStop);
while (ppEvent != ppEventStop)
{
CEventBallotPoll * pEvent = (CEventBallotPoll *)*--ppEventStop;
Assert(pEvent->EGetEventClass() == CEventBallotPoll::c_eEventClass);
if (pEvent->m_tsStarted == tsStarted)
return pEvent;
}
MessageLog_AppendTextFormatSev(eSeverityWarningToErrorLog, "OJapiAppBallotmaster::PFindPollByTimeStarted() - Unable to find poll matching tsStarted $t\n", tsStarted);
return NULL;
}
void
OJapiAppBallotmaster::OnEventBallotReceived(CEventBallotReceived * pEventBallotReceived)
{
Assert(pEventBallotReceived != NULL);
MessageLog_AppendTextFormatSev(eSeverityInfoTextBlack, "emit onBallotReceived($t)\n", pEventBallotReceived->m_tsEventID);
emit onBallotReceived(PGetOJapiBallot(pEventBallotReceived));
}
void
OJapiAppBallotmaster::OnEventVoteReceived(const CEventBallotSent * pEventBallotSent)
{
Assert(pEventBallotSent != NULL);
if (pEventBallotSent->PGetProfile() == m_pServiceBallotmaster->m_pProfileParent)
{
// Find the event matching the Event ID
CEventBallotPoll * pPoll = PFindPollByTimeStarted(pEventBallotSent->m_tsEventID);
if (pPoll != NULL)
{
MessageLog_AppendTextFormatSev(eSeverityInfoTextBlack, "emit onVoteReceived($t)\n", pPoll->m_tsEventID);
emit onVoteReceived(PGetOJapiPoll(pPoll));
}
}
}
void
OJapiAppBallotmaster::OnEventPollStopped(CEventBallotPoll * pEventBallotPoll)
{
MessageLog_AppendTextFormatSev(eSeverityInfoTextBlack, "emit onPollStopped($t)\n", pEventBallotPoll->m_tsEventID);
emit onPollStopped(PGetOJapiPoll(pEventBallotPoll));
}
POJapiPoll
OJapiAppBallotmaster::PGetOJapiBallot(CEventBallotReceived * pBallot)
{
if (pBallot != NULL)
return new OJapiBallot(pBallot); // This code causes a memory leak. Until Qt has a reference counter of QObjects, Cambrian must provide a mechanism to minimize the memory leaks.
return NULL;
}
// Return the JavaScript object for a ballot
POJapiPoll
OJapiAppBallotmaster::PGetOJapiPoll(CEventBallotPoll * pEventBallotPoll)
{
if (pEventBallotPoll != NULL)
return new OJapiPoll(pEventBallotPoll); // This code causes a memory leak. Until Qt has a reference counter of QObjects, Cambrian must provide a mechanism to minimize the memory leaks.
return NULL;
}
POJapiPoll
OJapiAppBallotmaster::PCreateNewPollFromTemplate(CEventBallotPoll * pPollTemplate)
{
CEventBallotPoll * pPollNew = m_pServiceBallotmaster->PAllocateBallot(pPollTemplate);
Assert(pPollNew != NULL);
pPollNew->m_uFlagsEvent |= IEvent::FE_kfEventDeleted; // When allocating a new poll, assume it has never been saved
OJapiPoll * poJapiPoll = (OJapiPoll *)PGetOJapiPoll(pPollNew);
Assert(poJapiPoll->status() == "unsaved");
return poJapiPoll;
}
// build(), slot
//
// Create a new poll
//
// Ideally the name new() should be used, however since new() is a C++ operator (reserved keyword), we cannot use it.
// Originally the name build() was chosen, however I think the name create() would be more appropriate.
POJapiPoll
OJapiAppBallotmaster::build()
{
//MessageLog_AppendTextFormatCo(d_coBlue, "OPolls::build()\n");
return PCreateNewPollFromTemplate(NULL);
}
// Create a new poll based on a template
POJapiPoll
OJapiAppBallotmaster::build(QObject * pObjectPollTemplate)
{
OJapiPoll * pPollTemplate = qobject_cast<OJapiPoll *>(pObjectPollTemplate); // Make sure we received an object of proper type
return PCreateNewPollFromTemplate((pPollTemplate != NULL) ? pPollTemplate->m_pBallot : NULL);
}
POJapiPoll
OJapiAppBallotmaster::build(const QString & sIdPollTemplate)
{
return PCreateNewPollFromTemplate(PFindPollByID(sIdPollTemplate));
}
// get(), slot
// Return the poll matching the ID
POJapiPoll
OJapiAppBallotmaster::get(const QString & sIdPoll)
{
CEventBallotPoll * pEventBallotPoll = PFindPollByID(sIdPoll);
if (pEventBallotPoll != NULL)
return PGetOJapiPoll(pEventBallotPoll);
return PGetOJapiBallot(PFindBallotByID(Timestamp_FromStringW_ML(sIdPoll))); // Search for ballots as well
}
// getList(), slot
// Return a list of polls and ballots.
QVariant
OJapiAppBallotmaster::getList()
{
Assert(m_pServiceBallotmaster != NULL);
QVariantList oList;
IEventBallot ** ppEventBallotStop;
IEventBallot ** ppEventBallot = PrgpGetEventsBallotsStop(OUT &ppEventBallotStop);
while (ppEventBallot != ppEventBallotStop)
{
CEventBallotReceived * pEventBallot = (CEventBallotReceived *)*ppEventBallot++;
if (pEventBallot->EGetEventClass() == CEventBallotReceived::c_eEventClass)
oList.append(QVariant::fromValue(PGetOJapiBallot(pEventBallot)));
}
IEvent ** ppEventStop;
IEvent ** ppEvent = m_pServiceBallotmaster->m_oVaultBallots.m_arraypaEvents.PrgpGetEventsStop(OUT &ppEventStop);
while (ppEvent != ppEventStop)
{
CEventBallotPoll * pEvent = (CEventBallotPoll *)*ppEvent++;
if (pEvent->EGetEventClass() != eEventClass_eBallotPoll)
continue;
if ((pEvent->m_uFlagsEvent & IEvent::FE_kfEventDeleted) == 0)
oList.append(QVariant::fromValue(PGetOJapiPoll(pEvent))); // List only non-deleted polls
}
MessageLog_AppendTextFormatCo(d_coBlue, "OPolls::list() - $i elements\n", oList.size());
return QVariant::fromValue(oList);
}
/*
// list(), slot
//
// Return an XML of all the polls available