Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added copyright and licence header to C++ files and headers for the common directory.
[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 "../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(){
34     
35     frmMain *frmMainPtrProc = static_cast<frmMain*>(frmMainPtr);
36     
37     wxCommandEvent shuffleevent(ACTMGR_SHUFFLE);
38     
39     if (intActivityProgress == 0){
40         
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);
45         
46         intActivityProgress++;
47         
48     } else if (intActivityProgress == 1){
49         
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);
54         
55         intActivityProgress++;
56         
57     } else if (intActivityProgress == 2){
58         
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);
63         
64         intActivityProgress++;
65         
66     } else if (intActivityProgress == 3){
67         
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);
72         
73         intActivityProgress++;
74         
75     } else if (intActivityProgress == 4){
76         
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);
81         
82         intActivityProgress++;
83         
84     } else if (intActivityProgress == 5){
85         
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);
90         
91         intActivityProgress++;
92         
93     } else if (intActivityProgress == 6){
94         
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);
99         
100         intActivityProgress++;
101         
102     } else if (intActivityProgress == 7){
103         
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);
108         
109         intActivityProgress++;
110         
111     } else if (intActivityProgress == 8){
112         
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);
117         
118         intActivityProgress++;
119         
120     } else if (intActivityProgress == 9){
121         
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);
126         
127         intActivityProgress++;
128         
129     } else if (intActivityProgress == 10){
130         
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);
135         
136         intActivityProgress++;
137         
138     } else if (intActivityProgress == 11){
139         
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);
144         
145         intActivityProgress = 0;
146         
147     }
148     
149     wxPostEvent(frmMainPtrProc, shuffleevent);
150     
153 void wxActTimer::SetBitmap(wxStaticBitmap *bitmap)
155     
156     bmpActivity = bitmap;
157     
160 void wxActTimer::SetSleepBitmap(wxBitmap *SleepBitmap)
162     
163     SleepBitmapPtr = SleepBitmap;
164     
167 void wxActTimer::StopAnimation()
169     
170     // Stop the icon animation.
171     
172     frmMain *frmMainPtrProc = static_cast<frmMain*>(frmMainPtr);
173     
174     wxCommandEvent actstop(ACTMGR_STOP);
175     wxPostEvent(frmMainPtrProc, actstop);
176     //frmMainPtrProc->ActivityIconStop();
177     
178     this->Stop();
179     bmpActivity->SetBitmap(*SleepBitmapPtr);
182 void wxActTimer::StartAnimation()
184     
185     frmMain *frmMainPtrProc = static_cast<frmMain*>(frmMainPtr);
186     wxCommandEvent actstart(ACTMGR_START);
187     wxPostEvent(frmMainPtrProc, actstart);
188     //frmMainPtrProc->ActivityIconStart();
189     
190     this->ResetBitmap();
191     this->Start(50, FALSE);
194 void wxActTimer::ResetBitmap()
196     
197     intActivityProgress = 0;
198     
201 void wxActTimer::SetupPointer(void *frmMainPtrIn)
203     
204     frmMainPtr = frmMainPtrIn;
205     
208 void wxActProcessTimer::Notify()
210     
211     // Pause the timer whilst processing to avoid problems.
212     
213     this->Stop();
214     
215     // Process Tasks.
216     
217     frmActivityMgr *ActMgrPtrProc = static_cast<frmActivityMgr*>(ActMgrPtr);
218     ActMgrPtrProc->ProcessTasks();
219     
222 void wxActProcessTimer::SetupPointers(void *ActMgr){
223     
224     ActMgrPtr = ActMgr;
225     
228 void wxActListProcTimer::Notify(){
229     
230     // Setup Label and get task data (update SetupPointers subroutine).
231     
232     this->Stop();
233     
234     for (std::map<int,int>::iterator iter = ActListPtr->begin();
235          iter != ActListPtr->end(); ++iter){
236         
237         // Check for any queued tasks and start one.        
238         
239         // 0 = Queued.
240         // 1 = Processing.
241         // 2 = Failed.
242         // 3 = Stopped.
243         // 4 = Completed.
244         // 5 = Failed (UI Updated).
245         // 6 = Stopped (UI Updated).
246         // 7 = (Reserved) (UI Updated).
247         // 8 = Completed. (UI Updated).
248         
249         std::map<int,long>::iterator LongIter = ActListUIPtr->find(iter->first);
250         
251         if (iter->second > 4){
252         
253             continue;
254         }
255         
256         std::map<int,wxString>::iterator DetailIter = ActListDetailPtr->find(iter->first);
257         
258         wxCommandEvent uevent(ACTMGR_UPDATESTATUSLABEL);
259         ActivityMgrLabelUpdate *ueventdata = new ActivityMgrLabelUpdate;
260         uevent.SetClientData(ueventdata);
261         
262         if (iter->second == 1){
263             
264             ueventdata->ItemIndex = LongIter->second;
265             ueventdata->ItemLabel = _("Processing...");     
266             //ueventdata->ItemDes = _("Adding Contact '") + DetailIter->second + _("'...");
267             
268             wxPostEvent(static_cast<frmActivityMgr*>(ActMgrPtr), uevent);
269             
270             /*lstActivityPtr->SetItem(LongIter->second, 2, _("Processing..."));
271             if (ActListDetailPtr->find(iter->first) != ActListDetailPtr->end()){
272                 lblDetailsPtr->SetLabel(_("Adding Contact '") + DetailIter->second + _("'..."));
273             }*/
274             
275         } else if (iter->second == 2){
276             
277             ueventdata->ItemIndex = LongIter->second;
278             ueventdata->ItemLabel = _("Failed");
279             
280             wxPostEvent(static_cast<frmActivityMgr*>(ActMgrPtr), uevent);
281             iter->second = 5;       
283             //lstActivityPtr->SetItem(LongIter->second, 2, _("Failed"));
284            
285         } else if (iter->second == 3){
286  
287             ueventdata->ItemIndex = LongIter->second;
288             ueventdata->ItemLabel = _("Stopped");
289             
290             wxPostEvent(static_cast<frmActivityMgr*>(ActMgrPtr), uevent);
291             iter->second = 5;
292             
293             //lstActivityPtr->SetItem(LongIter->second, 2, _("Stopped"));
294             //iter->second = 6;
295             
296         } else if (iter->second == 4){
297             
298             ueventdata->ItemIndex = LongIter->second;
299             ueventdata->ItemLabel = _("Completed");
300             
301             wxPostEvent(static_cast<frmActivityMgr*>(ActMgrPtr), uevent);
302             iter->second = 8;
303             
304             //lstActivityPtr->SetItem(LongIter->second, 2, _("Completed"));
305             //iter->second = 8;
306             
307         }
308         
309         // Insert a finish date.
310         
311         wxDateTime adt;
312         adt = adt.Now();
313         
314         ActListFinDTPtr->insert(std::make_pair(iter->first, adt));
315         
316     }
317     
318     if (ClearupProcessed == TRUE){
319         
320         // Clear up processed items.
321         
322         // Delete the processed items.
323         
324         ClearupProcessed = FALSE;
325         
326     }
327     
328     if (*TasksActivePtr == FALSE){
329         
330         ActTimerPtr->StopAnimation();
331         lblDetailsPtr->SetLabel(_("No activity."));
332         
333     } else {
334         
335         this->Start();
336         
337     }
338     
341 void wxActListProcTimer::SetupPointers(std::map<int,int> *ActList, std::map<int,long> *ActListUI,
342                                        std::map<int,wxString> *ActListDetail, bool *TasksActive,
343                                        wxListCtrl *lstActivity, wxStaticText *lblDetails, wxGauge *GauProgress,
344                                        void *ActMgr, wxActTimer *ActTimer, wxActProcessTimer *ActProcessTimer,
345                                        std::map<int,wxDateTime> *ActListFinDT){
346     
347     ActListPtr = ActList;
348     ActListUIPtr = ActListUI;
349     ActListDetailPtr = ActListDetail;
350     TasksActivePtr = TasksActive;
351     lstActivityPtr = lstActivity;
352     lblDetailsPtr = lblDetails;
353     GauProgressPtr = GauProgress;
354     ActMgrPtr = ActMgr;
355     ActTimerPtr = ActTimer;
356     ActProcessTimerPtr = ActProcessTimer;
357     ActListFinDTPtr = ActListFinDT;
358     
361 void wxActListProcTimer::EnableClearup(){
362     
363     ClearupProcessed = TRUE;
364     
367 void wxETagProcessTimer::Notify(){
368     
369     // Check each ETagDB and see if writing to file is required.
370     
371     //ETagDB* ETagDBPtr;
372     //ETagDB ETagDBCopy;
373     
374     std::map<wxString,ETagDB>::iterator ETagIter;
375     
376     for (ETagIter = ETagDBList.begin();
377          ETagIter != ETagDBList.end(); ++ETagIter){
378         
379         if (ETagIter->second.GetWriteStatus() == TRUE){
380             
381             ETagIter->second.WriteETagDB();
382             
383         }
384         
385     }
386     
387     
390 void wxETagProcessTimer::ReloadAccounts(){
391     
392     // Get the account preferences file and setup each database accordingly.
393     
394     ReloadLock.lock();
395     
396     // Stop the timer.
397     
398     this->Stop();
399     ETagDBList.clear();
400     
401     wxString PrefFilename = GetUserPrefDir();
402     
403     XABPreferences PrefData(PrefFilename);
404     
405     wxString AccountName;
406     wxString AccountDir;
407     wxString AccountAddress;
408     wxString AccountUsername;
409     wxString AccountPassword;
410     wxString AccountPrefix;
411     wxString AccountType;
412     
413     // Look for the account and get the friendly name for the account.
414     
415     for (int i = 0; i < PrefData.accounts.GetCount(); i++){
416         
417         AccountDir = PrefData.accounts.GetAccountDirectory(i);
418         AccountType = PrefData.accounts.GetAccountType(i);
419         
420         if (AccountDir.IsEmpty()){
421             continue;
422         }
423         
424         if (AccountType == wxT("Local") || AccountType == wxT("local")){
425             continue;
426         }
427         
428         AccountDir.Trim();
429         AccountDir.Append(wxT(".carddav"));
430         
431         ETagDB AccountDB;
432         AccountDB.SetupDB(AccountDir);
433         
434         ETagDBList.insert(std::make_pair(AccountDir, AccountDB));
435         
436         AccountDir.clear();
437         
438     }
439     
440     //raise(SIGABRT);
441     
442     /*for (int i = 0; i < PrefData.accounts.GetCount(); i++){
443      
444      AccountDir = PrefData.accounts.GetAccountDirectory(i);
445      
446      AccountDir.Trim();
447      AccountDir.Append(wxT(".carddav"));
448      
449      //AccountPrefix = PrefData.accounts.GetAccountDirPrefix(i);
450      
451      //ETagDB NewETagDB (AccountDir);
452      
453      ETagDB AccountDB;
454      AccountDB.SetupDB(AccountDir);
455      
456      //ETagDBList.insert(std::make_pair(AccountDir, Moo));
457      
458      //std::pair<wxString,ETagDB> Moo2;
459      
460      //ETagDB2 Meep;
461      
462      //ETagDBListv3.insert(std::make_pair(0,Meep));
463      
464      //ETagDBListv2.insert(std::make_pair(0, Moo));
465      
466      //ETagDBList.insert(Moo2);
467      
468      ETagDBList.insert(std::make_pair(AccountDir, AccountDB));
469      
470      //Moo55.insert(std::make_pair(wxT("Meep"), wxT("Meep")));
471      
472      //ETagDBList[AccountDir] = new ETagDB (AccountDir);
473      
474      }*/
475     
476     //ETagDB Test (AccountDir);
477     
478     //ETagDBList.insert(std::make_pair(AccountDir, Test));
479     
480     ReloadLock.unlock();
481     this->Start(15000);
482     
485 ETagDB* wxETagProcessTimer::GetPointer(wxString AccountName){
486     
487     ETagDB *ETagDBPtr = NULL;
488     
489     std::map<wxString,ETagDB>::iterator ETagIter;
490     
491     ETagIter = ETagDBList.find(AccountName);
492     
493     if (ETagIter != ETagDBList.end()){
494         
495         ETagDBPtr = &ETagIter->second;
496         
497     }
498     
499     return ETagDBPtr;
500     
503 void wxContactFileMonitorTimer::Notify(){
504     
505     if (Filename.IsEmpty()){
506         
507         return;
508         
509     }
510     
511     wxFileName FileCheck(Filename);
512     wxDateTime CurrentDateTime;
513     
514     CurrentDateTime = FileCheck.GetModificationTime();
515     
516     if (FileDateTime != CurrentDateTime){
517         
518         // The dates & times of the file are different.
519         // Send an event to notify the user of this change.
520         
521         wxCommandEvent dateevent(wxEVT_COMMAND_BUTTON_CLICKED);
522         wxPostEvent(frmCEPtr, dateevent);
523         
524     }
525     
528 void wxContactFileMonitorTimer::SetFilename(wxString FilenameInc){
529     
530     Filename = FilenameInc;
531     
532     wxFileName FileCheck(Filename);
533     FileDateTime = FileCheck.GetModificationTime();
534     
537 void wxContactFileMonitorTimer::SetupPointers(frmContactEditor *frmCEPtrInc){
538     
539     frmCEPtr = frmCEPtrInc;
540     
543 void wxContactFileMonitorTimer::UpdateTimestamp(){
544     
545     wxFileName FileCheck(Filename);
546     FileDateTime = FileCheck.GetModificationTime();
547     
550 void wxContactWindowMonitor::Notify(){
551     
552     // Look through the list of windows and see if any have
553     // closed. IF they have closed then delete and free the
554     // memory (also delete from the map).
555     
556     /*for(std::map<wxString, void*>::iterator iter = ContactWindowListPtr->begin();
557      iter != ContactWindowListPtr->end(); iter++){
558      
559      frmContact *ContactPtr = (frmContact*)iter->second;
560      
561      // Check if open.
562      
563      if (
564      
565      // Delete the form.
566      
567      // Delete the data from the map.
568      
569      }*/
570     
573 void wxContactWindowMonitor::SetupPointers(std::map<wxString, void*> *ContactWindowListPtrInc){
574     
575     ContactWindowListPtr = ContactWindowListPtrInc;
576     
579 void wxAccountSyncTimer::Notify(){
580     
581     frmActivityMgr *ActMgrPtrProc = static_cast<frmActivityMgr*>(frmActMgrPtr);
582     
583     // Disable the timer. (Don't go updating unless needed).
584     
585     this->Stop();
586     
587     if (ActMgrPtrProc->GetTaskStatus(intTaskID) < 2){
588         
589         this->Start();
590         return;
591         
592     }
593     
594     // Add task to the activity manager.
595     
596     intTaskID = ActMgrPtrProc->AddTask(3, wxT(""), AccName, 
597                                        wxT(""), wxT(""), wxT(""), wxT(""));
598     
599     // Go to sleep and wait for the task to be completed.
600     
601     // Enable the timer as we are now finished syncronising.
602     
603     this->Start();
604     
607 void wxAccountSyncTimer::SetupData(wxString AccNameInc,
608                                    wxString AccNameFullInc){
609     
610     // Set the account name.
611     
612     AccName = AccNameInc;
613     AccNameFull = AccNameFullInc;
614     
617 void wxAccountSyncTimer::SetupPointers(void *frmMainPtrInc, 
618                                        void *frmActMgrPtrInc, ETagDB *ETagDBPtrInc){
619     
620     // Set the pointers.
621     
622     frmMainPtr = frmMainPtrInc;
623     frmActMgrPtr = frmActMgrPtrInc;
624     ETagDBPtr = ETagDBPtrInc;
625     
628 void SleepFor(unsigned long longSleepNanoSecs){
629     
630 #ifdef __WIN32__
631     ::Sleep((longSleepNanoSecs / 1000000));
632 #elif __HAIKU__
633     
634 #else
635     
636     timespec n1, n2;
637     
638     n1.tv_sec = 0;
639     n1.tv_nsec = longSleepNanoSecs;
640     
641     nanosleep(&n1, &n2);
642     
643 #endif
644     
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