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

0001 /***************************************************************************
0002  *   Copyright (C) 2008 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 "webseedsmodel.h"
0022 
0023 #include <KLocalizedString>
0024 
0025 #include <interfaces/torrentinterface.h>
0026 #include <interfaces/webseedinterface.h>
0027 #include <util/functions.h>
0028 
0029 using namespace bt;
0030 
0031 namespace kt
0032 {
0033 
0034 WebSeedsModel::WebSeedsModel(QObject *parent)
0035     : QAbstractTableModel(parent)
0036     , curr_tc(nullptr)
0037 {
0038 }
0039 
0040 WebSeedsModel::~WebSeedsModel()
0041 {
0042 }
0043 
0044 void WebSeedsModel::changeTC(bt::TorrentInterface *tc)
0045 {
0046     beginResetModel();
0047     curr_tc = tc;
0048     items.clear();
0049     if (tc) {
0050         for (Uint32 i = 0; i < tc->getNumWebSeeds(); ++i) {
0051             const bt::WebSeedInterface *ws = curr_tc->getWebSeed(i);
0052             Item item;
0053             item.status = ws->getStatus();
0054             item.downloaded = ws->getTotalDownloaded();
0055             item.speed = ws->getDownloadRate();
0056             items.append(item);
0057         }
0058     }
0059     endResetModel();
0060 }
0061 
0062 bool WebSeedsModel::update()
0063 {
0064     if (!curr_tc)
0065         return false;
0066 
0067     bool ret = false;
0068 
0069     for (Uint32 i = 0; i < curr_tc->getNumWebSeeds(); ++i) {
0070         const bt::WebSeedInterface *ws = curr_tc->getWebSeed(i);
0071         Item &item = items[i];
0072         bool changed = false;
0073         if (item.status != ws->getStatus()) {
0074             changed = true;
0075             item.status = ws->getStatus();
0076         }
0077 
0078         if (item.downloaded != ws->getTotalDownloaded()) {
0079             changed = true;
0080             item.downloaded = ws->getTotalDownloaded();
0081         }
0082 
0083         if (item.speed != ws->getDownloadRate()) {
0084             changed = true;
0085             item.speed = ws->getDownloadRate();
0086         }
0087 
0088         if (changed) {
0089             dataChanged(createIndex(i, 1), createIndex(i, 3));
0090             ret = true;
0091         }
0092     }
0093 
0094     return ret;
0095 }
0096 
0097 int WebSeedsModel::rowCount(const QModelIndex &parent) const
0098 {
0099     if (parent.isValid())
0100         return 0;
0101     else
0102         return curr_tc ? curr_tc->getNumWebSeeds() : 0;
0103 }
0104 
0105 int WebSeedsModel::columnCount(const QModelIndex &parent) const
0106 {
0107     if (parent.isValid())
0108         return 0;
0109     else
0110         return 4;
0111 }
0112 
0113 QVariant WebSeedsModel::headerData(int section, Qt::Orientation orientation, int role) const
0114 {
0115     if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
0116         return QVariant();
0117 
0118     switch (section) {
0119     case 0:
0120         return i18n("URL");
0121     case 1:
0122         return i18n("Speed");
0123     case 2:
0124         return i18n("Downloaded");
0125     case 3:
0126         return i18n("Status");
0127     default:
0128         return QVariant();
0129     }
0130 }
0131 
0132 QVariant WebSeedsModel::data(const QModelIndex &index, int role) const
0133 {
0134     if (!curr_tc)
0135         return QVariant();
0136 
0137     if (!index.isValid() || index.row() >= static_cast<int>(curr_tc->getNumWebSeeds()) || index.row() < 0)
0138         return QVariant();
0139 
0140     if (role == Qt::DisplayRole) {
0141         const bt::WebSeedInterface *ws = curr_tc->getWebSeed(index.row());
0142         switch (index.column()) {
0143         case 0:
0144             return ws->getUrl().toDisplayString();
0145         case 1:
0146             return bt::BytesPerSecToString(ws->getDownloadRate());
0147         case 2:
0148             return bt::BytesToString(ws->getTotalDownloaded());
0149         case 3:
0150             return ws->getStatus();
0151         }
0152     }
0153     return QVariant();
0154 }
0155 }
0156 
0157 #include "moc_webseedsmodel.cpp"