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 "dockBuddiesList.h"
24 #include "ui_addBuddyDlg.h"
25 #include "ui_dockBuddiesList.h"
26 #include "configuration/doomseekerconfig.h"
27 #include "serverapi/mastermanager.h"
28 #include "serverapi/playerslist.h"
29 #include "serverapi/server.h"
30 #include "patternlist.h"
31 #include "strings.hpp"
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(NULL), 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  foreach (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  foreach (const BuddyLocationInfo &location, d->buddies)
173  {
174  if (location.location() == server)
175  {
176  return true;
177  }
178  }
179  return false;
180 }
181 
182 void DockBuddiesList::patternsListContextMenu(const QPoint &pos) const
183 {
184  // First lets get the selection since that will determine the menu.
185  QModelIndexList selection = d->patternsList->selectionModel()->selectedIndexes();
186 
187  QMenu context;
188 
189  // Always have the add function.
190  QAction *addAction = context.addAction(tr("Add"));
191  connect(addAction, SIGNAL( triggered() ), this, SLOT( addBuddy() ));
192 
193  // if anything is selected allow the user to delete them.
194  if(selection.size() > 0)
195  {
196  QAction *deleteAction = context.addAction(tr("Delete"));
197  connect(deleteAction, SIGNAL( triggered() ), this, SLOT( deleteBuddy() ));
198  }
199 
200  // Finally show our menu.
201  context.exec(d->patternsList->mapToGlobal(pos));
202 }
203 
204 void DockBuddiesList::scan(const MasterManager *master)
205 {
206  if(master == NULL && masterClient == NULL)
207  return;
208  else if(master != masterClient && master != NULL)
209  { // If the master is new
210  masterClient = master;
211  }
212 
213  d->buddies.clear(); //empty list
214  foreach(ServerPtr server, masterClient->allServers())
215  {
216  if (!server->isKnown())
217  {
218  continue;
219  }
220  for(int i = 0; i < server->players().numClients(); ++i)
221  {
222  const Player player = server->player(i);
223  if (d->patterns.isExactMatchAny(player.nameColorTagsStripped()))
224  {
225  BuddyLocationInfo candidate(player, server);
226  if (!d->buddies.contains(candidate))
227  {
228  d->buddies << BuddyLocationInfo(player, server);
229  }
230  }
231  }
232  }
233 
234  // Populate list
235  d->buddiesTableModel->removeRows(0, d->buddiesTableModel->rowCount());
236  for(int i = 0;i < d->buddies.size();i++)
237  {
238  const BuddyLocationInfo &info = d->buddies[i];
239  QList<QStandardItem *> columns;
240 
241  for(int j = 0;j < HOW_MANY_BUDDIESLIST_COLUMNS;j++)
242  {
243  switch(j)
244  {
245  case BLCID_ID:
246  {
247  QStandardItem *item = new QStandardItem();
248  item->setData(i);
249  columns << item;
250  break;
251  }
252  case BLCID_BUDDY:
253  columns << new QStandardItem(info.buddy().nameColorTagsStripped());
254  break;
255  case BLCID_LOCATION:
256  columns << new QStandardItem(info.location()->name());
257  break;
258  default:
259  columns << new QStandardItem();
260  break;
261  }
262  }
263 
264  d->buddiesTableModel->appendRow(columns);
265  }
266 
267  // Fits rows to contents
268  d->buddiesTable->resizeRowsToContents();
269  emit scanCompleted();
270 }
271 
273 
274 DClass<AddBuddyDlg> : public Ui::AddBuddyDlg
275 {
276 };
277 
278 DPointered(AddBuddyDlg)
279 
280 AddBuddyDlg::AddBuddyDlg(QWidget *parent) : QDialog(parent)
281 {
282  d->setupUi(this);
283 
284  connect(d->buttonBox, SIGNAL( clicked(QAbstractButton *) ), this, SLOT( buttonBoxClicked(QAbstractButton *) ));
285 }
286 
287 AddBuddyDlg::~AddBuddyDlg()
288 {
289 }
290 
291 void AddBuddyDlg::buttonBoxClicked(QAbstractButton *button)
292 {
293  if(d->buttonBox->standardButton(button) == QDialogButtonBox::Ok)
294  {
295  if (!pattern().isValid())
296  {
297  QMessageBox::information(this, tr("Invalid Pattern"), tr("The pattern you have specified is invalid."));
298  return;
299  }
300  accept();
301  }
302  else
303  reject();
304 }
305 
306 QRegExp AddBuddyDlg::pattern() const
307 {
308  QRegExp::PatternSyntax syntax = d->basicPattern->isChecked() ? QRegExp::Wildcard : QRegExp::RegExp;
309  return QRegExp(d->patternBox->text(), Qt::CaseInsensitive, syntax);
310 }
311 
313 
314 BuddyLocationInfo::BuddyLocationInfo(const Player &buddy, ServerPtr location)
315 {
316  d->buddy = buddy;
317  d->server = location;
318 }
319 
320 BuddyLocationInfo::~BuddyLocationInfo()
321 {
322 }
323 
324 const Player &BuddyLocationInfo::buddy() const
325 {
326  return d->buddy;
327 }
328 
329 ServerPtr BuddyLocationInfo::location() const
330 {
331  return d->server;
332 }
333 
334 bool BuddyLocationInfo::operator==(const BuddyLocationInfo &other) const
335 {
336  return d->buddy == other.d->buddy
337  && d->server->address() == other.d->server->address()
338  && d->server->port() == other.d->server->port()
339  && d->server->plugin() == other.d->server->plugin();
340 }
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:38