// 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;
coldata.SetId(ActivityTaskID);
coldata.SetData(ActivityTaskID);
if (ApplicationSleepMode == FALSE){
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;
}
}
ActivityMgrAccountSettings AccountSettings;
AccountSettings.Address = AccountAddress;
AccountSettings.Port = AccountPort;
AccountSettings.Username = AccountUsername;
AccountSettings.Password = AccountPassword;
AccountSettings.SSL = AccountSSL;
AccountSettings.Prefix = AccountPrefix;
AccountSettings.Dir = AccountDir;
// 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"));
// Setup all of the task details.
ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
CardDAV2 *ConnObject = new CardDAV2(AccountAddress.ToStdString(), AccountPort, AccountUsername.ToStdString(), AccountPassword.ToStdString(), AccountSSL, AccountPrefix.ToStdString(), AccountDir.ToStdString());
ConnObject->SetupConnectionObject();
ActivityListType.insert(std::make_pair(ActivityTaskID, 0));
ActivityListData.insert(std::make_pair(ActivityTaskID, TaskData));
ActivityListURL.insert(std::make_pair(ActivityTaskID, TaskURL));
ActivityListEditMode.insert(std::make_pair(ActivityTaskID, false));
ActivityListConnObject.insert(std::make_pair(ActivityTaskID, ConnObject));
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));
ActivityListAccSettings.insert(std::make_pair(ActivityTaskID, AccountSettings));
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"));
ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
CardDAV2 *ConnObject = new CardDAV2(AccountAddress.ToStdString(), AccountPort, AccountUsername.ToStdString(), AccountPassword.ToStdString(), AccountSSL, AccountPrefix.ToStdString(), AccountDir.ToStdString());
ConnObject->SetupConnectionObject();
ActivityListType.insert(std::make_pair(ActivityTaskID, 1));
ActivityListData.insert(std::make_pair(ActivityTaskID, TaskData));
ActivityListURL.insert(std::make_pair(ActivityTaskID, TaskURL));
ActivityListEditMode.insert(std::make_pair(ActivityTaskID, true));
ActivityListConnObject.insert(std::make_pair(ActivityTaskID, ConnObject));
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));
ActivityListAccSettings.insert(std::make_pair(ActivityTaskID, AccountSettings));
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);
CardDAV2 *ConnObject = new CardDAV2(AccountAddress.ToStdString(), AccountPort, AccountUsername.ToStdString(), AccountPassword.ToStdString(), AccountSSL, AccountPrefix.ToStdString(), AccountDir.ToStdString());
ConnObject->SetupConnectionObject();
ActivityList.insert(std::make_pair(ActivityTaskID, 0));
ActivityListType.insert(std::make_pair(ActivityTaskID, 2));
ActivityListData.insert(std::make_pair(ActivityTaskID, TaskData));
ActivityListURL.insert(std::make_pair(ActivityTaskID, TaskURL));
ActivityListAccount.insert(std::make_pair(ActivityTaskID, TaskAccount));
ActivityListFilename.insert(std::make_pair(ActivityTaskID, TaskFilename));
ActivityListConnObject.insert(std::make_pair(ActivityTaskID, ConnObject));
ActivityListIndex.insert(std::make_pair(ActivityTaskID, itemindex));
ActivityListAccSettings.insert(std::make_pair(ActivityTaskID, AccountSettings));
} else if (TaskType == 3){
coldata.SetText(_("Syncronising All Contacts"));
itemindex = lstActivity->InsertItem(coldata);
lstActivity->SetItem(itemindex, 1, AccountName);
lstActivity->SetItem(itemindex, 2, _("Queued"));
CardDAV2 *ConnObject = new CardDAV2(AccountAddress.ToStdString(), AccountPort, AccountUsername.ToStdString(), AccountPassword.ToStdString(), AccountSSL, AccountPrefix.ToStdString(), AccountDir.ToStdString());
ConnObject->SetupConnectionObject();
ActivityList.insert(std::make_pair(ActivityTaskID, 0));
ActivityListType.insert(std::make_pair(ActivityTaskID, 3));
ActivityListData.insert(std::make_pair(ActivityTaskID, TaskData));
ActivityListTaskDetail.insert(std::make_pair(ActivityTaskID, AccountPrefix));
ActivityListAccount.insert(std::make_pair(ActivityTaskID, TaskAccount));
ActivityListConnObject.insert(std::make_pair(ActivityTaskID, ConnObject));
ActivityListIndex.insert(std::make_pair(ActivityTaskID, itemindex));
ActivityListAccSettings.insert(std::make_pair(ActivityTaskID, AccountSettings));
}
ProcessTasksData = TRUE;
if (!ActProcessTimer.IsRunning() && ApplicationSleepMode == FALSE) {
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;
}
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;
}
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;
}
// 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;
}