-
Notifications
You must be signed in to change notification settings - Fork 13
/
CCalendarProtocol.cpp
2241 lines (1905 loc) · 56.3 KB
/
CCalendarProtocol.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
/*
Copyright (c) 2007 Cyrus Daboo. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
CCalendarProtocol.cpp
Author:
Description: <describe the CCalendarProtocol class here>
*/
#include "CCalendarProtocol.h"
#include "CCalDAVCalendarClient.h"
#include "CCalendarClient.h"
#include "CCalendarRecord.h"
#include "CCalendarStoreManager.h"
#include "CCalendarStoreNode.h"
#include "CCalendarStoreXML.h"
#include "CConnectionManager.h"
#include "CLocalCalendarClient.h"
#include "CPasswordManager.h"
#include "CPreferences.h"
#include "CWebDAVCalendarClient.h"
#include "cdfstream.h"
#include "CICalendarSync.h"
#include "CICalendarDateTime.h"
#include "CICalendarDuration.h"
#include "XMLDocument.h"
#include "XMLNode.h"
#include "XMLObject.h"
#include "XMLSAXSimple.h"
#if __dest_os == __win32_os || __dest_os == __linux_os
#include "StValueChanger.h"
#endif
#include <algorithm>
#include <memory>
#ifdef _MSC_VER
// suppress warning: 'this' : used in base member initializer list
// (base member just stashes the pointer for later use, so it's safe)
#pragma warning (disable : 4355)
#endif
using namespace calstore;
CCalendarProtocol::CCalendarProtocol(CINETAccount* acct) :
CINETProtocol(acct),
mStoreRoot(this)
{
mClient = NULL;
mCacheClient = NULL;
mCacheIsPrimary = false;
mSyncingList = false;
mListedFromCache = false;
// Only WebDAV servers can disconnect
switch(GetAccountType())
{
case CINETAccount::eWebDAVCalendar:
SetFlags(eCanDisconnect, IsOfflineAllowed() && GetCalendarAccount()->GetDisconnected());
break;
case CINETAccount::eCalDAVCalendar:
SetFlags(eCanDisconnect, IsOfflineAllowed() && GetCalendarAccount()->GetDisconnected());
break;
case CINETAccount::eHTTPCalendar: // This is always in disconnected mode but will not be present if not allowed
SetFlags(eCanDisconnect, true);
break;
default:
SetFlags(eCanDisconnect, false);
break;
}
// If not login at startup but it can disconnect, set to force disconnect mode
mFlags.Set(eForceDisconnect, !GetCalendarAccount()->GetLogonAtStart());
switch(GetAccountType())
{
case CINETAccount::eLocalCalendar:
mDirDelim = os_dir_delim;
break;
case CINETAccount::eHTTPCalendar:
case CINETAccount::eWebDAVCalendar:
case CINETAccount::eCalDAVCalendar:
mDirDelim = '/';
break;
default:
mDirDelim = 0;
break;
}
mRecorder = NULL;
CreateClient();
}
// Copy constructor
CCalendarProtocol::CCalendarProtocol(const CCalendarProtocol& copy, bool force_local, bool force_remote) :
CINETProtocol(copy),
mStoreRoot(this)
{
// Init instance variables
mClient = NULL;
mCacheClient = NULL;
mCacheIsPrimary = false;
mRecorder = NULL; // Only original proto uses this - clones do not
mDirDelim = copy.mDirDelim;
// Copy client
if (((GetAccountType() == CINETAccount::eCalDAVCalendar) || (GetAccountType() == CINETAccount::eWebDAVCalendar) || (GetAccountType() == CINETAccount::eHTTPCalendar)) &&
(force_local || force_remote))
{
if (force_remote)
{
SetFlags(eIsOffline, false);
SetFlags(eDisconnected, false);
switch(GetAccountType())
{
case CINETAccount::eWebDAVCalendar:
mClient = new CWebDAVCalendarClient(this);
if (CanDisconnect())
{
InitOfflineCWD();
mCacheClient = new CLocalCalendarClient(this);
mCacheIsPrimary = false;
}
break;
case CINETAccount::eCalDAVCalendar:
mClient = new CCalDAVCalendarClient(this);
if (CanDisconnect())
{
InitOfflineCWD();
mCacheClient = new CLocalCalendarClient(this);
mCacheIsPrimary = false;
}
break;
case CINETAccount::eHTTPCalendar:
// Create WebDAV client with private account
mClient = new CWebDAVCalendarClient(this);
{
CCalendarAccount* temp = new CCalendarAccount;
temp->SetServerType(CCalendarAccount::eHTTPCalendar);
temp->SetName(GetAccount()->GetName());
mClient->SetPrivateAccount(temp);
}
InitOfflineCWD();
mCacheClient = new CLocalCalendarClient(this);
mCacheIsPrimary = true;
break;
default:;
}
}
else
{
SetFlags(eIsOffline, true);
SetFlags(eDisconnected, true);
InitDisconnect();
mClient = new CLocalCalendarClient(this);
static_cast<CLocalCalendarClient*>(mClient)->SetRecorder(mRecorder);
}
CINETProtocol::mClient = mClient;
}
else
CopyClient(copy);
}
CCalendarProtocol::~CCalendarProtocol()
{
// Clear nodes first to ensure any active nodes write themselves out before the
// client is deleted
mStoreRoot.Clear();
// Delete client
RemoveClient();
// Delete recorder
delete mRecorder;
mRecorder = NULL;
}
void CCalendarProtocol::SetAccount(CINETAccount* account)
{
// Do inherited
CINETProtocol::SetAccount(account);
// Check disconnected state
switch(GetAccountType())
{
case CINETAccount::eWebDAVCalendar:
SetFlags(eCanDisconnect, IsOfflineAllowed() &&
GetCalendarAccount()->GetDisconnected());
break;
case CINETAccount::eCalDAVCalendar:
SetFlags(eCanDisconnect, IsOfflineAllowed() &&
GetCalendarAccount()->GetDisconnected());
break;
case CINETAccount::eHTTPCalendar:
SetFlags(eCanDisconnect, true);
break;
default:
SetFlags(eCanDisconnect, false);
break;
}
switch(GetAccountType())
{
case CINETAccount::eLocalCalendar:
mDirDelim = os_dir_delim;
break;
case CINETAccount::eHTTPCalendar:
case CINETAccount::eWebDAVCalendar:
case CINETAccount::eCalDAVCalendar:
mDirDelim = '/';
break;
default:
mDirDelim = 0;
break;
}
// Look for change in account name
if (mStoreRoot.GetDisplayName() != GetAccountName())
{
// Need to rename offline
if (mCacheClient != NULL)
{
RenameOffline();
}
// Rename root
mStoreRoot.SetDisplayName(GetAccountName());
}
if (mCacheClient != NULL)
{
// Don't allow throw
try
{
mCacheClient->Reset();
}
catch (...)
{
CLOG_LOGCATCH(...);
}
}
}
void CCalendarProtocol::DirtyAccount()
{
// Must override in derived classes
switch(GetAccountType())
{
case CINETAccount::eLocalCalendar:
CPreferences::sPrefs->mLocalCalendarAccount.SetDirty();
break;
case CINETAccount::eWebDAVCalendar:
case CINETAccount::eCalDAVCalendar:
CPreferences::sPrefs->mCalendarAccounts.SetDirty();
break;
case CINETAccount::eHTTPCalendar:
CPreferences::sPrefs->mWebCalendarAccount.SetDirty();
break;
default:
break;
}
}
void CCalendarProtocol::CreateClient()
{
RemoveClient();
// Check disconnected state
switch(GetAccountType())
{
case CINETAccount::eWebDAVCalendar:
case CINETAccount::eCalDAVCalendar:
case CINETAccount::eHTTPCalendar:
if ((CConnectionManager::sConnectionManager.IsConnected() && !IsForceDisconnect()) || !CanDisconnect())
{
SetFlags(eIsOffline, false);
SetFlags(eDisconnected, false);
switch(GetAccountType())
{
case CINETAccount::eWebDAVCalendar:
mClient = new CWebDAVCalendarClient(this);
if (CanDisconnect())
{
InitOfflineCWD();
mCacheClient = new CLocalCalendarClient(this);
mCacheIsPrimary = false;
}
break;
case CINETAccount::eCalDAVCalendar:
mClient = new CCalDAVCalendarClient(this);
if (CanDisconnect())
{
InitOfflineCWD();
mCacheClient = new CLocalCalendarClient(this);
mCacheIsPrimary = false;
}
break;
case CINETAccount::eHTTPCalendar:
// Create WebDAV client with private account
mClient = new CWebDAVCalendarClient(this);
{
CCalendarAccount* temp = new CCalendarAccount;
temp->SetServerType(CCalendarAccount::eHTTPCalendar);
temp->SetName(GetAccount()->GetName());
mClient->SetPrivateAccount(temp);
}
InitOfflineCWD();
mCacheClient = new CLocalCalendarClient(this);
mCacheIsPrimary = true;
break;
default:;
}
}
else
{
SetFlags(eIsOffline, true);
SetFlags(eDisconnected, true);
InitDisconnect();
mClient = new CLocalCalendarClient(this);
static_cast<CLocalCalendarClient*>(mClient)->SetRecorder(mRecorder);
}
break;
case CINETAccount::eLocalCalendar:
SetFlags(eIsOffline, true);
SetFlags(eDisconnected, false);
InitOffline();
mClient = new CLocalCalendarClient(this);
break;
default:;
}
CINETProtocol::mClient = mClient;
}
void CCalendarProtocol::CopyClient(const CINETProtocol& copy)
{
// NB This is called in the constructor and cannot rely on virtual functions
// being called for this object!
CCalendarClient* copy_it = static_cast<const CCalendarProtocol&>(copy).mClient;
if (dynamic_cast<CWebDAVCalendarClient*>(copy_it))
mClient = new CWebDAVCalendarClient(static_cast<const CWebDAVCalendarClient&>(*copy_it), this);
else if (dynamic_cast<CCalDAVCalendarClient*>(copy_it))
mClient = new CCalDAVCalendarClient(static_cast<const CCalDAVCalendarClient&>(*copy_it), this);
else if (dynamic_cast<CLocalCalendarClient*>(copy_it))
mClient = new CLocalCalendarClient(static_cast<const CLocalCalendarClient&>(*copy_it), this);
else
mClient = NULL;
CINETProtocol::mClient = mClient;
}
// Set into synchronising mode
void CCalendarProtocol::SetSynchronising()
{
// Just remove recorder
if (mRecorder)
{
// Remove from local client
CLocalCalendarClient* client = dynamic_cast<CLocalCalendarClient*>(mClient);
if (client)
client->SetRecorder(NULL);
delete mRecorder;
mRecorder = NULL;
}
// Remove cache client
if (mCacheClient != NULL)
{
delete mCacheClient;
mCacheClient = NULL;
mCacheIsPrimary = false;
}
}
void CCalendarProtocol::RemoveClient()
{
delete mClient;
mClient = NULL;
CINETProtocol::mClient = NULL;
delete mCacheClient;
mCacheClient = NULL;
mCacheIsPrimary = false;
}
// Open connection to protocol server
void CCalendarProtocol::Open()
{
// Only bother if not already open
if (IsOpenAllowed() && IsNotOpen())
{
// Get client to open
SetErrorProcess(false);
mClient->Open();
if (mCacheClient != NULL)
mCacheClient->Open();
mMPState = eINETOpen;
}
} // CCalendarProtocol::Open
// Close connection to protocol server
void CCalendarProtocol::Close()
{
// Only bother if not already closed
if (IsNotOpen())
return;
try
{
// Logoff if required
if (IsLoggedOn())
Logoff();
// Get client to close
mClient->Close();
if (mCacheClient != NULL)
mCacheClient->Close();
mMPState = eINETNotOpen;
SetErrorProcess(false);
// Clean any free connections in cache
CleanConnections();
}
catch (...)
{
CLOG_LOGCATCH(...);
// Clean up and throw up
mMPState = eINETNotOpen;
SetErrorProcess(false);
// Clean any free connections in cache
CleanConnections();
CLOG_LOGRETHROW;
throw;
}
}
// Logon to server
void CCalendarProtocol::Logon()
{
// No need to block since if its not logged in there can be no other network
// operation in progress. If it is logged in it won't issue a network call either.
if (IsOpenAllowed() && !IsLoggedOn())
{
// Recovering after a failure should be on here as it can be turned off
// at logoff
SetNoRecovery(false);
// Get client to logon
mClient->Logon();
if (mCacheClient != NULL)
mCacheClient->Logon();
// Recache user id & password after successful logon
if (GetAccount()->GetAuthenticator().RequiresUserPswd())
{
CAuthenticatorUserPswd* auth = GetAccount()->GetAuthenticatorUserPswd();
// Only bother if it contains something
if (!auth->GetPswd().empty())
{
CCalendarProtocol::SetCachedPswd(auth->GetUID(), auth->GetPswd());
CPasswordManager::GetManager()->AddPassword(GetAccount(), auth->GetPswd());
}
}
// Make copy of current authenticator
SetAuthenticatorUniqueness(GetAccount()->GetAuthenticator().GetUniqueness());
// Add to list of periodic items
CMailControl::RegisterPeriodic(this, true);
mMPState = eINETLoggedOn;
SetErrorProcess(false);
// Broadcast change in state
Broadcast_Message(eBroadcast_Logon, this);
}
}
// Logoff server
void CCalendarProtocol::Logoff()
{
// Must block
cdmutex::lock_cdmutex _lock(_mutex);
if (IsLoggedOn())
{
// Remove from list of periodic items
CMailControl::RegisterPeriodic(this, false);
// Do without errors appearing on screen as the user
// is not really interested in failures during logoff
bool old_error_alert = GetNoErrorAlert();
SetNoErrorAlert(true);
// No point in recovering after a failure
SetNoRecovery(true);
try
{
// Get client to logoff
mClient->Logoff();
}
catch(...)
{
CLOG_LOGCATCH(...);
}
try
{
// Get client to logoff
if (mCacheClient != NULL)
mCacheClient->Logoff();
}
catch(...)
{
CLOG_LOGCATCH(...);
}
SetNoErrorAlert(old_error_alert);
// Set flag
mMPState = eINETLoggedOff;
SetErrorProcess(false);
// Broadcast change in state
Broadcast_Message(eBroadcast_Logoff, this);
// Clean any free connections in cache
CleanConnections();
}
}
// Set the working directory prefix
void CCalendarProtocol::AddWD(const cdstring& wd)
{
// Create new hierarchy
CCalendarStoreNode* node = new CCalendarStoreNode(this, &mStoreRoot, true, false, false, wd);
node->SetFlags(CCalendarStoreNode::eIsDisplayHierarchy);
mStoreRoot.AddChild(node);
GetCalendarAccount()->GetWDs().push_back(CDisplayItem(wd));
// Broadcast addition
Broadcast_Message(eBroadcast_RefreshList, this);
// Make prefs as dirty
CPreferences::sPrefs->mCalendarAccounts.SetDirty();
// Now load it if logged in
if (IsLoggedOn())
{
LoadList();
SyncList();
}
}
// Rename the working directory prefix
void CCalendarProtocol::RenameWD(CCalendarStoreNode& node, const cdstring& new_name)
{
// Change name in list and force refresh
for(CDisplayItemList::iterator iter = GetCalendarAccount()->GetWDs().begin(); iter != GetCalendarAccount()->GetWDs().end(); iter++)
{
if ((*iter).GetName() == node.GetName())
{
(*iter).SetName(new_name);
break;
}
}
node.SetName(new_name);
// Make prefs as dirty
CPreferences::sPrefs->mCalendarAccounts.SetDirty();
// Now load it if logged in
if (IsLoggedOn())
{
LoadList();
SyncList();
}
}
// Set the working directory prefix
void CCalendarProtocol::RemoveWD(CCalendarStoreNode& node)
{
// Change name in list and force refresh
for(CDisplayItemList::iterator iter = GetCalendarAccount()->GetWDs().begin(); iter != GetCalendarAccount()->GetWDs().end(); iter++)
{
if ((*iter).GetName() == node.GetName())
{
GetCalendarAccount()->GetWDs().erase(iter);
break;
}
}
// Make prefs as dirty
CPreferences::sPrefs->mCalendarAccounts.SetDirty();
// Now load it if logged in
if (IsLoggedOn())
{
LoadList();
SyncList();
}
}
void CCalendarProtocol::LoadList()
{
// NB This protocol does not delete the list items when it logs out, so we
// need to do that here before reloading
// Tell listeners we are about to remove all calendar store nodes
Broadcast_Message(eBroadcast_ClearList, this);
// Remove calendar store nodes
mStoreRoot.Clear();
// Check whether connected or not or whether we always read from local cache
if (IsDisconnected() || mCacheIsPrimary)
ReadCalendars();
else
{
// First try the disconnected cache - but only use if current
if (mListedFromCache || ((mCacheClient == NULL) || !ReadCalendars(true)))
{
// Add WDs
for(CDisplayItemList::const_iterator iter = GetCalendarAccount()->GetWDs().begin(); iter != GetCalendarAccount()->GetWDs().end(); iter++)
{
CCalendarStoreNode* node = new CCalendarStoreNode(this, &mStoreRoot, true, false, false, (*iter).GetName());
node->SetFlags(CCalendarStoreNode::eIsDisplayHierarchy);
mStoreRoot.AddChild(node);
}
// Now get new list
mClient->_ListCalendars(&mStoreRoot);
// Always keep disconnected cache list in sync with server
if (mCacheClient != NULL)
{
DumpCalendars();
}
}
else
mListedFromCache = true;
}
// Tell listeners we have added calendar store nodes
Broadcast_Message(eBroadcast_RefreshList, this);
}
// Load calendar list from server
void CCalendarProtocol::LoadSubList(CCalendarStoreNode* node, bool deep)
{
// When disconnected or cache is primary the list is always up to date
if (IsDisconnected() || mCacheIsPrimary)
return;
// Tell listeners we are about to remove all child nodes
Broadcast_Message(eBroadcast_ClearSubList, node);
// Remove calendar store nodes
node->Clear();
// Now get new list
mClient->_ListCalendars(node);
// Always keep disconnected cache list in sync with server
if (mCacheClient != NULL)
{
DumpCalendars();
}
// Tell listeners we have added child nodes
Broadcast_Message(eBroadcast_RefreshSubList, node);
}
void CCalendarProtocol::SyncList()
{
// Get calendar name
cdstring calname = GetAccountName();
calname += cMailAccountSeparator;
// Find subscribed calendars for this account
cdstrvect subs;
for(cdstrset::const_iterator iter = CPreferences::sPrefs->mSubscribedCalendars.GetValue().begin(); iter != CPreferences::sPrefs->mSubscribedCalendars.GetValue().end(); iter++)
{
if ((*iter).compare_start(calname))
{
CCalendarStoreNode* node = SyncCalendarNode(*iter);
// Sync this node if not a directory
if ((node != NULL) && !node->IsDirectory())
{
// Subcribe but without changing prefs
CCalendarStoreManager::sCalendarStoreManager->SubscribeNode(node, true, true);
Broadcast_Message(eBroadcast_RefreshNode, node);
}
}
}
}
CCalendarStoreNode* CCalendarProtocol::SyncCalendarNode(const cdstring& node)
{
// Break the name down into components
cdstring acct;
cdstring path;
const char* start = node.c_str();
const char* end = ::strchr(start, cMailAccountSeparator);
if (end != NULL)
{
acct = cdstring(start, end - start);
path = cdstring(end + 1);
}
else
return NULL;
// Now test account name
if (acct != GetAccountName())
return NULL;
return mStoreRoot.FindNodeOrCreate(path);
}
void CCalendarProtocol::RefreshList()
{
// Reload and sync
LoadList();
SyncList();
}
void CCalendarProtocol::RefreshSubList(CCalendarStoreNode* node)
{
// Reload and sync
LoadSubList(node, false);
SyncList();
}
// List was changed in some way
void CCalendarProtocol::ListChanged()
{
// Always keep disconnected cache in sync with server
if (mCacheClient != NULL)
{
DumpCalendars();
}
}
CCalendarStoreNode* CCalendarProtocol::GetNode(const cdstring& cal, bool parent) const
{
// Break the name down into components
cdstring acct;
cdstring path;
const char* start = cal.c_str();
const char* end = ::strchr(start, cMailAccountSeparator);
if (end != NULL)
{
acct = cdstring(start, end - start);
path = cdstring(end + 1);
}
else
return NULL;
// Now test account name
if (acct != GetAccountName())
return NULL;
if (parent)
{
cdstrvect segments;
path.split(cdstring(GetDirDelim()), segments);
segments.pop_back();
path.join(segments, cdstring(GetDirDelim()));
}
return ((path.empty() || path == mStoreRoot.GetName()) ? const_cast<CCalendarStoreNode*>(&mStoreRoot) : mStoreRoot.FindNode(path));
}
CCalendarStoreNode* CCalendarProtocol::GetNodeByRemoteURL(const cdstring& url) const
{
// Scan down entire hierarchy
return mStoreRoot.FindNodeByRemoteURL(url);
}
CCalendarStoreNode* CCalendarProtocol::GetParentNode(const cdstring& cal) const
{
return GetNode(cal, true);
}
bool CCalendarProtocol::HasDisconnectedCalendars()
{
if (CanDisconnect() || mCacheIsPrimary)
{
if (!mStoreRoot.HasInferiors())
ReadCalendars();
return mStoreRoot.HasInferiors();
}
else
return false;
}
void CCalendarProtocol::CreateCalendar(const CCalendarStoreNode& node)
{
// Don't do on server if cache is primary
if (!mCacheIsPrimary)
mClient->_CreateCalendar(node);
// Always keep disconnected cache in sync with server
if (mCacheClient != NULL)
{
mCacheClient->_CreateCalendar(node);
DumpCalendars();
}
// Always sync the cache state
if (IsDisconnected())
const_cast<CCalendarStoreNode&>(node).TestDisconnectCache();
}
void CCalendarProtocol::TouchCalendar(const CCalendarStoreNode& node)
{
// Don't do on server if cache is primary
if (!mCacheIsPrimary)
mClient->_TouchCalendar(node);
// Always keep disconnected cache in sync with server
if (mCacheClient != NULL)
{
if (mCacheClient->_TouchCalendar(node))
DumpCalendars();
}
// Always sync the cache state
if (IsDisconnected())
const_cast<CCalendarStoreNode&>(node).TestDisconnectCache();
}
bool CCalendarProtocol::TestCalendar(const CCalendarStoreNode& node)
{
return (mCacheIsPrimary && (mCacheClient != NULL)) ? mCacheClient->_TestCalendar(node) : mClient->_TestCalendar(node);
}
void CCalendarProtocol::DeleteCalendar(const CCalendarStoreNode& node)
{
// Don't do on server if cache is primary
if (!mCacheIsPrimary)
mClient->_DeleteCalendar(node);
// Always keep disconnected cache in sync with server
if (mCacheClient != NULL)
{
if (mCacheClient->_TestCalendar(node))
mCacheClient->_DeleteCalendar(node);
}
}
void CCalendarProtocol::RenameCalendar(const CCalendarStoreNode& node, const cdstring& node_new)
{
// Rename it on the server
// Don't do on server if cache is primary
if (!mCacheIsPrimary)
mClient->_RenameCalendar(node, node_new);
// Always keep disconnected cache in sync with server
if (mCacheClient != NULL)
{
if (mCacheClient->_TestCalendar(node))
mCacheClient->_RenameCalendar(node, node_new);
}
}
bool CCalendarProtocol::CheckCalendar(const CCalendarStoreNode& node, iCal::CICalendar& cal)
{
bool result = false;
// See if offline or disconnected
if (IsOffline() || IsDisconnected())
{
// Nothing to do here. We will assume that local files are always
// up to date.
}
else if (IsWebCalendar())
{
SubscribeFullCalendar(node, cal);
}
else
{
// Look for local cache first
if ((mCacheClient != NULL) && !mCacheIsPrimary)
{
// Read in the calendar cache if it exists
if (mCacheClient->_TestCalendar(node))
mCacheClient->_ReadFullCalendar(node, cal);
// Sync cache with server doing playback if needed
result = SyncFromServer(node, cal);
}
else
{
// Just read full calendar from server
result = mClient->_CalendarChanged(node, cal);
if (result)
ReadFullCalendar(node, cal);
}
}
return result;
}
void CCalendarProtocol::SizeCalendar(CCalendarStoreNode& node)
{
// Don't do on server if cache is primary
if (mCacheIsPrimary)
mCacheClient->_SizeCalendar(node);
else
mClient->_SizeCalendar(node);
}
void CCalendarProtocol::OpenCalendar(const CCalendarStoreNode& node, iCal::CICalendar& cal)
{
// See if offline or disconnected
if (IsOffline() || IsDisconnected())
{
// Just read full calendar from offline/disconnect store
ReadFullCalendar(node, cal);
}
else
{
// Look for local cache first
if ((mCacheClient != NULL) && !mCacheIsPrimary)
{
// Read in the calendar cache if it exists
if (mCacheClient->_TestCalendar(node))
mCacheClient->_ReadFullCalendar(node, cal);
// Sync cache with server doing playback if needed
SyncFromServer(node, cal);
}
else
{
// Just read full calendar from server
ReadFullCalendar(node, cal);
}
}
// Make sure the calendar has some kind of name
if (cal.GetName().empty())
{
cal.SetName(node.GetShortName());
}
}
bool CCalendarProtocol::SyncFromServer(const CCalendarStoreNode& node, iCal::CICalendar& cal)
{
// Different based on full or component sync
bool result = false;
if (IsComponentCalendar())
result = SyncComponentsFromServer(node, cal);
else
result = SyncFullFromServer(node, cal);
Broadcast_Message(eBroadcast_RefreshNode, (void*)&node);
return result;
}
bool CCalendarProtocol::SyncFullFromServer(const CCalendarStoreNode& node, iCal::CICalendar& cal)
{
// We need to do this as a proper transaction with locking