void AbstractSortingStrategy::check(AbstractGroupableItem *itemToCheck)
{
    AbstractGroupableItem *item;
    if (!itemToCheck) {
        item = dynamic_cast<AbstractGroupableItem *>(sender());
    } else {
        item = itemToCheck;
    }

    if (!item) {
        qWarning() << "invalid item" << itemToCheck;
        return;
    }
    //qDebug() << item->name();

    if (item->itemType() == TaskItemType) {
        if (!(qobject_cast<TaskItem*>(item))->task()) { //ignore startup tasks
            return;
        }
    }

    if (!item->parentGroup()) {
        //qDebug() << "No parent group";
        return;
    }

    ItemList sortedList = item->parentGroup()->members();
    sortItems(sortedList);

    int oldIndex = item->parentGroup()->members().indexOf(item);
    int newIndex = sortedList.indexOf(item);
    if (oldIndex != newIndex) {
        item->parentGroup()->moveItem(oldIndex, newIndex);
    }
}
TaskGroup* AbstractGroupingStrategy::createGroup(ItemList items)
{
    GroupPtr oldGroup;
    if (!items.isEmpty() && items.first()->isGrouped()) {
        oldGroup = items.first()->parentGroup();
    } else {
        oldGroup = rootGroup();
    }

    TaskGroup *newGroup = new TaskGroup(d->groupManager);
    ItemList oldGroupMembers = oldGroup->members();
    int index = oldGroupMembers.count();
    d->createdGroups.append(newGroup);
    //kDebug() << "added group" << d->createdGroups.count();
    // NOTE: Queued is vital to make sure groups only get removed after their children, for
    // correct QAbstractItemModel (TasksModel) transaction semantics.
    connect(newGroup, SIGNAL(itemRemoved(AbstractGroupableItem*)), this, SLOT(checkGroup()), Qt::QueuedConnection);
    foreach (AbstractGroupableItem * item, items) {
        int idx = oldGroupMembers.indexOf(item);
        if (idx >= 0 && idx < index) {
            index = idx;
        }
        newGroup->add(item);
    }
bool AbstractSortingStrategy::moveItem(AbstractGroupableItem *item, int newIndex)
{
    //qDebug() << "move to " << newIndex;
    if (!item->parentGroup()) {
        qWarning() << "error: no parentgroup but the item was asked to move";
        return false;
    }

    const ItemList list = item->parentGroup()->members();
    if ((newIndex < 0) || (newIndex >= list.size())) {
        newIndex = list.size();
    }

    int oldIndex = list.indexOf(item);
    if (newIndex > oldIndex) {
        newIndex--; //the index has to be adjusted if we move the item from right to left because the item on the left is removed first
    }

    if (oldIndex != newIndex) {
        return item->parentGroup()->moveItem(oldIndex, newIndex);
    }

    return false;
}