servertooltip.cpp
1 //------------------------------------------------------------------------------
2 // servertooltip.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; 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/wadpathfinder.h"
27 #include "serverapi/tooltips/tooltipgenerator.h"
28 #include "serverapi/playerslist.h"
29 #include "serverapi/server.h"
31 
32 namespace ServerTooltip
33 {
34  QString FONT_COLOR_MISSING = "#ff0000";
35  QString FONT_COLOR_OPTIONAL = "#ff9f00";
36  QString FONT_COLOR_FOUND = "#009f00";
37 }
38 
39 QString ServerTooltip::createIwadToolTip(ServerPtr server)
40 {
41  if (!server->isKnown())
42  {
43  return QString();
44  }
45 
46  bool bFindIwad = gConfig.doomseeker.bTellMeWhereAreTheWADsWhenIHoverCursorOverWADSColumn;
47  if (bFindIwad)
48  {
49  static const QString FORMAT_TEMPLATE = "<font color=\"%1\">%2</font>";
50 
51  WadFindResult path = findWad(server, server->iwad());
52  if (path.isValid())
53  {
54  QString msg = path.path();
55  if (path.isAlias())
56  {
57  msg += " " + L10n::tr("(alias of: %1)").arg(server->iwad());
58  }
59  return FORMAT_TEMPLATE.arg(FONT_COLOR_FOUND, msg);
60  }
61  else
62  {
63  return FORMAT_TEMPLATE.arg(FONT_COLOR_MISSING, L10n::tr("MISSING"));
64  }
65  }
66 
67  return QString();
68 }
69 
70 QString ServerTooltip::createPlayersToolTip(ServerCPtr server)
71 {
72  if (server == NULL || !server->isKnown())
73  {
74  return QString();
75  }
76 
77  TooltipGenerator* tooltipGenerator = server->tooltipGenerator();
78 
79  QString ret;
80  ret = "<div style='white-space: pre'>";
81  ret += tooltipGenerator->gameInfoTableHTML();
82  if(server->players().numClients() != 0)
83  {
84  ret += tooltipGenerator->playerTableHTML();
85  }
86  ret += "</div>";
87 
88  delete tooltipGenerator;
89  return ret;
90 }
91 
92 QString ServerTooltip::createPortToolTip(ServerCPtr server)
93 {
94  if (server == NULL || !server->isKnown())
95  return QString();
96 
97  QString ret;
98  if (server->isLocked())
99  ret += "Password protected\n";
100  if (server->isLockedInGame())
101  ret += "Password protected in-game\n";
102  if (server->isSecure())
103  ret += "Enforces master bans\n";
104  return ret.trimmed();
105 }
106 
107 QString ServerTooltip::createPwadsToolTip(ServerPtr server)
108 {
109  if (server == NULL || !server->isKnown() || server->numWads() == 0)
110  {
111  return QString();
112  }
113 
114  // Prepare initial formatting.
115  static const QString toolTip = "<div style='white-space: pre'>%1</div>";
116  QString content;
117 
118  const QList<PWad>& pwads = server->wads();
119 
120  // Check if we should seek and colorize.
121  bool bFindWads = gConfig.doomseeker.bTellMeWhereAreTheWADsWhenIHoverCursorOverWADSColumn;
122 
123  // Engage!
124  if (bFindWads)
125  {
126  QStringList pwadsFormatted;
127  foreach (const PWad &wad, pwads)
128  {
129  pwadsFormatted << createPwadToolTipInfo(wad, server);
130  }
131 
132  content = "<table cellspacing=1>";
133  content += pwadsFormatted.join("\n");
134  content += "</table>";
135  }
136  else
137  {
138  foreach (const PWad &wad, pwads)
139  {
140  content += wad.name() + "\n";
141  }
142  content.chop(1); // Get rid of extra \n.
143  }
144 
145  return toolTip.arg(content);
146 }
147 
148 QString ServerTooltip::createPwadToolTipInfo(const PWad& pwad, const ServerPtr &server)
149 {
150  WadFindResult path = findWad(server, pwad.name());
151 
152  QString fontColor = "#777777";
153  QStringList cells;
154 
155  cells << pwad.name();
156  if (path.isValid())
157  {
158  fontColor = FONT_COLOR_FOUND;
159  cells << path.path();
160  }
161  else
162  {
163  if (pwad.isOptional())
164  {
165  fontColor = FONT_COLOR_OPTIONAL;
166  cells << L10n::tr("OPTIONAL");
167  }
168  else
169  {
170  fontColor = FONT_COLOR_MISSING;
171  cells << L10n::tr("MISSING");
172  }
173  }
174  if (path.isAlias())
175  {
176  cells << L10n::tr("ALIAS");
177  }
178  else
179  {
180  cells << "";
181  }
182 
183  QString formattedStringBegin = QString("<tr style=\"color: %1;\">").arg(fontColor);
184  QString formattedStringMiddle;
185  QString space = "";
186  foreach (const QString &cell, cells)
187  {
188  formattedStringMiddle += QString("<td style=\"padding-right: 5;\">%1</td>").arg(cell);
189  space = " ";
190  }
191  return formattedStringBegin + formattedStringMiddle + "</tr>";
192 }
193 
194 QString ServerTooltip::createServerNameToolTip(ServerCPtr server)
195 {
196  if (server == NULL)
197  {
198  return QString();
199  }
200 
201  TooltipGenerator* tooltipGenerator = server->tooltipGenerator();
202 
203  QString ret;
204  QString generalInfo = tooltipGenerator->generalInfoHTML();
205 
206  if (!generalInfo.isEmpty())
207  {
208  ret = "<div style='white-space: pre'>";
209  ret += generalInfo;
210  ret += "</div>";
211  }
212 
213  delete tooltipGenerator;
214  return ret;
215 }
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.
const QString & name() const
File name of the WAD.