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" 33 #include <QMessageBox> 35 #include <QStandardItemModel> 37 class BuddyLocationInfo
40 BuddyLocationInfo(
const Player &buddy, ServerPtr location);
43 const Player &buddy()
const;
44 ServerPtr location()
const;
46 bool operator==(
const BuddyLocationInfo &other)
const;
54 DClass<BuddyLocationInfo>
61 DPointered(BuddyLocationInfo)
66 QList<BuddyLocationInfo> buddies;
67 QStandardItemModel *buddiesTableModel;
74 : QDockWidget(parent), masterClient(
nullptr), save(false)
77 this->toggleViewAction()->setIcon(QIcon(
":/icons/buddies.png"));
80 d->buddiesTableModel =
new QStandardItemModel();
84 columns << DockBuddiesList::tr(
"ID");
85 columns << DockBuddiesList::tr(
"Buddy");
86 columns << DockBuddiesList::tr(
"Location");
87 d->buddiesTableModel->setHorizontalHeaderLabels(columns);
89 d->buddiesTable->setModel(d->buddiesTableModel);
90 d->buddiesTable->setColumnHidden(0,
true);
93 d->patterns = gConfig.doomseeker.buddies;
95 for (
const QRegExp &pattern : d->patterns)
97 d->patternsList->addItem(pattern.pattern());
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&)));
106 DockBuddiesList::~DockBuddiesList()
111 gConfig.doomseeker.buddies = d->patterns;
114 void DockBuddiesList::addBuddy()
117 int result = dlg.exec();
118 if (result != QDialog::Accepted)
121 QRegExp pattern = dlg.pattern();
122 d->patterns << pattern;
123 d->patternsList->addItem(pattern.pattern());
130 void DockBuddiesList::deleteBuddy()
133 if (d->patternsList->selectionModel()->selectedIndexes().size() <= 0)
136 if (QMessageBox::warning(
this, tr(
"Delete Buddy"), tr(
"Are you sure you want to delete this pattern?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes)
140 QModelIndexList selection = d->patternsList->selectionModel()->selectedIndexes();
142 while (selection.size() > 0)
144 int largestIndex = 0;
145 for (
int i = 1; i < selection.size(); i++)
147 if (selection[i].row() > selection[largestIndex].row())
151 d->patterns.removeAt(selection[largestIndex].row());
152 delete d->patternsList->item(selection[largestIndex].row());
153 selection.removeAt(largestIndex);
161 void DockBuddiesList::followBuddy(
const QModelIndex &index)
166 ServerPtr server = d->buddies[d->buddiesTableModel->item(index.row(), BLCID_ID)->data().toInt()].location();
167 emit joinServer(server);
170 bool DockBuddiesList::hasBuddy(
const ServerPtr &server)
172 for (
const BuddyLocationInfo &location : d->buddies)
174 if (location.location() == server)
180 void DockBuddiesList::patternsListContextMenu(
const QPoint &pos)
const 183 QModelIndexList selection = d->patternsList->selectionModel()->selectedIndexes();
188 QAction *addAction = context.addAction(tr(
"Add"));
189 connect(addAction, SIGNAL(triggered()),
this, SLOT(addBuddy()));
192 if (selection.size() > 0)
194 QAction *deleteAction = context.addAction(tr(
"Delete"));
195 connect(deleteAction, SIGNAL(triggered()),
this, SLOT(deleteBuddy()));
199 context.exec(d->patternsList->mapToGlobal(pos));
204 if (master ==
nullptr && masterClient ==
nullptr)
206 else if (master != masterClient && master !=
nullptr)
207 masterClient = master;
210 for (ServerPtr server : masterClient->allServers())
212 if (!server->isKnown())
214 for (
int i = 0; i < server->players().numClients(); ++i)
216 const Player player = server->player(i);
219 BuddyLocationInfo candidate(player, server);
220 if (!d->buddies.contains(candidate))
221 d->buddies << BuddyLocationInfo(player, server);
227 d->buddiesTableModel->removeRows(0, d->buddiesTableModel->rowCount());
228 for (
int i = 0; i < d->buddies.size(); i++)
230 const BuddyLocationInfo &info = d->buddies[i];
231 QList<QStandardItem *> columns;
233 for (
int j = 0; j < HOW_MANY_BUDDIESLIST_COLUMNS; j++)
239 auto item =
new QStandardItem();
245 columns <<
new QStandardItem(info.buddy().nameColorTagsStripped());
248 columns <<
new QStandardItem(info.location()->name());
251 columns <<
new QStandardItem();
256 d->buddiesTableModel->appendRow(columns);
260 d->buddiesTable->resizeRowsToContents();
261 emit scanCompleted();
266 DClass<AddBuddyDlg> :
public Ui::AddBuddyDlg
276 connect(d->buttonBox, SIGNAL(clicked(QAbstractButton*)),
this, SLOT(buttonBoxClicked(QAbstractButton*)));
279 AddBuddyDlg::~AddBuddyDlg()
283 void AddBuddyDlg::buttonBoxClicked(QAbstractButton *button)
285 if (d->buttonBox->standardButton(button) == QDialogButtonBox::Ok)
287 if (!pattern().isValid())
289 QMessageBox::information(
this, tr(
"Invalid Pattern"), tr(
"The pattern you have specified is invalid."));
298 QRegExp AddBuddyDlg::pattern()
const 300 QRegExp::PatternSyntax syntax = d->basicPattern->isChecked() ? QRegExp::Wildcard : QRegExp::RegExp;
301 return QRegExp(d->patternBox->text(), Qt::CaseInsensitive, syntax);
306 BuddyLocationInfo::BuddyLocationInfo(
const Player &buddy, ServerPtr location)
309 d->server = location;
312 BuddyLocationInfo::~BuddyLocationInfo()
316 const Player &BuddyLocationInfo::buddy()
const 321 ServerPtr BuddyLocationInfo::location()
const 326 bool BuddyLocationInfo::operator==(
const BuddyLocationInfo &other)
const 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();
QString nameColorTagsStripped() const
Data structure that holds information about players in a server.
Manager class for a number of MasterClient instances.