boinc/clientgui/ViewTransfers.cpp

706 lines
21 KiB
C++

// Berkeley Open Infrastructure for Network Computing
// http://boinc.berkeley.edu
// Copyright (C) 2005 University of California
//
// This is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation;
// either version 2.1 of the License, or (at your option) any later version.
//
// This software 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 Lesser General Public License for more details.
//
// To view the GNU Lesser General Public License visit
// http://www.gnu.org/copyleft/lesser.html
// or write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#if defined(__GNUG__) && !defined(__APPLE__)
#pragma implementation "ViewTransfers.h"
#endif
#include "stdwx.h"
#include "BOINCGUIApp.h"
#include "BOINCBaseFrame.h"
#include "MainDocument.h"
#include "AdvancedFrame.h"
#include "BOINCTaskCtrl.h"
#include "BOINCListCtrl.h"
#include "ViewTransfers.h"
#include "Events.h"
#include "error_numbers.h"
#include "res/xfer.xpm"
#define COLUMN_PROJECT 0
#define COLUMN_FILE 1
#define COLUMN_PROGRESS 2
#define COLUMN_SIZE 3
#define COLUMN_TIME 4
#define COLUMN_SPEED 5
#define COLUMN_STATUS 6
// buttons in the "tasks" area
#define BTN_RETRY 0
#define BTN_ABORT 1
CTransfer::CTransfer() {
}
CTransfer::~CTransfer() {
m_strProjectName.Clear();
m_strFileName.Clear();
m_fProgress = 0.0;
m_fBytesXferred = 0.0;
m_fTotalBytes = 0.0;
m_dTime = 0.0;
m_dSpeed = 0.0;
m_strStatus.Clear();
}
IMPLEMENT_DYNAMIC_CLASS(CViewTransfers, CBOINCBaseView)
BEGIN_EVENT_TABLE (CViewTransfers, CBOINCBaseView)
EVT_BUTTON(ID_TASK_TRANSFERS_RETRYNOW, CViewTransfers::OnTransfersRetryNow)
EVT_BUTTON(ID_TASK_TRANSFERS_ABORT, CViewTransfers::OnTransfersAbort)
EVT_LIST_ITEM_SELECTED(ID_LIST_TRANSFERSVIEW, CViewTransfers::OnListSelected)
EVT_LIST_ITEM_DESELECTED(ID_LIST_TRANSFERSVIEW, CViewTransfers::OnListDeselected)
EVT_LIST_COL_CLICK(ID_LIST_TRANSFERSVIEW, CViewTransfers::OnColClick)
EVT_LIST_CACHE_HINT(ID_LIST_TRANSFERSVIEW, CViewTransfers::OnCacheHint)
END_EVENT_TABLE ()
static CViewTransfers* MyCViewTransfers;
static bool CompareViewTransferItems(int iRowIndex1, int iRowIndex2) {
CTransfer* transfer1 = MyCViewTransfers->m_TransferCache.at(iRowIndex1);
CTransfer* transfer2 = MyCViewTransfers->m_TransferCache.at(iRowIndex2);
int result = 0;
switch (MyCViewTransfers->m_iSortColumn) {
case COLUMN_PROJECT:
result = transfer1->m_strProjectName.CmpNoCase(transfer2->m_strProjectName);
break;
case COLUMN_FILE:
result = transfer1->m_strFileName.CmpNoCase(transfer2->m_strFileName);
break;
case COLUMN_PROGRESS:
if (transfer1->m_fProgress < transfer2->m_fProgress) {
result = -1;
} else if (transfer1->m_fProgress > transfer2->m_fProgress) {
result = 1;
}
break;
case COLUMN_SIZE:
if (transfer1->m_fBytesXferred < transfer2->m_fBytesXferred) {
result = -1;
} else if (transfer1->m_fBytesXferred > transfer2->m_fBytesXferred) {
result = 1;
}
break;
case COLUMN_TIME:
if (transfer1->m_dTime < transfer2->m_dTime) {
result = -1;
} else if (transfer1->m_dTime > transfer2->m_dTime) {
result = 1;
}
break;
case COLUMN_SPEED:
if (transfer1->m_dSpeed < transfer2->m_dSpeed) {
result = -1;
} else if (transfer1->m_dSpeed > transfer2->m_dSpeed) {
result = 1;
}
break;
case COLUMN_STATUS:
result = transfer1->m_strStatus.CmpNoCase(transfer2->m_strStatus);
break;
}
// Always return FALSE for equality (result == 0)
return (MyCViewTransfers->m_bReverseSort ? (result > 0) : (result <= 0));
}
CViewTransfers::CViewTransfers()
{}
CViewTransfers::CViewTransfers(wxNotebook* pNotebook) :
CBOINCBaseView(pNotebook, ID_TASK_TRANSFERSVIEW, DEFAULT_TASK_FLAGS, ID_LIST_TRANSFERSVIEW, DEFAULT_LIST_MULTI_SEL_FLAGS)
{
CTaskItemGroup* pGroup = NULL;
CTaskItem* pItem = NULL;
wxASSERT(m_pTaskPane);
wxASSERT(m_pListPane);
//
// Setup View
//
pGroup = new CTaskItemGroup( _("Commands") );
m_TaskGroups.push_back( pGroup );
pItem = new CTaskItem(
_("Retry Now"),
_("Click 'Retry now' to transfer the file now"),
ID_TASK_TRANSFERS_RETRYNOW
);
pGroup->m_Tasks.push_back( pItem );
pItem = new CTaskItem(
_("Abort Transfer"),
_("Click 'Abort transfer' to delete the file from the transfer queue. This will prevent you from being granted credit for this result."),
ID_TASK_TRANSFERS_ABORT
);
pGroup->m_Tasks.push_back( pItem );
// Create Task Pane Items
m_pTaskPane->UpdateControls();
// Create List Pane Items
m_pListPane->InsertColumn(COLUMN_PROJECT, _("Project"), wxLIST_FORMAT_LEFT, 125);
m_pListPane->InsertColumn(COLUMN_FILE, _("File"), wxLIST_FORMAT_LEFT, 205);
m_pListPane->InsertColumn(COLUMN_PROGRESS, _("Progress"), wxLIST_FORMAT_CENTRE, 60);
m_pListPane->InsertColumn(COLUMN_SIZE, _("Size"), wxLIST_FORMAT_LEFT, 80);
m_pListPane->InsertColumn(COLUMN_TIME, _("Elapsed Time"), wxLIST_FORMAT_LEFT, 80);
m_pListPane->InsertColumn(COLUMN_SPEED, _("Speed"), wxLIST_FORMAT_LEFT, 80);
m_pListPane->InsertColumn(COLUMN_STATUS, _("Status"), wxLIST_FORMAT_LEFT, 150);
m_iProgressColumn = COLUMN_PROGRESS;
// Needed by static sort routine;
MyCViewTransfers = this;
m_funcSortCompare = CompareViewTransferItems;
UpdateSelection();
}
CViewTransfers::~CViewTransfers() {
EmptyCache();
EmptyTasks();
}
wxString& CViewTransfers::GetViewName() {
static wxString strViewName(_("Transfers"));
return strViewName;
}
wxString& CViewTransfers::GetViewDisplayName() {
static wxString strViewName(_("Transfers"));
return strViewName;
}
const char** CViewTransfers::GetViewIcon() {
return xfer_xpm;
}
void CViewTransfers::OnTransfersRetryNow( wxCommandEvent& WXUNUSED(event) ) {
wxLogTrace(wxT("Function Start/End"), wxT("CViewTransfers::OnTransfersRetryNow - Function Begin"));
CMainDocument* pDoc = wxGetApp().GetDocument();
CAdvancedFrame* pFrame = wxDynamicCast(GetParent()->GetParent()->GetParent(), CAdvancedFrame);
int row;
wxASSERT(pDoc);
wxASSERT(wxDynamicCast(pDoc, CMainDocument));
wxASSERT(pFrame);
wxASSERT(wxDynamicCast(pFrame, CAdvancedFrame));
pFrame->UpdateStatusText(_("Retrying transfer now..."));
row = -1;
while (1) {
// Step through all selected items
row = m_pListPane->GetNextItem(row, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
if (row < 0) break;
pDoc->TransferRetryNow(m_iSortedIndexes[row]);
}
pFrame->UpdateStatusText(wxT(""));
UpdateSelection();
pFrame->ResetReminderTimers();
pFrame->FireRefreshView();
wxLogTrace(wxT("Function Start/End"), wxT("CViewTransfers::OnTransfersRetryNow - Function End"));
}
void CViewTransfers::OnTransfersAbort( wxCommandEvent& WXUNUSED(event) ) {
wxLogTrace(wxT("Function Start/End"), wxT("CViewTransfers::OnTransfersAbort - Function Begin"));
wxInt32 iAnswer = 0;
wxString strMessage = wxEmptyString;
CMainDocument* pDoc = wxGetApp().GetDocument();
CAdvancedFrame* pFrame = wxDynamicCast(GetParent()->GetParent()->GetParent(), CAdvancedFrame);
CTransfer* pTransfer = NULL;
int row;
wxASSERT(pDoc);
wxASSERT(wxDynamicCast(pDoc, CMainDocument));
wxASSERT(pFrame);
wxASSERT(wxDynamicCast(pFrame, CAdvancedFrame));
if (!pDoc->IsUserAuthorized())
return;
pFrame->UpdateStatusText(_("Aborting transfer..."));
row = -1;
while (1) {
// Step through all selected items
row = m_pListPane->GetNextItem(row, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
if (row < 0) break;
pTransfer = m_TransferCache.at(m_iSortedIndexes[row]);
strMessage.Printf(
_("Are you sure you want to abort this file transfer '%s'?\nNOTE: Aborting a transfer will invalidate a task and you\nwill not receive credit for it."),
pTransfer->m_strFileName.c_str()
);
iAnswer = ::wxMessageBox(
strMessage,
_("Abort File Transfer"),
wxYES_NO | wxICON_QUESTION,
this
);
if (wxYES == iAnswer) {
pDoc->TransferAbort(m_iSortedIndexes[row]);
}
}
pFrame->UpdateStatusText(wxT(""));
UpdateSelection();
pFrame->FireRefreshView();
wxLogTrace(wxT("Function Start/End"), wxT("CViewTransfers::OnTransfersAbort - Function End"));
}
wxInt32 CViewTransfers::GetDocCount() {
return wxGetApp().GetDocument()->GetTransferCount();
}
wxString CViewTransfers::OnListGetItemText(long item, long column) const {
wxString strBuffer = wxEmptyString;
switch(column) {
case COLUMN_PROJECT:
FormatProjectName(item, strBuffer);
break;
case COLUMN_FILE:
FormatFileName(item, strBuffer);
break;
case COLUMN_PROGRESS:
FormatProgress(item, strBuffer);
break;
case COLUMN_SIZE:
FormatSize(item, strBuffer);
break;
case COLUMN_TIME:
FormatTime(item, strBuffer);
break;
case COLUMN_SPEED:
FormatSpeed(item, strBuffer);
break;
case COLUMN_STATUS:
FormatStatus(item, strBuffer);
break;
}
return strBuffer;
}
wxInt32 CViewTransfers::AddCacheElement() {
CTransfer* pItem = new CTransfer();
wxASSERT(pItem);
if (pItem) {
m_TransferCache.push_back(pItem);
m_iSortedIndexes.Add((int)m_TransferCache.size()-1);
return 0;
}
return -1;
}
wxInt32 CViewTransfers::EmptyCache() {
unsigned int i;
for (i=0; i<m_TransferCache.size(); i++) {
delete m_TransferCache[i];
}
m_TransferCache.clear();
m_iSortedIndexes.Clear();
return 0;
}
wxInt32 CViewTransfers::GetCacheCount() {
return (wxInt32)m_TransferCache.size();
}
wxInt32 CViewTransfers::RemoveCacheElement() {
unsigned int i;
delete m_TransferCache.back();
m_TransferCache.erase(m_TransferCache.end() - 1);
m_iSortedIndexes.Clear();
for (i=0; i<m_TransferCache.size(); i++) {
m_iSortedIndexes.Add(i);
}
return 0;
}
void CViewTransfers::UpdateSelection() {
CTaskItemGroup* pGroup = m_TaskGroups[0];
CBOINCBaseView::PreUpdateSelection();
if (m_pListPane->GetSelectedItemCount()) {
m_pTaskPane->EnableTaskGroupTasks(pGroup);
} else {
m_pTaskPane->DisableTaskGroupTasks(pGroup);
}
CBOINCBaseView::PostUpdateSelection();
}
bool CViewTransfers::SynchronizeCacheItem(wxInt32 iRowIndex, wxInt32 iColumnIndex) {
wxString strDocumentText = wxEmptyString;
float fDocumentFloat = 0.0;
double fDocumentDouble = 0.0;
CTransfer* transfer = m_TransferCache.at(m_iSortedIndexes[iRowIndex]);
bool bNeedRefresh = false;
strDocumentText.Empty();
switch(iColumnIndex) {
case COLUMN_PROJECT:
GetDocProjectName(m_iSortedIndexes[iRowIndex], strDocumentText);
if (!strDocumentText.IsSameAs(transfer->m_strProjectName)) {
transfer->m_strProjectName = strDocumentText;
bNeedRefresh = true;
}
break;
case COLUMN_FILE:
GetDocFileName(m_iSortedIndexes[iRowIndex], strDocumentText);
if (!strDocumentText.IsSameAs(transfer->m_strFileName)) {
transfer->m_strFileName = strDocumentText;
bNeedRefresh = true;
}
break;
case COLUMN_PROGRESS:
GetDocProgress(m_iSortedIndexes[iRowIndex], fDocumentFloat);
if (fDocumentFloat != transfer->m_fProgress) {
transfer->m_fProgress = fDocumentFloat;
bNeedRefresh = true;
}
break;
case COLUMN_SIZE:
GetDocBytesXferred(m_iSortedIndexes[iRowIndex], fDocumentDouble);
if (fDocumentDouble != transfer->m_fBytesXferred) {
transfer->m_fBytesXferred = fDocumentDouble;
bNeedRefresh = true;
}
GetDocTotalBytes(m_iSortedIndexes[iRowIndex], fDocumentDouble);
if (fDocumentDouble != transfer->m_fTotalBytes) {
transfer->m_fTotalBytes = fDocumentDouble;
bNeedRefresh = true;
}
break;
case COLUMN_TIME:
GetDocTime(m_iSortedIndexes[iRowIndex], fDocumentDouble);
if (fDocumentDouble != transfer->m_dTime) {
transfer->m_dTime = fDocumentDouble;
bNeedRefresh = true;
}
break;
case COLUMN_SPEED:
GetDocSpeed(m_iSortedIndexes[iRowIndex], fDocumentDouble);
if (fDocumentDouble != transfer->m_dSpeed) {
transfer->m_dSpeed = fDocumentDouble;
bNeedRefresh = true;
}
break;
case COLUMN_STATUS:
GetDocStatus(m_iSortedIndexes[iRowIndex], strDocumentText);
if (!strDocumentText.IsSameAs(transfer->m_strStatus)) {
transfer->m_strStatus = strDocumentText;
return true;
}
break;
}
return bNeedRefresh;
}
void CViewTransfers::GetDocProjectName(wxInt32 item, wxString& strBuffer) const {
FILE_TRANSFER* transfer = wxGetApp().GetDocument()->file_transfer(item);
if (transfer) {
strBuffer = wxString(transfer->project_name.c_str(), wxConvUTF8);
} else {
strBuffer = wxEmptyString;
}
}
wxInt32 CViewTransfers::FormatProjectName(wxInt32 item, wxString& strBuffer) const {
CTransfer* transfer = m_TransferCache.at(m_iSortedIndexes[item]);
strBuffer = HtmlEntityDecode(transfer->m_strProjectName);
return 0;
}
void CViewTransfers::GetDocFileName(wxInt32 item, wxString& strBuffer) const {
FILE_TRANSFER* transfer = wxGetApp().GetDocument()->file_transfer(item);
if (transfer) {
strBuffer = wxString(transfer->name.c_str(), wxConvUTF8);
} else {
strBuffer = wxEmptyString;
}
}
wxInt32 CViewTransfers::FormatFileName(wxInt32 item, wxString& strBuffer) const {
CTransfer* transfer = m_TransferCache.at(m_iSortedIndexes[item]);
strBuffer = transfer->m_strFileName;
return 0;
}
void CViewTransfers::GetDocProgress(wxInt32 item, float& fBuffer) const {
float fBytesSent = 0;
float fFileSize = 0;
FILE_TRANSFER* transfer = wxGetApp().GetDocument()->file_transfer(item);
fBuffer = 0;
if (transfer) {
fBytesSent = transfer->bytes_xferred;
fFileSize = transfer->nbytes;
}
// Curl apparently counts the HTTP header in byte count.
// Prevent this from causing > 100% display
//
if (fBytesSent > fFileSize) {
fBytesSent = fFileSize;
}
if (fFileSize) {
fBuffer = floor((fBytesSent / fFileSize) * 10000)/100;
}
}
wxInt32 CViewTransfers::FormatProgress(wxInt32 item, wxString& strBuffer) const {
CTransfer* transfer = m_TransferCache.at(m_iSortedIndexes[item]);
strBuffer.Printf(wxT("%.2f%%"), transfer->m_fProgress);
return 0;
}
void CViewTransfers::GetDocBytesXferred(wxInt32 item, double& fBuffer) const {
FILE_TRANSFER* transfer = wxGetApp().GetDocument()->file_transfer(item);
if (transfer) {
fBuffer = transfer->bytes_xferred;
} else {
fBuffer = 0.0;
}
}
void CViewTransfers::GetDocTotalBytes(wxInt32 item, double& fBuffer) const {
FILE_TRANSFER* transfer = wxGetApp().GetDocument()->file_transfer(item);
if (transfer) {
fBuffer = transfer->nbytes;
} else {
fBuffer = 0.0;
}
}
wxInt32 CViewTransfers::FormatSize(wxInt32 item, wxString& strBuffer) const {
double xTera = 1099511627776.0;
double xGiga = 1073741824.0;
double xMega = 1048576.0;
double xKilo = 1024.0;
CTransfer* transfer = m_TransferCache.at(m_iSortedIndexes[item]);
double fBytesSent = transfer->m_fBytesXferred;
double fFileSize = transfer->m_fTotalBytes;
if (fFileSize != 0) {
if (fFileSize >= xTera) {
strBuffer.Printf(wxT("%0.2f/%0.2f TB"), fBytesSent/xTera, fFileSize/xTera);
} else if (fFileSize >= xGiga) {
strBuffer.Printf(wxT("%0.2f/%0.2f GB"), fBytesSent/xGiga, fFileSize/xGiga);
} else if (fFileSize >= xMega) {
strBuffer.Printf(wxT("%0.2f/%0.2f MB"), fBytesSent/xMega, fFileSize/xMega);
} else if (fFileSize >= xKilo) {
strBuffer.Printf(wxT("%0.2f/%0.2f KB"), fBytesSent/xKilo, fFileSize/xKilo);
} else {
strBuffer.Printf(wxT("%0.0f/%0.0f bytes"), fBytesSent, fFileSize);
}
} else {
if (fBytesSent >= xTera) {
strBuffer.Printf(wxT("%0.2f TB"), fBytesSent/xTera);
} else if (fBytesSent >= xGiga) {
strBuffer.Printf(wxT("%0.2f GB"), fBytesSent/xGiga);
} else if (fBytesSent >= xMega) {
strBuffer.Printf(wxT("%0.2f MB"), fBytesSent/xMega);
} else if (fBytesSent >= xKilo) {
strBuffer.Printf(wxT("%0.2f KB"), fBytesSent/xKilo);
} else {
strBuffer.Printf(wxT("%0.0f bytes"), fBytesSent);
}
}
return 0;
}
void CViewTransfers::GetDocTime(wxInt32 item, double& fBuffer) const {
FILE_TRANSFER* transfer = wxGetApp().GetDocument()->file_transfer(item);
if (transfer) {
fBuffer = transfer->time_so_far;
} else {
fBuffer = 0.0;
}
}
wxInt32 CViewTransfers::FormatTime(wxInt32 item, wxString& strBuffer) const {
wxInt32 iHour = 0;
wxInt32 iMin = 0;
wxInt32 iSec = 0;
wxTimeSpan ts;
CTransfer* transfer = m_TransferCache.at(m_iSortedIndexes[item]);
float fBuffer = transfer->m_dTime;
iHour = (wxInt32)(fBuffer / (60 * 60));
iMin = (wxInt32)(fBuffer / 60) % 60;
iSec = (wxInt32)(fBuffer) % 60;
ts = wxTimeSpan(iHour, iMin, iSec);
strBuffer = ts.Format();
return 0;
}
void CViewTransfers::GetDocSpeed(wxInt32 item, double& fBuffer) const {
FILE_TRANSFER* transfer = wxGetApp().GetDocument()->file_transfer(item);
if (transfer) {
if (transfer->xfer_active)
fBuffer = transfer->xfer_speed / 1024;
else
fBuffer = 0.0;
}
}
wxInt32 CViewTransfers::FormatSpeed(wxInt32 item, wxString& strBuffer) const {
CTransfer* transfer = m_TransferCache.at(m_iSortedIndexes[item]);
strBuffer.Printf(wxT("%.2f KBps"), transfer->m_dSpeed);
return 0;
}
void CViewTransfers::GetDocStatus(wxInt32 item, wxString& strBuffer) const {
CMainDocument* doc = wxGetApp().GetDocument();
FILE_TRANSFER* transfer = wxGetApp().GetDocument()->file_transfer(item);
CC_STATUS status;
wxASSERT(doc);
wxASSERT(wxDynamicCast(doc, CMainDocument));
doc->GetCoreClientStatus(status);
wxDateTime dtNextRequest((time_t)transfer->next_request_time);
wxDateTime dtNow(wxDateTime::Now());
if (transfer) {
if (dtNextRequest > dtNow) {
wxTimeSpan tsNextRequest(dtNextRequest - dtNow);
strBuffer = _("Retry in ") + tsNextRequest.Format();
} else if (ERR_GIVEUP_DOWNLOAD == transfer->status) {
strBuffer = _("Download failed");
} else if (ERR_GIVEUP_UPLOAD == transfer->status) {
strBuffer = _("Upload failed");
} else {
if (status.network_suspend_reason) {
strBuffer = _("Suspended");
} else {
if (transfer->xfer_active) {
strBuffer = transfer->generated_locally? _("Uploading") : _("Downloading");
} else {
strBuffer = transfer->generated_locally? _("Upload pending") : _("Download pending");
}
}
}
}
}
wxInt32 CViewTransfers::FormatStatus(wxInt32 item, wxString& strBuffer) const {
CTransfer* transfer = m_TransferCache.at(m_iSortedIndexes[item]);
strBuffer = transfer->m_strStatus;
return 0;
}
double CViewTransfers::GetProgressValue(long item) {
double fBytesSent = 0;
double fFileSize = 0;
FILE_TRANSFER* transfer = wxGetApp().GetDocument()->file_transfer(item);
if (transfer) {
fBytesSent = transfer->bytes_xferred;
fFileSize = transfer->nbytes;
}
// Curl apparently counts the HTTP header in byte count.
// Prevent this from causing > 100% display
//
if (fBytesSent > fFileSize) {
fBytesSent = fFileSize;
}
if ( 0.0 == fFileSize ) return 0.0;
return (fBytesSent / fFileSize);
}
const char *BOINC_RCSID_7aadb93332 = "$Id$";