1 // timers.cpp - Timers subroutines.
3 // (c) 2012-2015 Xestia Software Development.
5 // This file is part of Xestia Address Book.
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.
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.
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 "../actmgr/frmActivityMgr.h"
20 #include "../frmContact.h"
21 #include "../frmMain.h"
22 #include <wx/mstream.h>
25 #include <wx/filename.h>
26 #include <wx/tokenzr.h>
27 #include "../bitmaps.h"
29 #include "../common/preferences.h"
30 #include "../common/etag.h"
31 #include "../common/dirs.h"
33 void wxActTimer::Notify(){
35 frmMain *frmMainPtrProc = static_cast<frmMain*>(frmMainPtr);
37 wxCommandEvent shuffleevent(ACTMGR_SHUFFLE);
39 if (intActivityProgress == 0){
41 wxMemoryInputStream istream(misc_activity1_png, sizeof(misc_activity1_png));
42 wxImage misc_activity1_pngi(istream, wxBITMAP_TYPE_PNG);
43 wxBitmap activityiconbmp(misc_activity1_pngi, -1);
44 bmpActivity->SetBitmap(activityiconbmp);
46 intActivityProgress++;
48 } else if (intActivityProgress == 1){
50 wxMemoryInputStream istream(misc_activity2_png, sizeof(misc_activity2_png));
51 wxImage misc_activity2_pngi(istream, wxBITMAP_TYPE_PNG);
52 wxBitmap activityiconbmp(misc_activity2_pngi, -1);
53 bmpActivity->SetBitmap(activityiconbmp);
55 intActivityProgress++;
57 } else if (intActivityProgress == 2){
59 wxMemoryInputStream istream(misc_activity3_png, sizeof(misc_activity3_png));
60 wxImage misc_activity3_pngi(istream, wxBITMAP_TYPE_PNG);
61 wxBitmap activityiconbmp(misc_activity3_pngi, -1);
62 bmpActivity->SetBitmap(activityiconbmp);
64 intActivityProgress++;
66 } else if (intActivityProgress == 3){
68 wxMemoryInputStream istream(misc_activity4_png, sizeof(misc_activity4_png));
69 wxImage misc_activity4_pngi(istream, wxBITMAP_TYPE_PNG);
70 wxBitmap activityiconbmp(misc_activity4_pngi, -1);
71 bmpActivity->SetBitmap(activityiconbmp);
73 intActivityProgress++;
75 } else if (intActivityProgress == 4){
77 wxMemoryInputStream istream(misc_activity5_png, sizeof(misc_activity5_png));
78 wxImage misc_activity5_pngi(istream, wxBITMAP_TYPE_PNG);
79 wxBitmap activityiconbmp(misc_activity5_pngi, -1);
80 bmpActivity->SetBitmap(activityiconbmp);
82 intActivityProgress++;
84 } else if (intActivityProgress == 5){
86 wxMemoryInputStream istream(misc_activity6_png, sizeof(misc_activity6_png));
87 wxImage misc_activity6_pngi(istream, wxBITMAP_TYPE_PNG);
88 wxBitmap activityiconbmp(misc_activity6_pngi, -1);
89 bmpActivity->SetBitmap(activityiconbmp);
91 intActivityProgress++;
93 } else if (intActivityProgress == 6){
95 wxMemoryInputStream istream(misc_activity7_png, sizeof(misc_activity7_png));
96 wxImage misc_activity7_pngi(istream, wxBITMAP_TYPE_PNG);
97 wxBitmap activityiconbmp(misc_activity7_pngi, -1);
98 bmpActivity->SetBitmap(activityiconbmp);
100 intActivityProgress++;
102 } else if (intActivityProgress == 7){
104 wxMemoryInputStream istream(misc_activity8_png, sizeof(misc_activity8_png));
105 wxImage misc_activity8_pngi(istream, wxBITMAP_TYPE_PNG);
106 wxBitmap activityiconbmp(misc_activity8_pngi, -1);
107 bmpActivity->SetBitmap(activityiconbmp);
109 intActivityProgress++;
111 } else if (intActivityProgress == 8){
113 wxMemoryInputStream istream(misc_activity9_png, sizeof(misc_activity9_png));
114 wxImage misc_activity9_pngi(istream, wxBITMAP_TYPE_PNG);
115 wxBitmap activityiconbmp(misc_activity9_pngi, -1);
116 bmpActivity->SetBitmap(activityiconbmp);
118 intActivityProgress++;
120 } else if (intActivityProgress == 9){
122 wxMemoryInputStream istream(misc_activity10_png, sizeof(misc_activity10_png));
123 wxImage misc_activity10_pngi(istream, wxBITMAP_TYPE_PNG);
124 wxBitmap activityiconbmp(misc_activity10_pngi, -1);
125 bmpActivity->SetBitmap(activityiconbmp);
127 intActivityProgress++;
129 } else if (intActivityProgress == 10){
131 wxMemoryInputStream istream(misc_activity11_png, sizeof(misc_activity11_png));
132 wxImage misc_activity11_pngi(istream, wxBITMAP_TYPE_PNG);
133 wxBitmap activityiconbmp(misc_activity11_pngi, -1);
134 bmpActivity->SetBitmap(activityiconbmp);
136 intActivityProgress++;
138 } else if (intActivityProgress == 11){
140 wxMemoryInputStream istream(misc_activity12_png, sizeof(misc_activity12_png));
141 wxImage misc_activity12_pngi(istream, wxBITMAP_TYPE_PNG);
142 wxBitmap activityiconbmp(misc_activity12_pngi, -1);
143 bmpActivity->SetBitmap(activityiconbmp);
145 intActivityProgress = 0;
149 wxPostEvent(frmMainPtrProc, shuffleevent);
153 void wxActTimer::SetBitmap(wxStaticBitmap *bitmap)
156 bmpActivity = bitmap;
160 void wxActTimer::SetSleepBitmap(wxBitmap *SleepBitmap)
163 SleepBitmapPtr = SleepBitmap;
167 void wxActTimer::StopAnimation()
170 // Stop the icon animation.
172 frmMain *frmMainPtrProc = static_cast<frmMain*>(frmMainPtr);
174 wxCommandEvent actstop(ACTMGR_STOP);
175 wxPostEvent(frmMainPtrProc, actstop);
176 //frmMainPtrProc->ActivityIconStop();
179 bmpActivity->SetBitmap(*SleepBitmapPtr);
182 void wxActTimer::StartAnimation()
185 frmMain *frmMainPtrProc = static_cast<frmMain*>(frmMainPtr);
186 wxCommandEvent actstart(ACTMGR_START);
187 wxPostEvent(frmMainPtrProc, actstart);
188 //frmMainPtrProc->ActivityIconStart();
191 this->Start(50, FALSE);
194 void wxActTimer::ResetBitmap()
197 intActivityProgress = 0;
201 void wxActTimer::SetupPointer(void *frmMainPtrIn)
204 frmMainPtr = frmMainPtrIn;
208 void wxActProcessTimer::Notify()
211 // Pause the timer whilst processing to avoid problems.
217 frmActivityMgr *ActMgrPtrProc = static_cast<frmActivityMgr*>(ActMgrPtr);
218 ActMgrPtrProc->ProcessTasks();
222 void wxActProcessTimer::SetupPointers(void *ActMgr){
228 void wxActListProcTimer::Notify(){
230 // Setup Label and get task data (update SetupPointers subroutine).
234 for (std::map<int,int>::iterator iter = ActListPtr->begin();
235 iter != ActListPtr->end(); ++iter){
237 // Check for any queued tasks and start one.
244 // 5 = Failed (UI Updated).
245 // 6 = Stopped (UI Updated).
246 // 7 = (Reserved) (UI Updated).
247 // 8 = Completed. (UI Updated).
249 std::map<int,long>::iterator LongIter = ActListUIPtr->find(iter->first);
251 if (iter->second > 4){
256 std::map<int,wxString>::iterator DetailIter = ActListDetailPtr->find(iter->first);
258 wxCommandEvent uevent(ACTMGR_UPDATESTATUSLABEL);
259 ActivityMgrLabelUpdate *ueventdata = new ActivityMgrLabelUpdate;
260 uevent.SetClientData(ueventdata);
262 if (iter->second == 1){
264 ueventdata->ItemIndex = LongIter->second;
265 ueventdata->ItemLabel = _("Processing...");
266 //ueventdata->ItemDes = _("Adding Contact '") + DetailIter->second + _("'...");
268 wxPostEvent(static_cast<frmActivityMgr*>(ActMgrPtr), uevent);
270 /*lstActivityPtr->SetItem(LongIter->second, 2, _("Processing..."));
271 if (ActListDetailPtr->find(iter->first) != ActListDetailPtr->end()){
272 lblDetailsPtr->SetLabel(_("Adding Contact '") + DetailIter->second + _("'..."));
275 } else if (iter->second == 2){
277 ueventdata->ItemIndex = LongIter->second;
278 ueventdata->ItemLabel = _("Failed");
280 wxPostEvent(static_cast<frmActivityMgr*>(ActMgrPtr), uevent);
283 //lstActivityPtr->SetItem(LongIter->second, 2, _("Failed"));
285 } else if (iter->second == 3){
287 ueventdata->ItemIndex = LongIter->second;
288 ueventdata->ItemLabel = _("Stopped");
290 wxPostEvent(static_cast<frmActivityMgr*>(ActMgrPtr), uevent);
293 //lstActivityPtr->SetItem(LongIter->second, 2, _("Stopped"));
296 } else if (iter->second == 4){
298 ueventdata->ItemIndex = LongIter->second;
299 ueventdata->ItemLabel = _("Completed");
301 wxPostEvent(static_cast<frmActivityMgr*>(ActMgrPtr), uevent);
304 //lstActivityPtr->SetItem(LongIter->second, 2, _("Completed"));
309 // Insert a finish date.
314 ActListFinDTPtr->insert(std::make_pair(iter->first, adt));
316 // Delete the connection object.
318 std::map<int, CardDAV*>::iterator ConnObject = ActListConnPtr->find(iter->first);
319 delete ConnObject->second;
320 ConnObject->second = NULL;
324 if (ClearupProcessed == TRUE){
326 // Clear up processed items.
328 // Delete the processed items.
330 ClearupProcessed = FALSE;
334 if (*TasksActivePtr == FALSE){
336 ActTimerPtr->StopAnimation();
337 lblDetailsPtr->SetLabel(_("No activity."));
347 void wxActListProcTimer::SetupPointers(std::map<int,int> *ActList, std::map<int,long> *ActListUI,
348 std::map<int,wxString> *ActListDetail, bool *TasksActive,
349 wxListCtrl *lstActivity, wxStaticText *lblDetails, wxGauge *GauProgress,
350 void *ActMgr, wxActTimer *ActTimer, wxActProcessTimer *ActProcessTimer,
351 std::map<int,wxDateTime> *ActListFinDT, std::map<int, CardDAV*> *ActListConn){
353 ActListPtr = ActList;
354 ActListUIPtr = ActListUI;
355 ActListDetailPtr = ActListDetail;
356 TasksActivePtr = TasksActive;
357 lstActivityPtr = lstActivity;
358 lblDetailsPtr = lblDetails;
359 GauProgressPtr = GauProgress;
361 ActTimerPtr = ActTimer;
362 ActProcessTimerPtr = ActProcessTimer;
363 ActListFinDTPtr = ActListFinDT;
364 ActListConnPtr = ActListConn;
368 void wxActListProcTimer::EnableClearup(){
370 ClearupProcessed = TRUE;
374 void wxETagProcessTimer::Notify(){
376 // Check each ETagDB and see if writing to file is required.
381 std::map<wxString,ETagDB>::iterator ETagIter;
383 for (ETagIter = ETagDBList.begin();
384 ETagIter != ETagDBList.end(); ++ETagIter){
386 if (ETagIter->second.GetWriteStatus() == TRUE){
388 ETagIter->second.WriteETagDB();
397 void wxETagProcessTimer::ReloadAccounts(){
399 // Get the account preferences file and setup each database accordingly.
408 wxString PrefFilename = GetUserPrefDir();
410 XABPreferences PrefData(PrefFilename);
412 wxString AccountName;
414 wxString AccountAddress;
415 wxString AccountUsername;
416 wxString AccountPassword;
417 wxString AccountPrefix;
418 wxString AccountType;
420 // Look for the account and get the friendly name for the account.
422 for (int i = 0; i < PrefData.accounts.GetCount(); i++){
424 AccountDir = PrefData.accounts.GetAccountDirectory(i);
425 AccountType = PrefData.accounts.GetAccountType(i);
427 if (AccountDir.IsEmpty()){
431 if (AccountType == wxT("Local") || AccountType == wxT("local")){
436 AccountDir.Append(wxT(".carddav"));
439 AccountDB.SetupDB(AccountDir);
441 ETagDBList.insert(std::make_pair(AccountDir, AccountDB));
449 /*for (int i = 0; i < PrefData.accounts.GetCount(); i++){
451 AccountDir = PrefData.accounts.GetAccountDirectory(i);
454 AccountDir.Append(wxT(".carddav"));
456 //AccountPrefix = PrefData.accounts.GetAccountDirPrefix(i);
458 //ETagDB NewETagDB (AccountDir);
461 AccountDB.SetupDB(AccountDir);
463 //ETagDBList.insert(std::make_pair(AccountDir, Moo));
465 //std::pair<wxString,ETagDB> Moo2;
469 //ETagDBListv3.insert(std::make_pair(0,Meep));
471 //ETagDBListv2.insert(std::make_pair(0, Moo));
473 //ETagDBList.insert(Moo2);
475 ETagDBList.insert(std::make_pair(AccountDir, AccountDB));
477 //Moo55.insert(std::make_pair(wxT("Meep"), wxT("Meep")));
479 //ETagDBList[AccountDir] = new ETagDB (AccountDir);
483 //ETagDB Test (AccountDir);
485 //ETagDBList.insert(std::make_pair(AccountDir, Test));
492 ETagDB* wxETagProcessTimer::GetPointer(wxString AccountName){
494 ETagDB *ETagDBPtr = NULL;
496 std::map<wxString,ETagDB>::iterator ETagIter;
498 ETagIter = ETagDBList.find(AccountName);
500 if (ETagIter != ETagDBList.end()){
502 ETagDBPtr = &ETagIter->second;
510 void wxContactFileMonitorTimer::Notify(){
512 if (Filename.IsEmpty()){
518 wxFileName FileCheck(Filename);
519 wxDateTime CurrentDateTime;
521 CurrentDateTime = FileCheck.GetModificationTime();
523 if (FileDateTime != CurrentDateTime){
525 // The dates & times of the file are different.
526 // Send an event to notify the user of this change.
528 wxCommandEvent dateevent(wxEVT_COMMAND_BUTTON_CLICKED);
529 wxPostEvent(frmCEPtr, dateevent);
535 void wxContactFileMonitorTimer::SetFilename(wxString FilenameInc){
537 Filename = FilenameInc;
539 wxFileName FileCheck(Filename);
540 FileDateTime = FileCheck.GetModificationTime();
544 void wxContactFileMonitorTimer::SetupPointers(frmContactEditor *frmCEPtrInc){
546 frmCEPtr = frmCEPtrInc;
550 void wxContactFileMonitorTimer::UpdateTimestamp(){
552 wxFileName FileCheck(Filename);
553 FileDateTime = FileCheck.GetModificationTime();
557 void wxContactWindowMonitor::Notify(){
559 // Look through the list of windows and see if any have
560 // closed. IF they have closed then delete and free the
561 // memory (also delete from the map).
563 /*for(std::map<wxString, void*>::iterator iter = ContactWindowListPtr->begin();
564 iter != ContactWindowListPtr->end(); iter++){
566 frmContact *ContactPtr = (frmContact*)iter->second;
574 // Delete the data from the map.
580 void wxContactWindowMonitor::SetupPointers(std::map<wxString, void*> *ContactWindowListPtrInc){
582 ContactWindowListPtr = ContactWindowListPtrInc;
586 void wxAccountSyncTimer::Notify(){
588 frmActivityMgr *ActMgrPtrProc = static_cast<frmActivityMgr*>(frmActMgrPtr);
590 // Disable the timer. (Don't go updating unless needed).
594 if (ActMgrPtrProc->GetTaskStatus(intTaskID) < 2){
601 // Add task to the activity manager.
603 intTaskID = ActMgrPtrProc->AddTask(3, wxT(""), AccName,
604 wxT(""), wxT(""), wxT(""), wxT(""));
606 // Go to sleep and wait for the task to be completed.
608 // Enable the timer as we are now finished syncronising.
614 void wxAccountSyncTimer::SetupData(wxString AccNameInc,
615 wxString AccNameFullInc){
617 // Set the account name.
619 AccName = AccNameInc;
620 AccNameFull = AccNameFullInc;
624 void wxAccountSyncTimer::SetupPointers(void *frmMainPtrInc,
625 void *frmActMgrPtrInc, ETagDB *ETagDBPtrInc){
629 frmMainPtr = frmMainPtrInc;
630 frmActMgrPtr = frmActMgrPtrInc;
631 ETagDBPtr = ETagDBPtrInc;
635 void SleepFor(unsigned long longSleepNanoSecs){
638 ::Sleep((longSleepNanoSecs / 1000000));
646 n1.tv_nsec = longSleepNanoSecs;