Skip to content

Commit

Permalink
Usage of [QAbstractItemModel::checkIndex] for unambiguous cases of in…
Browse files Browse the repository at this point in the history
…dices verification (when expected that index should be valid)
  • Loading branch information
Artiom Khachaturian committed Feb 14, 2025
1 parent 4ec0aca commit 8b5b80b
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions src/gui/folderstatusmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Qt::ItemFlags FolderStatusModel::flags(const QModelIndex &index) const

QVariant FolderStatusModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || (role == Qt::EditRole)) {
if ((role == Qt::EditRole) || !checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid | QAbstractItemModel::CheckIndexOption::DoNotUseParent)) {
return {};
}

Expand Down Expand Up @@ -426,7 +426,8 @@ int FolderStatusModel::rowCount(const QModelIndex &parent) const

FolderStatusModel::ItemType FolderStatusModel::classify(const QModelIndex &index) const
{
if (index.isValid()) {
if (checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid |
QAbstractItemModel::CheckIndexOption::DoNotUseParent)) {
if (const auto sub = static_cast<const SubFolderInfo *>(index.internalPointer())) {
return sub->hasLabel() ? FetchLabel : SubFolder;
}
Expand All @@ -439,31 +440,26 @@ FolderStatusModel::ItemType FolderStatusModel::classify(const QModelIndex &index

FolderStatusModel::SubFolderInfo *FolderStatusModel::infoForIndex(const QModelIndex &index) const
{
if (!index.isValid())
return nullptr;
if (const auto parentInfo = static_cast<SubFolderInfo *>(index.internalPointer())) {
if (parentInfo->hasLabel()) {
return nullptr;
}
if (index.row() >= parentInfo->_subs.size()) {
return nullptr;
}
return &parentInfo->_subs[index.row()];
} else {
if (index.row() >= _folders.count()) {
// AddButton
return nullptr;
if (checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid |
QAbstractItemModel::CheckIndexOption::DoNotUseParent)) {
if (const auto parentInfo = static_cast<SubFolderInfo *>(index.internalPointer())) {
if (parentInfo->hasLabel() || index.row() >= parentInfo->_subs.size()) {
return nullptr;
}
return &parentInfo->_subs[index.row()];
} else if (index.row() < _folders.size()){
return const_cast<SubFolderInfo *>(&_folders[index.row()]);
}
return const_cast<SubFolderInfo *>(&_folders[index.row()]);
}
return nullptr;
}

bool FolderStatusModel::isAnyAncestorEncrypted(const QModelIndex &index) const
{
auto parentIndex = parent(index);
while (parentIndex.isValid()) {
const auto info = infoForIndex(parentIndex);
if (info->isEncrypted()) {
if (info && info->isEncrypted()) {
return true;
}
parentIndex = parent(parentIndex);
Expand Down Expand Up @@ -550,7 +546,8 @@ QModelIndex FolderStatusModel::index(int row, int column, const QModelIndex &par

QModelIndex FolderStatusModel::parent(const QModelIndex &child) const
{
if (!child.isValid()) {
if (!checkIndex(child, QAbstractItemModel::CheckIndexOption::IndexIsValid |
QAbstractItemModel::CheckIndexOption::DoNotUseParent)) {
return {};
}
switch (classify(child)) {
Expand Down Expand Up @@ -654,9 +651,10 @@ void FolderStatusModel::fetchMore(const QModelIndex &parent)

void FolderStatusModel::resetAndFetch(const QModelIndex &parent)
{
const auto info = infoForIndex(parent);
info->resetSubs(this, parent);
fetchMore(parent);
if (const auto info = infoForIndex(parent)) {
info->resetSubs(this, parent);
fetchMore(parent);
}
}

void FolderStatusModel::slotGatherPermissions(const QString &href, const QMap<QString, QString> &map)
Expand Down

0 comments on commit 8b5b80b

Please sign in to comment.