OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4Network2ResDlg.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 // resource display list box
17 
18 #include "C4Include.h"
20 
21 #include "game/C4Application.h"
22 #include "gui/C4GameLobby.h"
23 #include "game/C4FullScreen.h"
24 #include "network/C4Network2.h"
25 #include "control/C4PlayerInfo.h"
28 
29 // ----------- C4Network2ResDlg::ListItem ----------------------------------------------------------------
30 
31 C4Network2ResDlg::ListItem::ListItem(C4Network2ResDlg *pForResDlg, const C4Network2Res *pByRes)
32  : pSaveBtn(nullptr)
33 {
34  // init by res core (2do)
35  iResID = pByRes->getResID();
36  const char *szFilename = GetFilename(pByRes->getCore().getFileName());
37  // get size
38  int iIconSize = ::GraphicsResource.TextFont.GetLineHeight();
39  int iWidth = pForResDlg->GetItemWidth();
40  int iVerticalIndent = 2;
41  SetBounds(C4Rect(0, 0, iWidth, iIconSize+2*iVerticalIndent));
42  C4GUI::ComponentAligner ca(GetContainedClientRect(), 0,iVerticalIndent);
43  // create subcomponents
44  pFileIcon = new C4GUI::Icon(ca.GetFromLeft(iIconSize), C4GUI::Ico_Resource);
45  pLabel = new C4GUI::Label(szFilename, iIconSize + IconLabelSpacing,iVerticalIndent, ALeft);
46  pProgress = nullptr;
47  // add components
48  AddElement(pFileIcon); AddElement(pLabel);
49  // tooltip
50  SetToolTip(LoadResStr("IDS_DESC_RESOURCE"));
51  // add to listbox (will eventually get moved)
52  pForResDlg->AddElement(this);
53  // first-time update
54  Update(pByRes);
55 }
56 
57 void C4Network2ResDlg::ListItem::Update(const C4Network2Res *pByRes)
58 {
59  // update progress label
60  iProgress = pByRes->getPresentPercent();
61  if (iProgress < 100)
62  {
63  StdStrBuf progress;
64  progress.Format("%d%%", iProgress);
65  if (pProgress)
66  pProgress->SetText(progress.getData());
67  else
68  {
69  pProgress = new C4GUI::Label(progress.getData(), GetBounds().Wdt - IconLabelSpacing, 0, ARight);
70  pProgress->SetToolTip(LoadResStr("IDS_NET_RESPROGRESS_DESC"));
71  AddElement(pProgress);
72  }
73  }
74  else if (pProgress) { delete pProgress; pProgress=nullptr; }
75  // update disk icon
76  if (IsSavePossible())
77  {
78  if (!pSaveBtn)
79  {
80  pSaveBtn = new C4GUI::CallbackButtonEx<C4Network2ResDlg::ListItem, C4GUI::IconButton>(C4GUI::Ico_Save, GetToprightCornerRect(16,16,2,1), 0, this, &ListItem::OnButtonSave);
81  AddElement(pSaveBtn);
82  }
83  }
84  else if (pSaveBtn)
85  delete pSaveBtn;
86 }
87 
88 void C4Network2ResDlg::ListItem::OnButtonSave(C4GUI::Control *pButton)
89 {
90  LocalSaveResource(false);
91 }
92 
93 void C4Network2ResDlg::ListItem::OnButtonSaveConfirm(C4GUI::Element *pNull)
94 {
95  LocalSaveResource(true);
96 }
97 
98 void C4Network2ResDlg::ListItem::LocalSaveResource(bool fDoOverwrite)
99 {
100  // get associated resource
101  C4Network2Res::Ref pRes = GetRefRes();
102  if (!pRes) return;
103  const char *szResFile = pRes->getFile();
104  StdCopyStrBuf strErrCopyFile(LoadResStr("IDS_NET_ERR_COPYFILE"));
105  if (!pRes->isTempFile())
106  {
107  GetScreen()->ShowMessage(LoadResStr("IDS_NET_ERR_COPYFILE_LOCAL"), strErrCopyFile.getData(), C4GUI::Ico_Error);
108  return;
109  }
110  const char *szFilename = GetFilename(pRes->getCore().getFileName());
111  /* const char *szSpecialPath = "";
112  if (WildcardMatch(C4CFN_PlayerFiles, szFilename))
113  // write players to player path
114  szSpecialPath = Config.General.PlayerPath;
115  */
116  const char *szTarget = Config.AtUserDataPath(szFilename);
117  if (!fDoOverwrite && ItemExists(szTarget))
118  {
119  // show a confirmation dlg, asking whether the resource should be overwritten
120  GetScreen()->ShowRemoveDlg(new C4GUI::ConfirmationDialog(
121  FormatString(LoadResStr("IDS_NET_RES_SAVE_OVERWRITE"), GetFilename(szTarget)).getData(), LoadResStr("IDS_NET_RES_SAVE"),
122  new C4GUI::CallbackHandler<C4Network2ResDlg::ListItem>(this, &C4Network2ResDlg::ListItem::OnButtonSaveConfirm), C4GUI::MessageDialog::btnYesNo));
123  return;
124  }
125  if (!C4Group_CopyItem(szResFile, szTarget))
126  GetScreen()->ShowMessage(strErrCopyFile.getData(), strErrCopyFile.getData(), C4GUI::Ico_Error);
127  else
128  {
129  GetScreen()->ShowMessage(FormatString(LoadResStr("IDS_NET_RES_SAVED_DESC"), GetFilename(szTarget)).getData(),
130  LoadResStr("IDS_NET_RES_SAVED"), C4GUI::Ico_Save);
131  }
132 }
133 
134 C4Network2Res::Ref C4Network2ResDlg::ListItem::GetRefRes()
135 {
136  // forward to network reslist
138 }
139 
140 bool C4Network2ResDlg::ListItem::IsSavePossible()
141 {
142  // check resource
143  bool fCanSave = false;
144  C4Network2Res::Ref pRes = GetRefRes();
145  if (!pRes) return false;
146  // temp files from network folder only
147  if (pRes->isTempFile())
148  {
149  // check type
150  C4Network2ResType eType = pRes->getType();
151  if ((eType == NRT_Player && Config.Lobby.AllowPlayerSave) || eType == NRT_Scenario || eType == NRT_Definitions)
152  // check complete
153  if (!pRes->isLoading())
154  // save OK
155  fCanSave = true;
156  }
157  return fCanSave;
158 }
159 
160 
161 // ----------- C4Network2ResDlg -----------------------------------------------------------------------
162 
163 C4Network2ResDlg::C4Network2ResDlg(const C4Rect &rcBounds, bool fActive) : ListBox(rcBounds)
164 {
165  // 2do
166  // initially active?
167  if (fActive) Activate();
168 }
169 
171 {
172  // create timer if necessary
173  Application.Add(this);
174  // force an update
175  Update();
176 }
177 
179 {
180  // release timer if set
181  Application.Remove(this);
182 }
183 
185 {
186  // check through own resources and current res list
187  ListItem *pItem = static_cast<ListItem *>(pClientWindow->GetFirst()), *pNext;
188  C4Network2Res *pRes; int iResID=-1;
189  while ((pRes = ::Network.ResList.getRefNextRes(++iResID)))
190  {
191  iResID = pRes->getResID();
192  // resource checking: deleted resource(s) present?
193  while (pItem && (pItem->GetResID() < iResID))
194  {
195  pNext = static_cast<ListItem *>(pItem->GetNext());
196  delete pItem; pItem = pNext;
197  }
198  // same resource present for update?
199  if (pItem && pItem->GetResID() == iResID)
200  {
201  pItem->Update(pRes);
202  pItem = static_cast<ListItem *>(pItem->GetNext());
203  }
204  else
205  // not present: insert (or add if pItem=nullptr)
206  InsertElement(new ListItem(this, pRes), pItem);
207  }
208  // del trailing items
209  while (pItem)
210  {
211  pNext = static_cast<ListItem *>(pItem->GetNext());
212  delete pItem; pItem = pNext;
213  }
214 }
C4Network2ResType
Definition: C4Network2Res.h:40
char * GetFilename(char *szPath)
Definition: StdFile.cpp:55
const char * getData() const
Definition: StdBuf.h:450
C4Config Config
Definition: C4Config.cpp:837
int GetLineHeight() const
Definition: C4FontLoader.h:132
int32_t AllowPlayerSave
Definition: C4Config.h:199
bool AddElement(Element *pChild, int32_t iIndent=0)
void SetToolTip(const char *szNewTooltip, bool is_immediate=false)
Definition: C4Gui.cpp:410
C4Network2ResType getType() const
bool InsertElement(Element *pChild, Element *pInsertBefore, int32_t iIndent=0)
const int ARight
Definition: C4Surface.h:43
Definition: C4Rect.h:29
void Format(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:181
C4GraphicsResource GraphicsResource
const char * getFileName() const
Definition: C4Network2Res.h:94
void Add(StdSchedulerProc *pProc)
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
bool isLoading() const
const char * getFile() const
C4Network2 Network
Definition: C4Globals.cpp:53
const C4Network2ResCore & getCore() const
C4Network2ResDlg(const C4Rect &rcBounds, bool fActive)
Element * GetFirst()
Definition: C4Gui.h:829
C4ConfigLobby Lobby
Definition: C4Config.h:257
bool C4Group_CopyItem(const char *szSource, const char *szTarget1, bool fNoSort, bool fResetAttributes)
Definition: C4Group.cpp:100
C4Network2Res::Ref getRefRes(int32_t iResID)
C4Network2ResList ResList
Definition: C4Network2.h:113
void Remove(StdSchedulerProc *pProc)
int32_t GetItemWidth()
Definition: C4Gui.h:1545
const char * AtUserDataPath(const char *szFilename)
Definition: C4Config.cpp:530
const int ALeft
Definition: C4Surface.h:43
bool isTempFile() const
Element * pNext
Definition: C4Gui.h:377
C4Network2Res::Ref getRefNextRes(int32_t iResID)
bool ItemExists(const char *szItemName)
Definition: StdFile.h:75
int32_t getPresentPercent() const
C4Application Application
Definition: C4Globals.cpp:44
int32_t getResID() const
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:277
ScrollWindow * pClientWindow
Definition: C4Gui.h:1509