bool Album::isAncestorOf(Album* album) const { bool val = false; Album* a = album; while (a && !a->isRoot()) { if (a == this) { val = true; break; } a = a->parent(); } return val; }
void TagModificationHelper::slotMultipleTagDel(QList<TAlbum* >& tags) { QString tagWithChildrens; QString tagWithImages; QMultiMap<int, TAlbum*> sortedTags; foreach(TAlbum* const t, tags) { if (!t || t->isRoot()) { continue; } AlbumPointer<TAlbum> tag(t); // find number of subtags int children = 0; AlbumIterator iter(tag); while (iter.current()) { ++children; ++iter; } if(children) tagWithChildrens.append(tag->title() + QString(" ")); QList<qlonglong> assignedItems = DatabaseAccess().db()->getItemIDsInTag(tag->id()); if(!assignedItems.isEmpty()) tagWithImages.append(tag->title() + QString(" ")); /** * Tags must be deleted from children to parents, if we don't want * to step on invalid index. Use QMultiMap to order them by distance * to root tag */ Album* parent = t; int depth = 0; while(!parent->isRoot()) { parent = parent->parent(); depth++; } sortedTags.insert(depth,tag); } // ask for deletion of children if (!tagWithChildrens.isEmpty()) { int result = KMessageBox::warningContinueCancel(0, i18n("Tags '%1' have one or more subtags. " "Deleting them will also delete " "the subtags. " "Do you want to continue?", tagWithChildrens)); if (result != KMessageBox::Continue) { return; } } QString message; if (!tagWithImages.isEmpty()) { message = i18n("Tags '%1' are assigned to one or more items. " "Do you want to continue?", tagWithImages); } else { message = i18n("Delete '%1' tag(s)?", tagWithImages); } int result = KMessageBox::warningContinueCancel(0, message, i18n("Delete Tag"), KGuiItem(i18n("Delete"), "edit-delete")); if (result == KMessageBox::Continue) { QMultiMap<int, TAlbum*>::iterator it; /** * QMultimap doesn't provide reverse iterator, -1 is required * because end() points after the last element */ for(it = sortedTags.end()-1; it != sortedTags.begin()-1; --it) { emit aboutToDeleteTag(it.value()); QString errMsg; if (!AlbumManager::instance()->deleteTAlbum(it.value(), errMsg)) { KMessageBox::error(0, errMsg); } } } }