File indexing completed on 2024-05-05 04:59:13

0001 /***************************************************************************
0002  *   Copyright (C) 2007 by Joris Guisson and Ivan Vasic                    *
0003  *   joris.guisson@gmail.com                                               *
0004  *   ivasic@gmail.com                                                      *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  *   This program is distributed in the hope that it will be useful,       *
0012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0014  *   GNU General Public License for more details.                          *
0015  *                                                                         *
0016  *   You should have received a copy of the GNU General Public License     *
0017  *   along with this program; if not, write to the                         *
0018  *   Free Software Foundation, Inc.,                                       *
0019  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
0020  ***************************************************************************/
0021 #include "peerview.h"
0022 
0023 #include <QHeaderView>
0024 #include <QIcon>
0025 #include <QMenu>
0026 
0027 #include <KConfigGroup>
0028 #include <KLocalizedString>
0029 
0030 #include "peerviewmodel.h"
0031 #include <peer/accessmanager.h>
0032 #include <util/functions.h>
0033 
0034 using namespace bt;
0035 
0036 namespace kt
0037 {
0038 
0039 PeerView::PeerView(QWidget *parent)
0040     : QTreeView(parent)
0041 {
0042     setContextMenuPolicy(Qt::CustomContextMenu);
0043     setRootIsDecorated(false);
0044     setSortingEnabled(true);
0045     setAlternatingRowColors(true);
0046     setUniformRowHeights(true);
0047 
0048     model = new PeerViewModel(this);
0049     setModel(model);
0050 
0051     context_menu = new QMenu(this);
0052     context_menu->addAction(QIcon::fromTheme("list-remove-user"), i18n("Kick Peer"), this, &PeerView::kickPeer);
0053     context_menu->addAction(QIcon::fromTheme("view-filter"), i18n("Ban Peer"), this, &PeerView::banPeer);
0054     connect(this, &QWidget::customContextMenuRequested, this, &PeerView::showContextMenu);
0055 }
0056 
0057 PeerView::~PeerView()
0058 {
0059 }
0060 
0061 void PeerView::showContextMenu(const QPoint &pos)
0062 {
0063     if (selectionModel()->selectedRows().count() == 0)
0064         return;
0065 
0066     context_menu->popup(mapToGlobal(pos));
0067 }
0068 
0069 void PeerView::banPeer()
0070 {
0071     AccessManager &aman = AccessManager::instance();
0072 
0073     QModelIndexList indices = selectionModel()->selectedRows();
0074     foreach (const QModelIndex &idx, indices) {
0075         bt::PeerInterface *peer = model->indexToPeer(idx);
0076         if (peer) {
0077             aman.banPeer(peer->getStats().ip_address);
0078             peer->kill();
0079         }
0080     }
0081 }
0082 
0083 void PeerView::kickPeer()
0084 {
0085     QModelIndexList indices = selectionModel()->selectedRows();
0086     foreach (const QModelIndex &idx, indices) {
0087         bt::PeerInterface *peer = model->indexToPeer(idx);
0088         if (peer)
0089             peer->kill();
0090     }
0091 }
0092 
0093 void PeerView::peerAdded(PeerInterface *peer)
0094 {
0095     model->peerAdded(peer);
0096 }
0097 
0098 void PeerView::peerRemoved(PeerInterface *peer)
0099 {
0100     model->peerRemoved(peer);
0101 }
0102 
0103 void PeerView::update()
0104 {
0105     model->update();
0106 }
0107 
0108 void PeerView::removeAll()
0109 {
0110     model->clear();
0111 }
0112 
0113 void PeerView::saveState(KSharedConfigPtr cfg)
0114 {
0115     KConfigGroup g = cfg->group("PeerView");
0116     QByteArray s = header()->saveState();
0117     g.writeEntry("state", s.toBase64());
0118 }
0119 
0120 void PeerView::loadState(KSharedConfigPtr cfg)
0121 {
0122     KConfigGroup g = cfg->group("PeerView");
0123     QByteArray s = QByteArray::fromBase64(g.readEntry("state", QByteArray()));
0124     if (!s.isNull()) {
0125         QHeaderView *v = header();
0126         v->restoreState(s);
0127         sortByColumn(v->sortIndicatorSection(), v->sortIndicatorOrder());
0128         model->sort(v->sortIndicatorSection(), v->sortIndicatorOrder());
0129     }
0130 }
0131 }
0132 
0133 #include "moc_peerview.cpp"