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