servertooltip.cpp
1 //------------------------------------------------------------------------------
2 // servertooltip.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2015 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "servertooltip.h"
24 
25 #include "configuration/doomseekerconfig.h"
26 #include "pathfinder/pathfinder.h"
27 #include "pathfinder/wadpathfinder.h"
28 #include "serverapi/playerslist.h"
29 #include "serverapi/server.h"
31 #include "serverapi/tooltips/tooltipgenerator.h"
32 
33 namespace ServerTooltip
34 {
35 QString FONT_COLOR_MISSING = "#ff0000";
36 QString FONT_COLOR_WARNING = "#ff9f00";
37 QString FONT_COLOR_FOUND = "#009f00";
38 }
39 
40 QString ServerTooltip::createIwadToolTip(ServerPtr server)
41 {
42  if (!server->isKnown())
43  {
44  return QString();
45  }
46 
47  bool bFindIwad = gConfig.doomseeker.bTellMeWhereAreTheWADsWhenIHoverCursorOverWADSColumn;
48  if (bFindIwad)
49  {
50  static const QString FORMAT_TEMPLATE = "<font color=\"%1\">%2</font>";
51 
52  WadFindResult path = findWad(server, server->iwad());
53  if (path.isValid())
54  {
55  QString msg = path.path();
56  if (path.isAlias())
57  {
58  msg += " " + L10n::tr("(alias of: %1)").arg(server->iwad());
59  }
60  return FORMAT_TEMPLATE.arg(FONT_COLOR_FOUND, msg);
61  }
62  else
63  {
64  return FORMAT_TEMPLATE.arg(FONT_COLOR_MISSING, L10n::tr("MISSING"));
65  }
66  }
67 
68  return QString();
69 }
70 
71 QString ServerTooltip::createPlayersToolTip(ServerCPtr server)
72 {
73  if (server == nullptr || !server->isKnown())
74  {
75  return QString();
76  }
77 
78  TooltipGenerator *tooltipGenerator = server->tooltipGenerator();
79 
80  QString ret;
81  ret = "<div style='white-space: pre'>";
82  ret += tooltipGenerator->gameInfoTableHTML();
83  if (server->players().numClients() != 0)
84  {
85  ret += tooltipGenerator->playerTableHTML();
86  }
87  ret += "</div>";
88 
89  delete tooltipGenerator;
90  return ret;
91 }
92 
93 QString ServerTooltip::createPortToolTip(ServerCPtr server)
94 {
95  if (server == nullptr || !server->isKnown())
96  return QString();
97 
98  QString ret;
99  if (server->isLocked())
100  ret += "Password protected\n";
101  if (server->isLockedInGame())
102  ret += "Password protected in-game\n";
103  if (server->isSecure())
104  ret += "Enforces master bans\n";
105  return ret.trimmed();
106 }
107 
108 QString ServerTooltip::createPwadsToolTip(ServerPtr server)
109 {
110  if (server == nullptr || !server->isKnown() || server->numWads() == 0)
111  {
112  return QString();
113  }
114 
115  // Prepare initial formatting.
116  static const QString toolTip = "<div style='white-space: pre'>%1</div>";
117  QString content;
118 
119  const QList<PWad> &pwads = server->wads();
120 
121  // Check if we should seek and colorize.
122  bool bFindWads = gConfig.doomseeker.bTellMeWhereAreTheWADsWhenIHoverCursorOverWADSColumn;
123 
124  // Engage!
125  if (bFindWads)
126  {
127  QStringList pwadsFormatted;
128  PathFinder pathFinder = server->wadPathFinder();
129  WadPathFinder wadFinder(pathFinder);
130  for (const PWad &wad : pwads)
131  {
132  pwadsFormatted << createPwadToolTipInfo(wad, wadFinder);
133  }
134 
135  content = "<table cellspacing=1>";
136  content += pwadsFormatted.join("\n");
137  content += "</table>";
138  }
139  else
140  {
141  for (const PWad &wad : pwads)
142  {
143  content += wad.name() + "\n";
144  }
145  content.chop(1); // Get rid of extra \n.
146  }
147 
148  return toolTip.arg(content);
149 }
150 
151 QString ServerTooltip::createPwadToolTipInfo(const PWad &pwad, WadPathFinder &wadFinder)
152 {
153  WadFindResult findResult = wadFinder.find(pwad.name());
154 
155  QString fontColor = "#777777";
156  QStringList cells;
157 
158  cells << pwad.name();
159  if (findResult.isValid())
160  {
161  fontColor = FONT_COLOR_FOUND;
162  cells << findResult.path();
163  }
164  else
165  {
166  if (pwad.isOptional())
167  {
168  fontColor = FONT_COLOR_WARNING;
169  cells << L10n::tr("OPTIONAL");
170  }
171  else
172  {
173  fontColor = FONT_COLOR_MISSING;
174  cells << L10n::tr("MISSING");
175  }
176  }
177  if (findResult.isAlias())
178  {
179  cells << L10n::tr("ALIAS");
180  }
181  else
182  {
183  cells << "";
184  }
185 
186  QString formattedStringBegin = QString("<tr style=\"color: %1;\">").arg(fontColor);
187  QString formattedStringMiddle;
188  QString space = "";
189  for (const QString &cell : cells)
190  {
191  formattedStringMiddle += QString("<td style=\"padding-right: 5;\">%1</td>").arg(cell);
192  space = " ";
193  }
194  return formattedStringBegin + formattedStringMiddle + "</tr>";
195 }
196 
197 QString ServerTooltip::createServerNameToolTip(ServerCPtr server)
198 {
199  if (server == nullptr)
200  {
201  return QString();
202  }
203 
204  TooltipGenerator *tooltipGenerator = server->tooltipGenerator();
205 
206  QString ret;
207  QString generalInfo = tooltipGenerator->generalInfoHTML();
208 
209  if (!generalInfo.isEmpty())
210  {
211  ret = "<div style='white-space: pre'>";
212  ret += generalInfo;
213  ret += "</div>";
214  }
215 
216  delete tooltipGenerator;
217  return ret;
218 }
Performs a case-insensitive (OS independent) file searches.
Definition: pathfinder.h:81
PWAD hosted on a server.
virtual QString generalInfoHTML()
General info about server, like server name, version, email, etc.
virtual QString gameInfoTableHTML()
General info about current game (fraglimit, team scores, etc.)
bool isOptional() const
Is this WAD required to join the server?
virtual QString playerTableHTML()
Player table that is created when cursor hovers over players column.
Wrapper for PathFinder that specializes in findings WADs.
Definition: wadpathfinder.h:76
const QString & name() const
File name of the WAD.