dockBuddiesList.cpp
1 //------------------------------------------------------------------------------
2 // dockBuddiesList.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) 2009 Braden "Blzut3" Obrzut <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 #include "configuration/doomseekerconfig.h"
24 #include "dockBuddiesList.h"
25 #include "patternlist.h"
26 #include "serverapi/mastermanager.h"
27 #include "serverapi/playerslist.h"
28 #include "serverapi/server.h"
29 #include "strings.hpp"
30 #include "ui_addBuddyDlg.h"
31 #include "ui_dockBuddiesList.h"
32 #include <QMenu>
33 #include <QMessageBox>
34 #include <QRegExp>
35 #include <QStandardItemModel>
36 
37 class BuddyLocationInfo
38 {
39 public:
40  BuddyLocationInfo(const Player &buddy, ServerPtr location);
41  ~BuddyLocationInfo();
42 
43  const Player &buddy() const;
44  ServerPtr location() const;
45 
46  bool operator==(const BuddyLocationInfo &other) const;
47 
48 private:
50 };
51 
52 // [BL] This was moved earlier in the file in order to prevent double specialization
53 // with some compilers
54 DClass<BuddyLocationInfo>
55 {
56 public:
57  Player buddy;
58  ServerPtr server;
59 };
60 
61 DPointered(BuddyLocationInfo)
62 
63 DClass<DockBuddiesList> : public Ui::DockBuddiesList
64 {
65 public:
66  QList<BuddyLocationInfo> buddies;
67  QStandardItemModel *buddiesTableModel;
68  PatternList patterns;
69 };
70 
71 DPointered(DockBuddiesList)
72 
73 DockBuddiesList::DockBuddiesList(QWidget *parent)
74  : QDockWidget(parent), masterClient(nullptr), save(false)
75 {
76  d->setupUi(this);
77  this->toggleViewAction()->setIcon(QIcon(":/icons/buddies.png"));
78 
79  // Set up the model
80  d->buddiesTableModel = new QStandardItemModel();
81 
82  // Not working yet.
83  QStringList columns;
84  columns << DockBuddiesList::tr("ID");
85  columns << DockBuddiesList::tr("Buddy");
86  columns << DockBuddiesList::tr("Location");
87  d->buddiesTableModel->setHorizontalHeaderLabels(columns);
88 
89  d->buddiesTable->setModel(d->buddiesTableModel);
90  d->buddiesTable->setColumnHidden(0, true); // Hide the ID
91 
92  // Read config
93  d->patterns = gConfig.doomseeker.buddies;
94 
95  for (const QRegExp &pattern : d->patterns)
96  {
97  d->patternsList->addItem(pattern.pattern());
98  }
99 
100  connect(d->addButton, SIGNAL(clicked()), this, SLOT(addBuddy()));
101  connect(d->buddiesTable, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(followBuddy(const QModelIndex&)));
102  connect(d->deleteButton, SIGNAL(clicked()), this, SLOT(deleteBuddy()));
103  connect(d->patternsList, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(patternsListContextMenu(const QPoint&)));
104 }
105 
106 DockBuddiesList::~DockBuddiesList()
107 {
108  // See if we made any modification since modifying the setting will cause a
109  // write cycle.
110  if (save)
111  gConfig.doomseeker.buddies = d->patterns;
112 }
113 
114 void DockBuddiesList::addBuddy()
115 {
116  AddBuddyDlg dlg;
117  int result = dlg.exec();
118  if (result != QDialog::Accepted)
119  return;
120 
121  QRegExp pattern = dlg.pattern();
122  d->patterns << pattern;
123  d->patternsList->addItem(pattern.pattern());
124 
125  scan(masterClient);
126 
127  save = true;
128 }
129 
130 void DockBuddiesList::deleteBuddy()
131 {
132  // Do nothing if nothing is selected.
133  if (d->patternsList->selectionModel()->selectedIndexes().size() <= 0)
134  return;
135 
136  if (QMessageBox::warning(this, tr("Delete Buddy"), tr("Are you sure you want to delete this pattern?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes)
137  != QMessageBox::Yes)
138  return;
139 
140  QModelIndexList selection = d->patternsList->selectionModel()->selectedIndexes();
141  // We have to remove the bottom most row until they are all gone.
142  while (selection.size() > 0)
143  {
144  int largestIndex = 0;
145  for (int i = 1; i < selection.size(); i++)
146  {
147  if (selection[i].row() > selection[largestIndex].row())
148  largestIndex = i;
149  }
150 
151  d->patterns.removeAt(selection[largestIndex].row());
152  delete d->patternsList->item(selection[largestIndex].row());
153  selection.removeAt(largestIndex); // remove index
154  }
155 
156  scan(masterClient);
157 
158  save = true;
159 }
160 
161 void DockBuddiesList::followBuddy(const QModelIndex &index)
162 {
163  // Folow the buddy into the server.
164  QString error;
165 
166  ServerPtr server = d->buddies[d->buddiesTableModel->item(index.row(), BLCID_ID)->data().toInt()].location();
167  emit joinServer(server);
168 }
169 
170 bool DockBuddiesList::hasBuddy(const ServerPtr &server)
171 {
172  for (const BuddyLocationInfo &location : d->buddies)
173  {
174  if (location.location() == server)
175  return true;
176  }
177  return false;
178 }
179 
180 void DockBuddiesList::patternsListContextMenu(const QPoint &pos) const
181 {
182  // First lets get the selection since that will determine the menu.
183  QModelIndexList selection = d->patternsList->selectionModel()->selectedIndexes();
184 
185  QMenu context;
186 
187  // Always have the add function.
188  QAction *addAction = context.addAction(tr("Add"));
189  connect(addAction, SIGNAL(triggered()), this, SLOT(addBuddy()));
190 
191  // if anything is selected allow the user to delete them.
192  if (selection.size() > 0)
193  {
194  QAction *deleteAction = context.addAction(tr("Delete"));
195  connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteBuddy()));
196  }
197 
198  // Finally show our menu.
199  context.exec(d->patternsList->mapToGlobal(pos));
200 }
201 
202 void DockBuddiesList::scan(const MasterManager *master)
203 {
204  if (master == nullptr && masterClient == nullptr)
205  return;
206  else if (master != masterClient && master != nullptr) // If the master is new
207  masterClient = master;
208 
209  d->buddies.clear(); //empty list
210  for (ServerPtr server : masterClient->allServers())
211  {
212  if (!server->isKnown())
213  continue;
214  for (int i = 0; i < server->players().numClients(); ++i)
215  {
216  const Player player = server->player(i);
217  if (d->patterns.isExactMatchAny(player.nameColorTagsStripped()))
218  {
219  BuddyLocationInfo candidate(player, server);
220  if (!d->buddies.contains(candidate))
221  d->buddies << BuddyLocationInfo(player, server);
222  }
223  }
224  }
225 
226  // Populate list
227  d->buddiesTableModel->removeRows(0, d->buddiesTableModel->rowCount());
228  for (int i = 0; i < d->buddies.size(); i++)
229  {
230  const BuddyLocationInfo &info = d->buddies[i];
231  QList<QStandardItem *> columns;
232 
233  for (int j = 0; j < HOW_MANY_BUDDIESLIST_COLUMNS; j++)
234  {
235  switch (j)
236  {
237  case BLCID_ID:
238  {
239  auto item = new QStandardItem();
240  item->setData(i);
241  columns << item;
242  break;
243  }
244  case BLCID_BUDDY:
245  columns << new QStandardItem(info.buddy().nameColorTagsStripped());
246  break;
247  case BLCID_LOCATION:
248  columns << new QStandardItem(info.location()->name());
249  break;
250  default:
251  columns << new QStandardItem();
252  break;
253  }
254  }
255 
256  d->buddiesTableModel->appendRow(columns);
257  }
258 
259  // Fits rows to contents
260  d->buddiesTable->resizeRowsToContents();
261  emit scanCompleted();
262 }
263 
265 
266 DClass<AddBuddyDlg> : public Ui::AddBuddyDlg
267 {
268 };
269 
270 DPointered(AddBuddyDlg)
271 
272 AddBuddyDlg::AddBuddyDlg(QWidget *parent) : QDialog(parent)
273 {
274  d->setupUi(this);
275 
276  connect(d->buttonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(buttonBoxClicked(QAbstractButton*)));
277 }
278 
279 AddBuddyDlg::~AddBuddyDlg()
280 {
281 }
282 
283 void AddBuddyDlg::buttonBoxClicked(QAbstractButton *button)
284 {
285  if (d->buttonBox->standardButton(button) == QDialogButtonBox::Ok)
286  {
287  if (!pattern().isValid())
288  {
289  QMessageBox::information(this, tr("Invalid Pattern"), tr("The pattern you have specified is invalid."));
290  return;
291  }
292  accept();
293  }
294  else
295  reject();
296 }
297 
298 QRegExp AddBuddyDlg::pattern() const
299 {
300  QRegExp::PatternSyntax syntax = d->basicPattern->isChecked() ? QRegExp::Wildcard : QRegExp::RegExp;
301  return QRegExp(d->patternBox->text(), Qt::CaseInsensitive, syntax);
302 }
303 
305 
306 BuddyLocationInfo::BuddyLocationInfo(const Player &buddy, ServerPtr location)
307 {
308  d->buddy = buddy;
309  d->server = location;
310 }
311 
312 BuddyLocationInfo::~BuddyLocationInfo()
313 {
314 }
315 
316 const Player &BuddyLocationInfo::buddy() const
317 {
318  return d->buddy;
319 }
320 
321 ServerPtr BuddyLocationInfo::location() const
322 {
323  return d->server;
324 }
325 
326 bool BuddyLocationInfo::operator==(const BuddyLocationInfo &other) const
327 {
328  return d->buddy == other.d->buddy
329  && d->server->address() == other.d->server->address()
330  && d->server->port() == other.d->server->port()
331  && d->server->plugin() == other.d->server->plugin();
332 }
QString nameColorTagsStripped() const
Definition: player.cpp:94
Data structure that holds information about players in a server.
Definition: player.h:37
Manager class for a number of MasterClient instances.
Definition: mastermanager.h:40