Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Replaced CardDAV with CardDAV* in the activity manager, improving memory usage.
[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         // Delete the connection object.
317         
318         std::map<int, CardDAV*>::iterator ConnObject = ActListConnPtr->find(iter->first);
319         delete ConnObject->second;
320         ConnObject->second = NULL;
321         
322     }
323     
324     if (ClearupProcessed == TRUE){
325         
326         // Clear up processed items.
327         
328         // Delete the processed items.
329         
330         ClearupProcessed = FALSE;
331         
332     }
333     
334     if (*TasksActivePtr == FALSE){
335         
336         ActTimerPtr->StopAnimation();
337         lblDetailsPtr->SetLabel(_("No activity."));
338         
339     } else {
340         
341         this->Start();
342         
343     }
344     
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){
352     
353     ActListPtr = ActList;
354     ActListUIPtr = ActListUI;
355     ActListDetailPtr = ActListDetail;
356     TasksActivePtr = TasksActive;
357     lstActivityPtr = lstActivity;
358     lblDetailsPtr = lblDetails;
359     GauProgressPtr = GauProgress;
360     ActMgrPtr = ActMgr;
361     ActTimerPtr = ActTimer;
362     ActProcessTimerPtr = ActProcessTimer;
363     ActListFinDTPtr = ActListFinDT;
364     ActListConnPtr = ActListConn;
365     
368 void wxActListProcTimer::EnableClearup(){
369     
370     ClearupProcessed = TRUE;
371     
374 void wxETagProcessTimer::Notify(){
375     
376     // Check each ETagDB and see if writing to file is required.
377     
378     //ETagDB* ETagDBPtr;
379     //ETagDB ETagDBCopy;
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("Local") || AccountType == wxT("local")){
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     //raise(SIGABRT);
448     
449     /*for (int i = 0; i < PrefData.accounts.GetCount(); i++){
450      
451      AccountDir = PrefData.accounts.GetAccountDirectory(i);
452      
453      AccountDir.Trim();
454      AccountDir.Append(wxT(".carddav"));
455      
456      //AccountPrefix = PrefData.accounts.GetAccountDirPrefix(i);
457      
458      //ETagDB NewETagDB (AccountDir);
459      
460      ETagDB AccountDB;
461      AccountDB.SetupDB(AccountDir);
462      
463      //ETagDBList.insert(std::make_pair(AccountDir, Moo));
464      
465      //std::pair<wxString,ETagDB> Moo2;
466      
467      //ETagDB2 Meep;
468      
469      //ETagDBListv3.insert(std::make_pair(0,Meep));
470      
471      //ETagDBListv2.insert(std::make_pair(0, Moo));
472      
473      //ETagDBList.insert(Moo2);
474      
475      ETagDBList.insert(std::make_pair(AccountDir, AccountDB));
476      
477      //Moo55.insert(std::make_pair(wxT("Meep"), wxT("Meep")));
478      
479      //ETagDBList[AccountDir] = new ETagDB (AccountDir);
480      
481      }*/
482     
483     //ETagDB Test (AccountDir);
484     
485     //ETagDBList.insert(std::make_pair(AccountDir, Test));
486     
487     ReloadLock.unlock();
488     this->Start(15000);
489     
492 ETagDB* wxETagProcessTimer::GetPointer(wxString AccountName){
493     
494     ETagDB *ETagDBPtr = NULL;
495     
496     std::map<wxString,ETagDB>::iterator ETagIter;
497     
498     ETagIter = ETagDBList.find(AccountName);
499     
500     if (ETagIter != ETagDBList.end()){
501         
502         ETagDBPtr = &ETagIter->second;
503         
504     }
505     
506     return ETagDBPtr;
507     
510 void wxContactFileMonitorTimer::Notify(){
511     
512     if (Filename.IsEmpty()){
513         
514         return;
515         
516     }
517     
518     wxFileName FileCheck(Filename);
519     wxDateTime CurrentDateTime;
520     
521     CurrentDateTime = FileCheck.GetModificationTime();
522     
523     if (FileDateTime != CurrentDateTime){
524         
525         // The dates & times of the file are different.
526         // Send an event to notify the user of this change.
527         
528         wxCommandEvent dateevent(wxEVT_COMMAND_BUTTON_CLICKED);
529         wxPostEvent(frmCEPtr, dateevent);
530         
531     }
532     
535 void wxContactFileMonitorTimer::SetFilename(wxString FilenameInc){
536     
537     Filename = FilenameInc;
538     
539     wxFileName FileCheck(Filename);
540     FileDateTime = FileCheck.GetModificationTime();
541     
544 void wxContactFileMonitorTimer::SetupPointers(frmContactEditor *frmCEPtrInc){
545     
546     frmCEPtr = frmCEPtrInc;
547     
550 void wxContactFileMonitorTimer::UpdateTimestamp(){
551     
552     wxFileName FileCheck(Filename);
553     FileDateTime = FileCheck.GetModificationTime();
554     
557 void wxContactWindowMonitor::Notify(){
558     
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).
562     
563     /*for(std::map<wxString, void*>::iterator iter = ContactWindowListPtr->begin();
564      iter != ContactWindowListPtr->end(); iter++){
565      
566      frmContact *ContactPtr = (frmContact*)iter->second;
567      
568      // Check if open.
569      
570      if (
571      
572      // Delete the form.
573      
574      // Delete the data from the map.
575      
576      }*/
577     
580 void wxContactWindowMonitor::SetupPointers(std::map<wxString, void*> *ContactWindowListPtrInc){
581     
582     ContactWindowListPtr = ContactWindowListPtrInc;
583     
586 void wxAccountSyncTimer::Notify(){
587     
588     frmActivityMgr *ActMgrPtrProc = static_cast<frmActivityMgr*>(frmActMgrPtr);
589     
590     // Disable the timer. (Don't go updating unless needed).
591     
592     this->Stop();
593     
594     if (ActMgrPtrProc->GetTaskStatus(intTaskID) < 2){
595         
596         this->Start();
597         return;
598         
599     }
600     
601     // Add task to the activity manager.
602     
603     intTaskID = ActMgrPtrProc->AddTask(3, wxT(""), AccName, 
604                                        wxT(""), wxT(""), wxT(""), wxT(""));
605     
606     // Go to sleep and wait for the task to be completed.
607     
608     // Enable the timer as we are now finished syncronising.
609     
610     this->Start();
611     
614 void wxAccountSyncTimer::SetupData(wxString AccNameInc,
615                                    wxString AccNameFullInc){
616     
617     // Set the account name.
618     
619     AccName = AccNameInc;
620     AccNameFull = AccNameFullInc;
621     
624 void wxAccountSyncTimer::SetupPointers(void *frmMainPtrInc, 
625                                        void *frmActMgrPtrInc, ETagDB *ETagDBPtrInc){
626     
627     // Set the pointers.
628     
629     frmMainPtr = frmMainPtrInc;
630     frmActMgrPtr = frmActMgrPtrInc;
631     ETagDBPtr = ETagDBPtrInc;
632     
635 void SleepFor(unsigned long longSleepNanoSecs){
636     
637 #ifdef __WIN32__
638     ::Sleep((longSleepNanoSecs / 1000000));
639 #elif __HAIKU__
640     
641 #else
642     
643     timespec n1, n2;
644     
645     n1.tv_sec = 0;
646     n1.tv_nsec = longSleepNanoSecs;
647     
648     nanosleep(&n1, &n2);
649     
650 #endif
651     
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