Exemple #1
0
void IPTCEnvelope::applyMetadata(QByteArray& iptcData)
{
    KPMetadata meta;
    meta.setIptc(iptcData);

    if (d->destinationCheck->isChecked())
    {
        meta.setIptcTagString("Iptc.Envelope.Destination", d->destinationEdit->toPlainText());
    }
    else
    {
        meta.removeIptcTag("Iptc.Envelope.Destination");
    }

    if (d->envelopeIDCheck->isChecked())
    {
        meta.setIptcTagString("Iptc.Envelope.EnvelopeNumber", d->envelopeIDEdit->text());
    }
    else
    {
        meta.removeIptcTag("Iptc.Envelope.EnvelopeNumber");
    }

    if (d->serviceIDCheck->isChecked())
    {
        meta.setIptcTagString("Iptc.Envelope.ServiceId", d->serviceIDEdit->text());
    }
    else
    {
        meta.removeIptcTag("Iptc.Envelope.ServiceId");
    }

    if (d->unoIDCheck->isChecked())
    {
        meta.setIptcTagString("Iptc.Envelope.UNO", d->unoIDEdit->text());
    }
    else
    {
        meta.removeIptcTag("Iptc.Envelope.UNO");
    }

    if (d->productIDCheck->isChecked())
    {
        meta.setIptcTagString("Iptc.Envelope.ProductId", d->productIDEdit->text());
    }
    else
    {
        meta.removeIptcTag("Iptc.Envelope.ProductId");
    }

    if (d->priorityCheck->isChecked())
    {
        meta.setIptcTagString("Iptc.Envelope.EnvelopePriority", QString::number(d->priorityCB->currentIndex()));
    }
    else if (d->priorityCheck->isValid())
    {
        meta.removeIptcTag("Iptc.Envelope.EnvelopePriority");
    }

    if (d->formatCheck->isChecked())
    {
        QString key;
        int i = 0;

        for (IPTCEnvelopePriv::FileFormatMap::Iterator it = d->fileFormatMap.begin();
                it != d->fileFormatMap.end(); ++it)
        {
            if (i == d->formatCB->currentIndex()) key = it.key();
            i++;
        }

        QString format  = key.section('-', 0, 0);
        QString version = key.section('-', -1);
        meta.setIptcTagString("Iptc.Envelope.FileFormat", format);
        meta.setIptcTagString("Iptc.Envelope.FileVersion", version);
    }
    else if (d->priorityCheck->isValid())
    {
        meta.removeIptcTag("Iptc.Envelope.FileFormat");
        meta.removeIptcTag("Iptc.Envelope.FileVersion");
    }

    if (d->dateSentCheck->isChecked())
    {
        meta.setIptcTagString("Iptc.Envelope.DateSent",
                              d->dateSentSel->date().toString(Qt::ISODate));
    }
    else
    {
        meta.removeIptcTag("Iptc.Envelope.DateSent");
    }

    if (d->timeSentCheck->isChecked())
    {
        meta.setIptcTagString("Iptc.Envelope.TimeSent",
                              d->timeSentSel->time().toString(Qt::ISODate) +
                              d->zoneSentSel->getTimeZone());
    }
    else
    {
        meta.removeIptcTag("Iptc.Envelope.TimeSent");
    }

    meta.setImageProgramId(QString("Kipi-plugins"), QString(kipiplugins_version));

    iptcData = meta.getIptc();
}
bool FlickrTalker::addPhoto(const QString& photoPath, const FPhotoInfo& info,
                            bool sendOriginal, bool rescale, int maxDim, int imageQuality)
{
    if (m_job)
    {
        m_job->kill();
        m_job = 0;
    }

    KUrl    url(m_uploadUrl);

    // We dont' want to modify url as such, we just used the KURL object for storing the query items.
    KUrl  url2("");
    QString path = photoPath;
    MPForm  form;

    form.addPair("auth_token", m_token, "text/plain");
    url2.addQueryItem("auth_token", m_token);

    form.addPair("api_key", m_apikey, "text/plain");
    url2.addQueryItem("api_key", m_apikey);

    QString ispublic = (info.is_public == 1) ? "1" : "0";
    form.addPair("is_public", ispublic, "text/plain");
    url2.addQueryItem("is_public", ispublic);

    QString isfamily = (info.is_family == 1) ? "1" : "0";
    form.addPair("is_family", isfamily, "text/plain");
    url2.addQueryItem("is_family", isfamily);

    QString isfriend = (info.is_friend == 1) ? "1" : "0";
    form.addPair("is_friend", isfriend, "text/plain");
    url2.addQueryItem("is_friend", isfriend);

    QString safetyLevel = QString::number(static_cast<int>(info.safety_level));
    form.addPair("safety_level", safetyLevel, "text/plain");
    url2.addQueryItem("safety_level", safetyLevel);

    QString contentType = QString::number(static_cast<int>(info.content_type));
    form.addPair("content_type", contentType, "text/plain");
    url2.addQueryItem("content_type", contentType);

    QString tags = "\"" + info.tags.join("\" \"") + "\"";

    if (tags.length() > 0)
    {
        form.addPair("tags", tags, "text/plain");
        url2.addQueryItem("tags", tags);
    }

    if (!info.title.isEmpty())
    {
        form.addPair("title", info.title, "text/plain");
        url2.addQueryItem("title", info.title);
    }

    if (!info.description.isEmpty())
    {
        form.addPair("description", info.description, "text/plain");
        url2.addQueryItem("description", info.description);
    }

    QString md5 = getApiSig(m_secret, url2);
    form.addPair("api_sig", md5, "text/plain");
    QImage image;

    // Check if RAW file.
    if (KPMetadata::isRawFile(photoPath))
    {
        KDcrawIface::KDcraw::loadDcrawPreview(image, photoPath);
    }
    else
    {
        image.load(photoPath);
    }

    if (!image.isNull())
    {
        path = KStandardDirs::locateLocal("tmp", QFileInfo(photoPath).baseName().trimmed() + ".jpg");

        if (sendOriginal)
        {
            QFile imgFile(photoPath);
            imgFile.copy(path);
        }
        else
        {
            if (rescale && (image.width() > maxDim || image.height() > maxDim))
                image = image.scaled(maxDim, maxDim, Qt::KeepAspectRatio,
                                     Qt::SmoothTransformation);

            image.save(path, "JPEG", imageQuality);
        }

        // Restore all metadata.

        KPMetadata meta;

        if (meta.load(photoPath))
        {
            meta.setImageDimensions(image.size());

            // NOTE: see B.K.O #153207: Flickr use IPTC keywords to create Tags in web interface
            //       As IPTC do not support UTF-8, we need to remove it.
            meta.removeIptcTag("Iptc.Application2.Keywords", false);

            meta.setImageProgramId(QString("Kipi-plugins"), QString(kipiplugins_version));
            meta.save(path);
        }
        else
        {
            kWarning() << "(flickrExport::Image doesn't have metadata)";
        }

        kDebug() << "Resizing and saving to temp file: " << path;
    }

    if (!form.addFile("photo", path))
    {
        return false;
    }

    form.finish();

    KIO::TransferJob* job = KIO::http_post(url, form.formData(), KIO::HideProgressInfo);
    job->addMetaData("content-type", form.contentType());

    connect(job, SIGNAL(data(KIO::Job*,QByteArray)),
            this, SLOT(data(KIO::Job*,QByteArray)));

    connect(job, SIGNAL(result(KJob*)),
            this, SLOT(slotResult(KJob*)));

    m_state = FE_ADDPHOTO;
    m_job   = job;
    m_buffer.resize(0);
    emit signalBusy(true);
    return true;
}