Skip to content

Commit 9e3fdc4

Browse files
committed
Database: add CDatabaseFolder::InSubFolder
1 parent 0756a98 commit 9e3fdc4

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

App/Client/Favorite/FavoriteModel.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,15 @@ bool CFavoriteModel::Move(QModelIndex index, QModelIndex parentIndex)
282282
if(item.isFavorite())
283283
bRet = m_pDatabase->Move(item.id, nParentId);
284284
else {
285-
if(item.id != nParentId)
285+
if(item.id != nParentId && ip->FindRecursive(ipParent->item).isEmpty())
286286
bRet = m_pDatabase->MoveNode(item.id, nParentId);
287-
else
288-
qWarning(log) << "The same node:" << item.id;
287+
else {
288+
if(item.id == nParentId)
289+
qWarning(log) << "The same node:" << item.id;
290+
else
291+
qWarning(log) << "The destination node is a child node of this node."
292+
<< item.id << ipParent->item.id;
293+
}
289294
}
290295
if(bRet)
291296
bRet = MoveTree(ip->item, ipParent->item.id);

Src/Database/DatabaseTree.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,12 +346,21 @@ bool CDatabaseFolder::MoveFolder(int id, int newParentId)
346346
qWarning(log) << GetError();
347347
return false;
348348
}
349+
// Check the newParentId is not the child of id
350+
bool bRet = InSubFolder(id, newParentId);
351+
if(bRet) {
352+
SetError("The id is the parent of newParentId. "
353+
+ QString::number(id) + " is the parent of "
354+
+ QString::number(newParentId));
355+
qWarning(log) << GetError();
356+
return false;
357+
}
349358
QSqlQuery query(GetDatabase());
350359
query.prepare("UPDATE `" + m_szTableName + "` "
351360
" SET `parent_id` = :parent_id WHERE `id` = :id");
352361
query.bindValue(":id", id);
353362
query.bindValue(":parent_id", newParentId);
354-
bool bRet = query.exec();
363+
bRet = query.exec();
355364
if (bRet)
356365
emit sigChanged();
357366
else {
@@ -449,6 +458,19 @@ QList<TreeItem> CDatabaseFolder::GetSubFolders(int parentId)
449458
return folders;
450459
}
451460

461+
bool CDatabaseFolder::InSubFolder(int parentId, int id)
462+
{
463+
bool bRet = false;
464+
auto items = GetSubFolders(parentId);
465+
foreach(auto it, items) {
466+
if(it.GetId() == id)
467+
return true;
468+
bRet = InSubFolder(it.GetId(), id);
469+
if(bRet) return true;
470+
}
471+
return bRet;
472+
}
473+
452474
int CDatabaseFolder::GetCount(int parentId)
453475
{
454476
QSqlQuery query(GetDatabase());

Src/Database/DatabaseTree.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ class PLUGIN_EXPORT CDatabaseFolder : public CDatabase
7777
[[nodiscard]] TreeItem GetFolder(int id);
7878
[[nodiscard]] QList<TreeItem> GetAllFolders();
7979
[[nodiscard]] QList<TreeItem> GetSubFolders(int parentId);
80+
[[nodiscard]] bool InSubFolder(int parentId, int id);
81+
8082
/*!
8183
* \brief Get count
8284
* \param parentId: - 0 : Get all count

0 commit comments

Comments
 (0)