Esempio n. 1
0
void ChannelWatcher::storeContactInfo()
{
    QSqlQuery insertContactIdQuery;
    insertContactIdQuery.prepare(QStringLiteral("INSERT INTO contactData VALUES (NULL, :contactId, :displayName);"));
    insertContactIdQuery.bindValue(QStringLiteral(":contactId"), m_channel->targetContact()->id());

    const KPeople::PersonData person(QStringLiteral("ktp://") + m_accountObjectPath.mid(35) + QStringLiteral("?") + m_channel->targetContact()->id());

    insertContactIdQuery.bindValue(QStringLiteral(":displayName"), person.name());

    if (!m_db.transaction()) {
        qWarning() << "Cannot get a transaction lock for inserting contact data!";
    }

    if (insertContactIdQuery.exec()) {
        m_db.commit();
        m_contactDbId = insertContactIdQuery.lastInsertId().toUInt();
    } else {
        qWarning() << "Inserting contact data into database has failed:" << insertContactIdQuery.lastError().text();
        m_db.rollback();

        // Now we assume here that it failed because of the UNIQUE constraint
        // so try to get the id from the database, assuming it already exists
        QSqlQuery selectContactId;
        selectContactId.prepare(QStringLiteral("SELECT id FROM contactData WHERE targetContact = :targetContact"));
        selectContactId.bindValue(QStringLiteral(":targetContact"), m_channel->targetContact()->id());
        selectContactId.exec();

        if (!selectContactId.lastError().isValid() && selectContactId.first()) {
            m_contactDbId = selectContactId.value(0).toUInt();
        } else {
            qWarning() << "Error while getting contact id from database:" << selectContactId.lastError().text();
        }
    }
}
QVariant RecentContactsModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()) {
        return QVariant();
    }

    QString id = sourceModel()->data(index, ResultModel::ResourceRole).toString();

    KPeople::PersonData *data = 0;

    if (m_idToData.contains(id)) {
        data = m_idToData[id];
    }

    if (!data) {
        const_cast<RecentContactsModel *>(this)->insertPersonData(id, index.row());
        return QVariant();
    }

    if (role == Qt::DisplayRole) {
        return data->name();
    } else if (role == Qt::DecorationRole) {
        return data->presenceIconName();
    } else if (role == Kicker::FavoriteIdRole) {
        return id;
    } else if (role == Kicker::HasActionListRole) {
        return true;
    } else if (role == Kicker::ActionListRole) {
        QVariantList actionList ;

        const QVariantMap &forgetAction = Kicker::createActionItem(i18n("Forget Contact"), "forget");
        actionList << forgetAction;

        const QVariantMap &forgetAllAction = Kicker::createActionItem(i18n("Forget All Contacts"), "forgetAll");
        actionList << forgetAllAction;

        actionList << Kicker::createSeparatorActionItem();

        actionList << Kicker::createActionItem(i18n("Show Contact Information..."), "showContactInfo");

        return actionList;
    } else if (role == Kicker::DescriptionRole) {
        return QString();
    }

    return QVariant();
}