Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added comments to describe functions in common/timers.cpp
[xestiaab/.git] / source / common / timers.cpp
1 // timers.cpp - Timers subroutines.
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 "../actmgr/frmActivityMgr.h"
20 #include "../frmContact.h"
21 #include "../frmMain.h"
22 #include <wx/mstream.h>
23 #include <wx/file.h>
24 #include <wx/ffile.h>
25 #include <wx/filename.h>
26 #include <wx/tokenzr.h>
27 #include "../bitmaps.h"
28 #include "../enums.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;
149         }
151         wxPostEvent(frmMainPtrProc, shuffleevent);
152     
155 void wxActTimer::SetBitmap(wxStaticBitmap *bitmap)
157     
158         // Set the bitmap for wxActTimer.
159     
160         bmpActivity = bitmap;
161     
164 void wxActTimer::SetSleepBitmap(wxBitmap *SleepBitmap)
166     
167         // Set the sleep bitmap for wxActTimer.
168         
169         SleepBitmapPtr = SleepBitmap;
170     
173 void wxActTimer::StopAnimation()
175     
176         // Stop the icon animation.
177     
178         frmMain *frmMainPtrProc = static_cast<frmMain*>(frmMainPtr);
179     
180         wxCommandEvent actstop(ACTMGR_STOP);
181         wxPostEvent(frmMainPtrProc, actstop);
182     
183         this->Stop();
184         bmpActivity->SetBitmap(*SleepBitmapPtr);
185         
188 void wxActTimer::StartAnimation()
190     
191         frmMain *frmMainPtrProc = static_cast<frmMain*>(frmMainPtr);
192         wxCommandEvent actstart(ACTMGR_START);
193         wxPostEvent(frmMainPtrProc, actstart);
194     
195         this->ResetBitmap();
196         this->Start(50, FALSE);
197         
200 void wxActTimer::ResetBitmap()
202     
203         // Reset the bitmap for wxActTimer.
204     
205         intActivityProgress = 0;
206     
209 void wxActTimer::SetupPointer(void *frmMainPtrIn)
211     
212         // Setup the pointer for wxActTimer.
213     
214         frmMainPtr = frmMainPtrIn;
215     
218 void wxActProcessTimer::Notify()
220     
221         // Notify function for wxActProcessTimer.
222     
223         // Pause the timer whilst processing to avoid problems.
224     
225         this->Stop();
226     
227         // Process Tasks.
228     
229         frmActivityMgr *ActMgrPtrProc = static_cast<frmActivityMgr*>(ActMgrPtr);
230         ActMgrPtrProc->ProcessTasks();
231     
234 void wxActProcessTimer::SetupPointers(void *ActMgr){
235     
236         // Setup pointers for the wxActProcessTimer.
237     
238         ActMgrPtr = ActMgr;
239     
242 void wxActListProcTimer::Notify(){
243     
244         // Notify function for wxActListProcTimer.
245     
246         // Setup Label and get task data (update SetupPointers subroutine).
247     
248         this->Stop();
249     
250         for (std::map<int,int>::iterator iter = ActListPtr->begin();
251                 iter != ActListPtr->end(); ++iter){
252         
253                 // Check for any queued tasks and start one.        
254         
255                 // 0 = Queued.
256                 // 1 = Processing.
257                 // 2 = Failed.
258                 // 3 = Stopped.
259                 // 4 = Completed.
260                 // 5 = Failed (UI Updated).
261                 // 6 = Stopped (UI Updated).
262                 // 7 = (Reserved) (UI Updated).
263                 // 8 = Completed. (UI Updated).
264         
265                 std::map<int,long>::iterator LongIter = ActListUIPtr->find(iter->first);
266         
267                 if (iter->second > 4){
268         
269                         continue;
270                 
271                 }
272         
273                 std::map<int,wxString>::iterator DetailIter = ActListDetailPtr->find(iter->first);
274         
275                 wxCommandEvent uevent(ACTMGR_UPDATESTATUSLABEL);
276                 ActivityMgrLabelUpdate *ueventdata = new ActivityMgrLabelUpdate;
277                 uevent.SetClientData(ueventdata);
278         
279                 if (iter->second == 1){
280             
281                         ueventdata->ItemIndex = LongIter->second;
282                         ueventdata->ItemLabel = _("Processing...");         
283             
284                         wxPostEvent(static_cast<frmActivityMgr*>(ActMgrPtr), uevent);
285             
286                 } else if (iter->second == 2){
287             
288                         ueventdata->ItemIndex = LongIter->second;
289                         ueventdata->ItemLabel = _("Failed");
290             
291                         wxPostEvent(static_cast<frmActivityMgr*>(ActMgrPtr), uevent);
292                         iter->second = 5;           
294                 } else if (iter->second == 3){
295  
296                         ueventdata->ItemIndex = LongIter->second;
297                         ueventdata->ItemLabel = _("Stopped");
298             
299                         wxPostEvent(static_cast<frmActivityMgr*>(ActMgrPtr), uevent);
300                         iter->second = 5;
301             
302                 } else if (iter->second == 4){
303             
304                         ueventdata->ItemIndex = LongIter->second;
305                         ueventdata->ItemLabel = _("Completed");
306             
307                         wxPostEvent(static_cast<frmActivityMgr*>(ActMgrPtr), uevent);
308                         iter->second = 8;
309             
310                 }
311         
312                 // Insert a finish date.
313         
314                 wxDateTime adt;
315                 adt = adt.Now();
316         
317                 ActListFinDTPtr->insert(std::make_pair(iter->first, adt));
318         
319                 // Delete the connection object.
320         
321                 std::map<int, CardDAV*>::iterator ConnObject = ActListConnPtr->find(iter->first);
322                 delete ConnObject->second;
323                 ConnObject->second = NULL;
324         
325         }
326     
327         if (ClearupProcessed == TRUE){
328         
329                 // Clear up processed items.
330         
331                 // Delete the processed items.
332         
333                 ClearupProcessed = FALSE;
334         
335         }
336     
337         if (*TasksActivePtr == FALSE){
338         
339                 ActTimerPtr->StopAnimation();
340                 lblDetailsPtr->SetLabel(_("No activity."));
341         
342         } else {
343         
344                 this->Start();
345         
346         }
347     
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){
355     
356         // Setup the pointers for wxActListProcTimer.
357     
358         ActListPtr = ActList;
359         ActListUIPtr = ActListUI;
360         ActListDetailPtr = ActListDetail;
361         TasksActivePtr = TasksActive;
362         lstActivityPtr = lstActivity;
363         lblDetailsPtr = lblDetails;
364         GauProgressPtr = GauProgress;
365         ActMgrPtr = ActMgr;
366         ActTimerPtr = ActTimer;
367         ActProcessTimerPtr = ActProcessTimer;
368         ActListFinDTPtr = ActListFinDT;
369         ActListConnPtr = ActListConn;
370     
373 void wxActListProcTimer::EnableClearup(){
374     
375         // Enable clearup of the wxActListProcTimer.
376     
377         ClearupProcessed = TRUE;
378     
381 void wxETagProcessTimer::Notify(){
382     
383         // Notify function for wxETagProcessTimer.
384     
385         // Check each ETagDB and see if writing to file is required.
386     
387         std::map<wxString,ETagDB>::iterator ETagIter;
388     
389         for (ETagIter = ETagDBList.begin();
390                 ETagIter != ETagDBList.end(); ++ETagIter){
391         
392                 if (ETagIter->second.GetWriteStatus() == TRUE){
393             
394                         ETagIter->second.WriteETagDB();
395             
396                 }
397         
398         }
399     
400     
403 void wxETagProcessTimer::ReloadAccounts(){
404     
405         // Get the account preferences file and setup each database accordingly.
406     
407         ReloadLock.lock();
408     
409         // Stop the timer.
410     
411         this->Stop();
412         ETagDBList.clear();
413     
414         wxString PrefFilename = GetUserPrefDir();
415     
416         XABPreferences PrefData(PrefFilename);
417     
418         wxString AccountName;
419         wxString AccountDir;
420         wxString AccountAddress;
421         wxString AccountUsername;
422         wxString AccountPassword;
423         wxString AccountPrefix;
424         wxString AccountType;
425     
426         // Look for the account and get the friendly name for the account.
427     
428         for (int i = 0; i < PrefData.accounts.GetCount(); i++){
429         
430                 AccountDir = PrefData.accounts.GetAccountDirectory(i);
431                 AccountType = PrefData.accounts.GetAccountType(i);
432         
433                 if (AccountDir.IsEmpty()){
434                         continue;
435                 }
436         
437                 if (AccountType == wxT("Local") || AccountType == wxT("local")){
438                 continue;
439                 }
440         
441                 AccountDir.Trim();
442                 AccountDir.Append(wxT(".carddav"));
443         
444                 ETagDB AccountDB;
445                 AccountDB.SetupDB(AccountDir);
446         
447                 ETagDBList.insert(std::make_pair(AccountDir, AccountDB));
448         
449                 AccountDir.clear();
450         
451         }
452     
453         ReloadLock.unlock();
454         this->Start(15000);
455     
458 ETagDB* wxETagProcessTimer::GetPointer(wxString AccountName){
459     
460         // Get the pointer of the ETag Database.
461     
462         ETagDB *ETagDBPtr = NULL;
463     
464         std::map<wxString,ETagDB>::iterator ETagIter;
465     
466         ETagIter = ETagDBList.find(AccountName);
468         if (ETagIter != ETagDBList.end()){
470                 ETagDBPtr = &ETagIter->second;
472         }
474         return ETagDBPtr;
475     
478 void wxContactFileMonitorTimer::Notify(){
479     
480         // Notify function for wxContactFileMonitorTimer.
481     
482         if (Filename.IsEmpty()){
483         
484                 return;
485         
486         }
487     
488         wxFileName FileCheck(Filename);
489         wxDateTime CurrentDateTime;
490     
491         CurrentDateTime = FileCheck.GetModificationTime();
492     
493         if (FileDateTime != CurrentDateTime){
494         
495                 // The dates & times of the file are different.
496                 // Send an event to notify the user of this change.
497         
498                 wxCommandEvent dateevent(wxEVT_COMMAND_BUTTON_CLICKED);
499                 wxPostEvent(frmCEPtr, dateevent);
500         
501         }
502     
505 void wxContactFileMonitorTimer::SetFilename(wxString FilenameInc){
506     
507         // Set the filename for the wxContactFileMonitorTimer object.
508     
509         Filename = FilenameInc;
510     
511         wxFileName FileCheck(Filename);
512         FileDateTime = FileCheck.GetModificationTime();
513     
516 void wxContactFileMonitorTimer::SetupPointers(frmContactEditor *frmCEPtrInc){
517     
518         // Setup the pointers for the wxContactFileMonitorTimer object.
519     
520         frmCEPtr = frmCEPtrInc;
521     
524 void wxContactFileMonitorTimer::UpdateTimestamp(){
525     
526         // Update the timestamp for the wxContactFileMonitorTimer object.
527     
528         wxFileName FileCheck(Filename);
529         FileDateTime = FileCheck.GetModificationTime();
530     
533 void wxContactWindowMonitor::Notify(){
534     
535         // Notify function for the wxContactWindowMonitor object.
536     
539 void wxContactWindowMonitor::SetupPointers(std::map<wxString, void*> *ContactWindowListPtrInc){
540     
541         // Setup pointers for the wxContactWindowMonitor.
542     
543         ContactWindowListPtr = ContactWindowListPtrInc;
544     
547 void wxAccountSyncTimer::Notify(){
548     
549         // Notify function for the wxAccountSyncTimer.
550     
551         frmActivityMgr *ActMgrPtrProc = static_cast<frmActivityMgr*>(frmActMgrPtr);
552     
553         // Disable the timer. (Don't go updating unless needed).
554     
555         this->Stop();
556     
557         if (ActMgrPtrProc->GetTaskStatus(intTaskID) < 2){
558         
559                 this->Start();
560                 return;
561         
562         }
563     
564         // Add task to the activity manager.
565     
566         intTaskID = ActMgrPtrProc->AddTask(3, wxT(""), AccName, 
567                 wxT(""), wxT(""), wxT(""), wxT(""));
568     
569         // Go to sleep and wait for the task to be completed.
570     
571         // Enable the timer as we are now finished syncronising.
572     
573         this->Start();
574     
577 void wxAccountSyncTimer::SetupData(wxString AccNameInc,
578                                    wxString AccNameFullInc){
579     
580         // Setup the data for the wxAccountSyncTimer object.
581     
582         // Set the account name.
583     
584         AccName = AccNameInc;
585         AccNameFull = AccNameFullInc;
586     
589 void wxAccountSyncTimer::SetupPointers(void *frmMainPtrInc, 
590                                        void *frmActMgrPtrInc, ETagDB *ETagDBPtrInc){
591     
592         // Setup the pointers for the wxAccountSyncTimer object.
593     
594         frmMainPtr = frmMainPtrInc;
595         frmActMgrPtr = frmActMgrPtrInc;
596         ETagDBPtr = ETagDBPtrInc;
597     
600 void SleepFor(unsigned long longSleepNanoSecs){
601     
602         // Sleep for specified nano seconds.
603     
604 #ifdef __WIN32__
605         ::Sleep((longSleepNanoSecs / 1000000));
606 #elif __HAIKU__
607     
608 #else
609     
610         timespec n1, n2;
611     
612         n1.tv_sec = 0;
613         n1.tv_nsec = longSleepNanoSecs;
614     
615         nanosleep(&n1, &n2);
616     
617 #endif
618     
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