Beispiel #1
0
void AvatarService::fetchImageFromServer(const QString& email)
{
    if (get_avatar_req_) {
        if (email == get_avatar_req_->email()) {
            return;
        }
        queue_->enqueue(email);
        return;
    }

    if (!seafApplet->accountManager()->hasAccount())
        return;
    const Account& account = seafApplet->accountManager()->accounts().front();
    qint64 mtime = 0;

    if (autoupdate_db_) {
        char *zql = sqlite3_mprintf("SELECT timestamp FROM Avatar "
                                    "WHERE filename = %Q",
                                    avatarPathForEmail(account, email).toUtf8().data());
        sqlite_foreach_selected_row(autoupdate_db_, zql, loadTimeStampCB, &mtime);
        sqlite3_free(zql);
    }

    get_avatar_req_ = new GetAvatarRequest(account, email, mtime, devicePixelRatio() * kAvatarSize);

    connect(get_avatar_req_, SIGNAL(success(const QImage&)),
            this, SLOT(onGetAvatarSuccess(const QImage&)));
    connect(get_avatar_req_, SIGNAL(failed(const ApiError&)),
            this, SLOT(onGetAvatarFailed(const ApiError&)));

    get_avatar_req_->send();
}
void AvatarService::onGetAvatarSuccess(const QImage& img)
{
    if (!get_avatar_req_) {
        return;
    }

    const QString email = get_avatar_req_->email();

    // if no change? early return
    if (img.isNull()) {
        get_avatar_req_->deleteLater();
        get_avatar_req_ = NULL;

        queue_->clearWait(email);
        return;
    }

    image_ = img;

    cache_[email] = img;

    // save image to avatars/ folder
    QString path = avatarPathForEmail(get_avatar_req_->account(), email);
    if (!img.save(path, "PNG")) {
        qWarning("Unable to save new avatar file %s", path.toUtf8().data());
    }

    // update cache db
    if (autoupdate_db_) {
        QString mtime = QString::number(get_avatar_req_->mtime());
        char *zql = sqlite3_mprintf(
            "REPLACE INTO Avatar(filename, timestamp, url, username) "
            "VALUES (%Q, %Q, %Q, %Q)",
            path.toUtf8().data(),
            mtime.toUtf8().data(),
            get_avatar_req_->account().serverUrl.toEncoded().data(),
            get_avatar_req_->account().username.toUtf8().data());
        sqlite_query_exec(autoupdate_db_, zql);
        sqlite3_free(zql);
    }

    emit avatarUpdated(email, img);

    get_avatar_req_->deleteLater();
    get_avatar_req_ = NULL;

    queue_->clearWait(email);
}
Beispiel #3
0
QString AvatarService::getAvatarFilePath(const QString& email)
{
    const Account& account = seafApplet->accountManager()->accounts().front();
    return avatarPathForEmail(account, email);
}