/*!
    Sets the percentage of progress completed on the task described
    by the todo occurrence item to \a percentage. Note that the given
    \a percentage must be between 0 and 100, otherwise ignored.
 */
void QOrganizerTodoOccurrence::setProgressPercentage(int percentage)
{
    if (percentage >= 0 && percentage <= 100) {
        QOrganizerTodoProgress tp = detail(QOrganizerItemDetail::TypeTodoProgress);
        tp.setPercentageComplete(percentage);
        saveDetail(&tp);
    }
}
void OrganizerItemTransform::addTodoPostSaveDetails(QOrganizerItem *item, CTodo *ctodo)
{
    // Priority
    int tempint = ctodo->getPriority();
    if (tempint != -1) {
        QOrganizerItemPriority priority = item->detail<QOrganizerItemPriority>();
        priority.setPriority(static_cast<QOrganizerItemPriority::Priority>(tempint)); // assume that the saved priority is vCal compliant.
        item->saveDetail(&priority);
    }

    // Date start
    QDateTime tempdt = QDateTime::fromTime_t(ctodo->getDateStart());
    if (!tempdt.isNull()) {
        QOrganizerTodoTime todoTime = item->detail<QOrganizerTodoTime>();
        todoTime.setStartDateTime(tempdt);
        item->saveDetail(&todoTime);
    }

    // Due
    tempdt = QDateTime::fromTime_t(ctodo->getDue());
    if (!tempdt.isNull()) {
        QOrganizerTodoTime todoTime = item->detail<QOrganizerTodoTime>();
        todoTime.setDueDateTime(tempdt);
        item->saveDetail(&todoTime);
    }

    // Completed time
    tempdt = QDateTime::fromTime_t(ctodo->getCompleted());
    if (!tempdt.isNull()) {
        QOrganizerTodoProgress todoProgress = item->detail<QOrganizerTodoProgress>();
        todoProgress.setFinishedDateTime(tempdt);
        item->saveDetail(&todoProgress);
    }

    // Percent complete
    tempint = ctodo->getPercentComplete();
    if (!tempdt.isNull()) {
        QOrganizerTodoProgress todoProgress = item->detail<QOrganizerTodoProgress>();
        todoProgress.setPercentageComplete(tempint);
        item->saveDetail(&todoProgress);
    }

    // Status
    QOrganizerTodoProgress todoProgress = item->detail<QOrganizerTodoProgress>();
    todoProgress.setStatus(static_cast<QOrganizerTodoProgress::Status>(ctodo->getStatus()));
    item->saveDetail(&todoProgress);

    // Timestamps
    time_t createdTime = ctodo->getCreatedTime();
    time_t lastModifiedTime = ctodo->getLastModified();

    if (createdTime || lastModifiedTime) {
        QOrganizerItemTimestamp timeStamps = item->detail<QOrganizerItemTimestamp>();
        timeStamps.setCreated(QDateTime::fromTime_t(createdTime));
        timeStamps.setLastModified(QDateTime::fromTime_t(lastModifiedTime));
        item->saveDetail(&timeStamps);
    }
}
void tst_QOrganizerItemDetails::todoProgress()
{
    QOrganizerTodoProgress p;
    QOrganizerTodo t;

    QVERIFY(p.status() == QOrganizerTodoProgress::StatusNotStarted);
    QVERIFY(p.isEmpty());
    p.setStatus(QOrganizerTodoProgress::StatusInProgress);
    QVERIFY(!p.isEmpty());
    QVERIFY(p.status() == QOrganizerTodoProgress::StatusInProgress);

    p.setPercentageComplete(50);
    QVERIFY(p.percentageComplete() == 50);
    p.setPercentageComplete(200); // this should fail
    QVERIFY(p.percentageComplete() == 50); // value should remain unchanged.

    p.setFinishedDateTime(QDateTime(QDate(2010, 10, 13)));
    QVERIFY(p.finishedDateTime() == QDateTime(QDate(2010, 10, 13)));
    QVERIFY(p.status() != QOrganizerTodoProgress::StatusComplete); // XXX TODO: should this be automatic?
    p.setPercentageComplete(100);
    QVERIFY(p.percentageComplete() == 100);
    QVERIFY(p.status() != QOrganizerTodoProgress::StatusComplete); // XXX TODO: should this be automatic?

    // add
    QVERIFY(t.saveDetail(&p));
    QVERIFY(t.details(QOrganizerItemDetail::TypeTodoProgress).size() == 1);
    QVERIFY(t.detail(QOrganizerItemDetail::TypeTodoProgress) == p);

    // update
    p.setStatus(QOrganizerTodoProgress::StatusComplete);
    QVERIFY(p.status() == QOrganizerTodoProgress::StatusComplete);
    QVERIFY(t.detail(QOrganizerItemDetail::TypeTodoProgress) != p);
    QVERIFY(t.saveDetail(&p));
    QVERIFY(t.details(QOrganizerItemDetail::TypeTodoProgress).size() == 1); // update, not add another
    QVERIFY(t.detail(QOrganizerItemDetail::TypeTodoProgress) == p);

    // remove
    QVERIFY(t.removeDetail(&p));
    QVERIFY(t.details(QOrganizerItemDetail::TypeTodoProgress).size() == 0);
}