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

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 "iwfilelistmodel.h"
0022 
0023 #include <cmath>
0024 
0025 #include <KLocalizedString>
0026 
0027 #include <interfaces/torrentfileinterface.h>
0028 #include <interfaces/torrentinterface.h>
0029 #include <util/functions.h>
0030 
0031 using namespace bt;
0032 
0033 namespace kt
0034 {
0035 
0036 IWFileListModel::IWFileListModel(bt::TorrentInterface *tc, QObject *parent)
0037     : TorrentFileListModel(tc, KEEP_FILES, parent)
0038 {
0039     mmfile = IsMultimediaFile(tc->getStats().output_path);
0040     preview = false;
0041     percentage = 0;
0042 }
0043 
0044 IWFileListModel::~IWFileListModel()
0045 {
0046 }
0047 
0048 int IWFileListModel::columnCount(const QModelIndex &parent) const
0049 {
0050     if (!parent.isValid())
0051         return 5;
0052     else
0053         return 0;
0054 }
0055 
0056 QVariant IWFileListModel::headerData(int section, Qt::Orientation orientation, int role) const
0057 {
0058     if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
0059         return QVariant();
0060 
0061     if (section < 2)
0062         return TorrentFileListModel::headerData(section, orientation, role);
0063 
0064     switch (section) {
0065     case 2:
0066         return i18n("Priority");
0067     case 3:
0068         return i18n("Preview");
0069     // xgettext: no-c-format
0070     case 4:
0071         return i18nc("Percent of File Downloaded", "% Complete");
0072     default:
0073         return QVariant();
0074     }
0075 }
0076 
0077 static QString PriorityString(const bt::TorrentFileInterface *file)
0078 {
0079     switch (file->getPriority()) {
0080     case FIRST_PREVIEW_PRIORITY:
0081     case FIRST_PRIORITY:
0082         return i18nc("Download first", "First");
0083     case LAST_PREVIEW_PRIORITY:
0084     case LAST_PRIORITY:
0085         return i18nc("Download last", "Last");
0086     case ONLY_SEED_PRIORITY:
0087     case EXCLUDED:
0088         return QString();
0089     case NORMAL_PREVIEW_PRIORITY:
0090     default:
0091         return i18nc("Download Normal (not as first or last)", "Normal");
0092     }
0093 }
0094 
0095 QVariant IWFileListModel::data(const QModelIndex &index, int role) const
0096 {
0097     if (index.column() < 2 && role != Qt::ForegroundRole)
0098         return TorrentFileListModel::data(index, role);
0099 
0100     if (!index.isValid() || index.row() < 0 || index.row() >= rowCount(QModelIndex()))
0101         return QVariant();
0102 
0103     if (role == Qt::ForegroundRole && index.column() == 2 && tc->getStats().multi_file_torrent)
0104         return QVariant();
0105 
0106     if (role == Qt::DisplayRole)
0107         return displayData(index);
0108     else if (role == Qt::UserRole)
0109         return sortData(index);
0110 
0111     return QVariant();
0112 }
0113 
0114 QVariant IWFileListModel::displayData(const QModelIndex &index) const
0115 {
0116     if (tc->getStats().multi_file_torrent) {
0117         const bt::TorrentFileInterface *file = &tc->getTorrentFile(index.row());
0118         switch (index.column()) {
0119         case 2:
0120             return PriorityString(file);
0121         case 3:
0122             if (file->isMultimedia()) {
0123                 if (file->isPreviewAvailable())
0124                     return i18nc("Preview available", "Available");
0125                 else
0126                     return i18nc("Preview pending", "Pending");
0127             } else
0128                 return i18nc("No preview available", "No");
0129         case 4: {
0130             float percent = file->getDownloadPercentage();
0131             return ki18n("%1 %").subs(percent, 0, 'f', 2).toString();
0132         }
0133         default:
0134             return QVariant();
0135         }
0136     } else {
0137         switch (index.column()) {
0138         case 2:
0139             return QVariant();
0140         case 3:
0141             if (mmfile) {
0142                 if (tc->readyForPreview())
0143                     return i18nc("Preview available", "Available");
0144                 else
0145                     return i18nc("Preview pending", "Pending");
0146             } else
0147                 return i18nc("No preview available", "No");
0148         case 4: {
0149             double percent = bt::Percentage(tc->getStats());
0150             return ki18n("%1 %").subs(percent, 0, 'f', 2).toString();
0151         }
0152         default:
0153             return QVariant();
0154         }
0155     }
0156     return QVariant();
0157 }
0158 
0159 QVariant IWFileListModel::sortData(const QModelIndex &index) const
0160 {
0161     if (tc->getStats().multi_file_torrent) {
0162         const bt::TorrentFileInterface *file = &tc->getTorrentFile(index.row());
0163         switch (index.column()) {
0164         case 2:
0165             return (int)file->getPriority();
0166         case 3:
0167             if (file->isMultimedia()) {
0168                 if (file->isPreviewAvailable())
0169                     return 3;
0170                 else
0171                     return 2;
0172             } else
0173                 return 1;
0174         case 4:
0175             return file->getDownloadPercentage();
0176         }
0177     } else {
0178         switch (index.column()) {
0179         case 2:
0180             return QVariant();
0181         case 3:
0182             if (mmfile) {
0183                 if (tc->readyForPreview())
0184                     return 3;
0185                 else
0186                     return 2;
0187             } else
0188                 return 1;
0189         case 4:
0190             return bt::Percentage(tc->getStats());
0191         }
0192     }
0193     return QVariant();
0194 }
0195 
0196 bool IWFileListModel::setData(const QModelIndex &index, const QVariant &value, int role)
0197 {
0198     if (role == Qt::CheckStateRole)
0199         return TorrentFileListModel::setData(index, value, role);
0200 
0201     if (!index.isValid() || role != Qt::UserRole)
0202         return false;
0203 
0204     int r = index.row();
0205     if (r < 0 || r >= rowCount(QModelIndex()))
0206         return false;
0207 
0208     bt::TorrentFileInterface &file = tc->getTorrentFile(r);
0209     ;
0210     auto prio = (bt::Priority)value.toInt();
0211     Priority old = file.getPriority();
0212 
0213     if (prio != old) {
0214         file.setPriority(prio);
0215         dataChanged(createIndex(index.row(), 0), createIndex(index.row(), 4));
0216     }
0217 
0218     return true;
0219 }
0220 
0221 void IWFileListModel::filePercentageChanged(bt::TorrentFileInterface *file, float percentage)
0222 {
0223     Q_UNUSED(percentage)
0224     QModelIndex idx = createIndex(file->getIndex(), 4, file);
0225     Q_EMIT dataChanged(idx, idx);
0226 }
0227 
0228 void IWFileListModel::filePreviewChanged(bt::TorrentFileInterface *file, bool preview)
0229 {
0230     Q_UNUSED(preview)
0231     QModelIndex idx = createIndex(file->getIndex(), 3, file);
0232     Q_EMIT dataChanged(idx, idx);
0233 }
0234 
0235 void IWFileListModel::update()
0236 {
0237     if (!tc->getStats().multi_file_torrent) {
0238         bool changed = false;
0239         bool np = mmfile && tc->readyForPreview();
0240         if (preview != np) {
0241             preview = np;
0242             changed = true;
0243         }
0244 
0245         double perc = bt::Percentage(tc->getStats());
0246         if (fabs(perc - percentage) > 0.01) {
0247             percentage = perc;
0248             changed = true;
0249         }
0250 
0251         if (changed)
0252             dataChanged(createIndex(0, 0), createIndex(0, 4));
0253     }
0254 }
0255 }
0256 
0257 #include "moc_iwfilelistmodel.cpp"