Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Updated build system.
[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                 // TODO: Pass the ETagDB for the account to the connection.
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                                                 
648                                                         iter->second = 4;
649                                         
650                                                 }
651                                 
652                                         } else {
653                                 
654                                                 // Update the data to the server.
655                                         
656                                                 CardDAVIter->second.ProcessData();
657                                 
658                                         }
659                         
660                                 } else {
661                         
662                                         if (ETagOriginal != ETagServer){
663                                 
664                                                 // Server ETag is different from original ETag.
665                                                 // This is a conflict which the user will now
666                                                 // need to resolve.
667                                         
668                                                 frmMain *frmMainPtrGet = (frmMain *)frmMainPtr;
669                                         
670                                                 //ContactConflictEvent event(ContactConflictCmdEnv);
671                                                 //event.SetData(ContactFilename, Moo1, Moo2);
672                                         
673                                                 vCard34Conv vCard34Obj;
674                                                 
675                                                 wxString ServerContactData;
676                                                 vCard ClientData;
677                                                 vCard ConvertedV4Data;
678                                         
679                                                 // Load the client data from the filename given.
680                                         
681                                                 CardDAVIter->second.GetServerContactData();
682                                                 ServerContactData = CardDAVIter->second.GetPageData();
683                                                 ClientData.LoadFile(FullFilename);
684                                                 
685                                                 // Process v3 version into v4 version.
686                                                 
687                                                 vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data);
688                                         
689                                                 vCardConflictObj vCardProcData;
690                                         
691                                                 vCardProcData.vCardLocalData = &ClientData;
692                                                 vCardProcData.vCardServerData = &ConvertedV4Data;
693                                         
694                                                 bool *PauseMode = new bool;
695                                                 int ConflictResult;
696                                                 QRNotif qrn;
698                                                 *PauseMode = TRUE;                                              
699                                                 qrn.QResponse = &ConflictResult;
700                                                 qrn.PausePtr = PauseMode;
701                                                 
702                                                 vCardProcData.QRNotifData = &qrn;
703                                         
704                                                 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
705                                                 event.SetClientData(&vCardProcData);
706                                                 wxPostEvent(frmMainPtrGet, event);
707                                         
708                                                 /*timespec n1, n2;
709                                                 
710                                                 n1.tv_sec = 0;
711                                                 n1.tv_nsec = 250000000L;*/
712                                         
713                                                 while (*PauseMode == TRUE){
714                                                         //nanosleep(&n1, &n2);
715                                                         SleepFor(250000000);
716                                                 }
717                                         
718                                                 delete PauseMode;
719                                                 PauseMode = NULL;
720                                 
721                                                 //intResult = frmMainPtrGet->ShowConflictDialog(&Moo1, &Moo2);
722                                         
723                                                 if (ConflictResult == 0){
724                                         
725                                                         // Decide Later.
726                                         
727                                                         iter->second = 4;
728                                                         continue;
729                                         
730                                                 } else if (ConflictResult == 1){
731                                         
732                                                         // Decided to use client version.
733                                         
734                                                         CardDAVIter->second.ProcessData();
735                                                         //ETagServer = CardDAVIter->second.GetServerETagValueThread();
736                                                         //ETagDBPtr->UpdateETag(ContactFilename, ETagServer, ETagServer);
737                                                 
738                                                         iter->second = 4;
739                                         
740                                                 } else if (ConflictResult == 2){
741                                         
742                                                         // Decided to use server version.
743                                                 
744                                                         // Write server version to file.
745                                                 
746                                                         ConvertedV4Data.WriteFile(FullFilename);
747                                                 
748                                                         iter->second = 4;
749                                         
750                                                 }
751                                 
752                                         } else {
753                                 
754                                                 // Update the data to the server.
755                                         
756                                                 CardDAVIter->second.ProcessData();
757                                 
758                                         }
759                         
760                                 }
761                                 
762                                 // Update the ETag DB.
763                                 
764                                 CardDAVIter->second.GetServerETagValueThread();
765                                 ETagServer = CardDAVIter->second.ETagValueResult();
766                                 ETagDBPtr->UpdateETag(ContactFilename, ETagServer, ETagServer);
767                         
768                                 iter->second = 4;
769                         
770                         } else if (TypeIter->second == 2) {
771                         
772                                 // Delete the contact from the server and update the ETag DB.
773                                 
774                                 AccountDir = StringAccountIter->second;
775                                 ContactFilename = StringFilenameIter->second;
776                                                                 
777                                 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
778                                 
779                                 CardDAVIter->second.SetUploadMode(FALSE);
780                                 CardDAVIter->second.SetupData(wxT("DELETE"), ContactFilename, wxT(""));
781                                 //CardDAVIter->second.SetServerFilename();
782                                 CardDAVIter->second.ProcessData();
783                                 ETagDBPtr->RemoveETag(ContactFilename);
784                                 
785                                 iter->second = 4;
786                         
787                         } else if (TypeIter->second == 3) {
788                         
789                                 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
791                                 wxString AccountNameFriendly;
792                                 wxString PrefFilename = GetUserPrefDir();
793         
794                                 XABPreferences PrefData(PrefFilename);
795                                 int AccountID = 0;
797                                 for (int i = 0; i < PrefData.accounts.GetCount(); i++){
798         
799                                         wxString AccountDirCheck = PrefData.accounts.GetAccountDirectory(i);
800                 
801                                         AccountDirCheck.Trim();
802                                         AccountDirCheck.Append(wxT(".carddav"));
803         
804                                         if (AccountDirCheck == StringAccountIter->second){
805                                         
806                                                 AccountNameFriendly = PrefData.accounts.GetAccountName(i);
807                                                 AccountID = i;
808                 
809                                         }
810         
811                                 }
812                         
813                                 bool KeepUpdating = TRUE;
815                                 while (KeepUpdating == TRUE){
817                                         StringTaskDetailIter = ActivityListTaskDetail.find(iter->first);
818                         
819                                         AccountDir = StringAccountIter->second;
821                                         ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
822                                 
823                                         wxArrayString vCardAdd;
824                                         wxArrayString vCardDelete;
825                                         wxArrayString vCardList;
826                                         wxArrayString vCardUpdateLocal;
827                                         wxArrayString vCardUpdateRemote;
828                                         wxArrayString vCardUpdateLog;
829                                         std::map<wxString,wxString> vCardETag;
830                                         ContactListData ServerContactList;
831                                 
832                                         std::map<wxString,vCardDownloadDataFileClass> vCardDownloadData;
833                                         //wxArrayString vCardDownload;
834                                         //std::map<wxString,wxString> vCardDownloadData;
835                                 
836                                         // Get the list of vCard files locally.
837         
838                                         wxString AccountDirFinal;
839                                         wxString AccountSyncTokenFilename;
840                                         wxString SyncTokenLoad;
841         
842 #if defined(__HAIKU__)
843     
844                                             //preffilename = wxT("noo");
846 #elif defined(__WIN32__)
848                                         AccountDirFinal = GetAccountDir(AccountDir, FALSE);
849                                         AccountSyncTokenFilename = AccountDirFinal;
850                                         AccountSyncTokenFilename.Append(wxT("synctoken"));
852 #else
854                                         AccountDirFinal = GetAccountDir(AccountDir, FALSE);
855                                         AccountSyncTokenFilename = AccountDirFinal;
856                                         AccountSyncTokenFilename.Append(wxT("synctoken"));
857         
858 #endif
860                                         // Suppress Error Messages.
861                                 
862                                         wxLogNull PleaseDoNotDisturb;
864                                         wxFFile SyncLoad;
865                                         SyncLoad.Open(AccountSyncTokenFilename, wxT("r"));
866                                 
867                                         if (SyncLoad.IsOpened()){
868                                 
869                                                 SyncLoad.ReadAll(&SyncTokenLoad, wxMBConvUTF8());
870                                                 SyncLoad.Close();
871                                 
872                                         }
874                                         // Get a list of vCard files from the server.
876                                         // Get the list of vCard files from the ETagDB.
877                                 
878                                         std::map<wxString,ETagData> ETagDBData = ETagDBPtr->GetETagData();
879                                         
880                                         CardDAV CardDAVInstance = CardDAVIter->second;
881                                         
882                                         bool ExitLoop = FALSE;
884                                         std::map<wxString,FileSyncData> *SCListData = NULL;
885                                         
886                                         while (ExitLoop == FALSE){
887                                         
888                                                 //ServerContactList = CardDAVInstance.GetContactList(SyncTokenLoad.ToUTF8());
889                                                 ServerContactList = CardDAVIter->second.GetContactList(SyncTokenLoad.ToUTF8());
890                                                 SSLCertCollection certcol = CardDAVIter->second.GetCertificateData();
891                                 
892                                                 // Create a pointer for the std::map<wxString,FileSyncData>.
893                                 
894                                                 SCListData = &ServerContactList.ListData;
895                                 
896                                                 // Check if any errors have occured whilst getting the data.
897                                 
898                                                 int ErrorCode = CardDAVIter->second.GetResultCode();
899                                 
900                                                 if (ErrorCode != 0){
901                                         
902                                                         if (ErrorCode == 60 || ErrorCode == 51){
903                                                 
904                                                                 // Invalid SSL certificate so bring up a dialog to the user
905                                                                 // explaining what has happened and the options available.
906                                                         
907                                                                 // Stop all the timers.
908                                                                 
909                                                                 frmMainPtrGet->PauseAllTimers();
910                                                         
911                                                                 // Setup the data to be sent in the wxPostEvent command.
913                                                                 SSLInvalidCertNotifObj SSLICNProcData;
915                                                                 bool *PauseMode = new bool;
916                                                                 int SSLResult;
917                                                                 QRNotif qrn;
919                                                                 *PauseMode = TRUE;                                              
920                                                                 qrn.QResponse = &SSLResult;
921                                                                 qrn.PausePtr = PauseMode;
922                                                                 
923                                                                 SSLICNProcData.CertCollection = certcol;
924                                                                 SSLICNProcData.QRNotifData = &qrn;
925                                                                 SSLICNProcData.AccountName = AccountNameFriendly;
926                                                         
927                                                                 wxCommandEvent event(INVALIDSSLCERT);
928                                                                 event.SetClientData(&SSLICNProcData);
929                                                                 wxPostEvent(frmMainPtrGet, event);
930                                         
931                                                                 /*timespec n1, n2;
932                                                 
933                                                                 // Fall asleep until we get an response.
934                                                 
935                                                                 n1.tv_sec = 0;
936                                                                 n1.tv_nsec = 250000000L;*/
937                                         
938                                                                 while (*PauseMode == TRUE){
939                                                                         //nanosleep(&n1, &n2);
940                                                                         SleepFor(250000000);
941                                                                 }
942                                                         
943                                                                 // Process the response from the user.
944                                                         
945                                                                 if (SSLResult == 1){
946                                                                 
947                                                                         // Accept the certificate. Write the certificate into the account
948                                                                         // directory (as server.crt) - will overwrite old certificate.
950                                                                         frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, CardDAVIter->second.GetCertificateData());
951                                                                         WriteServerCertificate(AccountDir, CardDAVIter->second.GetCertificateData());
952                                                                 
953                                                                 } else if (SSLResult == 2){
954                                                                 
955                                                                         // Reject the certificate, abort the task and mark as failed.
957                                                                         frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, CardDAVIter->second.GetCertificateData());
958                                                                         iter->second = 2;
959                                                                         break;
960                                                                 
961                                                                 }
962                                                         
963                                                                 // Resume all the timers.
964                                                                 
965                                                                 frmMainPtrGet->ResumeAllTimers();
966                                                 
967                                                         } else {
968                                                         
969                                                                 iter->second = 4;
970                                                                 break;
971                                                         
972                                                         }
973                                         
974                                                 } else {
975                                         
976                                                         ExitLoop = TRUE;
977                                         
978                                                 }
979                                         
980                                         }
981                                         
982                                         if (iter->second == 2 || iter->second == 3 || iter->second == 4){
983                                                 break;
984                                         }
985                                 
986                                         frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, CardDAVIter->second.GetCertificateData());
987                                 
988                                         // Look in directory and see which vCards to add and delete
989                                 
990                                         // In short:
991                                         // vCard exists but not in ETagDB - Add (and upload).
992                                         // vCard missing but in ETagDB - Delete (from server).
993                                 
994                                         // Start by looking for vCards that are not in the ETagDB.
995                                 
996                                         wxDir AccDir(AccountDirFinal);
997                                         wxString AccDirFilename;
998                                 
999                                         bool ContinueProcess = AccDir.GetFirst(&AccDirFilename, wxEmptyString, wxDIR_DEFAULT);
1000                                 
1001                                         // Process the server data.
1003                                         if (SCListData != NULL){
1005                                                 for (std::map<wxString, FileSyncData>::iterator fsiter = SCListData->begin();
1006                                                         fsiter != SCListData->end(); fsiter++){
1008                                                         // Check the mode of the file before adding.
1010                                                         // Get the HTTP status of the file from the list.
1012                                                         FileSyncData fsd = fsiter->second;
1014                                                         if (fsd.DataFlag == 1){
1016                                                                 // Added or modified contact.
1018                                                                 //vCardAdd.Add(fsiter->first, 1);
1020                                                                 vCardDownloadDataFileClass vCardNewData;
1021                                                                 vCardNewData.DataType = 0;
1022                                                                 vCardDownloadData.insert(std::make_pair(fsiter->first, vCardNewData));
1024                                                                 //} else if (fsd.DataFlag == 1){
1026                                                                 // Contact has been modified on the server.
1028                                                                 //      vCardUpdateLocal.Add(fsiter->first, 1);
1030                                                         }
1031                                                         else if (fsd.DataFlag == 2){
1033                                                                 // Contact has been deleted.
1035                                                                 vCardDelete.Add(fsiter->first, 1);
1037                                                         }
1039                                                 }
1041                                         }
1042                                 
1043                                         // Look for vCards that are missing but in the ETagDB.
1044         
1045                                         for (std::map<wxString,ETagData>::iterator etagiter = ETagDBData.begin();
1046                                                 etagiter != ETagDBData.end(); etagiter++){
1047                                 
1048                                                 // Check if the vCard exists.
1049                                         
1050                                                 if (!wxFileExists(AccountDirFinal + etagiter->first)){
1051                                         
1052                                                         // vCard doesn't exist so mark for deleting.
1053                                         
1054                                                         vCardDelete.Add(etagiter->first, 1);
1055                                         
1056                                                 }
1057                                 
1058                                         }
1060                                         // Compare Server with the ETag DB. Work out which
1061                                         // needs to updated remotely and which needs to be
1062                                         // updated locally.
1064                                         for (std::map<wxString,FileSyncData>::iterator srviter = SCListData->begin();
1065                                                 srviter != SCListData->end(); srviter++){
1067                                                 if (ETagDBData.find(srviter->first) == ETagDBData.end()){
1068                                                         continue;
1069                                                 }
1070                                         
1071                                                 std::map<wxString,ETagData>::iterator lociter = ETagDBData.find(srviter->first);
1072                                 
1073                                                 ETagData etd = lociter->second;
1074                                                 FileSyncData fsd = srviter->second;
1075                                         
1076                                                 wxString ETagSrv = fsd.ETagData;
1078                                                 // Compare local ETag with original local ETag.
1079                                                 // If different, update to server is required.
1080                                 
1081                                                 if (etd.ETagValue != etd.ETagOrigValue){
1082                                 
1083                                                         // Check if local original ETag is different from server ETag.
1084                                                         // If it is then throw up a conflict resolution dialog.
1085                                 
1086                                                         if (etd.ETagOrigValue != ETagSrv){
1087                                         
1088                                                                 vCard34Conv vCard34Obj;
1089                                                 
1090                                                                 wxString ServerContactData;
1091                                                                 wxString FullFilename;
1092                                                                 vCard ClientData;
1093                                                                 vCard ConvertedV4Data;
1094                                         
1095                                                                 // Conflict. Bring up conflict resolution dialog.
1096                                                         
1097                                                                 // Get the server contact data for comparison.
1098                                                         
1099                                                                 CardDAVIter->second.SetServerFilename(srviter->first);
1100                                                                 CardDAVIter->second.GetServerContactData();
1101                                                                 ServerContactData = CardDAVIter->second.GetPageData();
1102                                                         
1103                                                                 FullFilename = CreateFilenamePath(AccountDirFinal, srviter->first);
1104                                                         
1105                                                                 // Convert it to v4.
1106                                         
1107                                                                 vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data);
1108                                         
1109                                                                 vCardConflictObj vCardProcData;
1110                                         
1111                                                                 vCardProcData.vCardLocalData = &ClientData;
1112                                                                 vCardProcData.vCardServerData = &ConvertedV4Data;
1113                                                         
1114                                                                 // Load the client data from the filename given.
1115                                                         
1116                                                                 ClientData.LoadFile(FullFilename);
1117                                                         
1118                                                                 // Show dialog.
1119                                         
1120                                                                 bool *PauseMode = new bool;
1121                                                                 int ConflictResult;
1122                                                                 QRNotif qrn;
1123                                                                 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
1125                                                                 *PauseMode = TRUE;
1126                                                                 qrn.QResponse = &ConflictResult;
1127                                                                 qrn.PausePtr = PauseMode;
1128                                                 
1129                                                                 vCardProcData.QRNotifData = &qrn;
1130                                         
1131                                                                 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
1132                                                                 event.SetClientData(&vCardProcData);
1133                                                                 wxPostEvent(frmMainPtrGet, event);
1134                                                 
1135                                                                 /*timespec n1, n2;
1136                                                 
1137                                                                 n1.tv_sec = 0;
1138                                                                 n1.tv_nsec = 250000000L;*/
1139                                                 
1140                                                                 while (*PauseMode == TRUE){
1141                                                                         //nanosleep(&n1, &n2);
1142                                                                         SleepFor(250000000);
1143                                                                 }
1144                                         
1145                                                                 delete PauseMode;
1146                                                                 PauseMode = NULL;
1147                                                         
1148                                                                 // If replacing local version, store copy for now.
1149                                                                 // Otherwise upload to server.
1150                                         
1151                                                                 if (ConflictResult == 0){
1152                                         
1153                                                                         // Decide Later.
1154                                         
1155                                                                         continue;
1156                                         
1157                                                                 } else if (ConflictResult == 1){
1158                                         
1159                                                                         // Decided to use client version.
1160                                         
1161                                                                         vCardUpdateLocal.Remove(srviter->first);
1162                                                                         vCardUpdateRemote.Add(srviter->first, 1);                                                               
1163                                         
1164                                                                 } else if (ConflictResult == 2){
1165                                         
1166                                                                         // Decided to use server version.
1167                                                                         // Store the converted downloaded copy for
1168                                                                         // processing later.
1170                                                                         //vCardUpdateLocal.Add(srviter->first, 1);
1171                                                                 
1172                                                                         vCardDownloadDataFileClass vCardNewData;
1173                                                                         vCardNewData.DataType = 1;
1174                                                                         vCardNewData.FileData = ConvertedV4Data.WriteString();
1176                                                                         ConvertedV4Data.WriteFile(FullFilename);
1177                                                                 
1178                                                                         vCardDownloadData.insert(std::make_pair(srviter->first, vCardNewData)); 
1179                                                                 
1180                                                                 
1181                                                                 
1182                                                                 }
1183                                         
1184                                                         } else {
1185                                         
1186                                                                 // Send vCard to the server.
1187                                                 
1188                                                                 vCardUpdateRemote.Add(srviter->first, 1);
1189                                         
1190                                                         }
1191                                 
1192                                                 } else {
1193                                 
1194                                                         // Check if local ETag is different from server ETag.
1195                                                         // If different, obtain the vCard from the server.
1196                                         
1197                                                         if (etd.ETagOrigValue != ETagSrv){
1198                                         
1199                                                                 vCardUpdateLocal.Add(srviter->first, 1);
1200                                         
1201                                                         }
1202                                 
1203                                                 }
1204                         
1205                                         }
1206                                 
1207                                         // Process the vCards that need to be downloaded and added/overwritten.
1208                                 
1209                                         wxString vCardFilenameFinal;
1210                                         wxString vCardOutputData;
1211                                         wxString vCardInputData;
1212                                         wxString ETagFileData;
1213                                         wxFFile vCardFile;
1214                                 
1215                                         for (std::map<wxString,vCardDownloadDataFileClass>::iterator additer = vCardDownloadData.begin();
1216                                                 additer != vCardDownloadData.end(); additer++){
1217                                         
1218                                                 vCardFilenameFinal.Clear();
1219                                                 vCardInputData.Clear();
1220                                                 vCardOutputData.Clear();
1221                                                 ETagFileData.Clear();
1222                                         
1223                                                 vCardFilenameFinal = CreateFilenamePath(AccountDirFinal, additer->first);
1224                                         
1225                                                 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
1226                                         
1227                                                 if (additer->second.DataType == 0){
1228                                         
1229                                                         vCard ConvertedV4Data;
1230                                         
1231                                                         // Grab the file from the server and write the contact out.
1232                                         
1233                                                         vCard34Conv vCard34Obj;
1234                                                 
1235                                                         CardDAVIter->second.SetServerFilename(additer->first);
1236                                                         CardDAVIter->second.GetServerContactData();
1237                                                         vCardInputData = CardDAVIter->second.GetPageData();
1238                                                 
1239                                                         vCard34Obj.ConvertToV4(&vCardInputData, &ConvertedV4Data);
1240                                                 
1241                                                         ConvertedV4Data.WriteFile(vCardFilenameFinal);
1242                                                 
1243                                                         // Update the ETagDB.
1244                                                 
1245                                                         //vCardFile.Open(vCardFilenameFinal, wxT("w"));
1246                                 
1247                                                         //if (vCardFile.IsOpened()){
1248                                 
1249                                                         //      vCardFile.Write(vCardOutputData, wxConvAuto());
1250                                                         //      vCardFile.Close();
1251                                 
1252                                                         //}
1253                                                 
1254                                                         // Get the server ETag.
1255                                                 
1256                                                         wxString ServerETag;
1257                                                         CardDAVIter->second.GetServerETagValueThread();
1258                                                         ServerETag = CardDAVIter->second.ETagValueResult();
1259                                                 
1260                                                         // Add to the ETag Database.
1261                                                 
1262                                                         ETagDBPtr->AddETag(additer->first, ServerETag, ServerETag);
1263                                                 
1264                                                 } else if (additer->second.DataType == 1){
1265                                         
1266                                                         // Write out the contact to the account directory.
1267                                         
1268                                                         vCardFilenameFinal = CreateFilenamePath(AccountDirFinal, additer->first);
1269                                                         vCardOutputData = additer->second.FileData;
1270                                                 
1271                                                         vCardFile.Open(vCardFilenameFinal, wxT("w"));
1272                                 
1273                                                         if (vCardFile.IsOpened()){
1274                                 
1275                                                                 vCardFile.Write(vCardOutputData, wxConvAuto());
1276                                                                 vCardFile.Close();
1277                                 
1278                                                         }
1279                                                 
1280                                                         // Update the ETagDB.
1282                                                         wxString ServerETag;
1283                                                         CardDAVIter->second.GetServerETagValueThread();
1284                                                         ServerETag = CardDAVIter->second.ETagValueResult();
1285                                                 
1286                                                         // Add to the ETag Database.
1287                                                 
1288                                                         ETagDBPtr->AddETag(additer->first, ServerETag, ServerETag);
1289                                                                                         
1290                                                 }
1291                                         }
1292         
1293                                         // Look for vCard files which aren't in the ETagDB.
1294         
1295                                         while(ContinueProcess){
1296                                 
1297                                                 // Check and make sure that the filename ends in .vcf/.vcard
1298                                                 // Skip any file that beings with a dot.
1299                                         
1300                                                 if (AccDirFilename.Left(1) == wxT(".")){
1301                                         
1302                                                         ContinueProcess = AccDir.GetNext(&AccDirFilename);
1303                                                         continue;
1304                                         
1305                                                 }
1306                                         
1307                                                 if (AccDirFilename.Right(4) != wxT(".vcf") && 
1308                                                 AccDirFilename.Right(6) != wxT(".vcard")){
1309                                         
1310                                                         ContinueProcess = AccDir.GetNext(&AccDirFilename);
1311                                                         continue;
1312                                         
1313                                                 }
1314                                 
1315                                                 // Look in the ETagDB for the file.
1316                                         
1317                                                 bool ETagExists = ETagDBPtr->CheckETagExists(AccDirFilename);
1318                                         
1319                                                 if (ETagExists == FALSE){
1320                                         
1321                                                         // Add the vCard to the list to be added.
1322                                                 
1323                                                         vCardAdd.Add(AccDirFilename, 1);
1324                                         
1325                                                 }
1326                                         
1327                                                 ContinueProcess = AccDir.GetNext(&AccDirFilename);
1328                                 
1329                                         }
1330                                 
1331                                         // Process the vCards to add/update to the server.
1332                                 
1333                                         for (int vi = 0; vi < vCardAdd.GetCount(); vi++){
1335                                                 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
1336                                                 ETag = ETagDBPtr->GetETag(vCardAdd[vi]);
1337                                                 ETagOriginal = ETagDBPtr->GetETagOriginal(vCardAdd[vi]);
1338                                                 
1339                                                 ContactFilename = vCardAdd[vi];
1340                                                 FullFilename = CreateFilenamePath(AccountDirFinal, vCardAdd[vi]);
1342                                                 ContactFilename.Trim();
1343                         
1344                                                 if (ETag == ETagOriginal && ETagDBPtr->ETagExists(vCardAdd[vi])){
1345                         
1346                                                         // Compare the ETag with the Server ETag.
1347                                                 
1348                                                         CardDAVIter->second.SetServerFilename(vCardAdd[vi]);
1349                                                         CardDAVIter->second.GetServerETagValueThread();
1350                                                         ETagServer = CardDAVIter->second.ETagValueResult();
1351                                 
1352                                                         if (ETagOriginal != ETagServer){
1353                                 
1354                                                                 // Server ETag is different from original ETag.
1355                                                                 // This is a conflict which the user will now
1356                                                                 // need to resolve.
1357                                         
1358                                                                 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
1359                                         
1360                                                                 vCard34Conv vCard34Obj;
1361                                                 
1362                                                                 wxString ServerContactData;
1363                                                                 vCard ClientData;
1364                                                                 vCard ConvertedV4Data;
1365                                         
1366                                                                 // Load the client data from the filename given.
1367                                         
1368                                                                 CardDAVIter->second.GetServerContactData();
1369                                                                 ServerContactData = CardDAVIter->second.GetPageData();
1370                                                 
1371                                                                 // Process v3 version into v4 version.
1372                                                 
1373                                                                 vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data);
1374                                         
1375                                                                 vCardConflictObj vCardProcData;
1376                                         
1377                                                                 vCardProcData.vCardLocalData = &ClientData;
1378                                                                 vCardProcData.vCardServerData = &ConvertedV4Data;
1379                                                                 ClientData.LoadFile(FullFilename);
1381                                                                 //ContactConflictEvent event(ContactConflictCmdEnv);
1382                                                                 //event.SetData(ContactFilename, Moo1, Moo2);
1383                                         
1384                                                                 //wxPostEvent(frmMainPtrGet, event);
1385                                         
1386                                                                 // Setup Conflict Resolution Dialog.
1387                                         
1388                                                                 // Fetch Data from Server and convert.
1389                                         
1390                                                                 bool *PauseMode = new bool;
1391                                                                 int ConflictResult;
1392                                                                 QRNotif qrn;
1394                                                                 *PauseMode = TRUE;                                              
1395                                                                 qrn.QResponse = &ConflictResult;
1396                                                                 qrn.PausePtr = PauseMode;
1397                                                 
1398                                                                 vCardProcData.QRNotifData = &qrn;
1399                                         
1400                                                                 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
1401                                                                 event.SetClientData(&vCardProcData);
1402                                                                 wxPostEvent(frmMainPtrGet, event);
1403                                                 
1404                                                                 /*timespec n1, n2;
1405                                                 
1406                                                                 n1.tv_sec = 0;
1407                                                                 n1.tv_nsec = 250000000L;*/
1408                                                 
1409                                                                 while (*PauseMode == TRUE){
1410                                                                         //nanosleep(&n1, &n2);
1411                                                                         SleepFor(250000000);
1412                                                                 }
1413                                         
1414                                                                 delete PauseMode;
1415                                                                 PauseMode = NULL;
1416                                         
1417                                                                 //intResult = frmMainPtrGet->ShowConflictDialog(&Moo1, &Moo2);
1418                                         
1419                                                                 if (ConflictResult == 0){
1420                                         
1421                                                                         // Decide Later.
1422                                         
1423                                                                         iter->second = 4;
1424                                                                         continue;
1425                                         
1426                                                                 } else if (ConflictResult == 1){
1427                                         
1428                                                                         // Decided to use client version.
1429                                         
1430                                                                         CardDAVIter->second.ProcessData();
1431                                                 
1432                                                                         iter->second = 4;
1433                                         
1434                                                                 } else if (ConflictResult == 2){
1435                                         
1436                                                                         // Decided to use server version.
1437                                                 
1438                                                                         // Download server version.
1439                                                                 
1440                                                                         ConvertedV4Data.WriteFile(CreateFilenamePath(AccountDirFinal, vCardAdd[vi]));
1441                                                 
1442                                                                         iter->second = 4;
1443                                         
1444                                                                 }
1445                                 
1446                                                         } else {
1447                                 
1448                                                                 // Setup the filename for the contact being added.
1449                                                         
1450                                                                 wxString wxSFilename = CreateFilenamePath(AccountDirFinal, vCardAdd[vi]);
1451                                                                 wxString wxSCardData;
1452                                                                 wxString TaskData;
1453                                                                 //wxString TaskURL;
1454                                                         
1455                                                                 // Load the data from the file.
1456                                                         
1457                                                                 wxFFile wxFCard;
1458                                                                 wxFCard.Open(wxSFilename, wxT("r"));
1459                                 
1460                                                                 if (wxFCard.IsOpened()){
1461                                 
1462                                                                         wxFCard.ReadAll(&wxSCardData, wxConvAuto());
1463                                                                         wxFCard.Close();
1464                                 
1465                                                                 }
1466                                                         
1467                                                                 // Convert the data to v3.
1468                                                         
1469                                                                 vCard34Conv vCard34Obj;
1470                                                         
1471                                                                 vCard34Obj.ConvertToV3(wxSFilename, &TaskData);
1472                                                         
1473                                                                 // Setup the URL address.
1474                                                         
1475                                                                 //TaskURL = StringTaskDetailIter->second + wxT("/") + vCardAdd[vi];
1477                                                                 // Upload the data to the server.
1478                                         
1479                                                                 CardDAVIter->second.SetUploadMode(TRUE);
1480                                                                 CardDAVIter->second.SetupData(wxT("PUT"), vCardAdd[vi], TaskData);
1481                                                                 CardDAVIter->second.ProcessData();
1482                                                         
1483                                                                 // Get the Server ETag and put it in the ETag database.
1484                                                         
1485                                                                 CardDAVIter->second.SetServerFilename(vCardAdd[vi]);
1486                                                                 CardDAVIter->second.GetServerETagValueThread();
1487                                                                 ETagServer = CardDAVIter->second.ETagValueResult();
1488                                 
1489                                                                 ETagDBPtr->AddETag(vCardAdd[vi], ETagServer, ETagServer);
1490                                 
1491                                                         }
1492                         
1493                                                 } else {
1494                         
1495                                                         if (ETagOriginal != ETagServer){
1496                                 
1497                                                                 // Server ETag is different from original ETag.
1498                                                                 // This is a conflict which the user will now
1499                                                                 // need to resolve.
1500                                         
1501                                                                 frmMain *frmMainPtrGet = (frmMain *)frmMainPtr;
1502                                         
1503                                                                 //ContactConflictEvent event(ContactConflictCmdEnv);
1504                                                                 //event.SetData(ContactFilename, Moo1, Moo2);
1505                                         
1506                                                                 vCard34Conv vCard34Obj;
1507                                                 
1508                                                                 wxString ServerContactData;
1509                                                                 vCard ClientData;
1510                                                                 vCard ConvertedV4Data;
1511                                         
1512                                                                 // Load the client data from the filename given.
1513                                         
1514                                                                 CardDAVIter->second.GetServerContactData();
1515                                                                 ServerContactData = CardDAVIter->second.GetPageData();
1516                                                                 ClientData.LoadFile(FullFilename);
1517                                                 
1518                                                                 // Process v3 version into v4 version.
1519                                                 
1520                                                                 vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data);
1521                                         
1522                                                                 vCardConflictObj vCardProcData;
1523                                         
1524                                                                 vCardProcData.vCardLocalData = &ClientData;
1525                                                                 vCardProcData.vCardServerData = &ConvertedV4Data;
1526                                         
1527                                                                 bool *PauseMode = new bool;
1528                                                                 int ConflictResult;
1529                                                                 QRNotif qrn;
1531                                                                 *PauseMode = TRUE;                                              
1532                                                                 qrn.QResponse = &ConflictResult;
1533                                                                 qrn.PausePtr = PauseMode;
1534                                                 
1535                                                                 vCardProcData.QRNotifData = &qrn;
1536                                         
1537                                                                 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
1538                                                                 event.SetClientData(&vCardProcData);
1539                                                                 wxPostEvent(frmMainPtrGet, event);
1540                                         
1541                                                                 /*timespec n1, n2;
1542                                                 
1543                                                                 n1.tv_sec = 0;
1544                                                                 n1.tv_nsec = 250000000L;*/
1545                                         
1546                                                                 while (*PauseMode == TRUE){
1547                                                                         //nanosleep(&n1, &n2);
1548                                                                         SleepFor(250000000);
1549                                                                 }
1550                                         
1551                                                                 delete PauseMode;
1552                                                                 PauseMode = NULL;
1553                                         
1554                                                                 //intResult = frmMainPtrGet->ShowConflictDialog(&Moo1, &Moo2);
1555                                         
1556                                                                 if (ConflictResult == 0){
1557                                         
1558                                                                         // Decide Later.
1559                                         
1560                                                                         iter->second = 4;
1561                                                                         continue;
1562                                         
1563                                                                 } else if (ConflictResult == 1){
1564                                         
1565                                                                         // Decided to use client version.
1566                                                                 
1567                                                                         CardDAVIter->second.ProcessData();
1568                                                                         //ETagServer = CardDAVIter->second.GetServerETagValueThread();
1569                                                                         //ETagDBPtr->UpdateETag(ContactFilename, ETagServer, ETagServer);
1570                                                 
1571                                                                         iter->second = 4;
1572                                         
1573                                                                 } else if (ConflictResult == 2){
1574                                         
1575                                                                         // Decided to use server version.
1576                                                 
1577                                                                         // Write server version to file.
1578                                                                 
1579                                                                         ConvertedV4Data.WriteFile(CreateFilenamePath(AccountDirFinal, vCardAdd[vi]));
1580                                                 
1581                                                                         iter->second = 4;
1582                                         
1583                                                                 }
1584                                 
1585                                                         } else {
1586                                 
1587                                                                 // Setup the filename for the contact being added.
1588                                                         
1589                                                                 wxString wxSFilename = CreateFilenamePath(AccountDirFinal, vCardAdd[vi]);
1590                                                                 wxString wxSCardData;
1591                                                                 wxString TaskData;
1592                                                                 //wxString TaskURL;
1593                                                         
1594                                                                 // Load the data from the file.
1595                                                         
1596                                                                 wxFFile wxFCard;
1597                                                                 wxFCard.Open(wxSFilename, wxT("r"));
1598                                 
1599                                                                 if (wxFCard.IsOpened()){
1600                                 
1601                                                                         wxFCard.ReadAll(&wxSCardData, wxConvAuto());
1602                                                                         wxFCard.Close();
1603                                 
1604                                                                 }
1605                                                         
1606                                                                 // Convert the data to v3.
1607                                                         
1608                                                                 vCard34Conv vCard34Obj;
1609                                                         
1610                                                                 vCard34Obj.ConvertToV3(wxSFilename, &TaskData);
1611                                                         
1612                                                                 // Setup the URL address.
1613                                                         
1614                                                                 //TaskURL = StringTaskDetailIter->second + wxT("/") + vCardAdd[vi];
1616                                                                 // Upload the data to the server.
1617                                         
1618                                                                 CardDAVIter->second.SetUploadMode(TRUE);
1619                                                                 CardDAVIter->second.SetupData(wxT("PUT"), vCardAdd[vi], TaskData);
1620                                                                 CardDAVIter->second.ProcessData();
1621                                                         
1622                                                                 // Get the Server ETag and put it in the ETag database.
1623                                                         
1624                                                                 CardDAVIter->second.SetServerFilename(vCardAdd[vi]);
1625                                                                 CardDAVIter->second.GetServerETagValueThread();
1626                                                                 ETagServer = CardDAVIter->second.ETagValueResult();
1627                                 
1628                                 
1629                                                                 ETagDBPtr->AddETag(vCardAdd[vi], ETagServer, ETagServer);
1630                                 
1631                                                         }
1632                                         
1633                                                 }
1635                                         }                       
1636                                 
1637                                         // Process the vCards to delete locally.
1638                                 
1639                                         for (int vi = 0; vi < vCardDelete.GetCount(); vi++){
1641                                                 if (!wxRemoveFile(CreateFilenamePath(AccountDirFinal, vCardDelete[vi]))){
1643                                                 } else {
1644                                         
1645                                                         ETagDBPtr->RemoveETag(vCardDelete[vi]);
1646                                         
1647                                                 }
1649                                         }
1650                                 
1651                                         // Process the vCards to delete from the server.
1652                                 
1653                                         std::map<wxString,wxString> *ETagFilenameMap = ETagDBPtr->GetFilenamePointer();
1654                                 
1655                                 
1656                                         for (std::map<wxString,wxString>::iterator deliter = ETagFilenameMap->begin();
1657                                                 deliter != ETagFilenameMap->end(); deliter++){
1658                                 
1659                                                 // Look through the ETagDB and find contacts which are marked as DELETED.
1660                                         
1661                                                 if (deliter->second == wxT("DELETED")){
1663                                                         CardDAVIter->second.SetUploadMode(FALSE);
1664                                                         CardDAVIter->second.SetupData(wxT("DELETE"), deliter->first, wxT(""));
1665                                                         CardDAVIter->second.ProcessData();
1666                                         
1667                                                         // Delete the ETag as the file has now been deleted.
1668                                         
1669                                                         ETagDBPtr->RemoveETag(deliter->first);
1670                                         
1671                                                 }
1672                                 
1673                                         }
1674                                 
1675                                         // Write out the update server sync token.
1677                                         wxString ServerSyncToken = ServerContactList.SyncToken;
1678                                 
1679                                         ServerSyncToken.Trim();
1680                                 
1681                                         if (!ServerSyncToken.IsEmpty()){
1682                                 
1683                                                 wxFFile SyncWrite;
1684                                                 SyncWrite.Open(AccountSyncTokenFilename, wxT("w"));
1685                                 
1686                                                 if (SyncWrite.IsOpened()){
1687                                 
1688                                                         SyncWrite.Write(ServerSyncToken, wxConvAuto());
1689                                                         SyncWrite.Close();
1690                                 
1691                                                 }
1692                                 
1693                                         }
1694                                 
1695                                         // Check for another sync token. If there is another 
1696                                         // sync token then repeat this. If token matches then stop processing.
1697                                 
1698                                         if (SyncTokenLoad == ServerSyncToken){
1699                                         
1700                                                 KeepUpdating = FALSE;
1701                                         
1702                                         }
1703                                         
1704                                 }
1705                                 
1706                                 // Mark as completed.
1707                                 
1708                                 iter->second = 4;
1709                                 continue;
1710                         
1711                         } else {
1712                         
1713                                 // Update the data to the server.
1714                                         
1715                                 CardDAVIter->second.ProcessData();
1716                         
1717                         }
1719                         /*timespec n1, n2;
1720                                                 
1721                         n1.tv_sec = 0;
1722                         n1.tv_nsec = 250000000L;*/
1724                         while (iter->second == 1){
1726                                 SleepFor(250000000);
1727                                 //nanosleep(&n1, &n2);
1728         
1729                         }
1730                         
1731                         if ((iter->second == 4 && TypeIter->second == 0) || 
1732                         (iter->second == 8 && TypeIter->second == 0)){
1734                                 AccountDir = StringAccountIter->second;
1735                                 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
1736                                 
1737                                 CardDAVIter->second.GetServerETagValueThread();
1738                                 ETagServer = CardDAVIter->second.ETagValueResult();
1739                                 ETagDBPtr->UpdateETag(ContactFilename, ETagServer, ETagServer);
1740                         
1741                         }
1742                         
1743                         if ((iter->second == 4 && TypeIter->second == 1) || 
1744                         (iter->second == 8 && TypeIter->second == 1)){
1745                         
1746                                 AccountDir = StringAccountIter->second;
1747                                 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);                 
1749                                 CardDAVIter->second.GetServerETagValueThread();
1750                                 ETagServer = CardDAVIter->second.ETagValueResult();
1751                                 ETagDBPtr->UpdateETag(ContactFilename, ETagServer, ETagServer);
1752                         
1753                         }
1754                         
1755                         break;
1756                         
1757                 }
1759         }
1760         
1761         if (TasksFoundProc == FALSE){
1762         
1763                 TasksActive = FALSE;
1764                 ProcessTasksData = FALSE;
1765         
1766         } else {
1768                 wxCommandEvent eventp(ACTMGR_STARTTIMER);
1769                 wxPostEvent(this, eventp);
1771                 //AnimationTimer.StartAnimation();
1772                 //ActListProcTimer.Start();
1773         
1774         }
1776         wxCommandEvent event(ACTMGR_STARTPROCESSTIMER);
1777         wxPostEvent(this, event);
1779         //ActProcessTimer.Start();
1783 void frmActivityMgr::ProcessTasks()
1786         std::thread ProcessThread(&frmActivityMgr::ProcessTasksThread, this);
1787         ProcessThread.detach();
1791 void frmActivityMgr::SetupPointers(wxETagProcessTimer *ETagTmrPtrInc, void *frmMainPtrInc)
1794         ETagTmrPtr = ETagTmrPtrInc;
1795         
1796         frmMainPtr = frmMainPtrInc;
1800 void frmActivityMgr::ResumeProcessing(wxCommandEvent &event){
1802         QRNotif *qrn = (QRNotif *)event.GetClientData(); 
1803         
1804         int *QRes = qrn->QResponse;
1806         *qrn->PausePtr = FALSE;
1808         *QRes = event.GetInt();
1809         
1812 int frmActivityMgr::GetTaskStatus(int intTaskID){
1814         int intResult = 2;
1815         
1816         if (ActivityList.find(intTaskID) == ActivityList.end()){
1817         
1818                 return intResult;
1819         
1820         }
1821         
1822         std::map<int,int>::iterator taskiter = ActivityList.find(intTaskID);
1823         intResult = taskiter->second;
1824         
1825         return intResult;
1829 void frmActivityMgr::ToggleConnectionStatus( wxCommandEvent &event ){
1831         wxCommandEvent connevent(CONNSTAT_UPDATE);
1832         
1833         if (SleepMode == FALSE){
1834         
1835                 connevent.SetInt(1);
1836                 SleepMode = TRUE;
1837         
1838         } else {
1839         
1840                 connevent.SetInt(0);
1841                 SleepMode = FALSE;
1842         
1843         }
1845         wxPostEvent(GetParent(), connevent);
1849 void frmActivityMgr::WriteServerCertificate(wxString AccountName, SSLCertCollection SSLCertInc){
1851         wxString ServerCertFinal;
1853         // Get the Cert section of the certificate and write it to the file.
1855         ServerCertFinal = GetAccountDir(AccountName, TRUE);
1857         wxFile CertFile;
1858         
1859         std::map<int, SSLCertData>::iterator SSLCDIter = SSLCertInc.SSLCollection.find(0);
1860         std::multimap<wxString,wxString>::iterator SSLDataIter = SSLCDIter->second.CertData.find(wxT("Cert"));
1861         
1862         CertFile.Open(ServerCertFinal, wxFile::write);  
1863         
1864         CertFile.Write(SSLDataIter->second, wxConvUTF8);
1865         CertFile.Close();
1869 void frmActivityMgr::StartTimer(wxCommandEvent& event){
1871         ActListProcTimer.Start(1000, FALSE);
1875 void frmActivityMgr::StopTimer(wxCommandEvent& event){
1877         ActListProcTimer.Stop();
1881 void frmActivityMgr::StartProcessTimer(wxCommandEvent& event){
1883         ActProcessTimer.Start(1000, FALSE);
1887 void frmActivityMgr::StopProcessTimer(wxCommandEvent& event){
1889         ActProcessTimer.Stop();
1893 void frmActivityMgr::UpdateStatusLabel(wxCommandEvent& event){
1895         ActivityMgrLabelUpdate *actmgrudata = (ActivityMgrLabelUpdate*)event.GetClientData();
1896         
1897         if (actmgrudata != NULL){
1898         
1899         wxPuts(wxT("ItemIndex:") + actmgrudata->ItemIndex);
1900         wxPuts(wxT("ItemLabel:") + actmgrudata->ItemLabel);
1901         wxPuts(wxT("ItemDes:  ") + actmgrudata->ItemDes);
1902         
1903         lstActivity->SetItem(actmgrudata->ItemIndex, 2, actmgrudata->ItemLabel);
1904         
1905         /*if (!actmgrudata->ItemDes.IsEmpty()){
1906                 lstActivity->SetItem(actmgrudata->ItemIndex, 0, actmgrudata->ItemDes);
1907         }*/
1908         
1909         delete actmgrudata;
1910         actmgrudata = NULL;
1911         
1912         }
1916 #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