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 // Notify function for wxActTimer.
37 frmMain *frmMainPtrProc = static_cast<frmMain*>(frmMainPtr);
39 wxCommandEvent shuffleevent(ACTMGR_SHUFFLE);
41 if (intActivityProgress == 0){
43 wxMemoryInputStream istream(misc_activity1_png, sizeof(misc_activity1_png));
44 wxImage misc_activity1_pngi(istream, wxBITMAP_TYPE_PNG);
45 wxBitmap activityiconbmp(misc_activity1_pngi, -1);
46 bmpActivity->SetBitmap(activityiconbmp);
48 intActivityProgress++;
50 } else if (intActivityProgress == 1){
52 wxMemoryInputStream istream(misc_activity2_png, sizeof(misc_activity2_png));
53 wxImage misc_activity2_pngi(istream, wxBITMAP_TYPE_PNG);
54 wxBitmap activityiconbmp(misc_activity2_pngi, -1);
55 bmpActivity->SetBitmap(activityiconbmp);
57 intActivityProgress++;
59 } else if (intActivityProgress == 2){
61 wxMemoryInputStream istream(misc_activity3_png, sizeof(misc_activity3_png));
62 wxImage misc_activity3_pngi(istream, wxBITMAP_TYPE_PNG);
63 wxBitmap activityiconbmp(misc_activity3_pngi, -1);
64 bmpActivity->SetBitmap(activityiconbmp);
66 intActivityProgress++;
68 } else if (intActivityProgress == 3){
70 wxMemoryInputStream istream(misc_activity4_png, sizeof(misc_activity4_png));
71 wxImage misc_activity4_pngi(istream, wxBITMAP_TYPE_PNG);
72 wxBitmap activityiconbmp(misc_activity4_pngi, -1);
73 bmpActivity->SetBitmap(activityiconbmp);
75 intActivityProgress++;
77 } else if (intActivityProgress == 4){
79 wxMemoryInputStream istream(misc_activity5_png, sizeof(misc_activity5_png));
80 wxImage misc_activity5_pngi(istream, wxBITMAP_TYPE_PNG);
81 wxBitmap activityiconbmp(misc_activity5_pngi, -1);
82 bmpActivity->SetBitmap(activityiconbmp);
84 intActivityProgress++;
86 } else if (intActivityProgress == 5){
88 wxMemoryInputStream istream(misc_activity6_png, sizeof(misc_activity6_png));
89 wxImage misc_activity6_pngi(istream, wxBITMAP_TYPE_PNG);
90 wxBitmap activityiconbmp(misc_activity6_pngi, -1);
91 bmpActivity->SetBitmap(activityiconbmp);
93 intActivityProgress++;
95 } else if (intActivityProgress == 6){
97 wxMemoryInputStream istream(misc_activity7_png, sizeof(misc_activity7_png));
98 wxImage misc_activity7_pngi(istream, wxBITMAP_TYPE_PNG);
99 wxBitmap activityiconbmp(misc_activity7_pngi, -1);
100 bmpActivity->SetBitmap(activityiconbmp);
102 intActivityProgress++;
104 } else if (intActivityProgress == 7){
106 wxMemoryInputStream istream(misc_activity8_png, sizeof(misc_activity8_png));
107 wxImage misc_activity8_pngi(istream, wxBITMAP_TYPE_PNG);
108 wxBitmap activityiconbmp(misc_activity8_pngi, -1);
109 bmpActivity->SetBitmap(activityiconbmp);
111 intActivityProgress++;
113 } else if (intActivityProgress == 8){
115 wxMemoryInputStream istream(misc_activity9_png, sizeof(misc_activity9_png));
116 wxImage misc_activity9_pngi(istream, wxBITMAP_TYPE_PNG);
117 wxBitmap activityiconbmp(misc_activity9_pngi, -1);
118 bmpActivity->SetBitmap(activityiconbmp);
120 intActivityProgress++;
122 } else if (intActivityProgress == 9){
124 wxMemoryInputStream istream(misc_activity10_png, sizeof(misc_activity10_png));
125 wxImage misc_activity10_pngi(istream, wxBITMAP_TYPE_PNG);
126 wxBitmap activityiconbmp(misc_activity10_pngi, -1);
127 bmpActivity->SetBitmap(activityiconbmp);
129 intActivityProgress++;
131 } else if (intActivityProgress == 10){
133 wxMemoryInputStream istream(misc_activity11_png, sizeof(misc_activity11_png));
134 wxImage misc_activity11_pngi(istream, wxBITMAP_TYPE_PNG);
135 wxBitmap activityiconbmp(misc_activity11_pngi, -1);
136 bmpActivity->SetBitmap(activityiconbmp);
138 intActivityProgress++;
140 } else if (intActivityProgress == 11){
142 wxMemoryInputStream istream(misc_activity12_png, sizeof(misc_activity12_png));
143 wxImage misc_activity12_pngi(istream, wxBITMAP_TYPE_PNG);
144 wxBitmap activityiconbmp(misc_activity12_pngi, -1);
145 bmpActivity->SetBitmap(activityiconbmp);
147 intActivityProgress = 0;
151 wxPostEvent(frmMainPtrProc, shuffleevent);
155 void wxActTimer::SetBitmap(wxStaticBitmap *bitmap)
158 // Set the bitmap for wxActTimer.
160 bmpActivity = bitmap;
164 void wxActTimer::SetSleepBitmap(wxBitmap *SleepBitmap)
167 // Set the sleep bitmap for wxActTimer.
169 SleepBitmapPtr = SleepBitmap;
173 void wxActTimer::StopAnimation()
176 // Stop the icon animation.
178 frmMain *frmMainPtrProc = static_cast<frmMain*>(frmMainPtr);
180 wxCommandEvent actstop(ACTMGR_STOP);
181 wxPostEvent(frmMainPtrProc, actstop);
184 bmpActivity->SetBitmap(*SleepBitmapPtr);
188 void wxActTimer::StartAnimation()
191 frmMain *frmMainPtrProc = static_cast<frmMain*>(frmMainPtr);
192 wxCommandEvent actstart(ACTMGR_START);
193 wxPostEvent(frmMainPtrProc, actstart);
196 this->Start(50, FALSE);
200 void wxActTimer::ResetBitmap()
203 // Reset the bitmap for wxActTimer.
205 intActivityProgress = 0;
209 void wxActTimer::SetupPointer(void *frmMainPtrIn)
212 // Setup the pointer for wxActTimer.
214 frmMainPtr = frmMainPtrIn;
218 void wxActProcessTimer::Notify()
221 // Notify function for wxActProcessTimer.
223 // Pause the timer whilst processing to avoid problems.
229 frmActivityMgr *ActMgrPtrProc = static_cast<frmActivityMgr*>(ActMgrPtr);
230 ActMgrPtrProc->ProcessTasks();
234 void wxActProcessTimer::SetupPointers(void *ActMgr){
236 // Setup pointers for the wxActProcessTimer.
242 void wxActListProcTimer::Notify(){
244 // Notify function for wxActListProcTimer.
246 // Setup Label and get task data (update SetupPointers subroutine).
250 for (std::map<int,int>::iterator iter = ActListPtr->begin();
251 iter != ActListPtr->end(); ++iter){
253 // Check for any queued tasks and start one.
260 // 5 = Failed (UI Updated).
261 // 6 = Stopped (UI Updated).
262 // 7 = (Reserved) (UI Updated).
263 // 8 = Completed. (UI Updated).
265 std::map<int,long>::iterator LongIter = ActListUIPtr->find(iter->first);
267 if (iter->second > 4){
273 std::map<int,wxString>::iterator DetailIter = ActListDetailPtr->find(iter->first);
275 wxCommandEvent uevent(ACTMGR_UPDATESTATUSLABEL);
276 ActivityMgrLabelUpdate *ueventdata = new ActivityMgrLabelUpdate;
277 uevent.SetClientData(ueventdata);
279 if (iter->second == 1){
281 ueventdata->ItemIndex = LongIter->second;
282 ueventdata->ItemLabel = _("Processing...");
284 wxPostEvent(static_cast<frmActivityMgr*>(ActMgrPtr), uevent);
286 } else if (iter->second == 2){
288 ueventdata->ItemIndex = LongIter->second;
289 ueventdata->ItemLabel = _("Failed");
291 wxPostEvent(static_cast<frmActivityMgr*>(ActMgrPtr), uevent);
294 } else if (iter->second == 3){
296 ueventdata->ItemIndex = LongIter->second;
297 ueventdata->ItemLabel = _("Stopped");
299 wxPostEvent(static_cast<frmActivityMgr*>(ActMgrPtr), uevent);
302 } else if (iter->second == 4){
304 ueventdata->ItemIndex = LongIter->second;
305 ueventdata->ItemLabel = _("Completed");
307 wxPostEvent(static_cast<frmActivityMgr*>(ActMgrPtr), uevent);
312 // Insert a finish date.
317 ActListFinDTPtr->insert(std::make_pair(iter->first, adt));
319 // Delete the connection object.
321 std::map<int, CardDAV*>::iterator ConnObject = ActListConnPtr->find(iter->first);
322 delete ConnObject->second;
323 ConnObject->second = NULL;
327 if (ClearupProcessed == TRUE){
329 // Clear up processed items.
331 // Delete the processed items.
333 ClearupProcessed = FALSE;
337 if (*TasksActivePtr == FALSE){
339 ActTimerPtr->StopAnimation();
340 lblDetailsPtr->SetLabel(_("No activity."));
350 void wxActListProcTimer::SetupPointers(std::map<int,int> *ActList, std::map<int,long> *ActListUI,
351 std::map<int,wxString> *ActListDetail, bool *TasksActive,
352 wxListCtrl *lstActivity, wxStaticText *lblDetails, wxGauge *GauProgress,
353 void *ActMgr, wxActTimer *ActTimer, wxActProcessTimer *ActProcessTimer,
354 std::map<int,wxDateTime> *ActListFinDT, std::map<int, CardDAV*> *ActListConn){
356 // Setup the pointers for wxActListProcTimer.
358 ActListPtr = ActList;
359 ActListUIPtr = ActListUI;
360 ActListDetailPtr = ActListDetail;
361 TasksActivePtr = TasksActive;
362 lstActivityPtr = lstActivity;
363 lblDetailsPtr = lblDetails;
364 GauProgressPtr = GauProgress;
366 ActTimerPtr = ActTimer;
367 ActProcessTimerPtr = ActProcessTimer;
368 ActListFinDTPtr = ActListFinDT;
369 ActListConnPtr = ActListConn;
373 void wxActListProcTimer::EnableClearup(){
375 // Enable clearup of the wxActListProcTimer.
377 ClearupProcessed = TRUE;
381 void wxETagProcessTimer::Notify(){
383 // Notify function for wxETagProcessTimer.
385 // Check each ETagDB and see if writing to file is required.
387 std::map<wxString,ETagDB>::iterator ETagIter;
389 for (ETagIter = ETagDBList.begin();
390 ETagIter != ETagDBList.end(); ++ETagIter){
392 if (ETagIter->second.GetWriteStatus() == TRUE){
394 ETagIter->second.WriteETagDB();
403 void wxETagProcessTimer::ReloadAccounts(){
405 // Get the account preferences file and setup each database accordingly.
414 wxString PrefFilename = GetUserPrefDir();
416 XABPreferences PrefData(PrefFilename);
418 wxString AccountName;
420 wxString AccountAddress;
421 wxString AccountUsername;
422 wxString AccountPassword;
423 wxString AccountPrefix;
424 wxString AccountType;
426 // Look for the account and get the friendly name for the account.
428 for (int i = 0; i < PrefData.accounts.GetCount(); i++){
430 AccountDir = PrefData.accounts.GetAccountDirectory(i);
431 AccountType = PrefData.accounts.GetAccountType(i);
433 if (AccountDir.IsEmpty()){
437 if (AccountType == wxT("Local") || AccountType == wxT("local")){
442 AccountDir.Append(wxT(".carddav"));
445 AccountDB.SetupDB(AccountDir);
447 ETagDBList.insert(std::make_pair(AccountDir, AccountDB));
458 ETagDB* wxETagProcessTimer::GetPointer(wxString AccountName){
460 // Get the pointer of the ETag Database.
462 ETagDB *ETagDBPtr = NULL;
464 std::map<wxString,ETagDB>::iterator ETagIter;
466 ETagIter = ETagDBList.find(AccountName);
468 if (ETagIter != ETagDBList.end()){
470 ETagDBPtr = &ETagIter->second;
478 void wxContactFileMonitorTimer::Notify(){
480 // Notify function for wxContactFileMonitorTimer.
482 if (Filename.IsEmpty()){
488 wxFileName FileCheck(Filename);
489 wxDateTime CurrentDateTime;
491 CurrentDateTime = FileCheck.GetModificationTime();
493 if (FileDateTime != CurrentDateTime){
495 // The dates & times of the file are different.
496 // Send an event to notify the user of this change.
498 wxCommandEvent dateevent(wxEVT_COMMAND_BUTTON_CLICKED);
499 wxPostEvent(frmCEPtr, dateevent);
505 void wxContactFileMonitorTimer::SetFilename(wxString FilenameInc){
507 // Set the filename for the wxContactFileMonitorTimer object.
509 Filename = FilenameInc;
511 wxFileName FileCheck(Filename);
512 FileDateTime = FileCheck.GetModificationTime();
516 void wxContactFileMonitorTimer::SetupPointers(frmContactEditor *frmCEPtrInc){
518 // Setup the pointers for the wxContactFileMonitorTimer object.
520 frmCEPtr = frmCEPtrInc;
524 void wxContactFileMonitorTimer::UpdateTimestamp(){
526 // Update the timestamp for the wxContactFileMonitorTimer object.
528 wxFileName FileCheck(Filename);
529 FileDateTime = FileCheck.GetModificationTime();
533 void wxContactWindowMonitor::Notify(){
535 // Notify function for the wxContactWindowMonitor object.
539 void wxContactWindowMonitor::SetupPointers(std::map<wxString, void*> *ContactWindowListPtrInc){
541 // Setup pointers for the wxContactWindowMonitor.
543 ContactWindowListPtr = ContactWindowListPtrInc;
547 void wxAccountSyncTimer::Notify(){
549 // Notify function for the wxAccountSyncTimer.
551 frmActivityMgr *ActMgrPtrProc = static_cast<frmActivityMgr*>(frmActMgrPtr);
553 // Disable the timer. (Don't go updating unless needed).
557 if (ActMgrPtrProc->GetTaskStatus(intTaskID) < 2){
564 // Add task to the activity manager.
566 intTaskID = ActMgrPtrProc->AddTask(3, wxT(""), AccName,
567 wxT(""), wxT(""), wxT(""), wxT(""));
569 // Go to sleep and wait for the task to be completed.
571 // Enable the timer as we are now finished syncronising.
577 void wxAccountSyncTimer::SetupData(wxString AccNameInc,
578 wxString AccNameFullInc){
580 // Setup the data for the wxAccountSyncTimer object.
582 // Set the account name.
584 AccName = AccNameInc;
585 AccNameFull = AccNameFullInc;
589 void wxAccountSyncTimer::SetupPointers(void *frmMainPtrInc,
590 void *frmActMgrPtrInc, ETagDB *ETagDBPtrInc){
592 // Setup the pointers for the wxAccountSyncTimer object.
594 frmMainPtr = frmMainPtrInc;
595 frmActMgrPtr = frmActMgrPtrInc;
596 ETagDBPtr = ETagDBPtrInc;
600 void SleepFor(unsigned long longSleepNanoSecs){
602 // Sleep for specified nano seconds.
605 ::Sleep((longSleepNanoSecs / 1000000));
613 n1.tv_nsec = longSleepNanoSecs;