OpenTTD
network_content_gui.cpp
Go to the documentation of this file.
1 /* $Id: network_content_gui.cpp 27288 2015-05-17 19:49:35Z michi_cc $ */
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 
308 
311  {
312  extern void OpenBrowser(const char *url);
313 
314  char url[1024];
315  const char *last = lastof(url);
316 
317  char *pos = strecpy(url, "http://grfsearch.openttd.org/?", last);
318 
319  if (this->auto_select) {
320  pos = strecpy(pos, "do=searchgrfid&q=", last);
321 
322  bool first = true;
323  for (ConstContentIterator iter = this->content.Begin(); iter != this->content.End(); iter++) {
324  const ContentInfo *ci = *iter;
325  if (ci->state != ContentInfo::DOES_NOT_EXIST) continue;
326 
327  if (!first) pos = strecpy(pos, ",", last);
328  first = false;
329 
330  pos += seprintf(pos, last, "%08X", ci->unique_id);
331  pos = strecpy(pos, ":", last);
332  pos = md5sumToString(pos, last, ci->md5sum);
333  }
334  } else {
335  pos = strecpy(pos, "do=searchtext&q=", last);
336 
337  /* Escape search term */
338  for (const char *search = this->filter_editbox.text.buf; *search != '\0'; search++) {
339  /* Remove quotes */
340  if (*search == '\'' || *search == '"') continue;
341 
342  /* Escape special chars, such as &%,= */
343  if (*search < 0x30) {
344  pos += seprintf(pos, last, "%%%02X", *search);
345  } else if (pos < last) {
346  *pos = *search;
347  *++pos = '\0';
348  }
349  }
350  }
351 
352  OpenBrowser(url);
353  }
354 
358  static void ExternalSearchDisclaimerCallback(Window *w, bool accepted)
359  {
360  if (accepted) {
362  ((NetworkContentListWindow*)w)->OpenExternalSearch();
363  }
364  }
365 
371  {
372  if (!this->content.NeedRebuild()) return;
373 
374  /* Create temporary array of games to use for listing */
375  this->content.Clear();
376 
377  bool all_available = true;
378 
380  if ((*iter)->state == ContentInfo::DOES_NOT_EXIST) all_available = false;
381  *this->content.Append() = *iter;
382  }
383 
384  this->SetWidgetDisabledState(WID_NCL_SEARCH_EXTERNAL, this->auto_select && all_available);
385 
386  this->FilterContentList();
387  this->content.Compact();
388  this->content.RebuildDone();
389  this->SortContentList();
390 
391  this->vscroll->SetCount(this->content.Length()); // Update the scrollbar
392  this->ScrollToSelected();
393  }
394 
396  static int CDECL NameSorter(const ContentInfo * const *a, const ContentInfo * const *b)
397  {
398  return strnatcmp((*a)->name, (*b)->name, true); // Sort by name (natural sorting).
399  }
400 
402  static int CDECL TypeSorter(const ContentInfo * const *a, const ContentInfo * const *b)
403  {
404  int r = 0;
405  if ((*a)->type != (*b)->type) {
406  r = strnatcmp(content_type_strs[(*a)->type], content_type_strs[(*b)->type]);
407  }
408  if (r == 0) r = NameSorter(a, b);
409  return r;
410  }
411 
413  static int CDECL StateSorter(const ContentInfo * const *a, const ContentInfo * const *b)
414  {
415  int r = (*a)->state - (*b)->state;
416  if (r == 0) r = TypeSorter(a, b);
417  return r;
418  }
419 
422  {
423  if (!this->content.Sort()) return;
424 
425  for (ConstContentIterator iter = this->content.Begin(); iter != this->content.End(); iter++) {
426  if (*iter == this->selected) {
427  this->list_pos = iter - this->content.Begin();
428  break;
429  }
430  }
431  }
432 
434  static bool CDECL TagNameFilter(const ContentInfo * const *a, StringFilter &filter)
435  {
436  filter.ResetState();
437  for (int i = 0; i < (*a)->tag_count; i++) {
438  filter.AddLine((*a)->tags[i]);
439  }
440  filter.AddLine((*a)->name);
441  return filter.GetState();
442  }
443 
446  {
447  if (!this->content.Filter(this->string_filter)) return;
448 
449  /* update list position */
450  for (ConstContentIterator iter = this->content.Begin(); iter != this->content.End(); iter++) {
451  if (*iter == this->selected) {
452  this->list_pos = iter - this->content.Begin();
453  return;
454  }
455  }
456 
457  /* previously selected item not in list anymore */
458  this->selected = NULL;
459  this->list_pos = 0;
460  }
461 
464  {
465  if (this->selected == NULL) return;
466 
467  this->vscroll->ScrollTowards(this->list_pos);
468  }
469 
470  friend void BuildContentTypeStringList();
471 public:
477  NetworkContentListWindow(WindowDesc *desc, bool select_all) :
478  Window(desc),
479  auto_select(select_all),
481  selected(NULL),
482  list_pos(0)
483  {
484  this->checkbox_size = maxdim(maxdim(GetSpriteSize(SPR_BOX_EMPTY), GetSpriteSize(SPR_BOX_CHECKED)), GetSpriteSize(SPR_BLOT));
485 
486  this->CreateNestedTree();
487  this->vscroll = this->GetScrollbar(WID_NCL_SCROLLBAR);
489 
490  this->GetWidget<NWidgetStacked>(WID_NCL_SEL_ALL_UPDATE)->SetDisplayedPlane(select_all);
491 
496 
498  this->content.SetListing(this->last_sorting);
499  this->content.SetFiltering(this->last_filtering);
500  this->content.SetSortFuncs(this->sorter_funcs);
501  this->content.SetFilterFuncs(this->filter_funcs);
502  this->content.ForceRebuild();
503  this->FilterContentList();
504  this->SortContentList();
505  this->InvalidateData();
506  }
507 
510  {
512  }
513 
514  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
515  {
516  switch (widget) {
517  case WID_NCL_FILTER_CAPT:
518  *size = maxdim(*size, GetStringBoundingBox(STR_CONTENT_FILTER_TITLE));
519  break;
520 
521  case WID_NCL_CHECKBOX:
522  size->width = this->checkbox_size.width + WD_MATRIX_RIGHT + WD_MATRIX_LEFT;
523  break;
524 
525  case WID_NCL_TYPE: {
526  Dimension d = *size;
527  for (int i = CONTENT_TYPE_BEGIN; i < CONTENT_TYPE_END; i++) {
528  d = maxdim(d, GetStringBoundingBox(STR_CONTENT_TYPE_BASE_GRAPHICS + i - CONTENT_TYPE_BASE_GRAPHICS));
529  }
530  size->width = d.width + WD_MATRIX_RIGHT + WD_MATRIX_LEFT;
531  break;
532  }
533 
534  case WID_NCL_MATRIX:
535  resize->height = max(this->checkbox_size.height, (uint)FONT_HEIGHT_NORMAL) + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
536  size->height = 10 * resize->height;
537  break;
538  }
539  }
540 
541 
542  virtual void DrawWidget(const Rect &r, int widget) const
543  {
544  switch (widget) {
545  case WID_NCL_FILTER_CAPT:
546  DrawString(r.left, r.right, r.top, STR_CONTENT_FILTER_TITLE, TC_FROMSTRING, SA_RIGHT);
547  break;
548 
549  case WID_NCL_DETAILS:
550  this->DrawDetails(r);
551  break;
552 
553  case WID_NCL_MATRIX:
554  this->DrawMatrix(r);
555  break;
556  }
557  }
558 
559  virtual void OnPaint()
560  {
561  const SortButtonState arrow = this->content.IsDescSortOrder() ? SBS_DOWN : SBS_UP;
562 
563  if (this->content.NeedRebuild()) {
564  this->BuildContentList();
565  }
566 
567  this->DrawWidgets();
568 
569  switch (this->content.SortType()) {
571  case WID_NCL_TYPE - WID_NCL_CHECKBOX: this->DrawSortButtonState(WID_NCL_TYPE, arrow); break;
572  case WID_NCL_NAME - WID_NCL_CHECKBOX: this->DrawSortButtonState(WID_NCL_NAME, arrow); break;
573  }
574  }
575 
580  void DrawMatrix(const Rect &r) const
581  {
582  const NWidgetBase *nwi_checkbox = this->GetWidget<NWidgetBase>(WID_NCL_CHECKBOX);
583  const NWidgetBase *nwi_name = this->GetWidget<NWidgetBase>(WID_NCL_NAME);
584  const NWidgetBase *nwi_type = this->GetWidget<NWidgetBase>(WID_NCL_TYPE);
585 
586  int line_height = max(this->checkbox_size.height, (uint)FONT_HEIGHT_NORMAL);
587 
588  /* Fill the matrix with the information */
589  int sprite_y_offset = WD_MATRIX_TOP + (line_height - this->checkbox_size.height) / 2 - 1;
590  int text_y_offset = WD_MATRIX_TOP + (line_height - FONT_HEIGHT_NORMAL) / 2;
591  uint y = r.top;
592  int cnt = 0;
593  for (ConstContentIterator iter = this->content.Get(this->vscroll->GetPosition()); iter != this->content.End() && cnt < this->vscroll->GetCapacity(); iter++, cnt++) {
594  const ContentInfo *ci = *iter;
595 
596  if (ci == this->selected) GfxFillRect(r.left + 1, y + 1, r.right - 1, y + this->resize.step_height - 1, PC_GREY);
597 
598  SpriteID sprite;
599  SpriteID pal = PAL_NONE;
600  switch (ci->state) {
601  case ContentInfo::UNSELECTED: sprite = SPR_BOX_EMPTY; break;
602  case ContentInfo::SELECTED: sprite = SPR_BOX_CHECKED; break;
603  case ContentInfo::AUTOSELECTED: sprite = SPR_BOX_CHECKED; break;
604  case ContentInfo::ALREADY_HERE: sprite = SPR_BLOT; pal = PALETTE_TO_GREEN; break;
605  case ContentInfo::DOES_NOT_EXIST: sprite = SPR_BLOT; pal = PALETTE_TO_RED; break;
606  default: NOT_REACHED();
607  }
608  DrawSprite(sprite, pal, nwi_checkbox->pos_x + (pal == PAL_NONE ? 2 : 3), y + sprite_y_offset + (pal == PAL_NONE ? 1 : 0));
609 
610  StringID str = STR_CONTENT_TYPE_BASE_GRAPHICS + ci->type - CONTENT_TYPE_BASE_GRAPHICS;
611  DrawString(nwi_type->pos_x, nwi_type->pos_x + nwi_type->current_x - 1, y + text_y_offset, str, TC_BLACK, SA_HOR_CENTER);
612 
613  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);
614  y += this->resize.step_height;
615  }
616  }
617 
622  void DrawDetails(const Rect &r) const
623  {
624  static const int DETAIL_LEFT = 5;
625  static const int DETAIL_RIGHT = 5;
626  static const int DETAIL_TOP = 5;
627 
628  /* Height for the title banner */
629  int DETAIL_TITLE_HEIGHT = 5 * FONT_HEIGHT_NORMAL;
630 
631  /* Create the nice grayish rectangle at the details top */
632  GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.top + DETAIL_TITLE_HEIGHT, PC_DARK_BLUE);
633  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);
634 
635  /* Draw the total download size */
636  SetDParam(0, this->filesize_sum);
637  DrawString(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, r.bottom - FONT_HEIGHT_NORMAL - WD_PAR_VSEP_NORMAL, STR_CONTENT_TOTAL_DOWNLOAD_SIZE);
638 
639  if (this->selected == NULL) return;
640 
641  /* And fill the rest of the details when there's information to place there */
642  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);
643 
644  /* Also show the total download size, so keep some space from the bottom */
645  const uint max_y = r.bottom - FONT_HEIGHT_NORMAL - WD_PAR_VSEP_WIDE;
646  int y = r.top + DETAIL_TITLE_HEIGHT + DETAIL_TOP;
647 
648  if (this->selected->upgrade) {
649  SetDParam(0, STR_CONTENT_TYPE_BASE_GRAPHICS + this->selected->type - CONTENT_TYPE_BASE_GRAPHICS);
650  y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_UPDATE);
651  y += WD_PAR_VSEP_WIDE;
652  }
653 
654  SetDParamStr(0, this->selected->name);
655  y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_NAME);
656 
657  if (!StrEmpty(this->selected->version)) {
658  SetDParamStr(0, this->selected->version);
659  y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_VERSION);
660  }
661 
662  if (!StrEmpty(this->selected->description)) {
663  SetDParamStr(0, this->selected->description);
664  y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_DESCRIPTION);
665  }
666 
667  if (!StrEmpty(this->selected->url)) {
668  SetDParamStr(0, this->selected->url);
669  y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_URL);
670  }
671 
672  SetDParam(0, STR_CONTENT_TYPE_BASE_GRAPHICS + this->selected->type - CONTENT_TYPE_BASE_GRAPHICS);
673  y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_TYPE);
674 
675  y += WD_PAR_VSEP_WIDE;
676  SetDParam(0, this->selected->filesize);
677  y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_FILESIZE);
678 
679  if (this->selected->dependency_count != 0) {
680  /* List dependencies */
681  char buf[DRAW_STRING_BUFFER] = "";
682  char *p = buf;
683  for (uint i = 0; i < this->selected->dependency_count; i++) {
684  ContentID cid = this->selected->dependencies[i];
685 
686  /* Try to find the dependency */
688  for (; iter != _network_content_client.End(); iter++) {
689  const ContentInfo *ci = *iter;
690  if (ci->id != cid) continue;
691 
692  p += seprintf(p, lastof(buf), p == buf ? "%s" : ", %s", (*iter)->name);
693  break;
694  }
695  }
696  SetDParamStr(0, buf);
697  y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_DEPENDENCIES);
698  }
699 
700  if (this->selected->tag_count != 0) {
701  /* List all tags */
702  char buf[DRAW_STRING_BUFFER] = "";
703  char *p = buf;
704  for (uint i = 0; i < this->selected->tag_count; i++) {
705  p += seprintf(p, lastof(buf), i == 0 ? "%s" : ", %s", this->selected->tags[i]);
706  }
707  SetDParamStr(0, buf);
708  y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_TAGS);
709  }
710 
711  if (this->selected->IsSelected()) {
712  /* When selected show all manually selected content that depends on this */
713  ConstContentVector tree;
715 
716  char buf[DRAW_STRING_BUFFER] = "";
717  char *p = buf;
718  for (ConstContentIterator iter = tree.Begin(); iter != tree.End(); iter++) {
719  const ContentInfo *ci = *iter;
720  if (ci == this->selected || ci->state != ContentInfo::SELECTED) continue;
721 
722  p += seprintf(p, lastof(buf), buf == p ? "%s" : ", %s", ci->name);
723  }
724  if (p != buf) {
725  SetDParamStr(0, buf);
726  y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_SELECTED_BECAUSE_OF);
727  }
728  }
729  }
730 
731  virtual void OnClick(Point pt, int widget, int click_count)
732  {
733  if (widget >= WID_NCL_TEXTFILE && widget < WID_NCL_TEXTFILE + TFT_END) {
734  if (this->selected == NULL || this->selected->state != ContentInfo::ALREADY_HERE) return;
735 
736  ShowContentTextfileWindow((TextfileType)(widget - WID_NCL_TEXTFILE), this->selected);
737  return;
738  }
739 
740  switch (widget) {
741  case WID_NCL_MATRIX: {
742  uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NCL_MATRIX);
743  if (id_v >= this->content.Length()) return; // click out of bounds
744 
745  this->selected = *this->content.Get(id_v);
746  this->list_pos = id_v;
747 
748  const NWidgetBase *checkbox = this->GetWidget<NWidgetBase>(WID_NCL_CHECKBOX);
749  if (click_count > 1 || IsInsideBS(pt.x, checkbox->pos_x, checkbox->current_x)) {
751  this->content.ForceResort();
752  }
753 
754  this->InvalidateData();
755  break;
756  }
757 
758  case WID_NCL_CHECKBOX:
759  case WID_NCL_TYPE:
760  case WID_NCL_NAME:
761  if (this->content.SortType() == widget - WID_NCL_CHECKBOX) {
762  this->content.ToggleSortOrder();
763  if (this->content.Length() > 0) this->list_pos = this->content.Length() - this->list_pos - 1;
764  } else {
765  this->content.SetSortType(widget - WID_NCL_CHECKBOX);
766  this->content.ForceResort();
767  this->SortContentList();
768  }
769  this->ScrollToSelected();
770  this->InvalidateData();
771  break;
772 
773  case WID_NCL_SELECT_ALL:
775  this->InvalidateData();
776  break;
777 
780  this->InvalidateData();
781  break;
782 
783  case WID_NCL_UNSELECT:
785  this->InvalidateData();
786  break;
787 
788  case WID_NCL_CANCEL:
789  delete this;
790  break;
791 
792  case WID_NCL_OPEN_URL:
793  if (this->selected != NULL) {
794  extern void OpenBrowser(const char *url);
795  OpenBrowser(this->selected->url);
796  }
797  break;
798 
799  case WID_NCL_DOWNLOAD:
801  break;
802 
805  this->OpenExternalSearch();
806  } else {
807  ShowQuery(STR_CONTENT_SEARCH_EXTERNAL_DISCLAIMER_CAPTION, STR_CONTENT_SEARCH_EXTERNAL_DISCLAIMER, this, ExternalSearchDisclaimerCallback);
808  }
809  break;
810  }
811  }
812 
813  virtual EventState OnKeyPress(WChar key, uint16 keycode)
814  {
815  switch (keycode) {
816  case WKC_UP:
817  /* scroll up by one */
818  if (this->list_pos > 0) this->list_pos--;
819  break;
820  case WKC_DOWN:
821  /* scroll down by one */
822  if (this->list_pos < (int)this->content.Length() - 1) this->list_pos++;
823  break;
824  case WKC_PAGEUP:
825  /* scroll up a page */
826  this->list_pos = (this->list_pos < this->vscroll->GetCapacity()) ? 0 : this->list_pos - this->vscroll->GetCapacity();
827  break;
828  case WKC_PAGEDOWN:
829  /* scroll down a page */
830  this->list_pos = min(this->list_pos + this->vscroll->GetCapacity(), (int)this->content.Length() - 1);
831  break;
832  case WKC_HOME:
833  /* jump to beginning */
834  this->list_pos = 0;
835  break;
836  case WKC_END:
837  /* jump to end */
838  this->list_pos = this->content.Length() - 1;
839  break;
840 
841  case WKC_SPACE:
842  case WKC_RETURN:
843  if (keycode == WKC_RETURN || !IsWidgetFocused(WID_NCL_FILTER)) {
844  if (this->selected != NULL) {
846  this->content.ForceResort();
847  this->InvalidateData();
848  }
849  return ES_HANDLED;
850  }
851  /* FALL THROUGH, space is pressed and filter isn't focused. */
852 
853  default:
854  return ES_NOT_HANDLED;
855  }
856 
857  if (this->content.Length() == 0) {
858  this->list_pos = 0; // above stuff may result in "-1".
859  return ES_HANDLED;
860  }
861 
862  this->selected = *this->content.Get(this->list_pos);
863 
864  /* scroll to the new server if it is outside the current range */
865  this->ScrollToSelected();
866 
867  /* redraw window */
868  this->InvalidateData();
869  return ES_HANDLED;
870  }
871 
872  virtual void OnEditboxChanged(int wid)
873  {
874  if (wid == WID_NCL_FILTER) {
875  this->string_filter.SetFilterTerm(this->filter_editbox.text.buf);
877  this->content.ForceRebuild();
878  this->InvalidateData();
879  }
880  }
881 
882  virtual void OnResize()
883  {
885  }
886 
887  virtual void OnReceiveContentInfo(const ContentInfo *rci)
888  {
890  this->content.ForceRebuild();
891  this->InvalidateData();
892  }
893 
894  virtual void OnDownloadComplete(ContentID cid)
895  {
896  this->content.ForceResort();
897  this->InvalidateData();
898  }
899 
900  virtual void OnConnect(bool success)
901  {
902  if (!success) {
903  ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_CONNECT, INVALID_STRING_ID, WL_ERROR);
904  delete this;
905  return;
906  }
907 
908  this->InvalidateData();
909  }
910 
916  virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
917  {
918  if (!gui_scope) return;
919  if (this->content.NeedRebuild()) this->BuildContentList();
920 
921  /* To sum all the bytes we intend to download */
922  this->filesize_sum = 0;
923  bool show_select_all = false;
924  bool show_select_upgrade = false;
925  for (ConstContentIterator iter = this->content.Begin(); iter != this->content.End(); iter++) {
926  const ContentInfo *ci = *iter;
927  switch (ci->state) {
930  this->filesize_sum += ci->filesize;
931  break;
932 
934  show_select_all = true;
935  show_select_upgrade |= ci->upgrade;
936  break;
937 
938  default:
939  break;
940  }
941  }
942 
943  /* If data == 2 then the status window caused this OnInvalidate */
946  this->SetWidgetDisabledState(WID_NCL_SELECT_ALL, !show_select_all);
947  this->SetWidgetDisabledState(WID_NCL_SELECT_UPDATE, !show_select_upgrade);
948  this->SetWidgetDisabledState(WID_NCL_OPEN_URL, this->selected == NULL || StrEmpty(this->selected->url));
949  for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
950  this->SetWidgetDisabledState(WID_NCL_TEXTFILE + tft, this->selected == NULL || this->selected->state != ContentInfo::ALREADY_HERE || this->selected->GetTextfile(tft) == NULL);
951  }
952 
953  this->GetWidget<NWidgetCore>(WID_NCL_CANCEL)->widget_data = this->filesize_sum == 0 ? STR_AI_SETTINGS_CLOSE : STR_AI_LIST_CANCEL;
954  }
955 };
956 
959 
961  &StateSorter,
962  &TypeSorter,
963  &NameSorter,
964 };
965 
967  &TagNameFilter,
968 };
969 
971 
976 {
977  for (int i = CONTENT_TYPE_BEGIN; i < CONTENT_TYPE_END; i++) {
978  GetString(NetworkContentListWindow::content_type_strs[i], STR_CONTENT_TYPE_BASE_GRAPHICS + i - CONTENT_TYPE_BASE_GRAPHICS, lastof(NetworkContentListWindow::content_type_strs[i]));
979  }
980 }
981 
985  NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
986  NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_CONTENT_TITLE, STR_NULL),
987  NWidget(WWT_DEFSIZEBOX, COLOUR_LIGHT_BLUE),
988  EndContainer(),
989  NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NCL_BACKGROUND),
992  /* Top */
993  NWidget(WWT_EMPTY, COLOUR_LIGHT_BLUE, WID_NCL_FILTER_CAPT), SetFill(1, 0), SetResize(1, 0),
994  NWidget(WWT_EDITBOX, COLOUR_LIGHT_BLUE, WID_NCL_FILTER), SetFill(1, 0), SetResize(1, 0),
995  SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
996  EndContainer(),
999  /* Left side. */
1000  NWidget(NWID_VERTICAL), SetPIP(0, 4, 0),
1004  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_CHECKBOX), SetMinimalSize(13, 1), SetDataTip(STR_EMPTY, STR_NULL),
1005  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_TYPE),
1006  SetDataTip(STR_CONTENT_TYPE_CAPTION, STR_CONTENT_TYPE_CAPTION_TOOLTIP),
1007  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_NAME), SetResize(1, 0), SetFill(1, 0),
1008  SetDataTip(STR_CONTENT_NAME_CAPTION, STR_CONTENT_NAME_CAPTION_TOOLTIP),
1009  EndContainer(),
1010  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),
1011  EndContainer(),
1012  NWidget(NWID_VSCROLLBAR, COLOUR_LIGHT_BLUE, WID_NCL_SCROLLBAR),
1013  EndContainer(),
1015  NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NCL_SEL_ALL_UPDATE), SetResize(1, 0), SetFill(1, 0),
1016  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_SELECT_UPDATE), SetResize(1, 0), SetFill(1, 0),
1017  SetDataTip(STR_CONTENT_SELECT_UPDATES_CAPTION, STR_CONTENT_SELECT_UPDATES_CAPTION_TOOLTIP),
1018  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_SELECT_ALL), SetResize(1, 0), SetFill(1, 0),
1019  SetDataTip(STR_CONTENT_SELECT_ALL_CAPTION, STR_CONTENT_SELECT_ALL_CAPTION_TOOLTIP),
1020  EndContainer(),
1021  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_UNSELECT), SetResize(1, 0), SetFill(1, 0),
1022  SetDataTip(STR_CONTENT_UNSELECT_ALL_CAPTION, STR_CONTENT_UNSELECT_ALL_CAPTION_TOOLTIP),
1023  EndContainer(),
1024  EndContainer(),
1025  /* Right side. */
1026  NWidget(NWID_VERTICAL), SetPIP(0, 4, 0),
1027  NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NCL_DETAILS), SetResize(1, 1), SetFill(1, 1), EndContainer(),
1029  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
1030  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
1031  EndContainer(),
1033  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),
1034  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
1035  EndContainer(),
1036  EndContainer(),
1037  EndContainer(),
1039  /* Bottom. */
1041  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_SEARCH_EXTERNAL), SetResize(1, 0), SetFill(1, 0),
1042  SetDataTip(STR_CONTENT_SEARCH_EXTERNAL, STR_CONTENT_SEARCH_EXTERNAL_TOOLTIP),
1044  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_CANCEL), SetResize(1, 0), SetFill(1, 0),
1045  SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
1046  NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NCL_DOWNLOAD), SetResize(1, 0), SetFill(1, 0),
1047  SetDataTip(STR_CONTENT_DOWNLOAD_CAPTION, STR_CONTENT_DOWNLOAD_CAPTION_TOOLTIP),
1048  EndContainer(),
1049  EndContainer(),
1051  /* Resize button. */
1053  NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1054  NWidget(WWT_RESIZEBOX, COLOUR_LIGHT_BLUE),
1055  EndContainer(),
1056  EndContainer(),
1057 };
1058 
1061  WDP_CENTER, "list_content", 630, 460,
1063  0,
1064  _nested_network_content_list_widgets, lengthof(_nested_network_content_list_widgets)
1065 );
1066 
1073 {
1074 #if defined(WITH_ZLIB)
1076  if (cv == NULL) {
1078  } else {
1080  }
1081 
1084 #else
1085  ShowErrorMessage(STR_CONTENT_NO_ZLIB, STR_CONTENT_NO_ZLIB_SUB, WL_ERROR);
1086  /* Connection failed... clean up the mess */
1087  if (cv != NULL) {
1088  for (ContentIterator iter = cv->Begin(); iter != cv->End(); iter++) delete *iter;
1089  }
1090 #endif /* WITH_ZLIB */
1091 }
1092 
1093 #endif /* ENABLE_NETWORK */