• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kget

  • sources
  • kde-4.12
  • kdenetwork
  • kget
  • core
verificationmodel.cpp
Go to the documentation of this file.
1 /**************************************************************************
2 * Copyright (C) 2009-2011 Matthias Fuchs <mat69@gmx.net> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
18 ***************************************************************************/
19 
20 #include "verificationmodel.h"
21 #include "verifier.h"
22 
23 #include <QtCore/QStringList>
24 
25 #include <KDebug>
26 #include <KIcon>
27 #include <KLocale>
28 
29 struct VerificationModelPrivate
30 {
31  VerificationModelPrivate()
32  {
33  }
34 
35  ~VerificationModelPrivate()
36  {
37  }
38 
39  QStringList types;
40  QStringList checksums;
41  QList<int> verificationStatus;
42 };
43 
44 VerificationModel::VerificationModel(QObject *parent)
45  : QAbstractTableModel(parent),
46  d(new VerificationModelPrivate)
47 {
48 }
49 
50 QVariant VerificationModel::data(const QModelIndex &index, int role) const
51 {
52  if (!index.isValid()) {
53  return QVariant();
54  }
55 
56  if ((index.column() == VerificationModel::Type) && (role == Qt::DisplayRole)) {
57  return d->types.at(index.row());
58  } else if ((index.column() == VerificationModel::Checksum) && (role == Qt::DisplayRole)) {
59  return d->checksums.at(index.row());
60  } else if (index.column() == VerificationModel::Verified) {
61  const int status = d->verificationStatus.at(index.row());
62  if (role == Qt::DecorationRole) {
63  switch (status) {
64  case Verifier::Verified:
65  return KIcon("dialog-ok");
66  case Verifier::NotVerified:
67  return KIcon("dialog-close");
68  case Verifier::NoResult:
69  default:
70  return KIcon();
71  }
72  } else if (role == Qt::EditRole) {
73  return status;
74  }
75  }
76 
77  return QVariant();
78 }
79 
80 Qt::ItemFlags VerificationModel::flags(const QModelIndex &index) const
81 {
82  if (!index.isValid()) {
83  return 0;
84  }
85 
86  Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
87  if (index.column() == VerificationModel::Type)
88  {
89  flags |= Qt::ItemIsEditable;
90  }
91  else if (index.column() == VerificationModel::Checksum)
92  {
93  flags |= Qt::ItemIsEditable;
94  }
95 
96  return flags;
97 }
98 
99 bool VerificationModel::setData(const QModelIndex &index, const QVariant &value, int role)
100 {
101  if (!index.isValid() || index.row() >= d->types.count()) {
102  return false;
103  }
104 
105  if ((index.column() == VerificationModel::Type) && role == Qt::EditRole) {
106  const QString type = value.toString();
107  if (Verifier::supportedVerficationTypes().contains(type) && !d->types.contains(type)) {
108  d->types[index.row()] = type;
109  emit dataChanged(index, index);
110  return true;
111  }
112  } else if ((index.column() == VerificationModel::Checksum) && role == Qt::EditRole) {
113  const QModelIndex typeIndex = index.sibling(index.row(), VerificationModel::Type);
114  const QString type = typeIndex.data().toString();
115  const QString checksum = value.toString();
116  if (Verifier::isChecksum(type, checksum)) {
117  d->checksums[index.row()] = checksum;
118  emit dataChanged(index, index);
119  return true;
120  }
121  } else if (index.column() == VerificationModel::Verified && role == Qt::EditRole) {
122  d->verificationStatus[index.row()] = value.toInt();
123  emit dataChanged(index, index);
124  return true;
125  }
126 
127  return false;
128 }
129 
130 int VerificationModel::rowCount(const QModelIndex &parent) const
131 {
132  Q_UNUSED(parent)
133 
134  return d->types.length();
135 }
136 
137 int VerificationModel::columnCount(const QModelIndex &parent) const
138 {
139  Q_UNUSED(parent)
140 
141  return 3;
142 }
143 
144 QVariant VerificationModel::headerData(int section, Qt::Orientation orientation, int role) const
145 {
146  if ((orientation != Qt::Horizontal) || (role != Qt::DisplayRole)) {
147  return QVariant();
148  }
149 
150  if (section == VerificationModel::Type) {
151  return i18nc("the type of the hash, e.g. MD5", "Type");
152  } else if (section == VerificationModel::Checksum) {
153  return i18nc("the used hash for verification", "Hash");
154  } else if (section == VerificationModel::Verified) {
155  return i18nc("verification-result of a file, can be true/false", "Verified");
156  }
157 
158  return QVariant();
159 }
160 
161 bool VerificationModel::removeRows(int row, int count, const QModelIndex &parent)
162 {
163  if (parent.isValid() || (row < 0) || (count < 1) || (row + count > rowCount())) {
164  return false;
165  }
166 
167  beginRemoveRows(parent, row, row + count - 1);
168  while (count) {
169  d->types.removeAt(row);
170  d->checksums.removeAt(row);
171  d->verificationStatus.removeAt(row);
172  --count;
173  }
174  endRemoveRows();
175 
176  return true;
177 }
178 
179 void VerificationModel::addChecksum(const QString &type, const QString &checksum, int verified)
180 {
181  if (!Verifier::isChecksum(type, checksum)) {
182  kWarning(5001) << "Could not add checksum.\nType:" << type << "\nChecksum:" << checksum;
183  return;
184  }
185 
186  //if the hashtype already exists in the model, then replace it
187  int position = d->types.indexOf(type);
188  if (position > -1) {
189  d->checksums[position] = checksum;
190  const QModelIndex index = this->index(position, VerificationModel::Checksum, QModelIndex());
191  emit dataChanged(index, index);
192  return;
193  }
194 
195  int rows = rowCount();
196  beginInsertRows(QModelIndex(), rows, rows);
197  d->types.append(type);
198  d->checksums.append(checksum.toLower());
199  d->verificationStatus.append(verified);
200  endInsertRows();
201 }
202 
203 void VerificationModel::addChecksums(const QHash<QString, QString> &checksums)
204 {
205  QHash<QString, QString>::const_iterator it;
206  QHash<QString, QString>::const_iterator itEnd = checksums.constEnd();
207  for (it = checksums.constBegin(); it != itEnd; ++it) {
208  addChecksum(it.key(), it.value());
209  }
210 }
211 
212 void VerificationModel::setVerificationStatus(const QString &type, int verified)
213 {
214  const int position = d->types.indexOf(type);
215  if (position > -1) {
216  d->verificationStatus[position] = verified;
217  const QModelIndex index = this->index(position, VerificationModel::Verified, QModelIndex());
218  emit dataChanged(index, index);
219  }
220 }
VerificationModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: verificationmodel.cpp:80
verificationmodel.h
VerificationModel::removeRows
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
Definition: verificationmodel.cpp:161
Verifier::Verified
Definition: verifier.h:80
Verifier::NoResult
Definition: verifier.h:78
Verifier::supportedVerficationTypes
static QStringList supportedVerficationTypes()
Returns the supported verification types.
Definition: verifier.cpp:216
Verifier::isChecksum
static bool isChecksum(const QString &type, const QString &checksum)
Tries to check if the checksum is a checksum and if it is supported it compares the diggestLength and...
Definition: verifier.cpp:256
QObject
VerificationModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: verificationmodel.cpp:137
VerificationModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: verificationmodel.cpp:130
VerificationModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Definition: verificationmodel.cpp:99
VerificationModel::setVerificationStatus
void setVerificationStatus(const QString &type, int verified)
Sets the verificationStatus for type.
Definition: verificationmodel.cpp:212
VerificationModel::VerificationModel
VerificationModel(QObject *parent=0)
Definition: verificationmodel.cpp:44
VerificationModel::Verified
Definition: verificationmodel.h:40
VerificationModel::data
QVariant data(const QModelIndex &index, int role) const
Definition: verificationmodel.cpp:50
verifier.h
VerificationModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: verificationmodel.cpp:144
VerificationModel::addChecksum
void addChecksum(const QString &type, const QString &checksum, int verified=0)
Add a checksum that is later used in the verification process.
Definition: verificationmodel.cpp:179
VerificationModel::addChecksums
void addChecksums(const QHash< QString, QString > &checksums)
Add multiple checksums that will later be used in the verification process.
Definition: verificationmodel.cpp:203
Verifier::NotVerified
Definition: verifier.h:79
VerificationModel::Type
Definition: verificationmodel.h:38
QAbstractTableModel
VerificationModel::Checksum
Definition: verificationmodel.h:39
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:18 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kget

Skip menu "kget"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal