Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added wxLocale lines and altered the code for checking the version required for Xesti...
[xestiaab/.git] / source / frmActivityMgr.cpp
1 #include "frmActivityMgr.h"
2 #include <wx/mstream.h>
3 #include <wx/file.h>
4 #include <wx/ffile.h>
5 #include <wx/tokenzr.h>
6 #include <wx/dir.h>
7 #include <wx/filefn.h>
8 #include <thread>
9 #include <curl/curl.h>
11 #include "bitmaps.h"
12 #include "enums.h"
13 #include "common/preferences.h"
14 #include "frmConflictResolution.h"
15 #include "frmInvalidSSLCertificate.h"
16 #include "frmMain.h"
17 #include "common/events.h"
18 #include "common/filename.h"
19 #include "common/dirs.h"
20 #include "vcard/vcard34conv.h"
21 #include "vcard/vcard.h"
24 #ifndef FRMACTIVITYMGR_H
25 #define FRMACTIVITYMGR_H
27 DEFINE_EVENT_TYPE(ACTMGR_RESUMEPROC);
28 DEFINE_EVENT_TYPE(ACTMGR_TOGGLECONN);
29 DEFINE_EVENT_TYPE(ACTMGR_STARTTIMER);
30 DEFINE_EVENT_TYPE(ACTMGR_STOPTIMER);
31 DEFINE_EVENT_TYPE(ACTMGR_STARTPROCESSTIMER);
32 DEFINE_EVENT_TYPE(ACTMGR_STOPPROCESSTIMER);
34 BEGIN_EVENT_TABLE(frmActivityMgr, wxWindow)
35   EVT_COMMAND(wxID_ANY, ACTMGR_RESUMEPROC, frmActivityMgr::ResumeProcessing)
36   EVT_COMMAND(wxID_ANY, ACTMGR_TOGGLECONN, frmActivityMgr::ToggleConnectionStatus)
37   EVT_COMMAND(wxID_ANY, ACTMGR_STARTTIMER, frmActivityMgr::StartTimer)
38   EVT_COMMAND(wxID_ANY, ACTMGR_STOPTIMER, frmActivityMgr::StopTimer)
39   EVT_COMMAND(wxID_ANY, ACTMGR_STARTPROCESSTIMER, frmActivityMgr::StartProcessTimer)
40   EVT_COMMAND(wxID_ANY, ACTMGR_STOPPROCESSTIMER, frmActivityMgr::StopProcessTimer)
41 END_EVENT_TABLE()
43 frmActivityMgr::frmActivityMgr( wxWindow* parent )
44 :
45 frmActivityMgrADT( parent )
46 {
48         wxMemoryInputStream istream(icons_activitywindow_png, sizeof(icons_activitywindow_png));
49         wxImage icons_activitywindow_pngi(istream, wxBITMAP_TYPE_PNG);
50         wxBitmap activityiconbmp(icons_activitywindow_pngi, -1);
51         wxIcon activityicon;
52         activityicon.CopyFromBitmap(activityiconbmp);
53         this->SetIcon(activityicon); 
55         wxMemoryInputStream istream2(misc_activitysleep_png, sizeof(misc_activitysleep_png));
56         wxImage misc_activitysleep_pngi(istream2, wxBITMAP_TYPE_PNG);
57         wxBitmap sleepiconbmp(misc_activitysleep_pngi, -1);
58         SleepBitmap = sleepiconbmp;
59         bmpActivity->SetBitmap(SleepBitmap);
60         ActProcessTimer.SetupPointers(this);
61         lblDetails->SetLabel(_("No activity."));
62         
63         ActListProcTimer.SetupPointers(&ActivityList, &ActivityListIndex, &ActivityListTaskDetail, 
64                                         &TasksActive, lstActivity, lblDetails, GauProgress, this, &AnimationTimer,
65                                         &ActProcessTimer, &ActivityListFinDate);
66         
67         AnimationTimer.SetBitmap(bmpActivity);
68         AnimationTimer.SetSleepBitmap(&SleepBitmap);
69         AnimationTimer.SetupPointer(this->GetParent());
70         
71         wxListItem ColumnData;
72         ColumnData.SetId(0);
73         ColumnData.SetText(_("Activity"));
74         ColumnData.SetWidth(175);
75         lstActivity->InsertColumn(0, ColumnData);
76         
77         wxListItem ColumnData2;
78         ColumnData2.SetId(1);
79         ColumnData2.SetText(_("Account"));
80         ColumnData2.SetWidth(100);
81         lstActivity->InsertColumn(1, ColumnData2);
83         wxListItem ColumnData3;
84         ColumnData3.SetId(2);
85         ColumnData3.SetText(_("Status"));
86         ColumnData3.SetWidth(100);
87         lstActivity->InsertColumn(2, ColumnData3);
88         
89         //AnimationTimer.StartAnimation();
90         
91         // Create recovery database if it doesn't exist.
92         
93         wxString RecoveryFilename = GetRecoveryDB();
94         
95         wxFile RecoveryDB;
96         
97         if (wxFile::Exists(RecoveryFilename) == FALSE){
98         
99                 // Create the recovery database.
101                 RecoveryDB.Open(RecoveryFilename.c_str(), wxFile::write);       
102         
103                 RecoveryDB.Write(wxT("BEGIN:XAB-RECOVERYDB\r\n"), wxConvUTF8);
104                 RecoveryDB.Write(wxT("VERSION:1.0\r\n"), wxConvUTF8);
105                 RecoveryDB.Write(wxT("COMMENT:Please do not alter or delete this file. DATA LOSS IS POSSIBLE!\r\n"), wxConvUTF8);
106                 RecoveryDB.Write(wxT("END:XAB-RECOVERYDB\r\n"), wxConvUTF8);
107                 RecoveryDB.Close();
109         }
110         
111         wxFFile RecoveryDBF;
112         
113 #if wxABI_VERSION < 20900
114         RecoveryDBF.Open(RecoveryFilename.c_str(), wxT("r"));
115 #else
116         RecoveryDBF.Open(RecoveryFilename, wxT("r"));
117 #endif  
118         
119         // Load the recovery database for tasks not done.
120         
121         if (RecoveryDBF.IsOpened() == TRUE){
122         
123                 wxString wxSRecoveryDB;
124                 wxString RecoveryLine;
125         
126                 // Check if we are using wxWidgets version 2.8 or less and
127                 // execute the required command accordingly.
128         
129                 RecoveryDBF.ReadAll(&wxSRecoveryDB, wxConvAuto());
130         
131                 // Split the lines.
132         
133                 std::map<int, wxString> RecoveryFileLines;
134                 std::map<int, wxString>::iterator striter;
135         
136                 wxStringTokenizer wSTRecoveryDBLines(wxSRecoveryDB, wxT("\r\n"));
138                 int RecoveryLineSeek = 0;
140                 while (wSTRecoveryDBLines.HasMoreTokens() == TRUE){
142                         RecoveryLine = wSTRecoveryDBLines.GetNextToken();
143                         RecoveryFileLines.insert(std::make_pair(RecoveryLineSeek, RecoveryLine));
144                         RecoveryLineSeek++;             
145         
146                 }
147         
148         }
149         
152         //TestTimer.Start(50, FALSE);
155 int frmActivityMgr::AddTask(int TaskType, wxString TaskDetail, wxString TaskAccount, 
156         wxString TaskURL, wxString TaskFilename, wxString TaskFullFilename, 
157         wxString TaskData){
159         ActivityTaskID++;
161         wxListItem coldata;
162         long itemindex = -1;
163         CardDAV CardDAVConn;
165         coldata.SetId(ActivityTaskID);
166         coldata.SetData(ActivityTaskID);
168         AnimationTimer.StartAnimation();
170         // Get the preferences file.
171         
172         wxString PrefFilename = GetUserPrefDir();
173         
174         XABPreferences PrefData(PrefFilename);
176         wxString AccountName;
177         wxString AccountDir;
178         wxString AccountAddress;
179         int AccountPort = 0;
180         wxString AccountUsername;
181         wxString AccountPassword;
182         bool AccountSSL = FALSE;
183         wxString AccountPrefix;
185         // Look for the account and get the friendly name for the account.
186         
187         for (int i = 0; i < PrefData.accounts.GetCount(); i++){
188         
189                 AccountDir = PrefData.accounts.GetAccountDirectory(i);
190                 
191                 AccountDir.Trim();
192                 AccountDir.Append(wxT(".carddav"));
193         
194                 if (AccountDir == TaskAccount){
195                 
196                         AccountName = PrefData.accounts.GetAccountName(i);
197                         AccountAddress = PrefData.accounts.GetAccountAddress(i);
198                         AccountPort = PrefData.accounts.GetAccountPort(i);
199                         AccountUsername = PrefData.accounts.GetAccountUsername(i);
200                         AccountPassword = PrefData.accounts.GetAccountPassword(i);
201                         AccountPrefix = PrefData.accounts.GetAccountDirPrefix(i);
202                         AccountSSL = PrefData.accounts.GetAccountSSL(i);
203                         
204                         AccountName.Trim();
205                         AccountAddress.Trim();
206                         //AccountPort.Trim();
207                         AccountUsername.Trim();
208                         AccountPassword.Trim();
209                         AccountPrefix.Trim();
210                         break;
211                 
212                 }
213         
214         }
215         
216         CardDAVConn.SetupConnection(AccountAddress, AccountPort, AccountUsername, AccountPassword, AccountSSL, AccountPrefix, AccountDir);
217         CardDAVConn.SetupVariables(&ActivityList, ActivityTaskID);
218         CardDAVConn.SetupResultBools(&BlankBool, &BlankBool);           
220         // TaskType:
221         // 0 - Add a contact.
222         // 1 - Update a contact.
223         // 2 - Delete a contact.
224         // 3 - Account syncronisation.
226         if (TaskType == 0){             
228                 coldata.SetText(_("Adding Contact '") + TaskDetail + _("'"));
229                 itemindex = lstActivity->InsertItem(coldata);
230                 
231                 lstActivity->SetItem(itemindex, 1, AccountName);
232                 lstActivity->SetItem(itemindex, 2, _("Queued"));
233                 
234                 CardDAVConn.SetUploadMode(TRUE);
235                 CardDAVConn.SetupData(wxT("PUT"), TaskURL, TaskData);
236                 
237                 // Setup all of the task details.
239                 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
240                 
241                 ActivityListType.insert(std::make_pair(ActivityTaskID, 0));
242                 ActivityListData.insert(std::make_pair(ActivityTaskID, TaskData));
243                 ActivityListConn.insert(std::make_pair(ActivityTaskID, CardDAVConn));
244                 ActivityListIndex.insert(std::make_pair(ActivityTaskID, itemindex));
245                 ActivityListTaskDetail.insert(std::make_pair(ActivityTaskID, TaskDetail));
246                 ActivityListAccount.insert(std::make_pair(ActivityTaskID, TaskAccount));
247                 ActivityListFilename.insert(std::make_pair(ActivityTaskID, TaskFilename));
248                 ActivityListFullFilename.insert(std::make_pair(ActivityTaskID, TaskFullFilename));
249                 
250                 ActivityListETag.insert(std::make_pair(ActivityTaskID, ETagDBPtr->GetETag(TaskFilename)));
251                 ActivityListETagOriginal.insert(std::make_pair(ActivityTaskID, ETagDBPtr->GetETagOriginal(TaskFilename)));
252                 ActivityList.insert(std::make_pair(ActivityTaskID, 0));
253         
254         } else if (TaskType == 1){
255         
256                 coldata.SetText(_("Updating Contact '") + TaskDetail + _("'"));
257                 itemindex = lstActivity->InsertItem(coldata);           
258                 
259                 lstActivity->SetItem(itemindex, 1, AccountName);
260                 lstActivity->SetItem(itemindex, 2, _("Queued"));
261                 
262                 CardDAVConn.SetUploadMode(TRUE);
263                 CardDAVConn.SetupData(wxT("PUT"), TaskURL, TaskData);
264                 CardDAVConn.SetEditMode(TRUE);
265                 
266                 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
267                 
268                 ActivityListType.insert(std::make_pair(ActivityTaskID, 1));
269                 ActivityListData.insert(std::make_pair(ActivityTaskID, TaskData));
270                 ActivityListConn.insert(std::make_pair(ActivityTaskID, CardDAVConn));
271                 ActivityListIndex.insert(std::make_pair(ActivityTaskID, itemindex));
272                 ActivityListTaskDetail.insert(std::make_pair(ActivityTaskID, TaskDetail));
273                 ActivityListAccount.insert(std::make_pair(ActivityTaskID, TaskAccount));
274                 ActivityListFilename.insert(std::make_pair(ActivityTaskID, TaskFilename));
275                 ActivityListFullFilename.insert(std::make_pair(ActivityTaskID, TaskFullFilename));
276                 
277                 ActivityListETag.insert(std::make_pair(ActivityTaskID, ETagDBPtr->GetETag(TaskFilename)));
278                 ActivityListETagOriginal.insert(std::make_pair(ActivityTaskID, ETagDBPtr->GetETagOriginal(TaskFilename)));
279                 ActivityList.insert(std::make_pair(ActivityTaskID, 0));
280         
281         } else if (TaskType == 2){
283                 coldata.SetText(_("Deleting Contact '") + TaskDetail + _("'"));
284                 itemindex = lstActivity->InsertItem(coldata);
285                 
286                 lstActivity->SetItem(itemindex, 1, AccountName);
287                 lstActivity->SetItem(itemindex, 2, _("Queued"));
288                 
289                 // TODO: Pass the ETagDB for the account to the connection.
290                 
291                 ActivityList.insert(std::make_pair(ActivityTaskID, 0));
292                 ActivityListType.insert(std::make_pair(ActivityTaskID, 2));
293                 ActivityListData.insert(std::make_pair(ActivityTaskID, TaskData));
294                 ActivityListAccount.insert(std::make_pair(ActivityTaskID, TaskAccount));
295                 ActivityListConn.insert(std::make_pair(ActivityTaskID, CardDAVConn));
296                 ActivityListFilename.insert(std::make_pair(ActivityTaskID, TaskFilename));
297                 ActivityListIndex.insert(std::make_pair(ActivityTaskID, itemindex));
298         
299         } else if (TaskType == 3){
301                 coldata.SetText(_("Syncronising All Contacts"));
302                 itemindex = lstActivity->InsertItem(coldata);
303                 
304                 lstActivity->SetItem(itemindex, 1, AccountName);
305                 lstActivity->SetItem(itemindex, 2, _("Queued"));
306                 
307                 ActivityList.insert(std::make_pair(ActivityTaskID, 0));
308                 ActivityListType.insert(std::make_pair(ActivityTaskID, 3));
309                 ActivityListData.insert(std::make_pair(ActivityTaskID, TaskData));
310                 ActivityListConn.insert(std::make_pair(ActivityTaskID, CardDAVConn));
311                 ActivityListTaskDetail.insert(std::make_pair(ActivityTaskID, AccountPrefix));
312                 ActivityListAccount.insert(std::make_pair(ActivityTaskID, TaskAccount));
313                 ActivityListIndex.insert(std::make_pair(ActivityTaskID, itemindex));
314         
315         }
316         
317         ProcessTasksData = TRUE;
318         
319         if (!ActProcessTimer.IsRunning()) {
320         
321                 wxCommandEvent event(ACTMGR_STARTTIMER);
322                 wxPostEvent(this, event);
324                 wxCommandEvent eventp(ACTMGR_STARTPROCESSTIMER);
325                 wxPostEvent(this, eventp);
327                 //ActProcessTimer.Start(1000, FALSE);
328                 //ActListProcTimer.Start(1000, FALSE);
329         
330         }
331         
332         // Update the recovery database.
333         
334         return ActivityTaskID;
338 void frmActivityMgr::RemoveTask(){
340         // Remove task from the recovery database.
344 void frmActivityMgr::StopTask( wxCommandEvent& event ){
346         // Check if activity has been completed already before stopping.
348         long longSelected = -1;
349         int intSelectedData = 0;
350                 
351         longSelected = lstActivity->GetNextItem(longSelected, 
352                 wxLIST_NEXT_ALL,
353                 wxLIST_STATE_SELECTED);
355         intSelectedData = (int)lstActivity->GetItemData(longSelected);
356         
357         if (longSelected == -1){
358                 return;
359         }
360         
361         std::map<int,CardDAV>::iterator CDAVter;
362         
363         CDAVter = ActivityListConn.find(intSelectedData);
364         
365         CDAVter->second.Abort();
366         
367         lstActivity->SetItem(longSelected, 2, _("Stopped"));
371 void frmActivityMgr::StopAllTasks( wxCommandEvent& event ){
373         // Check if activity has been completed already before stopping.
374         
375         // TODO: Look through for '1' and stop the running task(s).
376         
377         long longSelected = -1;
378         int intSelectedData = 0;
379                 
380         longSelected = lstActivity->GetNextItem(longSelected, 
381                 wxLIST_NEXT_ALL,
382                 wxLIST_STATE_SELECTED);
384         intSelectedData = (int)lstActivity->GetItemData(longSelected);
385         
386         if (longSelected == -1){
387                 return;
388         }
389         
390         std::map<int,CardDAV>::iterator CDAVter;
391         
392         CDAVter = ActivityListConn.find(intSelectedData);
393         
394         CDAVter->second.Abort();
395         
396         lstActivity->SetItem(longSelected, 2, _("Stopped"));
397         AnimationTimer.StopAnimation();
398         bmpActivity->SetBitmap(SleepBitmap);
399         lblDetails->SetLabel(_("No activity."));
403 void frmActivityMgr::RetryTask( wxCommandEvent& event ){
405         // Check if activity has been completed already before retrying.
406         
407         long longSelected = -1;
408         int intSelectedData = 0;
409                 
410         longSelected = lstActivity->GetNextItem(longSelected, 
411                 wxLIST_NEXT_ALL,
412                 wxLIST_STATE_SELECTED);
414         intSelectedData = (int)lstActivity->GetItemData(longSelected);
415         
416         if (longSelected == -1){
417                 return;
418         }
419         
420         std::map<int,CardDAV>::iterator CDAVter;
421         
422         CDAVter = ActivityListConn.find(intSelectedData);
423         
424         // Restart the task.
425         
426         //CDAVter->second.Abort();
427         
428         std::map<int,int>::iterator Iter;
429         Iter = ActivityList.find(intSelectedData);
430         
431         Iter->second = 0;
432         
433         AnimationTimer.StartAnimation();
434         
435         if (!ActProcessTimer.IsRunning()) {
436         
437                 ActProcessTimer.Start(150, FALSE);
438                 ActListProcTimer.Start(150, FALSE);
439         
440         }
441         
442         //lstActivity->SetItem(longSelected, 2, _("Processing..."));
446 void frmActivityMgr::OpenWindow()
448         WindowOpen = TRUE;
451 void frmActivityMgr::CloseWindow( wxCommandEvent& event )
453         WindowOpen = FALSE;
454         this->Hide();
457 void frmActivityMgr::ProcessTasksThread()
459         
460         if (ProcessTasksData == FALSE){
461         
462                 return;
463         
464         }
465         
466         std::map<int,int>::iterator TypeIter;
467         std::map<int,wxString>::iterator StringIter;
468         std::map<int,wxString>::iterator StringDetailIter;
469         std::map<int,wxString>::iterator StringAccountIter;
470         std::map<int,wxString>::iterator StringFilenameIter;
471         std::map<int,wxString>::iterator StringFullFilenameIter;
472         std::map<int,wxString>::iterator StringTaskDetailIter;
473         std::map<int,wxString>::iterator StringETagIter;
474         std::map<int,wxString>::iterator StringETagOrigIter;
475         std::map<int,CardDAV>::iterator CardDAVIter;
476         std::map<int,long>::iterator LongIter;
477         bool TasksFoundProc = FALSE;
478         wxString TaskDetail;
480         for (std::map<int,int>::iterator iter = ActivityList.begin(); 
481          iter != ActivityList.end(); ++iter){
482          
483                 // Check for any queued tasks and start one.
484                 
485                 if (iter->second > 4){
486                         continue;
487                 }
488                 
489                 CardDAVIter = ActivityListConn.find(iter->first);
490                 TypeIter = ActivityListType.find(iter->first);
491                 StringETagIter = ActivityListETag.find(iter->first);
492                 StringETagOrigIter = ActivityListETagOriginal.find(iter->first);
493                 StringAccountIter = ActivityListAccount.find(iter->first);
494                 StringFilenameIter = ActivityListFilename.find(iter->first);
495                 StringFullFilenameIter = ActivityListFullFilename.find(iter->first);
496                 
497                 // 0 = Queued.
498                 // 1 = Processing.
499                 // 2 = Failed.
500                 // 3 = Stopped.
501                 // 4 = Completed.
502                 // 5 = Failed (UI Updated).
503                 // 6 = Stopped (UI Updated).
504                 // 7 = (Reserved) (UI Updated).
505                 // 8 = Completed. (UI Updated). 
506                 
507                 if (iter->second == 0){
508                 
509                         //CardDAVIter->second.
510                         
511                         // Wait for process to finish.
513                         TasksActive = TRUE;
514                         TasksFoundProc = TRUE;
515                         wxString ETag;
516                         wxString ETagOriginal;
517                         wxString ETagServer;
518                         wxString AccountDir;
519                         wxString ContactFilename;
520                         wxString FullFilename;
521                         wxString ETagResult;
523                         iter->second = 1;
524                         
525                         if (TypeIter->second == 1){
526                                 CardDAVIter->second.GetServerETagValueThread();
527                                 ETagServer = CardDAVIter->second.ETagValueResult();
528                         }
529                         
530                         // Compare the ETags and work out if a conflict has occured.
531                         
532                         if (TypeIter->second == 1){
534                                 ETag = StringETagIter->second;
535                                 ETagOriginal = StringETagOrigIter->second;
536                                 AccountDir = StringAccountIter->second;
537                                 ContactFilename = StringFilenameIter->second;
538                                 FullFilename = StringFullFilenameIter->second;
539                                 
540                                 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
541                         
542                                 if (ETag == ETagOriginal){
543                         
544                                         // Compare the ETag with the Server ETag.
545                                 
546                                         if (ETagOriginal != ETagServer){
547                                 
548                                                 // Server ETag is different from original ETag.
549                                                 // This is a conflict which the user will now
550                                                 // need to resolve.
551                                         
552                                                 vCard Moo1;
553                                                 vCard Moo2;
554                                         
555                                                 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
556                                         
557                                                 vCard34Conv vCard34Obj;
558                                                 
559                                                 wxString ServerContactData;
560                                                 vCard ClientData;
561                                                 vCard ConvertedV4Data;
562                                         
563                                                 // Load the client data from the filename given.
564                                         
565                                                 CardDAVIter->second.GetServerContactData();
566                                                 ServerContactData = CardDAVIter->second.GetPageData();
567                                                 
568                                                 // Process v3 version into v4 version.
569                                                 
570                                                 vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data);
571                                         
572                                                 vCardConflictObj vCardProcData;
573                                         
574                                                 vCardProcData.vCardLocalData = &ClientData;
575                                                 vCardProcData.vCardServerData = &ConvertedV4Data;
576                                                 ClientData.LoadFile(FullFilename);
578                                                 //ContactConflictEvent event(ContactConflictCmdEnv);
579                                                 //event.SetData(ContactFilename, Moo1, Moo2);
580                                         
581                                                 //wxPostEvent(frmMainPtrGet, event);
582                                         
583                                                 // Setup Conflict Resolution Dialog.
584                                         
585                                                 // Fetch Data from Server and convert.
586                                         
587                                                 bool *PauseMode = new bool;
588                                                 int ConflictResult;
589                                                 QRNotif qrn;
591                                                 *PauseMode = TRUE;                                              
592                                                 qrn.QResponse = &ConflictResult;
593                                                 qrn.PausePtr = PauseMode;
594                                                 
595                                                 vCardProcData.QRNotifData = &qrn;
596                                         
597                                                 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
598                                                 event.SetClientData(&vCardProcData);
599                                                 wxPostEvent(frmMainPtrGet, event);
600                                                 
601                                                 /*timespec n1, n2;
602                                                 
603                                                 n1.tv_sec = 0;
604                                                 n1.tv_nsec = 250000000L;*/
605                                                 
606                                                 while (*PauseMode == TRUE){
607                                                         //nanosleep(&n1, &n2);
608                                                         SleepFor(250000000);
609                                                 }
610                                         
611                                                 delete PauseMode;
612                                                 PauseMode = NULL;
613                                         
614                                                 //intResult = frmMainPtrGet->ShowConflictDialog(&Moo1, &Moo2);
615                                         
616                                                 if (ConflictResult == 0){
617                                         
618                                                         // Decide Later.
619                                         
620                                                         iter->second = 4;
621                                                         continue;
622                                         
623                                                 } else if (ConflictResult == 1){
624                                         
625                                                         // Decided to use client version.
626                                         
627                                                         CardDAVIter->second.ProcessData();
628                                                 
629                                                         iter->second = 4;
630                                         
631                                                 } else if (ConflictResult == 2){
632                                         
633                                                         // Decided to use server version.
634                                                 
635                                                         // Download server version.
636                                                 
637                                                         ConvertedV4Data.WriteFile(FullFilename);
638                                                 
639                                                 
640                                                         iter->second = 4;
641                                         
642                                                 }
643                                 
644                                         } else {
645                                 
646                                                 // Update the data to the server.
647                                         
648                                                 CardDAVIter->second.ProcessData();
649                                 
650                                         }
651                         
652                                 } else {
653                         
654                                         if (ETagOriginal != ETagServer){
655                                 
656                                                 // Server ETag is different from original ETag.
657                                                 // This is a conflict which the user will now
658                                                 // need to resolve.
659                                         
660                                                 frmMain *frmMainPtrGet = (frmMain *)frmMainPtr;
661                                         
662                                                 //ContactConflictEvent event(ContactConflictCmdEnv);
663                                                 //event.SetData(ContactFilename, Moo1, Moo2);
664                                         
665                                                 vCard34Conv vCard34Obj;
666                                                 
667                                                 wxString ServerContactData;
668                                                 vCard ClientData;
669                                                 vCard ConvertedV4Data;
670                                         
671                                                 // Load the client data from the filename given.
672                                         
673                                                 CardDAVIter->second.GetServerContactData();
674                                                 ServerContactData = CardDAVIter->second.GetPageData();
675                                                 ClientData.LoadFile(FullFilename);
676                                                 
677                                                 // Process v3 version into v4 version.
678                                                 
679                                                 vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data);
680                                         
681                                                 vCardConflictObj vCardProcData;
682                                         
683                                                 vCardProcData.vCardLocalData = &ClientData;
684                                                 vCardProcData.vCardServerData = &ConvertedV4Data;
685                                         
686                                                 bool *PauseMode = new bool;
687                                                 int ConflictResult;
688                                                 QRNotif qrn;
690                                                 *PauseMode = TRUE;                                              
691                                                 qrn.QResponse = &ConflictResult;
692                                                 qrn.PausePtr = PauseMode;
693                                                 
694                                                 vCardProcData.QRNotifData = &qrn;
695                                         
696                                                 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
697                                                 event.SetClientData(&vCardProcData);
698                                                 wxPostEvent(frmMainPtrGet, event);
699                                         
700                                                 /*timespec n1, n2;
701                                                 
702                                                 n1.tv_sec = 0;
703                                                 n1.tv_nsec = 250000000L;*/
704                                         
705                                                 while (*PauseMode == TRUE){
706                                                         //nanosleep(&n1, &n2);
707                                                         SleepFor(250000000);
708                                                 }
709                                         
710                                                 delete PauseMode;
711                                                 PauseMode = NULL;
712                                 
713                                                 //intResult = frmMainPtrGet->ShowConflictDialog(&Moo1, &Moo2);
714                                         
715                                                 if (ConflictResult == 0){
716                                         
717                                                         // Decide Later.
718                                         
719                                                         iter->second = 4;
720                                                         continue;
721                                         
722                                                 } else if (ConflictResult == 1){
723                                         
724                                                         // Decided to use client version.
725                                         
726                                                         CardDAVIter->second.ProcessData();
727                                                         //ETagServer = CardDAVIter->second.GetServerETagValueThread();
728                                                         //ETagDBPtr->UpdateETag(ContactFilename, ETagServer, ETagServer);
729                                                 
730                                                         iter->second = 4;
731                                         
732                                                 } else if (ConflictResult == 2){
733                                         
734                                                         // Decided to use server version.
735                                                 
736                                                         // Write server version to file.
737                                                 
738                                                         ConvertedV4Data.WriteFile(FullFilename);
739                                                 
740                                                         iter->second = 4;
741                                         
742                                                 }
743                                 
744                                         } else {
745                                 
746                                                 // Update the data to the server.
747                                         
748                                                 CardDAVIter->second.ProcessData();
749                                 
750                                         }
751                         
752                                 }
753                                 
754                                 // Update the ETag DB.
755                                 
756                                 CardDAVIter->second.GetServerETagValueThread();
757                                 ETagServer = CardDAVIter->second.ETagValueResult();
758                                 ETagDBPtr->UpdateETag(ContactFilename, ETagServer, ETagServer);
759                         
760                                 iter->second = 4;
761                         
762                         } else if (TypeIter->second == 2) {
763                         
764                                 // Delete the contact from the server and update the ETag DB.
765                                 
766                                 AccountDir = StringAccountIter->second;
767                                 ContactFilename = StringFilenameIter->second;
768                                                                 
769                                 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
770                                 
771                                 CardDAVIter->second.SetUploadMode(FALSE);
772                                 CardDAVIter->second.SetupData(wxT("DELETE"), ContactFilename, wxT(""));
773                                 //CardDAVIter->second.SetServerFilename();
774                                 CardDAVIter->second.ProcessData();
775                                 ETagDBPtr->RemoveETag(ContactFilename);
776                                 
777                                 iter->second = 4;
778                         
779                         } else if (TypeIter->second == 3) {
780                         
781                                 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
783                                 wxString AccountNameFriendly;
784                                 wxString PrefFilename = GetUserPrefDir();
785         
786                                 XABPreferences PrefData(PrefFilename);
787                                 int AccountID = 0;
789                                 for (int i = 0; i < PrefData.accounts.GetCount(); i++){
790         
791                                         wxString AccountDirCheck = PrefData.accounts.GetAccountDirectory(i);
792                 
793                                         AccountDirCheck.Trim();
794                                         AccountDirCheck.Append(wxT(".carddav"));
795         
796                                         if (AccountDirCheck == StringAccountIter->second){
797                                         
798                                                 AccountNameFriendly = PrefData.accounts.GetAccountName(i);
799                                                 AccountID = i;
800                 
801                                         }
802         
803                                 }
804                         
805                                 bool KeepUpdating = TRUE;
807                                 while (KeepUpdating == TRUE){
809                                         StringTaskDetailIter = ActivityListTaskDetail.find(iter->first);
810                         
811                                         AccountDir = StringAccountIter->second;
813                                         ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
814                                 
815                                         wxArrayString vCardAdd;
816                                         wxArrayString vCardDelete;
817                                         wxArrayString vCardList;
818                                         wxArrayString vCardUpdateLocal;
819                                         wxArrayString vCardUpdateRemote;
820                                         wxArrayString vCardUpdateLog;
821                                         std::map<wxString,wxString> vCardETag;
822                                         ContactListData ServerContactList;
823                                 
824                                         std::map<wxString,vCardDownloadDataFileClass> vCardDownloadData;
825                                         //wxArrayString vCardDownload;
826                                         //std::map<wxString,wxString> vCardDownloadData;
827                                 
828                                         // Get the list of vCard files locally.
829         
830                                         wxString AccountDirFinal;
831                                         wxString AccountSyncTokenFilename;
832                                         wxString SyncTokenLoad;
833         
834 #if defined(__HAIKU__)
835     
836                                             //preffilename = wxT("noo");
838 #elif defined(__WIN32__)
840                                         AccountDirFinal = GetAccountDir(AccountDir, FALSE);
841                                         AccountSyncTokenFilename = AccountDirFinal;
842                                         AccountSyncTokenFilename.Append(wxT("synctoken"));
844 #else
846                                         AccountDirFinal = GetAccountDir(AccountDir, FALSE);
847                                         AccountSyncTokenFilename = AccountDirFinal;
848                                         AccountSyncTokenFilename.Append(wxT("synctoken"));
849         
850 #endif
852                                         // Suppress Error Messages.
853                                 
854                                         wxLogNull PleaseDoNotDisturb;
856                                         wxFFile SyncLoad;
857                                         SyncLoad.Open(AccountSyncTokenFilename, wxT("r"));
858                                 
859                                         if (SyncLoad.IsOpened()){
860                                 
861                                                 SyncLoad.ReadAll(&SyncTokenLoad, wxMBConvUTF8());
862                                                 SyncLoad.Close();
863                                 
864                                         }
866                                         // Get a list of vCard files from the server.
868                                         // Get the list of vCard files from the ETagDB.
869                                 
870                                         std::map<wxString,ETagData> ETagDBData = ETagDBPtr->GetETagData();
871                                         
872                                         CardDAV CardDAVInstance = CardDAVIter->second;
873                                         
874                                         bool ExitLoop = FALSE;
876                                         std::map<wxString,FileSyncData> *SCListData = NULL;
877                                         
878                                         while (ExitLoop == FALSE){
879                                         
880                                                 //ServerContactList = CardDAVInstance.GetContactList(SyncTokenLoad.ToUTF8());
881                                                 ServerContactList = CardDAVIter->second.GetContactList(SyncTokenLoad.ToUTF8());
882                                 
883                                                 // Create a pointer for the std::map<wxString,FileSyncData>.
884                                 
885                                                 SCListData = &ServerContactList.ListData;
886                                 
887                                                 // Check if any errors have occured whilst getting the data.
888                                 
889                                                 int ErrorCode = CardDAVIter->second.GetResultCode();
890                                 
891                                                 if (ErrorCode != 0){
892                                         
893                                                         if (ErrorCode == 60 || ErrorCode == 51){
894                                                 
895                                                                 // Invalid SSL certificate so bring up a dialog to the user
896                                                                 // explaining what has happened and the options available.
897                                                         
898                                                                 // Stop all the timers.
899                                                                 
900                                                                 frmMainPtrGet->PauseAllTimers();
901                                                         
902                                                                 // Setup the data to be sent in the wxPostEvent command.
904                                                                 SSLInvalidCertNotifObj SSLICNProcData;
906                                                                 bool *PauseMode = new bool;
907                                                                 int SSLResult;
908                                                                 QRNotif qrn;
910                                                                 *PauseMode = TRUE;                                              
911                                                                 qrn.QResponse = &SSLResult;
912                                                                 qrn.PausePtr = PauseMode;
913                                                                 
914                                                                 SSLICNProcData.CertCollection = CardDAVIter->second.GetCertificateData();
915                                                                 SSLICNProcData.QRNotifData = &qrn;
916                                                                 SSLICNProcData.AccountName = AccountNameFriendly;
917                                                         
918                                                                 wxCommandEvent event(INVALIDSSLCERT);
919                                                                 event.SetClientData(&SSLICNProcData);
920                                                                 wxPostEvent(frmMainPtrGet, event);
921                                         
922                                                                 /*timespec n1, n2;
923                                                 
924                                                                 // Fall asleep until we get an response.
925                                                 
926                                                                 n1.tv_sec = 0;
927                                                                 n1.tv_nsec = 250000000L;*/
928                                         
929                                                                 while (*PauseMode == TRUE){
930                                                                         //nanosleep(&n1, &n2);
931                                                                         SleepFor(250000000);
932                                                                 }
933                                                         
934                                                                 // Process the response from the user.
935                                                         
936                                                                 if (SSLResult == 1){
937                                                                 
938                                                                         // Accept the certificate. Write the certificate into the account
939                                                                         // directory (as server.crt) - will overwrite old certificate.
941                                                                         frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, CardDAVIter->second.GetCertificateData());
942                                                                         WriteServerCertificate(AccountDir, CardDAVIter->second.GetCertificateData());
943                                                                 
944                                                                 } else if (SSLResult == 2){
945                                                                 
946                                                                         // Reject the certificate, abort the task and mark as failed.
948                                                                         frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, CardDAVIter->second.GetCertificateData());
949                                                                         iter->second = 2;
950                                                                         break;
951                                                                 
952                                                                 }
953                                                         
954                                                                 // Resume all the timers.
955                                                                 
956                                                                 frmMainPtrGet->ResumeAllTimers();
957                                                 
958                                                         } else {
959                                                         
960                                                                 iter->second = 4;
961                                                                 break;
962                                                         
963                                                         }
964                                         
965                                                 } else {
966                                         
967                                                         ExitLoop = TRUE;
968                                         
969                                                 }
970                                         
971                                         }
972                                         
973                                         if (iter->second == 2 || iter->second == 3 || iter->second == 4){
974                                                 break;
975                                         }
976                                 
977                                         frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, CardDAVIter->second.GetCertificateData());
978                                 
979                                         // Look in directory and see which vCards to add and delete
980                                 
981                                         // In short:
982                                         // vCard exists but not in ETagDB - Add (and upload).
983                                         // vCard missing but in ETagDB - Delete (from server).
984                                 
985                                         // Start by looking for vCards that are not in the ETagDB.
986                                 
987                                         wxDir AccDir(AccountDirFinal);
988                                         wxString AccDirFilename;
989                                 
990                                         bool ContinueProcess = AccDir.GetFirst(&AccDirFilename, wxEmptyString, wxDIR_DEFAULT);
991                                 
992                                         // Process the server data.
994                                         if (SCListData != NULL){
996                                                 for (std::map<wxString, FileSyncData>::iterator fsiter = SCListData->begin();
997                                                         fsiter != SCListData->end(); fsiter++){
999                                                         // Check the mode of the file before adding.
1001                                                         // Get the HTTP status of the file from the list.
1003                                                         FileSyncData fsd = fsiter->second;
1005                                                         if (fsd.DataFlag == 1){
1007                                                                 // Added or modified contact.
1009                                                                 //vCardAdd.Add(fsiter->first, 1);
1011                                                                 vCardDownloadDataFileClass vCardNewData;
1012                                                                 vCardNewData.DataType = 0;
1013                                                                 vCardDownloadData.insert(std::make_pair(fsiter->first, vCardNewData));
1015                                                                 //} else if (fsd.DataFlag == 1){
1017                                                                 // Contact has been modified on the server.
1019                                                                 //      vCardUpdateLocal.Add(fsiter->first, 1);
1021                                                         }
1022                                                         else if (fsd.DataFlag == 2){
1024                                                                 // Contact has been deleted.
1026                                                                 vCardDelete.Add(fsiter->first, 1);
1028                                                         }
1030                                                 }
1032                                         }
1033                                 
1034                                         // Look for vCards that are missing but in the ETagDB.
1035         
1036                                         for (std::map<wxString,ETagData>::iterator etagiter = ETagDBData.begin();
1037                                                 etagiter != ETagDBData.end(); etagiter++){
1038                                 
1039                                                 // Check if the vCard exists.
1040                                         
1041                                                 if (!wxFileExists(AccountDirFinal + etagiter->first)){
1042                                         
1043                                                         // vCard doesn't exist so mark for deleting.
1044                                         
1045                                                         vCardDelete.Add(etagiter->first, 1);
1046                                         
1047                                                 }
1048                                 
1049                                         }
1051                                         // Compare Server with the ETag DB. Work out which
1052                                         // needs to updated remotely and which needs to be
1053                                         // updated locally.
1055                                         for (std::map<wxString,FileSyncData>::iterator srviter = SCListData->begin();
1056                                                 srviter != SCListData->end(); srviter++){
1058                                                 if (ETagDBData.find(srviter->first) == ETagDBData.end()){
1059                                                         continue;
1060                                                 }
1061                                         
1062                                                 std::map<wxString,ETagData>::iterator lociter = ETagDBData.find(srviter->first);
1063                                 
1064                                                 ETagData etd = lociter->second;
1065                                                 FileSyncData fsd = srviter->second;
1066                                         
1067                                                 wxString ETagSrv = fsd.ETagData;
1069                                                 // Compare local ETag with original local ETag.
1070                                                 // If different, update to server is required.
1071                                 
1072                                                 if (etd.ETagValue != etd.ETagOrigValue){
1073                                 
1074                                                         // Check if local original ETag is different from server ETag.
1075                                                         // If it is then throw up a conflict resolution dialog.
1076                                 
1077                                                         if (etd.ETagOrigValue != ETagSrv){
1078                                         
1079                                                                 vCard34Conv vCard34Obj;
1080                                                 
1081                                                                 wxString ServerContactData;
1082                                                                 wxString FullFilename;
1083                                                                 vCard ClientData;
1084                                                                 vCard ConvertedV4Data;
1085                                         
1086                                                                 // Conflict. Bring up conflict resolution dialog.
1087                                                         
1088                                                                 // Get the server contact data for comparison.
1089                                                         
1090                                                                 CardDAVIter->second.SetServerFilename(srviter->first);
1091                                                                 CardDAVIter->second.GetServerContactData();
1092                                                                 ServerContactData = CardDAVIter->second.GetPageData();
1093                                                         
1094                                                                 FullFilename = CreateFilenamePath(AccountDirFinal, srviter->first);
1095                                                         
1096                                                                 // Convert it to v4.
1097                                         
1098                                                                 vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data);
1099                                         
1100                                                                 vCardConflictObj vCardProcData;
1101                                         
1102                                                                 vCardProcData.vCardLocalData = &ClientData;
1103                                                                 vCardProcData.vCardServerData = &ConvertedV4Data;
1104                                                         
1105                                                                 // Load the client data from the filename given.
1106                                                         
1107                                                                 ClientData.LoadFile(FullFilename);
1108                                                         
1109                                                                 // Show dialog.
1110                                         
1111                                                                 bool *PauseMode = new bool;
1112                                                                 int ConflictResult;
1113                                                                 QRNotif qrn;
1114                                                                 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
1116                                                                 *PauseMode = TRUE;
1117                                                                 qrn.QResponse = &ConflictResult;
1118                                                                 qrn.PausePtr = PauseMode;
1119                                                 
1120                                                                 vCardProcData.QRNotifData = &qrn;
1121                                         
1122                                                                 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
1123                                                                 event.SetClientData(&vCardProcData);
1124                                                                 wxPostEvent(frmMainPtrGet, event);
1125                                                 
1126                                                                 /*timespec n1, n2;
1127                                                 
1128                                                                 n1.tv_sec = 0;
1129                                                                 n1.tv_nsec = 250000000L;*/
1130                                                 
1131                                                                 while (*PauseMode == TRUE){
1132                                                                         //nanosleep(&n1, &n2);
1133                                                                         SleepFor(250000000);
1134                                                                 }
1135                                         
1136                                                                 delete PauseMode;
1137                                                                 PauseMode = NULL;
1138                                                         
1139                                                                 // If replacing local version, store copy for now.
1140                                                                 // Otherwise upload to server.
1141                                         
1142                                                                 if (ConflictResult == 0){
1143                                         
1144                                                                         // Decide Later.
1145                                         
1146                                                                         continue;
1147                                         
1148                                                                 } else if (ConflictResult == 1){
1149                                         
1150                                                                         // Decided to use client version.
1151                                         
1152                                                                         vCardUpdateLocal.Remove(srviter->first);
1153                                                                         vCardUpdateRemote.Add(srviter->first, 1);                                                               
1154                                         
1155                                                                 } else if (ConflictResult == 2){
1156                                         
1157                                                                         // Decided to use server version.
1158                                                                         // Store the converted downloaded copy for
1159                                                                         // processing later.
1161                                                                         //vCardUpdateLocal.Add(srviter->first, 1);
1162                                                                 
1163                                                                         vCardDownloadDataFileClass vCardNewData;
1164                                                                         vCardNewData.DataType = 1;
1165                                                                         vCardNewData.FileData = ConvertedV4Data.WriteString();
1167                                                                         ConvertedV4Data.WriteFile(FullFilename);
1168                                                                 
1169                                                                         vCardDownloadData.insert(std::make_pair(srviter->first, vCardNewData)); 
1170                                                                 
1171                                                                 
1172                                                                 
1173                                                                 }
1174                                         
1175                                                         } else {
1176                                         
1177                                                                 // Send vCard to the server.
1178                                                 
1179                                                                 vCardUpdateRemote.Add(srviter->first, 1);
1180                                         
1181                                                         }
1182                                 
1183                                                 } else {
1184                                 
1185                                                         // Check if local ETag is different from server ETag.
1186                                                         // If different, obtain the vCard from the server.
1187                                         
1188                                                         if (etd.ETagOrigValue != ETagSrv){
1189                                         
1190                                                                 vCardUpdateLocal.Add(srviter->first, 1);
1191                                         
1192                                                         }
1193                                 
1194                                                 }
1195                         
1196                                         }
1197                                 
1198                                         // Process the vCards that need to be downloaded and added/overwritten.
1199                                 
1200                                         wxString vCardFilenameFinal;
1201                                         wxString vCardOutputData;
1202                                         wxString vCardInputData;
1203                                         wxString ETagFileData;
1204                                         wxFFile vCardFile;
1205                                 
1206                                         for (std::map<wxString,vCardDownloadDataFileClass>::iterator additer = vCardDownloadData.begin();
1207                                                 additer != vCardDownloadData.end(); additer++){
1208                                         
1209                                                 vCardFilenameFinal.Clear();
1210                                                 vCardInputData.Clear();
1211                                                 vCardOutputData.Clear();
1212                                                 ETagFileData.Clear();
1213                                         
1214                                                 vCardFilenameFinal = CreateFilenamePath(AccountDirFinal, additer->first);
1215                                         
1216                                                 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
1217                                         
1218                                                 if (additer->second.DataType == 0){
1219                                         
1220                                                         vCard ConvertedV4Data;
1221                                         
1222                                                         // Grab the file from the server and write the contact out.
1223                                         
1224                                                         vCard34Conv vCard34Obj;
1225                                                 
1226                                                         CardDAVIter->second.SetServerFilename(additer->first);
1227                                                         CardDAVIter->second.GetServerContactData();
1228                                                         vCardInputData = CardDAVIter->second.GetPageData();
1229                                                 
1230                                                         vCard34Obj.ConvertToV4(&vCardInputData, &ConvertedV4Data);
1231                                                 
1232                                                         ConvertedV4Data.WriteFile(vCardFilenameFinal);
1233                                                 
1234                                                         // Update the ETagDB.
1235                                                 
1236                                                         //vCardFile.Open(vCardFilenameFinal, wxT("w"));
1237                                 
1238                                                         //if (vCardFile.IsOpened()){
1239                                 
1240                                                         //      vCardFile.Write(vCardOutputData, wxConvAuto());
1241                                                         //      vCardFile.Close();
1242                                 
1243                                                         //}
1244                                                 
1245                                                         // Get the server ETag.
1246                                                 
1247                                                         wxString ServerETag;
1248                                                         CardDAVIter->second.GetServerETagValueThread();
1249                                                         ServerETag = CardDAVIter->second.ETagValueResult();
1250                                                 
1251                                                         // Add to the ETag Database.
1252                                                 
1253                                                         ETagDBPtr->AddETag(additer->first, ServerETag, ServerETag);
1254                                                 
1255                                                 } else if (additer->second.DataType == 1){
1256                                         
1257                                                         // Write out the contact to the account directory.
1258                                         
1259                                                         vCardFilenameFinal = CreateFilenamePath(AccountDirFinal, additer->first);
1260                                                         vCardOutputData = additer->second.FileData;
1261                                                 
1262                                                         vCardFile.Open(vCardFilenameFinal, wxT("w"));
1263                                 
1264                                                         if (vCardFile.IsOpened()){
1265                                 
1266                                                                 vCardFile.Write(vCardOutputData, wxConvAuto());
1267                                                                 vCardFile.Close();
1268                                 
1269                                                         }
1270                                                 
1271                                                         // Update the ETagDB.
1273                                                         wxString ServerETag;
1274                                                         CardDAVIter->second.GetServerETagValueThread();
1275                                                         ServerETag = CardDAVIter->second.ETagValueResult();
1276                                                 
1277                                                         // Add to the ETag Database.
1278                                                 
1279                                                         ETagDBPtr->AddETag(additer->first, ServerETag, ServerETag);
1280                                                                                         
1281                                                 }
1282                                         }
1283         
1284                                         // Look for vCard files which aren't in the ETagDB.
1285         
1286                                         while(ContinueProcess){
1287                                 
1288                                                 // Check and make sure that the filename ends in .vcf/.vcard
1289                                                 // Skip any file that beings with a dot.
1290                                         
1291                                                 if (AccDirFilename.Left(1) == wxT(".")){
1292                                         
1293                                                         ContinueProcess = AccDir.GetNext(&AccDirFilename);
1294                                                         continue;
1295                                         
1296                                                 }
1297                                         
1298                                                 if (AccDirFilename.Right(4) != wxT(".vcf") && 
1299                                                 AccDirFilename.Right(6) != wxT(".vcard")){
1300                                         
1301                                                         ContinueProcess = AccDir.GetNext(&AccDirFilename);
1302                                                         continue;
1303                                         
1304                                                 }
1305                                 
1306                                                 // Look in the ETagDB for the file.
1307                                         
1308                                                 bool ETagExists = ETagDBPtr->CheckETagExists(AccDirFilename);
1309                                         
1310                                                 if (ETagExists == FALSE){
1311                                         
1312                                                         // Add the vCard to the list to be added.
1313                                                 
1314                                                         vCardAdd.Add(AccDirFilename, 1);
1315                                         
1316                                                 }
1317                                         
1318                                                 ContinueProcess = AccDir.GetNext(&AccDirFilename);
1319                                 
1320                                         }
1321                                 
1322                                         // Process the vCards to add/update to the server.
1323                                 
1324                                         for (int vi = 0; vi < vCardAdd.GetCount(); vi++){
1326                                                 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
1327                                                 ETag = ETagDBPtr->GetETag(vCardAdd[vi]);
1328                                                 ETagOriginal = ETagDBPtr->GetETagOriginal(vCardAdd[vi]);
1329                                                 ContactFilename = vCardAdd[vi];
1330                                                 FullFilename = CreateFilenamePath(AccountDirFinal, vCardAdd[vi]);
1332                                                 ContactFilename.Trim();
1333                         
1334                                                 if (ETag == ETagOriginal){
1335                         
1336                                                         // Compare the ETag with the Server ETag.
1337                                 
1338                                                         if (ETagOriginal != ETagServer){
1339                                 
1340                                                                 // Server ETag is different from original ETag.
1341                                                                 // This is a conflict which the user will now
1342                                                                 // need to resolve.
1343                                         
1344                                                                 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
1345                                         
1346                                                                 vCard34Conv vCard34Obj;
1347                                                 
1348                                                                 wxString ServerContactData;
1349                                                                 vCard ClientData;
1350                                                                 vCard ConvertedV4Data;
1351                                         
1352                                                                 // Load the client data from the filename given.
1353                                         
1354                                                                 CardDAVIter->second.GetServerContactData();
1355                                                                 ServerContactData = CardDAVIter->second.GetPageData();
1356                                                 
1357                                                                 // Process v3 version into v4 version.
1358                                                 
1359                                                                 vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data);
1360                                         
1361                                                                 vCardConflictObj vCardProcData;
1362                                         
1363                                                                 vCardProcData.vCardLocalData = &ClientData;
1364                                                                 vCardProcData.vCardServerData = &ConvertedV4Data;
1365                                                                 ClientData.LoadFile(FullFilename);
1367                                                                 //ContactConflictEvent event(ContactConflictCmdEnv);
1368                                                                 //event.SetData(ContactFilename, Moo1, Moo2);
1369                                         
1370                                                                 //wxPostEvent(frmMainPtrGet, event);
1371                                         
1372                                                                 // Setup Conflict Resolution Dialog.
1373                                         
1374                                                                 // Fetch Data from Server and convert.
1375                                         
1376                                                                 bool *PauseMode = new bool;
1377                                                                 int ConflictResult;
1378                                                                 QRNotif qrn;
1380                                                                 *PauseMode = TRUE;                                              
1381                                                                 qrn.QResponse = &ConflictResult;
1382                                                                 qrn.PausePtr = PauseMode;
1383                                                 
1384                                                                 vCardProcData.QRNotifData = &qrn;
1385                                         
1386                                                                 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
1387                                                                 event.SetClientData(&vCardProcData);
1388                                                                 wxPostEvent(frmMainPtrGet, event);
1389                                                 
1390                                                                 /*timespec n1, n2;
1391                                                 
1392                                                                 n1.tv_sec = 0;
1393                                                                 n1.tv_nsec = 250000000L;*/
1394                                                 
1395                                                                 while (*PauseMode == TRUE){
1396                                                                         //nanosleep(&n1, &n2);
1397                                                                         SleepFor(250000000);
1398                                                                 }
1399                                         
1400                                                                 delete PauseMode;
1401                                                                 PauseMode = NULL;
1402                                         
1403                                                                 //intResult = frmMainPtrGet->ShowConflictDialog(&Moo1, &Moo2);
1404                                         
1405                                                                 if (ConflictResult == 0){
1406                                         
1407                                                                         // Decide Later.
1408                                         
1409                                                                         iter->second = 4;
1410                                                                         continue;
1411                                         
1412                                                                 } else if (ConflictResult == 1){
1413                                         
1414                                                                         // Decided to use client version.
1415                                         
1416                                                                         CardDAVIter->second.ProcessData();
1417                                                 
1418                                                                         iter->second = 4;
1419                                         
1420                                                                 } else if (ConflictResult == 2){
1421                                         
1422                                                                         // Decided to use server version.
1423                                                 
1424                                                                         // Download server version.
1425                                                                 
1426                                                                         ConvertedV4Data.WriteFile(CreateFilenamePath(AccountDirFinal, vCardAdd[vi]));
1427                                                 
1428                                                                         iter->second = 4;
1429                                         
1430                                                                 }
1431                                 
1432                                                         } else {
1433                                 
1434                                                                 // Setup the filename for the contact being added.
1435                                                         
1436                                                                 wxString wxSFilename = CreateFilenamePath(AccountDirFinal, vCardAdd[vi]);
1437                                                                 wxString wxSCardData;
1438                                                                 wxString TaskData;
1439                                                                 //wxString TaskURL;
1440                                                         
1441                                                                 // Load the data from the file.
1442                                                         
1443                                                                 wxFFile wxFCard;
1444                                                                 wxFCard.Open(wxSFilename, wxT("r"));
1445                                 
1446                                                                 if (wxFCard.IsOpened()){
1447                                 
1448                                                                         wxFCard.ReadAll(&wxSCardData, wxConvAuto());
1449                                                                         wxFCard.Close();
1450                                 
1451                                                                 }
1452                                                         
1453                                                                 // Convert the data to v3.
1454                                                         
1455                                                                 vCard34Conv vCard34Obj;
1456                                                         
1457                                                                 vCard34Obj.ConvertToV3(wxSFilename, &TaskData);
1458                                                         
1459                                                                 // Setup the URL address.
1460                                                         
1461                                                                 //TaskURL = StringTaskDetailIter->second + wxT("/") + vCardAdd[vi];
1463                                                                 // Upload the data to the server.
1464                                         
1465                                                                 CardDAVIter->second.SetUploadMode(TRUE);
1466                                                                 CardDAVIter->second.SetupData(wxT("PUT"), vCardAdd[vi], TaskData);
1467                                                                 CardDAVIter->second.ProcessData();
1468                                                         
1469                                                                 // Get the Server ETag and put it in the ETag database.
1470                                                         
1471                                                                 CardDAVIter->second.SetServerFilename(vCardAdd[vi]);
1472                                                                 CardDAVIter->second.GetServerETagValueThread();
1473                                                                 ETagServer = CardDAVIter->second.ETagValueResult();
1474                                 
1475                                                                 ETagDBPtr->AddETag(vCardAdd[vi], ETagServer, ETagServer);
1476                                 
1477                                                         }
1478                         
1479                                                 } else {
1480                         
1481                                                         if (ETagOriginal != ETagServer){
1482                                 
1483                                                                 // Server ETag is different from original ETag.
1484                                                                 // This is a conflict which the user will now
1485                                                                 // need to resolve.
1486                                         
1487                                                                 frmMain *frmMainPtrGet = (frmMain *)frmMainPtr;
1488                                         
1489                                                                 //ContactConflictEvent event(ContactConflictCmdEnv);
1490                                                                 //event.SetData(ContactFilename, Moo1, Moo2);
1491                                         
1492                                                                 vCard34Conv vCard34Obj;
1493                                                 
1494                                                                 wxString ServerContactData;
1495                                                                 vCard ClientData;
1496                                                                 vCard ConvertedV4Data;
1497                                         
1498                                                                 // Load the client data from the filename given.
1499                                         
1500                                                                 CardDAVIter->second.GetServerContactData();
1501                                                                 ServerContactData = CardDAVIter->second.GetPageData();
1502                                                                 ClientData.LoadFile(FullFilename);
1503                                                 
1504                                                                 // Process v3 version into v4 version.
1505                                                 
1506                                                                 vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data);
1507                                         
1508                                                                 vCardConflictObj vCardProcData;
1509                                         
1510                                                                 vCardProcData.vCardLocalData = &ClientData;
1511                                                                 vCardProcData.vCardServerData = &ConvertedV4Data;
1512                                         
1513                                                                 bool *PauseMode = new bool;
1514                                                                 int ConflictResult;
1515                                                                 QRNotif qrn;
1517                                                                 *PauseMode = TRUE;                                              
1518                                                                 qrn.QResponse = &ConflictResult;
1519                                                                 qrn.PausePtr = PauseMode;
1520                                                 
1521                                                                 vCardProcData.QRNotifData = &qrn;
1522                                         
1523                                                                 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
1524                                                                 event.SetClientData(&vCardProcData);
1525                                                                 wxPostEvent(frmMainPtrGet, event);
1526                                         
1527                                                                 /*timespec n1, n2;
1528                                                 
1529                                                                 n1.tv_sec = 0;
1530                                                                 n1.tv_nsec = 250000000L;*/
1531                                         
1532                                                                 while (*PauseMode == TRUE){
1533                                                                         //nanosleep(&n1, &n2);
1534                                                                         SleepFor(250000000);
1535                                                                 }
1536                                         
1537                                                                 delete PauseMode;
1538                                                                 PauseMode = NULL;
1539                                         
1540                                                                 //intResult = frmMainPtrGet->ShowConflictDialog(&Moo1, &Moo2);
1541                                         
1542                                                                 if (ConflictResult == 0){
1543                                         
1544                                                                         // Decide Later.
1545                                         
1546                                                                         iter->second = 4;
1547                                                                         continue;
1548                                         
1549                                                                 } else if (ConflictResult == 1){
1550                                         
1551                                                                         // Decided to use client version.
1552                                                                 
1553                                                                         CardDAVIter->second.ProcessData();
1554                                                                         //ETagServer = CardDAVIter->second.GetServerETagValueThread();
1555                                                                         //ETagDBPtr->UpdateETag(ContactFilename, ETagServer, ETagServer);
1556                                                 
1557                                                                         iter->second = 4;
1558                                         
1559                                                                 } else if (ConflictResult == 2){
1560                                         
1561                                                                         // Decided to use server version.
1562                                                 
1563                                                                         // Write server version to file.
1564                                                                 
1565                                                                         ConvertedV4Data.WriteFile(CreateFilenamePath(AccountDirFinal, vCardAdd[vi]));
1566                                                 
1567                                                                         iter->second = 4;
1568                                         
1569                                                                 }
1570                                 
1571                                                         } else {
1572                                 
1573                                                                 // Setup the filename for the contact being added.
1574                                                         
1575                                                                 wxString wxSFilename = CreateFilenamePath(AccountDirFinal, vCardAdd[vi]);
1576                                                                 wxString wxSCardData;
1577                                                                 wxString TaskData;
1578                                                                 //wxString TaskURL;
1579                                                         
1580                                                                 // Load the data from the file.
1581                                                         
1582                                                                 wxFFile wxFCard;
1583                                                                 wxFCard.Open(wxSFilename, wxT("r"));
1584                                 
1585                                                                 if (wxFCard.IsOpened()){
1586                                 
1587                                                                         wxFCard.ReadAll(&wxSCardData, wxConvAuto());
1588                                                                         wxFCard.Close();
1589                                 
1590                                                                 }
1591                                                         
1592                                                                 // Convert the data to v3.
1593                                                         
1594                                                                 vCard34Conv vCard34Obj;
1595                                                         
1596                                                                 vCard34Obj.ConvertToV3(wxSFilename, &TaskData);
1597                                                         
1598                                                                 // Setup the URL address.
1599                                                         
1600                                                                 //TaskURL = StringTaskDetailIter->second + wxT("/") + vCardAdd[vi];
1602                                                                 // Upload the data to the server.
1603                                         
1604                                                                 CardDAVIter->second.SetUploadMode(TRUE);
1605                                                                 CardDAVIter->second.SetupData(wxT("PUT"), vCardAdd[vi], TaskData);
1606                                                                 CardDAVIter->second.ProcessData();
1607                                                         
1608                                                                 // Get the Server ETag and put it in the ETag database.
1609                                                         
1610                                                                 CardDAVIter->second.SetServerFilename(vCardAdd[vi]);
1611                                                                 CardDAVIter->second.GetServerETagValueThread();
1612                                                                 ETagServer = CardDAVIter->second.ETagValueResult();
1613                                 
1614                                 
1615                                                                 ETagDBPtr->AddETag(vCardAdd[vi], ETagServer, ETagServer);
1616                                 
1617                                                         }
1618                                         
1619                                                 }
1621                                         }                       
1622                                 
1623                                         // Process the vCards to delete locally.
1624                                 
1625                                         for (int vi = 0; vi < vCardDelete.GetCount(); vi++){
1627                                                 if (!wxRemoveFile(CreateFilenamePath(AccountDirFinal, vCardDelete[vi]))){
1629                                                 } else {
1630                                         
1631                                                         ETagDBPtr->RemoveETag(vCardDelete[vi]);
1632                                         
1633                                                 }
1635                                         }
1636                                 
1637                                         // Process the vCards to delete from the server.
1638                                 
1639                                         std::map<wxString,wxString> *ETagFilenameMap = ETagDBPtr->GetFilenamePointer();
1640                                 
1641                                 
1642                                         for (std::map<wxString,wxString>::iterator deliter = ETagFilenameMap->begin();
1643                                                 deliter != ETagFilenameMap->end(); deliter++){
1644                                 
1645                                                 // Look through the ETagDB and find contacts which are marked as DELETED.
1646                                         
1647                                                 if (deliter->second == wxT("DELETED")){
1649                                                         CardDAVIter->second.SetUploadMode(FALSE);
1650                                                         CardDAVIter->second.SetupData(wxT("DELETE"), deliter->first, wxT(""));
1651                                                         CardDAVIter->second.ProcessData();
1652                                         
1653                                                         // Delete the ETag as the file has now been deleted.
1654                                         
1655                                                         ETagDBPtr->RemoveETag(deliter->first);
1656                                         
1657                                                 }
1658                                 
1659                                         }
1660                                 
1661                                         // Write out the update server sync token.
1663                                         wxString ServerSyncToken = ServerContactList.SyncToken;
1664                                 
1665                                         ServerSyncToken.Trim();
1666                                 
1667                                         if (!ServerSyncToken.IsEmpty()){
1668                                 
1669                                                 wxFFile SyncWrite;
1670                                                 SyncWrite.Open(AccountSyncTokenFilename, wxT("w"));
1671                                 
1672                                                 if (SyncWrite.IsOpened()){
1673                                 
1674                                                         SyncWrite.Write(ServerSyncToken, wxConvAuto());
1675                                                         SyncWrite.Close();
1676                                 
1677                                                 }
1678                                 
1679                                         }
1680                                 
1681                                         // Check for another sync token. If there is another 
1682                                         // sync token then repeat this. If token matches then stop processing.
1683                                 
1684                                         if (SyncTokenLoad == ServerSyncToken){
1685                                         
1686                                                 KeepUpdating = FALSE;
1687                                         
1688                                         }
1689                                         
1690                                 }
1691                                 
1692                                 // Mark as completed.
1693                                 
1694                                 iter->second = 4;
1695                                 continue;
1696                         
1697                         } else {
1698                         
1699                                 // Update the data to the server.
1700                                         
1701                                 CardDAVIter->second.ProcessData();
1702                         
1703                         }
1705                         /*timespec n1, n2;
1706                                                 
1707                         n1.tv_sec = 0;
1708                         n1.tv_nsec = 250000000L;*/
1710                         while (iter->second == 1){
1712                                 SleepFor(250000000);
1713                                 //nanosleep(&n1, &n2);
1714         
1715                         }
1716                         
1717                         if ((iter->second == 4 && TypeIter->second == 0) || 
1718                         (iter->second == 8 && TypeIter->second == 0)){
1720                                 AccountDir = StringAccountIter->second;
1721                                 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
1722                                 
1723                                 CardDAVIter->second.GetServerETagValueThread();
1724                                 ETagServer = CardDAVIter->second.ETagValueResult();
1725                                 ETagDBPtr->UpdateETag(ContactFilename, ETagServer, ETagServer);
1726                         
1727                         }
1728                         
1729                         if ((iter->second == 4 && TypeIter->second == 1) || 
1730                         (iter->second == 8 && TypeIter->second == 1)){
1731                         
1732                                 AccountDir = StringAccountIter->second;
1733                                 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);                 
1735                                 CardDAVIter->second.GetServerETagValueThread();
1736                                 ETagServer = CardDAVIter->second.ETagValueResult();
1737                                 ETagDBPtr->UpdateETag(ContactFilename, ETagServer, ETagServer);
1738                         
1739                         }
1740                         
1741                         break;
1742                         
1743                 }
1745         }
1746         
1747         if (TasksFoundProc == FALSE){
1748         
1749                 TasksActive = FALSE;
1750                 ProcessTasksData = FALSE;
1751         
1752         } else {
1754                 wxCommandEvent eventp(ACTMGR_STARTTIMER);
1755                 wxPostEvent(this, eventp);
1757                 //AnimationTimer.StartAnimation();
1758                 //ActListProcTimer.Start();
1759         
1760         }
1762         wxCommandEvent event(ACTMGR_STARTPROCESSTIMER);
1763         wxPostEvent(this, event);
1765         //ActProcessTimer.Start();
1769 void frmActivityMgr::ProcessTasks()
1772         std::thread ProcessThread(&frmActivityMgr::ProcessTasksThread, this);
1773         ProcessThread.detach();
1777 void frmActivityMgr::SetupPointers(wxETagProcessTimer *ETagTmrPtrInc, void *frmMainPtrInc)
1780         ETagTmrPtr = ETagTmrPtrInc;
1781         
1782         frmMainPtr = frmMainPtrInc;
1786 void frmActivityMgr::ResumeProcessing(wxCommandEvent &event){
1788         QRNotif *qrn = (QRNotif *)event.GetClientData(); 
1789         
1790         int *QRes = qrn->QResponse;
1792         *qrn->PausePtr = FALSE;
1794         *QRes = event.GetInt();
1795         
1798 int frmActivityMgr::GetTaskStatus(int intTaskID){
1800         int intResult = 2;
1801         
1802         if (ActivityList.find(intTaskID) == ActivityList.end()){
1803         
1804                 return intResult;
1805         
1806         }
1807         
1808         std::map<int,int>::iterator taskiter = ActivityList.find(intTaskID);
1809         intResult = taskiter->second;
1810         
1811         return intResult;
1815 void frmActivityMgr::ToggleConnectionStatus( wxCommandEvent &event ){
1817         wxCommandEvent connevent(CONNSTAT_UPDATE);
1818         
1819         if (SleepMode == FALSE){
1820         
1821                 connevent.SetInt(1);
1822                 SleepMode = TRUE;
1823         
1824         } else {
1825         
1826                 connevent.SetInt(0);
1827                 SleepMode = FALSE;
1828         
1829         }
1831         wxPostEvent(GetParent(), connevent);
1835 void frmActivityMgr::WriteServerCertificate(wxString AccountName, SSLCertCollection SSLCertInc){
1837         wxString ServerCertFinal;
1839         // Get the Cert section of the certificate and write it to the file.
1841         ServerCertFinal = GetAccountDir(AccountName, TRUE);
1843         wxFile CertFile;
1844         
1845         std::map<int, SSLCertData>::iterator SSLCDIter = SSLCertInc.SSLCollection.find(0);
1846         std::multimap<wxString,wxString>::iterator SSLDataIter = SSLCDIter->second.CertData.find(wxT("Cert"));
1847         
1848         CertFile.Open(ServerCertFinal, wxFile::write);  
1849         
1850         CertFile.Write(SSLDataIter->second, wxConvUTF8);
1851         CertFile.Close();
1855 void frmActivityMgr::StartTimer(wxCommandEvent& event){
1857         ActListProcTimer.Start(1000, FALSE);
1861 void frmActivityMgr::StopTimer(wxCommandEvent& event){
1863         ActListProcTimer.Stop();
1867 void frmActivityMgr::StartProcessTimer(wxCommandEvent& event){
1869         ActProcessTimer.Start(1000, FALSE);
1873 void frmActivityMgr::StopProcessTimer(wxCommandEvent& event){
1875         ActProcessTimer.Stop();
1879 #endif
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy