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"
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;
52 DClass<DockBuddiesList> :
public Ui::DockBuddiesList
55 QList<BuddyLocationInfo> buddies;
56 QStandardItemModel *buddiesTableModel;
63 : QDockWidget(parent), masterClient(NULL), save(false)
66 this->toggleViewAction()->setIcon(QIcon(
":/icons/buddies.png"));
69 d->buddiesTableModel =
new QStandardItemModel();
74 columns << tr(
"Buddy");
75 columns << tr(
"Location");
76 d->buddiesTableModel->setHorizontalHeaderLabels(columns);
78 d->buddiesTable->setModel(d->buddiesTableModel);
79 d->buddiesTable->setColumnHidden(0,
true);
82 const QVector<BuddyInfo>& buddies = gConfig.doomseeker.buddiesList;
86 d->pBuddies << buddy.pattern;
87 d->patternsList->addItem(buddy.pattern.pattern());
90 connect(d->addButton, SIGNAL( clicked() ),
this, SLOT( addBuddy() ));
91 connect(d->buddiesTable, SIGNAL( doubleClicked(
const QModelIndex &) ),
this, SLOT( followBuddy(
const QModelIndex &) ));
92 connect(d->deleteButton, SIGNAL( clicked() ),
this, SLOT( deleteBuddy() ));
93 connect(d->patternsList, SIGNAL( customContextMenuRequested(
const QPoint &) ),
this, SLOT( patternsListContextMenu(
const QPoint &) ));
96 DockBuddiesList::~DockBuddiesList()
103 gConfig.doomseeker.buddiesList.clear();
104 foreach (QRegExp pattern, d->pBuddies)
108 buddyInfo.pattern = pattern;
109 buddyInfo.patternType = pattern.patternSyntax() == QRegExp::Wildcard ? BuddyInfo::PT_BASIC : BuddyInfo::PT_ADVANCED;
111 gConfig.doomseeker.buddiesList << buddyInfo;
115 void DockBuddiesList::addBuddy()
118 int result = dlg.exec();
119 if(result != QDialog::Accepted)
122 QRegExp pattern(dlg.pattern(), Qt::CaseInsensitive, dlg.patternType() == BuddyInfo::PT_BASIC ? QRegExp::Wildcard : QRegExp::RegExp);
124 if(pattern.isValid())
126 d->pBuddies << pattern;
130 d->patternsList->addItem(dlg.pattern());
135 void DockBuddiesList::deleteBuddy()
138 if(d->patternsList->selectionModel()->selectedIndexes().size() <= 0)
141 if(QMessageBox::warning(
this, tr(
"Delete Buddy"), tr(
"Are you sure you want to delete this pattern?"), QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes)
145 QModelIndexList selection = d->patternsList->selectionModel()->selectedIndexes();
147 while(selection.size() > 0)
149 int largestIndex = 0;
150 for(
int i = 1;i < selection.size();i++)
152 if(selection[i].row() > selection[largestIndex].row())
156 d->pBuddies.removeAt(selection[largestIndex].row());
157 delete d->patternsList->item(selection[largestIndex].row());
158 selection.removeAt(largestIndex);
166 void DockBuddiesList::followBuddy(
const QModelIndex &index)
171 ServerPtr server = d->buddies[d->buddiesTableModel->item(index.row(), BLCID_ID)->data().toInt()].location();
172 emit joinServer(server);
175 bool DockBuddiesList::hasBuddy(
const ServerPtr &server)
177 foreach (
const BuddyLocationInfo &location, d->buddies)
179 if (location.location() == server)
187 void DockBuddiesList::patternsListContextMenu(
const QPoint &pos)
const
190 QModelIndexList selection = d->patternsList->selectionModel()->selectedIndexes();
195 QAction *addAction = context.addAction(tr(
"Add"));
196 connect(addAction, SIGNAL( triggered() ),
this, SLOT( addBuddy() ));
199 if(selection.size() > 0)
201 QAction *deleteAction = context.addAction(tr(
"Delete"));
202 connect(deleteAction, SIGNAL( triggered() ),
this, SLOT( deleteBuddy() ));
206 context.exec(d->patternsList->mapToGlobal(pos));
211 if(master == NULL && masterClient == NULL)
213 else if(master != masterClient && master != NULL)
215 masterClient = master;
219 foreach(ServerPtr server, masterClient->allServers())
221 if (!server->isKnown())
225 for(
int i = 0; i < server->players().numClients(); ++i)
227 const Player player = server->player(i);
230 BuddyLocationInfo candidate(player, server);
231 if (!d->buddies.contains(candidate))
233 d->buddies << BuddyLocationInfo(player, server);
240 d->buddiesTableModel->removeRows(0, d->buddiesTableModel->rowCount());
241 for(
int i = 0;i < d->buddies.size();i++)
243 const BuddyLocationInfo &info = d->buddies[i];
244 QList<QStandardItem *> columns;
246 for(
int j = 0;j < HOW_MANY_BUDDIESLIST_COLUMNS;j++)
252 QStandardItem *item =
new QStandardItem();
258 columns <<
new QStandardItem(info.buddy().nameColorTagsStripped());
261 columns <<
new QStandardItem(info.location()->name());
264 columns <<
new QStandardItem();
269 d->buddiesTableModel->appendRow(columns);
273 d->buddiesTable->resizeRowsToContents();
274 emit scanCompleted();
279 DClass<AddBuddyDlg> :
public Ui::AddBuddyDlg
289 connect(d->buttonBox, SIGNAL( clicked(QAbstractButton *) ),
this, SLOT( buttonBoxClicked(QAbstractButton *) ));
292 AddBuddyDlg::~AddBuddyDlg()
296 void AddBuddyDlg::buttonBoxClicked(QAbstractButton *button)
298 if(d->buttonBox->standardButton(button) == QDialogButtonBox::Ok)
300 if(patternType() == BuddyInfo::PT_ADVANCED)
302 QRegExp test(pattern());
305 QMessageBox::information(
this, tr(
"Invalid Pattern"), tr(
"The pattern you have specified is not a valid regular expression."));
315 QString AddBuddyDlg::pattern()
const
317 return d->patternBox->text();
320 BuddyInfo::PatternType AddBuddyDlg::patternType()
const
322 return d->basicPattern->isChecked() ? BuddyInfo::PT_BASIC : BuddyInfo::PT_ADVANCED;
327 DClass<BuddyLocationInfo>
334 DPointered(BuddyLocationInfo)
336 BuddyLocationInfo::BuddyLocationInfo(const
Player &buddy, ServerPtr location)
339 d->server = location;
342 BuddyLocationInfo::~BuddyLocationInfo()
346 const Player &BuddyLocationInfo::buddy()
const
351 ServerPtr BuddyLocationInfo::location()
const
356 bool BuddyLocationInfo::operator==(
const BuddyLocationInfo &other)
const
358 return d->buddy == other.d->buddy
359 && d->server->address() == other.d->server->address()
360 && d->server->port() == other.d->server->port()
361 && 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.