OpenTTD
network_content_gui.cpp
Go to the documentation of this file.
1 /* $Id: network_content_gui.cpp 26921 2014-09-25 19:27:07Z peter1138 $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD 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 2.
6  * OpenTTD 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.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #if defined(ENABLE_NETWORK)
13 #include "../stdafx.h"
14 #include "../strings_func.h"
15 #include "../gfx_func.h"
16 #include "../window_func.h"
17 #include "../error.h"
18 #include "../ai/ai.hpp"
19 #include "../game/game.hpp"
20 #include "../base_media_base.h"
21 #include "../sortlist_type.h"
22 #include "../stringfilter_type.h"
23 #include "../querystring_gui.h"
24 #include "../core/geometry_func.hpp"
25 #include "../textfile_gui.h"
26 #include "network_content_gui.h"
27 
28 
29 #include "table/strings.h"
30 #include "../table/sprites.h"
31 
32 #include "../safeguards.h"
33 
34 
36 static bool _accepted_external_search = false;
37 
38 
41  const ContentInfo *ci;
42 
44  {
45  const char *textfile = this->ci->GetTextfile(file_type);
46  this->LoadTextfile(textfile, GetContentInfoSubDir(this->ci->type));
47  }
48 
49  StringID GetTypeString() const
50  {
51  switch (this->ci->type) {
52  case CONTENT_TYPE_NEWGRF: return STR_CONTENT_TYPE_NEWGRF;
53  case CONTENT_TYPE_BASE_GRAPHICS: return STR_CONTENT_TYPE_BASE_GRAPHICS;
54  case CONTENT_TYPE_BASE_SOUNDS: return STR_CONTENT_TYPE_BASE_SOUNDS;
55  case CONTENT_TYPE_BASE_MUSIC: return STR_CONTENT_TYPE_BASE_MUSIC;
56  case CONTENT_TYPE_AI: return STR_CONTENT_TYPE_AI;
57  case CONTENT_TYPE_AI_LIBRARY: return STR_CONTENT_TYPE_AI_LIBRARY;
58  case CONTENT_TYPE_GAME: return STR_CONTENT_TYPE_GAME_SCRIPT;
59  case CONTENT_TYPE_GAME_LIBRARY: return STR_CONTENT_TYPE_GS_LIBRARY;
60  case CONTENT_TYPE_SCENARIO: return STR_CONTENT_TYPE_SCENARIO;
61  case CONTENT_TYPE_HEIGHTMAP: return STR_CONTENT_TYPE_HEIGHTMAP;
62  default: NOT_REACHED();
63  }
64  }
65 
66  /* virtual */ void SetStringParameters(int widget) const
67  {
68  if (widget == WID_TF_CAPTION) {
69  SetDParam(0, this->GetTypeString());
70  SetDParamStr(1, this->ci->name);
71  }
72  }
73 };
74 
75 void ShowContentTextfileWindow(TextfileType file_type, const ContentInfo *ci)
76 {
78  new ContentTextfileWindow(file_type, ci);
79 }
80 
83  NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CONTENT_DOWNLOAD_TITLE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
84  NWidget(WWT_PANEL, COLOUR_GREY, WID_NCDS_BACKGROUND),
88  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCDS_CANCELOK), SetMinimalSize(101, 12), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
89  NWidget(NWID_SPACER), SetFill(1, 0),
90  EndContainer(),
92  EndContainer(),
93 };
94 
97  WDP_CENTER, NULL, 0, 0,
99  WDF_MODAL,
100  _nested_network_content_download_status_window_widgets, lengthof(_nested_network_content_download_status_window_widgets)
101 );
102 
104  Window(desc), cur_id(UINT32_MAX)
105 {
108 
110 }
111 
113 {
115 }
116 
117 /* virtual */ void BaseNetworkContentDownloadStatusWindow::DrawWidget(const Rect &r, int widget) const
118 {
119  if (widget != WID_NCDS_BACKGROUND) return;
120 
121  /* Draw nice progress bar :) */
122  DrawFrameRect(r.left + 20, r.top + 4, r.left + 20 + (int)((this->width - 40LL) * this->downloaded_bytes / this->total_bytes), r.top + 14, COLOUR_MAUVE, FR_NONE);
123 
124  int y = r.top + 20;
125  SetDParam(0, this->downloaded_bytes);
126  SetDParam(1, this->total_bytes);
127  SetDParam(2, this->downloaded_bytes * 100LL / this->total_bytes);
128  DrawString(r.left + 2, r.right - 2, y, STR_CONTENT_DOWNLOAD_PROGRESS_SIZE, TC_FROMSTRING, SA_HOR_CENTER);
129 
130  StringID str;
131  if (this->downloaded_bytes == this->total_bytes) {
132  str = STR_CONTENT_DOWNLOAD_COMPLETE;
133  } else if (!StrEmpty(this->name)) {
134  SetDParamStr(0, this->name);
135  SetDParam(1, this->downloaded_files);
136  SetDParam(2, this->total_files);
137  str = STR_CONTENT_DOWNLOAD_FILE;
138  } else {
139  str = STR_CONTENT_DOWNLOAD_INITIALISE;
140  }
141 
142  y += FONT_HEIGHT_NORMAL + 5;
143  DrawStringMultiLine(r.left + 2, r.right - 2, y, y + FONT_HEIGHT_NORMAL * 2, str, TC_FROMSTRING, SA_CENTER);
144 }
145 
146 /* virtual */ void BaseNetworkContentDownloadStatusWindow::OnDownloadProgress(const ContentInfo *ci, int bytes)
147 {
148  if (ci->id != this->cur_id) {
149  strecpy(this->name, ci->filename, lastof(this->name));
150  this->cur_id = ci->id;
151  this->downloaded_files++;
152  }
153 
154  this->downloaded_bytes += bytes;
155  this->SetDirty();
156 }
157 
158 
161 private:
163 
164 public:
170  {
172  }
173 
176  {
178  for (ContentType *iter = this->receivedTypes.Begin(); iter != this->receivedTypes.End(); iter++) {
179  switch (*iter) {
180  case CONTENT_TYPE_AI:
182  /* AI::Rescan calls the scanner. */
183  break;
184  case CONTENT_TYPE_GAME:
186  /* Game::Rescan calls the scanner. */
187  break;
188 
192  mode |= TarScanner::BASESET;
193  break;
194 
195  case CONTENT_TYPE_NEWGRF:
196  /* ScanNewGRFFiles calls the scanner. */
197  break;
198 
201  mode |= TarScanner::SCENARIO;
202  break;
203 
204  default:
205  break;
206  }
207  }
208 
209  TarScanner::DoScan(mode);
210 
211  /* Tell all the backends about what we've downloaded */
212  for (ContentType *iter = this->receivedTypes.Begin(); iter != this->receivedTypes.End(); iter++) {
213  switch (*iter) {
214  case CONTENT_TYPE_AI:
216  AI::Rescan();
217  break;
218 
219  case CONTENT_TYPE_GAME:
221  Game::Rescan();
222  break;
223 
227  break;
228 
232  break;
233 
237  break;
238 
239  case CONTENT_TYPE_NEWGRF:
240  ScanNewGRFFiles(NULL);
241  break;
242 
245  extern void ScanScenarios();
246  ScanScenarios();
248  break;
249 
250  default:
251  break;
252  }
253  }
254 
255  /* Always invalidate the download window; tell it we are going to be gone */
257  }
258 
259  virtual void OnClick(Point pt, int widget, int click_count)
260  {
261  if (widget == WID_NCDS_CANCELOK) {
262  if (this->downloaded_bytes != this->total_bytes) {
264  delete this;
265  } else {
266  /* If downloading succeeded, close the online content window. This will close
267  * the current window as well. */
269  }
270  }
271  }
272 
273  virtual void OnDownloadProgress(const ContentInfo *ci, int bytes)
274  {
275  BaseNetworkContentDownloadStatusWindow::OnDownloadProgress(ci, bytes);
276  this->receivedTypes.Include(ci->type);
277 
278  /* When downloading is finished change cancel in ok */
279  if (this->downloaded_bytes == this->total_bytes) {
280  this->GetWidget<NWidgetCore>(WID_NCDS_CANCELOK)->widget_data = STR_BUTTON_OK;
281  }
282  }
283 };
284 
289 
290  static const uint EDITBOX_MAX_SIZE = 50;
291 
297  bool auto_select;
301 
303  int list_pos;
306 
309  {
310  extern void OpenBrowser(const char *url);
311 
312  char url[1024];
313  const char *last = lastof(url);
314 
315  char *pos = strecpy(url, "http://grfsearch.openttd.org/?", last);
316 
317  if (this->auto_select) {
318  pos = strecpy(pos, "do=searchgrfid&q=", last);
319 
320  bool first = true;
321  for (ConstContentIterator iter = this->content.Begin(); iter != this->content.End(); iter++) {
322  const ContentInfo *ci = *iter;
323  if (ci->state != ContentInfo::DOES_NOT_EXIST) continue;
324 
325  if (!first) pos = strecpy(pos, ",", last);
326  first = false;
327 
328  pos += seprintf(pos, last, "%08X", ci->unique_id);
329  pos = strecpy(pos, ":", last);
330  pos = md5sumToString(pos, last, ci->md5sum);
331  }
332  } else {
333  pos = strecpy(pos, "do=searchtext&q=", last);
334 
335  /* Escape search term */
336  for (const char *search = this->filter_editbox.text.buf; *search != '\0'; search++) {
337  /* Remove quotes */
338  if (*search == '\'' || *search == '"') continue;
339 
340  /* Escape special chars, such as &%,= */
341  if (*search < 0x30) {
342  pos += seprintf(pos, last, "%%%02X", *search);
343  } else if (pos < last) {
344  *pos = *search;
345  *++pos = '\0';
346  }
347  }
348  }
349 
350  OpenBrowser(url);
351  }
352 
356  static void ExternalSearchDisclaimerCallback(Window *w, bool accepted)
357  {
358  if (accepted) {
360  ((NetworkContentListWindow*)w)->OpenExternalSearch();
361  }
362  }
363 
369  {
370  if (!this->content.NeedRebuild()) return;
371 
372  /* Create temporary array of games to use for listing */
373  this->content.Clear();
374 
375  bool all_available = true;
376 
378  if ((*iter)->state == ContentInfo::DOES_NOT_EXIST) all_available = false;
379  *this->content.Append() = *iter;
380  }
381 
382  this->SetWidgetDisabledState(WID_NCL_SEARCH_EXTERNAL, this->auto_select && all_available);
383 
384  this->FilterContentList();
385  this->content.Compact();
386  this->content.RebuildDone();
387  this->SortContentList();
388 
389  this->vscroll->SetCount(this->content.Length()); // Update the scrollbar
390  this->ScrollToSelected();
391  }
392 
394  static int CDECL NameSorter(const ContentInfo * const *a, const ContentInfo * const *b)
395  {
396  return strnatcmp((*a)->name, (*b)->name, true); // Sort by name (natural sorting).
397  }
398 
400  static int CDECL TypeSorter(const ContentInfo * const *a, const ContentInfo * const *b)
401  {
402  int r = 0;
403  if ((*a)->type != (*b)->type) {
404  char a_str[64];
405  char b_str[64];
406  GetString(a_str, STR_CONTENT_TYPE_BASE_GRAPHICS + (*a)->type - CONTENT_TYPE_BASE_GRAPHICS, lastof(a_str));
407  GetString(b_str, STR_CONTENT_TYPE_BASE_GRAPHICS + (*b)->type - CONTENT_TYPE_BASE_GRAPHICS, lastof(b_str));
408  r = strnatcmp(a_str, b_str);
409  }
410  if (r == 0) r = NameSorter(a, b);
411  return r;
412  }
413 
415  static int CDECL StateSorter(const ContentInfo * const *a, const ContentInfo * const *b)
416  {
417  int r = (*a)->state - (*b)->state;
418  if (r == 0) r = TypeSorter(a, b);
419  return r;
420  }
421 
424  {
425  if (!this->content.Sort()) return;
426 
427  for (ConstContentIterator iter = this->content.Begin(); iter != this->content.End(); iter++) {
428  if (*iter == this->selected) {
429  this->list_pos = iter - this->content.Begin();
430  break;
431  }
432  }
433  }
434 
436  static bool CDECL TagNameFilter(const ContentInfo * const *a, StringFilter &filter)
437  {
438  filter.ResetState();
439  for (int i = 0; i < (*a)->tag_count; i++) {
440  filter.AddLine((*a)->tags[i]);
441  }
442  filter.AddLine((*a)->name);
443  return filter.GetState();
444  }
445 
448  {
449  if (!this->content.Filter(this->string_filter)) return;
450 
451  /* update list position */
452  for (ConstContentIterator iter = this->content.Begin(); iter != this->content.End(); iter++) {
453  if (*iter == this->selected) {
454  this->list_pos = iter - this->content.Begin();
455  return;
456  }
457  }
458 
459  /* previously selected item not in list anymore */
460  this->selected = NULL;
461  this->list_pos = 0;
462  }
463 
466  {
467  if (this->selected == NULL) return;
468 
469  this->vscroll->ScrollTowards(this->list_pos);
470  }
471 
472 public:
478  NetworkContentListWindow(WindowDesc *desc, bool select_all) :
479  Window(desc),
480  auto_select(select_all),
482  selected(NULL),
483  list_pos(0)
484  {
485  this->checkbox_size = maxdim(maxdim(GetSpriteSize(SPR_BOX_EMPTY), GetSpriteSize(SPR_BOX_CHECKED)), GetSpriteSize(SPR_BLOT));
486 
487  this->CreateNestedTree();
488  this->vscroll = this->GetScrollbar(WID_NCL_SCROLLBAR);
490 
491  this->GetWidget<NWidgetStacked>(WID_NCL_SEL_ALL_UPDATE)->SetDisplayedPlane(select_all);
492 
497 
499  this->content.SetListing(this->last_sorting);
500  this->content.SetFiltering(this->last_filtering);
501  this->content.SetSortFuncs(this->sorter_funcs);
502  this->content.SetFilterFuncs(this->filter_funcs);
503  this->content.ForceRebuild();
504  this->FilterContentList();
505  this->SortContentList();
506  this->InvalidateData();
507  }
508 
511  {
513  }
514 
515  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
516  {
517  switch (widget) {
518  case WID_NCL_FILTER_CAPT:
519  *size = maxdim(*size, GetStringBoundingBox(STR_CONTENT_FILTER_TITLE));
520  break;
521 
522  case WID_NCL_CHECKBOX:
523  size->width = this->checkbox_size.width + WD_MATRIX_RIGHT + WD_MATRIX_LEFT;
524  break;
525 
526  case WID_NCL_TYPE: {
527  Dimension d = *size;
528  for (int i = CONTENT_TYPE_BEGIN; i < CONTENT_TYPE_END; i++) {
529  d = maxdim(d, GetStringBoundingBox(STR_CONTENT_TYPE_BASE_GRAPHICS + i - CONTENT_TYPE_BASE_GRAPHICS));
530  }
531  size->width = d.width + WD_MATRIX_RIGHT + WD_MATRIX_LEFT;
532  break;
533  }
534 
535  case WID_NCL_MATRIX:
536  resize->height = max(this->checkbox_size.height, (uint)FONT_HEIGHT_NORMAL) + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
537  size->height = 10 * resize->height;
538  break;
539  }
540  }
541 
542 
543  virtual void DrawWidget(const Rect &r, int widget) const
544  {
545  switch (widget) {
546  case WID_NCL_FILTER_CAPT:
547  DrawString(r.left, r.right, r.top, STR_CONTENT_FILTER_TITLE, TC_FROMSTRING, SA_RIGHT);
548  break;
549 
550  case WID_NCL_DETAILS:
551  this->DrawDetails(r);
552  break;
553 
554  case WID_NCL_MATRIX:
555  this->DrawMatrix(r);
556  break;
557  }
558  }
559 
560  virtual void OnPaint()
561  {
562  const SortButtonState arrow = this->content.IsDescSortOrder() ? SBS_DOWN : SBS_UP;
563 
564  if (this->content.NeedRebuild()) {
565  this->BuildContentList();
566  }
567 
568  this->DrawWidgets();
569 
570  switch (this->content.SortType()) {
572  case WID_NCL_TYPE - WID_NCL_CHECKBOX: this->DrawSortButtonState(WID_NCL_TYPE, arrow); break;
573  case WID_NCL_NAME - WID_NCL_CHECKBOX: this->DrawSortButtonState(WID_NCL_NAME, arrow); break;
574  }
575  }
576 
581  void DrawMatrix(const Rect &r) const
582  {
583  const NWidgetBase *nwi_checkbox = this->GetWidget<NWidgetBase>(WID_NCL_CHECKBOX);
584  const NWidgetBase *nwi_name = this->GetWidget<NWidgetBase>(WID_NCL_NAME);
585  const NWidgetBase *nwi_type = this->GetWidget<NWidgetBase>(WID_NCL_TYPE);
586 
587  int line_height = max(this->checkbox_size.height, (uint)FONT_HEIGHT_NORMAL);
588 
589  /* Fill the matrix with the information */
590  int sprite_y_offset = WD_MATRIX_TOP + (line_height - this->checkbox_size.height) / 2 - 1;
591  int text_y_offset = WD_MATRIX_TOP + (line_height - FONT_HEIGHT_NORMAL) / 2;
592  uint y = r.top;
593  int cnt = 0;
594  for (ConstContentIterator iter = this->content.Get(this->vscroll->GetPosition()); iter != this->content.End() && cnt < this->vscroll->GetCapacity(); iter++, cnt++) {
595  const ContentInfo *ci = *iter;
596 
597  if (ci == this->selected) GfxFillRect(r.left + 1, y + 1, r.right - 1, y + this->resize.step_height - 1, PC_GREY);
598 
599  SpriteID sprite;
600  SpriteID pal = PAL_NONE;
601  switch (ci->state) {
602  case ContentInfo::UNSELECTED: sprite = SPR_BOX_EMPTY; break;
603  case ContentInfo::SELECTED: sprite = SPR_BOX_CHECKED; break;
604  case ContentInfo::AUTOSELECTED: sprite = SPR_BOX_CHECKED; break;
605  case ContentInfo::ALREADY_HERE: sprite = SPR_BLOT; pal = PALETTE_TO_GREEN; break;
606  case ContentInfo::DOES_NOT_EXIST: sprite = SPR_BLOT; pal = PALETTE_TO_RED; break;
607  default: NOT_REACHED();
608  }
609  DrawSprite(sprite, pal, nwi_checkbox->pos_x + (pal == PAL_NONE ? 2 : 3), y + sprite_y_offset + (pal == PAL_NONE ? 1 : 0));
610 
611  StringID str = STR_CONTENT_TYPE_BASE_GRAPHICS + ci->type - CONTENT_TYPE_BASE_GRAPHICS;
612  DrawString(nwi_type->pos_x, nwi_type->pos_x + nwi_type->current_x - 1, y + text_y_offset, str, TC_BLACK, SA_HOR_CENTER);
613 
614  DrawString(nwi_name->pos_x + WD_FRAMERECT_LEFT, nwi_name->pos_x + nwi_name->current_x - WD_FRAMERECT_RIGHT, y + text_y_offset, ci->name, TC_BLACK);
615  y += this->resize.step_height;
616  }
617  }
618 
623  void DrawDetails(const Rect &r) const
624  {
625  static const int DETAIL_LEFT = 5;
626  static const int DETAIL_RIGHT = 5;
627  static const int DETAIL_TOP = 5;
628 
629  /* Height for the title banner */
630  int DETAIL_TITLE_HEIGHT = 5 * FONT_HEIGHT_NORMAL;
631 
632  /* Create the nice grayish rectangle at the details top */
633  GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.top + DETAIL_TITLE_HEIGHT, PC_DARK_BLUE);
634  DrawString(r.left + WD_INSET_LEFT, r.right - WD_INSET_RIGHT, r.top + FONT_HEIGHT_NORMAL + WD_INSET_TOP, STR_CONTENT_DETAIL_TITLE, TC_FROMSTRING, SA_HOR_CENTER);
635 
636  /* Draw the total download size */
637  SetDParam(0, this->filesize_sum);
638  DrawString(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, r.bottom - FONT_HEIGHT_NORMAL - WD_PAR_VSEP_NORMAL, STR_CONTENT_TOTAL_DOWNLOAD_SIZE);
639 
640  if (this->selected == NULL) return;
641 
642  /* And fill the rest of the details when there's information to place there */
643  DrawStringMultiLine(r.left + WD_INSET_LEFT, r.right - WD_INSET_RIGHT, r.top + DETAIL_TITLE_HEIGHT / 2, r.top + DETAIL_TITLE_HEIGHT, STR_CONTENT_DETAIL_SUBTITLE_UNSELECTED + this->selected->state, TC_FROMSTRING, SA_CENTER);
644 
645  /* Also show the total download size, so keep some space from the bottom */
646  const uint max_y = r.bottom - FONT_HEIGHT_NORMAL - WD_PAR_VSEP_WIDE;
647  int y = r.top + DETAIL_TITLE_HEIGHT + DETAIL_TOP;
648 
649  if (this->selected->upgrade) {
650  SetDParam(0, STR_CONTENT_TYPE_BASE_GRAPHICS + this->selected->type - CONTENT_TYPE_BASE_GRAPHICS);
651  y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_UPDATE);
652  y += WD_PAR_VSEP_WIDE;
653  }
654 
655  SetDParamStr(0, this->selected->name);
656  y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_NAME);
657 
658  if (!StrEmpty(this->selected->version)) {
659  SetDParamStr(0, this->selected->version);
660  y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_VERSION);
661  }
662 
663  if (!StrEmpty(this->selected->description)) {
664  SetDParamStr(0, this->selected->description);
665  y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_DESCRIPTION);
666  }
667 
668  if (!StrEmpty(this->selected->url)) {
669  SetDParamStr(0, this->selected->url);
670  y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_URL);
671  }
672 
673  SetDParam(0, STR_CONTENT_TYPE_BASE_GRAPHICS + this->selected->type - CONTENT_TYPE_BASE_GRAPHICS);
674  y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_TYPE);
675 
676  y += WD_PAR_VSEP_WIDE;
677  SetDParam(0, this->selected->filesize);
678  y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_FILESIZE);
679 
680  if (this->selected->dependency_count != 0) {
681  /* List dependencies */
682  char buf[DRAW_STRING_BUFFER] = "";
683  char *p = buf;
684  for (uint i = 0; i < this->selected->dependency_count; i++) {
685  ContentID cid = this->selected->dependencies[i];
686 
687  /* Try to find the dependency */
689  for (; iter != _network_content_client.End(); iter++) {
690  const ContentInfo *ci = *iter;
691  if (ci->id != cid) continue;
692 
693  p += seprintf(p, lastof(buf), p == buf ? "%s" : ", %s", (*iter)->name);
694  break;
695  }
696  }
697  SetDParamStr(0, buf);
698  y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_DEPENDENCIES);
699  }
700 
701  if (this->selected->tag_count != 0) {
702  /* List all tags */
703  char buf[DRAW_STRING_BUFFER] = "";
704  char *p = buf;
705  for (uint i = 0; i < this->selected->tag_count; i++) {
706  p += seprintf(p, lastof(buf), i == 0 ? "%s" : ", %s", this->selected->tags[i]);
707  }
708  SetDParamStr(0, buf);
709  y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_TAGS);
710  }
711 
712  if (this->selected->IsSelected()) {
713  /* When selected show all manually selected content that depends on this */
714  ConstContentVector tree;
716 
717  char buf[DRAW_STRING_BUFFER] = "";
718  char *p = buf;
719  for (ConstContentIterator iter = tree.Begin(); iter != tree.End(); iter++) {
720  const ContentInfo *ci = *iter;
721  if (ci == this->selected || ci->state != ContentInfo::SELECTED) continue;
722 
723  p += seprintf(p, lastof(buf), buf == p ? "%s" : ", %s", ci->name);
724  }
725  if (p != buf) {
726  SetDParamStr(0, buf);
727  y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_SELECTED_BECAUSE_OF);
728  }
729  }
730  }
731 
732  virtual void OnClick(Point pt, int widget, int click_count)
733  {
734  if (widget >= WID_NCL_TEXTFILE && widget < WID_NCL_TEXTFILE + TFT_END) {
735  if (this->selected == NULL || this->selected->state != ContentInfo::ALREADY_HERE) return;
736 
737  ShowContentTextfileWindow((TextfileType)(widget - WID_NCL_TEXTFILE), this->selected);
738  return;
739  }
740 
741  switch (widget) {
742  case WID_NCL_MATRIX: {
743  uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NCL_MATRIX);
744  if (id_v >= this->content.Length()) return; // click out of bounds
745 
746  this->selected = *this->content.Get(id_v);
747  this->list_pos = id_v;
748 
749  const NWidgetBase *checkbox = this->GetWidget<NWidgetBase>(WID_NCL_CHECKBOX);
750  if (click_count > 1 || IsInsideBS(pt.x, checkbox->pos_x, checkbox->current_x)) {
752  this->content.ForceResort();
753  }
754 
755  this->InvalidateData();
756  break;
757  }
758 
759  case WID_NCL_CHECKBOX:
760  case WID_NCL_TYPE:
761  case WID_NCL_NAME:
762  if (this->content.SortType() == widget - WID_NCL_CHECKBOX) {
763  this->content.ToggleSortOrder();
764  if (this->content.Length() > 0) this->list_pos = this->content.Length() - this->list_pos - 1;
765  } else {
766  this->content.SetSortType(widget - WID_NCL_CHECKBOX);
767  this->content.ForceResort();
768  this->SortContentList();
769  }
770  this->ScrollToSelected();
771  this->InvalidateData();
772  break;
773 
774  case WID_NCL_SELECT_ALL:
776  this->InvalidateData();
777  break;
778 
781  this->InvalidateData();
782  break;
783 
784  case WID_NCL_UNSELECT:
786  this->InvalidateData();
787  break;
788 
789  case WID_NCL_CANCEL:
790  delete this;
791  break;
792 
793  case WID_NCL_OPEN_URL:
794  if (this->selected != NULL) {
795  extern void OpenBrowser(const char *url);
796  OpenBrowser(this->selected->url);
797  }
798  break;
799 
800  case WID_NCL_DOWNLOAD:
802  break;
803 
806  this->OpenExternalSearch();
807  } else {
808  ShowQuery(STR_CONTENT_SEARCH_EXTERNAL_DISCLAIMER_CAPTION, STR_CONTENT_SEARCH_EXTERNAL_DISCLAIMER, this, ExternalSearchDisclaimerCallback);
809  }
810  break;
811  }
812  }
813 
814  virtual EventState OnKeyPress(WChar key, uint16 keycode)
815  {
816  switch (keycode) {
817  case WKC_UP:
818  /* scroll up by one */
819  if (this->list_pos > 0) this->list_pos--;
820  break;
821  case WKC_DOWN:
822  /* scroll down by one */
823  if (this->list_pos < (int)this->content.Length() - 1) this->list_pos++;
824  break;
825  case WKC_PAGEUP:
826  /* scroll up a page */
827  this->list_pos = (this->list_pos < this->vscroll->GetCapacity()) ? 0 : this->list_pos - this->vscroll->GetCapacity();
828  break;
829  case WKC_PAGEDOWN:
830  /* scroll down a page */
831  this->list_pos = min(this->list_pos + this->vscroll->GetCapacity(), (int)this->content.Length() - 1);
832  break;
833  case WKC_HOME:
834  /* jump to beginning */
835  this->list_pos = 0;
836  break;
837  case WKC_END:
838  /* jump to end */
839  this->list_pos = this->content.Length() - 1;
840  break;
841 
842  case WKC_SPACE:
843  case WKC_RETURN:
844  if (keycode == WKC_RETURN || !IsWidgetFocused(WID_NCL_FILTER)) {
845  if (this->selected != NULL) {
847  this->content.ForceResort();
848  this->InvalidateData();
849  }
850  return ES_HANDLED;
851  }
852  /* FALL THROUGH, space is pressed and filter isn't focused. */
853 
854  default:
855  return ES_NOT_HANDLED;
856  }
857 
858  if (this->content.Length() == 0) {
859  this->list_pos = 0; // above stuff may result in "-1".
860  return ES_HANDLED;
861  }
862 
863  this->selected = *this->content.Get(this->list_pos);
864 
865  /* scroll to the new server if it is outside the current range */
866  this->ScrollToSelected();
867 
868  /* redraw window */
869  this->InvalidateData();
870  return ES_HANDLED;
871  }
872 
873  virtual void OnEditboxChanged(int wid)
874  {
875  if (wid == WID_NCL_FILTER) {
876  this->string_filter.SetFilterTerm(this->filter_editbox.text.buf);
878  this->content.ForceRebuild();
879  this->InvalidateData();
880  }
881  }
882 
883  virtual void OnResize()
884  {
886  }
887 
888  virtual void OnReceiveContentInfo(const ContentInfo *rci)
889  {
891  this->content.ForceRebuild();
892  this->InvalidateData();
893  }
894 
895  virtual void OnDownloadComplete(ContentID cid)
896  {
897  this->content.ForceResort();
898  this->InvalidateData();
899  }
900 
901  virtual void OnConnect(bool success)
902  {
903  if (!success) {
904  ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_CONNECT, INVALID_STRING_ID, WL_ERROR);
905  delete this;
906  return;
907  }
908 
909  this->InvalidateData();
910  }
911 
917  virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
918  {
919  if (!gui_scope) return;
920  if (this->content.NeedRebuild()) this->BuildContentList();
921 
922  /* To sum all the bytes we intend to download */
923  this->filesize_sum = 0;
924  bool show_select_all = false;
925  bool show_select_upgrade = false;
926  for (ConstContentIterator iter = this->content.Begin(); iter != this->content.End(); iter++) {
927  const ContentInfo *ci = *iter;
928  switch (ci->state) {
931  this->filesize_sum += ci->filesize;
932  break;
933 
935  show_select_all = true;
936  show_select_upgrade |= ci->upgrade;
937  break;
938 
939  default:
940  break;
941  }
942  }
943 
944  /* If data == 2 then the status window caused this OnInvalidate */
947  this->SetWidgetDisabledState(WID_NCL_SELECT_ALL, !show_select_all);
948  this->SetWidgetDisabledState(WID_NCL_SELECT_UPDATE, !show_select_upgrade);
949  this->SetWidgetDisabledState(WID_NCL_OPEN_URL, this->selected == NULL || StrEmpty(this->selected->url));
950  for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
951  this->SetWidgetDisabledState(WID_NCL_TEXTFILE + tft, this->selected == NULL || this->selected->state != ContentInfo::ALREADY_HERE || this->selected->GetTextfile(tft) == NULL);
952  }
953 
954  this->GetWidget<NWidgetCore>(WID_NCL_CANCEL)->widget_data = this->filesize_sum == 0 ? STR_AI_SETTINGS_CLOSE : STR_AI_LIST_CANCEL;
955  }
956 };
957 
960 
962  &StateSorter,
963  &TypeSorter,
964  &NameSorter,
965 };
966 
968  &TagNameFilter,
969 };
970 
974  NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
975  NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_CONTENT_TITLE, STR_NULL),
976  NWidget(WWT_DEFSIZEBOX, COLOUR_LIGHT_BLUE),
977  EndContainer(),
978  NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NCL_BACKGROUND),
981  /* Top */
982  NWidget(WWT_EMPTY, COLOUR_LIGHT_BLUE, WID_NCL_FILTER_CAPT), SetFill(1, 0), SetResize(1, 0),
983  NWidget(WWT_EDITBOX, COLOUR_LIGHT_BLUE, WID_NCL_FILTER), SetFill(1, 0), SetResize(1, 0),
984  SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
985  EndContainer(),
988  /* Left side. */
989  NWidget(NWID_VERTICAL), SetPIP(0, 4, 0),
993  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_CHECKBOX), SetMinimalSize(13, 1), SetDataTip(STR_EMPTY, STR_NULL),
994  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_TYPE),
995  SetDataTip(STR_CONTENT_TYPE_CAPTION, STR_CONTENT_TYPE_CAPTION_TOOLTIP),
996  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_NAME), SetResize(1, 0), SetFill(1, 0),
997  SetDataTip(STR_CONTENT_NAME_CAPTION, STR_CONTENT_NAME_CAPTION_TOOLTIP),
998  EndContainer(),
999  NWidget(WWT_MATRIX, COLOUR_LIGHT_BLUE, WID_NCL_MATRIX), SetResize(1, 14), SetFill(1, 1), SetScrollbar(WID_NCL_SCROLLBAR), SetMatrixDataTip(1, 0, STR_CONTENT_MATRIX_TOOLTIP),
1000  EndContainer(),
1001  NWidget(NWID_VSCROLLBAR, COLOUR_LIGHT_BLUE, WID_NCL_SCROLLBAR),
1002  EndContainer(),
1004  NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NCL_SEL_ALL_UPDATE), SetResize(1, 0), SetFill(1, 0),
1005  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_SELECT_UPDATE), SetResize(1, 0), SetFill(1, 0),
1006  SetDataTip(STR_CONTENT_SELECT_UPDATES_CAPTION, STR_CONTENT_SELECT_UPDATES_CAPTION_TOOLTIP),
1007  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_SELECT_ALL), SetResize(1, 0), SetFill(1, 0),
1008  SetDataTip(STR_CONTENT_SELECT_ALL_CAPTION, STR_CONTENT_SELECT_ALL_CAPTION_TOOLTIP),
1009  EndContainer(),
1010  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_UNSELECT), SetResize(1, 0), SetFill(1, 0),
1011  SetDataTip(STR_CONTENT_UNSELECT_ALL_CAPTION, STR_CONTENT_UNSELECT_ALL_CAPTION_TOOLTIP),
1012  EndContainer(),
1013  EndContainer(),
1014  /* Right side. */
1015  NWidget(NWID_VERTICAL), SetPIP(0, 4, 0),
1016  NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NCL_DETAILS), SetResize(1, 1), SetFill(1, 1), EndContainer(),
1018  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
1019  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
1020  EndContainer(),
1022  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_OPEN_URL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_CONTENT_OPEN_URL, STR_CONTENT_OPEN_URL_TOOLTIP),
1023  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
1024  EndContainer(),
1025  EndContainer(),
1026  EndContainer(),
1028  /* Bottom. */
1030  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_SEARCH_EXTERNAL), SetResize(1, 0), SetFill(1, 0),
1031  SetDataTip(STR_CONTENT_SEARCH_EXTERNAL, STR_CONTENT_SEARCH_EXTERNAL_TOOLTIP),
1033  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_CANCEL), SetResize(1, 0), SetFill(1, 0),
1034  SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
1035  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_DOWNLOAD), SetResize(1, 0), SetFill(1, 0),
1036  SetDataTip(STR_CONTENT_DOWNLOAD_CAPTION, STR_CONTENT_DOWNLOAD_CAPTION_TOOLTIP),
1037  EndContainer(),
1038  EndContainer(),
1040  /* Resize button. */
1042  NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1043  NWidget(WWT_RESIZEBOX, COLOUR_LIGHT_BLUE),
1044  EndContainer(),
1045  EndContainer(),
1046 };
1047 
1050  WDP_CENTER, "list_content", 630, 460,
1052  0,
1053  _nested_network_content_list_widgets, lengthof(_nested_network_content_list_widgets)
1054 );
1055 
1062 {
1063 #if defined(WITH_ZLIB)
1065  if (cv == NULL) {
1067  } else {
1069  }
1070 
1073 #else
1074  ShowErrorMessage(STR_CONTENT_NO_ZLIB, STR_CONTENT_NO_ZLIB_SUB, WL_ERROR);
1075  /* Connection failed... clean up the mess */
1076  if (cv != NULL) {
1077  for (ContentIterator iter = cv->Begin(); iter != cv->End(); iter++) delete *iter;
1078  }
1079 #endif /* WITH_ZLIB */
1080 }
1081 
1082 #endif /* ENABLE_NETWORK */