Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
88bbb3e2501a64bd5af2d82bf219d39db7aae325
[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));
319         }
320     
321         if (ClearupProcessed == TRUE){
322         
323                 // Clear up processed items.
324         
325                 // Delete the processed items.
326         
327                 ClearupProcessed = FALSE;
328         
329         }
330     
331         if (*TasksActivePtr == FALSE){
332         
333                 ActTimerPtr->StopAnimation();
334                 lblDetailsPtr->SetLabel(_("No activity."));
335         
336         } else {
337         
338                 this->Start();
339         
340         }
341     
344 void wxActListProcTimer::SetupPointers(std::map<int,int> *ActList, std::map<int,long> *ActListUI,
345                                        std::map<int,wxString> *ActListDetail, bool *TasksActive,
346                                        wxListCtrl *lstActivity, wxStaticText *lblDetails, wxGauge *GauProgress,
347                                        void *ActMgr, wxActTimer *ActTimer, wxActProcessTimer *ActProcessTimer,
348                                        std::map<int,wxDateTime> *ActListFinDT, std::map<int, CardDAV*> *ActListConn){
349     
350         // Setup the pointers for wxActListProcTimer.
351     
352         ActListPtr = ActList;
353         ActListUIPtr = ActListUI;
354         ActListDetailPtr = ActListDetail;
355         TasksActivePtr = TasksActive;
356         lstActivityPtr = lstActivity;
357         lblDetailsPtr = lblDetails;
358         GauProgressPtr = GauProgress;
359         ActMgrPtr = ActMgr;
360         ActTimerPtr = ActTimer;
361         ActProcessTimerPtr = ActProcessTimer;
362         ActListFinDTPtr = ActListFinDT;
363         ActListConnPtr = ActListConn;
364     
367 void wxActListProcTimer::EnableClearup(){
368     
369         // Enable clearup of the wxActListProcTimer.
370     
371         ClearupProcessed = TRUE;
372     
375 void wxETagProcessTimer::Notify(){
376     
377         // Notify function for wxETagProcessTimer.
378     
379         // Check each ETagDB and see if writing to file is required.
380     
381         std::map<wxString,ETagDB>::iterator ETagIter;
382     
383         for (ETagIter = ETagDBList.begin();
384                 ETagIter != ETagDBList.end(); ++ETagIter){
385         
386                 if (ETagIter->second.GetWriteStatus() == TRUE){
387             
388                         ETagIter->second.WriteETagDB();
389             
390                 }
391         
392         }
393     
394     
397 void wxETagProcessTimer::ReloadAccounts(){
398     
399         // Get the account preferences file and setup each database accordingly.
400     
401         ReloadLock.lock();
402     
403         // Stop the timer.
404     
405         this->Stop();
406         ETagDBList.clear();
407     
408         wxString PrefFilename = GetUserPrefDir();
409     
410         XABPreferences PrefData(PrefFilename);
411     
412         wxString AccountName;
413         wxString AccountDir;
414         wxString AccountAddress;
415         wxString AccountUsername;
416         wxString AccountPassword;
417         wxString AccountPrefix;
418         wxString AccountType;
419     
420         // Look for the account and get the friendly name for the account.
421     
422         for (int i = 0; i < PrefData.accounts.GetCount(); i++){
423         
424                 AccountDir = PrefData.accounts.GetAccountDirectory(i);
425                 AccountType = PrefData.accounts.GetAccountType(i);
426                 
427                 if (AccountDir.IsEmpty()){
428                         continue;
429                 }
430         
431                 if (AccountType != wxT("CardDAV") && AccountType != wxT("carddav")){
432                         continue;
433                 }
434         
435                 AccountDir.Trim();
436                 AccountDir.Append(wxT(".carddav"));
437         
438                 ETagDB AccountDB;
439                 AccountDB.SetupDB(AccountDir);
440         
441                 ETagDBList.insert(std::make_pair(AccountDir, AccountDB));
442         
443                 AccountDir.clear();
444         
445         }
446     
447         ReloadLock.unlock();
448         this->Start(15000);
449     
452 ETagDB* wxETagProcessTimer::GetPointer(wxString AccountName){
453     
454         // Get the pointer of the ETag Database.
455     
456         ETagDB *ETagDBPtr = NULL;
457     
458         std::map<wxString,ETagDB>::iterator ETagIter;
459     
460         ETagIter = ETagDBList.find(AccountName);
462         if (ETagIter != ETagDBList.end()){
464                 ETagDBPtr = &ETagIter->second;
466         }
468         return ETagDBPtr;
469     
472 void wxContactFileMonitorTimer::Notify(){
473     
474         // Notify function for wxContactFileMonitorTimer.
475     
476         if (Filename.IsEmpty()){
477         
478                 return;
479         
480         }
481     
482         wxFileName FileCheck(Filename);
483         wxDateTime CurrentDateTime;
484     
485         CurrentDateTime = FileCheck.GetModificationTime();
486     
487         if (FileDateTime != CurrentDateTime){
488         
489                 // The dates & times of the file are different.
490                 // Send an event to notify the user of this change.
491         
492                 wxCommandEvent dateevent(wxEVT_COMMAND_BUTTON_CLICKED);
493                 wxPostEvent(frmCEPtr, dateevent);
494         
495         }
496     
499 void wxContactFileMonitorTimer::SetFilename(wxString FilenameInc){
500     
501         // Set the filename for the wxContactFileMonitorTimer object.
502     
503         Filename = FilenameInc;
504     
505         wxFileName FileCheck(Filename);
506         FileDateTime = FileCheck.GetModificationTime();
507     
510 void wxContactFileMonitorTimer::SetupPointers(frmContactEditor *frmCEPtrInc){
511     
512         // Setup the pointers for the wxContactFileMonitorTimer object.
513     
514         frmCEPtr = frmCEPtrInc;
515     
518 void wxContactFileMonitorTimer::UpdateTimestamp(){
519     
520         // Update the timestamp for the wxContactFileMonitorTimer object.
521     
522         wxFileName FileCheck(Filename);
523         FileDateTime = FileCheck.GetModificationTime();
524     
527 void wxContactWindowMonitor::Notify(){
528     
529         // Notify function for the wxContactWindowMonitor object.
530     
533 void wxContactWindowMonitor::SetupPointers(std::map<wxString, void*> *ContactWindowListPtrInc){
534     
535         // Setup pointers for the wxContactWindowMonitor.
536     
537         ContactWindowListPtr = ContactWindowListPtrInc;
538     
541 void wxAccountSyncTimer::Notify(){
542     
543         // Notify function for the wxAccountSyncTimer.
544     
545         frmActivityMgr *ActMgrPtrProc = static_cast<frmActivityMgr*>(frmActMgrPtr);
546     
547         // Disable the timer. (Don't go updating unless needed).
548     
549         this->Stop();
550     
551         if (ActMgrPtrProc->GetTaskStatus(intTaskID) < 2){
552         
553                 this->Start();
554                 return;
555         
556         }
557     
558         // Add task to the activity manager.
559     
560         intTaskID = ActMgrPtrProc->AddTask(3, wxT(""), AccName, 
561                 wxT(""), wxT(""), wxT(""), wxT(""));
562     
563         // Go to sleep and wait for the task to be completed.
564     
565         // Enable the timer as we are now finished syncronising.
566     
567         this->Start();
568     
571 void wxAccountSyncTimer::SetupData(wxString AccNameInc,
572                                    wxString AccNameFullInc){
573     
574         // Setup the data for the wxAccountSyncTimer object.
575     
576         // Set the account name.
577     
578         AccName = AccNameInc;
579         AccNameFull = AccNameFullInc;
580     
583 void wxAccountSyncTimer::SetupPointers(void *frmMainPtrInc, 
584                                        void *frmActMgrPtrInc, ETagDB *ETagDBPtrInc){
585     
586         // Setup the pointers for the wxAccountSyncTimer object.
587     
588         frmMainPtr = frmMainPtrInc;
589         frmActMgrPtr = frmActMgrPtrInc;
590         ETagDBPtr = ETagDBPtrInc;
591     
594 void SleepFor(unsigned long longSleepNanoSecs){
595     
596         // Sleep for specified nano seconds.
597     
598 #ifdef __WIN32__
599         ::Sleep((longSleepNanoSecs / 1000000));
600 #elif __HAIKU__
601     
602 #else
603     
604         timespec n1, n2;
605     
606         n1.tv_sec = 0;
607         n1.tv_nsec = longSleepNanoSecs;
608     
609         nanosleep(&n1, &n2);
610     
611 #endif
612     
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