kgpg
keylistproxymodel.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "keylistproxymodel.h"
00020 #include "kgpgitemnode.h"
00021 #include "kgpgitemmodel.h"
00022 #include "core/kgpgkey.h"
00023 #include "core/convert.h"
00024
00025 #include <KDebug>
00026
00027 using namespace KgpgCore;
00028
00029 KeyListProxyModel::KeyListProxyModel(QObject *parent)
00030 : QSortFilterProxyModel(parent), m_onlysecret(false), m_mintrust(TRUST_UNKNOWN), m_previewsize(22)
00031 {
00032 setFilterCaseSensitivity(Qt::CaseInsensitive);
00033 setFilterKeyColumn(-1);
00034 m_idLength = 8;
00035 }
00036
00037 bool
00038 KeyListProxyModel::hasChildren(const QModelIndex &idx) const
00039 {
00040 return sourceModel()->hasChildren(mapToSource(idx));
00041 }
00042
00043 void
00044 KeyListProxyModel::setKeyModel(KGpgItemModel *md)
00045 {
00046 m_model = md;
00047 setSourceModel(md);
00048 }
00049
00050 bool
00051 KeyListProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
00052 {
00053 KGpgNode *l = m_model->nodeForIndex(left);
00054 KGpgNode *r = m_model->nodeForIndex(right);
00055
00056 return lessThan(l, r, left.column());
00057 }
00058
00059 bool
00060 KeyListProxyModel::lessThan(const KGpgNode *left, const KGpgNode *right, const int &column) const
00061 {
00062 KGpgRootNode *r = m_model->getRootNode();
00063
00064 if (r == left->getParentKeyNode()) {
00065 if (r == right->getParentKeyNode()) {
00066 if (left->getType() == ITYPE_GROUP) {
00067 if (right->getType() == ITYPE_GROUP)
00068 return left->getName() < right->getName();
00069 else
00070 return true;
00071 } else if (right->getType() == ITYPE_GROUP)
00072 return false;
00073
00074
00075 bool test1 = (left->getType() & ITYPE_PUBLIC) && !(left->getType() & ITYPE_SECRET);
00076 bool test2 = (right->getType() & ITYPE_PUBLIC) && !(right->getType() & ITYPE_SECRET);
00077
00078
00079 if (left->getType() == ITYPE_PAIR && test2) return true;
00080 if (right->getType() == ITYPE_PAIR && test1) return false;
00081
00082 return nodeLessThan(left, right, column);
00083 } else {
00084 lessThan(left, right->getParentKeyNode(), column);
00085 }
00086 } else {
00087 if (r == right->getParentKeyNode()) {
00088 return lessThan(left->getParentKeyNode(), right, column);
00089 } else if (left->getParentKeyNode() == right->getParentKeyNode()) {
00090 if (left->getType() != right->getType())
00091 return (left->getType() < right->getType());
00092
00093 return nodeLessThan(left, right, column);
00094 } else {
00095 return lessThan(left->getParentKeyNode(), right->getParentKeyNode(), column);
00096 }
00097 }
00098 return false;
00099 }
00100
00101 bool
00102 KeyListProxyModel::nodeLessThan(const KGpgNode *left, const KGpgNode *right, const int &column) const
00103 {
00104 Q_ASSERT(left->getType() == right->getType());
00105
00106 switch (column) {
00107 case KEYCOLUMN_NAME:
00108 if (left->getType() == ITYPE_SIGN) {
00109 if (left->getName().startsWith('[') && !right->getName().startsWith('['))
00110 return false;
00111 else if (!left->getName().startsWith('[') && right->getName().startsWith('['))
00112 return true;
00113 else if (left->getName().startsWith('[') && right->getName().startsWith('['))
00114 return (left->getId() < right->getId());
00115 }
00116 return (left->getName().compare(right->getName().toLower(), Qt::CaseInsensitive) < 0);
00117 case KEYCOLUMN_EMAIL:
00118 return (left->getEmail() < right->getEmail());
00119 case KEYCOLUMN_TRUST:
00120 return (left->getTrust() < right->getTrust());
00121 case KEYCOLUMN_EXPIR:
00122 return (left->getExpiration() < right->getExpiration());
00123 case KEYCOLUMN_SIZE:
00124 if ((left->getType() & ITYPE_PAIR) && (right->getType() & ITYPE_PAIR)) {
00125 unsigned int lsign, lenc, rsign, renc;
00126
00127 if (left->getType() & ITYPE_GROUP) {
00128 const KGpgGroupMemberNode *g = static_cast<const KGpgGroupMemberNode *>(left);
00129
00130 lsign = g->getSignKeySize();
00131 lenc = g->getEncryptionKeySize();
00132 } else {
00133 const KGpgKeyNode *g = static_cast<const KGpgKeyNode *>(left);
00134
00135 lsign = g->getSignKeySize();
00136 lenc = g->getEncryptionKeySize();
00137 }
00138
00139 if (right->getType() & ITYPE_GROUP) {
00140 const KGpgGroupMemberNode *g = static_cast<const KGpgGroupMemberNode *>(right);
00141
00142 rsign = g->getSignKeySize();
00143 renc = g->getEncryptionKeySize();
00144 } else {
00145 const KGpgKeyNode *g = static_cast<const KGpgKeyNode *>(right);
00146
00147 rsign = g->getSignKeySize();
00148 renc = g->getEncryptionKeySize();
00149 }
00150
00151 if (lsign != rsign)
00152 return lsign < rsign;
00153 else
00154 return lenc < renc;
00155 } else {
00156 return (left->getSize() < right->getSize());
00157 }
00158 case KEYCOLUMN_CREAT:
00159 return (left->getCreation() < right->getCreation());
00160 default:
00161 Q_ASSERT(column == KEYCOLUMN_ID);
00162 return (left->getId().right(m_idLength) < right->getId().right(m_idLength));
00163 }
00164 }
00165
00166 bool
00167 KeyListProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
00168 {
00169 QModelIndex idx = m_model->index(source_row, 0, source_parent);
00170 KGpgNode *l = m_model->nodeForIndex(idx);
00171
00172 if (m_onlysecret) {
00173 switch (l->getType()) {
00174 case ITYPE_PUBLIC:
00175 case ITYPE_GPUBLIC:
00176 case ITYPE_GROUP:
00177 return false;
00178 default:
00179 break;
00180 }
00181 }
00182
00183 if (l->getTrust() < m_mintrust)
00184 return false;
00185
00186 if (l->getParentKeyNode() != m_model->getRootNode())
00187 return true;
00188
00189 if (l->getName().contains(filterRegExp()))
00190 return true;
00191
00192 if (l->getEmail().contains(filterRegExp()))
00193 return true;
00194
00195 if (l->getId().contains(filterRegExp()))
00196 return true;
00197
00198 return false;
00199 }
00200
00201 void
00202 KeyListProxyModel::setOnlySecret(const bool &b)
00203 {
00204 m_onlysecret = b;
00205 invalidateFilter();
00206 }
00207
00208 void
00209 KeyListProxyModel::setTrustFilter(const KgpgCore::KgpgKeyTrustFlag &t)
00210 {
00211 m_mintrust = t;
00212 invalidateFilter();
00213 }
00214
00215 KGpgNode *
00216 KeyListProxyModel::nodeForIndex(const QModelIndex &index) const
00217 {
00218 return m_model->nodeForIndex(mapToSource(index));
00219 }
00220
00221 QModelIndex
00222 KeyListProxyModel::nodeIndex(KGpgNode *node)
00223 {
00224 return mapFromSource(m_model->nodeIndex(node));
00225 }
00226
00227 void
00228 KeyListProxyModel::setPreviewSize(const int &pixel)
00229 {
00230 emit layoutAboutToBeChanged();
00231 m_previewsize = pixel;
00232 emit layoutChanged();
00233 }
00234
00235 QVariant
00236 KeyListProxyModel::data(const QModelIndex &index, int role) const
00237 {
00238 if (!index.isValid())
00239 return QVariant();
00240
00241 KGpgNode *node = nodeForIndex(index);
00242
00243 if ((node->getType() == ITYPE_UAT) && (role == Qt::DecorationRole) && (index.column() == 0)) {
00244 if (m_previewsize > 0) {
00245 KGpgUatNode *nd = static_cast<KGpgUatNode *>(node);
00246 return nd->getPixmap().scaled(m_previewsize + 5, m_previewsize, Qt::KeepAspectRatio);
00247 } else {
00248 return Convert::toPixmap(ITYPE_UAT);
00249 }
00250 } else if ((role == Qt::DisplayRole) && (index.column() == KEYCOLUMN_ID)) {
00251 QString id = m_model->data(mapToSource(index), Qt::DisplayRole).toString();
00252 return id.right(m_idLength);
00253 } else if ((role == Qt::ToolTipRole) && (index.column() == KEYCOLUMN_ID)) {
00254 QString id = m_model->data(mapToSource(index), Qt::DisplayRole).toString();
00255 return id;
00256 }
00257 return m_model->data(mapToSource(index), role);
00258 }
00259
00260 void
00261 KeyListProxyModel::setIdLength(const int &length)
00262 {
00263 if (length == m_idLength)
00264 return;
00265
00266 m_idLength = length;
00267 invalidate();
00268 }