openclonk/src/network/C4Network2ResDlg.cpp

215 lines
6.5 KiB
C++
Raw Normal View History

2009-05-08 13:28:41 +00:00
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
2016-04-03 18:18:29 +00:00
* Copyright (c) 2009-2016, The OpenClonk Team and contributors
2009-05-08 13:28:41 +00:00
*
* Distributed under the terms of the ISC license; see accompanying file
* "COPYING" for details.
2009-05-08 13:28:41 +00:00
*
* "Clonk" is a registered trademark of Matthes Bender, used with permission.
* See accompanying file "TRADEMARK" for details.
2009-05-08 13:28:41 +00:00
*
* To redistribute this file separately, substitute the full license texts
* for the above references.
2009-05-08 13:28:41 +00:00
*/
// resource display list box
#include "C4Include.h"
#include "network/C4Network2Dialogs.h"
#include "control/C4PlayerInfo.h"
#include "game/C4Application.h"
#include "game/C4FullScreen.h"
#include "graphics/C4GraphicsResource.h"
#include "gui/C4GameLobby.h"
#include "network/C4Network2.h"
#include "network/C4Network2Players.h"
2009-05-08 13:28:41 +00:00
// ----------- C4Network2ResDlg::ListItem ----------------------------------------------------------------
C4Network2ResDlg::ListItem::ListItem(C4Network2ResDlg *pForResDlg, const C4Network2Res *pByRes)
: pSaveBtn(nullptr)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// init by res core (2do)
iResID = pByRes->getResID();
const char *szFilename = GetFilename(pByRes->getCore().getFileName());
// get size
int iIconSize = ::GraphicsResource.TextFont.GetLineHeight();
2009-05-08 13:28:41 +00:00
int iWidth = pForResDlg->GetItemWidth();
int iVerticalIndent = 2;
SetBounds(C4Rect(0, 0, iWidth, iIconSize+2*iVerticalIndent));
C4GUI::ComponentAligner ca(GetContainedClientRect(), 0,iVerticalIndent);
// create subcomponents
pFileIcon = new C4GUI::Icon(ca.GetFromLeft(iIconSize), C4GUI::Ico_Resource);
pLabel = new C4GUI::Label(szFilename, iIconSize + IconLabelSpacing,iVerticalIndent, ALeft);
pProgress = nullptr;
2009-05-08 13:28:41 +00:00
// add components
AddElement(pFileIcon); AddElement(pLabel);
// tooltip
SetToolTip(LoadResStr("IDS_DESC_RESOURCE"));
// add to listbox (will eventually get moved)
pForResDlg->AddElement(this);
// first-time update
Update(pByRes);
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4Network2ResDlg::ListItem::Update(const C4Network2Res *pByRes)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// update progress label
iProgress = pByRes->getPresentPercent();
if (iProgress < 100)
2010-03-28 18:58:01 +00:00
{
2009-05-11 13:09:53 +00:00
StdStrBuf progress;
progress.Format("%d%%", iProgress);
2009-05-08 13:28:41 +00:00
if (pProgress)
2009-05-11 13:09:53 +00:00
pProgress->SetText(progress.getData());
2009-05-08 13:28:41 +00:00
else
2010-03-28 18:58:01 +00:00
{
2009-05-11 13:09:53 +00:00
pProgress = new C4GUI::Label(progress.getData(), GetBounds().Wdt - IconLabelSpacing, 0, ARight);
2009-05-08 13:28:41 +00:00
pProgress->SetToolTip(LoadResStr("IDS_NET_RESPROGRESS_DESC"));
AddElement(pProgress);
}
2010-03-28 18:58:01 +00:00
}
else if (pProgress) { delete pProgress; pProgress=nullptr; }
2009-05-08 13:28:41 +00:00
// update disk icon
if (IsSavePossible())
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
if (!pSaveBtn)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
pSaveBtn = new C4GUI::CallbackButtonEx<C4Network2ResDlg::ListItem, C4GUI::IconButton>(C4GUI::Ico_Save, GetToprightCornerRect(16,16,2,1), 0, this, &ListItem::OnButtonSave);
AddElement(pSaveBtn);
}
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
else if (pSaveBtn)
delete pSaveBtn;
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4Network2ResDlg::ListItem::OnButtonSave(C4GUI::Control *pButton)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
LocalSaveResource(false);
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4Network2ResDlg::ListItem::OnButtonSaveConfirm(C4GUI::Element *pNull)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
LocalSaveResource(true);
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4Network2ResDlg::ListItem::LocalSaveResource(bool fDoOverwrite)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// get associated resource
C4Network2Res::Ref pRes = GetRefRes();
if (!pRes) return;
const char *szResFile = pRes->getFile();
2010-03-28 18:58:01 +00:00
StdCopyStrBuf strErrCopyFile(LoadResStr("IDS_NET_ERR_COPYFILE"));
if (!pRes->isTempFile())
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
GetScreen()->ShowMessage(LoadResStr("IDS_NET_ERR_COPYFILE_LOCAL"), strErrCopyFile.getData(), C4GUI::Ico_Error);
return;
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
const char *szFilename = GetFilename(pRes->getCore().getFileName());
2010-03-28 18:58:01 +00:00
/* const char *szSpecialPath = "";
if (WildcardMatch(C4CFN_PlayerFiles, szFilename))
// write players to player path
szSpecialPath = Config.General.PlayerPath;
*/
2009-05-08 13:28:41 +00:00
const char *szTarget = Config.AtUserDataPath(szFilename);
if (!fDoOverwrite && ItemExists(szTarget))
2010-03-28 18:58:01 +00:00
{
// show a confirmation dlg, asking whether the resource should be overwritten
2009-05-11 13:09:53 +00:00
GetScreen()->ShowRemoveDlg(new C4GUI::ConfirmationDialog(
2010-03-28 18:58:01 +00:00
FormatString(LoadResStr("IDS_NET_RES_SAVE_OVERWRITE"), GetFilename(szTarget)).getData(), LoadResStr("IDS_NET_RES_SAVE"),
new C4GUI::CallbackHandler<C4Network2ResDlg::ListItem>(this, &C4Network2ResDlg::ListItem::OnButtonSaveConfirm), C4GUI::MessageDialog::btnYesNo));
2009-05-08 13:28:41 +00:00
return;
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
if (!C4Group_CopyItem(szResFile, szTarget))
GetScreen()->ShowMessage(strErrCopyFile.getData(), strErrCopyFile.getData(), C4GUI::Ico_Error);
else
2010-03-28 18:58:01 +00:00
{
2009-05-11 13:09:53 +00:00
GetScreen()->ShowMessage(FormatString(LoadResStr("IDS_NET_RES_SAVED_DESC"), GetFilename(szTarget)).getData(),
2010-03-28 18:58:01 +00:00
LoadResStr("IDS_NET_RES_SAVED"), C4GUI::Ico_Save);
2009-05-08 13:28:41 +00:00
}
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
C4Network2Res::Ref C4Network2ResDlg::ListItem::GetRefRes()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// forward to network reslist
2009-06-05 15:19:46 +00:00
return ::Network.ResList.getRefRes(iResID);
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
bool C4Network2ResDlg::ListItem::IsSavePossible()
2010-03-28 18:58:01 +00:00
{
// check resource
2009-05-08 13:28:41 +00:00
bool fCanSave = false;
C4Network2Res::Ref pRes = GetRefRes();
if (!pRes) return false;
// temp files from network folder only
if (pRes->isTempFile())
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// check type
C4Network2ResType eType = pRes->getType();
if ((eType == NRT_Player && Config.Lobby.AllowPlayerSave) || eType == NRT_Scenario || eType == NRT_Definitions)
// check complete
if (!pRes->isLoading())
// save OK
fCanSave = true;
}
2010-03-28 18:58:01 +00:00
return fCanSave;
}
2009-05-08 13:28:41 +00:00
// ----------- C4Network2ResDlg -----------------------------------------------------------------------
C4Network2ResDlg::C4Network2ResDlg(const C4Rect &rcBounds, bool fActive) : ListBox(rcBounds)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// 2do
// initially active?
if (fActive) Activate();
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4Network2ResDlg::Activate()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// create timer if necessary
Application.Add(this);
// force an update
Update();
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4Network2ResDlg::Deactivate()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// release timer if set
Application.Remove(this);
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4Network2ResDlg::Update()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// check through own resources and current res list
ListItem *pItem = static_cast<ListItem *>(pClientWindow->GetFirst()), *pNext;
C4Network2Res *pRes; int iResID=-1;
2010-01-25 04:00:59 +00:00
while ((pRes = ::Network.ResList.getRefNextRes(++iResID)))
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
iResID = pRes->getResID();
// resource checking: deleted resource(s) present?
2009-05-08 13:28:41 +00:00
while (pItem && (pItem->GetResID() < iResID))
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
pNext = static_cast<ListItem *>(pItem->GetNext());
delete pItem; pItem = pNext;
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
// same resource present for update?
if (pItem && pItem->GetResID() == iResID)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
pItem->Update(pRes);
pItem = static_cast<ListItem *>(pItem->GetNext());
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
else
// not present: insert (or add if pItem=nullptr)
2009-05-08 13:28:41 +00:00
InsertElement(new ListItem(this, pRes), pItem);
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
// del trailing items
while (pItem)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
pNext = static_cast<ListItem *>(pItem->GetNext());
delete pItem; pItem = pNext;
}
2010-03-28 18:58:01 +00:00
}