File indexing completed on 2024-04-14 04:52:02

0001 /**************************************************************************
0002  *   Copyright (C) 2009-2011 Matthias Fuchs <mat69@gmx.net>                *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
0018  ***************************************************************************/
0019 
0020 #include "verificationmodel.h"
0021 #include "verifier.h"
0022 
0023 #include <QStringList>
0024 
0025 #include "kget_debug.h"
0026 #include <KLocalizedString>
0027 #include <QDebug>
0028 #include <QIcon>
0029 
0030 struct VerificationModelPrivate {
0031     VerificationModelPrivate()
0032     {
0033     }
0034 
0035     ~VerificationModelPrivate()
0036     {
0037     }
0038 
0039     QStringList types;
0040     QStringList checksums;
0041     QList<int> verificationStatus;
0042 };
0043 
0044 VerificationModel::VerificationModel(QObject *parent)
0045     : QAbstractTableModel(parent)
0046     , d(new VerificationModelPrivate)
0047 {
0048 }
0049 
0050 QVariant VerificationModel::data(const QModelIndex &index, int role) const
0051 {
0052     if (!index.isValid()) {
0053         return QVariant();
0054     }
0055 
0056     if ((index.column() == VerificationModel::Type) && (role == Qt::DisplayRole)) {
0057         return d->types.at(index.row());
0058     } else if ((index.column() == VerificationModel::Checksum) && (role == Qt::DisplayRole)) {
0059         return d->checksums.at(index.row());
0060     } else if (index.column() == VerificationModel::Verified) {
0061         const int status = d->verificationStatus.at(index.row());
0062         if (role == Qt::DecorationRole) {
0063             switch (status) {
0064             case Verifier::Verified:
0065                 return QIcon::fromTheme("dialog-ok");
0066             case Verifier::NotVerified:
0067                 return QIcon::fromTheme("dialog-close");
0068             case Verifier::NoResult:
0069             default:
0070                 return QIcon::fromTheme(QString());
0071             }
0072         } else if (role == Qt::EditRole) {
0073             return status;
0074         }
0075     }
0076 
0077     return QVariant();
0078 }
0079 
0080 Qt::ItemFlags VerificationModel::flags(const QModelIndex &index) const
0081 {
0082     if (!index.isValid()) {
0083         return Qt::NoItemFlags;
0084     }
0085 
0086     Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
0087     if (index.column() == VerificationModel::Type) {
0088         flags |= Qt::ItemIsEditable;
0089     } else if (index.column() == VerificationModel::Checksum) {
0090         flags |= Qt::ItemIsEditable;
0091     }
0092 
0093     return flags;
0094 }
0095 
0096 bool VerificationModel::setData(const QModelIndex &index, const QVariant &value, int role)
0097 {
0098     if (!index.isValid() || index.row() >= d->types.count()) {
0099         return false;
0100     }
0101 
0102     if ((index.column() == VerificationModel::Type) && role == Qt::EditRole) {
0103         const QString type = value.toString();
0104         if (Verifier::supportedVerficationTypes().contains(type) && !d->types.contains(type)) {
0105             d->types[index.row()] = type;
0106             Q_EMIT dataChanged(index, index);
0107             return true;
0108         }
0109     } else if ((index.column() == VerificationModel::Checksum) && role == Qt::EditRole) {
0110         const QModelIndex typeIndex = index.sibling(index.row(), VerificationModel::Type);
0111         const QString type = typeIndex.data().toString();
0112         const QString checksum = value.toString();
0113         if (Verifier::isChecksum(type, checksum)) {
0114             d->checksums[index.row()] = checksum;
0115             Q_EMIT dataChanged(index, index);
0116             return true;
0117         }
0118     } else if (index.column() == VerificationModel::Verified && role == Qt::EditRole) {
0119         d->verificationStatus[index.row()] = value.toInt();
0120         Q_EMIT dataChanged(index, index);
0121         return true;
0122     }
0123 
0124     return false;
0125 }
0126 
0127 int VerificationModel::rowCount(const QModelIndex &parent) const
0128 {
0129     Q_UNUSED(parent)
0130 
0131     return d->types.length();
0132 }
0133 
0134 int VerificationModel::columnCount(const QModelIndex &parent) const
0135 {
0136     Q_UNUSED(parent)
0137 
0138     return 3;
0139 }
0140 
0141 QVariant VerificationModel::headerData(int section, Qt::Orientation orientation, int role) const
0142 {
0143     if ((orientation != Qt::Horizontal) || (role != Qt::DisplayRole)) {
0144         return QVariant();
0145     }
0146 
0147     if (section == VerificationModel::Type) {
0148         return i18nc("the type of the hash, e.g. MD5", "Type");
0149     } else if (section == VerificationModel::Checksum) {
0150         return i18nc("the used hash for verification", "Hash");
0151     } else if (section == VerificationModel::Verified) {
0152         return i18nc("verification-result of a file, can be true/false", "Verified");
0153     }
0154 
0155     return QVariant();
0156 }
0157 
0158 bool VerificationModel::removeRows(int row, int count, const QModelIndex &parent)
0159 {
0160     if (parent.isValid() || (row < 0) || (count < 1) || (row + count > rowCount())) {
0161         return false;
0162     }
0163 
0164     beginRemoveRows(parent, row, row + count - 1);
0165     while (count) {
0166         d->types.removeAt(row);
0167         d->checksums.removeAt(row);
0168         d->verificationStatus.removeAt(row);
0169         --count;
0170     }
0171     endRemoveRows();
0172 
0173     return true;
0174 }
0175 
0176 void VerificationModel::addChecksum(const QString &type, const QString &checksum, int verified)
0177 {
0178     if (!Verifier::isChecksum(type, checksum)) {
0179         qCWarning(KGET_DEBUG) << "Could not add checksum.\nType:" << type << "\nChecksum:" << checksum;
0180         return;
0181     }
0182 
0183     // if the hashtype already exists in the model, then replace it
0184     int position = d->types.indexOf(type);
0185     if (position > -1) {
0186         d->checksums[position] = checksum;
0187         const QModelIndex index = this->index(position, VerificationModel::Checksum, QModelIndex());
0188         Q_EMIT dataChanged(index, index);
0189         return;
0190     }
0191 
0192     int rows = rowCount();
0193     beginInsertRows(QModelIndex(), rows, rows);
0194     d->types.append(type);
0195     d->checksums.append(checksum.toLower());
0196     d->verificationStatus.append(verified);
0197     endInsertRows();
0198 }
0199 
0200 void VerificationModel::addChecksums(const QMultiHash<QString, QString> &checksums)
0201 {
0202     QMultiHash<QString, QString>::const_iterator it;
0203     QMultiHash<QString, QString>::const_iterator itEnd = checksums.constEnd();
0204     for (it = checksums.constBegin(); it != itEnd; ++it) {
0205         addChecksum(it.key(), it.value());
0206     }
0207 }
0208 
0209 void VerificationModel::setVerificationStatus(const QString &type, int verified)
0210 {
0211     const int position = d->types.indexOf(type);
0212     if (position > -1) {
0213         d->verificationStatus[position] = verified;
0214         const QModelIndex index = this->index(position, VerificationModel::Verified, QModelIndex());
0215         Q_EMIT dataChanged(index, index);
0216     }
0217 }
0218 
0219 #include "moc_verificationmodel.cpp"