1 // frmActivityMgr-addtask.cpp - Activity Manager add task 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 "frmActivityMgr.h"
21 int frmActivityMgr::AddTask(int TaskType, wxString TaskDetail, wxString TaskAccount,
22 wxString TaskURL, wxString TaskFilename, wxString TaskFullFilename,
29 CardDAV* CardDAVConn = new CardDAV;
31 coldata.SetId(ActivityTaskID);
32 coldata.SetData(ActivityTaskID);
34 AnimationTimer.StartAnimation();
36 // Get the preferences file.
38 wxString PrefFilename = GetUserPrefDir();
40 XABPreferences PrefData(PrefFilename);
44 wxString AccountAddress;
46 wxString AccountUsername;
47 wxString AccountPassword;
48 bool AccountSSL = FALSE;
49 wxString AccountPrefix;
51 // Look for the account and get the friendly name for the account.
53 for (int i = 0; i < PrefData.accounts.GetCount(); i++){
55 AccountDir = PrefData.accounts.GetAccountDirectory(i);
58 AccountDir.Append(wxT(".carddav"));
60 if (AccountDir == TaskAccount){
62 AccountName = PrefData.accounts.GetAccountName(i);
63 AccountAddress = PrefData.accounts.GetAccountAddress(i);
64 AccountPort = PrefData.accounts.GetAccountPort(i);
65 AccountUsername = PrefData.accounts.GetAccountUsername(i);
66 AccountPassword = PrefData.accounts.GetAccountPassword(i);
67 AccountPrefix = PrefData.accounts.GetAccountDirPrefix(i);
68 AccountSSL = PrefData.accounts.GetAccountSSL(i);
71 AccountAddress.Trim();
73 AccountUsername.Trim();
74 AccountPassword.Trim();
82 CardDAVConn->SetupConnection(AccountAddress, AccountPort, AccountUsername, AccountPassword, AccountSSL, AccountPrefix, AccountDir);
83 CardDAVConn->SetupVariables(&ActivityList, ActivityTaskID);
84 CardDAVConn->SetupResultBools(&BlankBool, &BlankBool);
88 // 1 - Update a contact.
89 // 2 - Delete a contact.
90 // 3 - Account syncronisation.
94 coldata.SetText(_("Adding Contact '") + TaskDetail + _("'"));
95 itemindex = lstActivity->InsertItem(coldata);
97 lstActivity->SetItem(itemindex, 1, AccountName);
98 lstActivity->SetItem(itemindex, 2, _("Queued"));
100 CardDAVConn->SetUploadMode(TRUE);
101 CardDAVConn->SetupData(wxT("PUT"), TaskURL, TaskData);
103 // Setup all of the task details.
105 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
107 ActivityListType.insert(std::make_pair(ActivityTaskID, 0));
108 ActivityListData.insert(std::make_pair(ActivityTaskID, TaskData));
109 ActivityListConn.insert(std::make_pair(ActivityTaskID, CardDAVConn));
110 ActivityListIndex.insert(std::make_pair(ActivityTaskID, itemindex));
111 ActivityListTaskDetail.insert(std::make_pair(ActivityTaskID, TaskDetail));
112 ActivityListAccount.insert(std::make_pair(ActivityTaskID, TaskAccount));
113 ActivityListFilename.insert(std::make_pair(ActivityTaskID, TaskFilename));
114 ActivityListFullFilename.insert(std::make_pair(ActivityTaskID, TaskFullFilename));
116 ActivityListETag.insert(std::make_pair(ActivityTaskID, ETagDBPtr->GetETag(TaskFilename)));
117 ActivityListETagOriginal.insert(std::make_pair(ActivityTaskID, ETagDBPtr->GetETagOriginal(TaskFilename)));
118 ActivityList.insert(std::make_pair(ActivityTaskID, 0));
120 } else if (TaskType == 1){
122 coldata.SetText(_("Updating Contact '") + TaskDetail + _("'"));
123 itemindex = lstActivity->InsertItem(coldata);
125 lstActivity->SetItem(itemindex, 1, AccountName);
126 lstActivity->SetItem(itemindex, 2, _("Queued"));
128 CardDAVConn->SetUploadMode(TRUE);
129 CardDAVConn->SetupData(wxT("PUT"), TaskURL, TaskData);
130 CardDAVConn->SetEditMode(TRUE);
132 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
134 ActivityListType.insert(std::make_pair(ActivityTaskID, 1));
135 ActivityListData.insert(std::make_pair(ActivityTaskID, TaskData));
136 ActivityListConn.insert(std::make_pair(ActivityTaskID, CardDAVConn));
137 ActivityListIndex.insert(std::make_pair(ActivityTaskID, itemindex));
138 ActivityListTaskDetail.insert(std::make_pair(ActivityTaskID, TaskDetail));
139 ActivityListAccount.insert(std::make_pair(ActivityTaskID, TaskAccount));
140 ActivityListFilename.insert(std::make_pair(ActivityTaskID, TaskFilename));
141 ActivityListFullFilename.insert(std::make_pair(ActivityTaskID, TaskFullFilename));
143 ActivityListETag.insert(std::make_pair(ActivityTaskID, ETagDBPtr->GetETag(TaskFilename)));
144 ActivityListETagOriginal.insert(std::make_pair(ActivityTaskID, ETagDBPtr->GetETagOriginal(TaskFilename)));
145 ActivityList.insert(std::make_pair(ActivityTaskID, 0));
147 } else if (TaskType == 2){
149 coldata.SetText(_("Deleting Contact '") + TaskDetail + _("'"));
150 itemindex = lstActivity->InsertItem(coldata);
152 lstActivity->SetItem(itemindex, 1, AccountName);
153 lstActivity->SetItem(itemindex, 2, _("Queued"));
155 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
157 ActivityList.insert(std::make_pair(ActivityTaskID, 0));
158 ActivityListType.insert(std::make_pair(ActivityTaskID, 2));
159 ActivityListData.insert(std::make_pair(ActivityTaskID, TaskData));
160 ActivityListAccount.insert(std::make_pair(ActivityTaskID, TaskAccount));
161 ActivityListConn.insert(std::make_pair(ActivityTaskID, CardDAVConn));
162 ActivityListFilename.insert(std::make_pair(ActivityTaskID, TaskFilename));
163 ActivityListIndex.insert(std::make_pair(ActivityTaskID, itemindex));
165 } else if (TaskType == 3){
167 coldata.SetText(_("Syncronising All Contacts"));
168 itemindex = lstActivity->InsertItem(coldata);
170 lstActivity->SetItem(itemindex, 1, AccountName);
171 lstActivity->SetItem(itemindex, 2, _("Queued"));
173 ActivityList.insert(std::make_pair(ActivityTaskID, 0));
174 ActivityListType.insert(std::make_pair(ActivityTaskID, 3));
175 ActivityListData.insert(std::make_pair(ActivityTaskID, TaskData));
176 ActivityListConn.insert(std::make_pair(ActivityTaskID, CardDAVConn));
177 ActivityListTaskDetail.insert(std::make_pair(ActivityTaskID, AccountPrefix));
178 ActivityListAccount.insert(std::make_pair(ActivityTaskID, TaskAccount));
179 ActivityListIndex.insert(std::make_pair(ActivityTaskID, itemindex));
183 ProcessTasksData = TRUE;
185 if (!ActProcessTimer.IsRunning()) {
187 wxCommandEvent event(ACTMGR_STARTTIMER);
188 wxPostEvent(this, event);
190 wxCommandEvent eventp(ACTMGR_STARTPROCESSTIMER);
191 wxPostEvent(this, eventp);
193 //ActProcessTimer.Start(1000, FALSE);
194 //ActListProcTimer.Start(1000, FALSE);
198 // Update the recovery database.
200 return ActivityTaskID;
204 void frmActivityMgr::RemoveTask(){
206 // Remove task from the recovery database.
210 void frmActivityMgr::StopTask( wxCommandEvent& event ){
212 // Check if activity has been completed already before stopping.
214 long longSelected = -1;
215 int intSelectedData = 0;
217 longSelected = lstActivity->GetNextItem(longSelected,
219 wxLIST_STATE_SELECTED);
221 intSelectedData = (int)lstActivity->GetItemData(longSelected);
223 if (longSelected == -1){
227 std::map<int,CardDAV*>::iterator CDAVter;
229 CDAVter = ActivityListConn.find(intSelectedData);
231 CDAVter->second->Abort();
233 lstActivity->SetItem(longSelected, 2, _("Stopped"));
237 void frmActivityMgr::StopAllTasks( wxCommandEvent& event ){
239 // Check if activity has been completed already before stopping.
241 // TODO: Look through for '1' and stop the running task(s).
243 long longSelected = -1;
244 int intSelectedData = 0;
246 longSelected = lstActivity->GetNextItem(longSelected,
248 wxLIST_STATE_SELECTED);
250 intSelectedData = (int)lstActivity->GetItemData(longSelected);
252 if (longSelected == -1){
256 std::map<int,CardDAV*>::iterator CDAVter;
258 CDAVter = ActivityListConn.find(intSelectedData);
260 CDAVter->second->Abort();
262 lstActivity->SetItem(longSelected, 2, _("Stopped"));
263 AnimationTimer.StopAnimation();
264 bmpActivity->SetBitmap(SleepBitmap);
265 lblDetails->SetLabel(_("No activity."));
269 void frmActivityMgr::RetryTask( wxCommandEvent& event ){
271 // Check if activity has been completed already before retrying.
273 long longSelected = -1;
274 int intSelectedData = 0;
276 longSelected = lstActivity->GetNextItem(longSelected,
278 wxLIST_STATE_SELECTED);
280 intSelectedData = (int)lstActivity->GetItemData(longSelected);
282 if (longSelected == -1){
286 std::map<int,CardDAV*>::iterator CDAVter;
288 CDAVter = ActivityListConn.find(intSelectedData);
292 //CDAVter->second.Abort();
294 std::map<int,int>::iterator Iter;
295 Iter = ActivityList.find(intSelectedData);
299 AnimationTimer.StartAnimation();
301 if (!ActProcessTimer.IsRunning()) {
303 ActProcessTimer.Start(150, FALSE);
304 ActListProcTimer.Start(150, FALSE);
308 //lstActivity->SetItem(longSelected, 2, _("Processing..."));
312 int frmActivityMgr::GetTaskStatus(int intTaskID){
316 if (ActivityList.find(intTaskID) == ActivityList.end()){
322 std::map<int,int>::iterator taskiter = ActivityList.find(intTaskID);
323 intResult = taskiter->second;