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;
54 DClass<BuddyLocationInfo>
61 DPointered(BuddyLocationInfo)
66 QList<BuddyLocationInfo> buddies;
67 QStandardItemModel *buddiesTableModel;
74 : QDockWidget(parent), masterClient(NULL), save(false)
77 this->toggleViewAction()->setIcon(QIcon(
":/icons/buddies.png"));
80 d->buddiesTableModel =
new QStandardItemModel();
85 columns << tr(
"Buddy");
86 columns << tr(
"Location");
87 d->buddiesTableModel->setHorizontalHeaderLabels(columns);
89 d->buddiesTable->setModel(d->buddiesTableModel);
90 d->buddiesTable->setColumnHidden(0,
true);
93 const QVector<BuddyInfo>& buddies = gConfig.doomseeker.buddiesList;
97 d->pBuddies << buddy.pattern;
98 d->patternsList->addItem(buddy.pattern.pattern());
101 connect(d->addButton, SIGNAL( clicked() ),
this, SLOT( addBuddy() ));
102 connect(d->buddiesTable, SIGNAL( doubleClicked(
const QModelIndex &) ),
this, SLOT( followBuddy(
const QModelIndex &) ));
103 connect(d->deleteButton, SIGNAL( clicked() ),
this, SLOT( deleteBuddy() ));
104 connect(d->patternsList, SIGNAL( customContextMenuRequested(
const QPoint &) ),
this, SLOT( patternsListContextMenu(
const QPoint &) ));
107 DockBuddiesList::~DockBuddiesList()
114 gConfig.doomseeker.buddiesList.clear();
115 foreach (QRegExp pattern, d->pBuddies)
119 buddyInfo.pattern = pattern;
120 buddyInfo.patternType = pattern.patternSyntax() == QRegExp::Wildcard ? BuddyInfo::PT_BASIC : BuddyInfo::PT_ADVANCED;
122 gConfig.doomseeker.buddiesList << buddyInfo;
126 void DockBuddiesList::addBuddy()
129 int result = dlg.exec();
130 if(result != QDialog::Accepted)
133 QRegExp pattern(dlg.pattern(), Qt::CaseInsensitive, dlg.patternType() == BuddyInfo::PT_BASIC ? QRegExp::Wildcard : QRegExp::RegExp);
135 if(pattern.isValid())
137 d->pBuddies << pattern;
141 d->patternsList->addItem(dlg.pattern());
146 void DockBuddiesList::deleteBuddy()
149 if(d->patternsList->selectionModel()->selectedIndexes().size() <= 0)
152 if(QMessageBox::warning(
this, tr(
"Delete Buddy"), tr(
"Are you sure you want to delete this pattern?"), QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes)
156 QModelIndexList selection = d->patternsList->selectionModel()->selectedIndexes();
158 while(selection.size() > 0)
160 int largestIndex = 0;
161 for(
int i = 1;i < selection.size();i++)
163 if(selection[i].row() > selection[largestIndex].row())
167 d->pBuddies.removeAt(selection[largestIndex].row());
168 delete d->patternsList->item(selection[largestIndex].row());
169 selection.removeAt(largestIndex);
177 void DockBuddiesList::followBuddy(
const QModelIndex &index)
182 ServerPtr server = d->buddies[d->buddiesTableModel->item(index.row(), BLCID_ID)->data().toInt()].location();
183 emit joinServer(server);
186 bool DockBuddiesList::hasBuddy(
const ServerPtr &server)
188 foreach (
const BuddyLocationInfo &location, d->buddies)
190 if (location.location() == server)
198 void DockBuddiesList::patternsListContextMenu(
const QPoint &pos)
const 201 QModelIndexList selection = d->patternsList->selectionModel()->selectedIndexes();
206 QAction *addAction = context.addAction(tr(
"Add"));
207 connect(addAction, SIGNAL( triggered() ),
this, SLOT( addBuddy() ));
210 if(selection.size() > 0)
212 QAction *deleteAction = context.addAction(tr(
"Delete"));
213 connect(deleteAction, SIGNAL( triggered() ),
this, SLOT( deleteBuddy() ));
217 context.exec(d->patternsList->mapToGlobal(pos));
222 if(master == NULL && masterClient == NULL)
224 else if(master != masterClient && master != NULL)
226 masterClient = master;
230 foreach(ServerPtr server, masterClient->allServers())
232 if (!server->isKnown())
236 for(
int i = 0; i < server->players().numClients(); ++i)
238 const Player player = server->player(i);
241 BuddyLocationInfo candidate(player, server);
242 if (!d->buddies.contains(candidate))
244 d->buddies << BuddyLocationInfo(player, server);
251 d->buddiesTableModel->removeRows(0, d->buddiesTableModel->rowCount());
252 for(
int i = 0;i < d->buddies.size();i++)
254 const BuddyLocationInfo &info = d->buddies[i];
255 QList<QStandardItem *> columns;
257 for(
int j = 0;j < HOW_MANY_BUDDIESLIST_COLUMNS;j++)
263 QStandardItem *item =
new QStandardItem();
269 columns <<
new QStandardItem(info.buddy().nameColorTagsStripped());
272 columns <<
new QStandardItem(info.location()->name());
275 columns <<
new QStandardItem();
280 d->buddiesTableModel->appendRow(columns);
284 d->buddiesTable->resizeRowsToContents();
285 emit scanCompleted();
290 DClass<AddBuddyDlg> :
public Ui::AddBuddyDlg
300 connect(d->buttonBox, SIGNAL( clicked(QAbstractButton *) ),
this, SLOT( buttonBoxClicked(QAbstractButton *) ));
303 AddBuddyDlg::~AddBuddyDlg()
307 void AddBuddyDlg::buttonBoxClicked(QAbstractButton *button)
309 if(d->buttonBox->standardButton(button) == QDialogButtonBox::Ok)
311 if(patternType() == BuddyInfo::PT_ADVANCED)
313 QRegExp test(pattern());
316 QMessageBox::information(
this, tr(
"Invalid Pattern"), tr(
"The pattern you have specified is not a valid regular expression."));
326 QString AddBuddyDlg::pattern()
const 328 return d->patternBox->text();
331 BuddyInfo::PatternType AddBuddyDlg::patternType()
const 333 return d->basicPattern->isChecked() ? BuddyInfo::PT_BASIC : BuddyInfo::PT_ADVANCED;
338 BuddyLocationInfo::BuddyLocationInfo(
const Player &buddy, ServerPtr location)
341 d->server = location;
344 BuddyLocationInfo::~BuddyLocationInfo()
348 const Player &BuddyLocationInfo::buddy()
const 353 ServerPtr BuddyLocationInfo::location()
const 358 bool BuddyLocationInfo::operator==(
const BuddyLocationInfo &other)
const 360 return d->buddy == other.d->buddy
361 && d->server->address() == other.d->server->address()
362 && d->server->port() == other.d->server->port()
363 && 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.