// frmActivityMgr-addtask.cpp - Activity Manager add task subroutines.
//
// (c) 2012-2015 Xestia Software Development.
//
// This file is part of Xestia Address Book.
//
// Xestia Address Book is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by the
// Free Software Foundation, version 3 of the license.
//
// Xestia Address Book is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with Xestia Address Book. If not, see
#include "frmActivityMgr.h"
int frmActivityMgr::AddTask(int TaskType, wxString TaskDetail, wxString TaskAccount,
wxString TaskURL, wxString TaskFilename, wxString TaskFullFilename,
wxString TaskData){
ActivityTaskID++;
wxListItem coldata;
long itemindex = -1;
CardDAV* CardDAVConn = new CardDAV;
coldata.SetId(ActivityTaskID);
coldata.SetData(ActivityTaskID);
AnimationTimer.StartAnimation();
// Get the preferences file.
wxString PrefFilename = GetUserPrefDir();
XABPreferences PrefData(PrefFilename);
wxString AccountName;
wxString AccountDir;
wxString AccountAddress;
int AccountPort = 0;
wxString AccountUsername;
wxString AccountPassword;
bool AccountSSL = FALSE;
wxString AccountPrefix;
// Look for the account and get the friendly name for the account.
for (int i = 0; i < PrefData.accounts.GetCount(); i++){
AccountDir = PrefData.accounts.GetAccountDirectory(i);
AccountDir.Trim();
AccountDir.Append(wxT(".carddav"));
if (AccountDir == TaskAccount){
AccountName = PrefData.accounts.GetAccountName(i);
AccountAddress = PrefData.accounts.GetAccountAddress(i);
AccountPort = PrefData.accounts.GetAccountPort(i);
AccountUsername = PrefData.accounts.GetAccountUsername(i);
AccountPassword = PrefData.accounts.GetAccountPassword(i);
AccountPrefix = PrefData.accounts.GetAccountDirPrefix(i);
AccountSSL = PrefData.accounts.GetAccountSSL(i);
AccountName.Trim();
AccountAddress.Trim();
//AccountPort.Trim();
AccountUsername.Trim();
AccountPassword.Trim();
AccountPrefix.Trim();
break;
}
}
CardDAVConn->SetupConnection(AccountAddress, AccountPort, AccountUsername, AccountPassword, AccountSSL, AccountPrefix, AccountDir);
CardDAVConn->SetupVariables(&ActivityList, ActivityTaskID);
CardDAVConn->SetupResultBools(&BlankBool, &BlankBool);
// TaskType:
// 0 - Add a contact.
// 1 - Update a contact.
// 2 - Delete a contact.
// 3 - Account syncronisation.
if (TaskType == 0){
coldata.SetText(_("Adding Contact '") + TaskDetail + _("'"));
itemindex = lstActivity->InsertItem(coldata);
lstActivity->SetItem(itemindex, 1, AccountName);
lstActivity->SetItem(itemindex, 2, _("Queued"));
CardDAVConn->SetUploadMode(TRUE);
CardDAVConn->SetupData(wxT("PUT"), TaskURL, TaskData);
// Setup all of the task details.
ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
ActivityListType.insert(std::make_pair(ActivityTaskID, 0));
ActivityListData.insert(std::make_pair(ActivityTaskID, TaskData));
ActivityListConn.insert(std::make_pair(ActivityTaskID, CardDAVConn));
ActivityListIndex.insert(std::make_pair(ActivityTaskID, itemindex));
ActivityListTaskDetail.insert(std::make_pair(ActivityTaskID, TaskDetail));
ActivityListAccount.insert(std::make_pair(ActivityTaskID, TaskAccount));
ActivityListFilename.insert(std::make_pair(ActivityTaskID, TaskFilename));
ActivityListFullFilename.insert(std::make_pair(ActivityTaskID, TaskFullFilename));
ActivityListETag.insert(std::make_pair(ActivityTaskID, ETagDBPtr->GetETag(TaskFilename)));
ActivityListETagOriginal.insert(std::make_pair(ActivityTaskID, ETagDBPtr->GetETagOriginal(TaskFilename)));
ActivityList.insert(std::make_pair(ActivityTaskID, 0));
} else if (TaskType == 1){
coldata.SetText(_("Updating Contact '") + TaskDetail + _("'"));
itemindex = lstActivity->InsertItem(coldata);
lstActivity->SetItem(itemindex, 1, AccountName);
lstActivity->SetItem(itemindex, 2, _("Queued"));
CardDAVConn->SetUploadMode(TRUE);
CardDAVConn->SetupData(wxT("PUT"), TaskURL, TaskData);
CardDAVConn->SetEditMode(TRUE);
ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
ActivityListType.insert(std::make_pair(ActivityTaskID, 1));
ActivityListData.insert(std::make_pair(ActivityTaskID, TaskData));
ActivityListConn.insert(std::make_pair(ActivityTaskID, CardDAVConn));
ActivityListIndex.insert(std::make_pair(ActivityTaskID, itemindex));
ActivityListTaskDetail.insert(std::make_pair(ActivityTaskID, TaskDetail));
ActivityListAccount.insert(std::make_pair(ActivityTaskID, TaskAccount));
ActivityListFilename.insert(std::make_pair(ActivityTaskID, TaskFilename));
ActivityListFullFilename.insert(std::make_pair(ActivityTaskID, TaskFullFilename));
ActivityListETag.insert(std::make_pair(ActivityTaskID, ETagDBPtr->GetETag(TaskFilename)));
ActivityListETagOriginal.insert(std::make_pair(ActivityTaskID, ETagDBPtr->GetETagOriginal(TaskFilename)));
ActivityList.insert(std::make_pair(ActivityTaskID, 0));
} else if (TaskType == 2){
coldata.SetText(_("Deleting Contact '") + TaskDetail + _("'"));
itemindex = lstActivity->InsertItem(coldata);
lstActivity->SetItem(itemindex, 1, AccountName);
lstActivity->SetItem(itemindex, 2, _("Queued"));
ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
ActivityList.insert(std::make_pair(ActivityTaskID, 0));
ActivityListType.insert(std::make_pair(ActivityTaskID, 2));
ActivityListData.insert(std::make_pair(ActivityTaskID, TaskData));
ActivityListAccount.insert(std::make_pair(ActivityTaskID, TaskAccount));
ActivityListConn.insert(std::make_pair(ActivityTaskID, CardDAVConn));
ActivityListFilename.insert(std::make_pair(ActivityTaskID, TaskFilename));
ActivityListIndex.insert(std::make_pair(ActivityTaskID, itemindex));
} else if (TaskType == 3){
coldata.SetText(_("Syncronising All Contacts"));
itemindex = lstActivity->InsertItem(coldata);
lstActivity->SetItem(itemindex, 1, AccountName);
lstActivity->SetItem(itemindex, 2, _("Queued"));
ActivityList.insert(std::make_pair(ActivityTaskID, 0));
ActivityListType.insert(std::make_pair(ActivityTaskID, 3));
ActivityListData.insert(std::make_pair(ActivityTaskID, TaskData));
ActivityListConn.insert(std::make_pair(ActivityTaskID, CardDAVConn));
ActivityListTaskDetail.insert(std::make_pair(ActivityTaskID, AccountPrefix));
ActivityListAccount.insert(std::make_pair(ActivityTaskID, TaskAccount));
ActivityListIndex.insert(std::make_pair(ActivityTaskID, itemindex));
}
ProcessTasksData = TRUE;
if (!ActProcessTimer.IsRunning()) {
wxCommandEvent event(ACTMGR_STARTTIMER);
wxPostEvent(this, event);
wxCommandEvent eventp(ACTMGR_STARTPROCESSTIMER);
wxPostEvent(this, eventp);
//ActProcessTimer.Start(1000, FALSE);
//ActListProcTimer.Start(1000, FALSE);
}
// Update the recovery database.
return ActivityTaskID;
}
void frmActivityMgr::RemoveTask(){
// Remove task from the recovery database.
}
void frmActivityMgr::StopTask( wxCommandEvent& event ){
// Check if activity has been completed already before stopping.
long longSelected = -1;
int intSelectedData = 0;
longSelected = lstActivity->GetNextItem(longSelected,
wxLIST_NEXT_ALL,
wxLIST_STATE_SELECTED);
intSelectedData = (int)lstActivity->GetItemData(longSelected);
if (longSelected == -1){
return;
}
std::map::iterator CDAVter;
CDAVter = ActivityListConn.find(intSelectedData);
CDAVter->second->Abort();
lstActivity->SetItem(longSelected, 2, _("Stopped"));
}
void frmActivityMgr::StopAllTasks( wxCommandEvent& event ){
// Check if activity has been completed already before stopping.
// TODO: Look through for '1' and stop the running task(s).
long longSelected = -1;
int intSelectedData = 0;
longSelected = lstActivity->GetNextItem(longSelected,
wxLIST_NEXT_ALL,
wxLIST_STATE_SELECTED);
intSelectedData = (int)lstActivity->GetItemData(longSelected);
if (longSelected == -1){
return;
}
std::map::iterator CDAVter;
CDAVter = ActivityListConn.find(intSelectedData);
CDAVter->second->Abort();
lstActivity->SetItem(longSelected, 2, _("Stopped"));
AnimationTimer.StopAnimation();
bmpActivity->SetBitmap(SleepBitmap);
lblDetails->SetLabel(_("No activity."));
}
void frmActivityMgr::RetryTask( wxCommandEvent& event ){
// Check if activity has been completed already before retrying.
long longSelected = -1;
int intSelectedData = 0;
longSelected = lstActivity->GetNextItem(longSelected,
wxLIST_NEXT_ALL,
wxLIST_STATE_SELECTED);
intSelectedData = (int)lstActivity->GetItemData(longSelected);
if (longSelected == -1){
return;
}
std::map::iterator CDAVter;
CDAVter = ActivityListConn.find(intSelectedData);
// Restart the task.
//CDAVter->second.Abort();
std::map::iterator Iter;
Iter = ActivityList.find(intSelectedData);
Iter->second = 0;
AnimationTimer.StartAnimation();
if (!ActProcessTimer.IsRunning()) {
ActProcessTimer.Start(150, FALSE);
ActListProcTimer.Start(150, FALSE);
}
//lstActivity->SetItem(longSelected, 2, _("Processing..."));
}
int frmActivityMgr::GetTaskStatus(int intTaskID){
int intResult = 2;
if (ActivityList.find(intTaskID) == ActivityList.end()){
return intResult;
}
std::map::iterator taskiter = ActivityList.find(intTaskID);
intResult = taskiter->second;
return intResult;
}