int DeclarativeGroupManager::createOutgoingMessageEvent(int groupId, const QString &localUid,
                                                        const QString &remoteUid, const QString &text)
{
    if (groupId < 0) {
        // Try to find an appropriate group
        GroupObject *group = findGroup(localUid, remoteUid);
        if (group) {
            groupId = group->id();
        } else {
            Group g;
            g.setLocalUid(localUid);
            g.setRemoteUids(QStringList() << remoteUid);
            g.setChatType(Group::ChatTypeP2P);
            DEBUG() << Q_FUNC_INFO << "Creating group for" << localUid << remoteUid;
            if (!addGroup(g)) {
                qWarning() << Q_FUNC_INFO << "Failed creating group";
                return -1;
            }
            groupId = g.id();
        }
    }

    Event event;
    if (localUid.indexOf("/ring/tel/") >= 0)
        event.setType(Event::SMSEvent);
    else
        event.setType(Event::IMEvent);

    event.setDirection(Event::Outbound);
    event.setIsRead(true);
    event.setLocalUid(localUid);
    event.setRemoteUid(remoteUid);
    event.setFreeText(text);
    event.setStartTime(QDateTime::currentDateTime());
    event.setEndTime(event.startTime());
    event.setStatus(Event::SendingStatus);
    event.setGroupId(groupId);

    DEBUG() << Q_FUNC_INFO << groupId << localUid << remoteUid << text;
    EventModel model;
    if (model.addEvent(event))
        return event.id();

    qWarning() << Q_FUNC_INFO << "Failed creating event";
    return -1;
}
QVariant GroupModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || index.row() >= d->groups.count()) {
        return QVariant();
    }

    GroupObject *group = d->groups.value(index.row());
    if (!group)
        return QVariant();

    if (role == GroupRole) {
        return QVariant::fromValue(group->toGroup());
    } else if (role == GroupObjectRole) {
        return QVariant::fromValue<QObject*>(group);
    } else if (role == ContactIdsRole) {
        return QVariant::fromValue(group->contactIds());
    } else if (role == WeekdaySectionRole) {
        QDateTime dateTime = group->endTime().toLocalTime();

        // Return the date for the past week, and group all older items together under an
        // arbitrary older date
        const int daysDiff = QDate::currentDate().toJulianDay() - dateTime.date().toJulianDay();
        if (daysDiff < 7)
            return dateTime.date();

        // Arbitrary static date for older items..
        return QDate(2000, 1, 1);
    }

    int column = index.column();
    if (role >= BaseRole) {
        column = role - BaseRole;
        role = Qt::DisplayRole;
    }
    
    if (role != Qt::DisplayRole) {
        return QVariant();
    }

    QVariant var;
    switch (column) {
        case GroupId:
            var = QVariant::fromValue(group->id());
            break;
        case LocalUid:
            var = QVariant::fromValue(group->localUid());
            break;
        case RemoteUids:
            var = QVariant::fromValue(group->remoteUids());
            break;
        case ChatName:
            var = QVariant::fromValue(group->chatName());
            break;
        case EndTime:
            var = QVariant::fromValue(group->endTime());
            break;
        case TotalMessages:
            var = QVariant::fromValue(group->totalMessages());
            break;
        case UnreadMessages:
            var = QVariant::fromValue(group->unreadMessages());
            break;
        case SentMessages:
            var = QVariant::fromValue(group->sentMessages());
            break;
        case LastEventId:
            var = QVariant::fromValue(group->lastEventId());
            break;
        case Contacts:
            var = QVariant::fromValue(group->contacts());
            break;
        case LastMessageText:
            var = QVariant::fromValue(group->lastMessageText());
            break;
        case LastVCardFileName:
            var = QVariant::fromValue(group->lastVCardFileName());
            break;
        case LastVCardLabel:
            var = QVariant::fromValue(group->lastVCardLabel());
            break;
        case LastEventType:
            var = QVariant::fromValue((int)group->lastEventType());
            break;
        case LastEventStatus:
            var = QVariant::fromValue((int)group->lastEventStatus());
            break;
        case LastModified:
            var = QVariant::fromValue(group->lastModified());
            break;
        case StartTime:
            var = QVariant::fromValue(group->startTime());
            break;
        default:
            qDebug() << "Group::data: invalid column id??" << column;
            break;
    }

    return var;
}