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