Пример #1
0
QContactDetail *CntTransformThumbnail::transformItemField(const CContactItemField& field, const QContact &contact)
{
    Q_UNUSED(contact);

    QContactThumbnail *thumbnail = NULL;

    if (field.ContentType().ContainsFieldType(KUidContactFieldPicture)
            || field.ContentType().ContainsFieldType(KUidContactFieldVCardMapJPEG)) {
        // check storage type
        if (field.StorageType() != KStorageTypeStore) {
            return NULL;
        }

        // Create new field
        CContactStoreField* storage = field.StoreStorage();
        QImage image;
        HBufC8 *theThing = storage->Thing();
        if (theThing != NULL) {
            QByteArray bytes((char*)theThing->Ptr(), theThing->Length());
            bool loaded = image.loadFromData(bytes, "JPG");
            if (loaded) {
                // Create thumbnail detail
                thumbnail = new QContactThumbnail();
                thumbnail->setThumbnail(image);
                // Keep jpg image, so no need for conversion when saving thumbnail detail.
                thumbnail->setValue(KThumbnailChecksum, image.cacheKey());
                thumbnail->setValue(KThumbnailJpgImage, bytes);
            }
        }
    }

    return thumbnail;
}
//Only support local file
void QDeclarativeContact::setThumbnail(const QUrl& url)
{
    QImage image(100, 50, QImage::Format_RGB32);
    image.load(url.toLocalFile());
    QContactThumbnail detail;
    detail.setThumbnail(image);
    d->m_contact.saveDetail(&detail);
    emit detailsChanged();
}
QTM_USE_NAMESPACE

// This is run in a low priority thread.
QImage ContactThumbnailImageProvider::requestImage(const QString &id, QSize *size, const QSize &req_size)
{
    if (m_thumbnails.contains(id)) {
        if (size)
            *size = req_size;
        return m_thumbnails.value(id).scaled(req_size);
    }

    /* url format:
        image://thumbnail/{managerUri.contactid}
     */

    QString managerUri = id.split('.').first();
    QString localId = id.split('.').last();

    QContactManager* manager = 0;
    if (m_managers.contains(managerUri)) {
        manager = m_managers.value(managerUri);
    } else {
        manager = QContactManager::fromUri(managerUri);
        m_managers.insert(managerUri, manager);
    }

    QContact c = manager->contact(localId.toInt());

    QImage image(
            req_size.width() > 0 ? req_size.width() : 100,
            req_size.height() > 0 ? req_size.height() : 50,
            QImage::Format_RGB32);

    QContactThumbnail t = c.detail<QContactThumbnail>();
    if (!t.thumbnail().isNull()) {
        image = t.thumbnail().scaled(image.size());
    } else {
        QContactAvatar a = c.detail<QContactAvatar>();
        QString imageUrl = a.imageUrl().isEmpty()? QLatin1String(":/default.svg") : a.imageUrl().toString();
        image.load(imageUrl);
    }

    if (size)
        *size = image.size();

    m_thumbnails.insert(id, image);

    return image;
}